1 /*
2  * Copyright (C) 2019 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.systemui.statusbar.notification;
18 
19 import android.content.Context;
20 import android.util.ArraySet;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.systemui.statusbar.NotificationLockscreenUserManager;
24 import com.android.systemui.statusbar.phone.UnlockMethodCache;
25 
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 
29 /**
30  * A controller which dynamically controls the visibility of Notification content
31  */
32 @Singleton
33 public class DynamicPrivacyController implements UnlockMethodCache.OnUnlockMethodChangedListener {
34 
35     private final UnlockMethodCache mUnlockMethodCache;
36     private final NotificationLockscreenUserManager mLockscreenUserManager;
37     private ArraySet<Listener> mListeners = new ArraySet<>();
38 
39     private boolean mLastDynamicUnlocked;
40     private boolean mCacheInvalid;
41 
42     @Inject
DynamicPrivacyController(Context context, NotificationLockscreenUserManager notificationLockscreenUserManager)43     DynamicPrivacyController(Context context,
44             NotificationLockscreenUserManager notificationLockscreenUserManager) {
45         this(notificationLockscreenUserManager, UnlockMethodCache.getInstance(context));
46     }
47 
48     @VisibleForTesting
DynamicPrivacyController(NotificationLockscreenUserManager notificationLockscreenUserManager, UnlockMethodCache unlockMethodCache)49     DynamicPrivacyController(NotificationLockscreenUserManager notificationLockscreenUserManager,
50             UnlockMethodCache unlockMethodCache) {
51         mLockscreenUserManager = notificationLockscreenUserManager;
52         mUnlockMethodCache = unlockMethodCache;
53         mUnlockMethodCache.addListener(this);
54         mLastDynamicUnlocked = isDynamicallyUnlocked();
55     }
56 
57     @Override
onUnlockMethodStateChanged()58     public void onUnlockMethodStateChanged() {
59         if (isDynamicPrivacyEnabled()) {
60             // We only want to notify our listeners if dynamic privacy is actually active
61             boolean dynamicallyUnlocked = isDynamicallyUnlocked();
62             if (dynamicallyUnlocked != mLastDynamicUnlocked || mCacheInvalid) {
63                 mLastDynamicUnlocked = dynamicallyUnlocked;
64                 for (Listener listener : mListeners) {
65                     listener.onDynamicPrivacyChanged();
66                 }
67             }
68             mCacheInvalid = false;
69         } else {
70             mCacheInvalid = true;
71         }
72     }
73 
isDynamicPrivacyEnabled()74     private boolean isDynamicPrivacyEnabled() {
75         return !mLockscreenUserManager.shouldHideNotifications(
76                 mLockscreenUserManager.getCurrentUserId());
77     }
78 
isDynamicallyUnlocked()79     public boolean isDynamicallyUnlocked() {
80         return mUnlockMethodCache.canSkipBouncer() && isDynamicPrivacyEnabled();
81     }
82 
addListener(Listener listener)83     public void addListener(Listener listener) {
84         mListeners.add(listener);
85     }
86 
87     public interface Listener {
onDynamicPrivacyChanged()88         void onDynamicPrivacyChanged();
89     }
90 }