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.model;
18 
19 import static android.view.Display.DEFAULT_DISPLAY;
20 
21 import android.annotation.NonNull;
22 import android.util.Log;
23 
24 import com.android.systemui.Dumpable;
25 import com.android.systemui.shared.system.QuickStepContract;
26 
27 import java.io.FileDescriptor;
28 import java.io.PrintWriter;
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 import javax.inject.Singleton;
33 
34 /**
35  * Contains sysUi state flags and notifies registered
36  * listeners whenever changes happen.
37  */
38 @Singleton
39 public class SysUiState implements Dumpable {
40 
41     private static final String TAG = SysUiState.class.getSimpleName();
42     public static final boolean DEBUG = false;
43 
44     private @QuickStepContract.SystemUiStateFlags int mFlags;
45     private final List<SysUiStateCallback> mCallbacks = new ArrayList<>();
46     private int mFlagsToSet = 0;
47     private int mFlagsToClear = 0;
48 
49     /**
50      * Add listener to be notified of changes made to SysUI state.
51      * The callback will also be called as part of this function.
52      */
addCallback(@onNull SysUiStateCallback callback)53     public void addCallback(@NonNull SysUiStateCallback callback) {
54         mCallbacks.add(callback);
55         callback.onSystemUiStateChanged(mFlags);
56     }
57 
58     /** Callback will no longer receive events on state change */
removeCallback(@onNull SysUiStateCallback callback)59     public void removeCallback(@NonNull SysUiStateCallback callback) {
60         mCallbacks.remove(callback);
61     }
62 
63     /** Returns the current sysui state flags. */
getFlags()64     public int getFlags() {
65         return mFlags;
66     }
67 
68     /** Methods to this call can be chained together before calling {@link #commitUpdate(int)}. */
setFlag(int flag, boolean enabled)69     public SysUiState setFlag(int flag, boolean enabled) {
70         if (enabled) {
71             mFlagsToSet |= flag;
72         } else {
73             mFlagsToClear |= flag;
74         }
75         return this;
76     }
77 
78     /** Call to save all the flags updated from {@link #setFlag(int, boolean)}. */
commitUpdate(int displayId)79     public void commitUpdate(int displayId) {
80         updateFlags(displayId);
81         mFlagsToSet = 0;
82         mFlagsToClear = 0;
83     }
84 
updateFlags(int displayId)85     private void updateFlags(int displayId) {
86         if (displayId != DEFAULT_DISPLAY) {
87             // Ignore non-default displays for now
88             Log.w(TAG, "Ignoring flag update for display: " + displayId, new Throwable());
89             return;
90         }
91 
92         int newState = mFlags;
93         newState |= mFlagsToSet;
94         newState &= ~mFlagsToClear;
95         notifyAndSetSystemUiStateChanged(newState, mFlags);
96     }
97 
98     /** Notify all those who are registered that the state has changed. */
notifyAndSetSystemUiStateChanged(int newFlags, int oldFlags)99     private void notifyAndSetSystemUiStateChanged(int newFlags, int oldFlags) {
100         if (DEBUG) {
101             Log.d(TAG, "SysUiState changed: old=" + oldFlags + " new=" + newFlags);
102         }
103         if (newFlags != oldFlags) {
104             mCallbacks.forEach(callback -> callback.onSystemUiStateChanged(newFlags));
105             mFlags = newFlags;
106         }
107     }
108 
109     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)110     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
111         pw.println("SysUiState state:");
112         pw.print("  mSysUiStateFlags="); pw.println(mFlags);
113         pw.println("    " + QuickStepContract.getSystemUiStateString(mFlags));
114         pw.print("    backGestureDisabled=");
115         pw.println(QuickStepContract.isBackGestureDisabled(mFlags));
116         pw.print("    assistantGestureDisabled=");
117         pw.println(QuickStepContract.isAssistantGestureDisabled(mFlags));
118     }
119 
120     /** Callback to be notified whenever system UI state flags are changed. */
121     public interface SysUiStateCallback{
122         /** To be called when any SysUiStateFlag gets updated */
onSystemUiStateChanged(@uickStepContract.SystemUiStateFlags int sysUiFlags)123         void onSystemUiStateChanged(@QuickStepContract.SystemUiStateFlags int sysUiFlags);
124     }
125 }
126 
127 
128