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 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.media.AudioAttributes; 22 import android.net.Uri; 23 import android.provider.Settings; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 import com.android.systemui.R; 27 import com.android.systemui.SystemUI; 28 29 import java.util.Arrays; 30 31 public class NotificationChannels extends SystemUI { 32 public static String ALERTS = "ALR"; 33 public static String SCREENSHOTS_LEGACY = "SCN"; 34 public static String SCREENSHOTS_HEADSUP = "SCN_HEADSUP"; 35 public static String GENERAL = "GEN"; 36 public static String STORAGE = "DSK"; 37 public static String TVPIP = "TPP"; 38 public static String BATTERY = "BAT"; 39 public static String HINTS = "HNT"; 40 NotificationChannels(Context context)41 public NotificationChannels(Context context) { 42 super(context); 43 } 44 createAll(Context context)45 public static void createAll(Context context) { 46 final NotificationManager nm = context.getSystemService(NotificationManager.class); 47 final NotificationChannel batteryChannel = new NotificationChannel(BATTERY, 48 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()); 56 batteryChannel.setBlockable(true); 57 58 final NotificationChannel alerts = new NotificationChannel( 59 ALERTS, 60 context.getString(R.string.notification_channel_alerts), 61 NotificationManager.IMPORTANCE_HIGH); 62 63 final NotificationChannel general = new NotificationChannel( 64 GENERAL, 65 context.getString(R.string.notification_channel_general), 66 NotificationManager.IMPORTANCE_MIN); 67 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); 74 75 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 81 nm.createNotificationChannels(Arrays.asList( 82 alerts, 83 general, 84 storage, 85 createScreenshotChannel( 86 context.getString(R.string.notification_channel_screenshot), 87 nm.getNotificationChannel(SCREENSHOTS_LEGACY)), 88 batteryChannel, 89 hint 90 )); 91 92 // 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 98 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 } 107 } 108 109 /** 110 * Set up screenshot channel, respecting any previously committed user settings on legacy 111 * channel. 112 * @return 113 */ createScreenshotChannel( String name, NotificationChannel legacySS)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 119 screenshotChannel.setSound(null, // silent 120 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()); 121 screenshotChannel.setBlockable(true); 122 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 144 @Override start()145 public void start() { 146 createAll(mContext); 147 } 148 isTv(Context context)149 private static boolean isTv(Context context) { 150 PackageManager packageManager = context.getPackageManager(); 151 return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK); 152 } 153 } 154