Skip to content

Commit

Permalink
Make DeleteTask notification quieter on some devices(Fixed commons-ap…
Browse files Browse the repository at this point in the history
…p#2528) (commons-app#2538)

As commit ab4fca5 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.
  • Loading branch information
zhao-gang authored and neslihanturan committed Mar 12, 2019
1 parent 52724b8 commit 62c14ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/delete/DeleteTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ protected void onPreExecute() {
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(
context,
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL)
.setOnlyAlertOnce(true);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER,0,0);
toast = Toast.makeText(context,"Trying to nominate "+media.getDisplayTitle()+ " for deletion",Toast.LENGTH_SHORT);
Expand Down
44 changes: 17 additions & 27 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public class UploadService extends HandlerService<Contribution> {
@Inject ContributionDao contributionDao;

private NotificationManager notificationManager;
private NotificationCompat.Builder curProgressNotification;
private NotificationCompat.Builder curFailedNotification;
private NotificationCompat.Builder curNotification;
private int toUpload;

/**
Expand Down Expand Up @@ -95,19 +94,19 @@ private class NotificationUpdateProgressListener implements MediaWikiApi.Progres
public void onProgress(long transferred, long total) {
Timber.d("Uploaded %d of %d", transferred, total);
if (!notificationTitleChanged) {
curProgressNotification.setContentTitle(notificationProgressTitle);
curNotification.setContentTitle(notificationProgressTitle);
notificationTitleChanged = true;
contribution.setState(Contribution.STATE_IN_PROGRESS);
}
if (transferred == total) {
// Completed!
curProgressNotification.setContentTitle(notificationFinishingTitle)
curNotification.setContentTitle(notificationFinishingTitle)
.setTicker(notificationFinishingTitle)
.setProgress(0, 100, true);
} 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);
contributionDao.save(contribution);
Expand All @@ -126,8 +125,7 @@ public void onCreate() {
super.onCreate();
CommonsApplication.createNotificationChannel(getApplicationContext());
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
curProgressNotification = getProgressNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
curFailedNotification = getFailedNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
curNotification = getNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
}

@Override
Expand All @@ -151,10 +149,10 @@ public void queue(int what, Contribution contribution) {
contribution.setTransferred(0);
contributionDao.save(contribution);
toUpload++;
if (curProgressNotification != null && toUpload != 1) {
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
if (curNotification != null && toUpload != 1) {
curNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, 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);
Expand Down Expand Up @@ -184,25 +182,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
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")
private NotificationCompat.Builder getNotificationBuilder(String channelId) {
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setProgress(100, 0, true)
.setOngoing(true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));

}

private void uploadContribution(Contribution contribution) {
Expand All @@ -225,10 +214,10 @@ private void uploadContribution(Contribution contribution) {
}

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))
.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();
try {
Expand Down Expand Up @@ -294,10 +283,11 @@ private void uploadContribution(Contribution contribution) {
@SuppressLint("StringFormatInvalid")
@SuppressWarnings("deprecation")
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()))
.setContentText(getString(R.string.upload_failed_notification_subtitle));
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curFailedNotification.build());
.setContentText(getString(R.string.upload_failed_notification_subtitle))
.setProgress(0, 0, false);
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curNotification.build());

contribution.setState(Contribution.STATE_FAILED);
contributionDao.save(contribution);
Expand Down

0 comments on commit 62c14ec

Please sign in to comment.