main commit

Post-processing infrastructure
* remove interfaces with one implementation
* fix download resources with unknow length
* marquee style for ProgressDrawable
* "view details" option in mission context menu
* notification for finished downloads
* postprocessing infrastructure: sub-missions, circular file, layers for layers of abstractions for Java IO streams
* Mp4 muxing (only DASH brand)
* WebM muxing
* Captions downloading
* alert dialog for overwrite existing downloads finished or not.

Misc changes
* delete SQLiteDownloadDataSource.java
* delete DownloadMissionSQLiteHelper.java
* implement Localization from #114

Misc fixes (this branch)
* restore old mission listeners variables. Prevents registered listeners get de-referenced on low-end devices
* DownloadManagerService.checkForRunningMission() now return false if the mission has a error.
* use Intent.FLAG_ACTIVITY_NEW_TASK when launching an activity from gigaget threads (apparently it is required in old versions of android)

More changes
* proper error handling "infrastructure"
* queue instead of multiple downloads
* move serialized pending downloads (.giga files) to app data
* stop downloads when swicthing to mobile network (never works, see 2nd point)
* save the thread count for next downloads
* a lot of incoherences fixed
* delete DownloadManagerTest.java (too many changes to keep this file updated)
This commit is contained in:
kapodamy 2018-09-23 15:12:23 -03:00
parent 45fea983b9
commit 5825843f68
48 changed files with 4379 additions and 1119 deletions

View file

@ -3,10 +3,11 @@ package us.shandian.giga.util;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import org.schabi.newpipe.R;
@ -21,12 +22,14 @@ import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
public class Utility {
public enum FileType {
VIDEO,
MUSIC,
SUBTITLE,
UNKNOWN
}
@ -54,41 +57,32 @@ public class Utility {
}
}
public static void writeToFile(@NonNull String fileName, @NonNull Serializable serializable) {
ObjectOutputStream objectOutputStream = null;
public static void writeToFile(@NonNull File file, @NonNull Serializable serializable) {
try {
objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
objectOutputStream.writeObject(serializable);
} catch (Exception e) {
//nothing to do
} finally {
if(objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (Exception e) {
//nothing to do
}
}
}
//nothing to do
}
@Nullable
@SuppressWarnings("unchecked")
public static <T> T readFromFile(String file) {
T object = null;
public static <T> T readFromFile(File file) {
T object;
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
object = (T) objectInputStream.readObject();
} catch (Exception e) {
//nothing to do
object = null;
}
if(objectInputStream != null){
if (objectInputStream != null) {
try {
objectInputStream .close();
objectInputStream.close();
} catch (Exception e) {
//nothing to do
}
@ -119,39 +113,68 @@ public class Utility {
}
}
public static FileType getFileType(String file) {
if (file.endsWith(".mp3") || file.endsWith(".wav") || file.endsWith(".flac") || file.endsWith(".m4a")) {
public static FileType getFileType(char kind, String file) {
switch (kind) {
case 'v':
return FileType.VIDEO;
case 'a':
return FileType.MUSIC;
case 's':
return FileType.SUBTITLE;
//default '?':
}
if (file.endsWith(".srt") || file.endsWith(".vtt") || file.endsWith(".ssa")) {
return FileType.SUBTITLE;
} else if (file.endsWith(".mp3") || file.endsWith(".wav") || file.endsWith(".flac") || file.endsWith(".m4a") || file.endsWith(".opus")) {
return FileType.MUSIC;
} else if (file.endsWith(".mp4") || file.endsWith(".mpeg") || file.endsWith(".rm") || file.endsWith(".rmvb")
|| file.endsWith(".flv") || file.endsWith(".webp") || file.endsWith(".webm")) {
return FileType.VIDEO;
} else {
return FileType.UNKNOWN;
}
return FileType.UNKNOWN;
}
@ColorRes
public static int getBackgroundForFileType(FileType type) {
@ColorInt
public static int getBackgroundForFileType(Context ctx, FileType type) {
int colorRes;
switch (type) {
case MUSIC:
return R.color.audio_left_to_load_color;
colorRes = R.color.audio_left_to_load_color;
break;
case VIDEO:
return R.color.video_left_to_load_color;
colorRes = R.color.video_left_to_load_color;
break;
case SUBTITLE:
colorRes = R.color.subtitle_left_to_load_color;
break;
default:
return R.color.gray;
colorRes = R.color.gray;
}
return ContextCompat.getColor(ctx, colorRes);
}
@ColorRes
public static int getForegroundForFileType(FileType type) {
@ColorInt
public static int getForegroundForFileType(Context ctx, FileType type) {
int colorRes;
switch (type) {
case MUSIC:
return R.color.audio_already_load_color;
colorRes = R.color.audio_already_load_color;
break;
case VIDEO:
return R.color.video_already_load_color;
colorRes = R.color.video_already_load_color;
break;
case SUBTITLE:
colorRes = R.color.subtitle_already_load_color;
break;
default:
return R.color.gray;
colorRes = R.color.gray;
break;
}
return ContextCompat.getColor(ctx, colorRes);
}
@DrawableRes
@ -161,6 +184,8 @@ public class Utility {
return R.drawable.music;
case VIDEO:
return R.drawable.video;
case SUBTITLE:
return R.drawable.subtitle;
default:
return R.drawable.video;
}
@ -168,12 +193,18 @@ public class Utility {
public static void copyToClipboard(Context context, String str) {
ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (cm == null) {
Toast.makeText(context, R.string.permission_denied, Toast.LENGTH_LONG).show();
return;
}
cm.setPrimaryClip(ClipData.newPlainText("text", str));
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show();
}
public static String checksum(String path, String algorithm) {
MessageDigest md = null;
MessageDigest md;
try {
md = MessageDigest.getInstance(algorithm);
@ -181,7 +212,7 @@ public class Utility {
throw new RuntimeException(e);
}
FileInputStream i = null;
FileInputStream i;
try {
i = new FileInputStream(path);
@ -190,14 +221,14 @@ public class Utility {
}
byte[] buf = new byte[1024];
int len = 0;
int len;
try {
while ((len = i.read(buf)) != -1) {
md.update(buf, 0, len);
}
} catch (IOException ignored) {
} catch (IOException e) {
// nothing to do
}
byte[] digest = md.digest();
@ -211,4 +242,16 @@ public class Utility {
return sb.toString();
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static boolean mkdir(File path, boolean allDirs) {
if (path.exists()) return true;
if (allDirs)
path.mkdirs();
else
path.mkdir();
return path.exists();
}
}