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.os.RemoteException; 24 import android.provider.Settings; 25 26 import com.android.internal.R; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.List; 31 32 // Manages the NotificationChannels used by the frameworks itself. 33 public class SystemNotificationChannels { 34 public static String VIRTUAL_KEYBOARD = "VIRTUAL_KEYBOARD"; 35 public static String PHYSICAL_KEYBOARD = "PHYSICAL_KEYBOARD"; 36 public static String SECURITY = "SECURITY"; 37 public static String CAR_MODE = "CAR_MODE"; 38 public static String ACCOUNT = "ACCOUNT"; 39 public static String DEVELOPER = "DEVELOPER"; 40 public static String UPDATES = "UPDATES"; 41 public static String NETWORK_STATUS = "NETWORK_STATUS"; 42 public static String NETWORK_ALERTS = "NETWORK_ALERTS"; 43 public static String NETWORK_AVAILABLE = "NETWORK_AVAILABLE"; 44 public static String VPN = "VPN"; 45 public static String DEVICE_ADMIN = "DEVICE_ADMIN"; 46 public static String ALERTS = "ALERTS"; 47 public static String RETAIL_MODE = "RETAIL_MODE"; 48 public static String USB = "USB"; 49 public static String FOREGROUND_SERVICE = "FOREGROUND_SERVICE"; 50 createAll(Context context)51 public static void createAll(Context context) { 52 final NotificationManager nm = context.getSystemService(NotificationManager.class); 53 List<NotificationChannel> channelsList = new ArrayList<NotificationChannel>(); 54 channelsList.add(new NotificationChannel( 55 VIRTUAL_KEYBOARD, 56 context.getString(R.string.notification_channel_virtual_keyboard), 57 NotificationManager.IMPORTANCE_LOW)); 58 59 final NotificationChannel physicalKeyboardChannel = new NotificationChannel( 60 PHYSICAL_KEYBOARD, 61 context.getString(R.string.notification_channel_physical_keyboard), 62 NotificationManager.IMPORTANCE_DEFAULT); 63 physicalKeyboardChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, 64 Notification.AUDIO_ATTRIBUTES_DEFAULT); 65 channelsList.add(physicalKeyboardChannel); 66 67 channelsList.add(new NotificationChannel( 68 SECURITY, 69 context.getString(R.string.notification_channel_security), 70 NotificationManager.IMPORTANCE_LOW)); 71 72 channelsList.add(new NotificationChannel( 73 CAR_MODE, 74 context.getString(R.string.notification_channel_car_mode), 75 NotificationManager.IMPORTANCE_LOW)); 76 77 channelsList.add(newAccountChannel(context)); 78 79 channelsList.add(new NotificationChannel( 80 DEVELOPER, 81 context.getString(R.string.notification_channel_developer), 82 NotificationManager.IMPORTANCE_LOW)); 83 84 channelsList.add(new NotificationChannel( 85 UPDATES, 86 context.getString(R.string.notification_channel_updates), 87 NotificationManager.IMPORTANCE_LOW)); 88 89 channelsList.add(new NotificationChannel( 90 NETWORK_STATUS, 91 context.getString(R.string.notification_channel_network_status), 92 NotificationManager.IMPORTANCE_LOW)); 93 94 final NotificationChannel networkAlertsChannel = new NotificationChannel( 95 NETWORK_ALERTS, 96 context.getString(R.string.notification_channel_network_alerts), 97 NotificationManager.IMPORTANCE_HIGH); 98 networkAlertsChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, 99 Notification.AUDIO_ATTRIBUTES_DEFAULT); 100 channelsList.add(networkAlertsChannel); 101 102 channelsList.add(new NotificationChannel( 103 NETWORK_AVAILABLE, 104 context.getString(R.string.notification_channel_network_available), 105 NotificationManager.IMPORTANCE_LOW)); 106 107 channelsList.add(new NotificationChannel( 108 VPN, 109 context.getString(R.string.notification_channel_vpn), 110 NotificationManager.IMPORTANCE_LOW)); 111 112 channelsList.add(new NotificationChannel( 113 DEVICE_ADMIN, 114 context.getString(R.string.notification_channel_device_admin), 115 NotificationManager.IMPORTANCE_LOW)); 116 117 final NotificationChannel alertsChannel = new NotificationChannel( 118 ALERTS, 119 context.getString(R.string.notification_channel_alerts), 120 NotificationManager.IMPORTANCE_DEFAULT); 121 alertsChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, 122 Notification.AUDIO_ATTRIBUTES_DEFAULT); 123 channelsList.add(alertsChannel); 124 125 channelsList.add(new NotificationChannel( 126 RETAIL_MODE, 127 context.getString(R.string.notification_channel_retail_mode), 128 NotificationManager.IMPORTANCE_LOW)); 129 130 channelsList.add(new NotificationChannel( 131 USB, 132 context.getString(R.string.notification_channel_usb), 133 NotificationManager.IMPORTANCE_MIN)); 134 135 channelsList.add(new NotificationChannel( 136 FOREGROUND_SERVICE, 137 context.getString(R.string.notification_channel_foreground_service), 138 NotificationManager.IMPORTANCE_LOW)); 139 140 nm.createNotificationChannels(channelsList); 141 } 142 createAccountChannelForPackage(String pkg, int uid, Context context)143 public static void createAccountChannelForPackage(String pkg, int uid, Context context) { 144 final INotificationManager iNotificationManager = NotificationManager.getService(); 145 try { 146 iNotificationManager.createNotificationChannelsForPackage(pkg, uid, 147 new ParceledListSlice(Arrays.asList(newAccountChannel(context)))); 148 } catch (RemoteException e) { 149 throw e.rethrowFromSystemServer(); 150 } 151 } 152 newAccountChannel(Context context)153 private static NotificationChannel newAccountChannel(Context context) { 154 return new NotificationChannel( 155 ACCOUNT, 156 context.getString(R.string.notification_channel_account), 157 NotificationManager.IMPORTANCE_LOW); 158 } 159 SystemNotificationChannels()160 private SystemNotificationChannels() {} 161 } 162