Fix lint warnings (#4019)
Clears the baseline of issues in our code, and resolves most of the straightforward warnings from the report
This commit is contained in:
parent
7dfc8790c7
commit
f99cb6d1d5
35 changed files with 154 additions and 252 deletions
|
|
@ -1,76 +0,0 @@
|
|||
/* Copyright 2017 Andrew Dawson
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
package com.keylesspalace.tusky.network;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import okio.BufferedSink;
|
||||
|
||||
public final class ProgressRequestBody extends RequestBody {
|
||||
private final InputStream content;
|
||||
private final long contentLength;
|
||||
private final UploadCallback uploadListener;
|
||||
private final MediaType mediaType;
|
||||
|
||||
private static final int DEFAULT_BUFFER_SIZE = 2048;
|
||||
|
||||
public interface UploadCallback {
|
||||
void onProgressUpdate(int percentage);
|
||||
}
|
||||
|
||||
public ProgressRequestBody(final InputStream content, long contentLength, final MediaType mediaType, final UploadCallback listener) {
|
||||
this.content = content;
|
||||
this.contentLength = contentLength;
|
||||
this.mediaType = mediaType;
|
||||
this.uploadListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return contentLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(@NonNull BufferedSink sink) throws IOException {
|
||||
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
long uploaded = 0;
|
||||
|
||||
try {
|
||||
int read;
|
||||
while ((read = content.read(buffer)) != -1) {
|
||||
uploadListener.onProgressUpdate((int)(100 * uploaded / contentLength));
|
||||
|
||||
uploaded += read;
|
||||
sink.write(buffer, 0, read);
|
||||
}
|
||||
|
||||
uploadListener.onProgressUpdate((int)(100 * uploaded / contentLength));
|
||||
} finally {
|
||||
content.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/* Copyright 2017 Andrew Dawson
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
package com.keylesspalace.tusky.network
|
||||
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.RequestBody
|
||||
import okio.BufferedSink
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
class ProgressRequestBody(private val content: InputStream, private val contentLength: Long, private val mediaType: MediaType, private val uploadListener: UploadCallback) : RequestBody() {
|
||||
fun interface UploadCallback {
|
||||
fun onProgressUpdate(percentage: Int)
|
||||
}
|
||||
|
||||
override fun contentType(): MediaType {
|
||||
return mediaType
|
||||
}
|
||||
|
||||
override fun contentLength(): Long {
|
||||
return contentLength
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun writeTo(sink: BufferedSink) {
|
||||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||
var uploaded: Long = 0
|
||||
|
||||
content.use { content ->
|
||||
var read: Int
|
||||
while (content.read(buffer).also { read = it } != -1) {
|
||||
uploadListener.onProgressUpdate((100 * uploaded / contentLength).toInt())
|
||||
uploaded += read.toLong()
|
||||
sink.write(buffer, 0, read)
|
||||
}
|
||||
uploadListener.onProgressUpdate((100 * uploaded / contentLength).toInt())
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val DEFAULT_BUFFER_SIZE = 2048
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue