MainActivity.java: resume uploads that got stuck because of app being killed or device being rebooted (#5399)

This commit is contained in:
Ritika Pahwa 2023-12-12 19:00:25 +05:30 committed by GitHub
parent 3ae5bb5cd7
commit d9e41b9d2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -46,8 +46,10 @@ import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.upload.worker.WorkRequestHelper;
import fr.free.nrw.commons.utils.PermissionUtils;
import fr.free.nrw.commons.utils.ViewUtilWrapper;
import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;
@ -170,6 +172,7 @@ public class MainActivity extends BaseActivity
R.string.add_location_manually,
permission.ACCESS_MEDIA_LOCATION);
}
checkAndResumeStuckUploads();
}
}
@ -277,6 +280,34 @@ public class MainActivity extends BaseActivity
}
}
/**
* Resume the uploads that got stuck because of the app being killed
* or the device being rebooted.
*
* When the app is terminated or the device is restarted, contributions remain in the
* 'STATE_IN_PROGRESS' state. This status persists and doesn't change during these events.
* So, retrieving contributions labeled as 'STATE_IN_PROGRESS'
* from the database will provide the list of uploads that appear as stuck on opening the app again
*/
@SuppressLint("CheckResult")
private void checkAndResumeStuckUploads() {
List<Contribution> stuckUploads = contributionDao.getContribution(
Collections.singletonList(Contribution.STATE_IN_PROGRESS))
.subscribeOn(Schedulers.io())
.blockingGet();
Timber.d("Resuming " + stuckUploads.size() + " uploads...");
if(!stuckUploads.isEmpty()) {
for(Contribution contribution: stuckUploads) {
contribution.setState(Contribution.STATE_QUEUED);
Completable.fromAction(() -> contributionDao.saveSynchronous(contribution))
.subscribeOn(Schedulers.io())
.subscribe();
}
WorkRequestHelper.Companion.makeOneTimeWorkRequest(
this, ExistingWorkPolicy.APPEND_OR_REPLACE);
}
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);