diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 13b1da102..18c15b3cf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -90,7 +90,7 @@ - + diff --git a/app/src/main/java/com/zegoggles/smssync/activity/StatusPreference.java b/app/src/main/java/com/zegoggles/smssync/activity/StatusPreference.java index 9a9e249cc..88566d120 100644 --- a/app/src/main/java/com/zegoggles/smssync/activity/StatusPreference.java +++ b/app/src/main/java/com/zegoggles/smssync/activity/StatusPreference.java @@ -15,7 +15,7 @@ import com.zegoggles.smssync.preferences.Preferences; import com.zegoggles.smssync.service.SmsBackupService; import com.zegoggles.smssync.service.SmsRestoreService; -import com.zegoggles.smssync.service.UserCanceled; +import com.zegoggles.smssync.service.CancelEvent; import com.zegoggles.smssync.service.state.BackupState; import com.zegoggles.smssync.service.state.RestoreState; import com.zegoggles.smssync.service.state.SmsSyncState; @@ -58,7 +58,7 @@ public void onClick(View v) { // Sync button will be restored on next status update. mBackupButton.setText(R.string.ui_sync_button_label_canceling); mBackupButton.setEnabled(false); - App.bus.post(new UserCanceled()); + App.bus.post(new CancelEvent()); } } else if (v == mRestoreButton) { if (LOCAL_LOGV) Log.v(TAG, "restore"); @@ -67,7 +67,7 @@ public void onClick(View v) { } else { mRestoreButton.setText(R.string.ui_sync_button_label_canceling); mRestoreButton.setEnabled(false); - App.bus.post(new UserCanceled()); + App.bus.post(new CancelEvent()); } } } diff --git a/app/src/main/java/com/zegoggles/smssync/receiver/UpdateReceiver.java b/app/src/main/java/com/zegoggles/smssync/receiver/PackageReplacedReceiver.java similarity index 84% rename from app/src/main/java/com/zegoggles/smssync/receiver/UpdateReceiver.java rename to app/src/main/java/com/zegoggles/smssync/receiver/PackageReplacedReceiver.java index 74aa2a648..47781610a 100644 --- a/app/src/main/java/com/zegoggles/smssync/receiver/UpdateReceiver.java +++ b/app/src/main/java/com/zegoggles/smssync/receiver/PackageReplacedReceiver.java @@ -11,13 +11,13 @@ import static com.zegoggles.smssync.App.LOCAL_LOGV; import static com.zegoggles.smssync.App.TAG; -public class UpdateReceiver extends BroadcastReceiver { +public class PackageReplacedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (LOCAL_LOGV) Log.v(TAG, "onReceive(" + context + "," + intent + ")"); if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) { - Log.d(TAG, "updating to version " + new Preferences(context).getVersion(true)); + Log.d(TAG, "now installed version: " + new Preferences(context).getVersion(true)); // just post event and let application handle the rest App.bus.post(new AutoBackupSettingsChangedEvent()); } else { diff --git a/app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java b/app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java index 545061783..064c9a358 100644 --- a/app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java +++ b/app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java @@ -56,7 +56,7 @@ public class BackupJobs { static final String CONTENT_TRIGGER_TAG = "contentTrigger"; private final Preferences preferences; - private FirebaseJobDispatcher firebaseJobDispatcher; + private final FirebaseJobDispatcher firebaseJobDispatcher; public BackupJobs(Context context) { this(context, new Preferences(context)); @@ -65,7 +65,7 @@ public BackupJobs(Context context) { BackupJobs(Context context, Preferences preferences) { this.preferences = preferences; firebaseJobDispatcher = new FirebaseJobDispatcher( - this.preferences.isUseOldScheduler() ? + preferences.isUseOldScheduler() ? new AlarmManagerDriver(context) : new GooglePlayDriver(context)); } diff --git a/app/src/main/java/com/zegoggles/smssync/service/BackupTask.java b/app/src/main/java/com/zegoggles/smssync/service/BackupTask.java index c307192da..a081394a0 100644 --- a/app/src/main/java/com/zegoggles/smssync/service/BackupTask.java +++ b/app/src/main/java/com/zegoggles/smssync/service/BackupTask.java @@ -24,7 +24,6 @@ import com.zegoggles.smssync.mail.MessageConverter; import com.zegoggles.smssync.mail.PersonLookup; import com.zegoggles.smssync.preferences.AuthPreferences; -import com.zegoggles.smssync.preferences.DataTypePreferences; import com.zegoggles.smssync.preferences.Preferences; import com.zegoggles.smssync.service.state.BackupState; import com.zegoggles.smssync.service.state.SmsSyncState; @@ -55,7 +54,6 @@ class BackupTask extends AsyncTask { private final ContactAccessor contactAccessor; private final TokenRefresher tokenRefresher; - BackupTask(@NonNull SmsBackupService service) { final Context context = service.getApplicationContext(); this.service = service; @@ -108,8 +106,11 @@ protected void onPreExecute() { App.bus.register(this); } - @Subscribe public void userCanceled(UserCanceled canceled) { - cancel(false); + @Subscribe public void canceled(CancelEvent cancelEvent) { + if (LOCAL_LOGV) { + Log.v(TAG, "canceled("+cancelEvent+")"); + } + cancel(cancelEvent.mayInterruptIfRunning()); } @Override protected BackupState doInBackground(BackupConfig... params) { diff --git a/app/src/main/java/com/zegoggles/smssync/service/CancelEvent.java b/app/src/main/java/com/zegoggles/smssync/service/CancelEvent.java new file mode 100644 index 000000000..d21b763d5 --- /dev/null +++ b/app/src/main/java/com/zegoggles/smssync/service/CancelEvent.java @@ -0,0 +1,27 @@ +package com.zegoggles.smssync.service; + +import static com.zegoggles.smssync.service.CancelEvent.Origin.SYSTEM; + +public class CancelEvent { + enum Origin { USER, SYSTEM } + private final Origin origin; + + public CancelEvent() { + this(Origin.USER); + } + + CancelEvent(Origin origin) { + this.origin = origin; + } + + public boolean mayInterruptIfRunning() { + return origin == SYSTEM; + } + + @Override + public String toString() { + return "CancelEvent{" + + "origin=" + origin + + '}'; + } +} diff --git a/app/src/main/java/com/zegoggles/smssync/service/RestoreTask.java b/app/src/main/java/com/zegoggles/smssync/service/RestoreTask.java index c0d356ca9..63548dcd0 100644 --- a/app/src/main/java/com/zegoggles/smssync/service/RestoreTask.java +++ b/app/src/main/java/com/zegoggles/smssync/service/RestoreTask.java @@ -72,8 +72,8 @@ protected void onPreExecute() { App.bus.register(this); } - @Subscribe public void userCanceled(UserCanceled canceled) { - cancel(false); + @Subscribe public void canceled(CancelEvent canceled) { + cancel(canceled.mayInterruptIfRunning()); } @NonNull protected RestoreState doInBackground(RestoreConfig... params) { @@ -193,7 +193,7 @@ protected void onPostExecute(RestoreState result) { @Override protected void onCancelled() { - Log.d(TAG, "restore canceled by user"); + Log.d(TAG, "restore cancelled"); post(transition(CANCELED_RESTORE, null)); App.bus.unregister(this); } diff --git a/app/src/main/java/com/zegoggles/smssync/service/SmsJobService.java b/app/src/main/java/com/zegoggles/smssync/service/SmsJobService.java index 0cb4904e0..c3dd25aed 100644 --- a/app/src/main/java/com/zegoggles/smssync/service/SmsJobService.java +++ b/app/src/main/java/com/zegoggles/smssync/service/SmsJobService.java @@ -30,6 +30,7 @@ import static com.zegoggles.smssync.App.LOCAL_LOGV; import static com.zegoggles.smssync.App.TAG; import static com.zegoggles.smssync.service.BackupType.REGULAR; +import static com.zegoggles.smssync.service.CancelEvent.Origin.SYSTEM; public class SmsJobService extends JobService { @@ -71,8 +72,7 @@ public boolean onStartJob(JobParameters jobParameters) { if (LOCAL_LOGV) { Log.v(TAG, "scheduling follow-up job for content triggered job "+jobParameters); } - new BackupJobs(this).scheduleIncoming(); - jobFinished(jobParameters, false); + getBackupJobs().scheduleIncoming(); return false; } else if (shouldRun(jobParameters)) { startService(new Intent(this, SmsBackupService.class).putExtras(extras)); @@ -85,11 +85,11 @@ public boolean onStartJob(JobParameters jobParameters) { } } - private boolean wasTriggeredByContentUri(JobParameters jobParameters) { - return BackupJobs.CONTENT_TRIGGER_TAG.equals(jobParameters.getTag()); - } - /** + * Called when the scheduling engine has decided to interrupt the execution of a running job, most + * likely because the runtime constraints associated with the job are no longer satisfied. The job + * must stop execution. + * * @return true if the job should be retried */ @Override @@ -97,6 +97,7 @@ public boolean onStopJob(JobParameters jobParameters) { if (LOCAL_LOGV) { Log.v(TAG, "onStopJob(" + jobParameters + ", extras=" + jobParameters.getExtras() + ")"); } + App.bus.post(new CancelEvent(SYSTEM)); return false; } @@ -117,17 +118,25 @@ public void backupStateChanged(BackupState state) { } } + private boolean wasTriggeredByContentUri(JobParameters jobParameters) { + return BackupJobs.CONTENT_TRIGGER_TAG.equals(jobParameters.getTag()); + } + private boolean shouldRun(JobParameters jobParameters) { if (BackupType.fromName(jobParameters.getTag()) == REGULAR) { final Preferences prefs = new Preferences(this); final boolean autoBackupEnabled = prefs.isEnableAutoSync(); if (!autoBackupEnabled) { // was disabled in meantime, cancel - new BackupJobs(this).cancelRegular(); + getBackupJobs().cancelRegular(); } return autoBackupEnabled; } else { return true; } } + + private BackupJobs getBackupJobs() { + return new BackupJobs(this); + } } diff --git a/app/src/main/java/com/zegoggles/smssync/service/UserCanceled.java b/app/src/main/java/com/zegoggles/smssync/service/UserCanceled.java deleted file mode 100644 index 027b34a53..000000000 --- a/app/src/main/java/com/zegoggles/smssync/service/UserCanceled.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.zegoggles.smssync.service; - -public class UserCanceled { -} diff --git a/app/src/test/java/com/zegoggles/smssync/service/SmsJobServiceTest.java b/app/src/test/java/com/zegoggles/smssync/service/SmsJobServiceTest.java index 021444eae..051393a0d 100644 --- a/app/src/test/java/com/zegoggles/smssync/service/SmsJobServiceTest.java +++ b/app/src/test/java/com/zegoggles/smssync/service/SmsJobServiceTest.java @@ -21,12 +21,17 @@ public void setUp() throws Exception { smsJobService = setupService(SmsJobService.class); } - @Test - public void testOnStartJob() { + @Test public void testOnStartJob() { final JobParameters jobParameters = mock(JobParameters.class);; when(jobParameters.getTag()).thenReturn(BackupJobs.CONTENT_TRIGGER_TAG); boolean moreWork = smsJobService.onStartJob(jobParameters); assertThat(moreWork).isFalse(); } + + @Test public void testOnStopJob() { + final JobParameters jobParameters = mock(JobParameters.class); + boolean shouldRetry = smsJobService.onStopJob(jobParameters); + assertThat(shouldRetry).isFalse(); + } }