• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.voicemail.impl.configui;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.os.Bundle;
22 import android.os.PersistableBundle;
23 import android.preference.EditTextPreference;
24 import android.preference.Preference;
25 import android.preference.Preference.OnPreferenceChangeListener;
26 import android.preference.PreferenceFragment;
27 import android.preference.PreferenceManager;
28 import android.preference.PreferenceScreen;
29 import android.preference.SwitchPreference;
30 import android.support.annotation.Nullable;
31 import android.support.annotation.VisibleForTesting;
32 import android.telecom.PhoneAccount;
33 import android.telecom.PhoneAccountHandle;
34 import android.telecom.TelecomManager;
35 import android.text.TextUtils;
36 import com.android.dialer.common.Assert;
37 import com.android.dialer.common.concurrent.ThreadUtil;
38 import com.android.voicemail.VoicemailComponent;
39 
40 /**
41  * Fragment to edit the override values for the {@link import
42  * com.android.voicemail.impl.OmtpVvmCarrierConfigHelper}
43  */
44 public class ConfigOverrideFragment extends PreferenceFragment
45     implements OnPreferenceChangeListener {
46 
47   /**
48    * Any preference with key that starts with this prefix will be written to the dialer carrier
49    * config.
50    */
51   @VisibleForTesting static final String CONFIG_OVERRIDE_KEY_PREFIX = "vvm_config_override_key_";
52 
53   @Override
onCreate(@ullable Bundle savedInstanceState)54   public void onCreate(@Nullable Bundle savedInstanceState) {
55     super.onCreate(savedInstanceState);
56     PreferenceManager.setDefaultValues(getActivity(), R.xml.vvm_config_override, false);
57     addPreferencesFromResource(R.xml.vvm_config_override);
58 
59     // add listener so the value of a EditTextPreference will be updated to the summary.
60     for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
61       Preference preference = getPreferenceScreen().getPreference(i);
62       preference.setOnPreferenceChangeListener(this);
63       updatePreference(preference);
64     }
65   }
66 
67   @Override
onPreferenceChange(Preference preference, Object newValue)68   public boolean onPreferenceChange(Preference preference, Object newValue) {
69     Assert.isMainThread();
70     ThreadUtil.postOnUiThread(() -> updatePreference(preference));
71     return true;
72   }
73 
updatePreference(Preference preference)74   private void updatePreference(Preference preference) {
75     if (preference instanceof EditTextPreference) {
76       EditTextPreference editTextPreference = (EditTextPreference) preference;
77       editTextPreference.setSummary(editTextPreference.getText());
78     }
79   }
80 
81   @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)82   public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
83     if (TextUtils.equals(
84         preference.getKey(), getString(R.string.vvm_config_override_load_current_key))) {
85       loadCurrentConfig();
86     }
87     return super.onPreferenceTreeClick(preferenceScreen, preference);
88   }
89 
90   /**
91    * Loads the config for the currently carrier into the override values, from the dialer or the
92    * carrier config app. This is a "reset" button to load the defaults.
93    */
loadCurrentConfig()94   private void loadCurrentConfig() {
95     Context context = getActivity();
96     PhoneAccountHandle phoneAccountHandle =
97         context
98             .getSystemService(TelecomManager.class)
99             .getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_VOICEMAIL);
100 
101     PersistableBundle config =
102         VoicemailComponent.get(context).getVoicemailClient().getConfig(context, phoneAccountHandle);
103 
104     for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
105       Preference preference = getPreferenceScreen().getPreference(i);
106       String key = preference.getKey();
107       if (!key.startsWith(CONFIG_OVERRIDE_KEY_PREFIX)) {
108         continue;
109       }
110 
111       String configKey = key.substring(CONFIG_OVERRIDE_KEY_PREFIX.length());
112 
113       if (configKey.endsWith("bool")) {
114         ((SwitchPreference) preference).setChecked(config.getBoolean(configKey));
115       } else if (configKey.endsWith("int")) {
116         ((EditTextPreference) preference).setText(String.valueOf(config.getInt(configKey)));
117       } else if (configKey.endsWith("string")) {
118         ((EditTextPreference) preference).setText(config.getString(configKey));
119       } else if (configKey.endsWith("string_array")) {
120         ((EditTextPreference) preference).setText(toCsv(config.getStringArray(configKey)));
121       } else {
122         throw Assert.createAssertionFailException("unknown type for key " + configKey);
123       }
124       updatePreference(preference);
125     }
126   }
127 
isOverridden(Context context)128   public static boolean isOverridden(Context context) {
129     return PreferenceManager.getDefaultSharedPreferences(context)
130         .getBoolean(context.getString(R.string.vvm_config_override_enabled_key), false);
131   }
132 
getConfig(Context context)133   public static PersistableBundle getConfig(Context context) {
134     Assert.checkState(isOverridden(context));
135     PersistableBundle result = new PersistableBundle();
136 
137     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
138     for (String key : preferences.getAll().keySet()) {
139       if (!key.startsWith(CONFIG_OVERRIDE_KEY_PREFIX)) {
140         continue;
141       }
142       String configKey = key.substring(CONFIG_OVERRIDE_KEY_PREFIX.length());
143       if (configKey.endsWith("bool")) {
144         result.putBoolean(configKey, preferences.getBoolean(key, false));
145       } else if (configKey.endsWith("int")) {
146         result.putInt(configKey, Integer.valueOf(preferences.getString(key, null)));
147       } else if (configKey.endsWith("string")) {
148         result.putString(configKey, preferences.getString(key, null));
149       } else if (configKey.endsWith("string_array")) {
150         result.putStringArray(configKey, fromCsv(preferences.getString(key, null)));
151       } else {
152         throw Assert.createAssertionFailException("unknown type for key " + configKey);
153       }
154     }
155     return result;
156   }
157 
toCsv(String[] array)158   private static String toCsv(String[] array) {
159     if (array == null) {
160       return "";
161     }
162     StringBuilder result = new StringBuilder();
163     for (String element : array) {
164       if (result.length() != 0) {
165         result.append(",");
166       }
167       result.append(element);
168     }
169     return result.toString();
170   };
171 
fromCsv(String csv)172   private static String[] fromCsv(String csv) {
173     return csv.split(",");
174   }
175 }
176