Skip to content

Commit d9e41b9

Browse files
MainActivity.java: resume uploads that got stuck because of app being killed or device being rebooted (#5399)
1 parent 3ae5bb5 commit d9e41b9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
import fr.free.nrw.commons.upload.worker.WorkRequestHelper;
4747
import fr.free.nrw.commons.utils.PermissionUtils;
4848
import fr.free.nrw.commons.utils.ViewUtilWrapper;
49+
import io.reactivex.Completable;
4950
import io.reactivex.schedulers.Schedulers;
5051
import java.util.Collections;
52+
import java.util.List;
5153
import javax.inject.Inject;
5254
import javax.inject.Named;
5355
import timber.log.Timber;
@@ -170,6 +172,7 @@ public void onCreate(Bundle savedInstanceState) {
170172
R.string.add_location_manually,
171173
permission.ACCESS_MEDIA_LOCATION);
172174
}
175+
checkAndResumeStuckUploads();
173176
}
174177
}
175178

@@ -277,6 +280,34 @@ public void setNumOfUploads(int uploadCount) {
277280
}
278281
}
279282

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+
280311
@Override
281312
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
282313
super.onPostCreate(savedInstanceState);

0 commit comments

Comments
 (0)