• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2014 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 package com.android.server.notification;
17 
18 import android.app.Notification;
19 import android.app.NotificationManager;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.util.Slog;
24 
25 /**
26  * Determines if the given notification can display sensitive content on the lockscreen.
27  */
28 public class VisibilityExtractor implements NotificationSignalExtractor {
29     private static final String TAG = "VisibilityExtractor";
30     private static final boolean DBG = false;
31 
32     private RankingConfig mConfig;
33     private DevicePolicyManager mDpm;
34 
initialize(Context ctx, NotificationUsageStats usageStats)35     public void initialize(Context ctx, NotificationUsageStats usageStats) {
36         if (DBG) Slog.d(TAG, "Initializing  " + getClass().getSimpleName() + ".");
37         mDpm = ctx.getSystemService(DevicePolicyManager.class);
38     }
39 
process(NotificationRecord record)40     public RankingReconsideration process(NotificationRecord record) {
41         if (record == null || record.getNotification() == null) {
42             if (DBG) Slog.d(TAG, "skipping empty notification");
43             return null;
44         }
45 
46         if (mConfig == null) {
47             if (DBG) Slog.d(TAG, "missing config");
48             return null;
49         }
50 
51         int userId = record.getUserId();
52 
53         if (userId == UserHandle.USER_ALL) {
54             record.setPackageVisibilityOverride(record.getChannel().getLockscreenVisibility());
55         } else {
56             boolean userCanShowNotifications =
57                     mConfig.canShowNotificationsOnLockscreen(userId);
58             boolean dpmCanShowNotifications = adminAllowsKeyguardFeature(userId,
59                     DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
60             boolean channelCanShowNotifications = record.getChannel().getLockscreenVisibility()
61                     != Notification.VISIBILITY_SECRET;
62 
63             if (!userCanShowNotifications || !dpmCanShowNotifications
64                     || !channelCanShowNotifications) {
65                 record.setPackageVisibilityOverride(Notification.VISIBILITY_SECRET);
66             } else {
67                 // notifications are allowed but should they be redacted?
68 
69                 boolean userCanShowContents =
70                         mConfig.canShowPrivateNotificationsOnLockScreen(userId);
71                 boolean dpmCanShowContents = adminAllowsKeyguardFeature(userId,
72                         DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
73                 boolean channelCanShowContents = record.getChannel().getLockscreenVisibility()
74                         != Notification.VISIBILITY_PRIVATE;
75 
76                 if (!userCanShowContents || !dpmCanShowContents || !channelCanShowContents) {
77                     record.setPackageVisibilityOverride(Notification.VISIBILITY_PRIVATE);
78                 } else {
79                     record.setPackageVisibilityOverride(NotificationManager.VISIBILITY_NO_OVERRIDE);
80                 }
81             }
82         }
83 
84         return null;
85     }
86 
87     @Override
setConfig(RankingConfig config)88     public void setConfig(RankingConfig config) {
89         mConfig = config;
90     }
91 
92     @Override
setZenHelper(ZenModeHelper helper)93     public void setZenHelper(ZenModeHelper helper) {
94 
95     }
96 
adminAllowsKeyguardFeature(int userHandle, int feature)97     private boolean adminAllowsKeyguardFeature(int userHandle, int feature) {
98         if (userHandle == UserHandle.USER_ALL) {
99             return true;
100         }
101         final int dpmFlags = mDpm.getKeyguardDisabledFeatures(null /* admin */, userHandle);
102         return (dpmFlags & feature) == 0;
103     }
104 
105 }
106