blob: 8c60747dffc7a0d9b6ecf8b6248d4351d18a01d4 [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
Dave Mankoffa5d8a392019-10-10 12:21:09 -040041 public NotificationChannels(Context context) {
42 super(context);
43 }
44
Beverly70dcd002018-03-29 17:09:16 -040045 public static void createAll(Context context) {
Dan Sandler8e032e12017-01-25 13:41:38 -050046 final NotificationManager nm = context.getSystemService(NotificationManager.class);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040047 final NotificationChannel batteryChannel = new NotificationChannel(BATTERY,
Beverly334bc5f2017-07-31 10:37:17 -040048 context.getString(R.string.notification_channel_battery),
49 NotificationManager.IMPORTANCE_MAX);
50 final String soundPath = Settings.Global.getString(context.getContentResolver(),
51 Settings.Global.LOW_BATTERY_SOUND);
52 batteryChannel.setSound(Uri.parse("file://" + soundPath), new AudioAttributes.Builder()
53 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
54 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
55 .build());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040056 batteryChannel.setBlockableSystem(true);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040057
58 final NotificationChannel alerts = new NotificationChannel(
59 ALERTS,
60 context.getString(R.string.notification_channel_alerts),
61 NotificationManager.IMPORTANCE_HIGH);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040062
63 final NotificationChannel general = new NotificationChannel(
64 GENERAL,
65 context.getString(R.string.notification_channel_general),
66 NotificationManager.IMPORTANCE_MIN);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040067
68 final NotificationChannel storage = new NotificationChannel(
69 STORAGE,
70 context.getString(R.string.notification_channel_storage),
71 isTv(context)
72 ? NotificationManager.IMPORTANCE_DEFAULT
73 : NotificationManager.IMPORTANCE_LOW);
Beverly334bc5f2017-07-31 10:37:17 -040074
Makoto Onuki52c62952018-03-22 10:43:03 -070075 final NotificationChannel hint = new NotificationChannel(
76 HINTS,
77 context.getString(R.string.notification_channel_hints),
78 NotificationManager.IMPORTANCE_DEFAULT);
79 // No need to bypass DND.
80
Dan Sandler8e032e12017-01-25 13:41:38 -050081 nm.createNotificationChannels(Arrays.asList(
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040082 alerts,
83 general,
84 storage,
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050085 createScreenshotChannel(
86 context.getString(R.string.notification_channel_screenshot),
87 nm.getNotificationChannel(SCREENSHOTS_LEGACY)),
Makoto Onuki52c62952018-03-22 10:43:03 -070088 batteryChannel,
89 hint
Beverly334bc5f2017-07-31 10:37:17 -040090 ));
91
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050092 // Delete older SS channel if present.
93 // Screenshots promoted to heads-up in P, this cleans up the lower priority channel from O.
94 // This line can be deleted in Q.
95 nm.deleteNotificationChannel(SCREENSHOTS_LEGACY);
96
97
Jaewan Kim26c63562017-04-26 15:41:43 +090098 if (isTv(context)) {
99 // TV specific notification channel for TV PIP controls.
100 // Importance should be {@link NotificationManager#IMPORTANCE_MAX} to have the highest
101 // priority, so it can be shown in all times.
102 nm.createNotificationChannel(new NotificationChannel(
103 TVPIP,
104 context.getString(R.string.notification_channel_tv_pip),
105 NotificationManager.IMPORTANCE_MAX));
106 }
Dan Sandler8e032e12017-01-25 13:41:38 -0500107 }
108
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500109 /**
110 * Set up screenshot channel, respecting any previously committed user settings on legacy
111 * channel.
112 * @return
113 */
114 @VisibleForTesting static NotificationChannel createScreenshotChannel(
115 String name, NotificationChannel legacySS) {
116 NotificationChannel screenshotChannel = new NotificationChannel(SCREENSHOTS_HEADSUP,
117 name, NotificationManager.IMPORTANCE_HIGH); // pop on screen
118
Julia Reynoldsaca215a2018-06-20 10:20:31 -0400119 screenshotChannel.setSound(null, // silent
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500120 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -0400121 screenshotChannel.setBlockableSystem(true);
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500122
123 if (legacySS != null) {
124 // Respect any user modified fields from the old channel.
125 int userlock = legacySS.getUserLockedFields();
126 if ((userlock & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0) {
127 screenshotChannel.setImportance(legacySS.getImportance());
128 }
129 if ((userlock & NotificationChannel.USER_LOCKED_SOUND) != 0) {
130 screenshotChannel.setSound(legacySS.getSound(), legacySS.getAudioAttributes());
131 }
132 if ((userlock & NotificationChannel.USER_LOCKED_VIBRATION) != 0) {
133 screenshotChannel.setVibrationPattern(legacySS.getVibrationPattern());
134 }
135 if ((userlock & NotificationChannel.USER_LOCKED_LIGHTS) != 0) {
136 screenshotChannel.setLightColor(legacySS.getLightColor());
137 }
138 // skip show_badge, irrelevant for system channel
139 }
140
141 return screenshotChannel;
142 }
143
Dan Sandler8e032e12017-01-25 13:41:38 -0500144 @Override
145 public void start() {
146 createAll(mContext);
147 }
Dmitri Plotnikov519c0882017-02-13 14:37:05 -0800148
149 private static boolean isTv(Context context) {
150 PackageManager packageManager = context.getPackageManager();
151 return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
152 }
Dan Sandler8e032e12017-01-25 13:41:38 -0500153}