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.
This commit is contained in:
Zhao Gang 2019-03-01 01:12:47 +08:00 committed by Josephine Lim
parent 64eae8be82
commit ab4fca5ea9

View file

@ -1,7 +1,6 @@
package fr.free.nrw.commons.upload; package fr.free.nrw.commons.upload;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -57,6 +56,7 @@ public class UploadService extends HandlerService<Contribution> {
private NotificationManager notificationManager; private NotificationManager notificationManager;
private NotificationCompat.Builder curProgressNotification; private NotificationCompat.Builder curProgressNotification;
private NotificationCompat.Builder curFailedNotification;
private int toUpload; private int toUpload;
/** /**
@ -101,12 +101,13 @@ public class UploadService extends HandlerService<Contribution> {
} }
if (transferred == total) { if (transferred == total) {
// Completed! // Completed!
curProgressNotification.setContentTitle(notificationFinishingTitle); curProgressNotification.setContentTitle(notificationFinishingTitle)
curProgressNotification.setProgress(0, 100, true); .setTicker(notificationFinishingTitle)
.setProgress(0, 100, true);
} else { } else {
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false); curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
} }
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
contribution.setTransferred(transferred); contribution.setTransferred(transferred);
contributionDao.save(contribution); contributionDao.save(contribution);
@ -125,6 +126,8 @@ 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);
curFailedNotification = getFailedNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
} }
@Override @Override
@ -151,7 +154,7 @@ public class UploadService extends HandlerService<Contribution> {
if (curProgressNotification != null && toUpload != 1) { if (curProgressNotification != null && toUpload != 1) {
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)); curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
Timber.d("%d uploads left", toUpload); Timber.d("%d uploads left", toUpload);
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
} }
super.queue(what, contribution); super.queue(what, contribution);
@ -181,18 +184,25 @@ 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(Contribution contribution, String channelId) { private NotificationCompat.Builder getNotificationBuilder(String channelId) {
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true) return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true) .setAutoCancel(true)
.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle())) .setOnlyAlertOnce(true)
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)) .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));
.setOngoing(true)
.setProgress(100, 0, true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
} }
private void uploadContribution(Contribution contribution) { private void uploadContribution(Contribution contribution) {
@ -215,10 +225,10 @@ public class UploadService extends HandlerService<Contribution> {
} }
Timber.d("Before execution!"); Timber.d("Before execution!");
curProgressNotification = getNotificationBuilder( curProgressNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
contribution, .setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL); .setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
String filename = contribution.getFilename(); String filename = contribution.getFilename();
try { try {
@ -248,10 +258,9 @@ public class UploadService extends HandlerService<Contribution> {
UploadResult uploadResult = mwApi.uploadFile(filename, fileInputStream, contribution.getDataLength(), UploadResult uploadResult = mwApi.uploadFile(filename, fileInputStream, contribution.getDataLength(),
contribution.getPageContents(getApplicationContext()), contribution.getEditSummary(), localUri, contribution.getContentProviderUri(), notificationUpdater); contribution.getPageContents(getApplicationContext()), contribution.getEditSummary(), localUri, contribution.getContentProviderUri(), notificationUpdater);
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
Timber.d("Response is %s", uploadResult.toString()); Timber.d("Response is %s", uploadResult.toString());
curProgressNotification = null;
String resultStatus = uploadResult.getResultStatus(); String resultStatus = uploadResult.getResultStatus();
if (!resultStatus.equals("Success")) { if (!resultStatus.equals("Success")) {
Timber.d("Contribution upload failed. Wikidata entity won't be edited"); Timber.d("Contribution upload failed. Wikidata entity won't be edited");
@ -267,6 +276,7 @@ public class UploadService extends HandlerService<Contribution> {
} }
} catch (IOException e) { } catch (IOException e) {
Timber.d("I have a network fuckup"); Timber.d("I have a network fuckup");
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
showFailedNotification(contribution); showFailedNotification(contribution);
} finally { } finally {
if (filename != null) { if (filename != null) {
@ -284,15 +294,10 @@ public class UploadService extends HandlerService<Contribution> {
@SuppressLint("StringFormatInvalid") @SuppressLint("StringFormatInvalid")
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void showFailedNotification(Contribution contribution) { private void showFailedNotification(Contribution contribution) {
Notification failureNotification = new NotificationCompat.Builder(this, CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL).setAutoCancel(true) curFailedNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
.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));
.build(); notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, curFailedNotification.build());
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
contribution.setState(Contribution.STATE_FAILED); contribution.setState(Contribution.STATE_FAILED);
contributionDao.save(contribution); contributionDao.save(contribution);