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