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