mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-11-10 20:10:02 +00:00
Merge pull request #8767 from Isira-Seneviratne/Use_ByteString
Use Okio's ByteString for download checksums.
This commit is contained in:
commit
7864521cb4
@ -1,5 +1,25 @@
|
|||||||
package us.shandian.giga.ui.adapter;
|
package us.shandian.giga.ui.adapter;
|
||||||
|
|
||||||
|
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
||||||
|
import static android.content.Intent.FLAG_GRANT_PREFIX_URI_PERMISSION;
|
||||||
|
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_CONNECT_HOST;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_FILE_CREATION;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_HTTP_NO_CONTENT;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_INSUFFICIENT_STORAGE;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_PATH_CREATION;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_PERMISSION_DENIED;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_HOLD;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_STOPPED;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_PROGRESS_LOST;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_RESOURCE_GONE;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_SSL_EXCEPTION;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_TIMEOUT;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_EXCEPTION;
|
||||||
|
import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_HOST;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@ -10,7 +30,6 @@ import android.os.Build;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
|
||||||
import android.view.HapticFeedbackConstants;
|
import android.view.HapticFeedbackConstants;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -38,10 +57,11 @@ import com.google.android.material.snackbar.Snackbar;
|
|||||||
|
|
||||||
import org.schabi.newpipe.BuildConfig;
|
import org.schabi.newpipe.BuildConfig;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.error.ErrorUtil;
|
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
|
||||||
import org.schabi.newpipe.error.ErrorInfo;
|
import org.schabi.newpipe.error.ErrorInfo;
|
||||||
|
import org.schabi.newpipe.error.ErrorUtil;
|
||||||
import org.schabi.newpipe.error.UserAction;
|
import org.schabi.newpipe.error.UserAction;
|
||||||
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
|
import org.schabi.newpipe.streams.io.StoredFileHelper;
|
||||||
import org.schabi.newpipe.util.Localization;
|
import org.schabi.newpipe.util.Localization;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
import org.schabi.newpipe.util.external_communication.ShareUtils;
|
import org.schabi.newpipe.util.external_communication.ShareUtils;
|
||||||
@ -60,47 +80,19 @@ import us.shandian.giga.get.DownloadMission;
|
|||||||
import us.shandian.giga.get.FinishedMission;
|
import us.shandian.giga.get.FinishedMission;
|
||||||
import us.shandian.giga.get.Mission;
|
import us.shandian.giga.get.Mission;
|
||||||
import us.shandian.giga.get.MissionRecoveryInfo;
|
import us.shandian.giga.get.MissionRecoveryInfo;
|
||||||
import org.schabi.newpipe.streams.io.StoredFileHelper;
|
|
||||||
import us.shandian.giga.service.DownloadManager;
|
import us.shandian.giga.service.DownloadManager;
|
||||||
import us.shandian.giga.service.DownloadManagerService;
|
import us.shandian.giga.service.DownloadManagerService;
|
||||||
import us.shandian.giga.ui.common.Deleter;
|
import us.shandian.giga.ui.common.Deleter;
|
||||||
import us.shandian.giga.ui.common.ProgressDrawable;
|
import us.shandian.giga.ui.common.ProgressDrawable;
|
||||||
import us.shandian.giga.util.Utility;
|
import us.shandian.giga.util.Utility;
|
||||||
|
|
||||||
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
|
||||||
import static android.content.Intent.FLAG_GRANT_PREFIX_URI_PERMISSION;
|
|
||||||
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_CONNECT_HOST;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_FILE_CREATION;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_HTTP_NO_CONTENT;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_INSUFFICIENT_STORAGE;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_PATH_CREATION;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_PERMISSION_DENIED;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_HOLD;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_STOPPED;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_PROGRESS_LOST;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_RESOURCE_GONE;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_SSL_EXCEPTION;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_TIMEOUT;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_EXCEPTION;
|
|
||||||
import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_HOST;
|
|
||||||
|
|
||||||
public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callback {
|
public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callback {
|
||||||
private static final SparseArray<String> ALGORITHMS = new SparseArray<>();
|
|
||||||
private static final String TAG = "MissionAdapter";
|
private static final String TAG = "MissionAdapter";
|
||||||
private static final String UNDEFINED_PROGRESS = "--.-%";
|
private static final String UNDEFINED_PROGRESS = "--.-%";
|
||||||
private static final String DEFAULT_MIME_TYPE = "*/*";
|
private static final String DEFAULT_MIME_TYPE = "*/*";
|
||||||
private static final String UNDEFINED_ETA = "--:--";
|
private static final String UNDEFINED_ETA = "--:--";
|
||||||
|
|
||||||
private static final int HASH_NOTIFICATION_ID = 123790;
|
private static final int HASH_NOTIFICATION_ID = 123790;
|
||||||
|
|
||||||
static {
|
|
||||||
ALGORITHMS.put(R.id.md5, "MD5");
|
|
||||||
ALGORITHMS.put(R.id.sha1, "SHA1");
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final LayoutInflater mInflater;
|
private final LayoutInflater mInflater;
|
||||||
private final DownloadManager mDownloadManager;
|
private final DownloadManager mDownloadManager;
|
||||||
@ -697,7 +689,7 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
|
|||||||
.build());
|
.build());
|
||||||
final StoredFileHelper storage = h.item.mission.storage;
|
final StoredFileHelper storage = h.item.mission.storage;
|
||||||
compositeDisposable.add(
|
compositeDisposable.add(
|
||||||
Observable.fromCallable(() -> Utility.checksum(storage, ALGORITHMS.get(id)))
|
Observable.fromCallable(() -> Utility.checksum(storage, id))
|
||||||
.subscribeOn(Schedulers.computation())
|
.subscribeOn(Schedulers.computation())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(result -> {
|
.subscribe(result -> {
|
||||||
|
@ -13,8 +13,11 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.streams.io.SharpStream;
|
import org.schabi.newpipe.streams.io.SharpInputStream;
|
||||||
|
import org.schabi.newpipe.streams.io.StoredFileHelper;
|
||||||
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -25,11 +28,9 @@ import java.io.ObjectInputStream;
|
|||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.schabi.newpipe.streams.io.StoredFileHelper;
|
import okio.ByteString;
|
||||||
|
|
||||||
public class Utility {
|
public class Utility {
|
||||||
|
|
||||||
@ -203,44 +204,18 @@ public class Utility {
|
|||||||
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String checksum(StoredFileHelper source, String algorithm) {
|
public static String checksum(final StoredFileHelper source, final int algorithmId)
|
||||||
MessageDigest md;
|
throws IOException {
|
||||||
|
ByteString byteString;
|
||||||
try {
|
try (var inputStream = new SharpInputStream(source.getStream())) {
|
||||||
md = MessageDigest.getInstance(algorithm);
|
byteString = ByteString.of(Util.toByteArray(inputStream));
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
if (algorithmId == R.id.md5) {
|
||||||
SharpStream i;
|
byteString = byteString.md5();
|
||||||
|
} else if (algorithmId == R.id.sha1) {
|
||||||
try {
|
byteString = byteString.sha1();
|
||||||
i = source.getStream();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
return byteString.hex();
|
||||||
byte[] buf = new byte[1024];
|
|
||||||
int len;
|
|
||||||
|
|
||||||
try {
|
|
||||||
while ((len = i.read(buf)) != -1) {
|
|
||||||
md.update(buf, 0, len);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// nothing to do
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] digest = md.digest();
|
|
||||||
|
|
||||||
// HEX
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for (byte b : digest) {
|
|
||||||
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
|
Loading…
Reference in New Issue
Block a user