Add geofence preferences

pull/281/head
Alex Baker 10 years ago
parent a508845889
commit fe20210ac0

@ -19,4 +19,8 @@ public class GeofenceApi {
public void cancel(Geofence geofence) {
}
public void cancel(List<Geofence> geofences) {
}
}

@ -6,20 +6,13 @@ import android.content.DialogInterface;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;

@ -14,11 +14,14 @@ import com.google.common.base.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.Preferences;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static com.google.android.gms.location.Geofence.NEVER_EXPIRE;
@ -31,14 +34,16 @@ public class GeofenceApi {
private static final Logger log = LoggerFactory.getLogger(GeofenceApi.class);
private Context context;
private Preferences preferences;
@Inject
public GeofenceApi(@ForApplication Context context) {
public GeofenceApi(@ForApplication Context context, Preferences preferences) {
this.context = context;
this.preferences = preferences;
}
public void register(final List<Geofence> geofences) {
if (geofences.isEmpty()) {
if (geofences.isEmpty() || !preferences.geofencesEnabled()) {
return;
}
@ -66,19 +71,32 @@ public class GeofenceApi {
}
public void cancel(final Geofence geofence) {
cancel(singletonList(geofence));
}
public void cancel(final List<Geofence> geofences) {
if (geofences.isEmpty()) {
return;
}
final List<String> ids = newArrayList(transform(geofences, new Function<Geofence, String>() {
@Override
public String apply(Geofence geofence) {
return Long.toString(geofence.getMetadataId());
}
}));
newClient(new GoogleApi.GoogleApiClientConnectionHandler() {
@Override
public void onConnect(final GoogleApiClient client) {
LocationServices.GeofencingApi.removeGeofences(
client,
singletonList(Long.toString(geofence.getMetadataId())))
LocationServices.GeofencingApi.removeGeofences(client, ids)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
log.info("Removed {}", geofence);
log.info("Removed {}", geofences);
} else {
log.error("Failed to remove {}", geofence);
log.error("Failed to remove {}", geofences);
}
client.disconnect();
@ -102,9 +120,11 @@ public class GeofenceApi {
}
private com.google.android.gms.location.Geofence toGoogleGeofence(Geofence geofence) {
int radius = preferences.getIntegerFromString(R.string.p_geofence_radius, 250);
int responsiveness = (int) TimeUnit.SECONDS.toMillis(preferences.getIntegerFromString(R.string.p_geofence_responsiveness, 60));
return new com.google.android.gms.location.Geofence.Builder()
.setCircularRegion(geofence.getLatitude(), geofence.getLongitude(), geofence.getRadius())
.setNotificationResponsiveness((int) TimeUnit.SECONDS.toMillis(30))
.setCircularRegion(geofence.getLatitude(), geofence.getLongitude(), radius)
.setNotificationResponsiveness(responsiveness)
.setRequestId(Long.toString(geofence.getMetadataId()))
.setTransitionTypes(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.setExpirationDuration(NEVER_EXPIRE)

@ -388,7 +388,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
controlSetMap.put(getString(R.string.TEA_ctrl_notes_pref),
notesControlSet);
reminderControlSet = new ReminderControlSet(alarmService, geofenceService, this);
reminderControlSet = new ReminderControlSet(alarmService, geofenceService, this, preferences);
controls.add(reminderControlSet);
controlSetMap.put(getString(R.string.TEA_ctrl_reminders_pref), reminderControlSet);

@ -17,8 +17,11 @@ import org.joda.time.DateTime;
import org.tasks.R;
import org.tasks.activities.TimePickerActivity;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.DeviceInfo;
import org.tasks.ui.TimePreference;
import javax.inject.Inject;
import static com.todoroo.andlib.utility.AndroidUtilities.preJellybean;
public class ReminderPreferences extends InjectingPreferenceActivity {
@ -26,23 +29,46 @@ 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";
private Bundle result;
@Inject DeviceInfo deviceInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = savedInstanceState == null ? new Bundle() : savedInstanceState.getBundle(EXTRA_RESULT);
addPreferencesFromResource(R.xml.preferences_reminders);
if (preJellybean()) {
getPreferenceScreen().removePreference(findPreference(getString(R.string.p_rmd_notif_actions_enabled)));
}
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 {
getPreferenceScreen().removePreference(findPreference(getString(R.string.geolocation_reminders)));
}
initializeRingtonePreference();
initializeTimePreference(getDefaultRemindTimePreference(), REQUEST_DEFAULT_REMIND);
initializeTimePreference(getQuietStartPreference(), REQUEST_QUIET_START);
initializeTimePreference(getQuietEndPreference(), REQUEST_QUIET_END);
}
@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
@ -112,4 +138,15 @@ 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;
}
});
}
}

@ -6,6 +6,7 @@
package com.todoroo.astrid.ui;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
@ -33,6 +34,7 @@ import org.tasks.activities.DateAndTimePickerActivity;
import org.tasks.activities.LocationPickerActivity;
import org.tasks.location.Geofence;
import org.tasks.location.GeofenceService;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.Date;
@ -66,15 +68,18 @@ public class ReminderControlSet extends TaskEditControlSetBase implements Adapte
private AlarmService alarmService;
private GeofenceService geofenceService;
private TaskEditFragment taskEditFragment;
private Preferences preferences;
private List<String> spinnerOptions = new ArrayList<>();
private ArrayAdapter<String> remindAdapter;
public ReminderControlSet(AlarmService alarmService, GeofenceService geofenceService, TaskEditFragment taskEditFragment) {
public ReminderControlSet(AlarmService alarmService, GeofenceService geofenceService,
TaskEditFragment taskEditFragment, Preferences preferences) {
super(taskEditFragment.getActivity(), R.layout.control_set_reminders);
this.alarmService = alarmService;
this.geofenceService = geofenceService;
this.taskEditFragment = taskEditFragment;
this.preferences = preferences;
}
public int getValue() {
@ -126,6 +131,10 @@ public class ReminderControlSet extends TaskEditControlSetBase implements Adapte
}
});
alertItem.setTag(geofence);
if (!preferences.geofencesEnabled()) {
TextView alarmString = (TextView) alertItem.findViewById(R.id.alarm_string);
alarmString.setPaintFlags(alarmString.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
}
private View addAlarmRow(String text, Long timestamp, final OnClickListener onRemove) {
@ -165,7 +174,7 @@ public class ReminderControlSet extends TaskEditControlSetBase implements Adapte
if (randomControlSet == null) {
spinnerOptions.add(taskEditFragment.getString(R.string.randomly));
}
if (taskEditFragment.getResources().getBoolean(R.bool.location_enabled)) {
if (preferences.geofencesEnabled()) {
spinnerOptions.add(taskEditFragment.getString(R.string.pick_a_location));
}
spinnerOptions.add(taskEditFragment.getString(R.string.pick_a_date_and_time));

@ -16,6 +16,9 @@ import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.SynchronizeMetadataCallback;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -53,6 +56,10 @@ public class GeofenceService {
geofenceApi.register(getGeofencesForTask(taskId));
}
public void cancelGeofences() {
geofenceApi.cancel(getActiveGeofences());
}
public void cancelGeofences(long taskId) {
for (Geofence geofence : getGeofences(taskId)) {
geofenceApi.cancel(geofence);

@ -24,8 +24,8 @@ public class ActivityPreferences extends Preferences {
private final Activity activity;
@Inject
public ActivityPreferences(Activity activity) {
super(activity);
public ActivityPreferences(Activity activity, DeviceInfo deviceInfo) {
super(activity, deviceInfo);
this.activity = activity;
}

@ -1,10 +1,13 @@
package org.tasks.preferences;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import com.todoroo.astrid.reminders.ReminderPreferences;
import org.tasks.R;
import org.tasks.injection.InjectingPreferenceActivity;
@ -12,14 +15,18 @@ import javax.inject.Inject;
public class BasicPreferences extends InjectingPreferenceActivity {
private static final String EXTRA_RESULT = "extra_result";
private static final int RC_PREFS = 10001;
@Inject DeviceInfo deviceInfo;
private Bundle result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = savedInstanceState == null ? new Bundle() : savedInstanceState.getBundle(EXTRA_RESULT);
addPreferencesFromResource(R.xml.preferences);
if (!getResources().getBoolean(R.bool.sync_enabled)) {
getPreferenceScreen().removePreference(findPreference(getString(R.string.synchronization)));
@ -31,6 +38,13 @@ public class BasicPreferences extends InjectingPreferenceActivity {
return true;
}
});
findPreference(getString(R.string.notifications)).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(new Intent(BasicPreferences.this, ReminderPreferences.class), RC_PREFS);
return true;
}
});
findPreference(getString(R.string.contact_developer)).setIntent(
new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "Tasks Support <support@tasks.org>", null)) {{
@ -45,10 +59,21 @@ public class BasicPreferences extends InjectingPreferenceActivity {
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(EXTRA_RESULT, result);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_PREFS) {
setResult(resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
result.putAll(data.getExtras());
setResult(Activity.RESULT_OK, new Intent() {{
putExtras(result);
}});
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}

@ -58,6 +58,10 @@ public class DeviceInfo {
return context.getResources().getBoolean(R.bool.billing_enabled) && isPlayStoreAvailable();
}
public boolean supportsLocationServices() {
return context.getResources().getBoolean(R.bool.location_enabled);
}
public String getDebugInfo() {
if (debugInfo == null) {
debugInfo = buildDebugString();

@ -1,5 +1,6 @@
package org.tasks.preferences;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
@ -49,12 +50,14 @@ public class Preferences {
private static final String FILE_APPENDER_NAME = "FILE";
protected final Context context;
private DeviceInfo deviceInfo;
private final SharedPreferences prefs;
private final SharedPreferences publicPrefs;
@Inject
public Preferences(@ForApplication Context context) {
public Preferences(@ForApplication Context context, DeviceInfo deviceInfo) {
this.context = context;
this.deviceInfo = deviceInfo;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
publicPrefs = context.getSharedPreferences(AstridApiConstants.PUBLIC_PREFS, Context.MODE_WORLD_READABLE);
}
@ -68,6 +71,10 @@ public class Preferences {
return getBoolean(WidgetConfigActivity.PREF_DARK_THEME + widgetId, legacySetting);
}
public boolean geofencesEnabled() {
return deviceInfo.supportsLocationServices() && getBoolean(R.string.p_geofence_reminders_enabled, true);
}
public void clear() {
prefs
.edit()

@ -35,6 +35,7 @@ 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -43,8 +44,10 @@ import org.tasks.filters.FilterCounter;
import org.tasks.filters.FilterProvider;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingFragment;
import org.tasks.location.GeofenceService;
import org.tasks.preferences.AppearancePreferences;
import org.tasks.preferences.BasicPreferences;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
@ -86,6 +89,8 @@ public class NavigationDrawerFragment extends InjectingFragment {
@Inject FilterCounter filterCounter;
@Inject FilterProvider filterProvider;
@Inject @ForApplication Context context;
@Inject GeofenceService geofenceService;
@Inject Preferences preferences;
public NavigationDrawerFragment() {
}
@ -112,12 +117,22 @@ public class NavigationDrawerFragment extends InjectingFragment {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_SETTINGS && resultCode == Activity.RESULT_OK) {
if (requestCode == ACTIVITY_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();
}
if (data.getBooleanExtra(AppearancePreferences.FILTERS_CHANGED, false)) {
refresh();
}
if (data.getBooleanExtra(AppearancePreferences.FORCE_REFRESH, false)) {
getActivity().finish();
getActivity().startActivity(getActivity().getIntent());
} else if (data.getBooleanExtra(AppearancePreferences.FILTERS_CHANGED, false)) {
refresh();
}
} else {
super.onActivityResult(requestCode, resultCode, data);

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="geofence_radius_values">
<item>75</item>
<item>125</item>
<item>250</item>
<item>500</item>
<item>1000</item>
</string-array>
<string-array name="geofence_responsiveness">
<item>00:15</item>
<item>00:30</item>
<item>01:00</item>
<item>05:00</item>
<item>10:00</item>
</string-array>
<string-array name="geofence_responsiveness_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>300</item>
<item>600</item>
</string-array>
</resources>

@ -236,8 +236,11 @@
<string name="TEA_ctrl_gcal">TEA_ctrl_gcal</string>
<string name="sync_gtasks">sync_gtasks</string>
<string name="p_rmd_notif_actions_enabled">p_rmd_notif_actions_enabled</string>
<string name="p_geofence_radius">geolocation_radius</string>
<string name="p_geofence_responsiveness">geofence_responsiveness</string>
<string name="p_geofence_reminders_enabled">geofence_reminders_enabled</string>
<string-array name="TEA_control_sets_prefs">
<string-array name="TEA_control_sets_prefs">
<item>@string/TEA_ctrl_when_pref</item>
<item>@string/TEA_ctrl_repeat_pref</item>
<item>@string/TEA_ctrl_importance_pref</item>

@ -103,6 +103,9 @@
<string name="type_a_location">Type a location</string>
<string name="when_overdue">When overdue</string>
<string name="when_due">When due</string>
<string name="geofence_radius_title">Radius (meters)</string>
<string name="geofence_responsiveness_title">Response time (higher value conserves battery)</string>
<string name="geolocation_reminders">Geolocation Reminders</string>
<string-array name="sync_SPr_interval_entries">
<!-- sync_SPr_interval_entries: Synchronization Intervals -->

@ -5,11 +5,9 @@
android:key="@string/EPr_appearance_header"
android:title="@string/EPr_appearance_header" />
<Preference android:title="@string/notifications">
<intent
android:targetClass="com.todoroo.astrid.reminders.ReminderPreferences"
android:targetPackage="org.tasks" />
</Preference>
<Preference
android:key="@string/notifications"
android:title="@string/notifications" />
<Preference android:title="@string/task_defaults">
<intent

@ -50,21 +50,49 @@
android:key="@string/p_rmd_maxvolume"
android:summary="@string/rmd_EPr_multiple_maxvolume_desc_true"
android:title="@string/rmd_EPr_multiple_maxvolume_title" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="@string/p_rmd_enabled"
android:key="@string/p_rmd_enable_quiet"
android:summary="@string/quiet_hours_summary"
android:title="@string/quiet_hours" />
<org.tasks.ui.TimePreference
android:defaultValue="@integer/default_quiet_hours_start"
android:dependency="@string/p_rmd_enable_quiet"
android:key="@string/p_rmd_quietStart"
android:title="@string/rmd_EPr_quiet_hours_start_title" />
<org.tasks.ui.TimePreference
android:defaultValue="@integer/default_quiet_hours_end"
android:dependency="@string/p_rmd_enable_quiet"
android:key="@string/p_rmd_quietEnd"
android:title="@string/rmd_EPr_quiet_hours_end_title" />
<PreferenceCategory
android:key="@string/geolocation_reminders"
android:title="@string/geolocation_reminders">
<CheckBoxPreference
android:defaultValue="true"
android:dependency="@string/p_rmd_enabled"
android:key="@string/p_geofence_reminders_enabled"
android:title="@string/enabled" />
<com.todoroo.astrid.ui.MultilineListPreference
android:defaultValue="250"
android:dependency="@string/p_geofence_reminders_enabled"
android:entries="@array/geofence_radius_values"
android:entryValues="@array/geofence_radius_values"
android:key="@string/p_geofence_radius"
android:title="@string/geofence_radius_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:defaultValue="60"
android:dependency="@string/p_geofence_reminders_enabled"
android:entries="@array/geofence_responsiveness"
android:entryValues="@array/geofence_responsiveness_values"
android:key="@string/p_geofence_responsiveness"
android:title="@string/geofence_responsiveness_title" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/quiet_hours">
<CheckBoxPreference
android:defaultValue="false"
android:dependency="@string/p_rmd_enabled"
android:key="@string/p_rmd_enable_quiet"
android:summary="@string/quiet_hours_summary"
android:title="@string/enabled" />
<org.tasks.ui.TimePreference
android:defaultValue="@integer/default_quiet_hours_start"
android:dependency="@string/p_rmd_enable_quiet"
android:key="@string/p_rmd_quietStart"
android:title="@string/rmd_EPr_quiet_hours_start_title" />
<org.tasks.ui.TimePreference
android:defaultValue="@integer/default_quiet_hours_end"
android:dependency="@string/p_rmd_enable_quiet"
android:key="@string/p_rmd_quietEnd"
android:title="@string/rmd_EPr_quiet_hours_end_title" />
</PreferenceCategory>
</PreferenceScreen>

Loading…
Cancel
Save