Fix issue where upload notification shows up on app start (#4099)

This commit is contained in:
Vivek Maskara 2020-12-14 05:55:17 -07:00 committed by GitHub
parent 0415a57d34
commit 5c6e777b37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 10 deletions

View file

@ -68,6 +68,9 @@ public abstract class ContributionDao {
@Query("UPDATE contribution SET state=:state WHERE state in (:toUpdateStates)") @Query("UPDATE contribution SET state=:state WHERE state in (:toUpdateStates)")
public abstract Single<Integer> updateStates(int state, int[] toUpdateStates); public abstract Single<Integer> updateStates(int state, int[] toUpdateStates);
@Query("SELECT COUNT(*) from contribution WHERE state in (:toUpdateStates)")
public abstract Single<Integer> getPendingUploads(int[] toUpdateStates);
@Query("Delete FROM contribution") @Query("Delete FROM contribution")
public abstract void deleteAll() throws SQLiteException; public abstract void deleteAll() throws SQLiteException;

View file

@ -224,8 +224,7 @@ public class UploadService extends CommonsDaggerService {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, showUploadNotification();
curNotification.setContentText(getText(R.string.starting_uploads)).build());
if (ACTION_START_SERVICE.equals(intent.getAction()) && freshStart) { if (ACTION_START_SERVICE.equals(intent.getAction()) && freshStart) {
compositeDisposable.add(contributionDao.updateStates(Contribution.STATE_FAILED, compositeDisposable.add(contributionDao.updateStates(Contribution.STATE_FAILED,
new int[]{Contribution.STATE_QUEUED, Contribution.STATE_IN_PROGRESS}) new int[]{Contribution.STATE_QUEUED, Contribution.STATE_IN_PROGRESS})
@ -234,19 +233,32 @@ public class UploadService extends CommonsDaggerService {
.subscribe()); .subscribe());
freshStart = false; freshStart = false;
} else if (PROCESS_PENDING_LIMITED_CONNECTION_MODE_UPLOADS.equals(intent.getAction())) { } else if (PROCESS_PENDING_LIMITED_CONNECTION_MODE_UPLOADS.equals(intent.getAction())) {
contributionDao.getContribution(Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE) contributionDao.getContribution(Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE)
.flatMapObservable( .flatMapObservable(
(Function<List<Contribution>, ObservableSource<Contribution>>) contributions -> Observable.fromIterable(contributions)) (Function<List<Contribution>, ObservableSource<Contribution>>) contributions -> Observable
.concatMapCompletable(contribution -> Completable.fromAction(() -> queue(contribution))) .fromIterable(contributions))
.subscribeOn(ioThreadScheduler) .concatMapCompletable(contribution -> Completable.fromAction(() -> queue(contribution)))
.subscribe(); .subscribeOn(ioThreadScheduler)
} .subscribe();
}
return START_REDELIVER_INTENT; return START_REDELIVER_INTENT;
} }
private void showUploadNotification() {
compositeDisposable.add(contributionDao
.getPendingUploads(new int[]{Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED})
.subscribe(count -> {
if (count > 0) {
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS,
curNotification.setContentText(getText(R.string.starting_uploads)).build());
}
}));
}
@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)
.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)