blob: 67fa049d688ff7325e262b9a907893ff141651ab [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;
19
20import android.content.Context;
Dmitri Plotnikov519c0882017-02-13 14:37:05 -080021import android.content.pm.PackageManager;
Beverly334bc5f2017-07-31 10:37:17 -040022import android.media.AudioAttributes;
23import android.net.Uri;
24import android.provider.Settings;
25
Dan Sandler8e032e12017-01-25 13:41:38 -050026import com.android.internal.annotations.VisibleForTesting;
27import com.android.systemui.R;
28import com.android.systemui.SystemUI;
29
30import java.util.Arrays;
31
32public class NotificationChannels extends SystemUI {
33 public static String ALERTS = "ALR";
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050034 public static String SCREENSHOTS_LEGACY = "SCN";
35 public static String SCREENSHOTS_HEADSUP = "SCN_HEADSUP";
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -050036 public static String GENERAL = "GEN";
Dan Sandler8e032e12017-01-25 13:41:38 -050037 public static String STORAGE = "DSK";
Jaewan Kim26c63562017-04-26 15:41:43 +090038 public static String TVPIP = "TPP";
Beverly334bc5f2017-07-31 10:37:17 -040039 public static String BATTERY = "BAT";
Makoto Onuki52c62952018-03-22 10:43:03 -070040 public static String HINTS = "HNT";
Dan Sandler8e032e12017-01-25 13:41:38 -050041
Beverly70dcd002018-03-29 17:09:16 -040042 public static void createAll(Context context) {
Dan Sandler8e032e12017-01-25 13:41:38 -050043 final NotificationManager nm = context.getSystemService(NotificationManager.class);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040044 final NotificationChannel batteryChannel = new NotificationChannel(BATTERY,
Beverly334bc5f2017-07-31 10:37:17 -040045 context.getString(R.string.notification_channel_battery),
46 NotificationManager.IMPORTANCE_MAX);
47 final String soundPath = Settings.Global.getString(context.getContentResolver(),
48 Settings.Global.LOW_BATTERY_SOUND);
49 batteryChannel.setSound(Uri.parse("file://" + soundPath), new AudioAttributes.Builder()
50 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
51 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
52 .build());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040053 batteryChannel.setBlockableSystem(true);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040054
55 final NotificationChannel alerts = new NotificationChannel(
56 ALERTS,
57 context.getString(R.string.notification_channel_alerts),
58 NotificationManager.IMPORTANCE_HIGH);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040059
60 final NotificationChannel general = new NotificationChannel(
61 GENERAL,
62 context.getString(R.string.notification_channel_general),
63 NotificationManager.IMPORTANCE_MIN);
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040064
65 final NotificationChannel storage = new NotificationChannel(
66 STORAGE,
67 context.getString(R.string.notification_channel_storage),
68 isTv(context)
69 ? NotificationManager.IMPORTANCE_DEFAULT
70 : NotificationManager.IMPORTANCE_LOW);
Beverly334bc5f2017-07-31 10:37:17 -040071
Makoto Onuki52c62952018-03-22 10:43:03 -070072 final NotificationChannel hint = new NotificationChannel(
73 HINTS,
74 context.getString(R.string.notification_channel_hints),
75 NotificationManager.IMPORTANCE_DEFAULT);
76 // No need to bypass DND.
77
Dan Sandler8e032e12017-01-25 13:41:38 -050078 nm.createNotificationChannels(Arrays.asList(
Julia Reynoldsfd80cff2018-03-18 14:28:48 -040079 alerts,
80 general,
81 storage,
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050082 createScreenshotChannel(
83 context.getString(R.string.notification_channel_screenshot),
84 nm.getNotificationChannel(SCREENSHOTS_LEGACY)),
Makoto Onuki52c62952018-03-22 10:43:03 -070085 batteryChannel,
86 hint
Beverly334bc5f2017-07-31 10:37:17 -040087 ));
88
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050089 // Delete older SS channel if present.
90 // Screenshots promoted to heads-up in P, this cleans up the lower priority channel from O.
91 // This line can be deleted in Q.
92 nm.deleteNotificationChannel(SCREENSHOTS_LEGACY);
93
94
Jaewan Kim26c63562017-04-26 15:41:43 +090095 if (isTv(context)) {
96 // TV specific notification channel for TV PIP controls.
97 // Importance should be {@link NotificationManager#IMPORTANCE_MAX} to have the highest
98 // priority, so it can be shown in all times.
99 nm.createNotificationChannel(new NotificationChannel(
100 TVPIP,
101 context.getString(R.string.notification_channel_tv_pip),
102 NotificationManager.IMPORTANCE_MAX));
103 }
Dan Sandler8e032e12017-01-25 13:41:38 -0500104 }
105
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500106 /**
107 * Set up screenshot channel, respecting any previously committed user settings on legacy
108 * channel.
109 * @return
110 */
111 @VisibleForTesting static NotificationChannel createScreenshotChannel(
112 String name, NotificationChannel legacySS) {
113 NotificationChannel screenshotChannel = new NotificationChannel(SCREENSHOTS_HEADSUP,
114 name, NotificationManager.IMPORTANCE_HIGH); // pop on screen
115
116 screenshotChannel.setSound(Uri.parse(""), // silent
117 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -0400118 screenshotChannel.setBlockableSystem(true);
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500119
120 if (legacySS != null) {
121 // Respect any user modified fields from the old channel.
122 int userlock = legacySS.getUserLockedFields();
123 if ((userlock & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0) {
124 screenshotChannel.setImportance(legacySS.getImportance());
125 }
126 if ((userlock & NotificationChannel.USER_LOCKED_SOUND) != 0) {
127 screenshotChannel.setSound(legacySS.getSound(), legacySS.getAudioAttributes());
128 }
129 if ((userlock & NotificationChannel.USER_LOCKED_VIBRATION) != 0) {
130 screenshotChannel.setVibrationPattern(legacySS.getVibrationPattern());
131 }
132 if ((userlock & NotificationChannel.USER_LOCKED_LIGHTS) != 0) {
133 screenshotChannel.setLightColor(legacySS.getLightColor());
134 }
135 // skip show_badge, irrelevant for system channel
136 }
137
138 return screenshotChannel;
139 }
140
Dan Sandler8e032e12017-01-25 13:41:38 -0500141 @Override
142 public void start() {
143 createAll(mContext);
144 }
Dmitri Plotnikov519c0882017-02-13 14:37:05 -0800145
146 private static boolean isTv(Context context) {
147 PackageManager packageManager = context.getPackageManager();
148 return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
149 }
Dan Sandler8e032e12017-01-25 13:41:38 -0500150}