diff options
author | James Lemieux <jplemieux@google.com> | 2016-12-05 20:57:23 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2016-12-05 20:57:23 +0000 |
commit | fadb6b35070b69632f7da14457dc49f31c5a2602 (patch) | |
tree | 5bf2d41c87e01e1b87a1c157921bfc19a221bebe | |
parent | 0e320a32fda4820da25cd537621c6c4005fbf12b (diff) | |
parent | ff62e7fa903e3b6b11d0443543725c1351ab289d (diff) | |
download | android_packages_apps_DeskClock-fadb6b35070b69632f7da14457dc49f31c5a2602.tar.gz android_packages_apps_DeskClock-fadb6b35070b69632f7da14457dc49f31c5a2602.tar.bz2 android_packages_apps_DeskClock-fadb6b35070b69632f7da14457dc49f31c5a2602.zip |
Merge "Refactor where SharedPreferences are built" into ub-deskclock-huddle
19 files changed, 167 insertions, 212 deletions
diff --git a/src/com/android/deskclock/DeskClockApplication.java b/src/com/android/deskclock/DeskClockApplication.java index 5929032c4..072872684 100644 --- a/src/com/android/deskclock/DeskClockApplication.java +++ b/src/com/android/deskclock/DeskClockApplication.java @@ -16,8 +16,12 @@ package com.android.deskclock; +import android.annotation.TargetApi; import android.app.Application; import android.content.Context; +import android.content.SharedPreferences; +import android.os.Build; +import android.preference.PreferenceManager; import com.android.deskclock.controller.Controller; import com.android.deskclock.data.DataModel; @@ -32,9 +36,31 @@ public class DeskClockApplication extends Application { super.onCreate(); final Context applicationContext = getApplicationContext(); - DataModel.getDataModel().setContext(applicationContext); - UiDataModel.getUiDataModel().setContext(applicationContext); + final SharedPreferences prefs = getDefaultSharedPreferences(applicationContext); + + DataModel.getDataModel().init(applicationContext, prefs); + UiDataModel.getUiDataModel().init(applicationContext, prefs); Controller.getController().setContext(applicationContext); Events.addEventTracker(new LogEventTracker(applicationContext)); } + + /** + * Returns the default {@link SharedPreferences} instance from the underlying storage context. + */ + @TargetApi(Build.VERSION_CODES.N) + private static SharedPreferences getDefaultSharedPreferences(Context context) { + final Context storageContext; + if (Utils.isNOrLater()) { + // All N devices have split storage areas. Migrate the existing preferences into the new + // device encrypted storage area if that has not yet occurred. + final String name = PreferenceManager.getDefaultSharedPreferencesName(context); + storageContext = context.createDeviceProtectedStorageContext(); + if (!storageContext.moveSharedPreferencesFrom(context, name)) { + LogUtils.wtf("Failed to migrate shared preferences"); + } + } else { + storageContext = context; + } + return PreferenceManager.getDefaultSharedPreferences(storageContext); + } }
\ No newline at end of file diff --git a/src/com/android/deskclock/data/AlarmModel.java b/src/com/android/deskclock/data/AlarmModel.java index 7602157cc..bf27b8b9c 100644 --- a/src/com/android/deskclock/data/AlarmModel.java +++ b/src/com/android/deskclock/data/AlarmModel.java @@ -30,8 +30,6 @@ import com.android.deskclock.provider.Alarm; */ final class AlarmModel { - private final Context mContext; - /** The model from which settings are fetched. */ private final SettingsModel mSettingsModel; @@ -39,11 +37,10 @@ final class AlarmModel { private Uri mDefaultAlarmRingtoneUri; AlarmModel(Context context, SettingsModel settingsModel) { - mContext = context; mSettingsModel = settingsModel; // Clear caches affected by system settings when system settings change. - final ContentResolver cr = mContext.getContentResolver(); + final ContentResolver cr = context.getContentResolver(); final ContentObserver observer = new SystemAlarmAlertChangeObserver(); cr.registerContentObserver(Settings.System.DEFAULT_ALARM_ALERT_URI, false, observer); } diff --git a/src/com/android/deskclock/data/CityDAO.java b/src/com/android/deskclock/data/CityDAO.java index 66021ca3e..8e1c74cf9 100644 --- a/src/com/android/deskclock/data/CityDAO.java +++ b/src/com/android/deskclock/data/CityDAO.java @@ -57,8 +57,7 @@ final class CityDAO { * @param cityMap maps city ids to city instances * @return the list of city ids selected for display by the user */ - static List<City> getSelectedCities(Map<String, City> cityMap) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static List<City> getSelectedCities(SharedPreferences prefs, Map<String, City> cityMap) { final int size = prefs.getInt(NUMBER_OF_CITIES, 0); final List<City> selectedCities = new ArrayList<>(size); @@ -76,8 +75,7 @@ final class CityDAO { /** * @param cities the collection of cities selected for display by the user */ - static void setSelectedCities(Collection<City> cities) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setSelectedCities(SharedPreferences prefs, Collection<City> cities) { final SharedPreferences.Editor editor = prefs.edit(); editor.putInt(NUMBER_OF_CITIES, cities.size()); diff --git a/src/com/android/deskclock/data/CityModel.java b/src/com/android/deskclock/data/CityModel.java index b81b961ef..9f4587568 100644 --- a/src/com/android/deskclock/data/CityModel.java +++ b/src/com/android/deskclock/data/CityModel.java @@ -44,6 +44,8 @@ final class CityModel { private final Context mContext; + private final SharedPreferences mPrefs; + /** The model from which settings are fetched. */ private final SettingsModel mSettingsModel; @@ -76,8 +78,9 @@ final class CityModel { /** A city instance representing the home timezone of the user. */ private City mHomeCity; - CityModel(Context context, SettingsModel settingsModel, SharedPreferences prefs) { + CityModel(Context context, SharedPreferences prefs, SettingsModel settingsModel) { mContext = context; + mPrefs = prefs; mSettingsModel = settingsModel; // Clear caches affected by locale when locale changes. @@ -118,22 +121,6 @@ final class CityModel { } /** - * @param cityName the case-insensitive city name to search for - * @return the city with the given {@code cityName}; {@code null} if no such city exists - */ - City getCity(String cityName) { - cityName = cityName.toUpperCase(); - - for (City city : getAllCities()) { - if (cityName.equals(city.getNameUpperCase())) { - return city; - } - } - - return null; - } - - /** * @return a city representing the user's home timezone */ City getHomeCity() { @@ -176,7 +163,7 @@ final class CityModel { */ List<City> getSelectedCities() { if (mSelectedCities == null) { - final List<City> selectedCities = CityDAO.getSelectedCities(getCityMap()); + final List<City> selectedCities = CityDAO.getSelectedCities(mPrefs, getCityMap()); Collections.sort(selectedCities, new City.UtcOffsetComparator()); mSelectedCities = Collections.unmodifiableList(selectedCities); } @@ -189,7 +176,7 @@ final class CityModel { */ void setSelectedCities(Collection<City> cities) { final List<City> oldCities = getAllCities(); - CityDAO.setSelectedCities(cities); + CityDAO.setSelectedCities(mPrefs, cities); // Clear caches affected by this update. mAllCities = null; diff --git a/src/com/android/deskclock/data/CustomRingtoneDAO.java b/src/com/android/deskclock/data/CustomRingtoneDAO.java index d560b27d8..a4d641781 100644 --- a/src/com/android/deskclock/data/CustomRingtoneDAO.java +++ b/src/com/android/deskclock/data/CustomRingtoneDAO.java @@ -16,12 +16,9 @@ package com.android.deskclock.data; -import android.content.Context; import android.content.SharedPreferences; import android.net.Uri; -import com.android.deskclock.Utils; - import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -53,9 +50,7 @@ final class CustomRingtoneDAO { * @param title the title of the audio content at the given {@code uri} * @return the newly added custom ringtone */ - static CustomRingtone addCustomRingtone(Uri uri, String title) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); - + static CustomRingtone addCustomRingtone(SharedPreferences prefs, Uri uri, String title) { final long id = prefs.getLong(NEXT_RINGTONE_ID, 0); final Set<String> ids = getRingtoneIds(prefs); ids.add(String.valueOf(id)); @@ -73,9 +68,7 @@ final class CustomRingtoneDAO { /** * @param id identifies the ringtone to be removed */ - static void removeCustomRingtone(long id) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); - + static void removeCustomRingtone(SharedPreferences prefs, long id) { final Set<String> ids = getRingtoneIds(prefs); ids.remove(String.valueOf(id)); @@ -94,9 +87,7 @@ final class CustomRingtoneDAO { /** * @return a list of all known custom ringtones */ - static List<CustomRingtone> getCustomRingtones() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); - + static List<CustomRingtone> getCustomRingtones(SharedPreferences prefs) { final Set<String> ids = prefs.getStringSet(RINGTONE_IDS, Collections.<String>emptySet()); final List<CustomRingtone> ringtones = new ArrayList<>(ids.size()); diff --git a/src/com/android/deskclock/data/DataModel.java b/src/com/android/deskclock/data/DataModel.java index 0c377a8e3..537dd7774 100644 --- a/src/com/android/deskclock/data/DataModel.java +++ b/src/com/android/deskclock/data/DataModel.java @@ -16,21 +16,17 @@ package com.android.deskclock.data; -import android.annotation.TargetApi; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.media.AudioManager; import android.net.Uri; -import android.os.Build; import android.os.Handler; import android.os.Looper; -import android.preference.PreferenceManager; import android.support.annotation.StringRes; import android.view.View; -import com.android.deskclock.LogUtils; import com.android.deskclock.R; import com.android.deskclock.Utils; import com.android.deskclock.timer.TimerService; @@ -96,7 +92,7 @@ public final class DataModel { final int index = am.getStreamMaxVolume(STREAM_ALARM) / 3; am.setStreamVolume(STREAM_ALARM, index, FLAG_SHOW_UI); } - }; + } private static class ChangeSoundSettingsListener implements View.OnClickListener { @Override @@ -105,7 +101,7 @@ public final class DataModel { context.startActivity(new Intent(ACTION_SOUND_SETTINGS) .addFlags(FLAG_ACTIVITY_NEW_TASK)); } - }; + } private static class ChangeAppNotificationSettingsListener implements View.OnClickListener { @Override @@ -130,7 +126,7 @@ public final class DataModel { .setData(Uri.fromParts("package", context.getPackageName(), null)) .addFlags(FLAG_ACTIVITY_NEW_TASK)); } - }; + } } public static final String ACTION_WORLD_CITIES_CHANGED = @@ -187,50 +183,28 @@ public final class DataModel { private DataModel() {} /** - * The context may be set precisely once during the application life. + * Initializes the data model with the context and shared preferences to be used. */ - public void setContext(Context context) { + public void init(Context context, SharedPreferences prefs) { if (mContext != context) { mContext = context.getApplicationContext(); - sSharedPreferences = getDefaultSharedPreferences(mContext); + sSharedPreferences = prefs; mTimeModel = new TimeModel(mContext); - mWidgetModel = new WidgetModel(); - mSettingsModel = new SettingsModel(mContext); - mRingtoneModel = new RingtoneModel(mContext); + mWidgetModel = new WidgetModel(prefs); + mSettingsModel = new SettingsModel(mContext, prefs); + mRingtoneModel = new RingtoneModel(mContext, prefs); mNotificationModel = new NotificationModel(); - mCityModel = new CityModel(mContext, mSettingsModel, sSharedPreferences); + mCityModel = new CityModel(mContext, prefs, mSettingsModel); mAlarmModel = new AlarmModel(mContext, mSettingsModel); mSilentSettingsModel = new SilentSettingsModel(mContext, mNotificationModel); - mStopwatchModel = new StopwatchModel(mContext, mNotificationModel); - mTimerModel = new TimerModel(mContext, mSettingsModel, mRingtoneModel, - mNotificationModel, sSharedPreferences); + mStopwatchModel = new StopwatchModel(mContext, prefs, mNotificationModel); + mTimerModel = new TimerModel(mContext, prefs, mSettingsModel, mRingtoneModel, + mNotificationModel); } } /** - * Returns the default {@link SharedPreferences} instance from the underlying storage context. - */ - @TargetApi(Build.VERSION_CODES.N) - private static synchronized SharedPreferences getDefaultSharedPreferences(Context context) { - final Context storageContext; - if (Utils.isNOrLater()) { - // All N devices have split storage areas, but we may need to - // migrate existing preferences into the new device encrypted - // storage area, which is where our data lives from now on. - storageContext = context.createDeviceProtectedStorageContext(); - if (!storageContext.moveSharedPreferencesFrom(context, - PreferenceManager.getDefaultSharedPreferencesName(context))) { - LogUtils.wtf("Failed to migrate shared preferences"); - } - } else { - storageContext = context; - } - sSharedPreferences = PreferenceManager.getDefaultSharedPreferences(storageContext); - return sSharedPreferences; - } - - /** * Convenience for {@code run(runnable, 0)}, i.e. waits indefinitely. */ public void run(Runnable runnable) { @@ -342,15 +316,6 @@ public final class DataModel { } /** - * @param cityName the case-insensitive city name to search for - * @return the city with the given {@code cityName}; {@code null} if no such city exists - */ - public City getCity(String cityName) { - enforceMainLooper(); - return mCityModel.getCity(cityName); - } - - /** * @return a city representing the user's home timezone */ public City getHomeCity() { diff --git a/src/com/android/deskclock/data/RingtoneModel.java b/src/com/android/deskclock/data/RingtoneModel.java index e73a8ccb3..aa87e9564 100644 --- a/src/com/android/deskclock/data/RingtoneModel.java +++ b/src/com/android/deskclock/data/RingtoneModel.java @@ -21,6 +21,7 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.database.ContentObserver; import android.database.Cursor; import android.media.Ringtone; @@ -48,6 +49,8 @@ final class RingtoneModel { private final Context mContext; + private final SharedPreferences mPrefs; + /** Maps ringtone uri to ringtone title; looking up a title from scratch is expensive. */ private final Map<Uri, String> mRingtoneTitles = new ArrayMap<>(16); @@ -58,8 +61,9 @@ final class RingtoneModel { /** A mutable copy of the custom ringtones. */ private List<CustomRingtone> mCustomRingtones; - RingtoneModel(Context context) { + RingtoneModel(Context context, SharedPreferences prefs) { mContext = context; + mPrefs = prefs; // Clear caches affected by system settings when system settings change. final ContentResolver cr = mContext.getContentResolver(); @@ -78,7 +82,7 @@ final class RingtoneModel { return existing; } - final CustomRingtone ringtone = CustomRingtoneDAO.addCustomRingtone(uri, title); + final CustomRingtone ringtone = CustomRingtoneDAO.addCustomRingtone(mPrefs, uri, title); getMutableCustomRingtones().add(ringtone); Collections.sort(getMutableCustomRingtones()); return ringtone; @@ -88,7 +92,7 @@ final class RingtoneModel { final List<CustomRingtone> ringtones = getMutableCustomRingtones(); for (CustomRingtone ringtone : ringtones) { if (ringtone.getUri().equals(uri)) { - CustomRingtoneDAO.removeCustomRingtone(ringtone.getId()); + CustomRingtoneDAO.removeCustomRingtone(mPrefs, ringtone.getId()); ringtones.remove(ringtone); break; } @@ -163,7 +167,7 @@ final class RingtoneModel { private List<CustomRingtone> getMutableCustomRingtones() { if (mCustomRingtones == null) { - mCustomRingtones = CustomRingtoneDAO.getCustomRingtones(); + mCustomRingtones = CustomRingtoneDAO.getCustomRingtones(mPrefs); Collections.sort(mCustomRingtones); } diff --git a/src/com/android/deskclock/data/SettingsDAO.java b/src/com/android/deskclock/data/SettingsDAO.java index c3d271b29..e98a706c0 100644 --- a/src/com/android/deskclock/data/SettingsDAO.java +++ b/src/com/android/deskclock/data/SettingsDAO.java @@ -54,9 +54,8 @@ final class SettingsDAO { /** * @return an enumerated value indicating the order in which cities are ordered */ - static CitySort getCitySort() { + static CitySort getCitySort(SharedPreferences prefs) { final int defaultSortOrdinal = CitySort.NAME.ordinal(); - final SharedPreferences prefs = DataModel.getSharedPreferences(); final int citySortOrdinal = prefs.getInt(KEY_SORT_PREFERENCE, defaultSortOrdinal); return CitySort.values()[citySortOrdinal]; } @@ -64,10 +63,9 @@ final class SettingsDAO { /** * Adjust the sort order of cities. */ - static void toggleCitySort() { - final CitySort oldSort = getCitySort(); + static void toggleCitySort(SharedPreferences prefs) { + final CitySort oldSort = getCitySort(prefs); final CitySort newSort = oldSort == CitySort.NAME ? CitySort.UTC_OFFSET : CitySort.NAME; - final SharedPreferences prefs = DataModel.getSharedPreferences(); prefs.edit().putInt(KEY_SORT_PREFERENCE, newSort.ordinal()).apply(); } @@ -75,16 +73,14 @@ final class SettingsDAO { * @return {@code true} if a clock for the user's home timezone should be automatically * displayed when it doesn't match the current timezone */ - static boolean getAutoShowHomeClock() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static boolean getAutoShowHomeClock(SharedPreferences prefs) { return prefs.getBoolean(SettingsActivity.KEY_AUTO_HOME_CLOCK, false); } /** * @return the user's home timezone */ - static TimeZone getHomeTimeZone() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static TimeZone getHomeTimeZone(SharedPreferences prefs) { final String defaultTimeZoneId = TimeZone.getDefault().getID(); final String timeZoneId = prefs.getString(SettingsActivity.KEY_HOME_TZ, defaultTimeZoneId); return TimeZone.getTimeZone(timeZoneId); @@ -95,8 +91,7 @@ final class SettingsDAO { * * @param homeTimeZone the timezone to set as the user's home timezone if necessary */ - static void setDefaultHomeTimeZone(TimeZone homeTimeZone) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setDefaultHomeTimeZone(SharedPreferences prefs, TimeZone homeTimeZone) { final String homeTimeZoneId = prefs.getString(SettingsActivity.KEY_HOME_TZ, null); if (homeTimeZoneId == null) { prefs.edit().putString(SettingsActivity.KEY_HOME_TZ, homeTimeZone.getID()).apply(); @@ -106,23 +101,21 @@ final class SettingsDAO { /** * @return a value indicating whether analog or digital clocks are displayed in the app */ - static ClockStyle getClockStyle(Context context) { - return getClockStyle(context, SettingsActivity.KEY_CLOCK_STYLE); + static ClockStyle getClockStyle(Context context, SharedPreferences prefs) { + return getClockStyle(context, prefs, SettingsActivity.KEY_CLOCK_STYLE); } /** * @return a value indicating whether analog or digital clocks are displayed in the app */ - static boolean getDisplayClockSeconds() { - return DataModel.getSharedPreferences().getBoolean( - SettingsActivity.KEY_CLOCK_DISPLAY_SECONDS, false); + static boolean getDisplayClockSeconds(SharedPreferences prefs) { + return prefs.getBoolean(SettingsActivity.KEY_CLOCK_DISPLAY_SECONDS, false); } /** * @param displaySeconds whether or not to display seconds on main clock */ - static void setDisplayClockSeconds(boolean displaySeconds) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setDisplayClockSeconds(SharedPreferences prefs, boolean displaySeconds) { prefs.edit().putBoolean(SettingsActivity.KEY_CLOCK_DISPLAY_SECONDS, displaySeconds).apply(); } @@ -130,27 +123,25 @@ final class SettingsDAO { * Sets the user's display seconds preference based on the currently selected clock if one has * not yet been manually chosen. */ - static void setDefaultDisplayClockSeconds(Context context) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setDefaultDisplayClockSeconds(Context context, SharedPreferences prefs) { if (!prefs.contains(SettingsActivity.KEY_CLOCK_DISPLAY_SECONDS)) { // If on analog clock style on upgrade, default to true. Otherwise, default to false. - final boolean isAnalog = getClockStyle(context) == ClockStyle.ANALOG; - setDisplayClockSeconds(isAnalog); + final boolean isAnalog = getClockStyle(context, prefs) == ClockStyle.ANALOG; + setDisplayClockSeconds(prefs, isAnalog); } } /** * @return a value indicating whether analog or digital clocks are displayed on the screensaver */ - static ClockStyle getScreensaverClockStyle(Context context) { - return getClockStyle(context, ScreensaverSettingsActivity.KEY_CLOCK_STYLE); + static ClockStyle getScreensaverClockStyle(Context context, SharedPreferences prefs) { + return getClockStyle(context, prefs, ScreensaverSettingsActivity.KEY_CLOCK_STYLE); } /** * @return {@code true} if the screen saver should be dimmed for lower contrast at night */ - static boolean getScreensaverNightModeOn() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static boolean getScreensaverNightModeOn(SharedPreferences prefs) { return prefs.getBoolean(ScreensaverSettingsActivity.KEY_NIGHT_MODE, false); } @@ -158,8 +149,7 @@ final class SettingsDAO { * @return the uri of the selected ringtone or the {@code defaultUri} if no explicit selection * has yet been made */ - static Uri getTimerRingtoneUri(Uri defaultUri) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static Uri getTimerRingtoneUri(SharedPreferences prefs, Uri defaultUri) { final String uriString = prefs.getString(SettingsActivity.KEY_TIMER_RINGTONE, null); return uriString == null ? defaultUri : Uri.parse(uriString); } @@ -167,24 +157,21 @@ final class SettingsDAO { /** * @return whether timer vibration is enabled. false by default. */ - static boolean getTimerVibrate() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static boolean getTimerVibrate(SharedPreferences prefs) { return prefs.getBoolean(SettingsActivity.KEY_TIMER_VIBRATE, false); } /** * @param enabled whether vibration will be turned on for all timers. */ - static void setTimerVibrate(boolean enabled) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setTimerVibrate(SharedPreferences prefs, boolean enabled) { prefs.edit().putBoolean(SettingsActivity.KEY_TIMER_VIBRATE, enabled).apply(); } /** * @param uri the uri of the ringtone to play for all timers */ - static void setTimerRingtoneUri(Uri uri) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setTimerRingtoneUri(SharedPreferences prefs, Uri uri) { prefs.edit().putString(SettingsActivity.KEY_TIMER_RINGTONE, uri.toString()).apply(); } @@ -192,8 +179,7 @@ final class SettingsDAO { * @return the uri of the selected ringtone or the {@code defaultUri} if no explicit selection * has yet been made */ - static Uri getDefaultAlarmRingtoneUri() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static Uri getDefaultAlarmRingtoneUri(SharedPreferences prefs) { final String uriString = prefs.getString(KEY_DEFAULT_ALARM_RINGTONE_URI, null); return uriString == null ? Settings.System.DEFAULT_ALARM_ALERT_URI : Uri.parse(uriString); } @@ -201,8 +187,7 @@ final class SettingsDAO { /** * @param uri identifies the default ringtone to play for new alarms */ - static void setDefaultAlarmRingtoneUri(Uri uri) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setDefaultAlarmRingtoneUri(SharedPreferences prefs, Uri uri) { prefs.edit().putString(KEY_DEFAULT_ALARM_RINGTONE_URI, uri.toString()).apply(); } @@ -210,8 +195,7 @@ final class SettingsDAO { * @return the display order of the weekdays, which can start with {@link Calendar#SATURDAY}, * {@link Calendar#SUNDAY} or {@link Calendar#MONDAY} */ - static Weekdays.Order getWeekdayOrder() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static Weekdays.Order getWeekdayOrder(SharedPreferences prefs) { final String defaultValue = String.valueOf(Calendar.getInstance().getFirstDayOfWeek()); final String value = prefs.getString(SettingsActivity.KEY_WEEK_START, defaultValue); final int firstCalendarDay = Integer.parseInt(value); @@ -224,10 +208,9 @@ final class SettingsDAO { } } - private static ClockStyle getClockStyle(Context context, String prefKey) { + private static ClockStyle getClockStyle(Context context, SharedPreferences prefs, String key) { final String defaultStyle = context.getString(R.string.default_clock_style); - final SharedPreferences prefs = DataModel.getSharedPreferences(); - final String clockStyle = prefs.getString(prefKey, defaultStyle); + final String clockStyle = prefs.getString(key, defaultStyle); // Use hardcoded locale to perform toUpperCase, because in some languages toUpperCase adds // accent to character, which breaks the enum conversion. return ClockStyle.valueOf(clockStyle.toUpperCase(Locale.US)); diff --git a/src/com/android/deskclock/data/SettingsModel.java b/src/com/android/deskclock/data/SettingsModel.java index aa1a62160..21e928f0f 100644 --- a/src/com/android/deskclock/data/SettingsModel.java +++ b/src/com/android/deskclock/data/SettingsModel.java @@ -17,6 +17,7 @@ package com.android.deskclock.data; import android.content.Context; +import android.content.SharedPreferences; import android.net.Uri; import com.android.deskclock.R; @@ -33,58 +34,61 @@ final class SettingsModel { private final Context mContext; + private final SharedPreferences mPrefs; + /** The uri of the default ringtone to use for timers until the user explicitly chooses one. */ private Uri mDefaultTimerRingtoneUri; - SettingsModel(Context context) { + SettingsModel(Context context, SharedPreferences prefs) { mContext = context; + mPrefs = prefs; // Set the user's default home timezone if one has not yet been chosen. - SettingsDAO.setDefaultHomeTimeZone(TimeZone.getDefault()); + SettingsDAO.setDefaultHomeTimeZone(prefs, TimeZone.getDefault()); // Set the user's default display seconds preference if one has not yet been chosen. - SettingsDAO.setDefaultDisplayClockSeconds(mContext); + SettingsDAO.setDefaultDisplayClockSeconds(mContext, prefs); } CitySort getCitySort() { - return SettingsDAO.getCitySort(); + return SettingsDAO.getCitySort(mPrefs); } void toggleCitySort() { - SettingsDAO.toggleCitySort(); + SettingsDAO.toggleCitySort(mPrefs); } TimeZone getHomeTimeZone() { - return SettingsDAO.getHomeTimeZone(); + return SettingsDAO.getHomeTimeZone(mPrefs); } ClockStyle getClockStyle() { - return SettingsDAO.getClockStyle(mContext); + return SettingsDAO.getClockStyle(mContext, mPrefs); } boolean getDisplayClockSeconds() { - return SettingsDAO.getDisplayClockSeconds(); + return SettingsDAO.getDisplayClockSeconds(mPrefs); } void setDisplayClockSeconds(boolean shouldDisplaySeconds) { - SettingsDAO.setDisplayClockSeconds(shouldDisplaySeconds); + SettingsDAO.setDisplayClockSeconds(mPrefs, shouldDisplaySeconds); } ClockStyle getScreensaverClockStyle() { - return SettingsDAO.getScreensaverClockStyle(mContext); + return SettingsDAO.getScreensaverClockStyle(mContext, mPrefs); } boolean getScreensaverNightModeOn() { - return SettingsDAO.getScreensaverNightModeOn(); + return SettingsDAO.getScreensaverNightModeOn(mPrefs); } boolean getShowHomeClock() { - if (!SettingsDAO.getAutoShowHomeClock()) { + if (!SettingsDAO.getAutoShowHomeClock(mPrefs)) { return false; } // Show the home clock if the current time and home time differ. // (By using UTC offset for this comparison the various DST rules are considered) - final TimeZone homeTimeZone = SettingsDAO.getHomeTimeZone(); + final TimeZone homeTimeZone = SettingsDAO.getHomeTimeZone(mPrefs); final long now = System.currentTimeMillis(); return homeTimeZone.getOffset(now) != TimeZone.getDefault().getOffset(now); } @@ -98,30 +102,30 @@ final class SettingsModel { } void setTimerRingtoneUri(Uri uri) { - SettingsDAO.setTimerRingtoneUri(uri); + SettingsDAO.setTimerRingtoneUri(mPrefs, uri); } Uri getTimerRingtoneUri() { - return SettingsDAO.getTimerRingtoneUri(getDefaultTimerRingtoneUri()); + return SettingsDAO.getTimerRingtoneUri(mPrefs, getDefaultTimerRingtoneUri()); } Uri getDefaultAlarmRingtoneUri() { - return SettingsDAO.getDefaultAlarmRingtoneUri(); + return SettingsDAO.getDefaultAlarmRingtoneUri(mPrefs); } void setDefaultAlarmRingtoneUri(Uri uri) { - SettingsDAO.setDefaultAlarmRingtoneUri(uri); + SettingsDAO.setDefaultAlarmRingtoneUri(mPrefs, uri); } Weekdays.Order getWeekdayOrder() { - return SettingsDAO.getWeekdayOrder(); + return SettingsDAO.getWeekdayOrder(mPrefs); } boolean getTimerVibrate() { - return SettingsDAO.getTimerVibrate(); + return SettingsDAO.getTimerVibrate(mPrefs); } void setTimerVibrate(boolean enabled) { - SettingsDAO.setTimerVibrate(enabled); + SettingsDAO.setTimerVibrate(mPrefs, enabled); } }
\ No newline at end of file diff --git a/src/com/android/deskclock/data/SilentSettingsModel.java b/src/com/android/deskclock/data/SilentSettingsModel.java index 3492848f9..4e41dfecc 100644 --- a/src/com/android/deskclock/data/SilentSettingsModel.java +++ b/src/com/android/deskclock/data/SilentSettingsModel.java @@ -146,8 +146,7 @@ final class SilentSettingsModel { * associated ringtone from playing. If any of them would prevent an alarm from firing or * making noise, a description of the setting is reported to this model on the main thread. */ - private final class CheckSilenceSettingsTask - extends AsyncTask<Void, Void, SilentSetting> { + private final class CheckSilenceSettingsTask extends AsyncTask<Void, Void, SilentSetting> { @Override protected SilentSetting doInBackground(Void... parameters) { if (!isCancelled() && isDoNotDisturbBlockingAlarms()) { diff --git a/src/com/android/deskclock/data/StopwatchDAO.java b/src/com/android/deskclock/data/StopwatchDAO.java index 6353a7ab9..823ab63fd 100644 --- a/src/com/android/deskclock/data/StopwatchDAO.java +++ b/src/com/android/deskclock/data/StopwatchDAO.java @@ -55,8 +55,7 @@ final class StopwatchDAO { /** * @return the stopwatch from permanent storage or a reset stopwatch if none exists */ - static Stopwatch getStopwatch() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static Stopwatch getStopwatch(SharedPreferences prefs) { final int stateIndex = prefs.getInt(STATE, RESET.ordinal()); final State state = State.values()[stateIndex]; final long lastStartTime = prefs.getLong(LAST_START_TIME, Stopwatch.UNUSED); @@ -68,8 +67,7 @@ final class StopwatchDAO { /** * @param stopwatch the last state of the stopwatch */ - static void setStopwatch(Stopwatch stopwatch) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setStopwatch(SharedPreferences prefs, Stopwatch stopwatch) { final SharedPreferences.Editor editor = prefs.edit(); if (stopwatch.isReset()) { @@ -90,9 +88,7 @@ final class StopwatchDAO { /** * @return a list of recorded laps for the stopwatch */ - static List<Lap> getLaps() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); - + static List<Lap> getLaps(SharedPreferences prefs) { // Prepare the container to be filled with laps. final int lapCount = prefs.getInt(LAP_COUNT, 0); final List<Lap> laps = new ArrayList<>(lapCount); @@ -125,8 +121,7 @@ final class StopwatchDAO { * @param newLapCount the number of laps including the new lap * @param accumulatedTime the amount of time accumulate by the stopwatch at the end of the lap */ - static void addLap(int newLapCount, long accumulatedTime) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void addLap(SharedPreferences prefs, int newLapCount, long accumulatedTime) { prefs.edit() .putInt(LAP_COUNT, newLapCount) .putLong(LAP_ACCUMULATED_TIME + newLapCount, accumulatedTime) @@ -136,8 +131,7 @@ final class StopwatchDAO { /** * Remove the recorded laps for the stopwatch */ - static void clearLaps() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void clearLaps(SharedPreferences prefs) { final SharedPreferences.Editor editor = prefs.edit(); final int lapCount = prefs.getInt(LAP_COUNT, 0); diff --git a/src/com/android/deskclock/data/StopwatchModel.java b/src/com/android/deskclock/data/StopwatchModel.java index e53723717..7d1e4ecda 100644 --- a/src/com/android/deskclock/data/StopwatchModel.java +++ b/src/com/android/deskclock/data/StopwatchModel.java @@ -21,6 +21,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.support.annotation.VisibleForTesting; import android.support.v4.app.NotificationManagerCompat; @@ -35,6 +36,8 @@ final class StopwatchModel { private final Context mContext; + private final SharedPreferences mPrefs; + /** The model from which notification data are fetched. */ private final NotificationModel mNotificationModel; @@ -58,8 +61,9 @@ final class StopwatchModel { /** A mutable copy of the recorded stopwatch laps. */ private List<Lap> mLaps; - StopwatchModel(Context context, NotificationModel notificationModel) { + StopwatchModel(Context context, SharedPreferences prefs, NotificationModel notificationModel) { mContext = context; + mPrefs = prefs; mNotificationModel = notificationModel; mNotificationManager = NotificationManagerCompat.from(context); @@ -87,7 +91,7 @@ final class StopwatchModel { */ Stopwatch getStopwatch() { if (mStopwatch == null) { - mStopwatch = StopwatchDAO.getStopwatch(); + mStopwatch = StopwatchDAO.getStopwatch(mPrefs); } return mStopwatch; @@ -99,7 +103,7 @@ final class StopwatchModel { Stopwatch setStopwatch(Stopwatch stopwatch) { final Stopwatch before = getStopwatch(); if (before != stopwatch) { - StopwatchDAO.setStopwatch(stopwatch); + StopwatchDAO.setStopwatch(mPrefs, stopwatch); mStopwatch = stopwatch; // Refresh the stopwatch notification to reflect the latest stopwatch state. @@ -140,7 +144,7 @@ final class StopwatchModel { final List<Lap> laps = getMutableLaps(); final int lapNumber = laps.size() + 1; - StopwatchDAO.addLap(lapNumber, totalTime); + StopwatchDAO.addLap(mPrefs, lapNumber, totalTime); final long prevAccumulatedTime = laps.isEmpty() ? 0 : laps.get(0).getAccumulatedTime(); final long lapTime = totalTime - prevAccumulatedTime; @@ -166,7 +170,7 @@ final class StopwatchModel { */ @VisibleForTesting void clearLaps() { - StopwatchDAO.clearLaps(); + StopwatchDAO.clearLaps(mPrefs); getMutableLaps().clear(); } @@ -233,7 +237,7 @@ final class StopwatchModel { private List<Lap> getMutableLaps() { if (mLaps == null) { - mLaps = StopwatchDAO.getLaps(); + mLaps = StopwatchDAO.getLaps(mPrefs); } return mLaps; @@ -248,4 +252,4 @@ final class StopwatchModel { updateNotification(); } } -} +}
\ No newline at end of file diff --git a/src/com/android/deskclock/data/TimerDAO.java b/src/com/android/deskclock/data/TimerDAO.java index c153b1f92..32d36d9db 100644 --- a/src/com/android/deskclock/data/TimerDAO.java +++ b/src/com/android/deskclock/data/TimerDAO.java @@ -69,9 +69,7 @@ final class TimerDAO { /** * @return the timers from permanent storage */ - static List<Timer> getTimers() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); - + static List<Timer> getTimers(SharedPreferences prefs) { // Read the set of timer ids. final Set<String> timerIds = prefs.getStringSet(TIMER_IDS, Collections.<String>emptySet()); final List<Timer> timers = new ArrayList<>(timerIds.size()); @@ -104,8 +102,7 @@ final class TimerDAO { /** * @param timer the timer to be added */ - static Timer addTimer(Timer timer) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static Timer addTimer(SharedPreferences prefs, Timer timer) { final SharedPreferences.Editor editor = prefs.edit(); // Fetch the next timer id. @@ -113,7 +110,7 @@ final class TimerDAO { editor.putInt(NEXT_TIMER_ID, id + 1); // Add the new timer id to the set of all timer ids. - final Set<String> timerIds = new HashSet<>(getTimerIds()); + final Set<String> timerIds = new HashSet<>(getTimerIds(prefs)); timerIds.add(String.valueOf(id)); editor.putStringSet(TIMER_IDS, timerIds); @@ -138,8 +135,7 @@ final class TimerDAO { /** * @param timer the timer to be updated */ - static void updateTimer(Timer timer) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void updateTimer(SharedPreferences prefs, Timer timer) { final SharedPreferences.Editor editor = prefs.edit(); // Record the fields of the timer. @@ -159,14 +155,13 @@ final class TimerDAO { /** * @param timer the timer to be removed */ - static void removeTimer(Timer timer) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void removeTimer(SharedPreferences prefs, Timer timer) { final SharedPreferences.Editor editor = prefs.edit(); final int id = timer.getId(); // Remove the timer id from the set of all timer ids. - final Set<String> timerIds = new HashSet<>(getTimerIds()); + final Set<String> timerIds = new HashSet<>(getTimerIds(prefs)); timerIds.remove(String.valueOf(id)); if (timerIds.isEmpty()) { editor.remove(TIMER_IDS); @@ -188,8 +183,7 @@ final class TimerDAO { editor.apply(); } - private static Set<String> getTimerIds() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + private static Set<String> getTimerIds(SharedPreferences prefs) { return prefs.getStringSet(TIMER_IDS, Collections.<String>emptySet()); } }
\ No newline at end of file diff --git a/src/com/android/deskclock/data/TimerModel.java b/src/com/android/deskclock/data/TimerModel.java index ca676c80a..fb2c06a3c 100644 --- a/src/com/android/deskclock/data/TimerModel.java +++ b/src/com/android/deskclock/data/TimerModel.java @@ -64,6 +64,8 @@ final class TimerModel { private final Context mContext; + private final SharedPreferences mPrefs; + /** The alarm manager system service that calls back when timers expire. */ private final AlarmManager mAlarmManager; @@ -126,9 +128,10 @@ final class TimerModel { */ private Service mService; - TimerModel(Context context, SettingsModel settingsModel, RingtoneModel ringtoneModel, - NotificationModel notificationModel, SharedPreferences prefs) { + TimerModel(Context context, SharedPreferences prefs, SettingsModel settingsModel, + RingtoneModel ringtoneModel, NotificationModel notificationModel) { mContext = context; + mPrefs = prefs; mSettingsModel = settingsModel; mRingtoneModel = ringtoneModel; mNotificationModel = notificationModel; @@ -175,7 +178,7 @@ final class TimerModel { /** * @return all missed timers in their expiration order */ - List<Timer> getMissedTimers() { + private List<Timer> getMissedTimers() { return Collections.unmodifiableList(getMutableMissedTimers()); } @@ -214,7 +217,7 @@ final class TimerModel { label, deleteAfterUse); // Add the timer to permanent storage. - timer = TimerDAO.addTimer(timer); + timer = TimerDAO.addTimer(mPrefs, timer); // Add the timer to the cache. getMutableTimers().add(0, timer); @@ -460,7 +463,7 @@ final class TimerModel { private List<Timer> getMutableTimers() { if (mTimers == null) { - mTimers = TimerDAO.getTimers(); + mTimers = TimerDAO.getTimers(mPrefs); Collections.sort(mTimers, Timer.ID_COMPARATOR); } @@ -516,7 +519,7 @@ final class TimerModel { } // Update the timer in permanent storage. - TimerDAO.updateTimer(timer); + TimerDAO.updateTimer(mPrefs, timer); // Update the timer in the cache. final Timer oldTimer = timers.set(index, timer); @@ -550,9 +553,9 @@ final class TimerModel { * * @param timer an existing timer to be removed */ - void doRemoveTimer(Timer timer) { + private void doRemoveTimer(Timer timer) { // Remove the timer from permanent storage. - TimerDAO.removeTimer(timer); + TimerDAO.removeTimer(mPrefs, timer); // Remove the timer from the cache. final List<Timer> timers = getMutableTimers(); diff --git a/src/com/android/deskclock/data/WidgetDAO.java b/src/com/android/deskclock/data/WidgetDAO.java index c7609c7bb..da7dc1922 100644 --- a/src/com/android/deskclock/data/WidgetDAO.java +++ b/src/com/android/deskclock/data/WidgetDAO.java @@ -34,8 +34,7 @@ final class WidgetDAO { * @param count the number of widgets of the given type * @return the delta between the new count and the old count */ - static int updateWidgetCount(Class widgetProviderClass, int count) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static int updateWidgetCount(SharedPreferences prefs, Class widgetProviderClass, int count) { final String key = widgetProviderClass.getSimpleName() + WIDGET_COUNT; final int oldCount = prefs.getInt(key, 0); if (count == 0) { diff --git a/src/com/android/deskclock/data/WidgetModel.java b/src/com/android/deskclock/data/WidgetModel.java index d51c4d9ff..27fdd2ed0 100644 --- a/src/com/android/deskclock/data/WidgetModel.java +++ b/src/com/android/deskclock/data/WidgetModel.java @@ -16,7 +16,7 @@ package com.android.deskclock.data; -import android.content.Context; +import android.content.SharedPreferences; import android.support.annotation.StringRes; import com.android.deskclock.R; @@ -27,13 +27,19 @@ import com.android.deskclock.events.Events; */ final class WidgetModel { + private final SharedPreferences mPrefs; + + WidgetModel(SharedPreferences prefs) { + mPrefs = prefs; + } + /** * @param widgetClass indicates the type of widget being counted * @param count the number of widgets of the given type * @param eventCategoryId identifies the category of event to send */ void updateWidgetCount(Class widgetClass, int count, @StringRes int eventCategoryId) { - int delta = WidgetDAO.updateWidgetCount(widgetClass, count); + int delta = WidgetDAO.updateWidgetCount(mPrefs, widgetClass, count); for (; delta > 0; delta--) { Events.sendEvent(eventCategoryId, R.string.action_create, 0); } diff --git a/src/com/android/deskclock/uidata/TabDAO.java b/src/com/android/deskclock/uidata/TabDAO.java index 195c24a97..3be5d1971 100644 --- a/src/com/android/deskclock/uidata/TabDAO.java +++ b/src/com/android/deskclock/uidata/TabDAO.java @@ -18,8 +18,6 @@ package com.android.deskclock.uidata; import android.content.SharedPreferences; -import com.android.deskclock.data.DataModel; - import static com.android.deskclock.uidata.UiDataModel.Tab; /** @@ -35,8 +33,7 @@ final class TabDAO { /** * @return an enumerated value indicating the currently selected primary tab */ - static Tab getSelectedTab() { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static Tab getSelectedTab(SharedPreferences prefs) { final int ordinal = prefs.getInt(KEY_SELECTED_TAB, Tab.CLOCKS.ordinal()); return Tab.values()[ordinal]; } @@ -44,8 +41,7 @@ final class TabDAO { /** * @param tab an enumerated value indicating the newly selected primary tab */ - static void setSelectedTab(Tab tab) { - final SharedPreferences prefs = DataModel.getSharedPreferences(); + static void setSelectedTab(SharedPreferences prefs, Tab tab) { prefs.edit().putInt(KEY_SELECTED_TAB, tab.ordinal()).apply(); } }
\ No newline at end of file diff --git a/src/com/android/deskclock/uidata/TabModel.java b/src/com/android/deskclock/uidata/TabModel.java index dea9f24f3..5b878ed9d 100644 --- a/src/com/android/deskclock/uidata/TabModel.java +++ b/src/com/android/deskclock/uidata/TabModel.java @@ -16,6 +16,7 @@ package com.android.deskclock.uidata; +import android.content.SharedPreferences; import android.text.TextUtils; import java.util.ArrayList; @@ -31,6 +32,8 @@ import static com.android.deskclock.uidata.UiDataModel.Tab; */ final class TabModel { + private final SharedPreferences mPrefs; + /** The listeners to notify when the selected tab is changed. */ private final List<TabListener> mTabListeners = new ArrayList<>(); @@ -43,7 +46,8 @@ final class TabModel { /** An enumerated value indicating the currently selected tab. */ private Tab mSelectedTab; - TabModel() { + TabModel(SharedPreferences prefs) { + mPrefs = prefs; Arrays.fill(mTabScrolledToTop, true); } @@ -99,7 +103,7 @@ final class TabModel { */ Tab getSelectedTab() { if (mSelectedTab == null) { - mSelectedTab = TabDAO.getSelectedTab(); + mSelectedTab = TabDAO.getSelectedTab(mPrefs); } return mSelectedTab; } @@ -111,7 +115,7 @@ final class TabModel { final Tab oldSelectedTab = getSelectedTab(); if (oldSelectedTab != tab) { mSelectedTab = tab; - TabDAO.setSelectedTab(tab); + TabDAO.setSelectedTab(mPrefs, tab); // Notify of the tab change. for (TabListener tl : mTabListeners) { diff --git a/src/com/android/deskclock/uidata/UiDataModel.java b/src/com/android/deskclock/uidata/UiDataModel.java index b9c8ab114..cdc55366b 100644 --- a/src/com/android/deskclock/uidata/UiDataModel.java +++ b/src/com/android/deskclock/uidata/UiDataModel.java @@ -17,6 +17,7 @@ package com.android.deskclock.uidata; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Typeface; import android.support.annotation.ColorInt; import android.support.annotation.DrawableRes; @@ -85,14 +86,14 @@ public final class UiDataModel { /** * The context may be set precisely once during the application life. */ - public void setContext(Context context) { + public void init(Context context, SharedPreferences prefs) { if (mContext != context) { mContext = context.getApplicationContext(); mPeriodicCallbackModel = new PeriodicCallbackModel(mContext); mFormattedStringModel = new FormattedStringModel(mContext); mColorModel = new ColorModel(mPeriodicCallbackModel); - mTabModel = new TabModel(); + mTabModel = new TabModel(prefs); } } |