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