Skip to content

Commit

Permalink
Handle JobScheduler cancel event
Browse files Browse the repository at this point in the history
  • Loading branch information
jberkel committed Dec 8, 2017
1 parent d2e80d0 commit 42af5ea
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
</intent-filter>
</receiver>

<receiver android:name=".receiver.UpdateReceiver" android:enabled="true">
<receiver android:name=".receiver.PackageReplacedReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,7 +54,6 @@ class BackupTask extends AsyncTask<BackupConfig, BackupState, BackupState> {
private final ContactAccessor contactAccessor;
private final TokenRefresher tokenRefresher;


BackupTask(@NonNull SmsBackupService service) {
final Context context = service.getApplicationContext();
this.service = service;
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/zegoggles/smssync/service/CancelEvent.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
23 changes: 16 additions & 7 deletions app/src/main/java/com/zegoggles/smssync/service/SmsJobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
Expand All @@ -85,18 +85,19 @@ 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
public boolean onStopJob(JobParameters jobParameters) {
if (LOCAL_LOGV) {
Log.v(TAG, "onStopJob(" + jobParameters + ", extras=" + jobParameters.getExtras() + ")");
}
App.bus.post(new CancelEvent(SYSTEM));
return false;
}

Expand All @@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 42af5ea

Please sign in to comment.