Set default prefs from XML

pull/253/head
Alex Baker 10 years ago
parent 8e619bdd21
commit c29efa18fd

@ -1,126 +0,0 @@
package com.todoroo.astrid.utility;
import android.annotation.SuppressLint;
import android.test.AndroidTestCase;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.concurrent.TimeUnit;
public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
@SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
Preferences preferences;
AstridDefaultPreferenceSpec spec;
@Override
public void setUp() {
preferences = new Preferences(getContext());
spec = new AstridDefaultPreferenceSpec(getContext(), preferences);
preferences.clear();
assertFalse(hasMigrated());
}
public void testQuietHoursEnabledAfterMigration() {
setOldQuietHoursStart(1);
spec.migrateToNewQuietHours();
assertTrue(quietHoursEnabled());
}
public void testQuietHoursDisabledAfterMigration() {
preferences.setString(getContext().getString(R.string.p_rmd_quietStart_old), "");
spec.migrateToNewQuietHours();
assertFalse(quietHoursEnabled());
}
public void testMigrateFromStartOfDefaultReminderTimeArray() {
setOldReminderTime(0);
spec.migrateToNewQuietHours();
assertEquals(0, newReminderTime());
}
public void testMigrateFromEndOfDefaultReminderTimeArray() {
setOldReminderTime(23);
spec.migrateToNewQuietHours();
assertEquals(23 * MILLIS_PER_HOUR, newReminderTime());
}
public void testMigrateFromStartOfQuietHourStartArray() {
setOldQuietHoursStart(1);
spec.migrateToNewQuietHours();
assertEquals(MILLIS_PER_HOUR, newQuietHoursStartTime());
}
public void testMigrateFromEndOfQuietHoursStartArray() {
setOldQuietHoursStart(23);
spec.migrateToNewQuietHours();
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursStartTime());
}
public void testMigrateFromStartOfQuietHoursEndArray() {
setOldQuietHoursStart(1);
setOldQuietHoursEnd(0);
spec.migrateToNewQuietHours();
assertEquals(0, newQuietHoursEndTime());
}
public void testMigrateFromEndOfQuietHoursEndArray() {
setOldQuietHoursStart(1);
setOldQuietHoursEnd(23);
spec.migrateToNewQuietHours();
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursEndTime());
}
private boolean quietHoursEnabled() {
return preferences.getBoolean(R.string.p_rmd_enable_quiet, false);
}
private boolean hasMigrated() {
return preferences.getBoolean(R.string.p_rmd_hasMigrated, false);
}
private void setOldQuietHoursStart(int index) {
preferences.setStringFromInteger(R.string.p_rmd_quietStart_old, index);
}
private void setOldQuietHoursEnd(int index) {
preferences.setStringFromInteger(R.string.p_rmd_quietEnd_old, index);
}
private void setOldReminderTime(int index) {
preferences.setStringFromInteger(R.string.p_rmd_time_old, index);
}
private int newQuietHoursStartTime() {
return preferences.getInt(R.string.p_rmd_quietStart);
}
private int newQuietHoursEndTime() {
return preferences.getInt(R.string.p_rmd_quietEnd);
}
private int newReminderTime() {
return preferences.getInt(R.string.p_rmd_time);
}
}

@ -253,10 +253,7 @@ public class TaskDao {
0));
}
if(!item.containsValue(Task.REMINDER_FLAGS)) {
int reminder_flags = preferences.getIntegerFromString(R.string.p_default_reminders_key,
Task.NOTIFY_AT_DEADLINE | Task.NOTIFY_AFTER_DEADLINE) |
preferences.getIntegerFromString(R.string.p_default_reminders_mode_key, 0);
item.setReminderFlags(reminder_flags);
item.setReminderFlags(preferences.getDefaultReminders() | preferences.getDefaultRingMode());
}
}

