Merged the latest changes
This commit is contained in:
commit
d2aaa6f691
1254 changed files with 39193 additions and 18652 deletions
|
|
@ -6,8 +6,8 @@ import android.system.ErrnoException;
|
|||
import android.system.OsConstants;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.schabi.newpipe.DownloaderImpl;
|
||||
|
||||
|
|
@ -223,6 +223,7 @@ public class DownloadMission extends Mission {
|
|||
conn.setInstanceFollowRedirects(true);
|
||||
conn.setRequestProperty("User-Agent", DownloaderImpl.USER_AGENT);
|
||||
conn.setRequestProperty("Accept", "*/*");
|
||||
conn.setRequestProperty("Accept-Encoding", "*");
|
||||
|
||||
if (headRequest) conn.setRequestMethod("HEAD");
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ public class DownloadRunnable extends Thread {
|
|||
Log.d(TAG, mId + ":acquired block at position=" + block.position + " done=" + block.done);
|
||||
}
|
||||
|
||||
long start = block.position * DownloadMission.BLOCK_SIZE;
|
||||
long start = (long)block.position * DownloadMission.BLOCK_SIZE;
|
||||
long end = start + DownloadMission.BLOCK_SIZE - 1;
|
||||
|
||||
start += block.done;
|
||||
|
|
|
|||
|
|
@ -6,9 +6,10 @@ import android.database.Cursor;
|
|||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.net.Uri;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ public class ChunkFileInputStream extends SharpStream {
|
|||
|
||||
@Override
|
||||
public long available() {
|
||||
return (int) (length - position);
|
||||
return length - position;
|
||||
}
|
||||
|
||||
@SuppressWarnings("EmptyCatchBlock")
|
||||
|
|
|
|||
|
|
@ -221,7 +221,7 @@ public class CircularFileWriter extends SharpStream {
|
|||
available = out.length - offsetOut;
|
||||
}
|
||||
|
||||
int length = Math.min(len, (int) available);
|
||||
int length = Math.min(len, (int) Math.min(Integer.MAX_VALUE, available));
|
||||
out.write(b, off, length);
|
||||
|
||||
len -= length;
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@ package us.shandian.giga.io;
|
|||
import android.content.ContentResolver;
|
||||
import android.net.Uri;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.schabi.newpipe.streams.io.SharpStream;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import android.database.Cursor;
|
|||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.DocumentsContract;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
|
|
|
|||
|
|
@ -7,10 +7,11 @@ import android.content.Intent;
|
|||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.DocumentsContract;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import org.schabi.newpipe.streams.io.SharpStream;
|
||||
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ public abstract class Postprocessing implements Serializable {
|
|||
|
||||
private transient DownloadMission mission;
|
||||
|
||||
private File tempFile;
|
||||
private transient File tempFile;
|
||||
|
||||
Postprocessing(boolean reserveSpace, boolean worksOnSameFile, String algorithmName) {
|
||||
this.reserveSpace = reserveSpace;
|
||||
|
|
@ -95,8 +95,12 @@ public abstract class Postprocessing implements Serializable {
|
|||
|
||||
public void cleanupTemporalDir() {
|
||||
if (tempFile != null && tempFile.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
tempFile.delete();
|
||||
try {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
tempFile.delete();
|
||||
} catch (Exception e) {
|
||||
// nothing to do
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,15 +2,10 @@ package us.shandian.giga.postprocessing;
|
|||
|
||||
import android.util.Log;
|
||||
|
||||
import org.schabi.newpipe.streams.SubtitleConverter;
|
||||
import org.schabi.newpipe.streams.SrtFromTtmlWriter;
|
||||
import org.schabi.newpipe.streams.io.SharpStream;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
/**
|
||||
* @author kapodamy
|
||||
|
|
@ -27,33 +22,16 @@ class TtmlConverter extends Postprocessing {
|
|||
int process(SharpStream out, SharpStream... sources) throws IOException {
|
||||
// check if the subtitle is already in srt and copy, this should never happen
|
||||
String format = getArgumentAt(0, null);
|
||||
boolean ignoreEmptyFrames = getArgumentAt(1, "true").equals("true");
|
||||
|
||||
if (format == null || format.equals("ttml")) {
|
||||
SubtitleConverter ttmlDumper = new SubtitleConverter();
|
||||
SrtFromTtmlWriter writer = new SrtFromTtmlWriter(out, ignoreEmptyFrames);
|
||||
|
||||
try {
|
||||
ttmlDumper.dumpTTML(
|
||||
sources[0],
|
||||
out,
|
||||
getArgumentAt(1, "true").equals("true"),
|
||||
getArgumentAt(2, "true").equals("true")
|
||||
);
|
||||
writer.build(sources[0]);
|
||||
} catch (Exception err) {
|
||||
Log.e(TAG, "subtitle parse failed", err);
|
||||
|
||||
if (err instanceof IOException) {
|
||||
return 1;
|
||||
} else if (err instanceof ParseException) {
|
||||
return 2;
|
||||
} else if (err instanceof SAXException) {
|
||||
return 3;
|
||||
} else if (err instanceof ParserConfigurationException) {
|
||||
return 4;
|
||||
} else if (err instanceof XPathExpressionException) {
|
||||
return 7;
|
||||
}
|
||||
|
||||
return 8;
|
||||
return err instanceof IOException ? 1 : 8;
|
||||
}
|
||||
|
||||
return OK_RESULT;
|
||||
|
|
|
|||
|
|
@ -139,6 +139,9 @@ public class DownloadManager {
|
|||
Log.d(TAG, "Loading pending downloads from directory: " + mPendingMissionsDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
File tempDir = pickAvailableTemporalDir(ctx);
|
||||
Log.i(TAG, "using '" + tempDir + "' as temporal directory");
|
||||
|
||||
for (File sub : subs) {
|
||||
if (!sub.isFile()) continue;
|
||||
if (sub.getName().equals(".tmp")) continue;
|
||||
|
|
@ -184,7 +187,7 @@ public class DownloadManager {
|
|||
|
||||
if (mis.psAlgorithm != null) {
|
||||
mis.psAlgorithm.cleanupTemporalDir();
|
||||
mis.psAlgorithm.setTemporalDir(pickAvailableTemporalDir(ctx));
|
||||
mis.psAlgorithm.setTemporalDir(tempDir);
|
||||
}
|
||||
|
||||
mis.metadata = sub;
|
||||
|
|
@ -513,13 +516,21 @@ public class DownloadManager {
|
|||
}
|
||||
|
||||
static File pickAvailableTemporalDir(@NonNull Context ctx) {
|
||||
if (isDirectoryAvailable(ctx.getExternalFilesDir(null)))
|
||||
return ctx.getExternalFilesDir(null);
|
||||
else if (isDirectoryAvailable(ctx.getFilesDir()))
|
||||
return ctx.getFilesDir();
|
||||
File dir = ctx.getExternalFilesDir(null);
|
||||
if (isDirectoryAvailable(dir)) return dir;
|
||||
|
||||
dir = ctx.getFilesDir();
|
||||
if (isDirectoryAvailable(dir)) return dir;
|
||||
|
||||
// this never should happen
|
||||
return ctx.getDir("tmp", Context.MODE_PRIVATE);
|
||||
dir = ctx.getDir("muxing_tmp", Context.MODE_PRIVATE);
|
||||
if (isDirectoryAvailable(dir)) return dir;
|
||||
|
||||
// fallback to cache dir
|
||||
dir = ctx.getCacheDir();
|
||||
if (isDirectoryAvailable(dir)) return dir;
|
||||
|
||||
throw new RuntimeException("Not temporal directories are available");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import android.app.Activity;
|
|||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
|
|
@ -35,6 +36,8 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||
import androidx.recyclerview.widget.RecyclerView.Adapter;
|
||||
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
|
||||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import org.schabi.newpipe.BuildConfig;
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.extractor.NewPipe;
|
||||
|
|
@ -46,6 +49,7 @@ import java.io.File;
|
|||
import java.lang.ref.WeakReference;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import us.shandian.giga.get.DownloadMission;
|
||||
import us.shandian.giga.get.FinishedMission;
|
||||
|
|
@ -104,8 +108,12 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||
private MenuItem mPauseButton;
|
||||
private View mEmptyMessage;
|
||||
private RecoverHelper mRecover;
|
||||
private View mView;
|
||||
private ArrayList<Mission> mHidden;
|
||||
private Snackbar mSnackbar;
|
||||
|
||||
private final Runnable rUpdater = this::updater;
|
||||
private final Runnable rDelete = this::deleteFinishedDownloads;
|
||||
|
||||
public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) {
|
||||
mContext = context;
|
||||
|
|
@ -122,6 +130,10 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||
|
||||
mDeleter = new Deleter(root, mContext, this, mDownloadManager, mIterator, mHandler);
|
||||
|
||||
mView = root;
|
||||
|
||||
mHidden = new ArrayList<>();
|
||||
|
||||
checkEmptyMessageVisibility();
|
||||
onResume();
|
||||
}
|
||||
|
|
@ -329,17 +341,7 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||
if (BuildConfig.DEBUG)
|
||||
Log.v(TAG, "Mime: " + mimeType + " package: " + BuildConfig.APPLICATION_ID + ".provider");
|
||||
|
||||
Uri uri;
|
||||
|
||||
if (mission.storage.isDirect()) {
|
||||
uri = FileProvider.getUriForFile(
|
||||
mContext,
|
||||
BuildConfig.APPLICATION_ID + ".provider",
|
||||
new File(URI.create(mission.storage.getUri().toString()))
|
||||
);
|
||||
} else {
|
||||
uri = mission.storage.getUri();
|
||||
}
|
||||
Uri uri = resolveShareableUri(mission);
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
|
|
@ -367,11 +369,30 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||
|
||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||
intent.setType(resolveMimeType(mission));
|
||||
intent.putExtra(Intent.EXTRA_STREAM, mission.storage.getUri());
|
||||
intent.putExtra(Intent.EXTRA_STREAM, resolveShareableUri(mission));
|
||||
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
|
||||
|
||||
mContext.startActivity(Intent.createChooser(intent, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an Uri which can be shared to other applications.
|
||||
*
|
||||
* @see <a href="https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed">
|
||||
* https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed</a>
|
||||
*/
|
||||
private Uri resolveShareableUri(Mission mission) {
|
||||
if (mission.storage.isDirect()) {
|
||||
return FileProvider.getUriForFile(
|
||||
mContext,
|
||||
BuildConfig.APPLICATION_ID + ".provider",
|
||||
new File(URI.create(mission.storage.getUri().toString()))
|
||||
);
|
||||
} else {
|
||||
return mission.storage.getUri();
|
||||
}
|
||||
}
|
||||
|
||||
private static String resolveMimeType(@NonNull Mission mission) {
|
||||
String mimeType;
|
||||
|
||||
|
|
@ -522,7 +543,7 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||
);
|
||||
}
|
||||
|
||||
builder.setNegativeButton(android.R.string.ok, (dialog, which) -> dialog.cancel())
|
||||
builder.setNegativeButton(R.string.finish, (dialog, which) -> dialog.cancel())
|
||||
.setTitle(mission.storage.getName())
|
||||
.create()
|
||||
.show();
|
||||
|
|
@ -557,9 +578,50 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||
);
|
||||
}
|
||||
|
||||
public void clearFinishedDownloads() {
|
||||
mDownloadManager.forgetFinishedDownloads();
|
||||
applyChanges();
|
||||
public void clearFinishedDownloads(boolean delete) {
|
||||
if (delete && mIterator.hasFinishedMissions() && mHidden.isEmpty()) {
|
||||
for (int i = 0; i < mIterator.getOldListSize(); i++) {
|
||||
FinishedMission mission = mIterator.getItem(i).mission instanceof FinishedMission ? (FinishedMission) mIterator.getItem(i).mission : null;
|
||||
if (mission != null) {
|
||||
mIterator.hide(mission);
|
||||
mHidden.add(mission);
|
||||
}
|
||||
}
|
||||
applyChanges();
|
||||
|
||||
String msg = String.format(mContext.getString(R.string.deleted_downloads), mHidden.size());
|
||||
mSnackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE);
|
||||
mSnackbar.setAction(R.string.undo, s -> {
|
||||
Iterator<Mission> i = mHidden.iterator();
|
||||
while (i.hasNext()) {
|
||||
mIterator.unHide(i.next());
|
||||
i.remove();
|
||||
}
|
||||
applyChanges();
|
||||
mHandler.removeCallbacks(rDelete);
|
||||
});
|
||||
mSnackbar.setActionTextColor(Color.YELLOW);
|
||||
mSnackbar.show();
|
||||
|
||||
mHandler.postDelayed(rDelete, 5000);
|
||||
} else if (!delete) {
|
||||
mDownloadManager.forgetFinishedDownloads();
|
||||
applyChanges();
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteFinishedDownloads() {
|
||||
if (mSnackbar != null) mSnackbar.dismiss();
|
||||
|
||||
Iterator<Mission> i = mHidden.iterator();
|
||||
while (i.hasNext()) {
|
||||
Mission mission = i.next();
|
||||
if (mission != null) {
|
||||
mDownloadManager.deleteMission(mission);
|
||||
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri()));
|
||||
}
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem option) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package us.shandian.giga.ui.common;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
|
||||
|
|
|
|||
|
|
@ -189,10 +189,12 @@ public class MissionsFragment extends Fragment {
|
|||
return true;
|
||||
case R.id.clear_list:
|
||||
AlertDialog.Builder prompt = new AlertDialog.Builder(mContext);
|
||||
prompt.setTitle(R.string.clear_finished_download);
|
||||
prompt.setTitle(R.string.clear_download_history);
|
||||
prompt.setMessage(R.string.confirm_prompt);
|
||||
prompt.setPositiveButton(android.R.string.ok, (dialog, which) -> mAdapter.clearFinishedDownloads());
|
||||
prompt.setNegativeButton(R.string.cancel, null);
|
||||
// Intentionally misusing button's purpose in order to achieve good order
|
||||
prompt.setNegativeButton(R.string.clear_download_history, (dialog, which) -> mAdapter.clearFinishedDownloads(false));
|
||||
prompt.setPositiveButton(R.string.delete_downloaded_files, (dialog, which) -> mAdapter.clearFinishedDownloads(true));
|
||||
prompt.setNeutralButton(R.string.cancel, null);
|
||||
prompt.create().show();
|
||||
return true;
|
||||
case R.id.start_downloads:
|
||||
|
|
@ -222,15 +224,9 @@ public class MissionsFragment extends Fragment {
|
|||
mList.setAdapter(mAdapter);
|
||||
|
||||
if (mSwitch != null) {
|
||||
boolean isLight = ThemeHelper.isLightThemeSelected(mContext);
|
||||
int icon;
|
||||
|
||||
if (mLinear)
|
||||
icon = isLight ? R.drawable.ic_grid_black_24dp : R.drawable.ic_grid_white_24dp;
|
||||
else
|
||||
icon = isLight ? R.drawable.ic_list_black_24dp : R.drawable.ic_list_white_24dp;
|
||||
|
||||
mSwitch.setIcon(icon);
|
||||
mSwitch.setIcon(mLinear
|
||||
? ThemeHelper.resolveResourceIdFromAttr(requireContext(), R.attr.ic_grid)
|
||||
: ThemeHelper.resolveResourceIdFromAttr(requireContext(), R.attr.ic_list));
|
||||
mSwitch.setTitle(mLinear ? R.string.grid : R.string.list);
|
||||
mPrefs.edit().putBoolean("linear", mLinear).apply();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -191,12 +191,12 @@ public class Utility {
|
|||
public static int getIconForFileType(FileType type) {
|
||||
switch (type) {
|
||||
case MUSIC:
|
||||
return R.drawable.music;
|
||||
return R.drawable.ic_headset_white_24dp;
|
||||
default:
|
||||
case VIDEO:
|
||||
return R.drawable.video;
|
||||
return R.drawable.ic_movie_white_24dp;
|
||||
case SUBTITLE:
|
||||
return R.drawable.subtitle;
|
||||
return R.drawable.ic_subtitles_white_24dp;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue