• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2021 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 android.view;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.NonNull;
21 import android.os.Binder;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.os.RemoteException;
25 import android.os.SystemProperties;
26 import android.util.ArrayMap;
27 import android.util.Log;
28 
29 import com.android.internal.util.Preconditions;
30 
31 import java.util.concurrent.Executor;
32 import java.util.function.Consumer;
33 
34 /**
35  * Class that holds all registered {@link CrossWindowBlurEnabledListener}s. It listens
36  * for updates from the WindowManagerService and updates all registered listeners.
37  * @hide
38  */
39 public final class CrossWindowBlurListeners {
40     private static final String TAG = "CrossWindowBlurListeners";
41 
42     // property for background blur support in surface flinger
43     private static final String BLUR_PROPERTY = "ro.surface_flinger.supports_background_blur";
44     public static final boolean CROSS_WINDOW_BLUR_SUPPORTED =
45             SystemProperties.getBoolean(BLUR_PROPERTY, false);
46 
47     private static volatile CrossWindowBlurListeners sInstance;
48     private static final Object sLock = new Object();
49 
50     private final BlurEnabledListenerInternal mListenerInternal = new BlurEnabledListenerInternal();
51     private final ArrayMap<Consumer<Boolean>, Executor> mListeners = new ArrayMap();
52     private final Handler mMainHandler = new Handler(Looper.getMainLooper());
53     private boolean mInternalListenerAttached = false;
54     private boolean mCrossWindowBlurEnabled;
55 
CrossWindowBlurListeners()56     private CrossWindowBlurListeners() {}
57 
58     /**
59      * Returns a CrossWindowBlurListeners instance
60      */
getInstance()61     public static CrossWindowBlurListeners getInstance() {
62         CrossWindowBlurListeners instance = sInstance;
63         if (instance == null) {
64 
65             synchronized (sLock) {
66                 instance = sInstance;
67                 if (instance == null) {
68                     instance = new CrossWindowBlurListeners();
69                     sInstance = instance;
70                 }
71             }
72         }
73         return instance;
74     }
75 
isCrossWindowBlurEnabled()76     public boolean isCrossWindowBlurEnabled() {
77         synchronized (sLock) {
78             attachInternalListenerIfNeededLocked();
79             return mCrossWindowBlurEnabled;
80         }
81     }
82 
addListener(@onNull @allbackExecutor Executor executor, @NonNull Consumer<Boolean> listener)83     public void addListener(@NonNull @CallbackExecutor Executor executor,
84             @NonNull Consumer<Boolean> listener) {
85         Preconditions.checkNotNull(listener, "listener cannot be null");
86         Preconditions.checkNotNull(executor, "executor cannot be null");
87 
88         synchronized (sLock) {
89             attachInternalListenerIfNeededLocked();
90 
91             mListeners.put(listener, executor);
92             notifyListener(listener, executor, mCrossWindowBlurEnabled);
93         }
94     }
95 
96 
removeListener(Consumer<Boolean> listener)97     public void removeListener(Consumer<Boolean> listener) {
98         Preconditions.checkNotNull(listener, "listener cannot be null");
99 
100         synchronized (sLock) {
101             mListeners.remove(listener);
102 
103             if (mInternalListenerAttached && mListeners.size() == 0) {
104                 try {
105                     WindowManagerGlobal.getWindowManagerService()
106                             .unregisterCrossWindowBlurEnabledListener(mListenerInternal);
107                     mInternalListenerAttached = false;
108                 } catch (RemoteException e) {
109                     Log.d(TAG, "Could not unregister ICrossWindowBlurEnabledListener");
110                 }
111             }
112         }
113     }
114 
attachInternalListenerIfNeededLocked()115     private void attachInternalListenerIfNeededLocked() {
116         if (!mInternalListenerAttached) {
117             try {
118                 mCrossWindowBlurEnabled = WindowManagerGlobal.getWindowManagerService()
119                         .registerCrossWindowBlurEnabledListener(mListenerInternal);
120                 mInternalListenerAttached = true;
121             } catch (RemoteException e) {
122                 Log.d(TAG, "Could not register ICrossWindowBlurEnabledListener");
123             }
124         }
125     }
126 
notifyListener(Consumer<Boolean> listener, Executor executor, boolean enabled)127     private void notifyListener(Consumer<Boolean> listener, Executor executor, boolean enabled) {
128         executor.execute(() -> listener.accept(enabled));
129     }
130 
131     private final class BlurEnabledListenerInternal extends ICrossWindowBlurEnabledListener.Stub {
132         @Override
onCrossWindowBlurEnabledChanged(boolean enabled)133         public void onCrossWindowBlurEnabledChanged(boolean enabled) {
134             synchronized (sLock) {
135                 mCrossWindowBlurEnabled = enabled;
136 
137                 final long token = Binder.clearCallingIdentity();
138                 try {
139                     for (int i = 0; i < mListeners.size(); i++) {
140                         notifyListener(mListeners.keyAt(i), mListeners.valueAt(i), enabled);
141                     }
142                 } finally {
143                     Binder.restoreCallingIdentity(token);
144                 }
145             }
146         }
147     }
148 }
149