@ -9,9 +9,6 @@ import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
@ -89,32 +86,10 @@ public final class ReminderService {
ReminderService(@ForApplication Context context, Preferences preferences) {
this.context = context;
this.preferences = preferences;
setPreferenceDefaults();
}
// --- preference handling
private static boolean preferencesInitialized = false;
private static final int MILLIS_PER_HOUR = 60 * 60 * 1000;
/** Set preference defaults, if unset. called at startup */
public void setPreferenceDefaults() {
if(preferencesInitialized) {
return;
}
SharedPreferences prefs = preferences.getPrefs();
Editor editor = prefs.edit();
Resources r = context.getResources();
preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_default_random_hours, 0);
preferences.setIfUnset(prefs, editor, r, R.string.p_rmd_persistent, true);
editor.commit();
preferencesInitialized = true;
}
// --- reminder scheduling logic
/**

@ -1,110 +0,0 @@
package com.todoroo.astrid.utility;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.data.Task;
import org.joda.time.DateTime;
import org.tasks.R;
import org.tasks.preferences.Preferences;
public class AstridDefaultPreferenceSpec {
private Context context;
private Preferences preferences;
public AstridDefaultPreferenceSpec(Context context, Preferences preferences) {
this.context = context;
this.preferences = preferences;
}
public void setIfUnset() {
SharedPreferences prefs = preferences.getPrefs();
Editor editor = prefs.edit();
Resources r = context.getResources();
setPreference(prefs, editor, r, R.string.p_default_urgency_key, 0);
setPreference(prefs, editor, r, R.string.p_default_importance_key, 2);
setPreference(prefs, editor, r, R.string.p_default_hideUntil_key, 0);
setPreference(prefs, editor, r, R.string.p_default_reminders_key, Task.NOTIFY_AT_DEADLINE | Task.NOTIFY_AFTER_DEADLINE);
setPreference(prefs, editor, r, R.string.p_rmd_default_random_hours, 0);
setPreference(prefs, editor, r, R.string.p_fontSize, 16);
setPreference(prefs, editor, r, R.string.p_field_missed_calls, true);
setPreference(prefs, editor, r, R.string.p_end_at_deadline, true);
setPreference(prefs, editor, r, R.string.p_rmd_persistent, true);
setPreference(prefs, editor, r, R.string.p_show_today_filter, true);
setPreference(prefs, editor, r, R.string.p_show_recently_modified_filter, true);
setPreference(prefs, editor, r, R.string.p_show_not_in_list_filter, true);
setPreference(prefs, editor, r, R.string.p_calendar_reminders, true);
setPreference(prefs, editor, r, R.string.p_use_dark_theme, false);
setPreference(prefs, editor, r, R.string.p_show_task_edit_comments, true);
setPreference(prefs, editor, r, R.string.p_rmd_quietStart_old, 22); // enable quiet hours by default
setIntIfUnset(prefs, editor, r, R.string.p_rmd_quietStart, r.getInteger(R.integer.default_quiet_hours_start));
setIntIfUnset(prefs, editor, r, R.string.p_rmd_quietEnd, r.getInteger(R.integer.default_quiet_hours_end));
setIntIfUnset(prefs, editor, r, R.string.p_rmd_time, r.getInteger(R.integer.default_remind_time));
BeastModePreferences.setDefaultOrder(preferences, context);
editor.commit();
migrateToNewQuietHours();
}
void migrateToNewQuietHours() {
if(!preferences.getBoolean(R.string.p_rmd_hasMigrated, false)) {
preferences.setBoolean(R.string.p_rmd_enable_quiet, preferences.getIntegerFromString(R.string.p_rmd_quietStart_old, -1) >= 0);
setTime(R.string.p_rmd_quietStart_old, R.string.p_rmd_quietStart, 22);
setTime(R.string.p_rmd_quietEnd_old, R.string.p_rmd_quietEnd, 10);
setTime(R.string.p_rmd_time_old, R.string.p_rmd_time, 18);
preferences.setBoolean(R.string.p_rmd_hasMigrated, true);
}
}
private void setTime(int oldResourceId, int newResourceId, int defValue) {
int hour = preferences.getIntegerFromString(oldResourceId, defValue);
int millisOfDay = new DateTime().withMillisOfDay(0).withHourOfDay(hour).getMillisOfDay();
preferences.setInt(newResourceId, millisOfDay);
}
private static void setPreference(SharedPreferences prefs, Editor editor, Resources r, int key, int value) {
setIfUnset(prefs, editor, r, key, value);
}
private static void setPreference(SharedPreferences prefs, Editor editor, Resources r, int key, boolean value) {
setIfUnset(prefs, editor, r, key, value);
}
private static void setIntIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, int value) {
String key = r.getString(keyResource);
if(!prefs.contains(key)) {
editor.putInt(key, value);
}
}
private static void setIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, int value) {
String key = r.getString(keyResource);
if(!prefs.contains(key)) {
editor.putString(key, Integer.toString(value));
}
}
private static void setIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, boolean value) {
String key = r.getString(keyResource);
if(!prefs.contains(key) || !(prefs.getAll().get(key) instanceof Boolean)) {
editor.putBoolean(key, value);
}
}
}

@ -6,9 +6,10 @@ import android.content.res.Resources;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.utility.AstridDefaultPreferenceSpec;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.widget.WidgetConfigActivity;
import org.slf4j.Logger;
@ -61,20 +62,6 @@ public class Preferences {
return getBoolean(WidgetConfigActivity.PREF_DARK_THEME + widgetId, legacySetting);
}
public void setIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, int value) {
String key = r.getString(keyResource);
if (!prefs.contains(key)) {
editor.putString(key, Integer.toString(value));
}
}
public void setIfUnset(SharedPreferences prefs, Editor editor, Resources r, int keyResource, boolean value) {
String key = r.getString(keyResource);
if (!prefs.contains(key) || !(prefs.getAll().get(key) instanceof Boolean)) {
editor.putBoolean(key, value);
}
}
public SharedPreferences getPrefs() {
return prefs;
}
@ -87,7 +74,12 @@ public class Preferences {
}
public void setDefaults() {
new AstridDefaultPreferenceSpec(context, this).setIfUnset();
PreferenceManager.setDefaultValues(context, R.xml.preferences_defaults, true);
PreferenceManager.setDefaultValues(context, R.xml.preferences_appearance, true);
PreferenceManager.setDefaultValues(context, R.xml.preferences_misc, true);
PreferenceManager.setDefaultValues(context, R.xml.preferences_reminders, true);
BeastModePreferences.setDefaultOrder(this, context);
}
public void reset() {
@ -103,6 +95,14 @@ public class Preferences {
return prefs.getString(context.getResources().getString(keyResource), null);
}
public int getDefaultReminders() {
return getIntegerFromString(R.string.p_default_reminders_key, Task.NOTIFY_AT_DEADLINE | Task.NOTIFY_AFTER_DEADLINE);
}
public int getDefaultRingMode() {
return getIntegerFromString(R.string.p_default_reminders_mode_key, 0);
}
public int getIntegerFromString(int keyResource, int defaultValue) {
Resources r = context.getResources();
String value = prefs.getString(r.getString(keyResource), null);

@ -61,13 +61,9 @@
<string-array name="EPr_default_reminders">
<!-- reminders: labels for "Task Defaults" preference item. -->
<!-- slide 43a -->
<item>No deadline reminders</item>
<!-- slide 43b -->
<item>At deadline</item>
<!-- slide 43c -->
<item>When overdue</item>
<!-- slide 43d -->
<item>At deadline or overdue</item>
</string-array>

@ -15,7 +15,8 @@
android:entryValues="@array/EPr_font_size"
android:key="@string/p_fontSize"
android:summary="@string/EPr_fontSize_desc"
android:title="@string/EPr_fontSize_title" />
android:title="@string/EPr_fontSize_title"
android:defaultValue="16"/>
<com.todoroo.astrid.ui.MultilineCheckboxPreference
android:defaultValue="false"

@ -1,45 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
** Copyright (c) 2012 Todoroo Inc
**
** See the file "LICENSE" for the full license governing this code.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/task_defaults">
<com.todoroo.astrid.ui.MultilineListPreference
android:key="@string/p_default_importance_key"
android:defaultValue="2"
android:entries="@array/EPr_default_importance"
android:entryValues="@array/EPr_default_importance_values"
android:key="@string/p_default_importance_key"
android:title="@string/EPr_default_importance_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:key="@string/p_default_urgency_key"
android:defaultValue="0"
android:entries="@array/EPr_default_urgency"
android:entryValues="@array/EPr_default_urgency_values"
android:key="@string/p_default_urgency_key"
android:title="@string/EPr_default_urgency_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:key="@string/p_default_hideUntil_key"
android:defaultValue="0"
android:entries="@array/EPr_default_hideUntil"
android:entryValues="@array/EPr_default_hideUntil_values"
android:key="@string/p_default_hideUntil_key"
android:title="@string/EPr_default_hideUntil_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:key="@string/p_default_reminders_key"
android:defaultValue="6"
android:entries="@array/EPr_default_reminders"
android:entryValues="@array/EPr_default_reminders_values"
android:key="@string/p_default_reminders_key"
android:title="@string/EPr_default_reminders_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:key="@string/p_rmd_default_random_hours"
android:title="@string/rmd_EPr_defaultRemind_title"
android:defaultValue="0"
android:entries="@array/EPr_reminder_random"
android:entryValues="@array/EPr_reminder_random_hours" />
<ListPreference
android:key="@string/p_default_reminders_mode_key"
android:title="@string/EPr_default_reminders_mode_title"
android:entryValues="@array/EPr_reminder_random_hours"
android:key="@string/p_rmd_default_random_hours"
android:title="@string/rmd_EPr_defaultRemind_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:defaultValue="0"
android:entries="@array/EPr_default_reminders_mode"
android:entryValues="@array/EPr_default_reminders_mode_values" />
android:entryValues="@array/EPr_default_reminders_mode_values"
android:key="@string/p_default_reminders_mode_key"
android:title="@string/EPr_default_reminders_mode_title" />
<com.todoroo.astrid.ui.MultilineListPreference
android:title="@string/EPr_default_addtocalendar_title"
android:key="@string/gcal_p_default"
android:defaultValue="0"
android:entries="@array/EPr_default_addtocalendar"
android:entryValues="@array/EPr_default_addtocalendar_values" />
android:entryValues="@array/EPr_default_addtocalendar_values"
android:key="@string/gcal_p_default"
android:title="@string/EPr_default_addtocalendar_title" />
</PreferenceScreen>

@ -16,7 +16,8 @@
android:entries="@array/sync_SPr_interval_entries"
android:entryValues="@array/sync_SPr_interval_values"
android:key="@string/gtasks_GPr_interval_key"
android:title="@string/sync_SPr_interval_title" />
android:title="@string/sync_SPr_interval_title"
android:defaultValue="0"/>
<com.todoroo.astrid.ui.MultilinePreference
android:summary="@string/sync_SPr_forget_description"

@ -17,19 +17,22 @@
android:key="@string/p_field_missed_calls"
android:summaryOff="@string/MCA_missed_calls_pref_desc_disabled"
android:summaryOn="@string/MCA_missed_calls_pref_desc_enabled"
android:title="@string/MCA_missed_calls_pref_title" />
android:title="@string/MCA_missed_calls_pref_title"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/p_calendar_reminders"
android:summaryOff="@string/CRA_calendar_reminders_pref_desc_disabled"
android:summaryOn="@string/CRA_calendar_reminders_pref_desc_enabled"
android:title="@string/CRA_calendar_reminders_pref_title" />
android:title="@string/CRA_calendar_reminders_pref_title"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/p_end_at_deadline"
android:summaryOff="@string/EPr_cal_end_at_due_time"
android:summaryOn="@string/EPr_cal_start_at_due_time"
android:title="@string/EPr_cal_end_or_start_at_due_time" />
android:title="@string/EPr_cal_end_or_start_at_due_time"
android:defaultValue="true"/>
<com.todoroo.astrid.ui.MultilineCheckboxPreference
android:defaultValue="true"

@ -27,7 +27,8 @@
android:key="@string/p_rmd_persistent"
android:summaryOff="@string/rmd_EPr_persistent_desc_false"
android:summaryOn="@string/rmd_EPr_persistent_desc_true"
android:title="@string/rmd_EPr_persistent_title" />
android:title="@string/rmd_EPr_persistent_title"
android:defaultValue="true"/>
<org.tasks.ui.TimePreference
android:defaultValue="@integer/default_remind_time"
android:dependency="@string/p_rmd_enabled"

Loading…
Cancel
Save