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.tv.common.util; 18 19 import android.content.Context; 20 import android.os.AsyncTask; 21 import android.preference.PreferenceManager; 22 import com.android.tv.common.CommonConstants; 23 24 /** Static utilities for {@link android.content.SharedPreferences} */ 25 public final class SharedPreferencesUtils { 26 // Note that changing the preference name will reset the preference values. 27 public static final String SHARED_PREF_FEATURES = "sharePreferencesFeatures"; 28 public static final String SHARED_PREF_BROWSABLE = "browsable_shared_preference"; 29 public static final String SHARED_PREF_WATCHED_HISTORY = "watched_history_shared_preference"; 30 public static final String SHARED_PREF_DVR_WATCHED_POSITION = 31 "dvr_watched_position_shared_preference"; 32 public static final String SHARED_PREF_AUDIO_CAPABILITIES = 33 CommonConstants.BASE_PACKAGE + ".audio_capabilities"; 34 public static final String SHARED_PREF_RECURRING_RUNNER = "sharedPreferencesRecurringRunner"; 35 public static final String SHARED_PREF_EPG = "epg_preferences"; 36 public static final String SHARED_PREF_SERIES_RECORDINGS = "seriesRecordings"; 37 /** No need to pre-initialize. It's used only on the worker thread. */ 38 public static final String SHARED_PREF_CHANNEL_LOGO_URIS = "channelLogoUris"; 39 /** Stores the UI related settings */ 40 public static final String SHARED_PREF_UI_SETTINGS = "ui_settings"; 41 42 public static final String SHARED_PREF_PREVIEW_PROGRAMS = "previewPrograms"; 43 44 private static boolean sInitializeCalled; 45 46 /** 47 * {@link android.content.SharedPreferences} loads the preference file when {@link 48 * Context#getSharedPreferences(String, int)} is called for the first time. Call {@link 49 * Context#getSharedPreferences(String, int)} as early as possible to avoid the ANR due to the 50 * file loading. 51 */ initialize(final Context context, final Runnable postTask)52 public static synchronized void initialize(final Context context, final Runnable postTask) { 53 if (!sInitializeCalled) { 54 sInitializeCalled = true; 55 new AsyncTask<Void, Void, Void>() { 56 @Override 57 protected Void doInBackground(Void... params) { 58 PreferenceManager.getDefaultSharedPreferences(context); 59 context.getSharedPreferences(SHARED_PREF_FEATURES, Context.MODE_PRIVATE); 60 context.getSharedPreferences(SHARED_PREF_BROWSABLE, Context.MODE_PRIVATE); 61 context.getSharedPreferences(SHARED_PREF_WATCHED_HISTORY, Context.MODE_PRIVATE); 62 context.getSharedPreferences( 63 SHARED_PREF_DVR_WATCHED_POSITION, Context.MODE_PRIVATE); 64 context.getSharedPreferences( 65 SHARED_PREF_AUDIO_CAPABILITIES, Context.MODE_PRIVATE); 66 context.getSharedPreferences( 67 SHARED_PREF_RECURRING_RUNNER, Context.MODE_PRIVATE); 68 context.getSharedPreferences(SHARED_PREF_EPG, Context.MODE_PRIVATE); 69 context.getSharedPreferences( 70 SHARED_PREF_SERIES_RECORDINGS, Context.MODE_PRIVATE); 71 context.getSharedPreferences(SHARED_PREF_UI_SETTINGS, Context.MODE_PRIVATE); 72 return null; 73 } 74 75 @Override 76 protected void onPostExecute(Void result) { 77 postTask.run(); 78 } 79 }.execute(); 80 } 81 } 82 SharedPreferencesUtils()83 private SharedPreferencesUtils() {} 84 } 85