Skip to content

Commit 53165c3

Browse files
committed
Fix issue where upload notification shows up on app start
1 parent 7d8e09f commit 53165c3

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public Completable delete(final Contribution contribution) {
6868
@Query("UPDATE contribution SET state=:state WHERE state in (:toUpdateStates)")
6969
public abstract Single<Integer> updateStates(int state, int[] toUpdateStates);
7070

71+
@Query("SELECT COUNT(*) from contribution WHERE state in (:toUpdateStates)")
72+
public abstract Single<Integer> getPendingUploads(int[] toUpdateStates);
73+
7174
@Query("Delete FROM contribution")
7275
public abstract void deleteAll() throws SQLiteException;
7376

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

+22-10
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ public void queue(Contribution contribution) {
224224

225225
@Override
226226
public int onStartCommand(Intent intent, int flags, int startId) {
227-
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS,
228-
curNotification.setContentText(getText(R.string.starting_uploads)).build());
227+
showUploadNotification();
229228
if (ACTION_START_SERVICE.equals(intent.getAction()) && freshStart) {
230229
compositeDisposable.add(contributionDao.updateStates(Contribution.STATE_FAILED,
231230
new int[]{Contribution.STATE_QUEUED, Contribution.STATE_IN_PROGRESS})
@@ -234,19 +233,32 @@ public int onStartCommand(Intent intent, int flags, int startId) {
234233
.subscribe());
235234
freshStart = false;
236235
} else if (PROCESS_PENDING_LIMITED_CONNECTION_MODE_UPLOADS.equals(intent.getAction())) {
237-
contributionDao.getContribution(Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE)
238-
.flatMapObservable(
239-
(Function<List<Contribution>, ObservableSource<Contribution>>) contributions -> Observable.fromIterable(contributions))
240-
.concatMapCompletable(contribution -> Completable.fromAction(() -> queue(contribution)))
241-
.subscribeOn(ioThreadScheduler)
242-
.subscribe();
243-
}
236+
contributionDao.getContribution(Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE)
237+
.flatMapObservable(
238+
(Function<List<Contribution>, ObservableSource<Contribution>>) contributions -> Observable
239+
.fromIterable(contributions))
240+
.concatMapCompletable(contribution -> Completable.fromAction(() -> queue(contribution)))
241+
.subscribeOn(ioThreadScheduler)
242+
.subscribe();
243+
}
244244
return START_REDELIVER_INTENT;
245245
}
246246

247+
private void showUploadNotification() {
248+
compositeDisposable.add(contributionDao
249+
.getPendingUploads(new int[]{Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED})
250+
.subscribe(count -> {
251+
if (count > 0) {
252+
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS,
253+
curNotification.setContentText(getText(R.string.starting_uploads)).build());
254+
}
255+
}));
256+
}
257+
247258
@SuppressLint("StringFormatInvalid")
248259
private NotificationCompat.Builder getNotificationBuilder(String channelId) {
249-
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
260+
return new NotificationCompat.Builder(this, channelId)
261+
.setAutoCancel(true)
250262
.setSmallIcon(R.drawable.ic_launcher)
251263
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
252264
.setAutoCancel(true)

0 commit comments

Comments
 (0)