|
46 | 46 | import fr.free.nrw.commons.upload.worker.WorkRequestHelper;
|
47 | 47 | import fr.free.nrw.commons.utils.PermissionUtils;
|
48 | 48 | import fr.free.nrw.commons.utils.ViewUtilWrapper;
|
| 49 | +import io.reactivex.Completable; |
49 | 50 | import io.reactivex.schedulers.Schedulers;
|
50 | 51 | import java.util.Collections;
|
| 52 | +import java.util.List; |
51 | 53 | import javax.inject.Inject;
|
52 | 54 | import javax.inject.Named;
|
53 | 55 | import timber.log.Timber;
|
@@ -170,6 +172,7 @@ public void onCreate(Bundle savedInstanceState) {
|
170 | 172 | R.string.add_location_manually,
|
171 | 173 | permission.ACCESS_MEDIA_LOCATION);
|
172 | 174 | }
|
| 175 | + checkAndResumeStuckUploads(); |
173 | 176 | }
|
174 | 177 | }
|
175 | 178 |
|
@@ -277,6 +280,34 @@ public void setNumOfUploads(int uploadCount) {
|
277 | 280 | }
|
278 | 281 | }
|
279 | 282 |
|
| 283 | + /** |
| 284 | + * Resume the uploads that got stuck because of the app being killed |
| 285 | + * or the device being rebooted. |
| 286 | + * |
| 287 | + * When the app is terminated or the device is restarted, contributions remain in the |
| 288 | + * 'STATE_IN_PROGRESS' state. This status persists and doesn't change during these events. |
| 289 | + * So, retrieving contributions labeled as 'STATE_IN_PROGRESS' |
| 290 | + * from the database will provide the list of uploads that appear as stuck on opening the app again |
| 291 | + */ |
| 292 | + @SuppressLint("CheckResult") |
| 293 | + private void checkAndResumeStuckUploads() { |
| 294 | + List<Contribution> stuckUploads = contributionDao.getContribution( |
| 295 | + Collections.singletonList(Contribution.STATE_IN_PROGRESS)) |
| 296 | + .subscribeOn(Schedulers.io()) |
| 297 | + .blockingGet(); |
| 298 | + Timber.d("Resuming " + stuckUploads.size() + " uploads..."); |
| 299 | + if(!stuckUploads.isEmpty()) { |
| 300 | + for(Contribution contribution: stuckUploads) { |
| 301 | + contribution.setState(Contribution.STATE_QUEUED); |
| 302 | + Completable.fromAction(() -> contributionDao.saveSynchronous(contribution)) |
| 303 | + .subscribeOn(Schedulers.io()) |
| 304 | + .subscribe(); |
| 305 | + } |
| 306 | + WorkRequestHelper.Companion.makeOneTimeWorkRequest( |
| 307 | + this, ExistingWorkPolicy.APPEND_OR_REPLACE); |
| 308 | + } |
| 309 | + } |
| 310 | + |
280 | 311 | @Override
|
281 | 312 | protected void onPostCreate(@Nullable Bundle savedInstanceState) {
|
282 | 313 | super.onPostCreate(savedInstanceState);
|
|
0 commit comments