1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.internal.notification;
16 
17 import android.app.INotificationManager;
18 import android.app.Notification;
19 import android.app.NotificationChannel;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.content.pm.ParceledListSlice;
23 import android.media.AudioAttributes;
24 import android.os.RemoteException;
25 import android.provider.Settings;
26 
27 import com.android.internal.R;
28 
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32 
33 // Manages the NotificationChannels used by the frameworks itself.
34 public class SystemNotificationChannels {
35     public static String VIRTUAL_KEYBOARD  = "VIRTUAL_KEYBOARD";
36     public static String PHYSICAL_KEYBOARD = "PHYSICAL_KEYBOARD";
37     public static String SECURITY = "SECURITY";
38     public static String CAR_MODE = "CAR_MODE";
39     public static String ACCOUNT = "ACCOUNT";
40     public static String DEVELOPER = "DEVELOPER";
41     public static String DEVELOPER_IMPORTANT = "DEVELOPER_IMPORTANT";
42     public static String UPDATES = "UPDATES";
43     public static String NETWORK_STATUS = "NETWORK_STATUS";
44     public static String NETWORK_ALERTS = "NETWORK_ALERTS";
45     public static String NETWORK_AVAILABLE = "NETWORK_AVAILABLE";
46     public static String VPN = "VPN";
47     /**
48      * @deprecated Legacy device admin channel with low importance which is no longer used,
49      *  Use the high importance {@link #DEVICE_ADMIN} channel instead.
50      */
51     @Deprecated public static String DEVICE_ADMIN_DEPRECATED = "DEVICE_ADMIN";
52     public static String DEVICE_ADMIN = "DEVICE_ADMIN_ALERTS";
53     public static String ALERTS = "ALERTS";
54     public static String RETAIL_MODE = "RETAIL_MODE";
55     public static String USB = "USB";
56     public static String FOREGROUND_SERVICE = "FOREGROUND_SERVICE";
57     public static String HEAVY_WEIGHT_APP = "HEAVY_WEIGHT_APP";
58     public static String SYSTEM_CHANGES = "SYSTEM_CHANGES";
59     public static String DO_NOT_DISTURB = "DO_NOT_DISTURB";
60 
createAll(Context context)61     public static void createAll(Context context) {
62         final NotificationManager nm = context.getSystemService(NotificationManager.class);
63         List<NotificationChannel> channelsList = new ArrayList<NotificationChannel>();
64         final NotificationChannel keyboard = new NotificationChannel(
65                 VIRTUAL_KEYBOARD,
66                 context.getString(R.string.notification_channel_virtual_keyboard),
67                 NotificationManager.IMPORTANCE_LOW);
68         keyboard.setBlockable(true);
69         channelsList.add(keyboard);
70 
71         final NotificationChannel physicalKeyboardChannel = new NotificationChannel(
72                 PHYSICAL_KEYBOARD,
73                 context.getString(R.string.notification_channel_physical_keyboard),
74                 NotificationManager.IMPORTANCE_DEFAULT);
75         physicalKeyboardChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
76                 Notification.AUDIO_ATTRIBUTES_DEFAULT);
77         physicalKeyboardChannel.setBlockable(true);
78         channelsList.add(physicalKeyboardChannel);
79 
80         final NotificationChannel security = new NotificationChannel(
81                 SECURITY,
82                 context.getString(R.string.notification_channel_security),
83                 NotificationManager.IMPORTANCE_LOW);
84         channelsList.add(security);
85 
86         final NotificationChannel car = new NotificationChannel(
87                 CAR_MODE,
88                 context.getString(R.string.notification_channel_car_mode),
89                 NotificationManager.IMPORTANCE_LOW);
90         car.setBlockable(true);
91         channelsList.add(car);
92 
93         channelsList.add(newAccountChannel(context));
94 
95         final NotificationChannel developer = new NotificationChannel(
96                 DEVELOPER,
97                 context.getString(R.string.notification_channel_developer),
98                 NotificationManager.IMPORTANCE_LOW);
99         developer.setBlockable(true);
100         channelsList.add(developer);
101 
102         final NotificationChannel developerImportant = new NotificationChannel(
103                 DEVELOPER_IMPORTANT,
104                 context.getString(R.string.notification_channel_developer_important),
105                 NotificationManager.IMPORTANCE_HIGH);
106         developer.setBlockable(true);
107         channelsList.add(developerImportant);
108 
109         final NotificationChannel updates = new NotificationChannel(
110                 UPDATES,
111                 context.getString(R.string.notification_channel_updates),
112                 NotificationManager.IMPORTANCE_LOW);
113         channelsList.add(updates);
114 
115         final NotificationChannel network = new NotificationChannel(
116                 NETWORK_STATUS,
117                 context.getString(R.string.notification_channel_network_status),
118                 NotificationManager.IMPORTANCE_LOW);
119         network.setBlockable(true);
120         channelsList.add(network);
121 
122         final NotificationChannel networkAlertsChannel = new NotificationChannel(
123                 NETWORK_ALERTS,
124                 context.getString(R.string.notification_channel_network_alerts),
125                 NotificationManager.IMPORTANCE_HIGH);
126         networkAlertsChannel.setBlockable(true);
127         channelsList.add(networkAlertsChannel);
128 
129         final NotificationChannel networkAvailable = new NotificationChannel(
130                 NETWORK_AVAILABLE,
131                 context.getString(R.string.notification_channel_network_available),
132                 NotificationManager.IMPORTANCE_LOW);
133         networkAvailable.setBlockable(true);
134         channelsList.add(networkAvailable);
135 
136         final NotificationChannel vpn = new NotificationChannel(
137                 VPN,
138                 context.getString(R.string.notification_channel_vpn),
139                 NotificationManager.IMPORTANCE_LOW);
140         channelsList.add(vpn);
141 
142         final NotificationChannel deviceAdmin = new NotificationChannel(
143                 DEVICE_ADMIN,
144                 context.getString(R.string.notification_channel_device_admin),
145                 NotificationManager.IMPORTANCE_HIGH);
146         channelsList.add(deviceAdmin);
147 
148         final NotificationChannel alertsChannel = new NotificationChannel(
149                 ALERTS,
150                 context.getString(R.string.notification_channel_alerts),
151                 NotificationManager.IMPORTANCE_DEFAULT);
152         channelsList.add(alertsChannel);
153 
154         final NotificationChannel retail = new NotificationChannel(
155                 RETAIL_MODE,
156                 context.getString(R.string.notification_channel_retail_mode),
157                 NotificationManager.IMPORTANCE_LOW);
158         channelsList.add(retail);
159 
160         final NotificationChannel usb = new NotificationChannel(
161                 USB,
162                 context.getString(R.string.notification_channel_usb),
163                 NotificationManager.IMPORTANCE_MIN);
164         channelsList.add(usb);
165 
166         NotificationChannel foregroundChannel = new NotificationChannel(
167                 FOREGROUND_SERVICE,
168                 context.getString(R.string.notification_channel_foreground_service),
169                 NotificationManager.IMPORTANCE_LOW);
170         foregroundChannel.setBlockable(true);
171         channelsList.add(foregroundChannel);
172 
173         NotificationChannel heavyWeightChannel = new NotificationChannel(
174                 HEAVY_WEIGHT_APP,
175                 context.getString(R.string.notification_channel_heavy_weight_app),
176                 NotificationManager.IMPORTANCE_DEFAULT);
177         heavyWeightChannel.setShowBadge(false);
178         heavyWeightChannel.setSound(null, new AudioAttributes.Builder()
179                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
180                 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
181                 .build());
182         channelsList.add(heavyWeightChannel);
183 
184         NotificationChannel systemChanges = new NotificationChannel(SYSTEM_CHANGES,
185                 context.getString(R.string.notification_channel_system_changes),
186                 NotificationManager.IMPORTANCE_LOW);
187         channelsList.add(systemChanges);
188 
189         NotificationChannel dndChanges = new NotificationChannel(DO_NOT_DISTURB,
190                 context.getString(R.string.notification_channel_do_not_disturb),
191                 NotificationManager.IMPORTANCE_LOW);
192         channelsList.add(dndChanges);
193 
194         nm.createNotificationChannels(channelsList);
195     }
196 
197     /** Remove notification channels which are no longer used */
removeDeprecated(Context context)198     public static void removeDeprecated(Context context) {
199         final NotificationManager nm = context.getSystemService(NotificationManager.class);
200         nm.deleteNotificationChannel(DEVICE_ADMIN_DEPRECATED);
201     }
202 
createAccountChannelForPackage(String pkg, int uid, Context context)203     public static void createAccountChannelForPackage(String pkg, int uid, Context context) {
204         final INotificationManager iNotificationManager = NotificationManager.getService();
205         try {
206             iNotificationManager.createNotificationChannelsForPackage(pkg, uid,
207                     new ParceledListSlice(Arrays.asList(newAccountChannel(context))));
208         } catch (RemoteException e) {
209             throw e.rethrowFromSystemServer();
210         }
211     }
212 
newAccountChannel(Context context)213     private static NotificationChannel newAccountChannel(Context context) {
214         return new NotificationChannel(
215                 ACCOUNT,
216                 context.getString(R.string.notification_channel_account),
217                 NotificationManager.IMPORTANCE_LOW);
218     }
219 
SystemNotificationChannels()220     private SystemNotificationChannels() {}
221 }
222