From 917f4595690f2ccd67cc8395caa41ca02ff4ea4e Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 11 Dec 2020 07:29:24 +0530 Subject: [PATCH] Use a notification instead of a ProgressDialog in MissionAdapter. --- app/src/main/java/org/schabi/newpipe/App.java | 15 ++++++-- .../giga/ui/adapter/MissionAdapter.java | 37 ++++++++++--------- app/src/main/res/values/strings.xml | 4 ++ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index de401d4f5..e6dce4d67 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -242,8 +242,9 @@ public class App extends MultiDexApplication { String name = getString(R.string.notification_channel_name); String description = getString(R.string.notification_channel_description); - // Keep this below DEFAULT to avoid making noise on every notification update - final int importance = NotificationManager.IMPORTANCE_LOW; + // Keep this below DEFAULT to avoid making noise on every notification update for the main + // and update channels + int importance = NotificationManager.IMPORTANCE_LOW; final NotificationChannel mainChannel = new NotificationChannel(id, name, importance); mainChannel.setDescription(description); @@ -255,9 +256,17 @@ public class App extends MultiDexApplication { final NotificationChannel appUpdateChannel = new NotificationChannel(id, name, importance); appUpdateChannel.setDescription(description); + id = getString(R.string.hash_channel_id); + name = getString(R.string.hash_channel_name); + description = getString(R.string.hash_channel_description); + importance = NotificationManager.IMPORTANCE_HIGH; + + final NotificationChannel hashChannel = new NotificationChannel(id, name, importance); + hashChannel.setDescription(description); + final NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannels(Arrays.asList(mainChannel, - appUpdateChannel)); + appUpdateChannel, hashChannel)); } protected boolean isDisposedRxExceptionsReported() { diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index be7d78299..f102206c1 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -1,7 +1,7 @@ package us.shandian.giga.ui.adapter; import android.annotation.SuppressLint; -import android.app.ProgressDialog; +import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.graphics.Color; @@ -26,6 +26,8 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.core.view.ViewCompat; import androidx.recyclerview.widget.DiffUtil; @@ -91,6 +93,7 @@ public class MissionAdapter extends Adapter implements Handler.Callb private static final String DEFAULT_MIME_TYPE = "*/*"; private static final String UNDEFINED_ETA = "--:--"; + private static final int HASH_NOTIFICATION_ID = 123790; static { ALGORITHMS.put(R.id.md5, "MD5"); @@ -678,28 +681,28 @@ public class MissionAdapter extends Adapter implements Handler.Callb return true; case R.id.md5: case R.id.sha1: - ProgressDialog progressDialog = null; - if (mContext != null) { - // Create dialog - progressDialog = new ProgressDialog(mContext); - progressDialog.setCancelable(false); - progressDialog.setMessage(mContext.getString(R.string.msg_wait)); - progressDialog.show(); - } - final ProgressDialog finalProgressDialog = progressDialog; + final NotificationManager notificationManager + = ContextCompat.getSystemService(mContext, NotificationManager.class); + final NotificationCompat.Builder progressNotificationBuilder + = new NotificationCompat.Builder(mContext, + mContext.getString(R.string.hash_channel_id)) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setSmallIcon(R.drawable.ic_newpipe_triangle_white) + .setContentTitle(mContext.getString(R.string.msg_calculating_hash)) + .setContentText(mContext.getString(R.string.msg_wait)) + .setProgress(0, 0, true) + .setOngoing(true); + + notificationManager.notify(HASH_NOTIFICATION_ID, progressNotificationBuilder + .build()); final StoredFileHelper storage = h.item.mission.storage; compositeDisposable.add( Observable.fromCallable(() -> Utility.checksum(storage, ALGORITHMS.get(id))) .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { - if (finalProgressDialog != null) { - Utility.copyToClipboard(finalProgressDialog.getContext(), - result); - if (mContext != null) { - finalProgressDialog.dismiss(); - } - } + Utility.copyToClipboard(mContext, result); + notificationManager.cancel(HASH_NOTIFICATION_ID); }) ); return true; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f96518ccf..bc70e793b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -195,6 +195,9 @@ newpipeAppUpdate App Update Notification Notifications for new NewPipe version + newpipeHash + Video Hash Notification + Notifications for video hashing progress [Unknown] Toggle Orientation Switch to Background @@ -347,6 +350,7 @@ Malformed URL or Internet not available NewPipe Downloading Tap for details + Calculating hash Please wait… Copied to clipboard Please define a download folder later in settings