• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.app.admin.DevicePolicyResources.Strings.Core.NOTIFICATION_CHANNEL_DEVICE_ADMIN;
18 
19 import android.app.INotificationManager;
20 import android.app.Notification;
21 import android.app.NotificationChannel;
22 import android.app.NotificationManager;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.Context;
25 import android.content.pm.ParceledListSlice;
26 import android.media.AudioAttributes;
27 import android.os.RemoteException;
28 import android.provider.Settings;
29 
30 import com.android.internal.R;
31 
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 
36 // Manages the NotificationChannels used by the frameworks itself.
37 public class SystemNotificationChannels {
38     public static String VIRTUAL_KEYBOARD  = "VIRTUAL_KEYBOARD";
39     public static String PHYSICAL_KEYBOARD = "PHYSICAL_KEYBOARD";
40     public static String SECURITY = "SECURITY";
41     public static String CAR_MODE = "CAR_MODE";
42     public static String ACCOUNT = "ACCOUNT";
43     public static String DEVELOPER = "DEVELOPER";
44     public static String DEVELOPER_IMPORTANT = "DEVELOPER_IMPORTANT";
45     public static String UPDATES = "UPDATES";
46     public static String NETWORK_STATUS = "NETWORK_STATUS";
47     public static String NETWORK_ALERTS = "NETWORK_ALERTS";
48     public static String NETWORK_AVAILABLE = "NETWORK_AVAILABLE";
49     public static String VPN = "VPN";
50     /**
51      * @deprecated Legacy device admin channel with low importance which is no longer used,
52      *  Use the high importance {@link #DEVICE_ADMIN} channel instead.
53      */
54     @Deprecated public static String DEVICE_ADMIN_DEPRECATED = "DEVICE_ADMIN";
55     public static String DEVICE_ADMIN = "DEVICE_ADMIN_ALERTS";
56     public static String ALERTS = "ALERTS";
57     public static String RETAIL_MODE = "RETAIL_MODE";
58     public static String USB = "USB";
59     public static String FOREGROUND_SERVICE = "FOREGROUND_SERVICE";
60     public static String HEAVY_WEIGHT_APP = "HEAVY_WEIGHT_APP";
61     /**
62      * @deprecated Legacy system changes channel with low importance which is no longer used,
63      *  Use the default importance {@link #SYSTEM_CHANGES} channel instead.
64      */
65     @Deprecated public static String SYSTEM_CHANGES_DEPRECATED = "SYSTEM_CHANGES";
66     public static String SYSTEM_CHANGES = "SYSTEM_CHANGES_ALERTS";
67     public static String DO_NOT_DISTURB = "DO_NOT_DISTURB";
68     public static String ACCESSIBILITY_MAGNIFICATION = "ACCESSIBILITY_MAGNIFICATION";
69     public static String ACCESSIBILITY_SECURITY_POLICY = "ACCESSIBILITY_SECURITY_POLICY";
70     public static String ABUSIVE_BACKGROUND_APPS = "ABUSIVE_BACKGROUND_APPS";
71 
createAll(Context context)72     public static void createAll(Context context) {
73         final NotificationManager nm = context.getSystemService(NotificationManager.class);
74         List<NotificationChannel> channelsList = new ArrayList<NotificationChannel>();
75         final NotificationChannel keyboard = new NotificationChannel(
76                 VIRTUAL_KEYBOARD,
77                 context.getString(R.string.notification_channel_virtual_keyboard),
78                 NotificationManager.IMPORTANCE_LOW);
79         keyboard.setBlockable(true);
80         channelsList.add(keyboard);
81 
82         final NotificationChannel physicalKeyboardChannel = new NotificationChannel(
83                 PHYSICAL_KEYBOARD,
84                 context.getString(R.string.notification_channel_physical_keyboard),
85                 NotificationManager.IMPORTANCE_DEFAULT);
86         physicalKeyboardChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
87                 Notification.AUDIO_ATTRIBUTES_DEFAULT);
88         physicalKeyboardChannel.setBlockable(true);
89         channelsList.add(physicalKeyboardChannel);
90 
91         final NotificationChannel security = new NotificationChannel(
92                 SECURITY,
93                 context.getString(R.string.notification_channel_security),
94                 NotificationManager.IMPORTANCE_LOW);
95         channelsList.add(security);
96 
97         final NotificationChannel car = new NotificationChannel(
98                 CAR_MODE,
99                 context.getString(R.string.notification_channel_car_mode),
100                 NotificationManager.IMPORTANCE_LOW);
101         car.setBlockable(true);
102         channelsList.add(car);
103 
104         channelsList.add(newAccountChannel(context));
105 
106         final NotificationChannel developer = new NotificationChannel(
107                 DEVELOPER,
108                 context.getString(R.string.notification_channel_developer),
109                 NotificationManager.IMPORTANCE_LOW);
110         developer.setBlockable(true);
111         channelsList.add(developer);
112 
113         final NotificationChannel developerImportant = new NotificationChannel(
114                 DEVELOPER_IMPORTANT,
115                 context.getString(R.string.notification_channel_developer_important),
116                 NotificationManager.IMPORTANCE_HIGH);
117         developer.setBlockable(true);
118         channelsList.add(developerImportant);
119 
120         final NotificationChannel updates = new NotificationChannel(
121                 UPDATES,
122                 context.getString(R.string.notification_channel_updates),
123                 NotificationManager.IMPORTANCE_LOW);
124         channelsList.add(updates);
125 
126         final NotificationChannel network = new NotificationChannel(
127                 NETWORK_STATUS,
128                 context.getString(R.string.notification_channel_network_status),
129                 NotificationManager.IMPORTANCE_LOW);
130         network.setBlockable(true);
131         channelsList.add(network);
132 
133         final NotificationChannel networkAlertsChannel = new NotificationChannel(
134                 NETWORK_ALERTS,
135                 context.getString(R.string.notification_channel_network_alerts),
136                 NotificationManager.IMPORTANCE_HIGH);
137         networkAlertsChannel.setBlockable(true);
138         channelsList.add(networkAlertsChannel);
139 
140         final NotificationChannel networkAvailable = new NotificationChannel(
141                 NETWORK_AVAILABLE,
142                 context.getString(R.string.notification_channel_network_available),
143                 NotificationManager.IMPORTANCE_LOW);
144         networkAvailable.setBlockable(true);
145         channelsList.add(networkAvailable);
146 
147         final NotificationChannel vpn = new NotificationChannel(
148                 VPN,
149                 context.getString(R.string.notification_channel_vpn),
150                 NotificationManager.IMPORTANCE_LOW);
151         channelsList.add(vpn);
152 
153         final NotificationChannel deviceAdmin = new NotificationChannel(
154                 DEVICE_ADMIN,
155                 getDeviceAdminNotificationChannelName(context),
156                 NotificationManager.IMPORTANCE_HIGH);
157         channelsList.add(deviceAdmin);
158 
159         final NotificationChannel alertsChannel = new NotificationChannel(
160                 ALERTS,
161                 context.getString(R.string.notification_channel_alerts),
162                 NotificationManager.IMPORTANCE_DEFAULT);
163         channelsList.add(alertsChannel);
164 
165         final NotificationChannel retail = new NotificationChannel(
166                 RETAIL_MODE,
167                 context.getString(R.string.notification_channel_retail_mode),
168                 NotificationManager.IMPORTANCE_LOW);
169         channelsList.add(retail);
170 
171         final NotificationChannel usb = new NotificationChannel(
172                 USB,
173                 context.getString(R.string.notification_channel_usb),
174                 NotificationManager.IMPORTANCE_MIN);
175         channelsList.add(usb);
176 
177         NotificationChannel foregroundChannel = new NotificationChannel(
178                 FOREGROUND_SERVICE,
179                 context.getString(R.string.notification_channel_foreground_service),
180                 NotificationManager.IMPORTANCE_LOW);
181         foregroundChannel.setBlockable(true);
182         channelsList.add(foregroundChannel);
183 
184         NotificationChannel heavyWeightChannel = new NotificationChannel(
185                 HEAVY_WEIGHT_APP,
186                 context.getString(R.string.notification_channel_heavy_weight_app),
187                 NotificationManager.IMPORTANCE_DEFAULT);
188         heavyWeightChannel.setShowBadge(false);
189         heavyWeightChannel.setSound(null, new AudioAttributes.Builder()
190                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
191                 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
192                 .build());
193         channelsList.add(heavyWeightChannel);
194 
195         NotificationChannel systemChanges = new NotificationChannel(SYSTEM_CHANGES,
196                 context.getString(R.string.notification_channel_system_changes),
197                 NotificationManager.IMPORTANCE_DEFAULT);
198         systemChanges.setSound(null, new AudioAttributes.Builder()
199                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
200                 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
201                 .build());
202         channelsList.add(systemChanges);
203 
204         NotificationChannel dndChanges = new NotificationChannel(DO_NOT_DISTURB,
205                 context.getString(R.string.notification_channel_do_not_disturb),
206                 NotificationManager.IMPORTANCE_LOW);
207         channelsList.add(dndChanges);
208 
209         final NotificationChannel newFeaturePrompt = new NotificationChannel(
210                 ACCESSIBILITY_MAGNIFICATION,
211                 context.getString(R.string.notification_channel_accessibility_magnification),
212                 NotificationManager.IMPORTANCE_HIGH);
213         newFeaturePrompt.setBlockable(true);
214         channelsList.add(newFeaturePrompt);
215 
216         final NotificationChannel accessibilitySecurityPolicyChannel = new NotificationChannel(
217                 ACCESSIBILITY_SECURITY_POLICY,
218                 context.getString(R.string.notification_channel_accessibility_security_policy),
219                 NotificationManager.IMPORTANCE_LOW);
220         channelsList.add(accessibilitySecurityPolicyChannel);
221 
222         final NotificationChannel abusiveBackgroundAppsChannel = new NotificationChannel(
223                 ABUSIVE_BACKGROUND_APPS,
224                 context.getString(R.string.notification_channel_abusive_bg_apps),
225                 NotificationManager.IMPORTANCE_LOW);
226         channelsList.add(abusiveBackgroundAppsChannel);
227 
228         nm.createNotificationChannels(channelsList);
229     }
230 
getDeviceAdminNotificationChannelName(Context context)231     private static String getDeviceAdminNotificationChannelName(Context context) {
232         DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
233         return dpm.getResources().getString(NOTIFICATION_CHANNEL_DEVICE_ADMIN,
234                 () -> context.getString(R.string.notification_channel_device_admin));
235     }
236 
237     /** Remove notification channels which are no longer used */
removeDeprecated(Context context)238     public static void removeDeprecated(Context context) {
239         final NotificationManager nm = context.getSystemService(NotificationManager.class);
240         nm.deleteNotificationChannel(DEVICE_ADMIN_DEPRECATED);
241         nm.deleteNotificationChannel(SYSTEM_CHANGES_DEPRECATED);
242     }
243 
createAccountChannelForPackage(String pkg, int uid, Context context)244     public static void createAccountChannelForPackage(String pkg, int uid, Context context) {
245         final INotificationManager iNotificationManager = NotificationManager.getService();
246         try {
247             iNotificationManager.createNotificationChannelsForPackage(pkg, uid,
248                     new ParceledListSlice(Arrays.asList(newAccountChannel(context))));
249         } catch (RemoteException e) {
250             throw e.rethrowFromSystemServer();
251         }
252     }
253 
newAccountChannel(Context context)254     private static NotificationChannel newAccountChannel(Context context) {
255         return new NotificationChannel(
256                 ACCOUNT,
257                 context.getString(R.string.notification_channel_account),
258                 NotificationManager.IMPORTANCE_LOW);
259     }
260 
SystemNotificationChannels()261     private SystemNotificationChannels() {}
262 }
263