Run geofence & reminder schedulers on pref change

pull/384/head
Alex Baker 10 years ago
parent 75f3c509da
commit cf3d13b4d4

@ -475,7 +475,7 @@
</receiver>
<service
android:name=".scheduling.AlarmSchedulingIntentService"
android:name=".scheduling.GeofenceSchedulingIntentService"
android:exported="false" />
<service
android:name=".scheduling.BackupIntentService"

@ -300,10 +300,6 @@ public class AndroidUtilities {
}
}
public static boolean preJellybean() {
return !atLeastJellybean();
}
public static boolean preLollipop() {
return !atLeastLollipop();
}

@ -14,33 +14,28 @@ import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.time.DateTime;
import org.tasks.R;
import org.tasks.activities.TimePickerActivity;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.DeviceInfo;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.scheduling.GeofenceSchedulingIntentService;
import org.tasks.scheduling.ReminderSchedulerIntentService;
import org.tasks.time.DateTime;
import org.tasks.ui.TimePreference;
import javax.inject.Inject;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybean;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow;
import static com.todoroo.andlib.utility.AndroidUtilities.preJellybean;
public class ReminderPreferences extends InjectingPreferenceActivity {
private static final int REQUEST_QUIET_START = 10001;
private static final int REQUEST_QUIET_END = 10002;
private static final int REQUEST_DEFAULT_REMIND = 10003;
private static final String EXTRA_RESULT = "extra_result";
public static String RESET_GEOFENCES = "reset_geofences";
public static String TOGGLE_GEOFENCES = "toggle_geofences";
public static String RESCHEDULE_ALARMS = "reschedule_alarms";
private Bundle result;
@Inject DeviceInfo deviceInfo;
@Inject PermissionRequestor permissionRequestor;
@ -52,28 +47,18 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = savedInstanceState == null ? new Bundle() : savedInstanceState.getBundle(EXTRA_RESULT);
addPreferencesFromResource(R.xml.preferences_reminders);
PreferenceScreen preferenceScreen = getPreferenceScreen();
if (preJellybean()) {
preferenceScreen.removePreference(findPreference(getString(R.string.p_rmd_notif_actions_enabled)));
preferenceScreen.removePreference(findPreference(getString(R.string.p_notification_priority)));
}
if (atLeastMarshmallow()) {
setExtraOnChange(R.string.p_doze_notifications, RESCHEDULE_ALARMS);
} else {
preferenceScreen.removePreference(findPreference(getString(R.string.p_doze_notifications)));
}
if (deviceInfo.supportsLocationServices()) {
setExtraOnChange(R.string.p_geofence_radius, RESET_GEOFENCES);
setExtraOnChange(R.string.p_geofence_responsiveness, RESET_GEOFENCES);
setExtraOnChange(R.string.p_geofence_reminders_enabled, TOGGLE_GEOFENCES);
} else {
preferenceScreen.removePreference(findPreference(getString(R.string.geolocation_reminders)));
}
rescheduleNotificationsOnChange(
R.string.p_rmd_time,
R.string.p_doze_notifications,
R.string.p_rmd_enable_quiet,
R.string.p_rmd_quietStart,
R.string.p_rmd_quietEnd);
resetGeofencesOnChange(
R.string.p_geofence_radius,
R.string.p_geofence_responsiveness,
R.string.p_geofence_reminders_enabled);
fieldMissedCalls = (CheckBoxPreference) findPreference(getString(R.string.p_field_missed_calls));
fieldMissedCalls.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@ -88,6 +73,34 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
initializeTimePreference(getDefaultRemindTimePreference(), REQUEST_DEFAULT_REMIND);
initializeTimePreference(getQuietStartPreference(), REQUEST_QUIET_START);
initializeTimePreference(getQuietEndPreference(), REQUEST_QUIET_END);
requires(atLeastJellybean(), R.string.p_rmd_notif_actions_enabled, R.string.p_notification_priority);
requires(atLeastMarshmallow(), R.string.p_doze_notifications);
requires(deviceInfo.supportsLocationServices(), R.string.geolocation_reminders);
}
private void rescheduleNotificationsOnChange(int... resIds) {
for (int resId : resIds) {
findPreference(getString(resId)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
startService(new Intent(ReminderPreferences.this, ReminderSchedulerIntentService.class));
return true;
}
});
}
}
private void resetGeofencesOnChange(int... resIds) {
for (int resId : resIds) {
findPreference(getString(resId)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
startService(new Intent(ReminderPreferences.this, GeofenceSchedulingIntentService.class));
return true;
}
});
}
}
@Override
@ -104,12 +117,6 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(EXTRA_RESULT, result);
}
private void initializeTimePreference(final TimePreference preference, final int requestCode) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
@ -179,15 +186,4 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
private TimePreference getTimePreference(int resId) {
return (TimePreference) findPreference(getString(resId));
}
private void setExtraOnChange(int resId, final String extra) {
findPreference(getString(resId)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
result.putBoolean(extra, true);
setResult(RESULT_OK, new Intent().putExtras(result));
return true;
}
});
}
}

