Skip to content

Commit ab4fca5

Browse files
zhao-gangmisaochan
authored andcommitted
Make uploading file notification quieter on some devices(Fixed #2528) (#2529)
Add setOnlyAlertOnce flag to NotificationCompat.Builder. This fixed the issue that notification makes an alarm(beep) every time uploading progress updated on some devices. Also let UploadService.class reuse NotificationCompat.Builder as much as possible(instead of creating a NotificationCompat.Builder every time in method uploadContribution). And some small refactors.
1 parent 64eae8b commit ab4fca5

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

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

+31-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.free.nrw.commons.upload;
22

33
import android.annotation.SuppressLint;
4-
import android.app.Notification;
54
import android.app.NotificationManager;
65
import android.app.PendingIntent;
76
import android.content.ContentResolver;
@@ -57,6 +56,7 @@ public class UploadService extends HandlerService<Contribution> {
5756

5857
private NotificationManager notificationManager;
5958
private NotificationCompat.Builder curProgressNotification;
59+
private NotificationCompat.Builder curFailedNotification;
6060
private int toUpload;
6161

6262
/**
@@ -101,12 +101,13 @@ public void onProgress(long transferred, long total) {
101101
}
102102
if (transferred == total) {
103103
// Completed!
104-
curProgressNotification.setContentTitle(notificationFinishingTitle);
105-
curProgressNotification.setProgress(0, 100, true);
104+
curProgressNotification.setContentTitle(notificationFinishingTitle)
105+
.setTicker(notificationFinishingTitle)
106+
.setProgress(0, 100, true);
106107
} else {
107108
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
108109
}
109-
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
110+
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
110111

111112
contribution.setTransferred(transferred);
112113
contributionDao.save(contribution);
@@ -125,6 +126,8 @@ public void onCreate() {
125126
super.onCreate();
126127
CommonsApplication.createNotificationChannel(getApplicationContext());
127128
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
129+
curProgressNotification = getProgressNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
130+
curFailedNotification = getFailedNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
128131
}
129132

130133
@Override
@@ -151,7 +154,7 @@ public void queue(int what, Contribution contribution) {
151154
if (curProgressNotification != null && toUpload != 1) {
152155
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
153156
Timber.d("%d uploads left", toUpload);
154-
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
157+
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
155158
}
156159

157160
super.queue(what, contribution);
@@ -181,18 +184,25 @@ public int onStartCommand(Intent intent, int flags, int startId) {
181184
return START_REDELIVER_INTENT;
182185
}
183186

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+
184197
@SuppressLint("StringFormatInvalid")
185-
private NotificationCompat.Builder getNotificationBuilder(Contribution contribution, String channelId) {
198+
private NotificationCompat.Builder getNotificationBuilder(String channelId) {
186199
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
187200
.setSmallIcon(R.drawable.ic_launcher)
188201
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
189202
.setAutoCancel(true)
190-
.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
191-
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
192-
.setOngoing(true)
193-
.setProgress(100, 0, true)
194-
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
195-
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
203+
.setOnlyAlertOnce(true)
204+
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));
205+
196206
}
197207

198208
private void uploadContribution(Contribution contribution) {
@@ -215,10 +225,10 @@ private void uploadContribution(Contribution contribution) {
215225
}
216226

217227
Timber.d("Before execution!");
218-
curProgressNotification = getNotificationBuilder(
219-
contribution,
220-
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
221-
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
228+
curProgressNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
229+
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
230+
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
231+
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
222232

223233
String filename = contribution.getFilename();
224234
try {
@@ -248,10 +258,9 @@ private void uploadContribution(Contribution contribution) {
248258
UploadResult uploadResult = mwApi.uploadFile(filename, fileInputStream, contribution.getDataLength(),
249259
contribution.getPageContents(getApplicationContext()), contribution.getEditSummary(), localUri, contribution.getContentProviderUri(), notificationUpdater);
250260

261+
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
251262
Timber.d("Response is %s", uploadResult.toString());
252263

253-
curProgressNotification = null;
254-
255264
String resultStatus = uploadResult.getResultStatus();
256265
if (!resultStatus.equals("Success")) {
257266
Timber.d("Contribution upload failed. Wikidata entity won't be edited");
@@ -267,6 +276,7 @@ private void uploadContribution(Contribution contribution) {
267276
}
268277
} catch (IOException e) {
269278
Timber.d("I have a network fuckup");
279+
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
270280
showFailedNotification(contribution);
271281
} finally {
272282
if (filename != null) {
@@ -284,15 +294,10 @@ private void uploadContribution(Contribution contribution) {
284294
@SuppressLint("StringFormatInvalid")
285295
@SuppressWarnings("deprecation")
286296
private void showFailedNotification(Contribution contribution) {
287-
Notification failureNotification = new NotificationCompat.Builder(this, CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL).setAutoCancel(true)
288-
.setSmallIcon(R.drawable.ic_launcher)
289-
.setAutoCancel(true)
290-
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
291-
.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
297+
curFailedNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
292298
.setContentTitle(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
293-
.setContentText(getString(R.string.upload_failed_notification_subtitle))
294-
.build();
295-
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
299+
.setContentText(getString(R.string.upload_failed_notification_subtitle));
300+
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curFailedNotification.build());
296301

297302
contribution.setState(Contribution.STATE_FAILED);
298303
contributionDao.save(contribution);

0 commit comments

Comments
 (0)