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