Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 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.systemui.util; |
| 16 | |
| 17 | import android.app.NotificationChannel; |
| 18 | import android.app.NotificationManager; |
| 19 | |
| 20 | import android.content.Context; |
Dmitri Plotnikov | 519c088 | 2017-02-13 14:37:05 -0800 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 22 | import android.media.AudioAttributes; |
| 23 | import android.net.Uri; |
| 24 | import android.provider.Settings; |
| 25 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | import com.android.systemui.R; |
| 28 | import com.android.systemui.SystemUI; |
| 29 | |
| 30 | import java.util.Arrays; |
| 31 | |
| 32 | public class NotificationChannels extends SystemUI { |
| 33 | public static String ALERTS = "ALR"; |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 34 | public static String SCREENSHOTS_LEGACY = "SCN"; |
| 35 | public static String SCREENSHOTS_HEADSUP = "SCN_HEADSUP"; |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 36 | public static String GENERAL = "GEN"; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 37 | public static String STORAGE = "DSK"; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 38 | public static String TVPIP = "TPP"; |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 39 | public static String BATTERY = "BAT"; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 40 | public static String HINTS = "HNT"; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 41 | |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 42 | public static void createAll(Context context) { |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 43 | final NotificationManager nm = context.getSystemService(NotificationManager.class); |
Julia Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 44 | final NotificationChannel batteryChannel = new NotificationChannel(BATTERY, |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 45 | 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 Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 53 | batteryChannel.setBlockableSystem(true); |
Julia Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 54 | |
| 55 | final NotificationChannel alerts = new NotificationChannel( |
| 56 | ALERTS, |
| 57 | context.getString(R.string.notification_channel_alerts), |
| 58 | NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 59 | |
| 60 | final NotificationChannel general = new NotificationChannel( |
| 61 | GENERAL, |
| 62 | context.getString(R.string.notification_channel_general), |
| 63 | NotificationManager.IMPORTANCE_MIN); |
Julia Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 64 | |
| 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); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 71 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 72 | 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 Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 78 | nm.createNotificationChannels(Arrays.asList( |
Julia Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 79 | alerts, |
| 80 | general, |
| 81 | storage, |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 82 | createScreenshotChannel( |
| 83 | context.getString(R.string.notification_channel_screenshot), |
| 84 | nm.getNotificationChannel(SCREENSHOTS_LEGACY)), |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 85 | batteryChannel, |
| 86 | hint |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 87 | )); |
| 88 | |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 89 | // 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 Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 95 | 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 Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 106 | /** |
| 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 Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 118 | screenshotChannel.setBlockableSystem(true); |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 119 | |
| 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 Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 141 | @Override |
| 142 | public void start() { |
| 143 | createAll(mContext); |
| 144 | } |
Dmitri Plotnikov | 519c088 | 2017-02-13 14:37:05 -0800 | [diff] [blame] | 145 | |
| 146 | private static boolean isTv(Context context) { |
| 147 | PackageManager packageManager = context.getPackageManager(); |
| 148 | return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK); |
| 149 | } |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 150 | } |