Skip to content

Commit c3d7361

Browse files
committed
Add cancel logging
1 parent 2290389 commit c3d7361

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

app/src/main/java/com/zegoggles/smssync/activity/MainActivity.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,24 @@ public boolean onOptionsItemSelected(MenuItem item) {
273273
}
274274
}
275275

276+
@Subscribe public void autoBackupChanged(final AutoBackupChangedEvent event) {
277+
if (LOCAL_LOGV) {
278+
Log.v(TAG, "autoBackupChanged("+event+")");
279+
}
280+
final ComponentName componentName = new ComponentName(MainActivity.this, SmsBroadcastReceiver.class);
281+
282+
getPackageManager().setComponentEnabledSetting(
283+
componentName,
284+
event.autoBackupEnabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED,
285+
DONT_KILL_APP);
286+
287+
if (event.autoBackupEnabled) {
288+
getBackupJobs().scheduleFirstRegular();
289+
} else {
290+
getBackupJobs().cancel();
291+
}
292+
}
293+
276294
@Subscribe public void onOAuth2Callback(OAuth2CallbackTask.OAuth2CallbackEvent event) {
277295
dismiss(Dialogs.ACCESS_TOKEN);
278296
if (event.valid()) {
@@ -376,15 +394,15 @@ public void run() {
376394
WIFI_ONLY.key);
377395
}
378396

379-
private void addSummaryListener(final Runnable r, String... prefs) {
397+
private void addSummaryListener(final Runnable runnable, String... prefs) {
380398
for (String p : prefs) {
381399
findPreference(p).setOnPreferenceChangeListener(
382400
new OnPreferenceChangeListener() {
383401
public boolean onPreferenceChange(Preference preference, final Object newValue) {
384402
new Handler().post(new Runnable() {
385403
@Override
386404
public void run() {
387-
r.run();
405+
runnable.run();
388406
onContentChanged();
389407
}
390408
});
@@ -793,7 +811,7 @@ public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
793811
findPreference(ENABLE_AUTO_BACKUP.key)
794812
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
795813
public boolean onPreferenceChange(Preference preference, Object newValue) {
796-
autoBackupWillChange((Boolean) newValue);
814+
App.bus.post(new AutoBackupChangedEvent((Boolean) newValue));
797815
return true;
798816
}
799817
});
@@ -884,22 +902,6 @@ public void run() {
884902
}
885903
}
886904

887-
private void autoBackupWillChange(boolean autoBackupEnabled) {
888-
if (LOCAL_LOGV) {
889-
Log.v(TAG, "autoBackupWillChange("+autoBackupEnabled+")");
890-
}
891-
final ComponentName componentName = new ComponentName(MainActivity.this, SmsBroadcastReceiver.class);
892-
893-
getPackageManager().setComponentEnabledSetting(
894-
componentName,
895-
autoBackupEnabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED,
896-
DONT_KILL_APP);
897-
898-
if (!autoBackupEnabled) {
899-
getBackupJobs().cancel();
900-
}
901-
}
902-
903905
private BackupJobs getBackupJobs() {
904906
return new BackupJobs(this);
905907
}
@@ -988,4 +990,12 @@ private void checkDefaultSmsApp() {
988990
restoreDefaultSmsProvider(preferences.getSmsDefaultPackage());
989991
}
990992
}
993+
994+
public static class AutoBackupChangedEvent {
995+
final boolean autoBackupEnabled;
996+
997+
AutoBackupChangedEvent(boolean autoBackupEnabled) {
998+
this.autoBackupEnabled = autoBackupEnabled;
999+
}
1000+
}
9911001
}

app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.zegoggles.smssync.compat.GooglePlayServices;
3232
import com.zegoggles.smssync.preferences.Preferences;
3333

34+
import static com.firebase.jobdispatcher.FirebaseJobDispatcher.CANCEL_RESULT_SUCCESS;
3435
import static com.firebase.jobdispatcher.FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS;
3536
import static com.zegoggles.smssync.App.LOCAL_LOGV;
3637
import static com.zegoggles.smssync.App.TAG;
@@ -61,6 +62,10 @@ public Job scheduleIncoming() {
6162
return schedule(mPreferences.getIncomingTimeoutSecs(), INCOMING, false);
6263
}
6364

65+
public Job scheduleFirstRegular() {
66+
return schedule(mPreferences.getRegularTimeoutSecs(), REGULAR, true);
67+
}
68+
6469
public Job scheduleRegular() {
6570
return schedule(mPreferences.getRegularTimeoutSecs(), REGULAR, false);
6671
}
@@ -74,7 +79,14 @@ public Job scheduleImmediate() {
7479
}
7580

7681
public void cancel() {
77-
firebaseJobDispatcher.cancelAll();
82+
final int result = firebaseJobDispatcher.cancelAll();
83+
if (result == CANCEL_RESULT_SUCCESS) {
84+
if (LOCAL_LOGV) {
85+
Log.v(TAG, "cancel()");
86+
}
87+
} else {
88+
Log.w(TAG, "unable to cancel jobs: "+result);
89+
}
7890
}
7991

8092
@Nullable private Job schedule(int inSeconds, BackupType backupType, boolean force) {

0 commit comments

Comments
 (0)