Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.deskclock.data;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.net.Uri;
     22 import android.preference.PreferenceManager;
     23 
     24 import com.android.deskclock.R;
     25 import com.android.deskclock.Utils;
     26 import com.android.deskclock.data.DataModel.CitySort;
     27 import com.android.deskclock.data.DataModel.ClockStyle;
     28 import com.android.deskclock.settings.ScreensaverSettingsActivity;
     29 import com.android.deskclock.settings.SettingsActivity;
     30 
     31 import java.util.Locale;
     32 import java.util.TimeZone;
     33 
     34 /**
     35  * This class encapsulates the storage of application preferences in {@link SharedPreferences}.
     36  */
     37 final class SettingsDAO {
     38 
     39     private static final String KEY_SORT_PREFERENCE = "sort_preference";
     40     private static final String KEY_DEFAULT_ALARM_RINGTONE_URI = "default_alarm_ringtone_uri";
     41 
     42     // Lazily instantiated and cached for the life of the application.
     43     private static SharedPreferences sPrefs;
     44 
     45     private SettingsDAO() {}
     46 
     47     /**
     48      * @return an enumerated value indicating the order in which cities are ordered
     49      */
     50     static CitySort getCitySort(Context context) {
     51         final int defaultSortOrdinal = CitySort.NAME.ordinal();
     52         final SharedPreferences prefs = getSharedPreferences(context);
     53         final int citySortOrdinal = prefs.getInt(KEY_SORT_PREFERENCE, defaultSortOrdinal);
     54         return CitySort.values()[citySortOrdinal];
     55     }
     56 
     57     /**
     58      * Adjust the sort order of cities.
     59      */
     60     static void toggleCitySort(Context context) {
     61         final CitySort oldSort = getCitySort(context);
     62         final CitySort newSort = oldSort == CitySort.NAME ? CitySort.UTC_OFFSET : CitySort.NAME;
     63         final SharedPreferences prefs = getSharedPreferences(context);
     64         prefs.edit().putInt(KEY_SORT_PREFERENCE, newSort.ordinal()).apply();
     65     }
     66 
     67     /**
     68      * @return {@code true} if a clock for the user's home timezone should be automatically
     69      *      displayed when it doesn't match the current timezone
     70      */
     71     static boolean getAutoShowHomeClock(Context context) {
     72         final SharedPreferences prefs = getSharedPreferences(context);
     73         return prefs.getBoolean(SettingsActivity.KEY_AUTO_HOME_CLOCK, false);
     74     }
     75 
     76     /**
     77      * @return the user's home timezone
     78      */
     79     static TimeZone getHomeTimeZone(Context context) {
     80         final SharedPreferences prefs = getSharedPreferences(context);
     81         final String defaultTimeZoneId = TimeZone.getDefault().getID();
     82         final String timeZoneId = prefs.getString(SettingsActivity.KEY_HOME_TZ, defaultTimeZoneId);
     83         return TimeZone.getTimeZone(timeZoneId);
     84     }
     85 
     86     /**
     87      * Sets the user's home timezone to the current system timezone if no home timezone is yet set.
     88      *
     89      * @param homeTimeZone the timezone to set as the user's home timezone if necessary
     90      */
     91     static void setDefaultHomeTimeZone(Context context, TimeZone homeTimeZone) {
     92         final SharedPreferences prefs = getSharedPreferences(context);
     93         final String homeTimeZoneId = prefs.getString(SettingsActivity.KEY_HOME_TZ, null);
     94         if (homeTimeZoneId == null) {
     95             prefs.edit().putString(SettingsActivity.KEY_HOME_TZ, homeTimeZone.getID()).apply();
     96         }
     97     }
     98 
     99     /**
    100      * @return a value indicating whether analog or digital clocks are displayed in the app
    101      */
    102     static ClockStyle getClockStyle(Context context) {
    103         return getClockStyle(context, SettingsActivity.KEY_CLOCK_STYLE);
    104     }
    105 
    106     /**
    107      * @return a value indicating whether analog or digital clocks are displayed on the screensaver
    108      */
    109     static ClockStyle getScreensaverClockStyle(Context context) {
    110         return getClockStyle(context, ScreensaverSettingsActivity.KEY_CLOCK_STYLE);
    111     }
    112 
    113     /**
    114      * @return the uri of the selected ringtone or the {@code defaultUri} if no explicit selection
    115      *      has yet been made
    116      */
    117     static Uri getTimerRingtoneUri(Context context, Uri defaultUri) {
    118         final SharedPreferences prefs = getSharedPreferences(context);
    119         final String uriString = prefs.getString(SettingsActivity.KEY_TIMER_RINGTONE, null);
    120         return uriString == null ? defaultUri : Uri.parse(uriString);
    121     }
    122 
    123     /**
    124      * @return the uri of the selected ringtone or the {@code defaultUri} if no explicit selection
    125      *      has yet been made
    126      */
    127     static Uri getDefaultAlarmRingtoneUri(Context context, Uri defaultUri) {
    128         final SharedPreferences prefs = getSharedPreferences(context);
    129         final String uriString = prefs.getString(KEY_DEFAULT_ALARM_RINGTONE_URI, null);
    130         return uriString == null ? defaultUri : Uri.parse(uriString);
    131     }
    132 
    133     /**
    134      * @param uri identifies the default ringtone to play for new alarms
    135      */
    136     static void setDefaultAlarmRingtoneUri(Context context, Uri uri) {
    137         final SharedPreferences prefs = getSharedPreferences(context);
    138         prefs.edit().putString(KEY_DEFAULT_ALARM_RINGTONE_URI, uri.toString()).apply();
    139     }
    140 
    141     private static ClockStyle getClockStyle(Context context, String prefKey) {
    142         final String defaultStyle = context.getString(R.string.default_clock_style);
    143         final SharedPreferences prefs = getSharedPreferences(context);
    144         final String clockStyle = prefs.getString(prefKey, defaultStyle);
    145         // Use hardcoded locale to perform toUpperCase, because in some languages toUpperCase adds
    146         // accent to character, which breaks the enum conversion.
    147         return ClockStyle.valueOf(clockStyle.toUpperCase(Locale.US));
    148     }
    149 
    150     private static SharedPreferences getSharedPreferences(Context context) {
    151         if (sPrefs == null) {
    152             sPrefs = Utils.getDefaultSharedPreferences(context.getApplicationContext());
    153         }
    154 
    155         return sPrefs;
    156     }
    157 }
    158