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 UPDATES = "UPDATES";
42     public static String NETWORK_STATUS = "NETWORK_STATUS";
43     public static String NETWORK_ALERTS = "NETWORK_ALERTS";
44     public static String NETWORK_AVAILABLE = "NETWORK_AVAILABLE";
45     public static String VPN = "VPN";
46     /**
47      * @deprecated Legacy device admin channel with low importance which is no longer used,
48      *  Use the high importance {@link #DEVICE_ADMIN} channel instead.
49      */
50     @Deprecated public static String DEVICE_ADMIN_DEPRECATED = "DEVICE_ADMIN";
51     public static String DEVICE_ADMIN = "DEVICE_ADMIN_ALERTS";
52     public static String ALERTS = "ALERTS";
53     public static String RETAIL_MODE = "RETAIL_MODE";
54     public static String USB = "USB";
55     public static String FOREGROUND_SERVICE = "FOREGROUND_SERVICE";
56     public static String HEAVY_WEIGHT_APP = "HEAVY_WEIGHT_APP";
57     public static String SYSTEM_CHANGES = "SYSTEM_CHANGES";
58     public static String DO_NOT_DISTURB = "DO_NOT_DISTURB";
59 
createAll(Context context)60     public static void createAll(Context context) {
61         final NotificationManager nm = context.getSystemService(NotificationManager.class);
62         List<NotificationChannel> channelsList = new ArrayList<NotificationChannel>();
63         final NotificationChannel keyboard = new NotificationChannel(
64                 VIRTUAL_KEYBOARD,
65                 context.getString(R.string.notification_channel_virtual_keyboard),
66                 NotificationManager.IMPORTANCE_LOW);
67         keyboard.setBlockableSystem(true);
68         channelsList.add(keyboard);
69 
70         final NotificationChannel physicalKeyboardChannel = new NotificationChannel(
71                 PHYSICAL_KEYBOARD,
72                 context.getString(R.string.notification_channel_physical_keyboard),
73                 NotificationManager.IMPORTANCE_DEFAULT);
74         physicalKeyboardChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
75                 Notification.AUDIO_ATTRIBUTES_DEFAULT);
76         physicalKeyboardChannel.setBlockableSystem(true);
77         channelsList.add(physicalKeyboardChannel);
78 
79         final NotificationChannel security = new NotificationChannel(
80                 SECURITY,
81                 context.getString(R.string.notification_channel_security),
82                 NotificationManager.IMPORTANCE_LOW);
83         channelsList.add(security);
84 
85         final NotificationChannel car = new NotificationChannel(
86                 CAR_MODE,
87                 context.getString(R.string.notification_channel_car_mode),
88                 NotificationManager.IMPORTANCE_LOW);
89         car.setBlockableSystem(true);
90         channelsList.add(car);
91 
92         channelsList.add(newAccountChannel(context));
93 
94         final NotificationChannel developer = new NotificationChannel(
95                 DEVELOPER,
96                 context.getString(R.string.notification_channel_developer),
97                 NotificationManager.IMPORTANCE_LOW);
98         developer.setBlockableSystem(true);
99         channelsList.add(developer);
100 
101         final NotificationChannel updates = new NotificationChannel(
102                 UPDATES,
103                 context.getString(R.string.notification_channel_updates),
104                 NotificationManager.IMPORTANCE_LOW);
105         channelsList.add(updates);
106 
107         final NotificationChannel network = new NotificationChannel(
108                 NETWORK_STATUS,
109                 context.getString(R.string.notification_channel_network_status),
110                 NotificationManager.IMPORTANCE_LOW);
111         network.setBlockableSystem(true);
112         channelsList.add(network);
113 
114         final NotificationChannel networkAlertsChannel = new NotificationChannel(
115                 NETWORK_ALERTS,
116                 context.getString(R.string.notification_channel_network_alerts),
117                 NotificationManager.IMPORTANCE_HIGH);
118         networkAlertsChannel.setBlockableSystem(true);
119         channelsList.add(networkAlertsChannel);
120 
121         final NotificationChannel networkAvailable = new NotificationChannel(
122                 NETWORK_AVAILABLE,
123                 context.getString(R.string.notification_channel_network_available),
124                 NotificationManager.IMPORTANCE_LOW);
125         networkAvailable.setBlockableSystem(true);
126         channelsList.add(networkAvailable);
127 
128         final NotificationChannel vpn = new NotificationChannel(
129                 VPN,
130                 context.getString(R.string.notification_channel_vpn),
131                 NotificationManager.IMPORTANCE_LOW);
132         channelsList.add(vpn);
133 
134         final NotificationChannel deviceAdmin = new NotificationChannel(
135                 DEVICE_ADMIN,
136                 context.getString(R.string.notification_channel_device_admin),
137                 NotificationManager.IMPORTANCE_HIGH);
138         channelsList.add(deviceAdmin);
139 
140         final NotificationChannel alertsChannel = new NotificationChannel(
141                 ALERTS,
142                 context.getString(R.string.notification_channel_alerts),
143                 NotificationManager.IMPORTANCE_DEFAULT);
144         channelsList.add(alertsChannel);
145 
146         final NotificationChannel retail = new NotificationChannel(
147                 RETAIL_MODE,
148                 context.getString(R.string.notification_channel_retail_mode),
149                 NotificationManager.IMPORTANCE_LOW);
150         channelsList.add(retail);
151 
152         final NotificationChannel usb = new NotificationChannel(
153                 USB,
154                 context.getString(R.string.notification_channel_usb),
155                 NotificationManager.IMPORTANCE_MIN);
156         channelsList.add(usb);
157 
158         NotificationChannel foregroundChannel = new NotificationChannel(
159                 FOREGROUND_SERVICE,
160                 context.getString(R.string.notification_channel_foreground_service),
161                 NotificationManager.IMPORTANCE_LOW);
162         foregroundChannel.setBlockableSystem(true);
163         channelsList.add(foregroundChannel);
164 
165         NotificationChannel heavyWeightChannel = new NotificationChannel(
166                 HEAVY_WEIGHT_APP,
167                 context.getString(R.string.notification_channel_heavy_weight_app),
168                 NotificationManager.IMPORTANCE_DEFAULT);
169         heavyWeightChannel.setShowBadge(false);
170         heavyWeightChannel.setSound(null, new AudioAttributes.Builder()
171                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
172                 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
173                 .build());
174         channelsList.add(heavyWeightChannel);
175 
176         NotificationChannel systemChanges = new NotificationChannel(SYSTEM_CHANGES,
177                 context.getString(R.string.notification_channel_system_changes),
178                 NotificationManager.IMPORTANCE_LOW);
179         channelsList.add(systemChanges);
180 
181         NotificationChannel dndChanges = new NotificationChannel(DO_NOT_DISTURB,
182                 context.getString(R.string.notification_channel_do_not_disturb),
183                 NotificationManager.IMPORTANCE_LOW);
184         channelsList.add(dndChanges);
185 
186         nm.createNotificationChannels(channelsList);
187     }
188 
189     /** Remove notification channels which are no longer used */
removeDeprecated(Context context)190     public static void removeDeprecated(Context context) {
191         final NotificationManager nm = context.getSystemService(NotificationManager.class);
192         nm.deleteNotificationChannel(DEVICE_ADMIN_DEPRECATED);
193     }
194 
createAccountChannelForPackage(String pkg, int uid, Context context)195     public static void createAccountChannelForPackage(String pkg, int uid, Context context) {
196         final INotificationManager iNotificationManager = NotificationManager.getService();
197         try {
198             iNotificationManager.createNotificationChannelsForPackage(pkg, uid,
199                     new ParceledListSlice(Arrays.asList(newAccountChannel(context))));
200         } catch (RemoteException e) {
201             throw e.rethrowFromSystemServer();
202         }
203     }
204 
newAccountChannel(Context context)205     private static NotificationChannel newAccountChannel(Context context) {
206         return new NotificationChannel(
207                 ACCOUNT,
208                 context.getString(R.string.notification_channel_account),
209                 NotificationManager.IMPORTANCE_LOW);
210     }
211 
SystemNotificationChannels()212     private SystemNotificationChannels() {}
213 }
214