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 17 package com.android.server.telecom.ui; 18 19 import android.app.NotificationChannel; 20 import android.app.NotificationManager; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.media.AudioAttributes; 26 import android.net.Uri; 27 import android.telecom.Log; 28 29 import com.android.server.telecom.R; 30 31 /** 32 * Manages the {@link android.app.NotificationChannel}s for Telecom. 33 */ 34 public class NotificationChannelManager { 35 public static final String CHANNEL_ID_NAME = "Telecom-"; 36 37 public static final String CHANNEL_ID_MISSED_CALLS = "TelecomMissedCalls"; 38 public static final String CHANNEL_ID_INCOMING_CALLS = "TelecomIncomingCalls"; 39 public static final String CHANNEL_ID_CALL_BLOCKING = "TelecomCallBlocking"; 40 41 private BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { 42 @Override 43 public void onReceive(Context context, Intent intent) { 44 Log.i(this, "Locale change; recreating channels."); 45 createOrUpdateAll(context); 46 } 47 }; 48 createChannels(Context context)49 public void createChannels(Context context) { 50 context.registerReceiver(mLocaleChangeReceiver, 51 new IntentFilter(Intent.ACTION_LOCALE_CHANGED)); 52 53 createOrUpdateAll(context); 54 } 55 createOrUpdateAll(Context context)56 private void createOrUpdateAll(Context context) { 57 createOrUpdateChannel(context, CHANNEL_ID_MISSED_CALLS); 58 createOrUpdateChannel(context, CHANNEL_ID_INCOMING_CALLS); 59 createOrUpdateChannel(context, CHANNEL_ID_CALL_BLOCKING); 60 } 61 createOrUpdateChannel(Context context, String channelId)62 private void createOrUpdateChannel(Context context, String channelId) { 63 NotificationChannel channel = createChannel(context, channelId); 64 getNotificationManager(context).createNotificationChannel(channel); 65 } 66 createChannel(Context context, String channelId)67 private NotificationChannel createChannel(Context context, String channelId) { 68 Uri silentRingtone = Uri.parse(""); 69 70 CharSequence name = ""; 71 int importance = NotificationManager.IMPORTANCE_DEFAULT; 72 boolean canShowBadge = false; 73 boolean lights = false; 74 boolean vibration = false; 75 Uri sound = silentRingtone; 76 switch (channelId) { 77 case CHANNEL_ID_INCOMING_CALLS: 78 name = context.getText(R.string.notification_channel_incoming_call); 79 importance = NotificationManager.IMPORTANCE_MAX; 80 canShowBadge = false; 81 lights = true; 82 vibration = false; 83 sound = silentRingtone; 84 break; 85 case CHANNEL_ID_MISSED_CALLS: 86 name = context.getText(R.string.notification_channel_missed_call); 87 importance = NotificationManager.IMPORTANCE_DEFAULT; 88 canShowBadge = true; 89 lights = true; 90 vibration = true; 91 sound = silentRingtone; 92 break; 93 case CHANNEL_ID_CALL_BLOCKING: 94 name = context.getText(R.string.notification_channel_call_blocking); 95 importance = NotificationManager.IMPORTANCE_LOW; 96 canShowBadge = false; 97 lights = false; 98 vibration = false; 99 sound = null; 100 break; 101 } 102 103 NotificationChannel channel = new NotificationChannel(channelId, name, importance); 104 channel.setShowBadge(canShowBadge); 105 if (sound != null) { 106 channel.setSound( 107 sound, 108 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION) 109 .build()); 110 } 111 channel.enableLights(lights); 112 channel.enableVibration(vibration); 113 return channel; 114 } 115 getNotificationManager(Context context)116 private NotificationManager getNotificationManager(Context context) { 117 return context.getSystemService(NotificationManager.class); 118 } 119 } 120