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