Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2355eb3b authored by Chen Xu's avatar Chen Xu Committed by Android (Google) Code Review
Browse files

Merge "migration voicemail settings to notificationChannel" into oc-dev

parents 9f4db346 646c42d8
Loading
Loading
Loading
Loading
+45 −13
Original line number Diff line number Diff line
@@ -22,7 +22,9 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.net.Uri;
import android.provider.Settings;
import android.telephony.SubscriptionManager;

import com.android.internal.R;

@@ -54,13 +56,6 @@ public class NotificationChannelController {
        alertChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());

        final NotificationChannel voiceMailChannel = new NotificationChannel(
                CHANNEL_ID_VOICE_MAIL,
                context.getText(R.string.notification_channel_voice_mail),
                NotificationManager.IMPORTANCE_DEFAULT);
        voiceMailChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());

        context.getSystemService(NotificationManager.class)
                .createNotificationChannels(Arrays.asList(
                new NotificationChannel(CHANNEL_ID_CALL_FORWARD,
@@ -75,12 +70,18 @@ public class NotificationChannelController {
                new NotificationChannel(CHANNEL_ID_WFC,
                        context.getText(R.string.notification_channel_wfc),
                        NotificationManager.IMPORTANCE_LOW),
                alertChannel, voiceMailChannel));
                alertChannel));
        // only for update
        if (getChannel(CHANNEL_ID_VOICE_MAIL, context) != null) {
            migrateVoicemailNotificationSettings(context);
        }
    }

    public NotificationChannelController(Context context) {
        context.registerReceiver(mLocaleChangeReceiver,
                new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
        intentFilter.addAction(Intent.ACTION_SIM_STATE_CHANGED);
        context.registerReceiver(mBroadcastReceiver, intentFilter);
        createAll(context);
    }

@@ -89,11 +90,42 @@ public class NotificationChannelController {
                .getNotificationChannel(channelId);
    }

    // rename all registered channels on locale change
    private BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() {
    /**
     * migrate deprecated voicemail notification settings to initial notification channel settings
     * {@link VoicemailNotificationSettingsUtil#getRingTonePreference(Context)}}
     * {@link VoicemailNotificationSettingsUtil#getVibrationPreference(Context)}
     * notification settings are based on subId, only migrate if sub id matches.
     * otherwise fallback to predefined voicemail channel settings.
     * @param context
     */
    private static void migrateVoicemailNotificationSettings(Context context) {
        final NotificationChannel voiceMailChannel = new NotificationChannel(
                CHANNEL_ID_VOICE_MAIL,
                context.getText(R.string.notification_channel_voice_mail),
                NotificationManager.IMPORTANCE_DEFAULT);
        voiceMailChannel.enableVibration(
                VoicemailNotificationSettingsUtil.getVibrationPreference(context));
        Uri sound = VoicemailNotificationSettingsUtil.getRingTonePreference(context);
        voiceMailChannel.setSound(
                (sound == null) ? Settings.System.DEFAULT_NOTIFICATION_URI : sound,
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
        context.getSystemService(NotificationManager.class)
                .createNotificationChannel(voiceMailChannel);
    }

    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) {
                // rename all notification channels on locale change
                createAll(context);
            } else if (Intent.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
                // migrate voicemail notification settings on sim load
                if (SubscriptionManager.INVALID_SUBSCRIPTION_ID !=
                        SubscriptionManager.getDefaultSubscriptionId()) {
                    migrateVoicemailNotificationSettings(context);
                }
            }
        }
    };
}
+165 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony.util;

