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 import com.android.wm.shell.pip.tv.TvPipNotificationController;
29 
30 import java.util.Arrays;
31 
32 public class NotificationChannels extends SystemUI {
33     public static String ALERTS      = "ALR";
34     public static String SCREENSHOTS_HEADSUP = "SCN_HEADSUP";
35     public static String GENERAL     = "GEN";
36     public static String STORAGE     = "DSK";
37     public static String BATTERY     = "BAT";
38     public static String TVPIP       = TvPipNotificationController.NOTIFICATION_CHANNEL; // "TVPIP"
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                 batteryChannel,
88                 hint
89         ));
90 
91         if (isTv(context)) {
92             // TV specific notification channel for TV PIP controls.
93             // Importance should be {@link NotificationManager#IMPORTANCE_MAX} to have the highest
94             // priority, so it can be shown in all times.
95             nm.createNotificationChannel(new NotificationChannel(
96                     TVPIP,
97                     context.getString(R.string.notification_channel_tv_pip),
98                     NotificationManager.IMPORTANCE_MAX));
99         }
100     }
101 
102     /**
103      * Set up screenshot channel, respecting any previously committed user settings on legacy
104      * channel.
105      * @return
106      */
createScreenshotChannel( String name)107     @VisibleForTesting static NotificationChannel createScreenshotChannel(
108             String name) {
109         NotificationChannel screenshotChannel = new NotificationChannel(SCREENSHOTS_HEADSUP,
110                 name, NotificationManager.IMPORTANCE_HIGH); // pop on screen
111 
112         screenshotChannel.setSound(null, // silent
113                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
114         screenshotChannel.setBlockable(true);
115 
116         return screenshotChannel;
117     }
118 
119     @Override
start()120     public void start() {
121         createAll(mContext);
122     }
123 
isTv(Context context)124     private static boolean isTv(Context context) {
125         PackageManager packageManager = context.getPackageManager();
126         return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
127     }
128 }
129