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_AUDIO_CAPABILITIES =
     32             "com.android.tv.audio_capabilities";
     33     public static final String SHARED_PREF_RECURRING_RUNNER = "sharedPreferencesRecurringRunner";
     34 
     35     private static boolean sInitializeCalled;
     36 
     37     /**
     38      * {@link android.content.SharedPreferences} loads the preference file when
     39      * {@link Context#getSharedPreferences(String, int)} is called for the first time.
     40      * Call {@link Context#getSharedPreferences(String, int)} as early as possible to avoid the ANR
     41      * due to the file loading.
     42      */
     43     public static synchronized void initialize(final Context context) {
     44         if (!sInitializeCalled) {
     45             sInitializeCalled = true;
     46             new AsyncTask<Void, Void, Void>() {
     47                 @Override
     48                 protected Void doInBackground(Void... params) {
     49                     PreferenceManager.getDefaultSharedPreferences(context);
     50                     context.getSharedPreferences(SHARED_PREF_FEATURES, Context.MODE_PRIVATE);
     51                     context.getSharedPreferences(SHARED_PREF_BROWSABLE, Context.MODE_PRIVATE);
     52                     context.getSharedPreferences(SHARED_PREF_WATCHED_HISTORY, Context.MODE_PRIVATE);
     53                     context.getSharedPreferences(SHARED_PREF_AUDIO_CAPABILITIES,
     54                             Context.MODE_PRIVATE);
     55                     context.getSharedPreferences(SHARED_PREF_RECURRING_RUNNER,
     56                             Context.MODE_PRIVATE);
     57                     return null;
     58                 }
     59             }.execute();
     60         }
     61     }
     62 
     63     private SharedPreferencesUtils() { }
     64 }
     65