1 /*
2  * Copyright (C) 2014 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 
17 package com.android.phone.settings;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.net.Uri;
22 import android.preference.PreferenceManager;
23 import android.provider.Settings;
24 import android.telephony.TelephonyManager;
25 import android.text.TextUtils;
26 
27 import com.android.internal.telephony.Phone;
28 import com.android.phone.R;
29 
30 public class VoicemailNotificationSettingsUtil {
31     private static final String VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY_PREFIX =
32             "voicemail_notification_ringtone_";
33     private static final String VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY_PREFIX =
34             "voicemail_notification_vibrate_";
35 
36     // Old voicemail notification vibration string constants used for migration.
37     private static final String OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY =
38             "button_voicemail_notification_ringtone_key";
39     private static final String OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY =
40             "button_voicemail_notification_vibrate_key";
41     private static final String OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY =
42             "button_voicemail_notification_vibrate_when_key";
43     private static final String OLD_VOICEMAIL_RINGTONE_SHARED_PREFS_KEY =
44             "button_voicemail_notification_ringtone_key";
45     private static final String OLD_VOICEMAIL_VIBRATION_ALWAYS = "always";
46     private static final String OLD_VOICEMAIL_VIBRATION_NEVER = "never";
47 
setVibrationEnabled(Phone phone, boolean isEnabled)48     public static void setVibrationEnabled(Phone phone, boolean isEnabled) {
49         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(phone.getContext());
50         SharedPreferences.Editor editor = prefs.edit();
51         editor.putBoolean(getVoicemailVibrationSharedPrefsKey(phone), isEnabled);
52         editor.commit();
53     }
54 
isVibrationEnabled(Phone phone)55     public static boolean isVibrationEnabled(Phone phone) {
56         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(phone.getContext());
57         migrateVoicemailVibrationSettingsIfNeeded(phone, prefs);
58         return prefs.getBoolean(getVoicemailVibrationSharedPrefsKey(phone), false /* defValue */);
59     }
60 
setRingtoneUri(Phone phone, Uri ringtoneUri)61    public static void setRingtoneUri(Phone phone, Uri ringtoneUri) {
62         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(phone.getContext());
63         String ringtoneUriStr = ringtoneUri != null ? ringtoneUri.toString() : "";
64 
65         SharedPreferences.Editor editor = prefs.edit();
66         editor.putString(getVoicemailRingtoneSharedPrefsKey(phone), ringtoneUriStr);
67         editor.commit();
68     }
69 
getRingtoneUri(Phone phone)70     public static Uri getRingtoneUri(Phone phone) {
71         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(phone.getContext());
72         migrateVoicemailRingtoneSettingsIfNeeded(phone, prefs);
73         String uriString = prefs.getString(
74                 getVoicemailRingtoneSharedPrefsKey(phone),
75                 Settings.System.DEFAULT_NOTIFICATION_URI.toString());
76         return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null;
77     }
78 
79     /**
80      * Migrate voicemail settings from {@link #OLD_VIBRATE_WHEN_KEY} or
81      * {@link #OLD_VOICEMAIL_NOTIFICATION_VIBRATE_KEY}.
82      *
83      * TODO: Add helper which migrates settings from old version to new version.
84      */
migrateVoicemailVibrationSettingsIfNeeded( Phone phone, SharedPreferences prefs)85     private static void migrateVoicemailVibrationSettingsIfNeeded(
86             Phone phone, SharedPreferences prefs) {
87         String key = getVoicemailVibrationSharedPrefsKey(phone);
88         TelephonyManager telephonyManager = TelephonyManager.from(phone.getContext());
89 
90         // Skip if a preference exists, or if phone is MSIM.
91         if (prefs.contains(key) || telephonyManager.getPhoneCount() != 1) {
92             return;
93         }
94 
95         if (prefs.contains(OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY)) {
96             boolean voicemailVibrate = prefs.getBoolean(
97                     OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY, false /* defValue */);
98 
99             SharedPreferences.Editor editor = prefs.edit();
100             editor.putBoolean(key, voicemailVibrate)
101                     .remove(OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY)
102                     .commit();
103         }
104 
105         if (prefs.contains(OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY)) {
106             // If vibrateWhen is always, then voicemailVibrate should be true.
107             // If it is "only in silent mode", or "never", then voicemailVibrate should be false.
108             String vibrateWhen = prefs.getString(
109                     OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY, OLD_VOICEMAIL_VIBRATION_NEVER);
110             boolean voicemailVibrate = vibrateWhen.equals(OLD_VOICEMAIL_VIBRATION_ALWAYS);
111 
112             SharedPreferences.Editor editor = prefs.edit();
113             editor.putBoolean(key, voicemailVibrate)
114                     .remove(OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY)
115                     .commit();
116         }
117     }
118 
119     /**
120      * Migrate voicemail settings from OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY.
121      *
122      * TODO: Add helper which migrates settings from old version to new version.
123      */
migrateVoicemailRingtoneSettingsIfNeeded( Phone phone, SharedPreferences prefs)124     private static void migrateVoicemailRingtoneSettingsIfNeeded(
125             Phone phone, SharedPreferences prefs) {
126         String key = getVoicemailRingtoneSharedPrefsKey(phone);
127         TelephonyManager telephonyManager = TelephonyManager.from(phone.getContext());
128 
129         // Skip if a preference exists, or if phone is MSIM.
130         if (prefs.contains(key) || telephonyManager.getPhoneCount() != 1) {
131             return;
132         }
133 
134         if (prefs.contains(OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY)) {
135             String uriString = prefs.getString(
136                     OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY, null /* defValue */);
137 
138             SharedPreferences.Editor editor = prefs.edit();
139             editor.putString(key, uriString)
140                     .remove(OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY)
141                     .commit();
142         }
143     }
144 
getVoicemailVibrationSharedPrefsKey(Phone phone)145     private static String getVoicemailVibrationSharedPrefsKey(Phone phone) {
146         return VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY_PREFIX + phone.getSubId();
147     }
148 
getVoicemailRingtoneSharedPrefsKey(Phone phone)149     public static String getVoicemailRingtoneSharedPrefsKey(Phone phone) {
150         return VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY_PREFIX + phone.getSubId();
151     }
152 }
153