|
6 | 6 | import android.content.Intent; |
7 | 7 | import android.graphics.BitmapFactory; |
8 | 8 | import android.net.Uri; |
| 9 | +import android.os.Binder; |
9 | 10 | import android.os.Bundle; |
| 11 | +import android.os.IBinder; |
10 | 12 | import androidx.core.app.NotificationCompat; |
11 | 13 | import androidx.core.app.NotificationManagerCompat; |
12 | 14 | import fr.free.nrw.commons.BuildConfig; |
13 | 15 | import fr.free.nrw.commons.CommonsApplication; |
14 | | -import fr.free.nrw.commons.HandlerService; |
15 | 16 | import fr.free.nrw.commons.R; |
16 | 17 | import fr.free.nrw.commons.auth.SessionManager; |
17 | 18 | import fr.free.nrw.commons.contributions.Contribution; |
18 | 19 | import fr.free.nrw.commons.contributions.ContributionDao; |
19 | 20 | import fr.free.nrw.commons.contributions.MainActivity; |
20 | 21 | import fr.free.nrw.commons.di.CommonsApplicationModule; |
| 22 | +import fr.free.nrw.commons.di.CommonsDaggerService; |
21 | 23 | import fr.free.nrw.commons.media.MediaClient; |
22 | 24 | import fr.free.nrw.commons.utils.CommonsDateUtil; |
23 | 25 | import fr.free.nrw.commons.wikidata.WikidataEditService; |
24 | 26 | import io.reactivex.Observable; |
25 | 27 | import io.reactivex.Scheduler; |
26 | 28 | import io.reactivex.disposables.CompositeDisposable; |
| 29 | +import io.reactivex.processors.PublishProcessor; |
27 | 30 | import io.reactivex.schedulers.Schedulers; |
28 | 31 | import java.io.File; |
29 | 32 | import java.io.IOException; |
|
36 | 39 | import javax.inject.Named; |
37 | 40 | import timber.log.Timber; |
38 | 41 |
|
39 | | -public class UploadService extends HandlerService<Contribution> { |
| 42 | +public class UploadService extends CommonsDaggerService { |
40 | 43 |
|
41 | 44 | private static final String EXTRA_PREFIX = "fr.free.nrw.commons.upload"; |
42 | 45 |
|
43 | | - public static final int ACTION_UPLOAD_FILE = 1; |
44 | | - |
45 | 46 | public static final String ACTION_START_SERVICE = EXTRA_PREFIX + ".upload"; |
46 | 47 | public static final String EXTRA_FILES = EXTRA_PREFIX + ".files"; |
47 | 48 | @Inject WikidataEditService wikidataEditService; |
@@ -72,10 +73,6 @@ public class UploadService extends HandlerService<Contribution> { |
72 | 73 | public static final int NOTIFICATION_UPLOAD_IN_PROGRESS = 1; |
73 | 74 | public static final int NOTIFICATION_UPLOAD_FAILED = 3; |
74 | 75 |
|
75 | | - public UploadService() { |
76 | | - super("UploadService"); |
77 | | - } |
78 | | - |
79 | 76 | protected class NotificationUpdateProgressListener{ |
80 | 77 |
|
81 | 78 | String notificationTag; |
@@ -124,55 +121,57 @@ public void onDestroy() { |
124 | 121 | Timber.d("UploadService.onDestroy; %s are yet to be uploaded", unfinishedUploads); |
125 | 122 | } |
126 | 123 |
|
| 124 | + public class UploadServiceLocalBinder extends Binder { |
| 125 | + public UploadService getService() { |
| 126 | + return UploadService.this; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private final IBinder localBinder = new UploadServiceLocalBinder(); |
| 131 | + |
| 132 | + private PublishProcessor<Contribution> contributionsToUpload; |
| 133 | + |
| 134 | + @Override |
| 135 | + public IBinder onBind(Intent intent) { |
| 136 | + return localBinder; |
| 137 | + } |
| 138 | + |
127 | 139 | @Override |
128 | 140 | public void onCreate() { |
129 | 141 | super.onCreate(); |
130 | 142 | CommonsApplication.createNotificationChannel(getApplicationContext()); |
131 | 143 | compositeDisposable = new CompositeDisposable(); |
132 | 144 | notificationManager = NotificationManagerCompat.from(this); |
133 | 145 | curNotification = getNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL); |
| 146 | + contributionsToUpload = PublishProcessor.create(); |
| 147 | + compositeDisposable.add(contributionsToUpload.subscribe(this::handleUpload)); |
134 | 148 | } |
135 | 149 |
|
136 | | - @Override |
137 | | - protected void handle(int what, Contribution contribution) { |
138 | | - switch (what) { |
139 | | - case ACTION_UPLOAD_FILE: |
140 | | - uploadContribution(contribution); |
141 | | - break; |
142 | | - default: |
143 | | - throw new IllegalArgumentException("Unknown value for what"); |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - @Override |
148 | | - public void queue(int what, Contribution contribution) { |
149 | | - switch (what) { |
150 | | - case ACTION_UPLOAD_FILE: |
151 | | - |
152 | | - contribution.setState(Contribution.STATE_QUEUED); |
153 | | - contribution.setTransferred(0); |
154 | | - toUpload++; |
155 | | - if (curNotification != null && toUpload != 1) { |
156 | | - curNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)); |
157 | | - Timber.d("%d uploads left", toUpload); |
158 | | - notificationManager.notify(contribution.getLocalUri().toString(), NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build()); |
159 | | - } |
160 | | - compositeDisposable.add(contributionDao |
161 | | - .save(contribution) |
162 | | - .subscribeOn(ioThreadScheduler) |
163 | | - .observeOn(mainThreadScheduler) |
164 | | - .subscribe(aLong->{ |
165 | | - contribution.set_id(aLong); |
166 | | - UploadService.super.queue(what, contribution); |
167 | | - }, Throwable::printStackTrace)); |
168 | | - break; |
169 | | - default: |
170 | | - throw new IllegalArgumentException("Unknown value for what"); |
| 150 | + public void handleUpload(Contribution contribution) { |
| 151 | + contribution.setState(Contribution.STATE_QUEUED); |
| 152 | + contribution.setTransferred(0); |
| 153 | + toUpload++; |
| 154 | + if (curNotification != null && toUpload != 1) { |
| 155 | + curNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)); |
| 156 | + Timber.d("%d uploads left", toUpload); |
| 157 | + notificationManager.notify(contribution.getLocalUri().toString(), NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification.build()); |
171 | 158 | } |
| 159 | + compositeDisposable.add(contributionDao |
| 160 | + .save(contribution) |
| 161 | + .subscribeOn(ioThreadScheduler) |
| 162 | + .observeOn(mainThreadScheduler) |
| 163 | + .subscribe(aLong->{ |
| 164 | + contribution.set_id(aLong); |
| 165 | + uploadContribution(contribution); |
| 166 | + }, Throwable::printStackTrace)); |
172 | 167 | } |
173 | 168 |
|
174 | 169 | private boolean freshStart = true; |
175 | 170 |
|
| 171 | + public void queue(Contribution contribution) { |
| 172 | + contributionsToUpload.offer(contribution); |
| 173 | + } |
| 174 | + |
176 | 175 | @Override |
177 | 176 | public int onStartCommand(Intent intent, int flags, int startId) { |
178 | 177 | if (ACTION_START_SERVICE.equals(intent.getAction()) && freshStart) { |
|
0 commit comments