• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
17 
18 import android.annotation.Nullable;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.preference.PreferenceManager;
23 import android.telephony.VisualVoicemailSmsFilterSettings;
24 import android.util.ArraySet;
25 
26 import com.android.phone.vvm.RemoteVvmTaskManager;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Set;
31 
32 /**
33  * Stores the config values needed for visual voicemail sms filtering. The values from
34  * OmtpVvmCarrierConfigHelper are stored here during activation instead. These values are read and
35  * written through TelephonyManager.
36  */
37 public class VisualVoicemailSmsFilterConfig {
38 
39     private static final String VVM_SMS_FILTER_COFIG_SHARED_PREFS_KEY_PREFIX =
40             "vvm_sms_filter_config_";
41     private static final String ENABLED_KEY = "_enabled";
42     private static final String PREFIX_KEY = "_prefix";
43     private static final String ORIGINATING_NUMBERS_KEY = "_originating_numbers";
44     private static final String DESTINATION_PORT_KEY = "_destination_port";
45     private static final String DEFAULT_PACKAGE = "com.android.phone";
46 
enableVisualVoicemailSmsFilter(Context context, String callingPackage, int subId, VisualVoicemailSmsFilterSettings settings)47     public static void enableVisualVoicemailSmsFilter(Context context, String callingPackage,
48             int subId,
49             VisualVoicemailSmsFilterSettings settings) {
50         new Editor(context, callingPackage, subId)
51                 .setBoolean(ENABLED_KEY, true)
52                 .setString(PREFIX_KEY, settings.clientPrefix)
53                 .setStringList(ORIGINATING_NUMBERS_KEY, settings.originatingNumbers)
54                 .setInt(DESTINATION_PORT_KEY, settings.destinationPort)
55                 .apply();
56     }
57 
disableVisualVoicemailSmsFilter(Context context, String callingPackage, int subId)58     public static void disableVisualVoicemailSmsFilter(Context context, String callingPackage,
59             int subId) {
60         new Editor(context, callingPackage, subId)
61                 .setBoolean(ENABLED_KEY, false)
62                 .apply();
63     }
64 
getActiveVisualVoicemailSmsFilterSettings( Context context, int subId)65     public static VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(
66             Context context, int subId) {
67         ComponentName componentName = RemoteVvmTaskManager.getRemotePackage(context, subId);
68         String packageName;
69         if (componentName == null) {
70             packageName = DEFAULT_PACKAGE;
71         } else {
72             packageName = componentName.getPackageName();
73         }
74         return getVisualVoicemailSmsFilterSettings(
75                 context,
76                 packageName,
77                 subId);
78     }
79 
80     @Nullable
getVisualVoicemailSmsFilterSettings( Context context, String packageName, int subId)81     public static VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
82             Context context,
83             String packageName, int subId) {
84         Reader reader = new Reader(context, packageName, subId);
85         if (!reader.getBoolean(ENABLED_KEY, false)) {
86             return null;
87         }
88         return new VisualVoicemailSmsFilterSettings.Builder()
89                 .setClientPrefix(reader.getString(PREFIX_KEY,
90                         VisualVoicemailSmsFilterSettings.DEFAULT_CLIENT_PREFIX))
91                 .setOriginatingNumbers(reader.getStringSet(ORIGINATING_NUMBERS_KEY,
92                         VisualVoicemailSmsFilterSettings.DEFAULT_ORIGINATING_NUMBERS))
93                 .setDestinationPort(reader.getInt(DESTINATION_PORT_KEY,
94                         VisualVoicemailSmsFilterSettings.DEFAULT_DESTINATION_PORT))
95                 .build();
96     }
97 
getSharedPreferences(Context context)98     private static SharedPreferences getSharedPreferences(Context context) {
99         return PreferenceManager
100                 .getDefaultSharedPreferences(context.createDeviceProtectedStorageContext());
101     }
102 
makePerPhoneAccountKeyPrefix(String packageName, int subId)103     private static String makePerPhoneAccountKeyPrefix(String packageName, int subId) {
104         // subId is persistent across reboot and upgrade, but not across devices.
105         // ICC id is better as a key but it involves more trouble to get one as subId is more
106         // commonly passed around.
107         return VVM_SMS_FILTER_COFIG_SHARED_PREFS_KEY_PREFIX + packageName + "_"
108                 + subId;
109     }
110 
111     private static class Editor {
112 
113         private final SharedPreferences.Editor mPrefsEditor;
114         private final String mKeyPrefix;
115 
Editor(Context context, String packageName, int subId)116         public Editor(Context context, String packageName, int subId) {
117             mPrefsEditor = getSharedPreferences(context).edit();
118             mKeyPrefix = makePerPhoneAccountKeyPrefix(packageName, subId);
119         }
120 
setInt(String key, int value)121         private Editor setInt(String key, int value) {
122             mPrefsEditor.putInt(makeKey(key), value);
123             return this;
124         }
125 
setString(String key, String value)126         private Editor setString(String key, String value) {
127             mPrefsEditor.putString(makeKey(key), value);
128             return this;
129         }
130 
setBoolean(String key, boolean value)131         private Editor setBoolean(String key, boolean value) {
132             mPrefsEditor.putBoolean(makeKey(key), value);
133             return this;
134         }
135 
setStringList(String key, List<String> value)136         private Editor setStringList(String key, List<String> value) {
137             mPrefsEditor.putStringSet(makeKey(key), new ArraySet(value));
138             return this;
139         }
140 
apply()141         public void apply() {
142             mPrefsEditor.apply();
143         }
144 
makeKey(String key)145         private String makeKey(String key) {
146             return mKeyPrefix + key;
147         }
148     }
149 
150 
151     private static class Reader {
152 
153         private final SharedPreferences mPrefs;
154         private final String mKeyPrefix;
155 
Reader(Context context, String packageName, int subId)156         public Reader(Context context, String packageName, int subId) {
157             mPrefs = getSharedPreferences(context);
158             mKeyPrefix = makePerPhoneAccountKeyPrefix(packageName, subId);
159         }
160 
getInt(String key, int defaultValue)161         private int getInt(String key, int defaultValue) {
162             return mPrefs.getInt(makeKey(key), defaultValue);
163         }
164 
getString(String key, String defaultValue)165         private String getString(String key, String defaultValue) {
166             return mPrefs.getString(makeKey(key), defaultValue);
167         }
168 
getBoolean(String key, boolean defaultValue)169         private boolean getBoolean(String key, boolean defaultValue) {
170             return mPrefs.getBoolean(makeKey(key), defaultValue);
171         }
172 
getStringSet(String key, List<String> defaultValue)173         private List<String> getStringSet(String key, List<String> defaultValue) {
174             Set<String> result = mPrefs.getStringSet(makeKey(key), null);
175             if (result == null) {
176                 return defaultValue;
177             }
178             return new ArrayList<>(result);
179         }
180 
makeKey(String key)181         private String makeKey(String key) {
182             return mKeyPrefix + key;
183         }
184     }
185 }
186