Skip to content

Commit 62c14ec

Browse files
zhao-gangneslihanturan
authored andcommitted
Make DeleteTask notification quieter on some devices(Fixed commons-app#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.
1 parent 52724b8 commit 62c14ec

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

app/src/main/java/fr/free/nrw/commons/delete/DeleteTask.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected void onPreExecute() {
5555
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
5656
notificationBuilder = new NotificationCompat.Builder(
5757
context,
58-
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
58+
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL)
59+
.setOnlyAlertOnce(true);
5960
Toast toast = new Toast(context);
6061
toast.setGravity(Gravity.CENTER,0,0);
6162
toast = Toast.makeText(context,"Trying to nominate "+media.getDisplayTitle()+ " for deletion",Toast.LENGTH_SHORT);

app/src/main/java/fr/free/nrw/commons/upload/UploadService.java

+17-27
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public class UploadService extends HandlerService<Contribution> {
5555
@Inject ContributionDao contributionDao;
5656

5757
private NotificationManager notificationManager;
58-
private NotificationCompat.Builder curProgressNotification;
59-
private NotificationCompat.Builder curFailedNotification;
58+
private NotificationCompat.Builder curNotification;
6059
private int toUpload;
6160

6261
/**
@@ -95,19 +94,19 @@ private class NotificationUpdateProgressListener implements MediaWikiApi.Progres
9594
public void onProgress(long transferred, long total) {
9695
Timber.d("Uploaded %d of %d", transferred, total);
9796
if (!notificationTitleChanged) {
98-
curProgressNotification.setContentTitle(notificationProgressTitle);
97+
curNotification.setContentTitle(notificationProgressTitle);
9998
notificationTitleChanged = true;
10099
contribution.setState(Contribution.STATE_IN_PROGRESS);
101100
}
102101
if (transferred == total) {
103102
// Completed!
104-
curProgressNotification.setContentTitle(notificationFinishingTitle)
103+
curNotification.setContentTitle(notificationFinishingTitle)
105104
.setTicker(notificationFinishingTitle)
106105
.setProgress(0, 100, true);
107106
} else {
108-
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
107+
curNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
109108
}
110-
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
109+
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build());
111110

112111
contribution.setTransferred(transferred);
113112
contributionDao.save(contribution);
@@ -126,8 +125,7 @@ public void onCreate() {
126125
super.onCreate();
127126
CommonsApplication.createNotificationChannel(getApplicationContext());
128127
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
129-
curProgressNotification = getProgressNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
130-
curFailedNotification = getFailedNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
128+
curNotification = getNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
131129
}
132130

133131
@Override
@@ -151,10 +149,10 @@ public void queue(int what, Contribution contribution) {
151149
contribution.setTransferred(0);
152150
contributionDao.save(contribution);
153151
toUpload++;
154-
if (curProgressNotification != null && toUpload != 1) {
155-
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
152+
if (curNotification != null && toUpload != 1) {
153+
curNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
156154
Timber.d("%d uploads left", toUpload);
157-
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
155+
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build());
158156
}
159157

160158
super.queue(what, contribution);
@@ -184,25 +182,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
184182
return START_REDELIVER_INTENT;
185183
}
186184

187-
private NotificationCompat.Builder getProgressNotificationBuilder(String channelId) {
188-
return getNotificationBuilder(channelId)
189-
.setProgress(100, 0, true)
190-
.setOngoing(true);
191-
}
192-
193-
private NotificationCompat.Builder getFailedNotificationBuilder(String channelId) {
194-
return getNotificationBuilder(channelId);
195-
}
196-
197185
@SuppressLint("StringFormatInvalid")
198186
private NotificationCompat.Builder getNotificationBuilder(String channelId) {
199187
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
200188
.setSmallIcon(R.drawable.ic_launcher)
201189
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
202190
.setAutoCancel(true)
203191
.setOnlyAlertOnce(true)
192+
.setProgress(100, 0, true)
193+
.setOngoing(true)
204194
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));
205-
206195
}
207196

208197
private void uploadContribution(Contribution contribution) {
@@ -225,10 +214,10 @@ private void uploadContribution(Contribution contribution) {
225214
}
226215

227216
Timber.d("Before execution!");
228-
curProgressNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
217+
curNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
229218
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
230219
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
231-
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
220+
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build());
232221

233222
String filename = contribution.getFilename();
234223
try {
@@ -294,10 +283,11 @@ private void uploadContribution(Contribution contribution) {
294283
@SuppressLint("StringFormatInvalid")
295284
@SuppressWarnings("deprecation")
296285
private void showFailedNotification(Contribution contribution) {
297-
curFailedNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
286+
curNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
298287
.setContentTitle(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
299-
.setContentText(getString(R.string.upload_failed_notification_subtitle));
300-
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curFailedNotification.build());
288+
.setContentText(getString(R.string.upload_failed_notification_subtitle))
289+
.setProgress(0, 0, false);
290+
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curNotification.build());
301291

302292
contribution.setState(Contribution.STATE_FAILED);
303293
contributionDao.save(contribution);

0 commit comments

Comments
 (0)