Home | History | Annotate | Download | only in util
      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;
     21 import android.content.pm.PackageManager;
     22 import android.media.AudioAttributes;
     23 import android.net.Uri;
     24 import android.provider.Settings;
     25 
     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";
     34     public static String SCREENSHOTS_LEGACY = "SCN";
     35     public static String SCREENSHOTS_HEADSUP = "SCN_HEADSUP";
     36     public static String GENERAL     = "GEN";
     37     public static String STORAGE     = "DSK";
     38     public static String TVPIP       = "TPP";
     39     public static String BATTERY     = "BAT";
     40     public static String HINTS       = "HNT";
     41 
     42     public static void createAll(Context context) {
     43         final NotificationManager nm = context.getSystemService(NotificationManager.class);
     44         final NotificationChannel batteryChannel = new NotificationChannel(BATTERY,
     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());
     53         batteryChannel.setBlockableSystem(true);
     54 
     55         final NotificationChannel alerts = new NotificationChannel(
     56                 ALERTS,
     57                 context.getString(R.string.notification_channel_alerts),
     58                 NotificationManager.IMPORTANCE_HIGH);
     59 
     60         final NotificationChannel general = new NotificationChannel(
     61                 GENERAL,
     62                 context.getString(R.string.notification_channel_general),
     63                 NotificationManager.IMPORTANCE_MIN);
     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);
     71 
     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 
     78         nm.createNotificationChannels(Arrays.asList(
     79                 alerts,
     80                 general,
     81                 storage,
     82                 createScreenshotChannel(
     83                         context.getString(R.string.notification_channel_screenshot),
     84                         nm.getNotificationChannel(SCREENSHOTS_LEGACY)),
     85                 batteryChannel,
     86                 hint
     87         ));
     88 
     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 
     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         }
    104     }
    105 
    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());
    118         screenshotChannel.setBlockableSystem(true);
    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 
    141     @Override
    142     public void start() {
    143         createAll(mContext);
    144     }
    145 
    146     private static boolean isTv(Context context) {
    147         PackageManager packageManager = context.getPackageManager();
    148         return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
    149     }
    150 }
    151