Migrate more preferences

pull/189/head
Alex Baker 11 years ago
parent abd2b8d34b
commit 4700467574

@ -22,31 +22,6 @@ import com.todoroo.andlib.service.ContextManager;
@Deprecated
public class Preferences {
/**
* Helper to write to editor if key specified is null. Writes a String
* property with the given integer
*/
public 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));
}
}
/**
* Helper to write to editor if key specified is null
*/
public 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);
}
}
/* ======================================================================
* ======================================================= helper methods
* ====================================================================== */
private static SharedPreferences preferences = null;
/** Get preferences object from the context */
@ -70,12 +45,6 @@ public class Preferences {
return preferences;
}
/** @return true if given preference is set */
public static boolean isSet(String key) {
Context context = ContextManager.getContext();
return getPrefs(context).contains(key);
}
// --- preference fetching (string)
/** Gets an string value from a string preference. Returns null
@ -117,13 +86,6 @@ public class Preferences {
}
}
/**
* Sets string preference
*/
public static void setString(int resourceId, String value) {
setString(ContextManager.getContext().getString(resourceId), value);
}
public static void setString(String key, String newValue) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();
@ -131,89 +93,14 @@ public class Preferences {
editor.commit();
}
/**
* Sets string preference from integer value
*/
public static void setStringFromInteger(int keyResource, int newValue) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();
editor.putString(context.getString(keyResource), Integer.toString(newValue));
editor.commit();
}
// --- preference fetching (boolean)
/** Gets a boolean preference (e.g. a CheckBoxPreference setting)
* @return default if value is unset otherwise the value
*/
public static boolean getBoolean(String key, boolean defValue) {
public static boolean getBoolean(int keyResources, boolean defValue) {
Context context = ContextManager.getContext();
try {
return getPrefs(context).getBoolean(key, defValue);
return getPrefs(context).getBoolean(context.getString(keyResources), defValue);
} catch (ClassCastException e) {
return defValue;
}
}
/** Gets a boolean preference (e.g. a CheckBoxPreference setting)
* @return default if value is unset otherwise the value
*/
public static boolean getBoolean(int keyResources) {
return getBoolean(keyResources, false);
}
public static boolean getBoolean(int keyResources, boolean defValue) {
Context context = ContextManager.getContext();
return getBoolean(context.getString(keyResources), defValue);
}
/**
* Sets boolean preference
*/
public static void setBoolean(int keyResource, boolean value) {
Context context = ContextManager.getContext();
setBoolean(context.getString(keyResource), value);
}
/**
* Sets boolean preference
*/
public static void setBoolean(String key, boolean value) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();
editor.putBoolean(key, value);
editor.commit();
}
// --- preference fetching (int)
/** Gets a int preference
* @return default if value is unset otherwise the value
*/
public static int getInt(int resourceId) {
return getInt(resourceId, 0);
}
public static int getInt(int resourceId, int defValue) {
return getInt(ContextManager.getContext().getString(resourceId), defValue);
}
public static int getInt(String key, int defValue) {
Context context = ContextManager.getContext();
return getPrefs(context).getInt(key, defValue);
}
/**
* Sets int preference
*/
public static void setInt(int resourceId, int value) {
setInt(ContextManager.getContext().getString(resourceId), value);
}
public static void setInt(String key, int value) {
Context context = ContextManager.getContext();
Editor editor = getPrefs(context).edit();
editor.putInt(key, value);
editor.commit();
}
}

