Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 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 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 17 | import android.app.INotificationManager; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 18 | import android.app.Notification; |
| 19 | import android.app.NotificationChannel; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.content.Context; |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 22 | import android.content.pm.ParceledListSlice; |
Dianne Hackborn | 70c95c5 | 2017-10-06 16:30:59 -0700 | [diff] [blame] | 23 | import android.media.AudioAttributes; |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 24 | import android.os.RemoteException; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 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"; |
Amin Shaikh | 1117656 | 2017-05-03 21:13:04 -0700 | [diff] [blame] | 44 | public static String NETWORK_AVAILABLE = "NETWORK_AVAILABLE"; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 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"; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 50 | public static String FOREGROUND_SERVICE = "FOREGROUND_SERVICE"; |
Dianne Hackborn | 70c95c5 | 2017-10-06 16:30:59 -0700 | [diff] [blame] | 51 | public static String HEAVY_WEIGHT_APP = "HEAVY_WEIGHT_APP"; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 52 | |
| 53 | public static void createAll(Context context) { |
| 54 | final NotificationManager nm = context.getSystemService(NotificationManager.class); |
| 55 | List<NotificationChannel> channelsList = new ArrayList<NotificationChannel>(); |
| 56 | channelsList.add(new NotificationChannel( |
| 57 | VIRTUAL_KEYBOARD, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 58 | context.getString(R.string.notification_channel_virtual_keyboard), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 59 | NotificationManager.IMPORTANCE_LOW)); |
| 60 | |
| 61 | final NotificationChannel physicalKeyboardChannel = new NotificationChannel( |
| 62 | PHYSICAL_KEYBOARD, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 63 | context.getString(R.string.notification_channel_physical_keyboard), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 64 | NotificationManager.IMPORTANCE_DEFAULT); |
| 65 | physicalKeyboardChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, |
| 66 | Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 67 | channelsList.add(physicalKeyboardChannel); |
| 68 | |
| 69 | channelsList.add(new NotificationChannel( |
| 70 | SECURITY, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 71 | context.getString(R.string.notification_channel_security), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 72 | NotificationManager.IMPORTANCE_LOW)); |
| 73 | |
| 74 | channelsList.add(new NotificationChannel( |
| 75 | CAR_MODE, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 76 | context.getString(R.string.notification_channel_car_mode), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 77 | NotificationManager.IMPORTANCE_LOW)); |
| 78 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 79 | channelsList.add(newAccountChannel(context)); |
| 80 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 81 | channelsList.add(new NotificationChannel( |
| 82 | DEVELOPER, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 83 | context.getString(R.string.notification_channel_developer), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 84 | NotificationManager.IMPORTANCE_LOW)); |
| 85 | |
| 86 | channelsList.add(new NotificationChannel( |
| 87 | UPDATES, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 88 | context.getString(R.string.notification_channel_updates), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 89 | NotificationManager.IMPORTANCE_LOW)); |
| 90 | |
| 91 | channelsList.add(new NotificationChannel( |
| 92 | NETWORK_STATUS, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 93 | context.getString(R.string.notification_channel_network_status), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 94 | NotificationManager.IMPORTANCE_LOW)); |
| 95 | |
| 96 | final NotificationChannel networkAlertsChannel = new NotificationChannel( |
| 97 | NETWORK_ALERTS, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 98 | context.getString(R.string.notification_channel_network_alerts), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 99 | NotificationManager.IMPORTANCE_HIGH); |
| 100 | networkAlertsChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, |
| 101 | Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 102 | channelsList.add(networkAlertsChannel); |
| 103 | |
| 104 | channelsList.add(new NotificationChannel( |
Amin Shaikh | 1117656 | 2017-05-03 21:13:04 -0700 | [diff] [blame] | 105 | NETWORK_AVAILABLE, |
| 106 | context.getString(R.string.notification_channel_network_available), |
| 107 | NotificationManager.IMPORTANCE_LOW)); |
| 108 | |
| 109 | channelsList.add(new NotificationChannel( |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 110 | VPN, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 111 | context.getString(R.string.notification_channel_vpn), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 112 | NotificationManager.IMPORTANCE_LOW)); |
| 113 | |
| 114 | channelsList.add(new NotificationChannel( |
| 115 | DEVICE_ADMIN, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 116 | context.getString(R.string.notification_channel_device_admin), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 117 | NotificationManager.IMPORTANCE_LOW)); |
| 118 | |
| 119 | final NotificationChannel alertsChannel = new NotificationChannel( |
| 120 | ALERTS, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 121 | context.getString(R.string.notification_channel_alerts), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 122 | NotificationManager.IMPORTANCE_DEFAULT); |
| 123 | alertsChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, |
| 124 | Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 125 | channelsList.add(alertsChannel); |
| 126 | |
| 127 | channelsList.add(new NotificationChannel( |
| 128 | RETAIL_MODE, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 129 | context.getString(R.string.notification_channel_retail_mode), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 130 | NotificationManager.IMPORTANCE_LOW)); |
| 131 | |
| 132 | channelsList.add(new NotificationChannel( |
| 133 | USB, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 134 | context.getString(R.string.notification_channel_usb), |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 135 | NotificationManager.IMPORTANCE_MIN)); |
| 136 | |
Julia Reynolds | c23a2e6 | 2017-08-17 13:34:27 -0400 | [diff] [blame] | 137 | NotificationChannel foregroundChannel = new NotificationChannel( |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 138 | FOREGROUND_SERVICE, |
| 139 | context.getString(R.string.notification_channel_foreground_service), |
Julia Reynolds | c23a2e6 | 2017-08-17 13:34:27 -0400 | [diff] [blame] | 140 | NotificationManager.IMPORTANCE_LOW); |
| 141 | foregroundChannel.setBlockableSystem(true); |
| 142 | channelsList.add(foregroundChannel); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 143 | |
Dianne Hackborn | 70c95c5 | 2017-10-06 16:30:59 -0700 | [diff] [blame] | 144 | NotificationChannel heavyWeightChannel = new NotificationChannel( |
| 145 | HEAVY_WEIGHT_APP, |
| 146 | context.getString(R.string.notification_channel_heavy_weight_app), |
| 147 | NotificationManager.IMPORTANCE_DEFAULT); |
| 148 | heavyWeightChannel.setShowBadge(false); |
| 149 | heavyWeightChannel.setSound(null, new AudioAttributes.Builder() |
| 150 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 151 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT) |
| 152 | .build()); |
| 153 | channelsList.add(heavyWeightChannel); |
| 154 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 155 | nm.createNotificationChannels(channelsList); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 156 | } |
| 157 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 158 | public static void createAccountChannelForPackage(String pkg, int uid, Context context) { |
| 159 | final INotificationManager iNotificationManager = NotificationManager.getService(); |
| 160 | try { |
| 161 | iNotificationManager.createNotificationChannelsForPackage(pkg, uid, |
| 162 | new ParceledListSlice(Arrays.asList(newAccountChannel(context)))); |
| 163 | } catch (RemoteException e) { |
| 164 | throw e.rethrowFromSystemServer(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private static NotificationChannel newAccountChannel(Context context) { |
| 169 | return new NotificationChannel( |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 170 | ACCOUNT, |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 171 | context.getString(R.string.notification_channel_account), |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 172 | NotificationManager.IMPORTANCE_LOW); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private SystemNotificationChannels() {} |
| 176 | } |