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.dialer.notification;
18 
19 import android.annotation.TargetApi;
20 import android.app.NotificationChannel;
21 import android.app.NotificationManager;
22 import android.content.Context;
23 import android.media.AudioAttributes;
24 import android.os.Build.VERSION_CODES;
25 import android.support.annotation.NonNull;
26 import android.support.annotation.Nullable;
27 import android.support.v4.os.BuildCompat;
28 import android.telecom.PhoneAccountHandle;
29 import android.util.ArraySet;
30 import com.android.dialer.common.Assert;
31 import com.android.dialer.common.LogUtil;
32 import java.util.Set;
33 
34 /** Creates all notification channels for Dialer. */
35 @TargetApi(VERSION_CODES.O)
36 public final class NotificationChannelManager {
37 
38   /**
39    * Creates all the notification channels Dialer will need. This method is called at app startup
40    * and must be fast. Currently it takes between 3 to 7 milliseconds on a Pixel XL.
41    *
42    * <p>An alternative approach would be to lazily create channels when we actualy post a
43    * notification. The advatange to precreating channels is that:
44    *
45    * <ul>
46    *   <li>channels will be available to user right away. For example, users can customize voicemail
47    *       sounds when they first get their device without waiting for a voicemail to arrive first.
48    *   <li>code that posts a notification can be simpler
49    *   <li>channel management code is simpler and it's easier to ensure that the correct set of
50    *       channels are visible.
51    *       <ul>
52    */
initChannels(@onNull Context context)53   public static void initChannels(@NonNull Context context) {
54     Assert.checkArgument(BuildCompat.isAtLeastO());
55     Assert.isNotNull(context);
56 
57     NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
58     Set<String> desiredChannelIds = getAllDesiredChannelIds(context);
59     Set<String> existingChannelIds = getAllExistingChannelIds(context);
60 
61     if (desiredChannelIds.equals(existingChannelIds)) {
62       return;
63     }
64     LogUtil.i(
65         "NotificationChannelManager.initChannels",
66         "doing an expensive initialization of all notification channels");
67     LogUtil.i(
68         "NotificationChannelManager.initChannels", "desired channel IDs: " + desiredChannelIds);
69     LogUtil.i(
70         "NotificationChannelManager.initChannels", "existing channel IDs: " + existingChannelIds);
71 
72     // Delete any old channels that we don't use any more. This is safe because if we're recreate
73     // this later then any user settings will be restored. An example is SIM specific voicemail
74     // channel that gets deleted when the user removes the SIM and is then restored when the user
75     // re-inserts the SIM.
76     for (String existingChannelId : existingChannelIds) {
77       if (!desiredChannelIds.contains(existingChannelId)) {
78         notificationManager.deleteNotificationChannel(existingChannelId);
79       }
80     }
81 
82     // Just recreate all desired channels. We won't do this often so it's ok to do this now.
83     createIncomingCallChannel(context);
84     createOngoingCallChannel(context);
85     createMissedCallChannel(context);
86     createDefaultChannel(context);
87     VoicemailChannelUtils.createAllChannels(context);
88   }
89 
90   @NonNull
getVoicemailChannelId( @onNull Context context, @Nullable PhoneAccountHandle handle)91   public static String getVoicemailChannelId(
92       @NonNull Context context, @Nullable PhoneAccountHandle handle) {
93     Assert.checkArgument(BuildCompat.isAtLeastO());
94     Assert.isNotNull(context);
95     return VoicemailChannelUtils.getChannelId(context, handle);
96   }
97 
getAllExistingChannelIds(@onNull Context context)98   private static Set<String> getAllExistingChannelIds(@NonNull Context context) {
99     Set<String> result = new ArraySet<>();
100     NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
101     for (NotificationChannel channel : notificationManager.getNotificationChannels()) {
102       result.add(channel.getId());
103     }
104     return result;
105   }
106 
getAllDesiredChannelIds(@onNull Context context)107   private static Set<String> getAllDesiredChannelIds(@NonNull Context context) {
108     Set<String> result = new ArraySet<>();
109     result.add(NotificationChannelId.INCOMING_CALL);
110     result.add(NotificationChannelId.ONGOING_CALL);
111     result.add(NotificationChannelId.MISSED_CALL);
112     result.add(NotificationChannelId.DEFAULT);
113     result.addAll(VoicemailChannelUtils.getAllChannelIds(context));
114     return result;
115   }
116 
createIncomingCallChannel(@onNull Context context)117   private static void createIncomingCallChannel(@NonNull Context context) {
118     NotificationChannel channel =
119         new NotificationChannel(
120             NotificationChannelId.INCOMING_CALL,
121             context.getText(R.string.notification_channel_incoming_call),
122             NotificationManager.IMPORTANCE_MAX);
123     channel.setShowBadge(false);
124     channel.enableLights(true);
125     channel.enableVibration(false);
126     channel.setSound(
127         null, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
128     context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
129   }
130 
createOngoingCallChannel(@onNull Context context)131   private static void createOngoingCallChannel(@NonNull Context context) {
132     NotificationChannel channel =
133         new NotificationChannel(
134             NotificationChannelId.ONGOING_CALL,
135             context.getText(R.string.notification_channel_ongoing_call),
136             NotificationManager.IMPORTANCE_DEFAULT);
137     channel.setShowBadge(false);
138     channel.enableLights(false);
139     channel.enableVibration(false);
140     channel.setSound(
141         null, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
142     context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
143   }
144 
createMissedCallChannel(@onNull Context context)145   private static void createMissedCallChannel(@NonNull Context context) {
146     NotificationChannel channel =
147         new NotificationChannel(
148             NotificationChannelId.MISSED_CALL,
149             context.getText(R.string.notification_channel_missed_call),
150             NotificationManager.IMPORTANCE_DEFAULT);
151     channel.setShowBadge(true);
152     channel.enableLights(true);
153     channel.enableVibration(true);
154     channel.setSound(
155         null, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
156     context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
157   }
158 
createDefaultChannel(@onNull Context context)159   private static void createDefaultChannel(@NonNull Context context) {
160     NotificationChannel channel =
161         new NotificationChannel(
162             NotificationChannelId.DEFAULT,
163             context.getText(R.string.notification_channel_misc),
164             NotificationManager.IMPORTANCE_DEFAULT);
165     channel.setShowBadge(false);
166     channel.enableLights(true);
167     channel.enableVibration(true);
168     context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
169   }
170 
NotificationChannelManager()171   private NotificationChannelManager() {}
172 }
173