@ -8,6 +8,7 @@ package com.todoroo.astrid.gtasks;
import android.content.Context;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Metadata;
@ -180,7 +181,7 @@ public class GtasksMetadataServiceTest extends DatabaseTestCase {
@Override
public void setContext(Context context) {
super.setContext(context);
if (!Preferences.isSet(GtasksPreferenceService.PREF_DEFAULT_LIST))
if (!isSet(GtasksPreferenceService.PREF_DEFAULT_LIST))
Preferences.setString(GtasksPreferenceService.PREF_DEFAULT_LIST, "list");
}
@ -188,4 +189,10 @@ public class GtasksMetadataServiceTest extends DatabaseTestCase {
protected Object getModule() {
return new GtasksMetadataServiceTestModule();
}
/** @return true if given preference is set */
public static boolean isSet(String key) {
Context context = ContextManager.getContext();
return Preferences.getPrefs(context).contains(key);
}
}

@ -3,12 +3,12 @@ package com.todoroo.astrid.reminders;
import android.annotation.SuppressLint;
import android.test.AndroidTestCase;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Task;
import org.joda.time.DateTime;
import org.tasks.Freeze;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.concurrent.TimeUnit;
@ -28,10 +28,12 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
}};
private ReminderService reminderService;
private Preferences preferences;
@Override
public void setUp() {
reminderService = new ReminderService(getContext());
preferences = new Preferences(getContext());
reminderService = new ReminderService(getContext(), preferences);
freezeAt(new DateTime(2014, 1, 24, 17, 23, 37));
}
@ -87,7 +89,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testDuringQuietHoursSetNotificationAtEnd() {
setQuietHours(0, 10);
Freeze.freezeAt(new DateTime(2014, 1, 27, 9, 13, 37, 501));
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
Task task = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
@ -100,7 +102,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testAfterQuietHoursSetNotificationOnePeriodCloserToDueDate() {
setQuietHours(0, 10);
Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 13, 37, 501));
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
Task task = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
@ -113,7 +115,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testBeforeQuietStartDueDateMoreThanOnePeriodAfterEnd() {
setQuietHours(2, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 1, 53, 37, 509));
Preferences.setInt(R.string.p_rmd_time, MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, MILLIS_PER_HOUR);
Task task = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
@ -126,7 +128,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testBeforeQuietStartDueDateLessThanOnePeriodAfterEnd() {
setQuietHours(3, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 1, 53, 37, 509));
Preferences.setInt(R.string.p_rmd_time, MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, MILLIS_PER_HOUR);
assertEquals(
new DateTime(2014, 1, 27, 2, 10, 13, 131).getMillis(),
reminderService.calculateNextDueDateReminder(dueAtNoon));
@ -135,7 +137,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testNoAlarmAfterQuietHoursStartWithWrap() {
setQuietHours(10, 1);
Freeze.freezeAt(new DateTime(2014, 1, 27, 10, 0, 0, 0));
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
NO_ALARM,
reminderService.calculateNextDueDateReminder(dueAtNoon));
@ -144,7 +146,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testSetToQuietAlarmEndWithWrap() {
setQuietHours(22, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 10, 59, 59, 999));
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
new DateTime(2014, 1, 27, 11, 0, 0, 0).getMillis(),
reminderService.calculateNextDueDateReminder(dueAtNoon));
@ -153,7 +155,7 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testSetReminderOnePeriodFromNowBeforeQuietHourStartWithWrap() {
setQuietHours(22, 11);
Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 0, 0, 0));
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
// wtf? this is after due date
new DateTime(2014, 1, 27, 13, 45, 0, 0).getMillis(),
@ -162,16 +164,16 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
public void testSetReminderOnePeriodFromNowNoQuietHours() {
Freeze.freezeAt(new DateTime(2014, 1, 27, 11, 0, 0, 0));
Preferences.setBoolean(R.string.p_rmd_enable_quiet, false);
Preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
preferences.setBoolean(R.string.p_rmd_enable_quiet, false);
preferences.setInt(R.string.p_rmd_time, 8 * MILLIS_PER_HOUR);
assertEquals(
new DateTime(2014, 1, 27, 11, 15, 0, 0).getMillis(),
reminderService.calculateNextDueDateReminder(dueAtNoon));
}
private void setQuietHours(int start, int end) {
Preferences.setBoolean(R.string.p_rmd_enable_quiet, true);
Preferences.setInt(R.string.p_rmd_quietStart, start * MILLIS_PER_HOUR);
Preferences.setInt(R.string.p_rmd_quietEnd, end * MILLIS_PER_HOUR);
preferences.setBoolean(R.string.p_rmd_enable_quiet, true);
preferences.setInt(R.string.p_rmd_quietStart, start * MILLIS_PER_HOUR);
preferences.setInt(R.string.p_rmd_quietEnd, end * MILLIS_PER_HOUR);
}
}