@ -2,6 +2,7 @@ package org.tasks.injection;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceScreen;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
@ -72,4 +73,13 @@ public abstract class InjectingPreferenceActivity extends AppCompatPreferenceAct
tracker.showScreen(getClass().getSimpleName());
}
protected void requires(boolean passesCheck, int... resIds) {
if (!passesCheck) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int resId : resIds) {
preferenceScreen.removePreference(findPreference(getString(resId)));
}
}
}
}

@ -7,7 +7,7 @@ import dagger.Module;
@Module(addsTo = TasksModule.class,
injects = {
AlarmSchedulingIntentService.class,
GeofenceSchedulingIntentService.class,
BackupIntentService.class,
GtasksBackgroundService.class,
MidnightRefreshService.class,

@ -18,7 +18,7 @@ public class BackgroundScheduler {
public void scheduleEverything() {
context.startService(new Intent(context, RefreshSchedulerIntentService.class));
context.startService(new Intent(context, AlarmSchedulingIntentService.class));
context.startService(new Intent(context, GeofenceSchedulingIntentService.class));
context.startService(new Intent(context, ReminderSchedulerIntentService.class));
scheduleBackupService();
scheduleMidnightRefresh();

@ -2,8 +2,6 @@ package org.tasks.scheduling;
import android.content.Intent;
import com.todoroo.astrid.alarms.AlarmService;
import org.tasks.injection.InjectingIntentService;
import org.tasks.location.GeofenceService;
@ -11,13 +9,12 @@ import javax.inject.Inject;
import timber.log.Timber;
public class AlarmSchedulingIntentService extends InjectingIntentService {
public class GeofenceSchedulingIntentService extends InjectingIntentService {
@Inject AlarmService alarmService;
@Inject GeofenceService geofenceService;
public AlarmSchedulingIntentService() {
super(AlarmSchedulingIntentService.class.getSimpleName());
public GeofenceSchedulingIntentService() {
super(GeofenceSchedulingIntentService.class.getSimpleName());
}
@Override
@ -26,7 +23,7 @@ public class AlarmSchedulingIntentService extends InjectingIntentService {
Timber.d("onHandleIntent(%s)", intent);
alarmService.scheduleAllAlarms();
geofenceService.cancelGeofences();
geofenceService.setupGeofences();
}
}

@ -2,6 +2,7 @@ package org.tasks.scheduling;
import android.content.Intent;
import com.todoroo.astrid.alarms.AlarmService;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.reminders.ReminderService;
@ -13,6 +14,7 @@ import timber.log.Timber;
public class ReminderSchedulerIntentService extends InjectingIntentService {
@Inject AlarmService alarmService;
@Inject ReminderService reminderService;
@Inject TaskDao taskDao;
@ -27,5 +29,6 @@ public class ReminderSchedulerIntentService extends InjectingIntentService {
Timber.d("onHandleIntent(%s)", intent);
reminderService.scheduleAllAlarms(taskDao);
alarmService.scheduleAllAlarms();
}
}

@ -22,17 +22,13 @@ import com.todoroo.astrid.adapter.FilterAdapter;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterListItem;
import com.todoroo.astrid.reminders.ReminderPreferences;
import org.tasks.R;
import org.tasks.filters.FilterCounter;
import org.tasks.filters.FilterProvider;
import org.tasks.filters.NavigationDrawerAction;
import org.tasks.injection.InjectingFragment;
import org.tasks.location.GeofenceService;
import org.tasks.preferences.AppearancePreferences;
import org.tasks.preferences.Preferences;
import org.tasks.scheduling.AlarmSchedulingIntentService;
import javax.inject.Inject;
@ -65,8 +61,6 @@ public class NavigationDrawerFragment extends InjectingFragment {
@Inject FilterCounter filterCounter;
@Inject FilterProvider filterProvider;
@Inject GeofenceService geofenceService;
@Inject Preferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -89,19 +83,6 @@ public class NavigationDrawerFragment extends InjectingFragment {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FilterAdapter.REQUEST_SETTINGS && resultCode == Activity.RESULT_OK && data != null) {
if (data.getBooleanExtra(ReminderPreferences.TOGGLE_GEOFENCES, false)) {
if (preferences.geofencesEnabled()) {
geofenceService.setupGeofences();
} else {
geofenceService.cancelGeofences();
}
} else if (data.getBooleanExtra(ReminderPreferences.RESET_GEOFENCES, false)) {
geofenceService.setupGeofences();
} else if (data.getBooleanExtra(ReminderPreferences.RESCHEDULE_ALARMS, false)) {
Context context = getContext();
context.startService(new Intent(context, AlarmSchedulingIntentService.class));
}
if (data.getBooleanExtra(AppearancePreferences.FILTERS_CHANGED, false)) {
refresh();
}

Loading…
Cancel
Save