From c29efa18fd8486db6d721e5664b57c61b2fdc584 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 18 Mar 2015 13:05:15 -0500 Subject: [PATCH] Set default prefs from XML --- .../AstridDefaultPreferenceSpecTest.java | 126 ------------------ .../java/com/todoroo/astrid/dao/TaskDao.java | 5 +- .../astrid/reminders/ReminderService.java | 25 ---- .../utility/AstridDefaultPreferenceSpec.java | 110 --------------- .../org/tasks/preferences/Preferences.java | 32 ++--- src/main/res/values/strings-defaults.xml | 4 - src/main/res/xml/preferences_appearance.xml | 3 +- src/main/res/xml/preferences_defaults.xml | 41 +++--- src/main/res/xml/preferences_gtasks.xml | 3 +- src/main/res/xml/preferences_misc.xml | 9 +- src/main/res/xml/preferences_reminders.xml | 3 +- 11 files changed, 52 insertions(+), 309 deletions(-) delete mode 100644 src/androidTest/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpecTest.java delete mode 100644 src/main/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpec.java diff --git a/src/androidTest/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpecTest.java b/src/androidTest/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpecTest.java deleted file mode 100644 index 85c200012..000000000 --- a/src/androidTest/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpecTest.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/todoroo/astrid/dao/TaskDao.java b/src/main/java/com/todoroo/astrid/dao/TaskDao.java index d08421f7c..f4da51569 100644 --- a/src/main/java/com/todoroo/astrid/dao/TaskDao.java +++ b/src/main/java/com/todoroo/astrid/dao/TaskDao.java @@ -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()); } } diff --git a/src/main/java/com/todoroo/astrid/reminders/ReminderService.java b/src/main/java/com/todoroo/astrid/reminders/ReminderService.java index bae212e23..26594be1b 100644 --- a/src/main/java/com/todoroo/astrid/reminders/ReminderService.java +++ b/src/main/java/com/todoroo/astrid/reminders/ReminderService.java @@ -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 /** diff --git a/src/main/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpec.java b/src/main/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpec.java deleted file mode 100644 index a27c1992b..000000000 --- a/src/main/java/com/todoroo/astrid/utility/AstridDefaultPreferenceSpec.java +++ /dev/null @@ -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); - } - } -} diff --git a/src/main/java/org/tasks/preferences/Preferences.java b/src/main/java/org/tasks/preferences/Preferences.java index f3da80220..20ee613fc 100644 --- a/src/main/java/org/tasks/preferences/Preferences.java +++ b/src/main/java/org/tasks/preferences/Preferences.java @@ -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); diff --git a/src/main/res/values/strings-defaults.xml b/src/main/res/values/strings-defaults.xml index e0e9dd7ec..d44643483 100644 --- a/src/main/res/values/strings-defaults.xml +++ b/src/main/res/values/strings-defaults.xml @@ -61,13 +61,9 @@ - No deadline reminders - At deadline - When overdue - At deadline or overdue diff --git a/src/main/res/xml/preferences_appearance.xml b/src/main/res/xml/preferences_appearance.xml index cf2353942..5928d94ef 100644 --- a/src/main/res/xml/preferences_appearance.xml +++ b/src/main/res/xml/preferences_appearance.xml @@ -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"/> - - - + + android:entryValues="@array/EPr_default_reminders_mode_values" + android:key="@string/p_default_reminders_mode_key" + android:title="@string/EPr_default_reminders_mode_title" /> + android:entryValues="@array/EPr_default_addtocalendar_values" + android:key="@string/gcal_p_default" + android:title="@string/EPr_default_addtocalendar_title" /> diff --git a/src/main/res/xml/preferences_gtasks.xml b/src/main/res/xml/preferences_gtasks.xml index f412d3be3..959f82fb8 100644 --- a/src/main/res/xml/preferences_gtasks.xml +++ b/src/main/res/xml/preferences_gtasks.xml @@ -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"/> + android:title="@string/MCA_missed_calls_pref_title" + android:defaultValue="true"/> + android:title="@string/CRA_calendar_reminders_pref_title" + android:defaultValue="true"/> + android:title="@string/EPr_cal_end_or_start_at_due_time" + android:defaultValue="true"/> + android:title="@string/rmd_EPr_persistent_title" + android:defaultValue="true"/>