@ -15,7 +15,6 @@ import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
@ -24,6 +23,7 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.Date;
@ -34,14 +34,15 @@ import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
public class NewRepeatTests<REMOTE_MODEL> extends DatabaseTestCase {
public class NewRepeatTests extends DatabaseTestCase {
@Inject TaskDao taskDao;
@Inject Preferences preferences;
@Override
protected void setUp() {
super.setUp();
Preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
}
private void saveAndTriggerRepeatListener(Task task) {

@ -5,16 +5,15 @@
*/
package com.todoroo.astrid.service;
import com.google.ical.values.Frequency;
import com.google.ical.values.RRule;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.tags.TagService;
import com.todoroo.astrid.test.DatabaseTestCase;
import com.todoroo.astrid.utility.TitleParser;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.Calendar;
@ -27,15 +26,14 @@ import static org.tasks.date.DateTimeUtils.newDate;
public class TitleParserTest extends DatabaseTestCase {
@Inject TaskService taskService;
@Inject TagService tagService;
@Inject MetadataService metadataService;
@Inject Preferences preferences;
@Override
protected void setUp() {
super.setUp();
Preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
}
/** test that completing a task w/ no regular expressions creates a simple task with no date, no repeat, no lists*/

@ -3,28 +3,27 @@ package com.todoroo.astrid.utility;
import android.annotation.SuppressLint;
import android.test.AndroidTestCase;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.andlib.service.ContextManager;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.concurrent.TimeUnit;
import static com.todoroo.andlib.utility.Preferences.getBoolean;
import static com.todoroo.andlib.utility.Preferences.getInt;
import static com.todoroo.andlib.utility.Preferences.setStringFromInteger;
import static com.todoroo.astrid.utility.AstridDefaultPreferenceSpec.migrateToNewQuietHours;
import static org.tasks.TestUtilities.clearPreferences;
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() throws Exception {
super.setUp();
public void setUp() {
preferences = new Preferences(getContext());
spec = new AstridDefaultPreferenceSpec(getContext(), preferences);
clearPreferences(getContext());
preferences.clear();
assertFalse(hasMigrated());
}
@ -32,15 +31,15 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
public void testQuietHoursEnabledAfterMigration() {
setOldQuietHoursStart(1);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertTrue(quietHoursEnabled());
}
public void testQuietHoursDisabledAfterMigration() {
Preferences.setString(R.string.p_rmd_quietStart_old, "");
setString(R.string.p_rmd_quietStart_old, "");
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertFalse(quietHoursEnabled());
}
@ -48,7 +47,7 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
public void testMigrateFromStartOfDefaultReminderTimeArray() {
setOldReminderTime(0);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertEquals(0, newReminderTime());
}
@ -56,7 +55,7 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
public void testMigrateFromEndOfDefaultReminderTimeArray() {
setOldReminderTime(23);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertEquals(23 * MILLIS_PER_HOUR, newReminderTime());
}
@ -64,7 +63,7 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
public void testMigrateFromStartOfQuietHourStartArray() {
setOldQuietHoursStart(1);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertEquals(MILLIS_PER_HOUR, newQuietHoursStartTime());
}
@ -72,7 +71,7 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
public void testMigrateFromEndOfQuietHoursStartArray() {
setOldQuietHoursStart(23);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursStartTime());
}
@ -81,7 +80,7 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
setOldQuietHoursStart(1);
setOldQuietHoursEnd(0);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertEquals(0, newQuietHoursEndTime());
}
@ -90,40 +89,47 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
setOldQuietHoursStart(1);
setOldQuietHoursEnd(23);
migrateToNewQuietHours();
spec.migrateToNewQuietHours();
assertEquals(23 * MILLIS_PER_HOUR, newQuietHoursEndTime());
}
private boolean quietHoursEnabled() {
return getBoolean(R.string.p_rmd_enable_quiet);
return preferences.getBoolean(R.string.p_rmd_enable_quiet);
}
private boolean hasMigrated() {
return getBoolean(R.string.p_rmd_hasMigrated);
return preferences.getBoolean(R.string.p_rmd_hasMigrated);
}
private void setOldQuietHoursStart(int index) {
setStringFromInteger(R.string.p_rmd_quietStart_old, index);
preferences.setStringFromInteger(R.string.p_rmd_quietStart_old, index);
}
private void setOldQuietHoursEnd(int index) {
setStringFromInteger(R.string.p_rmd_quietEnd_old, index);
preferences.setStringFromInteger(R.string.p_rmd_quietEnd_old, index);
}
private void setOldReminderTime(int index) {
setStringFromInteger(R.string.p_rmd_time_old, index);
preferences.setStringFromInteger(R.string.p_rmd_time_old, index);
}
private int newQuietHoursStartTime() {
return getInt(R.string.p_rmd_quietStart);
return preferences.getInt(R.string.p_rmd_quietStart);
}
private int newQuietHoursEndTime() {
return getInt(R.string.p_rmd_quietEnd);
return preferences.getInt(R.string.p_rmd_quietEnd);
}
private int newReminderTime() {
return getInt(R.string.p_rmd_time);
return preferences.getInt(R.string.p_rmd_time);
}
/**
* Sets string preference
*/
private void setString(int resourceId, String value) {
preferences.setString(ContextManager.getContext().getString(resourceId), value);
}
}

@ -2,8 +2,6 @@ package org.tasks;
import android.content.Context;
import org.tasks.preferences.Preferences;
public class TestUtilities {
private static boolean mockitoInitialized;
@ -14,8 +12,4 @@ public class TestUtilities {
mockitoInitialized = true;
}
}
public static void clearPreferences(Context context) {
new Preferences(context).clear();
}
}

