code cleanup

* migrate few annotations to androidx
* mission recovery: better error handling (except StreamExtractor.getErrorMessage() method always returns an error)
* post-processing: more detailed progress

[file specific changes]

DownloadMission.java
* remove redundant/boilerplate code (again)
* make few variables volatile
* better file "length" approximation
* use "done" variable to count the amount of bytes downloaded (simplify percent calc in UI code)

Postprocessing.java
* if case of error use "ERROR_POSTPROCESSING" instead of "ERROR_UNKNOWN_EXCEPTION"
* simplify source stream init

DownloadManager.java
* move all "service message sending" code to DownloadMission
* remove not implemented method "notifyUserPendingDownloads()" also his unused strings

DownloadManagerService.java
* use START_STICKY instead of START_NOT_STICKY
* simplify addMissionEventListener()/removeMissionEventListener() methods (always are called from the main thread)

Deleter.java
* better method definition

MissionAdapter.java
* better method definition
* code cleanup
* the UI is now refreshed every 750ms
* simplify download progress calculation
* indicates if the download is actually recovering
* smooth download speed measure
* show estimated remain time

MainFragment.java:
* check if viewPager is null (issued by "Apply changes" feature of Android Studio)
This commit is contained in:
kapodamy 2019-10-09 23:49:23 -03:00
parent 763995d4c9
commit e6d9d8e26d
53 changed files with 554 additions and 622 deletions

View file

@ -4,13 +4,14 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import org.schabi.newpipe.R;
import org.schabi.newpipe.streams.io.SharpStream;
@ -26,6 +27,7 @@ import java.io.Serializable;
import java.net.HttpURLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import us.shandian.giga.io.StoredFileHelper;
@ -39,26 +41,28 @@ public class Utility {
}
public static String formatBytes(long bytes) {
Locale locale = Locale.getDefault();
if (bytes < 1024) {
return String.format("%d B", bytes);
return String.format(locale, "%d B", bytes);
} else if (bytes < 1024 * 1024) {
return String.format("%.2f kB", bytes / 1024d);
return String.format(locale, "%.2f kB", bytes / 1024d);
} else if (bytes < 1024 * 1024 * 1024) {
return String.format("%.2f MB", bytes / 1024d / 1024d);
return String.format(locale, "%.2f MB", bytes / 1024d / 1024d);
} else {
return String.format("%.2f GB", bytes / 1024d / 1024d / 1024d);
return String.format(locale, "%.2f GB", bytes / 1024d / 1024d / 1024d);
}
}
public static String formatSpeed(float speed) {
public static String formatSpeed(double speed) {
Locale locale = Locale.getDefault();
if (speed < 1024) {
return String.format("%.2f B/s", speed);
return String.format(locale, "%.2f B/s", speed);
} else if (speed < 1024 * 1024) {
return String.format("%.2f kB/s", speed / 1024);
return String.format(locale, "%.2f kB/s", speed / 1024);
} else if (speed < 1024 * 1024 * 1024) {
return String.format("%.2f MB/s", speed / 1024 / 1024);
return String.format(locale, "%.2f MB/s", speed / 1024 / 1024);
} else {
return String.format("%.2f GB/s", speed / 1024 / 1024 / 1024);
return String.format(locale, "%.2f GB/s", speed / 1024 / 1024 / 1024);
}
}
@ -188,12 +192,11 @@ public class Utility {
switch (type) {
case MUSIC:
return R.drawable.music;
default:
case VIDEO:
return R.drawable.video;
case SUBTITLE:
return R.drawable.subtitle;
default:
return R.drawable.video;
}
}
@ -274,4 +277,25 @@ public class Utility {
return -1;
}
private static String pad(int number) {
return number < 10 ? ("0" + number) : String.valueOf(number);
}
public static String stringifySeconds(double seconds) {
int h = (int) Math.floor(seconds / 3600);
int m = (int) Math.floor((seconds - (h * 3600)) / 60);
int s = (int) (seconds - (h * 3600) - (m * 60));
String str = "";
if (h < 1 && m < 1) {
str = "00:";
} else {
if (h > 0) str = pad(h) + ":";
if (m > 0) str += pad(m) + ":";
}
return str + pad(s);
}
}