Home | History | Annotate | Download | only in common
      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     /** No need to pre-initialize. It's used only on the worker thread. */
     39     public static final String SHARED_PREF_CHANNEL_LOGO_URIS = "channelLogoUris";
     40     /** Stores the UI related settings */
     41     public static final String SHARED_PREF_UI_SETTINGS = "ui_settings";
     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
     48      * {@link Context#getSharedPreferences(String, int)} is called for the first time.
     49      * Call {@link Context#getSharedPreferences(String, int)} as early as possible to avoid the ANR
     50      * due to the file loading.
     51      */
     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(SHARED_PREF_DVR_WATCHED_POSITION,
     63                             Context.MODE_PRIVATE);
     64                     context.getSharedPreferences(SHARED_PREF_AUDIO_CAPABILITIES,
     65                             Context.MODE_PRIVATE);
     66                     context.getSharedPreferences(SHARED_PREF_RECURRING_RUNNER,
     67                             Context.MODE_PRIVATE);
     68                     context.getSharedPreferences(SHARED_PREF_EPG, Context.MODE_PRIVATE);
     69                     context.getSharedPreferences(SHARED_PREF_SERIES_RECORDINGS,
     70                             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 
     83     private SharedPreferencesUtils() { }
     84 }
     85