• 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 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         channelsList.add(network);
112 
113         final NotificationChannel networkAlertsChannel = new NotificationChannel(
114                 NETWORK_ALERTS,
115                 context.getString(R.string.notification_channel_network_alerts),
116                 NotificationManager.IMPORTANCE_HIGH);
117         networkAlertsChannel.setBlockableSystem(true);
118         channelsList.add(networkAlertsChannel);
119 
120         final NotificationChannel networkAvailable = new NotificationChannel(
121                 NETWORK_AVAILABLE,
122                 context.getString(R.string.notification_channel_network_available),
123                 NotificationManager.IMPORTANCE_LOW);
124         networkAvailable.setBlockableSystem(true);
125         channelsList.add(networkAvailable);
126 
127         final NotificationChannel vpn = new NotificationChannel(
128                 VPN,
129                 context.getString(R.string.notification_channel_vpn),
130                 NotificationManager.IMPORTANCE_LOW);
131         channelsList.add(vpn);
132 
133         final NotificationChannel deviceAdmin = new NotificationChannel(
134                 DEVICE_ADMIN,
135                 context.getString(R.string.notification_channel_device_admin),
136                 NotificationManager.IMPORTANCE_HIGH);
137         channelsList.add(deviceAdmin);
138 
139         final NotificationChannel alertsChannel = new NotificationChannel(
140                 ALERTS,
141                 context.getString(R.string.notification_channel_alerts),
142                 NotificationManager.IMPORTANCE_DEFAULT);
143         channelsList.add(alertsChannel);
144 
145         final NotificationChannel retail = new NotificationChannel(
146                 RETAIL_MODE,
147                 context.getString(R.string.notification_channel_retail_mode),
148                 NotificationManager.IMPORTANCE_LOW);
149         channelsList.add(retail);
150 
151         final NotificationChannel usb = new NotificationChannel(
152                 USB,
153                 context.getString(R.string.notification_channel_usb),
154                 NotificationManager.IMPORTANCE_MIN);
155         channelsList.add(usb);
156 
157         NotificationChannel foregroundChannel = new NotificationChannel(
158                 FOREGROUND_SERVICE,
159                 context.getString(R.string.notification_channel_foreground_service),
160                 NotificationManager.IMPORTANCE_LOW);
161         foregroundChannel.setBlockableSystem(true);
162         channelsList.add(foregroundChannel);
163 
164         NotificationChannel heavyWeightChannel = new NotificationChannel(
165                 HEAVY_WEIGHT_APP,
166                 context.getString(R.string.notification_channel_heavy_weight_app),
167                 NotificationManager.IMPORTANCE_DEFAULT);
168         heavyWeightChannel.setShowBadge(false);
169         heavyWeightChannel.setSound(null, new AudioAttributes.Builder()
170                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
171                 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
172                 .build());
173         channelsList.add(heavyWeightChannel);
174 
175         NotificationChannel systemChanges = new NotificationChannel(SYSTEM_CHANGES,
176                 context.getString(R.string.notification_channel_system_changes),
177                 NotificationManager.IMPORTANCE_LOW);
178         channelsList.add(systemChanges);
179 
180         NotificationChannel dndChanges = new NotificationChannel(DO_NOT_DISTURB,
181                 context.getString(R.string.notification_channel_do_not_disturb),
182                 NotificationManager.IMPORTANCE_LOW);
183         channelsList.add(dndChanges);
184 
185         nm.createNotificationChannels(channelsList);
186     }
187 
188     /** Remove notification channels which are no longer used */
removeDeprecated(Context context)189     public static void removeDeprecated(Context context) {
190         final NotificationManager nm = context.getSystemService(NotificationManager.class);
191         nm.deleteNotificationChannel(DEVICE_ADMIN_DEPRECATED);
192     }
193 
createAccountChannelForPackage(String pkg, int uid, Context context)194     public static void createAccountChannelForPackage(String pkg, int uid, Context context) {
195         final INotificationManager iNotificationManager = NotificationManager.getService();
196         try {
197             iNotificationManager.createNotificationChannelsForPackage(pkg, uid,
198                     new ParceledListSlice(Arrays.asList(newAccountChannel(context))));
199         } catch (RemoteException e) {
200             throw e.rethrowFromSystemServer();
201         }
202     }
203 
newAccountChannel(Context context)204     private static NotificationChannel newAccountChannel(Context context) {
205         return new NotificationChannel(
206                 ACCOUNT,
207                 context.getString(R.string.notification_channel_account),
208                 NotificationManager.IMPORTANCE_LOW);
209     }
210 
SystemNotificationChannels()211     private SystemNotificationChannels() {}
212 }
213