@ -25,7 +25,6 @@ import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.data.TaskAttachment;
import com.todoroo.astrid.files.FileExplore;
@ -44,6 +43,7 @@ import com.todoroo.astrid.voice.VoiceRecognizer;
import org.tasks.R;
import org.tasks.injection.InjectingTodorooPreferenceActivity;
import org.tasks.preferences.Preferences;
import org.tasks.widget.WidgetHelper;
import java.util.ArrayList;
@ -74,6 +74,7 @@ public class EditPreferences extends InjectingTodorooPreferenceActivity {
@Inject StartupService startupService;
@Inject TaskService taskService;
@Inject Preferences preferences;
private VoiceInputAssistant voiceInputAssistant;
@ -141,7 +142,7 @@ public class EditPreferences extends InjectingTodorooPreferenceActivity {
}
preference = screen.findPreference(getString(R.string.p_showNotes));
preference.setEnabled(Preferences.getIntegerFromString(R.string.p_taskRowStyle_v2, 0) == 0);
preference.setEnabled(preferences.getIntegerFromString(R.string.p_taskRowStyle_v2, 0) == 0);
removeForbiddenPreferences(screen, r);
}
@ -308,7 +309,7 @@ public class EditPreferences extends InjectingTodorooPreferenceActivity {
} else {
preference.setSummary(R.string.EPr_showNotes_desc_enabled);
}
if ((Boolean) value != Preferences.getBoolean(preference.getKey(), false)) {
if ((Boolean) value != preferences.getBoolean(preference.getKey(), false)) {
taskService.clearDetails(Criterion.all);
Flags.set(Flags.REFRESH);
}
@ -322,7 +323,7 @@ public class EditPreferences extends InjectingTodorooPreferenceActivity {
// pp preferences
else if (r.getString(R.string.p_files_dir).equals(preference.getKey())) {
String dir = Preferences.getStringValue(TaskAttachment.FILES_DIRECTORY_PREF);
String dir = preferences.getStringValue(TaskAttachment.FILES_DIRECTORY_PREF);
if (TextUtils.isEmpty(dir)) {
dir = r.getString(R.string.p_files_dir_desc_default);
@ -375,7 +376,7 @@ public class EditPreferences extends InjectingTodorooPreferenceActivity {
} else if (requestCode == REQUEST_CODE_FILES_DIR && resultCode == RESULT_OK) {
if (data != null) {
String dir = data.getStringExtra(FileExplore.RESULT_DIR_SELECTED);
Preferences.setString(TaskAttachment.FILES_DIRECTORY_PREF, dir);
preferences.setString(TaskAttachment.FILES_DIRECTORY_PREF, dir);
}
return;
}
@ -443,7 +444,7 @@ public class EditPreferences extends InjectingTodorooPreferenceActivity {
} catch (VerifyError e) {
// doesn't work :(
preference.setEnabled(false);
Preferences.setBoolean(preference.getKey(), false);
preferences.setBoolean(preference.getKey(), false);
}
}

@ -157,7 +157,7 @@ public class TasksXmlExporter {
xml.startTag(null, BackupConstants.ASTRID_TAG);
xml.attribute(null, BackupConstants.ASTRID_ATTR_VERSION,
Integer.toString(AstridPreferences.getCurrentVersion()));
Integer.toString(preferences.getCurrentVersion()));
xml.attribute(null, BackupConstants.ASTRID_ATTR_FORMAT,
Integer.toString(FORMAT));

@ -23,7 +23,6 @@ import android.widget.TextView;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.activity.EditPreferences;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.NotificationFragment.SnoozeDialog;
@ -34,6 +33,7 @@ import com.todoroo.astrid.service.ThemeService;
import org.tasks.R;
import org.tasks.injection.InjectingActivity;
import org.tasks.preferences.Preferences;
import java.io.InputStream;
@ -53,6 +53,7 @@ public class MissedCallActivity extends InjectingActivity {
@Inject StartupService startupService;
@Inject TaskService taskService;
@Inject Preferences preferences;
private final OnClickListener dismissListener = new OnClickListener() {
@Override
@ -66,7 +67,7 @@ public class MissedCallActivity extends InjectingActivity {
@Override
public void onClick(final View v) {
// Check for number of ignore presses
int ignorePresses = Preferences.getInt(PREF_IGNORE_PRESSES, 0);
int ignorePresses = preferences.getInt(PREF_IGNORE_PRESSES, 0);
ignorePresses++;
if (ignorePresses == IGNORE_PROMPT_COUNT) {
DialogUtilities.okCancelCustomDialog(MissedCallActivity.this,
@ -78,7 +79,7 @@ public class MissedCallActivity extends InjectingActivity {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Preferences.setBoolean(R.string.p_field_missed_calls, false);
preferences.setBoolean(R.string.p_field_missed_calls, false);
dismissListener.onClick(v);
}
},
@ -91,7 +92,7 @@ public class MissedCallActivity extends InjectingActivity {
} else {
dismissListener.onClick(v);
}
Preferences.setInt(PREF_IGNORE_PRESSES, ignorePresses);
preferences.setInt(PREF_IGNORE_PRESSES, ignorePresses);
}
};

@ -15,11 +15,8 @@ import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Join;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.tags.TaskToTagMetadata;
import com.todoroo.astrid.utility.AstridPreferences;
import java.util.ArrayList;
import java.util.HashSet;
@ -125,14 +122,7 @@ public class MetadataDao extends DatabaseDao<Metadata> {
item.setCreationDate(DateUtilities.now());
}
boolean state = super.persist(item);
if(Preferences.getBoolean(AstridPreferences.P_FIRST_LIST, true)) {
if (state && item.containsNonNullValue(Metadata.KEY) &&
item.getKey().equals(TaskToTagMetadata.KEY)) {
Preferences.setBoolean(AstridPreferences.P_FIRST_LIST, false);
}
}
return state;
return super.persist(item);
}
/**

@ -14,7 +14,6 @@ import android.widget.TextView;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.activity.EditPreferences;
import com.todoroo.astrid.activity.TaskListActivity;
import com.todoroo.astrid.activity.TaskListFragment;
@ -27,6 +26,7 @@ import com.todoroo.astrid.tags.TagFilterExposer;
import org.tasks.R;
import org.tasks.injection.InjectingActivity;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
@ -50,6 +50,7 @@ public class CalendarReminderActivity extends InjectingActivity {
@Inject StartupService startupService;
@Inject TagDataService tagDataService;
@Inject Preferences preferences;
private String eventName;
private long startTime;
@ -76,7 +77,7 @@ public class CalendarReminderActivity extends InjectingActivity {
@Override
public void onClick(final View v) {
// Check for number of ignore presses
int ignorePresses = Preferences.getInt(PREF_IGNORE_PRESSES, 0);
int ignorePresses = preferences.getInt(PREF_IGNORE_PRESSES, 0);
ignorePresses++;
if (ignorePresses == IGNORE_PROMPT_COUNT) {
DialogUtilities.okCancelCustomDialog(CalendarReminderActivity.this,
@ -88,7 +89,7 @@ public class CalendarReminderActivity extends InjectingActivity {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Preferences.setBoolean(R.string.p_calendar_reminders, false);
preferences.setBoolean(R.string.p_calendar_reminders, false);
dismissListener.onClick(v);
}
},
@ -101,7 +102,7 @@ public class CalendarReminderActivity extends InjectingActivity {
} else {
dismissListener.onClick(v);
}
Preferences.setInt(PREF_IGNORE_PRESSES, ignorePresses);
preferences.setInt(PREF_IGNORE_PRESSES, ignorePresses);
}
};

@ -7,9 +7,9 @@ import android.content.Intent;
import android.util.Log;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -22,11 +22,13 @@ public class GtasksScheduler {
private final GtasksPreferenceService gtasksPreferenceService;
private Context context;
private Preferences preferences;
@Inject
public GtasksScheduler(GtasksPreferenceService gtasksPreferenceService, @ForApplication Context context) {
public GtasksScheduler(GtasksPreferenceService gtasksPreferenceService, @ForApplication Context context, Preferences preferences) {
this.gtasksPreferenceService = gtasksPreferenceService;
this.context = context;
this.preferences = preferences;
}
/**
@ -35,10 +37,10 @@ public class GtasksScheduler {
public void scheduleService() {
int syncFrequencySeconds = 0;
try {
syncFrequencySeconds = Preferences.getIntegerFromString(
syncFrequencySeconds = preferences.getIntegerFromString(
gtasksPreferenceService.getSyncIntervalKey(), -1);
} catch(ClassCastException e) {
Preferences.setStringFromInteger(gtasksPreferenceService.getSyncIntervalKey(), 0);
preferences.setStringFromInteger(gtasksPreferenceService.getSyncIntervalKey(), 0);
}
if(syncFrequencySeconds <= 0) {
unscheduleService(context);

@ -18,7 +18,6 @@ import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Task;
@ -26,6 +25,7 @@ import com.todoroo.astrid.data.Task;
import org.joda.time.DateTime;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.Preferences;
import java.util.Date;
import java.util.Random;
@ -79,10 +79,12 @@ public final class ReminderService {
private long now = -1; // For tracking when reminders might be scheduled all at once
private Context context;
private Preferences preferences;
@Inject
ReminderService(@ForApplication Context context) {
ReminderService(@ForApplication Context context, Preferences preferences) {
this.context = context;
this.preferences = preferences;
setPreferenceDefaults();
}
@ -98,12 +100,12 @@ public final class ReminderService {
return;
}
SharedPreferences prefs = Preferences.getPrefs(context);
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);
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;
@ -293,20 +295,20 @@ public final class ReminderService {
dueDateAlarm = dueDate;
} else if (DateUtilities.now() > lastReminder + DateUtilities.ONE_DAY) {
// return notification time on this day
Date date = new DateTime(dueDate).withMillisOfDay(Preferences.getInt(R.string.p_rmd_time, 18 * MILLIS_PER_HOUR)).toDate();
Date date = new DateTime(dueDate).withMillisOfDay(preferences.getInt(R.string.p_rmd_time, 18 * MILLIS_PER_HOUR)).toDate();
dueDateAlarm = date.getTime();
if (dueDate > getNowValue() && dueDateAlarm < getNowValue()) {
// this only happens for tasks due today, cause dueDateAlarm wouldn't be in the past otherwise
// if the default reminder is in the past, then reschedule it
// on this day before start of quiet hours or after quiet hours
// randomly placed in this interval
long quietHoursStart = new DateTime().withMillisOfDay(Preferences.getInt(R.string.p_rmd_quietStart, 22 * MILLIS_PER_HOUR)).getMillis();
long quietHoursStart = new DateTime().withMillisOfDay(preferences.getInt(R.string.p_rmd_quietStart, 22 * MILLIS_PER_HOUR)).getMillis();
Date quietHoursStartDate = newDate(quietHoursStart);
long quietHoursEnd = new DateTime().withMillisOfDay(Preferences.getInt(R.string.p_rmd_quietEnd, 10 * MILLIS_PER_HOUR)).getMillis();
long quietHoursEnd = new DateTime().withMillisOfDay(preferences.getInt(R.string.p_rmd_quietEnd, 10 * MILLIS_PER_HOUR)).getMillis();
Date quietHoursEndDate = newDate(quietHoursEnd);
boolean quietHoursEnabled = Preferences.getBoolean(R.string.p_rmd_enable_quiet, false);
boolean quietHoursEnabled = preferences.getBoolean(R.string.p_rmd_enable_quiet, false);
long millisToQuiet;
long millisToEndOfDay = dueDate - getNowValue();

@ -43,7 +43,6 @@ import com.todoroo.astrid.provider.Astrid2TaskProvider;
import com.todoroo.astrid.provider.Astrid3ContentProvider;
import com.todoroo.astrid.reminders.ReminderStartupReceiver;
import com.todoroo.astrid.tags.TaskToTagMetadata;
import com.todoroo.astrid.utility.AstridPreferences;
import com.todoroo.astrid.utility.Constants;
import org.slf4j.Logger;
@ -141,7 +140,7 @@ public class StartupService {
// read current version
int latestSetVersion = 0;
try {
latestSetVersion = AstridPreferences.getCurrentVersion();
latestSetVersion = preferences.getCurrentVersion();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
@ -168,8 +167,8 @@ public class StartupService {
if(latestSetVersion > 0) {
upgradeService.performUpgrade(context, latestSetVersion);
}
AstridPreferences.setCurrentVersion(version);
AstridPreferences.setCurrentVersionName(versionName);
preferences.setCurrentVersion(version);
preferences.setCurrentVersionName(versionName);
}
final int finalLatestVersion = latestSetVersion;
@ -256,7 +255,7 @@ public class StartupService {
*/
private void databaseRestoreIfEmpty(Context context) {
try {
if(AstridPreferences.getCurrentVersion() >= UpgradeService.V3_0_0 &&
if(preferences.getCurrentVersion() >= UpgradeService.V3_0_0 &&
!context.getDatabasePath(database.getName()).exists()) {
// we didn't have a database! restore latest file
File directory = BackupConstants.defaultExportDirectory();

@ -7,6 +7,7 @@ package com.todoroo.astrid.ui;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.text.Html;
import android.text.Spanned;
@ -24,6 +25,7 @@ import android.widget.TextView;
import com.google.ical.values.Frequency;
import com.google.ical.values.RRule;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
@ -236,7 +238,7 @@ public class DateChangedAlerts {
}
private static void setupHideCheckbox(final Dialog d) {
int numShows = Preferences.getInt(PREF_NUM_HELPERS_SHOWN, 0);
int numShows = getInt(PREF_NUM_HELPERS_SHOWN, 0);
numShows++;
if (numShows >= HIDE_CHECKBOX_AFTER_SHOWS) {
CheckBox checkbox = (CheckBox) d.findViewById(R.id.reminders_should_show);
@ -244,11 +246,30 @@ public class DateChangedAlerts {
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Preferences.setBoolean(PREF_SHOW_HELPERS, !isChecked);
setBoolean(PREF_SHOW_HELPERS, !isChecked);
}
});
}
Preferences.setInt(PREF_NUM_HELPERS_SHOWN, numShows);
setInt(PREF_NUM_HELPERS_SHOWN, numShows);
}
public static void setBoolean(int keyResource, boolean value) {
Context context = ContextManager.getContext();
SharedPreferences.Editor editor = Preferences.getPrefs(context).edit();
editor.putBoolean(context.getString(keyResource), value);
editor.commit();
}
public static int getInt(String key, int defValue) {
Context context = ContextManager.getContext();
return Preferences.getPrefs(context).getInt(key, defValue);
}
private static void setInt(String key, int value) {
Context context = ContextManager.getContext();
SharedPreferences.Editor editor = Preferences.getPrefs(context).edit();
editor.putInt(key, value);
editor.commit();
}
private static void setupDialogLayoutParams(Context context, Dialog d) {

@ -5,26 +5,26 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.data.Task;
import org.joda.time.DateTime;
import org.tasks.R;
import static com.todoroo.andlib.utility.Preferences.setBoolean;
import org.tasks.preferences.Preferences;
public class AstridDefaultPreferenceSpec {
private Context context;
private Preferences preferences;
public AstridDefaultPreferenceSpec(Context context) {
public AstridDefaultPreferenceSpec(Context context, Preferences preferences) {
this.context = context;
this.preferences = preferences;
}
public void setIfUnset() {
SharedPreferences prefs = Preferences.getPrefs(context);
SharedPreferences prefs = preferences.getPrefs();
Editor editor = prefs.edit();
Resources r = context.getResources();
@ -71,7 +71,7 @@ public class AstridDefaultPreferenceSpec {
setIntIfUnset(prefs, editor, r, R.string.p_rmd_time, r.getInteger(R.integer.default_remind_time));
String dragDropTestInitialized = "android_drag_drop_initialized"; //$NON-NLS-1$
if (!Preferences.getBoolean(dragDropTestInitialized, false)) {
if (!preferences.getBoolean(dragDropTestInitialized, false)) {
SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(context);
if (publicPrefs != null) {
Editor edit = publicPrefs.edit();
@ -80,7 +80,7 @@ public class AstridDefaultPreferenceSpec {
edit.commit();
}
}
setBoolean(dragDropTestInitialized, true);
preferences.setBoolean(dragDropTestInitialized, true);
}
BeastModePreferences.setDefaultOrder(context);
@ -89,28 +89,28 @@ public class AstridDefaultPreferenceSpec {
migrateToNewQuietHours();
}
static void migrateToNewQuietHours() {
if(!Preferences.getBoolean(R.string.p_rmd_hasMigrated, false)) {
setBoolean(R.string.p_rmd_enable_quiet, Preferences.getIntegerFromString(R.string.p_rmd_quietStart_old, -1) >= 0);
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);
setBoolean(R.string.p_rmd_hasMigrated, true);
preferences.setBoolean(R.string.p_rmd_hasMigrated, true);
}
}
private static void setTime(int oldResourceId, int newResourceId, int defValue) {
int hour = Preferences.getIntegerFromString(oldResourceId, defValue);
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);
preferences.setInt(newResourceId, millisOfDay);
}
private static void setPreference(SharedPreferences prefs, Editor editor, Resources r, int key, int value) {
Preferences.setIfUnset(prefs, editor, r, key, value);
setIfUnset(prefs, editor, r, key, value);
}
private static void setPreference(SharedPreferences prefs, Editor editor, Resources r, int key, boolean value) {
Preferences.setIfUnset(prefs, editor, r, key, value);
setIfUnset(prefs, editor, r, key, value);
}
private static void setPreference(SharedPreferences prefs, Editor editor, Resources r, int key, String value) {
@ -130,4 +130,18 @@ public class AstridDefaultPreferenceSpec {
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);
}
}
}

@ -17,12 +17,6 @@ import org.tasks.R;
public class AstridPreferences {
private static final String P_CURRENT_VERSION = "cv"; //$NON-NLS-1$
private static final String P_CURRENT_VERSION_NAME = "cvname"; //$NON-NLS-1$
public static final String P_FIRST_LIST = "fl"; //$NON-NLS-1$
/* ======================================================================
* ========================================================= public prefs
* ====================================================================== */
@ -38,20 +32,6 @@ public class AstridPreferences {
* ========================================================= system prefs
* ====================================================================== */
/** CurrentVersion: the currently installed version of Astrid */
public static int getCurrentVersion() {
return Preferences.getInt(P_CURRENT_VERSION, 0);
}
/** CurrentVersion: the currently installed version of Astrid */
public static void setCurrentVersion(int version) {
Preferences.setInt(P_CURRENT_VERSION, version);
}
public static void setCurrentVersionName(String versionName) {
Preferences.setString(P_CURRENT_VERSION_NAME, versionName);
}
public static boolean useTabletLayout(Context context) {
return AndroidUtilities.isTabletSized(context) && !Preferences.getBoolean(R.string.p_force_phone_layout, false);
}

@ -14,6 +14,9 @@ import javax.inject.Inject;
public class Preferences {
private static final String P_CURRENT_VERSION = "cv"; //$NON-NLS-1$
private static final String P_CURRENT_VERSION_NAME = "cvname"; //$NON-NLS-1$
private final Context context;
private final SharedPreferences prefs;
@ -23,6 +26,24 @@ public class Preferences {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setIfUnset(SharedPreferences prefs, SharedPreferences.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, SharedPreferences.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;
}
public void clear() {
prefs
.edit()
@ -31,7 +52,7 @@ public class Preferences {
}
public void setDefaults() {
new AstridDefaultPreferenceSpec(context).setIfUnset();
new AstridDefaultPreferenceSpec(context, this).setIfUnset();
}
public void reset() {
@ -144,4 +165,16 @@ public class Preferences {
editor.remove(key);
editor.commit();
}
public int getCurrentVersion() {
return getInt(P_CURRENT_VERSION, 0);
}
public void setCurrentVersion(int version) {
setInt(P_CURRENT_VERSION, version);
}
public void setCurrentVersionName(String versionName) {
setString(P_CURRENT_VERSION_NAME, versionName);
}
}

Loading…
Cancel
Save