Home | History | Annotate | Download | only in configprovider
      1 /*
      2  * Copyright (C) 2017 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 package com.android.dialer.configprovider;
     17 
     18 import android.app.IntentService;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.SharedPreferences;
     22 import android.content.SharedPreferences.Editor;
     23 import android.support.annotation.Nullable;
     24 import com.android.dialer.common.Assert;
     25 import com.android.dialer.common.LogUtil;
     26 import com.android.dialer.inject.ApplicationContext;
     27 import com.android.dialer.storage.StorageComponent;
     28 import com.android.dialer.strictmode.StrictModeUtils;
     29 import javax.inject.Inject;
     30 
     31 /**
     32  * {@link ConfigProvider} which uses a shared preferences file.
     33  *
     34  * <p>Config flags can be written using adb (with root access), for example:
     35  *
     36  * <pre>
     37  *   adb root
     38  *   adb shell am startservice -n \
     39  *     'com.android.dialer/.configprovider.SharedPrefConfigProvider\$Service' \
     40  *     --ez boolean_flag_name flag_value
     41  * </pre>
     42  *
     43  * <p>(For longs use --el and for strings use --es.)
     44  *
     45  * <p>Flags can be viewed with:
     46  *
     47  * <pre>
     48  *   adb shell cat \
     49  *     /data/user_de/0/com.android.dialer/shared_prefs/com.android.dialer_preferences.xml
     50  * </pre>
     51  */
     52 class SharedPrefConfigProvider implements ConfigProvider {
     53   private static final String PREF_PREFIX = "config_provider_prefs_";
     54 
     55   private final Context appContext;
     56 
     57   @Inject
     58   SharedPrefConfigProvider(@ApplicationContext Context appContext) {
     59     this.appContext = appContext;
     60   }
     61 
     62   /** Service to write values into {@link SharedPrefConfigProvider} using adb. */
     63   public static class Service extends IntentService {
     64 
     65     public Service() {
     66       super("SharedPrefConfigProvider.Service");
     67     }
     68 
     69     @Override
     70     protected void onHandleIntent(@Nullable Intent intent) {
     71       if (intent == null || intent.getExtras() == null || intent.getExtras().size() != 1) {
     72         LogUtil.w("SharedPrefConfigProvider.Service.onHandleIntent", "must set exactly one extra");
     73         return;
     74       }
     75       String key = intent.getExtras().keySet().iterator().next();
     76       Object value = intent.getExtras().get(key);
     77       put(key, value);
     78     }
     79 
     80     private void put(String key, Object value) {
     81       Editor editor = getSharedPrefs(getApplicationContext()).edit();
     82       String prefixedKey = PREF_PREFIX + key;
     83       if (value instanceof Boolean) {
     84         editor.putBoolean(prefixedKey, (Boolean) value);
     85       } else if (value instanceof Long) {
     86         editor.putLong(prefixedKey, (Long) value);
     87       } else if (value instanceof String) {
     88         editor.putString(prefixedKey, (String) value);
     89       } else {
     90         throw Assert.createAssertionFailException("unsupported extra type: " + value.getClass());
     91       }
     92       editor.apply();
     93     }
     94   }
     95 
     96   @Override
     97   public String getString(String key, String defaultValue) {
     98     // Reading shared prefs on the main thread is generally safe since a single instance is cached.
     99     return StrictModeUtils.bypass(
    100         () -> getSharedPrefs(appContext).getString(PREF_PREFIX + key, defaultValue));
    101   }
    102 
    103   @Override
    104   public long getLong(String key, long defaultValue) {
    105     // Reading shared prefs on the main thread is generally safe since a single instance is cached.
    106     return StrictModeUtils.bypass(
    107         () -> getSharedPrefs(appContext).getLong(PREF_PREFIX + key, defaultValue));
    108   }
    109 
    110   @Override
    111   public boolean getBoolean(String key, boolean defaultValue) {
    112     // Reading shared prefs on the main thread is generally safe since a single instance is cached.
    113     return StrictModeUtils.bypass(
    114         () -> getSharedPrefs(appContext).getBoolean(PREF_PREFIX + key, defaultValue));
    115   }
    116 
    117   private static SharedPreferences getSharedPrefs(Context appContext) {
    118     return StorageComponent.get(appContext).unencryptedSharedPrefs();
    119   }
    120 }
    121