1646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu/* 2646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * Copyright (C) 2014 The Android Open Source Project 3646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * 4646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * Licensed under the Apache License, Version 2.0 (the "License"); 5646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * you may not use this file except in compliance with the License. 6646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * You may obtain a copy of the License at 7646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * 8646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * http://www.apache.org/licenses/LICENSE-2.0 9646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * 10646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * Unless required by applicable law or agreed to in writing, software 11646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * distributed under the License is distributed on an "AS IS" BASIS, 12646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * See the License for the specific language governing permissions and 14646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * limitations under the License. 15646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu */ 16646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 17646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxupackage com.android.internal.telephony.util; 18646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 19646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.app.NotificationChannel; 20646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.content.Context; 21646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.content.SharedPreferences; 22646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.net.Uri; 23646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.preference.PreferenceManager; 24646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.provider.Settings; 25646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.telephony.SubscriptionManager; 26646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.telephony.TelephonyManager; 27646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxuimport android.text.TextUtils; 28646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 29646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxupublic class VoicemailNotificationSettingsUtil { 30646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY_PREFIX = 31646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu "voicemail_notification_ringtone_"; 32646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY_PREFIX = 33646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu "voicemail_notification_vibrate_"; 34646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 35646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu // Old voicemail notification vibration string constants used for migration. 36646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY = 37646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu "button_voicemail_notification_ringtone_key"; 38646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY = 39646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu "button_voicemail_notification_vibrate_key"; 40646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY = 41646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu "button_voicemail_notification_vibrate_when_key"; 42646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String OLD_VOICEMAIL_RINGTONE_SHARED_PREFS_KEY = 43646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu "button_voicemail_notification_ringtone_key"; 44646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String OLD_VOICEMAIL_VIBRATION_ALWAYS = "always"; 45646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static final String OLD_VOICEMAIL_VIBRATION_NEVER = "never"; 46646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 47646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu public static void setVibrationEnabled(Context context, boolean isEnabled) { 48646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 49646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences.Editor editor = prefs.edit(); 50646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.putBoolean(getVoicemailVibrationSharedPrefsKey(), isEnabled); 51646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.commit(); 52646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 53646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 54646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu public static boolean isVibrationEnabled(Context context) { 55646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu final NotificationChannel channel = NotificationChannelController.getChannel( 56646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu NotificationChannelController.CHANNEL_ID_VOICE_MAIL, context); 57646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return (channel != null) ? channel.shouldVibrate() : getVibrationPreference(context); 58646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 59646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 60646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu public static boolean getVibrationPreference(Context context) { 61646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 62646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu migrateVoicemailVibrationSettingsIfNeeded(context, prefs); 63646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return prefs.getBoolean(getVoicemailVibrationSharedPrefsKey(), false /* defValue */); 64646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 65646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 66646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu public static void setRingtoneUri(Context context, Uri ringtoneUri) { 67646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 68646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu String ringtoneUriStr = ringtoneUri != null ? ringtoneUri.toString() : ""; 69646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 70646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences.Editor editor = prefs.edit(); 71646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.putString(getVoicemailRingtoneSharedPrefsKey(), ringtoneUriStr); 72646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.commit(); 73646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 74646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 75646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu public static Uri getRingtoneUri(Context context) { 76646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu final NotificationChannel channel = NotificationChannelController.getChannel( 77646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu NotificationChannelController.CHANNEL_ID_VOICE_MAIL, context); 78646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return (channel != null) ? channel.getSound() : getRingTonePreference(context); 79646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 80646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 81646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu public static Uri getRingTonePreference(Context context) { 82646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 83646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu migrateVoicemailRingtoneSettingsIfNeeded(context, prefs); 84646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu String uriString = prefs.getString( 85646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu getVoicemailRingtoneSharedPrefsKey(), 86646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu Settings.System.DEFAULT_NOTIFICATION_URI.toString()); 87646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null; 88646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 89646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 90646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu /** 91646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * Migrate voicemail settings from {@link #OLD_VIBRATE_WHEN_KEY} or 92646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * {@link #OLD_VOICEMAIL_NOTIFICATION_VIBRATE_KEY}. 93646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * 94646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * TODO: Add helper which migrates settings from old version to new version. 95646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu */ 96646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static void migrateVoicemailVibrationSettingsIfNeeded( 97646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu Context context, SharedPreferences prefs) { 98646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu String key = getVoicemailVibrationSharedPrefsKey(); 99646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu TelephonyManager telephonyManager = TelephonyManager.from(context); 100646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 101646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu // Skip if a preference exists, or if phone is MSIM. 102646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu if (prefs.contains(key) || telephonyManager.getPhoneCount() != 1) { 103646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return; 104646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 105646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 106646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu if (prefs.contains(OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY)) { 107646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu boolean voicemailVibrate = prefs.getBoolean( 108646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY, false /* defValue */); 109646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 110646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences.Editor editor = prefs.edit(); 111646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.putBoolean(key, voicemailVibrate) 112646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu .remove(OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY) 113646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu .commit(); 114646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 115646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 116646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu if (prefs.contains(OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY)) { 117646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu // If vibrateWhen is always, then voicemailVibrate should be true. 118646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu // If it is "only in silent mode", or "never", then voicemailVibrate should be false. 119646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu String vibrateWhen = prefs.getString( 120646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY, OLD_VOICEMAIL_VIBRATION_NEVER); 121646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu boolean voicemailVibrate = vibrateWhen.equals(OLD_VOICEMAIL_VIBRATION_ALWAYS); 122646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 123646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences.Editor editor = prefs.edit(); 124646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.putBoolean(key, voicemailVibrate) 125646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu .remove(OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY) 126646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu .commit(); 127646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 128646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 129646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 130646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu /** 131646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * Migrate voicemail settings from OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY. 132646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * 133646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu * TODO: Add helper which migrates settings from old version to new version. 134646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu */ 135646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static void migrateVoicemailRingtoneSettingsIfNeeded( 136646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu Context context, SharedPreferences prefs) { 137646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu String key = getVoicemailRingtoneSharedPrefsKey(); 138646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu TelephonyManager telephonyManager = TelephonyManager.from(context); 139646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 140646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu // Skip if a preference exists, or if phone is MSIM. 141646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu if (prefs.contains(key) || telephonyManager.getPhoneCount() != 1) { 142646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return; 143646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 144646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 145646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu if (prefs.contains(OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY)) { 146646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu String uriString = prefs.getString( 147646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY, null /* defValue */); 148646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 149646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu SharedPreferences.Editor editor = prefs.edit(); 150646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu editor.putString(key, uriString) 151646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu .remove(OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY) 152646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu .commit(); 153646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 154646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 155646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 156646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static String getVoicemailVibrationSharedPrefsKey() { 157646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY_PREFIX 158646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu + SubscriptionManager.getDefaultSubscriptionId(); 159646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 160646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu 161646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu private static String getVoicemailRingtoneSharedPrefsKey() { 162646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu return VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY_PREFIX 163646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu + SubscriptionManager.getDefaultSubscriptionId(); 164646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu } 165646c42d83c011d5d7cb4fe4ba5892b7aeb4abae1fionaxu} 166