Use a notification instead of a ProgressDialog in MissionAdapter.

This commit is contained in:
Isira Seneviratne 2020-12-11 07:29:24 +05:30
parent 0ced9ba799
commit 917f459569
3 changed files with 36 additions and 20 deletions

View file

@ -242,8 +242,9 @@ public class App extends MultiDexApplication {
String name = getString(R.string.notification_channel_name); String name = getString(R.string.notification_channel_name);
String description = getString(R.string.notification_channel_description); String description = getString(R.string.notification_channel_description);
// Keep this below DEFAULT to avoid making noise on every notification update // Keep this below DEFAULT to avoid making noise on every notification update for the main
final int importance = NotificationManager.IMPORTANCE_LOW; // and update channels
int importance = NotificationManager.IMPORTANCE_LOW;
final NotificationChannel mainChannel = new NotificationChannel(id, name, importance); final NotificationChannel mainChannel = new NotificationChannel(id, name, importance);
mainChannel.setDescription(description); mainChannel.setDescription(description);
@ -255,9 +256,17 @@ public class App extends MultiDexApplication {
final NotificationChannel appUpdateChannel = new NotificationChannel(id, name, importance); final NotificationChannel appUpdateChannel = new NotificationChannel(id, name, importance);
appUpdateChannel.setDescription(description); 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); final NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannels(Arrays.asList(mainChannel, notificationManager.createNotificationChannels(Arrays.asList(mainChannel,
appUpdateChannel)); appUpdateChannel, hashChannel));
} }
protected boolean isDisposedRxExceptionsReported() { protected boolean isDisposedRxExceptionsReported() {

View file

@ -1,7 +1,7 @@
package us.shandian.giga.ui.adapter; package us.shandian.giga.ui.adapter;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.ProgressDialog; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
@ -26,6 +26,8 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
@ -91,6 +93,7 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
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;
static { static {
ALGORITHMS.put(R.id.md5, "MD5"); ALGORITHMS.put(R.id.md5, "MD5");
@ -678,28 +681,28 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
return true; return true;
case R.id.md5: case R.id.md5:
case R.id.sha1: case R.id.sha1:
ProgressDialog progressDialog = null; final NotificationManager notificationManager
if (mContext != null) { = ContextCompat.getSystemService(mContext, NotificationManager.class);
// Create dialog final NotificationCompat.Builder progressNotificationBuilder
progressDialog = new ProgressDialog(mContext); = new NotificationCompat.Builder(mContext,
progressDialog.setCancelable(false); mContext.getString(R.string.hash_channel_id))
progressDialog.setMessage(mContext.getString(R.string.msg_wait)); .setPriority(NotificationCompat.PRIORITY_HIGH)
progressDialog.show(); .setSmallIcon(R.drawable.ic_newpipe_triangle_white)
} .setContentTitle(mContext.getString(R.string.msg_calculating_hash))
final ProgressDialog finalProgressDialog = progressDialog; .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; final StoredFileHelper storage = h.item.mission.storage;
compositeDisposable.add( compositeDisposable.add(
Observable.fromCallable(() -> Utility.checksum(storage, ALGORITHMS.get(id))) Observable.fromCallable(() -> Utility.checksum(storage, ALGORITHMS.get(id)))
.subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { .subscribe(result -> {
if (finalProgressDialog != null) { Utility.copyToClipboard(mContext, result);
Utility.copyToClipboard(finalProgressDialog.getContext(), notificationManager.cancel(HASH_NOTIFICATION_ID);
result);
if (mContext != null) {
finalProgressDialog.dismiss();
}
}
}) })
); );
return true; return true;

View file

@ -195,6 +195,9 @@
<string name="app_update_notification_channel_id" translatable="false">newpipeAppUpdate</string> <string name="app_update_notification_channel_id" translatable="false">newpipeAppUpdate</string>
<string name="app_update_notification_channel_name">App Update Notification</string> <string name="app_update_notification_channel_name">App Update Notification</string>
<string name="app_update_notification_channel_description">Notifications for new NewPipe version</string> <string name="app_update_notification_channel_description">Notifications for new NewPipe version</string>
<string name="hash_channel_id" translatable="false">newpipeHash</string>
<string name="hash_channel_name">Video Hash Notification</string>
<string name="hash_channel_description">Notifications for video hashing progress</string>
<string name="unknown_content">[Unknown]</string> <string name="unknown_content">[Unknown]</string>
<string name="toggle_orientation">Toggle Orientation</string> <string name="toggle_orientation">Toggle Orientation</string>
<string name="switch_to_background">Switch to Background</string> <string name="switch_to_background">Switch to Background</string>
@ -347,6 +350,7 @@
<string name="msg_url_malform">Malformed URL or Internet not available</string> <string name="msg_url_malform">Malformed URL or Internet not available</string>
<string name="msg_running">NewPipe Downloading</string> <string name="msg_running">NewPipe Downloading</string>
<string name="msg_running_detail">Tap for details</string> <string name="msg_running_detail">Tap for details</string>
<string name="msg_calculating_hash">Calculating hash</string>
<string name="msg_wait">Please wait…</string> <string name="msg_wait">Please wait…</string>
<string name="msg_copied">Copied to clipboard</string> <string name="msg_copied">Copied to clipboard</string>
<string name="no_available_dir">Please define a download folder later in settings</string> <string name="no_available_dir">Please define a download folder later in settings</string>