1 /* 2 * Copyright (C) 2016 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.phone.vvm.omtp; 17 18 import android.annotation.Nullable; 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.preference.PreferenceManager; 22 import android.telecom.PhoneAccountHandle; 23 24 import com.android.phone.NeededForTesting; 25 26 import java.util.Set; 27 28 /** 29 * Save visual voicemail values in shared preferences to be retrieved later. Because a voicemail 30 * source is tied 1:1 to a phone account, the phone account handle is used in the key for each 31 * voicemail source and the associated data. 32 */ 33 public class VisualVoicemailPreferences { 34 35 private static final String VISUAL_VOICEMAIL_SHARED_PREFS_KEY_PREFIX = 36 "visual_voicemail_"; 37 38 private final SharedPreferences mPreferences; 39 private final PhoneAccountHandle mPhoneAccountHandle; 40 41 public VisualVoicemailPreferences(Context context, PhoneAccountHandle phoneAccountHandle) { 42 mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 43 mPhoneAccountHandle = phoneAccountHandle; 44 } 45 46 public class Editor { 47 48 private final SharedPreferences.Editor mEditor; 49 50 private Editor() { 51 mEditor = mPreferences.edit(); 52 } 53 54 public void apply() { 55 mEditor.apply(); 56 } 57 58 public Editor putBoolean(String key, boolean value) { 59 mEditor.putBoolean(getKey(key), value); 60 return this; 61 } 62 63 @NeededForTesting 64 public Editor putFloat(String key, float value) { 65 mEditor.putFloat(getKey(key), value); 66 return this; 67 } 68 69 public Editor putInt(String key, int value) { 70 mEditor.putInt(getKey(key), value); 71 return this; 72 } 73 74 @NeededForTesting 75 public Editor putLong(String key, long value) { 76 mEditor.putLong(getKey(key), value); 77 return this; 78 } 79 80 public Editor putString(String key, String value) { 81 mEditor.putString(getKey(key), value); 82 return this; 83 } 84 85 @NeededForTesting 86 public Editor putStringSet(String key, Set<String> value) { 87 mEditor.putStringSet(getKey(key), value); 88 return this; 89 } 90 } 91 92 public Editor edit() { 93 return new Editor(); 94 } 95 96 public boolean getBoolean(String key, boolean defValue) { 97 return getValue(key, defValue); 98 } 99 100 @NeededForTesting 101 public float getFloat(String key, float defValue) { 102 return getValue(key, defValue); 103 } 104 105 public int getInt(String key, int defValue) { 106 return getValue(key, defValue); 107 } 108 109 @NeededForTesting 110 public long getLong(String key, long defValue) { 111 return getValue(key, defValue); 112 } 113 114 public String getString(String key, String defValue) { 115 return getValue(key, defValue); 116 } 117 118 @Nullable 119 public String getString(String key) { 120 return getValue(key, null); 121 } 122 123 @NeededForTesting 124 public Set<String> getStringSet(String key, Set<String> defValue) { 125 return getValue(key, defValue); 126 } 127 128 public boolean contains(String key) { 129 return mPreferences.contains(getKey(key)); 130 } 131 132 private <T> T getValue(String key, T defValue) { 133 if (!contains(key)) { 134 return defValue; 135 } 136 Object object = mPreferences.getAll().get(getKey(key)); 137 if (object == null) { 138 return defValue; 139 } 140 return (T) object; 141 } 142 143 private String getKey(String key) { 144 return VISUAL_VOICEMAIL_SHARED_PREFS_KEY_PREFIX + key + "_" + mPhoneAccountHandle.getId(); 145 } 146 } 147