You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tasks/src/com/timsu/astrid/utilities/Preferences.java

121 lines
4.2 KiB
Java

package com.timsu.astrid.utilities;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.net.Uri;
import android.preference.PreferenceManager;
import com.timsu.astrid.R;
public class Preferences {
// pref keys
private static final String P_CURRENT_VERSION = "cv";
private static final String P_SHOW_REPEAT_HELP = "repeathelp";
// default values
private static final boolean DEFAULT_PERSISTENCE_MODE = true;
/** Set preference defaults, if unset. called at startup */
public static void setPreferenceDefaults(Context context) {
SharedPreferences prefs = getPrefs(context);
Resources r = context.getResources();
Editor editor = prefs.edit();
if(!prefs.contains(r.getString(R.string.p_notif_annoy))) {
editor.putBoolean(r.getString(R.string.p_notif_annoy),
DEFAULT_PERSISTENCE_MODE);
}
if(!prefs.contains(r.getString(R.string.p_fontSize))) {
editor.putString(r.getString(R.string.p_fontSize), "20");
}
if(!prefs.contains(r.getString(R.string.p_deadlineTime))) {
editor.putString(r.getString(R.string.p_deadlineTime), "7");
}
editor.commit();
}
/** CurrentVersion: the currently installed version of Astrid */
public static int getCurrentVersion(Context context) {
return getPrefs(context).getInt(P_CURRENT_VERSION, 0);
}
/** CurrentVersion: the currently installed version of Astrid */
public static void setCurrentVersion(Context context, int version) {
Editor editor = getPrefs(context).edit();
editor.putInt(P_CURRENT_VERSION, version);
editor.commit();
}
/** ShowRepeatHelp: whether help dialog should be shown about repeats */
public static boolean shouldShowRepeatHelp(Context context) {
return getPrefs(context).getBoolean(P_SHOW_REPEAT_HELP, true);
}
public static void setShowRepeatHelp(Context context, boolean setting) {
Editor editor = getPrefs(context).edit();
editor.putBoolean(P_SHOW_REPEAT_HELP, setting);
editor.commit();
}
/** returns hour at which quiet hours start, or null if not set */
public static Integer getQuietHourStart(Context context) {
return getIntegerValue(context, R.string.p_notif_quietStart);
}
/** returns hour at which quiet hours start, or null if not set */
public static Integer getQuietHourEnd(Context context) {
return getIntegerValue(context, R.string.p_notif_quietEnd);
}
/** Get notification ringtone, or null if not set */
public static Uri getNotificationRingtone(Context context) {
Resources r = context.getResources();
String value = getPrefs(context).getString(r.getString(
R.string.key_notification_ringtone), "");
try {
return Uri.parse(value);
} catch (RuntimeException e) {
return null;
}
}
/** Get perstence mode setting */
public static boolean isPersistenceMode(Context context) {
Resources r = context.getResources();
return getPrefs(context).getBoolean(r.getString(
R.string.p_notif_annoy), DEFAULT_PERSISTENCE_MODE);
}
/** returns the font size user wants on the front page */
public static Integer getTaskListFontSize(Context context) {
return getIntegerValue(context, R.string.p_fontSize);
}
/** Return # of days from now to set deadlines by default */
public static Integer getDefaultDeadlineDays(Context context) {
return getIntegerValue(context, R.string.p_deadlineTime);
}
// --- helper methods
private static SharedPreferences getPrefs(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
private static Integer getIntegerValue(Context context, int keyResource) {
Resources r = context.getResources();
String value = getPrefs(context).getString(r.getString(keyResource), "");
try {
return Integer.parseInt(value);
} catch (Exception e) {
return null;
}
}
}