Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.notification;
     18 
     19 import android.app.Notification;
     20 import android.app.NotificationChannel;
     21 import android.app.NotificationManager;
     22 import android.app.admin.DevicePolicyManager;
     23 import android.content.Context;
     24 import android.content.pm.UserInfo;
     25 import android.os.UserHandle;
     26 import android.provider.Settings;
     27 import android.service.notification.NotificationListenerService;
     28 import android.support.v7.preference.Preference;
     29 
     30 import com.android.internal.widget.LockPatternUtils;
     31 import com.android.settings.R;
     32 import com.android.settings.RestrictedListPreference;
     33 import com.android.settings.core.PreferenceControllerMixin;
     34 import com.android.settingslib.RestrictedLockUtils;
     35 
     36 import java.util.ArrayList;
     37 
     38 public class VisibilityPreferenceController extends NotificationPreferenceController
     39         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
     40 
     41     private static final String TAG = "ChannelVisPrefContr";
     42     private static final String KEY_VISIBILITY_OVERRIDE = "visibility_override";
     43     private LockPatternUtils mLockPatternUtils;
     44 
     45     public VisibilityPreferenceController(Context context, LockPatternUtils utils,
     46             NotificationBackend backend) {
     47         super(context, backend);
     48         mLockPatternUtils = utils;
     49     }
     50 
     51     @Override
     52     public String getPreferenceKey() {
     53         return KEY_VISIBILITY_OVERRIDE;
     54     }
     55 
     56     @Override
     57     public boolean isAvailable() {
     58         if (!super.isAvailable()) {
     59             return false;
     60         }
     61         if (mChannel == null || mAppRow.banned) {
     62             return false;
     63         }
     64         return checkCanBeVisible(NotificationManager.IMPORTANCE_LOW) && isLockScreenSecure();
     65     }
     66 
     67     public void updateState(Preference preference) {
     68         if (mChannel != null && mAppRow != null) {
     69             RestrictedListPreference pref = (RestrictedListPreference) preference;
     70             ArrayList<CharSequence> entries = new ArrayList<>();
     71             ArrayList<CharSequence> values = new ArrayList<>();
     72 
     73             pref.clearRestrictedItems();
     74             if (getLockscreenNotificationsEnabled() && getLockscreenAllowPrivateNotifications()) {
     75                 final String summaryShowEntry =
     76                         mContext.getString(R.string.lock_screen_notifications_summary_show);
     77                 final String summaryShowEntryValue =
     78                         Integer.toString(NotificationManager.VISIBILITY_NO_OVERRIDE);
     79                 entries.add(summaryShowEntry);
     80                 values.add(summaryShowEntryValue);
     81                 setRestrictedIfNotificationFeaturesDisabled(pref, summaryShowEntry,
     82                         summaryShowEntryValue,
     83                         DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS
     84                                 | DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
     85             }
     86 
     87             if (getLockscreenNotificationsEnabled()) {
     88                 final String summaryHideEntry =
     89                         mContext.getString(R.string.lock_screen_notifications_summary_hide);
     90                 final String summaryHideEntryValue = Integer.toString(
     91                         Notification.VISIBILITY_PRIVATE);
     92                 entries.add(summaryHideEntry);
     93                 values.add(summaryHideEntryValue);
     94                 setRestrictedIfNotificationFeaturesDisabled(pref,
     95                         summaryHideEntry, summaryHideEntryValue,
     96                         DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
     97             }
     98             entries.add(mContext.getString(R.string.lock_screen_notifications_summary_disable));
     99             values.add(Integer.toString(Notification.VISIBILITY_SECRET));
    100             pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
    101             pref.setEntryValues(values.toArray(new CharSequence[values.size()]));
    102 
    103             if (mChannel.getLockscreenVisibility()
    104                     == NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) {
    105                 pref.setValue(Integer.toString(getGlobalVisibility()));
    106             } else {
    107                 pref.setValue(Integer.toString(mChannel.getLockscreenVisibility()));
    108             }
    109             pref.setSummary("%s");
    110         }
    111     }
    112 
    113     @Override
    114     public boolean onPreferenceChange(Preference preference, Object newValue) {
    115         if (mChannel != null) {
    116             int sensitive = Integer.parseInt((String) newValue);
    117             if (sensitive == getGlobalVisibility()) {
    118                 sensitive = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
    119             }
    120             mChannel.setLockscreenVisibility(sensitive);
    121             mChannel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
    122             saveChannel();
    123         }
    124         return true;
    125     }
    126 
    127     private void setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref,
    128             CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures) {
    129         RestrictedLockUtils.EnforcedAdmin admin =
    130                 RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
    131                         mContext, keyguardNotificationFeatures, mAppRow.userId);
    132         if (admin != null) {
    133             RestrictedListPreference.RestrictedItem item =
    134                     new RestrictedListPreference.RestrictedItem(entry, entryValue, admin);
    135             pref.addRestrictedItem(item);
    136         }
    137     }
    138 
    139     private int getGlobalVisibility() {
    140         int globalVis = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
    141         if (!getLockscreenNotificationsEnabled()) {
    142             globalVis = Notification.VISIBILITY_SECRET;
    143         } else if (!getLockscreenAllowPrivateNotifications()) {
    144             globalVis = Notification.VISIBILITY_PRIVATE;
    145         }
    146         return globalVis;
    147     }
    148 
    149     private boolean getLockscreenNotificationsEnabled() {
    150         final UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
    151         final int primaryUserId = parentUser != null ? parentUser.id : UserHandle.myUserId();
    152         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
    153                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, primaryUserId) != 0;
    154     }
    155 
    156     private boolean getLockscreenAllowPrivateNotifications() {
    157         return Settings.Secure.getInt(mContext.getContentResolver(),
    158                 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0;
    159     }
    160 
    161     protected boolean isLockScreenSecure() {
    162         boolean lockscreenSecure = mLockPatternUtils.isSecure(UserHandle.myUserId());
    163         UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
    164         if (parentUser != null){
    165             lockscreenSecure |= mLockPatternUtils.isSecure(parentUser.id);
    166         }
    167 
    168         return lockscreenSecure;
    169     }
    170 
    171 }
    172