1 /* 2 * Copyright (C) 2017 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.internal.telephony.util; 17 18 import android.app.NotificationChannel; 19 import android.app.NotificationManager; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.media.AudioAttributes; 25 import android.net.Uri; 26 import android.provider.Settings; 27 import android.telephony.SubscriptionManager; 28 29 import com.android.internal.R; 30 31 import java.util.Arrays; 32 import java.util.List; 33 34 35 public class NotificationChannelController { 36 37 /** 38 * list of {@link android.app.NotificationChannel} for telephony service. 39 */ 40 public static final String CHANNEL_ID_ALERT = "alert"; 41 public static final String CHANNEL_ID_CALL_FORWARD = "callForward"; 42 public static final String CHANNEL_ID_MOBILE_DATA_ALERT = "mobileDataAlert"; 43 public static final String CHANNEL_ID_SMS = "sms"; 44 public static final String CHANNEL_ID_VOICE_MAIL = "voiceMail"; 45 public static final String CHANNEL_ID_WFC = "wfc"; 46 47 /** 48 * Creates all notification channels and registers with NotificationManager. If a channel 49 * with the same ID is already registered, NotificationManager will ignore this call. 50 */ createAll(Context context)51 private static void createAll(Context context) { 52 final NotificationChannel alertChannel = new NotificationChannel( 53 CHANNEL_ID_ALERT, 54 context.getText(R.string.notification_channel_network_alert), 55 NotificationManager.IMPORTANCE_DEFAULT); 56 alertChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, 57 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()); 58 59 context.getSystemService(NotificationManager.class) 60 .createNotificationChannels(Arrays.asList( 61 new NotificationChannel(CHANNEL_ID_CALL_FORWARD, 62 context.getText(R.string.notification_channel_call_forward), 63 NotificationManager.IMPORTANCE_LOW), 64 new NotificationChannel(CHANNEL_ID_MOBILE_DATA_ALERT, 65 context.getText(R.string.notification_channel_mobile_data_alert), 66 NotificationManager.IMPORTANCE_DEFAULT), 67 new NotificationChannel(CHANNEL_ID_SMS, 68 context.getText(R.string.notification_channel_sms), 69 NotificationManager.IMPORTANCE_HIGH), 70 new NotificationChannel(CHANNEL_ID_WFC, 71 context.getText(R.string.notification_channel_wfc), 72 NotificationManager.IMPORTANCE_LOW), 73 alertChannel)); 74 // only for update 75 if (getChannel(CHANNEL_ID_VOICE_MAIL, context) != null) { 76 migrateVoicemailNotificationSettings(context); 77 } 78 } 79 NotificationChannelController(Context context)80 public NotificationChannelController(Context context) { 81 IntentFilter intentFilter = new IntentFilter(); 82 intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED); 83 intentFilter.addAction(Intent.ACTION_SIM_STATE_CHANGED); 84 context.registerReceiver(mBroadcastReceiver, intentFilter); 85 createAll(context); 86 } 87 getChannel(String channelId, Context context)88 public static NotificationChannel getChannel(String channelId, Context context) { 89 return context.getSystemService(NotificationManager.class) 90 .getNotificationChannel(channelId); 91 } 92 93 /** 94 * migrate deprecated voicemail notification settings to initial notification channel settings 95 * {@link VoicemailNotificationSettingsUtil#getRingTonePreference(Context)}} 96 * {@link VoicemailNotificationSettingsUtil#getVibrationPreference(Context)} 97 * notification settings are based on subId, only migrate if sub id matches. 98 * otherwise fallback to predefined voicemail channel settings. 99 * @param context 100 */ migrateVoicemailNotificationSettings(Context context)101 private static void migrateVoicemailNotificationSettings(Context context) { 102 final NotificationChannel voiceMailChannel = new NotificationChannel( 103 CHANNEL_ID_VOICE_MAIL, 104 context.getText(R.string.notification_channel_voice_mail), 105 NotificationManager.IMPORTANCE_DEFAULT); 106 voiceMailChannel.enableVibration( 107 VoicemailNotificationSettingsUtil.getVibrationPreference(context)); 108 Uri sound = VoicemailNotificationSettingsUtil.getRingTonePreference(context); 109 voiceMailChannel.setSound( 110 (sound == null) ? Settings.System.DEFAULT_NOTIFICATION_URI : sound, 111 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()); 112 context.getSystemService(NotificationManager.class) 113 .createNotificationChannel(voiceMailChannel); 114 } 115 116 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 117 @Override 118 public void onReceive(Context context, Intent intent) { 119 if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { 120 // rename all notification channels on locale change 121 createAll(context); 122 } else if (Intent.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) { 123 // migrate voicemail notification settings on sim load 124 if (SubscriptionManager.INVALID_SUBSCRIPTION_ID != 125 SubscriptionManager.getDefaultSubscriptionId()) { 126 migrateVoicemailNotificationSettings(context); 127 } 128 } 129 } 130 }; 131 } 132