1 /** 2 * Copyright (C) 2014 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.phone.settings; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.telephony.PhoneNumberUtils; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import com.android.internal.telephony.CallForwardInfo; 26 import com.android.internal.telephony.CommandsInterface; 27 import com.android.phone.PhoneGlobals; 28 29 public class VoicemailProviderSettingsUtil { 30 private static final String LOG_TAG = VoicemailProviderSettingsUtil.class.getSimpleName(); 31 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); 32 33 private static final String VM_NUMBERS_SHARED_PREFERENCES_NAME = "vm_numbers"; 34 35 // Suffix appended to provider key for storing vm number 36 private static final String VM_NUMBER_TAG = "#VMNumber"; 37 // Suffix appended to forward settings key for storing an individual setting 38 private static final String FWD_SETTING_TAG = "#Setting"; 39 // Suffix appended to provider key for storing forwarding settings 40 private static final String FWD_SETTINGS_TAG = "#FWDSettings"; 41 // Suffix appended to forward settings key for storing length of settings array 42 private static final String FWD_SETTINGS_LENGTH_TAG = "#Length"; 43 44 // Suffixes appended to forward setting key for storing an individual setting properties 45 private static final String FWD_SETTING_STATUS = "#Status"; 46 private static final String FWD_SETTING_REASON = "#Reason"; 47 private static final String FWD_SETTING_NUMBER = "#Number"; 48 private static final String FWD_SETTING_TIME = "#Time"; 49 50 /** 51 * Returns settings previously stored for the currently selected voice mail provider. If no 52 * setting is stored for the voice mail provider, return null. 53 */ load(Context context, String key)54 public static VoicemailProviderSettings load(Context context, String key) { 55 SharedPreferences prefs = getPrefs(context); 56 57 String vmNumberSetting = prefs.getString(key + VM_NUMBER_TAG, null); 58 if (vmNumberSetting == null) { 59 Log.w(LOG_TAG, "VoiceMailProvider settings for the key \"" + key + "\"" 60 + " were not found. Returning null."); 61 return null; 62 } 63 64 CallForwardInfo[] cfi = VoicemailProviderSettings.NO_FORWARDING; 65 String fwdKey = key + FWD_SETTINGS_TAG; 66 int fwdLen = prefs.getInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, 0); 67 if (fwdLen > 0) { 68 cfi = new CallForwardInfo[fwdLen]; 69 for (int i = 0; i < cfi.length; i++) { 70 String settingKey = fwdKey + FWD_SETTING_TAG + String.valueOf(i); 71 cfi[i] = new CallForwardInfo(); 72 cfi[i].status = prefs.getInt(settingKey + FWD_SETTING_STATUS, 0); 73 cfi[i].reason = prefs.getInt( 74 settingKey + FWD_SETTING_REASON, 75 CommandsInterface.CF_REASON_ALL_CONDITIONAL); 76 cfi[i].serviceClass = CommandsInterface.SERVICE_CLASS_VOICE; 77 cfi[i].toa = PhoneNumberUtils.TOA_International; 78 cfi[i].number = prefs.getString(settingKey + FWD_SETTING_NUMBER, ""); 79 cfi[i].timeSeconds = prefs.getInt(settingKey + FWD_SETTING_TIME, 20); 80 } 81 } 82 83 VoicemailProviderSettings settings = new VoicemailProviderSettings(vmNumberSetting, cfi); 84 if (DBG) log("Loaded settings for " + key + ": " + settings.toString()); 85 return settings; 86 } 87 88 /** 89 * Saves new VM provider settings and associates them with the currently selected provider if 90 * the settings are different than the ones already stored for this provider. 91 * 92 * These will be used later when the user switches a provider. 93 */ save(Context context, String key, VoicemailProviderSettings newSettings)94 public static void save(Context context, String key, VoicemailProviderSettings newSettings) { 95 VoicemailProviderSettings curSettings = load(context, key); 96 if (newSettings.equals(curSettings)) { 97 if (DBG) log("save: Not saving setting for " + key + " since they have not changed"); 98 return; 99 } 100 101 if (DBG) log("Saving settings for " + key + ": " + newSettings.toString()); 102 103 SharedPreferences prefs = getPrefs(context); 104 SharedPreferences.Editor editor = prefs.edit(); 105 editor.putString(key + VM_NUMBER_TAG, newSettings.getVoicemailNumber()); 106 String fwdKey = key + FWD_SETTINGS_TAG; 107 108 CallForwardInfo[] s = newSettings.getForwardingSettings(); 109 if (s != VoicemailProviderSettings.NO_FORWARDING) { 110 editor.putInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, s.length); 111 for (int i = 0; i < s.length; i++) { 112 String settingKey = fwdKey + FWD_SETTING_TAG + String.valueOf(i); 113 CallForwardInfo fi = s[i]; 114 editor.putInt(settingKey + FWD_SETTING_STATUS, fi.status); 115 editor.putInt(settingKey + FWD_SETTING_REASON, fi.reason); 116 editor.putString(settingKey + FWD_SETTING_NUMBER, fi.number); 117 editor.putInt(settingKey + FWD_SETTING_TIME, fi.timeSeconds); 118 } 119 } else { 120 editor.putInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, 0); 121 } 122 123 editor.apply(); 124 } 125 126 /** 127 * Deletes settings for the provider identified by this key. 128 */ delete(Context context, String key)129 public static void delete(Context context, String key) { 130 if (DBG) log("Deleting settings for" + key); 131 132 if (TextUtils.isEmpty(key)) { 133 return; 134 } 135 136 SharedPreferences prefs = getPrefs(context); 137 prefs.edit() 138 .putString(key + VM_NUMBER_TAG, null) 139 .putInt(key + FWD_SETTINGS_TAG + FWD_SETTINGS_LENGTH_TAG, 0) 140 .commit(); 141 } 142 getPrefs(Context context)143 private static SharedPreferences getPrefs(Context context) { 144 return context.getSharedPreferences( 145 VM_NUMBERS_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); 146 } 147 log(String msg)148 private static void log(String msg) { 149 Log.d(LOG_TAG, msg); 150 } 151 } 152