mirror of https://github.com/tasks/tasks
Set default prefs from XML
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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
Loading…
Reference in New Issue