Home | History | Annotate | Download | only in util
      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 
     33 
     34 public class NotificationChannelController {
     35 
     36     /**
     37      * list of {@link android.app.NotificationChannel} for telephony service.
     38      */
     39     public static final String CHANNEL_ID_ALERT = "alert";
     40     public static final String CHANNEL_ID_CALL_FORWARD = "callForward";
     41     public static final String CHANNEL_ID_MOBILE_DATA_STATUS = "mobileDataAlertNew";
     42     public static final String CHANNEL_ID_SIM = "sim";
     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     /** deprecated channel, replaced with @see #CHANNEL_ID_MOBILE_DATA_STATUS */
     48     private static final String CHANNEL_ID_MOBILE_DATA_ALERT_DEPRECATED = "mobileDataAlert";
     49 
     50     /**
     51      * Creates all notification channels and registers with NotificationManager. If a channel
     52      * with the same ID is already registered, NotificationManager will ignore this call.
     53      */
     54     private static void createAll(Context context) {
     55         final NotificationChannel alertChannel = new NotificationChannel(
     56                 CHANNEL_ID_ALERT,
     57                 context.getText(R.string.notification_channel_network_alert),
     58                 NotificationManager.IMPORTANCE_DEFAULT);
     59         alertChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
     60                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
     61         // allow users to block notifications from system
     62         alertChannel.setBlockableSystem(true);
     63 
     64         final NotificationChannel mobileDataStatusChannel = new NotificationChannel(
     65                 CHANNEL_ID_MOBILE_DATA_STATUS,
     66                 context.getText(R.string.notification_channel_mobile_data_status),
     67                 NotificationManager.IMPORTANCE_LOW);
     68         // allow users to block notifications from system
     69         mobileDataStatusChannel.setBlockableSystem(true);
     70 
     71         final NotificationChannel simChannel = new NotificationChannel(
     72                 CHANNEL_ID_SIM,
     73                 context.getText(R.string.notification_channel_sim),
     74                 NotificationManager.IMPORTANCE_LOW
     75         );
     76 
     77         simChannel.setSound(null, null);
     78 
     79         context.getSystemService(NotificationManager.class)
     80                 .createNotificationChannels(Arrays.asList(
     81                 new NotificationChannel(CHANNEL_ID_CALL_FORWARD,
     82                         context.getText(R.string.notification_channel_call_forward),
     83                         NotificationManager.IMPORTANCE_LOW),
     84                 new NotificationChannel(CHANNEL_ID_SMS,
     85                         context.getText(R.string.notification_channel_sms),
     86                         NotificationManager.IMPORTANCE_HIGH),
     87                 new NotificationChannel(CHANNEL_ID_WFC,
     88                         context.getText(R.string.notification_channel_wfc),
     89                         NotificationManager.IMPORTANCE_LOW),
     90                 alertChannel,
     91                 mobileDataStatusChannel,
     92                 simChannel));
     93         // only for update
     94         if (getChannel(CHANNEL_ID_VOICE_MAIL, context) != null) {
     95             migrateVoicemailNotificationSettings(context);
     96         }
     97         // after channel has been created there is no way to change the channel setting
     98         // programmatically. delete the old channel and create a new one with a new ID.
     99         if (getChannel(CHANNEL_ID_MOBILE_DATA_ALERT_DEPRECATED, context) != null) {
    100             context.getSystemService(NotificationManager.class)
    101                     .deleteNotificationChannel(CHANNEL_ID_MOBILE_DATA_ALERT_DEPRECATED);
    102         }
    103     }
    104 
    105     public NotificationChannelController(Context context) {
    106         IntentFilter intentFilter = new IntentFilter();
    107         intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
    108         intentFilter.addAction(Intent.ACTION_SIM_STATE_CHANGED);
    109         context.registerReceiver(mBroadcastReceiver, intentFilter);
    110         createAll(context);
    111     }
    112 
    113     public static NotificationChannel getChannel(String channelId, Context context) {
    114         return context.getSystemService(NotificationManager.class)
    115                 .getNotificationChannel(channelId);
    116     }
    117 
    118     /**
    119      * migrate deprecated voicemail notification settings to initial notification channel settings
    120      * {@link VoicemailNotificationSettingsUtil#getRingTonePreference(Context)}}
    121      * {@link VoicemailNotificationSettingsUtil#getVibrationPreference(Context)}
    122      * notification settings are based on subId, only migrate if sub id matches.
    123      * otherwise fallback to predefined voicemail channel settings.
    124      * @param context
    125      */
    126     private static void migrateVoicemailNotificationSettings(Context context) {
    127         final NotificationChannel voiceMailChannel = new NotificationChannel(
    128                 CHANNEL_ID_VOICE_MAIL,
    129                 context.getText(R.string.notification_channel_voice_mail),
    130                 NotificationManager.IMPORTANCE_DEFAULT);
    131         voiceMailChannel.enableVibration(
    132                 VoicemailNotificationSettingsUtil.getVibrationPreference(context));
    133         Uri sound = VoicemailNotificationSettingsUtil.getRingTonePreference(context);
    134         voiceMailChannel.setSound(
    135                 (sound == null) ? Settings.System.DEFAULT_NOTIFICATION_URI : sound,
    136                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
    137         context.getSystemService(NotificationManager.class)
    138                 .createNotificationChannel(voiceMailChannel);
    139     }
    140 
    141     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    142         @Override
    143         public void onReceive(Context context, Intent intent) {
    144             if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) {
    145                 // rename all notification channels on locale change
    146                 createAll(context);
    147             } else if (Intent.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
    148                 // migrate voicemail notification settings on sim load
    149                 if (SubscriptionManager.INVALID_SUBSCRIPTION_ID !=
    150                         SubscriptionManager.getDefaultSubscriptionId()) {
    151                     migrateVoicemailNotificationSettings(context);
    152                 }
    153             }
    154         }
    155     };
    156 }
    157