Make DeleteTask notification quieter on some devices(Fixed #2528) (#2538)

As commit ab4fca5e does, this commit fixed the repeated notification
alarms in DeleteTask.class.

Since progress indication in notification can be cleared by calling
.setProgress(0,0,false) on notificationBuilder(As shown in DeleteTask.class).
This commit also refactored notification related code in UploadService.class.
Make progress and failed notification uses the same notificationBuilder.
This commit is contained in:
Zhao Gang 2019-03-12 19:19:09 +08:00 committed by neslihanturan
parent 52724b8be0
commit 62c14ecb66
2 changed files with 19 additions and 28 deletions

View file

@ -55,7 +55,8 @@ public class DeleteTask extends AsyncTask<Void, Integer, Boolean> {
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder( notificationBuilder = new NotificationCompat.Builder(
context, context,
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL); CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL)
.setOnlyAlertOnce(true);
Toast toast = new Toast(context); Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER,0,0); toast.setGravity(Gravity.CENTER,0,0);
toast = Toast.makeText(context,"Trying to nominate "+media.getDisplayTitle()+ " for deletion",Toast.LENGTH_SHORT); toast = Toast.makeText(context,"Trying to nominate "+media.getDisplayTitle()+ " for deletion",Toast.LENGTH_SHORT);

View file

@ -55,8 +55,7 @@ public class UploadService extends HandlerService<Contribution> {
@Inject ContributionDao contributionDao; @Inject ContributionDao contributionDao;
private NotificationManager notificationManager; private NotificationManager notificationManager;
private NotificationCompat.Builder curProgressNotification; private NotificationCompat.Builder curNotification;
private NotificationCompat.Builder curFailedNotification;
private int toUpload; private int toUpload;
/** /**
@ -95,19 +94,19 @@ public class UploadService extends HandlerService<Contribution> {
public void onProgress(long transferred, long total) { public void onProgress(long transferred, long total) {
Timber.d("Uploaded %d of %d", transferred, total); Timber.d("Uploaded %d of %d", transferred, total);
if (!notificationTitleChanged) { if (!notificationTitleChanged) {
curProgressNotification.setContentTitle(notificationProgressTitle); curNotification.setContentTitle(notificationProgressTitle);
notificationTitleChanged = true; notificationTitleChanged = true;
contribution.setState(Contribution.STATE_IN_PROGRESS); contribution.setState(Contribution.STATE_IN_PROGRESS);
} }
if (transferred == total) { if (transferred == total) {
// Completed! // Completed!
curProgressNotification.setContentTitle(notificationFinishingTitle) curNotification.setContentTitle(notificationFinishingTitle)
.setTicker(notificationFinishingTitle) .setTicker(notificationFinishingTitle)
.setProgress(0, 100, true); .setProgress(0, 100, true);
} else { } else {
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false); curNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
} }
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build());
contribution.setTransferred(transferred); contribution.setTransferred(transferred);
contributionDao.save(contribution); contributionDao.save(contribution);
@ -126,8 +125,7 @@ public class UploadService extends HandlerService<Contribution> {
super.onCreate(); super.onCreate();
CommonsApplication.createNotificationChannel(getApplicationContext()); CommonsApplication.createNotificationChannel(getApplicationContext());
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
curProgressNotification = getProgressNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL); curNotification = getNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
curFailedNotification = getFailedNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
} }
@Override @Override
@ -151,10 +149,10 @@ public class UploadService extends HandlerService<Contribution> {
contribution.setTransferred(0); contribution.setTransferred(0);
contributionDao.save(contribution); contributionDao.save(contribution);
toUpload++; toUpload++;
if (curProgressNotification != null && toUpload != 1) { if (curNotification != null && toUpload != 1) {
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)); curNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
Timber.d("%d uploads left", toUpload); Timber.d("%d uploads left", toUpload);
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build());
} }
super.queue(what, contribution); super.queue(what, contribution);
@ -184,16 +182,6 @@ public class UploadService extends HandlerService<Contribution> {
return START_REDELIVER_INTENT; return START_REDELIVER_INTENT;
} }
private NotificationCompat.Builder getProgressNotificationBuilder(String channelId) {
return getNotificationBuilder(channelId)
.setProgress(100, 0, true)
.setOngoing(true);
}
private NotificationCompat.Builder getFailedNotificationBuilder(String channelId) {
return getNotificationBuilder(channelId);
}
@SuppressLint("StringFormatInvalid") @SuppressLint("StringFormatInvalid")
private NotificationCompat.Builder getNotificationBuilder(String channelId) { private NotificationCompat.Builder getNotificationBuilder(String channelId) {
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true) return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
@ -201,8 +189,9 @@ public class UploadService extends HandlerService<Contribution> {
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true) .setAutoCancel(true)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.setProgress(100, 0, true)
.setOngoing(true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0)); .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));
} }
private void uploadContribution(Contribution contribution) { private void uploadContribution(Contribution contribution) {
@ -225,10 +214,10 @@ public class UploadService extends HandlerService<Contribution> {
} }
Timber.d("Before execution!"); Timber.d("Before execution!");
curProgressNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle())) curNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)) .setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle())); .setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build());
String filename = contribution.getFilename(); String filename = contribution.getFilename();
try { try {
@ -294,10 +283,11 @@ public class UploadService extends HandlerService<Contribution> {
@SuppressLint("StringFormatInvalid") @SuppressLint("StringFormatInvalid")
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void showFailedNotification(Contribution contribution) { private void showFailedNotification(Contribution contribution) {
curFailedNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle())) curNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
.setContentTitle(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle())) .setContentTitle(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
.setContentText(getString(R.string.upload_failed_notification_subtitle)); .setContentText(getString(R.string.upload_failed_notification_subtitle))
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curFailedNotification.build()); .setProgress(0, 0, false);
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curNotification.build());
contribution.setState(Contribution.STATE_FAILED); contribution.setState(Contribution.STATE_FAILED);
contributionDao.save(contribution); contributionDao.save(contribution);