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.common;
17 
18 import android.content.Context;
19 import android.content.SharedPreferences;
20 import android.support.annotation.Nullable;
21 import android.telecom.PhoneAccountHandle;
22 import java.util.Set;
23 
24 /**
25  * Class that helps us store dialer preferences that are phone account dependent. This is necessary
26  * for cases such as settings that are phone account dependent e.g endless vm. The logic is to
27  * essentially store the shared preference by appending the phone account id to the key.
28  */
29 public class PerAccountSharedPreferences {
30   private final String sharedPrefsKeyPrefix;
31   private final SharedPreferences preferences;
32   private final PhoneAccountHandle phoneAccountHandle;
33 
PerAccountSharedPreferences( Context context, PhoneAccountHandle handle, SharedPreferences prefs)34   public PerAccountSharedPreferences(
35       Context context, PhoneAccountHandle handle, SharedPreferences prefs) {
36     preferences = prefs;
37     phoneAccountHandle = handle;
38     sharedPrefsKeyPrefix = "phone_account_dependent_";
39   }
40 
41   /**
42    * Not to be used, currently only used by {@VisualVoicemailPreferences} for legacy reasons.
43    */
PerAccountSharedPreferences( Context context, PhoneAccountHandle handle, SharedPreferences prefs, String prefix)44   protected PerAccountSharedPreferences(
45       Context context, PhoneAccountHandle handle, SharedPreferences prefs, String prefix) {
46     Assert.checkArgument(prefix.equals("visual_voicemail_"));
47     preferences = prefs;
48     phoneAccountHandle = handle;
49     sharedPrefsKeyPrefix = prefix;
50   }
51 
52   public class Editor {
53 
54     private final SharedPreferences.Editor editor;
55 
Editor()56     private Editor() {
57       editor = preferences.edit();
58     }
59 
apply()60     public void apply() {
61       editor.apply();
62     }
63 
putBoolean(String key, boolean value)64     public Editor putBoolean(String key, boolean value) {
65       editor.putBoolean(getKey(key), value);
66       return this;
67     }
68 
putFloat(String key, float value)69     public Editor putFloat(String key, float value) {
70       editor.putFloat(getKey(key), value);
71       return this;
72     }
73 
putInt(String key, int value)74     public Editor putInt(String key, int value) {
75       editor.putInt(getKey(key), value);
76       return this;
77     }
78 
putLong(String key, long value)79     public Editor putLong(String key, long value) {
80       editor.putLong(getKey(key), value);
81       return this;
82     }
83 
putString(String key, String value)84     public Editor putString(String key, String value) {
85       editor.putString(getKey(key), value);
86       return this;
87     }
88 
putStringSet(String key, Set<String> value)89     public Editor putStringSet(String key, Set<String> value) {
90       editor.putStringSet(getKey(key), value);
91       return this;
92     }
93   }
94 
edit()95   public Editor edit() {
96     return new Editor();
97   }
98 
getBoolean(String key, boolean defValue)99   public boolean getBoolean(String key, boolean defValue) {
100     return getValue(key, defValue);
101   }
102 
getFloat(String key, float defValue)103   public float getFloat(String key, float defValue) {
104     return getValue(key, defValue);
105   }
106 
getInt(String key, int defValue)107   public int getInt(String key, int defValue) {
108     return getValue(key, defValue);
109   }
110 
getLong(String key, long defValue)111   public long getLong(String key, long defValue) {
112     return getValue(key, defValue);
113   }
114 
getString(String key, String defValue)115   public String getString(String key, String defValue) {
116     return getValue(key, defValue);
117   }
118 
119   @Nullable
getString(String key)120   public String getString(String key) {
121     return getValue(key, null);
122   }
123 
getStringSet(String key, Set<String> defValue)124   public Set<String> getStringSet(String key, Set<String> defValue) {
125     return getValue(key, defValue);
126   }
127 
contains(String key)128   public boolean contains(String key) {
129     return preferences.contains(getKey(key));
130   }
131 
getValue(String key, T defValue)132   private <T> T getValue(String key, T defValue) {
133     if (!contains(key)) {
134       return defValue;
135     }
136     Object object = preferences.getAll().get(getKey(key));
137     if (object == null) {
138       return defValue;
139     }
140     return (T) object;
141   }
142 
getKey(String key)143   private String getKey(String key) {
144     return sharedPrefsKeyPrefix + key + "_" + phoneAccountHandle.getId();
145   }
146 }
147