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     public static final String CHANNEL_ID_AUDIO_PROCESSING = "TelecomBackgroundAudioProcessing";
41     public static final String CHANNEL_ID_DISCONNECTED_CALLS = "TelecomDisconnectedCalls";
42     public static final String CHANNEL_ID_IN_CALL_SERVICE_CRASH = "TelecomInCallServiceCrash";
43 
44     private BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() {
45         @Override
46         public void onReceive(Context context, Intent intent) {
47             Log.i(this, "Locale change; recreating channels.");
48             createOrUpdateAll(context);
49         }
50     };
51 
createChannels(Context context)52     public void createChannels(Context context) {
53         context.registerReceiver(mLocaleChangeReceiver,
54                 new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
55 
56         createOrUpdateAll(context);
57     }
58 
createOrUpdateAll(Context context)59     private void createOrUpdateAll(Context context) {
60         createOrUpdateChannel(context, CHANNEL_ID_MISSED_CALLS);
61         createOrUpdateChannel(context, CHANNEL_ID_INCOMING_CALLS);
62         createOrUpdateChannel(context, CHANNEL_ID_CALL_BLOCKING);
63         createOrUpdateChannel(context, CHANNEL_ID_AUDIO_PROCESSING);
64         createOrUpdateChannel(context, CHANNEL_ID_DISCONNECTED_CALLS);
65         createOrUpdateChannel(context, CHANNEL_ID_IN_CALL_SERVICE_CRASH);
66     }
67 
createOrUpdateChannel(Context context, String channelId)68     private void createOrUpdateChannel(Context context, String channelId) {
69         NotificationChannel channel = createChannel(context, channelId);
70         getNotificationManager(context).createNotificationChannel(channel);
71     }
72 
createChannel(Context context, String channelId)73     private NotificationChannel createChannel(Context context, String channelId) {
74         Uri silentRingtone = Uri.parse("");
75 
76         CharSequence name = "";
77         int importance = NotificationManager.IMPORTANCE_DEFAULT;
78         boolean canShowBadge = false;
79         boolean lights = false;
80         boolean vibration = false;
81         Uri sound = silentRingtone;
82         switch (channelId) {
83             case CHANNEL_ID_INCOMING_CALLS:
84                 name = context.getText(R.string.notification_channel_incoming_call);
85                 importance = NotificationManager.IMPORTANCE_MAX;
86                 canShowBadge = false;
87                 lights = true;
88                 vibration = false;
89                 sound = silentRingtone;
90                 break;
91             case CHANNEL_ID_MISSED_CALLS:
92                 name = context.getText(R.string.notification_channel_missed_call);
93                 importance = NotificationManager.IMPORTANCE_DEFAULT;
94                 canShowBadge = true;
95                 lights = true;
96                 vibration = true;
97                 sound = silentRingtone;
98                 break;
99             case CHANNEL_ID_CALL_BLOCKING:
100                 name = context.getText(R.string.notification_channel_call_blocking);
101                 importance = NotificationManager.IMPORTANCE_LOW;
102                 canShowBadge = false;
103                 lights = false;
104                 vibration = false;
105                 sound = null;
106                 break;
107             case CHANNEL_ID_AUDIO_PROCESSING:
108                 name = context.getText(R.string.notification_channel_background_calls);
109                 importance = NotificationManager.IMPORTANCE_LOW;
110                 canShowBadge = false;
111                 lights = false;
112                 vibration = false;
113                 sound = null;
114                 break;
115             case CHANNEL_ID_DISCONNECTED_CALLS:
116                 name = context.getText(R.string.notification_channel_disconnected_calls);
117                 importance = NotificationManager.IMPORTANCE_DEFAULT;
118                 canShowBadge = true;
119                 lights = true;
120                 vibration = true;
121                 sound = silentRingtone;
122                 break;
123             case CHANNEL_ID_IN_CALL_SERVICE_CRASH:
124                 name = context.getText(R.string.notification_channel_in_call_service_crash);
125                 importance = NotificationManager.IMPORTANCE_DEFAULT;
126                 canShowBadge = true;
127                 lights = true;
128                 vibration = true;
129                 sound = null;
130         }
131 
132         NotificationChannel channel = new NotificationChannel(channelId, name, importance);
133         channel.setShowBadge(canShowBadge);
134         if (sound != null) {
135             channel.setSound(
136                     sound,
137                     new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
138                             .build());
139         }
140         channel.enableLights(lights);
141         channel.enableVibration(vibration);
142         return channel;
143     }
144 
getNotificationManager(Context context)145     private NotificationManager getNotificationManager(Context context) {
146         return context.getSystemService(NotificationManager.class);
147     }
148 }
149