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