• 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                 .setPackageName(packageName)
96                 .build();
97     }
98 
getSharedPreferences(Context context)99     private static SharedPreferences getSharedPreferences(Context context) {
100         return PreferenceManager
101                 .getDefaultSharedPreferences(context.createDeviceProtectedStorageContext());
102     }
103 
makePerPhoneAccountKeyPrefix(String packageName, int subId)104     private static String makePerPhoneAccountKeyPrefix(String packageName, int subId) {
105         // subId is persistent across reboot and upgrade, but not across devices.
106         // ICC id is better as a key but it involves more trouble to get one as subId is more
107         // commonly passed around.
108         return VVM_SMS_FILTER_COFIG_SHARED_PREFS_KEY_PREFIX + packageName + "_"
109                 + subId;
110     }
111 
112     private static class Editor {
113 
114         private final SharedPreferences.Editor mPrefsEditor;
115         private final String mKeyPrefix;
116 
Editor(Context context, String packageName, int subId)117         public Editor(Context context, String packageName, int subId) {
118             mPrefsEditor = getSharedPreferences(context).edit();
119             mKeyPrefix = makePerPhoneAccountKeyPrefix(packageName, subId);
120         }
121 
setInt(String key, int value)122         private Editor setInt(String key, int value) {
123             mPrefsEditor.putInt(makeKey(key), value);
124             return this;
125         }
126 
setString(String key, String value)127         private Editor setString(String key, String value) {
128             mPrefsEditor.putString(makeKey(key), value);
129             return this;
130         }
131 
setBoolean(String key, boolean value)132         private Editor setBoolean(String key, boolean value) {
133             mPrefsEditor.putBoolean(makeKey(key), value);
134             return this;
135         }
136 
setStringList(String key, List<String> value)137         private Editor setStringList(String key, List<String> value) {
138             mPrefsEditor.putStringSet(makeKey(key), new ArraySet(value));
139             return this;
140         }
141 
apply()142         public void apply() {
143             mPrefsEditor.apply();
144         }
145 
makeKey(String key)146         private String makeKey(String key) {
147             return mKeyPrefix + key;
148         }
149     }
150 
151 
152     private static class Reader {
153 
154         private final SharedPreferences mPrefs;
155         private final String mKeyPrefix;
156 
Reader(Context context, String packageName, int subId)157         public Reader(Context context, String packageName, int subId) {
158             mPrefs = getSharedPreferences(context);
159             mKeyPrefix = makePerPhoneAccountKeyPrefix(packageName, subId);
160         }
161 
getInt(String key, int defaultValue)162         private int getInt(String key, int defaultValue) {
163             return mPrefs.getInt(makeKey(key), defaultValue);
164         }
165 
getString(String key, String defaultValue)166         private String getString(String key, String defaultValue) {
167             return mPrefs.getString(makeKey(key), defaultValue);
168         }
169 
getBoolean(String key, boolean defaultValue)170         private boolean getBoolean(String key, boolean defaultValue) {
171             return mPrefs.getBoolean(makeKey(key), defaultValue);
172         }
173 
getStringSet(String key, List<String> defaultValue)174         private List<String> getStringSet(String key, List<String> defaultValue) {
175             Set<String> result = mPrefs.getStringSet(makeKey(key), null);
176             if (result == null) {
177                 return defaultValue;
178             }
179             return new ArrayList<>(result);
180         }
181 
makeKey(String key)182         private String makeKey(String key) {
183             return mKeyPrefix + key;
184         }
185     }
186 }
187