blob: f35af90edc3c4ef715b3f7951d4e7ebaac563f29 [file] [log] [blame]
Dan Sandler8e032e12017-01-25 13:41: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.systemui.util;
16
17import android.app.NotificationChannel;
18import android.app.NotificationManager;
Dan Sandler8e032e12017-01-25 13:41:38 -050019import android.content.Context;
Dmitri Plotnikov519c0882017-02-13 14:37:05 -080020import android.content.pm.PackageManager;
Beverly334bc5f2017-07-31 10:37:17 -040021import android.media.AudioAttributes;
22import android.net.Uri;
23import android.provider.Settings;
24
Dan Sandler8e032e12017-01-25 13:41:38 -050025import com.android.internal.annotations.VisibleForTesting;
26import com.android.systemui.R;
27import com.android.systemui.SystemUI;
28
29import java.util.Arrays;
30
31public class NotificationChannels extends SystemUI {
32 public static String ALERTS = "ALR";
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050033 public static String SCREENSHOTS_LEGACY = "SCN";
34 public static String SCREENSHOTS_HEADSUP = "SCN_HEADSUP";
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -050035 public static String GENERAL = "GEN";
Dan Sandler8e032e12017-01-25 13:41:38 -050036 public static String STORAGE = "DSK";
Jaewan Kim26c63562017-04-26 15:41:43 +090037 public static String TVPIP = "TPP";
Beverly334bc5f2017-07-31 10:37:17 -040038 public static String BATTERY = "BAT";
Makoto Onuki52c62952018-03-22 10:43:03 -070039 public static String HINTS = "HNT";
Dan Sandler8e032e12017-01-25 13:41:38 -050040
Beverly70dcd002018-03-29 17:09:16 -040041 public static void createAll(Context context) {
Dan Sandler8e032e12017-01-25 13:41:38 -050042 final NotificationManager nm = context.getSystemService(NotificationManager.class);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040043 final NotificationChannel batteryChannel = new NotificationChannel(BATTERY,
Beverly334bc5f2017-07-31 10:37:17 -040044 context.getString(R.string.notification_channel_battery),
45 NotificationManager.IMPORTANCE_MAX);
46 final String soundPath = Settings.Global.getString(context.getContentResolver(),
47 Settings.Global.LOW_BATTERY_SOUND);
48 batteryChannel.setSound(Uri.parse("file://" + soundPath), new AudioAttributes.Builder()
49 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
50 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
51 .build());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040052 batteryChannel.setBlockableSystem(true);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040053
54 final NotificationChannel alerts = new NotificationChannel(
55 ALERTS,
56 context.getString(R.string.notification_channel_alerts),
57 NotificationManager.IMPORTANCE_HIGH);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040058
59 final NotificationChannel general = new NotificationChannel(
60 GENERAL,
61 context.getString(R.string.notification_channel_general),
62 NotificationManager.IMPORTANCE_MIN);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040063
64 final NotificationChannel storage = new NotificationChannel(
65 STORAGE,
66 context.getString(R.string.notification_channel_storage),
67 isTv(context)
68 ? NotificationManager.IMPORTANCE_DEFAULT
69 : NotificationManager.IMPORTANCE_LOW);
Beverly334bc5f2017-07-31 10:37:17 -040070
Makoto Onuki52c62952018-03-22 10:43:03 -070071 final NotificationChannel hint = new NotificationChannel(
72 HINTS,
73 context.getString(R.string.notification_channel_hints),
74 NotificationManager.IMPORTANCE_DEFAULT);
75 // No need to bypass DND.
76
Dan Sandler8e032e12017-01-25 13:41:38 -050077 nm.createNotificationChannels(Arrays.asList(
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040078 alerts,
79 general,
80 storage,
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050081 createScreenshotChannel(
82 context.getString(R.string.notification_channel_screenshot),
83 nm.getNotificationChannel(SCREENSHOTS_LEGACY)),
Makoto Onuki52c62952018-03-22 10:43:03 -070084 batteryChannel,
85 hint
Beverly334bc5f2017-07-31 10:37:17 -040086 ));
87
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050088 // Delete older SS channel if present.
89 // Screenshots promoted to heads-up in P, this cleans up the lower priority channel from O.
90 // This line can be deleted in Q.
91 nm.deleteNotificationChannel(SCREENSHOTS_LEGACY);
92
93
Jaewan Kim26c63562017-04-26 15:41:43 +090094 if (isTv(context)) {
95 // TV specific notification channel for TV PIP controls.
96 // Importance should be {@link NotificationManager#IMPORTANCE_MAX} to have the highest
97 // priority, so it can be shown in all times.
98 nm.createNotificationChannel(new NotificationChannel(
99 TVPIP,
100 context.getString(R.string.notification_channel_tv_pip),
101 NotificationManager.IMPORTANCE_MAX));
102 }
Dan Sandler8e032e12017-01-25 13:41:38 -0500103 }
104
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500105 /**
106 * Set up screenshot channel, respecting any previously committed user settings on legacy
107 * channel.
108 * @return
109 */
110 @VisibleForTesting static NotificationChannel createScreenshotChannel(
111 String name, NotificationChannel legacySS) {
112 NotificationChannel screenshotChannel = new NotificationChannel(SCREENSHOTS_HEADSUP,
113 name, NotificationManager.IMPORTANCE_HIGH); // pop on screen
114
Julia Reynoldsaca215a2018-06-20 10:20:31 -0400115 screenshotChannel.setSound(null, // silent
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500116 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -0400117 screenshotChannel.setBlockableSystem(true);
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500118
119 if (legacySS != null) {
120 // Respect any user modified fields from the old channel.
121 int userlock = legacySS.getUserLockedFields();
122 if ((userlock & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0) {
123 screenshotChannel.setImportance(legacySS.getImportance());
124 }
125 if ((userlock & NotificationChannel.USER_LOCKED_SOUND) != 0) {
126 screenshotChannel.setSound(legacySS.getSound(), legacySS.getAudioAttributes());
127 }
128 if ((userlock & NotificationChannel.USER_LOCKED_VIBRATION) != 0) {
129 screenshotChannel.setVibrationPattern(legacySS.getVibrationPattern());
130 }
131 if ((userlock & NotificationChannel.USER_LOCKED_LIGHTS) != 0) {
132 screenshotChannel.setLightColor(legacySS.getLightColor());
133 }
134 // skip show_badge, irrelevant for system channel
135 }
136
137 return screenshotChannel;
138 }
139
Dan Sandler8e032e12017-01-25 13:41:38 -0500140 @Override
141 public void start() {
142 createAll(mContext);
143 }
Dmitri Plotnikov519c0882017-02-13 14:37:05 -0800144
145 private static boolean isTv(Context context) {
146 PackageManager packageManager = context.getPackageManager();
147 return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
148 }
Dan Sandler8e032e12017-01-25 13:41:38 -0500149}