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