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 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 
     41     public static void createAll(Context context) {
     42         final NotificationManager nm = context.getSystemService(NotificationManager.class);
     43         final NotificationChannel batteryChannel = new NotificationChannel(BATTERY,
     44                 context.getString(R.string.notification_channel_battery),
     45                 NotificationManager.IMPORTANCE_MAX);
     46         final String soundPath = Settings.Global.getString(context.getContentResolver(),
     47                 Settings.Global.LOW_BATTERY_SOUND);
     48         batteryChannel.setSound(Uri.parse("file://" + soundPath), new AudioAttributes.Builder()
     49                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
     50                 .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
     51                 .build());
     52         batteryChannel.setBlockableSystem(true);
     53 
     54         final NotificationChannel alerts = new NotificationChannel(
     55                 ALERTS,
     56                 context.getString(R.string.notification_channel_alerts),
     57                 NotificationManager.IMPORTANCE_HIGH);
     58 
     59         final NotificationChannel general = new NotificationChannel(
     60                 GENERAL,
     61                 context.getString(R.string.notification_channel_general),
     62                 NotificationManager.IMPORTANCE_MIN);
     63 
     64         final NotificationChannel storage = new NotificationChannel(
     65                 STORAGE,
     66                 context.getString(R.string.notification_channel_storage),
     67                 isTv(context)
     68                         ? NotificationManager.IMPORTANCE_DEFAULT
     69                         : NotificationManager.IMPORTANCE_LOW);
     70 
     71         final NotificationChannel hint = new NotificationChannel(
     72                 HINTS,
     73                 context.getString(R.string.notification_channel_hints),
     74                 NotificationManager.IMPORTANCE_DEFAULT);
     75         // No need to bypass DND.
     76 
     77         nm.createNotificationChannels(Arrays.asList(
     78                 alerts,
     79                 general,
     80                 storage,
     81                 createScreenshotChannel(
     82                         context.getString(R.string.notification_channel_screenshot),
     83                         nm.getNotificationChannel(SCREENSHOTS_LEGACY)),
     84                 batteryChannel,
     85                 hint
     86         ));
     87 
     88         // Delete older SS channel if present.
     89         // Screenshots promoted to heads-up in P, this cleans up the lower priority channel from O.
     90         // This line can be deleted in Q.
     91         nm.deleteNotificationChannel(SCREENSHOTS_LEGACY);
     92 
     93 
     94         if (isTv(context)) {
     95             // TV specific notification channel for TV PIP controls.
     96             // Importance should be {@link NotificationManager#IMPORTANCE_MAX} to have the highest
     97             // priority, so it can be shown in all times.
     98             nm.createNotificationChannel(new NotificationChannel(
     99                     TVPIP,
    100                     context.getString(R.string.notification_channel_tv_pip),
    101                     NotificationManager.IMPORTANCE_MAX));
    102         }
    103     }
    104 
    105     /**
    106      * Set up screenshot channel, respecting any previously committed user settings on legacy
    107      * channel.
    108      * @return
    109      */
    110     @VisibleForTesting static NotificationChannel createScreenshotChannel(
    111             String name, NotificationChannel legacySS) {
    112         NotificationChannel screenshotChannel = new NotificationChannel(SCREENSHOTS_HEADSUP,
    113                 name, NotificationManager.IMPORTANCE_HIGH); // pop on screen
    114 
    115         screenshotChannel.setSound(null, // silent
    116                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
    117         screenshotChannel.setBlockableSystem(true);
    118 
    119         if (legacySS != null) {
    120             // Respect any user modified fields from the old channel.
    121             int userlock = legacySS.getUserLockedFields();
    122             if ((userlock & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0) {
    123                 screenshotChannel.setImportance(legacySS.getImportance());
    124             }
    125             if ((userlock & NotificationChannel.USER_LOCKED_SOUND) != 0)  {
    126                 screenshotChannel.setSound(legacySS.getSound(), legacySS.getAudioAttributes());
    127             }
    128             if ((userlock & NotificationChannel.USER_LOCKED_VIBRATION) != 0)  {
    129                 screenshotChannel.setVibrationPattern(legacySS.getVibrationPattern());
    130             }
    131             if ((userlock & NotificationChannel.USER_LOCKED_LIGHTS) != 0)  {
    132                 screenshotChannel.setLightColor(legacySS.getLightColor());
    133             }
    134             // skip show_badge, irrelevant for system channel
    135         }
    136 
    137         return screenshotChannel;
    138     }
    139 
    140     @Override
    141     public void start() {
    142         createAll(mContext);
    143     }
    144 
    145     private static boolean isTv(Context context) {
    146         PackageManager packageManager = context.getPackageManager();
    147         return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
    148     }
    149 }
    150