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