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