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