blob: 4a181b27b2e3d67ebc70bdc7d09fe3f8f868d70c [file] [log] [blame]
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001/*
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
15package com.android.internal.notification;
16
Geoffrey Pitsch3560f842017-03-22 16:42:43 -040017import android.app.INotificationManager;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050018import android.app.Notification;
19import android.app.NotificationChannel;
20import android.app.NotificationManager;
21import android.content.Context;
Geoffrey Pitsch3560f842017-03-22 16:42:43 -040022import android.content.pm.ParceledListSlice;
Dianne Hackborn70c95c52017-10-06 16:30:59 -070023import android.media.AudioAttributes;
Geoffrey Pitsch3560f842017-03-22 16:42:43 -040024import android.os.RemoteException;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050025import android.provider.Settings;
26
27import com.android.internal.R;
28
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.List;
32
33// Manages the NotificationChannels used by the frameworks itself.
34public 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 Shaikh11176562017-05-03 21:13:04 -070044 public static String NETWORK_AVAILABLE = "NETWORK_AVAILABLE";
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050045 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 Hackborn83b40f62017-04-26 13:59:47 -070050 public static String FOREGROUND_SERVICE = "FOREGROUND_SERVICE";
Dianne Hackborn70c95c52017-10-06 16:30:59 -070051 public static String HEAVY_WEIGHT_APP = "HEAVY_WEIGHT_APP";
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050052
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 Reynolds1d97e6a2017-03-13 15:05:40 -040058 context.getString(R.string.notification_channel_virtual_keyboard),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050059 NotificationManager.IMPORTANCE_LOW));
60
61 final NotificationChannel physicalKeyboardChannel = new NotificationChannel(
62 PHYSICAL_KEYBOARD,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -040063 context.getString(R.string.notification_channel_physical_keyboard),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050064 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 Reynolds1d97e6a2017-03-13 15:05:40 -040071 context.getString(R.string.notification_channel_security),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050072 NotificationManager.IMPORTANCE_LOW));
73
74 channelsList.add(new NotificationChannel(
75 CAR_MODE,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -040076 context.getString(R.string.notification_channel_car_mode),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050077 NotificationManager.IMPORTANCE_LOW));
78
Geoffrey Pitsch3560f842017-03-22 16:42:43 -040079 channelsList.add(newAccountChannel(context));
80
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050081 channelsList.add(new NotificationChannel(
82 DEVELOPER,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -040083 context.getString(R.string.notification_channel_developer),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050084 NotificationManager.IMPORTANCE_LOW));
85
86 channelsList.add(new NotificationChannel(
87 UPDATES,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -040088 context.getString(R.string.notification_channel_updates),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050089 NotificationManager.IMPORTANCE_LOW));
90
91 channelsList.add(new NotificationChannel(
92 NETWORK_STATUS,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -040093 context.getString(R.string.notification_channel_network_status),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050094 NotificationManager.IMPORTANCE_LOW));
95
96 final NotificationChannel networkAlertsChannel = new NotificationChannel(
97 NETWORK_ALERTS,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -040098 context.getString(R.string.notification_channel_network_alerts),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050099 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 Shaikh11176562017-05-03 21:13:04 -0700105 NETWORK_AVAILABLE,
106 context.getString(R.string.notification_channel_network_available),
107 NotificationManager.IMPORTANCE_LOW));
108
109 channelsList.add(new NotificationChannel(
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500110 VPN,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400111 context.getString(R.string.notification_channel_vpn),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500112 NotificationManager.IMPORTANCE_LOW));
113
114 channelsList.add(new NotificationChannel(
115 DEVICE_ADMIN,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400116 context.getString(R.string.notification_channel_device_admin),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500117 NotificationManager.IMPORTANCE_LOW));
118
119 final NotificationChannel alertsChannel = new NotificationChannel(
120 ALERTS,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400121 context.getString(R.string.notification_channel_alerts),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500122 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 Reynolds1d97e6a2017-03-13 15:05:40 -0400129 context.getString(R.string.notification_channel_retail_mode),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500130 NotificationManager.IMPORTANCE_LOW));
131
132 channelsList.add(new NotificationChannel(
133 USB,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400134 context.getString(R.string.notification_channel_usb),
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500135 NotificationManager.IMPORTANCE_MIN));
136
Julia Reynoldsc23a2e62017-08-17 13:34:27 -0400137 NotificationChannel foregroundChannel = new NotificationChannel(
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700138 FOREGROUND_SERVICE,
139 context.getString(R.string.notification_channel_foreground_service),
Julia Reynoldsc23a2e62017-08-17 13:34:27 -0400140 NotificationManager.IMPORTANCE_LOW);
141 foregroundChannel.setBlockableSystem(true);
142 channelsList.add(foregroundChannel);
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700143
Dianne Hackborn70c95c52017-10-06 16:30:59 -0700144 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 Pitschaf759c52017-02-15 09:35:38 -0500155 nm.createNotificationChannels(channelsList);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500156 }
157
Geoffrey Pitsch3560f842017-03-22 16:42:43 -0400158 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 Pitschaf759c52017-02-15 09:35:38 -0500170 ACCOUNT,
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400171 context.getString(R.string.notification_channel_account),
Geoffrey Pitsch3560f842017-03-22 16:42:43 -0400172 NotificationManager.IMPORTANCE_LOW);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500173 }
174
175 private SystemNotificationChannels() {}
176}