Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f82f1ff9 authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

Settings: improved stats upload logic



We don't get onStopJob() callbacks after the job is finished.

Ref: CYNGNOS-714

Change-Id: I1906cb80854a193efaba0f662d66823fec5f7073
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent c1e5f66a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -68,6 +68,11 @@ public class AnonymousStats extends SettingsPreferenceFragment {
    public static void removeJob(Context context, int jobId) {
        Set<String> jobQueue = getJobQueue(context);
        jobQueue.remove(String.valueOf(jobId));
        getPreferences(context)
                .edit()
                .remove(KEY_JOB_QUEUE)
                .commit();

        getPreferences(context)
                .edit()
                .putStringSet(KEY_JOB_QUEUE, jobQueue)
+16 −15
Original line number Diff line number Diff line
@@ -57,15 +57,16 @@ public class StatsUploadJobService extends JobService {
    public static final String KEY_CARRIER_ID = "carrierId";
    public static final String KEY_TIMESTAMP = "timeStamp";

    List<JobParameters> mFinishedJobs = new LinkedList<>();
    ArrayMap<JobParameters, StatsUploadTask> mCurrentJobs = new ArrayMap<>();
    private final ArrayMap<JobParameters, StatsUploadTask> mCurrentJobs = new ArrayMap<>();

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        if (DEBUG)
            Log.d(TAG, "onStartJob() called with " + "jobParameters = [" + jobParameters + "]");
        final StatsUploadTask uploadTask = new StatsUploadTask(jobParameters);
        synchronized (mCurrentJobs) {
            mCurrentJobs.put(jobParameters, uploadTask);
        }
        uploadTask.execute((Void) null);
        return true;
    }
@@ -75,19 +76,18 @@ public class StatsUploadJobService extends JobService {
        if (DEBUG)
            Log.d(TAG, "onStopJob() called with " + "jobParameters = [" + jobParameters + "]");

        final StatsUploadTask cancelledJob = mCurrentJobs.remove(jobParameters);

        // if we can't remove from finished jobs, that means it's not finished!
        final boolean jobSuccessfullyFinished = mFinishedJobs.remove(jobParameters);
        final StatsUploadTask cancelledJob;
        synchronized (mCurrentJobs) {
            cancelledJob = mCurrentJobs.remove(jobParameters);
        }

        if (!jobSuccessfullyFinished) {
            // cancel the ongoing background task
        if (cancelledJob != null) {
            // cancel the ongoing background task
            cancelledJob.cancel(true);
            }
            return true; // reschedule
        }

        return !jobSuccessfullyFinished;
        return false;
    }

    private class StatsUploadTask extends AsyncTask<Void, Void, Boolean> {
@@ -139,6 +139,10 @@ public class StatsUploadJobService extends JobService {
            }

            if (success) {
                // we hit the server, succeed either which way.
                synchronized (mCurrentJobs) {
                    mCurrentJobs.remove(mJobParams);
                }
                AnonymousStats.removeJob(StatsUploadJobService.this, mJobParams.getJobId());
            }

@@ -147,9 +151,6 @@ public class StatsUploadJobService extends JobService {

        @Override
        protected void onPostExecute(Boolean success) {
            if (success) {
                mFinishedJobs.add(mJobParams);
            }
            if (DEBUG)
                Log.d(TAG, "job id " + mJobParams.getJobId() + ", has finished with success="
                        + success);