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