import android.app.NotificationChannel;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class VoicemailNotificationSettingsUtil {
    private static final String VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY_PREFIX =
            "voicemail_notification_ringtone_";
    private static final String VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY_PREFIX =
            "voicemail_notification_vibrate_";

    // Old voicemail notification vibration string constants used for migration.
    private static final String OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY =
            "button_voicemail_notification_ringtone_key";
    private static final String OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY =
            "button_voicemail_notification_vibrate_key";
    private static final String OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY =
            "button_voicemail_notification_vibrate_when_key";
    private static final String OLD_VOICEMAIL_RINGTONE_SHARED_PREFS_KEY =
            "button_voicemail_notification_ringtone_key";
    private static final String OLD_VOICEMAIL_VIBRATION_ALWAYS = "always";
    private static final String OLD_VOICEMAIL_VIBRATION_NEVER = "never";

    public static void setVibrationEnabled(Context context, boolean isEnabled) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(getVoicemailVibrationSharedPrefsKey(), isEnabled);
        editor.commit();
    }

    public static boolean isVibrationEnabled(Context context) {
        final NotificationChannel channel = NotificationChannelController.getChannel(
                NotificationChannelController.CHANNEL_ID_VOICE_MAIL, context);
        return (channel != null) ? channel.shouldVibrate() : getVibrationPreference(context);
    }

    public static boolean getVibrationPreference(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        migrateVoicemailVibrationSettingsIfNeeded(context, prefs);
        return prefs.getBoolean(getVoicemailVibrationSharedPrefsKey(), false /* defValue */);
    }

   public static void setRingtoneUri(Context context, Uri ringtoneUri) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String ringtoneUriStr = ringtoneUri != null ? ringtoneUri.toString() : "";

        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(getVoicemailRingtoneSharedPrefsKey(), ringtoneUriStr);
        editor.commit();
    }

    public static Uri getRingtoneUri(Context context) {
        final NotificationChannel channel = NotificationChannelController.getChannel(
                NotificationChannelController.CHANNEL_ID_VOICE_MAIL, context);
        return (channel != null) ? channel.getSound() : getRingTonePreference(context);
    }

    public static Uri getRingTonePreference(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        migrateVoicemailRingtoneSettingsIfNeeded(context, prefs);
        String uriString = prefs.getString(
                getVoicemailRingtoneSharedPrefsKey(),
                Settings.System.DEFAULT_NOTIFICATION_URI.toString());
        return !TextUtils.isEmpty(uriString) ? Uri.parse(uriString) : null;
    }

    /**
     * Migrate voicemail settings from {@link #OLD_VIBRATE_WHEN_KEY} or
     * {@link #OLD_VOICEMAIL_NOTIFICATION_VIBRATE_KEY}.
     *
     * TODO: Add helper which migrates settings from old version to new version.
     */
    private static void migrateVoicemailVibrationSettingsIfNeeded(
            Context context, SharedPreferences prefs) {
        String key = getVoicemailVibrationSharedPrefsKey();
        TelephonyManager telephonyManager = TelephonyManager.from(context);

        // Skip if a preference exists, or if phone is MSIM.
        if (prefs.contains(key) || telephonyManager.getPhoneCount() != 1) {
            return;
        }

        if (prefs.contains(OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY)) {
            boolean voicemailVibrate = prefs.getBoolean(
                    OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY, false /* defValue */);

            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean(key, voicemailVibrate)
                    .remove(OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY)
                    .commit();
        }

        if (prefs.contains(OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY)) {
            // If vibrateWhen is always, then voicemailVibrate should be true.
            // If it is "only in silent mode", or "never", then voicemailVibrate should be false.
            String vibrateWhen = prefs.getString(
                    OLD_VOICEMAIL_VIBRATE_WHEN_SHARED_PREFS_KEY, OLD_VOICEMAIL_VIBRATION_NEVER);
            boolean voicemailVibrate = vibrateWhen.equals(OLD_VOICEMAIL_VIBRATION_ALWAYS);

            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean(key, voicemailVibrate)
                    .remove(OLD_VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY)
                    .commit();
        }
    }

    /**
     * Migrate voicemail settings from OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY.
     *
     * TODO: Add helper which migrates settings from old version to new version.
     */
    private static void migrateVoicemailRingtoneSettingsIfNeeded(
            Context context, SharedPreferences prefs) {
        String key = getVoicemailRingtoneSharedPrefsKey();
        TelephonyManager telephonyManager = TelephonyManager.from(context);

        // Skip if a preference exists, or if phone is MSIM.
        if (prefs.contains(key) || telephonyManager.getPhoneCount() != 1) {
            return;
        }

        if (prefs.contains(OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY)) {
            String uriString = prefs.getString(
                    OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY, null /* defValue */);

            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(key, uriString)
                    .remove(OLD_VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY)
                    .commit();
        }
    }

    private static String getVoicemailVibrationSharedPrefsKey() {
        return VOICEMAIL_NOTIFICATION_VIBRATION_SHARED_PREFS_KEY_PREFIX
                + SubscriptionManager.getDefaultSubscriptionId();
    }

    private static String getVoicemailRingtoneSharedPrefsKey() {
        return VOICEMAIL_NOTIFICATION_RINGTONE_SHARED_PREFS_KEY_PREFIX
                + SubscriptionManager.getDefaultSubscriptionId();
    }
}