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 com.android.wm.shell.onehanded; 18 19 import android.annotation.IntDef; 20 21 import java.io.PrintWriter; 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 Represents current OHM state by following steps, a generic CUJ is 29 STATE_NONE -> STATE_ENTERING -> STATE_ACTIVE -> STATE_EXITING -> STATE_NONE 30 */ 31 public class OneHandedState { 32 /** DEFAULT STATE after OHM feature initialized. */ 33 public static final int STATE_NONE = 0; 34 /** The state flag set when user trigger OHM. */ 35 public static final int STATE_ENTERING = 1; 36 /** The state flag set when transitioning */ 37 public static final int STATE_ACTIVE = 2; 38 /** The state flag set when user stop OHM feature. */ 39 public static final int STATE_EXITING = 3; 40 41 @IntDef(prefix = { "STATE_" }, value = { 42 STATE_NONE, 43 STATE_ENTERING, 44 STATE_ACTIVE, 45 STATE_EXITING 46 }) 47 @Retention(RetentionPolicy.SOURCE) 48 @interface State {} 49 OneHandedState()50 public OneHandedState() { 51 sCurrentState = STATE_NONE; 52 } 53 54 @State 55 private static int sCurrentState = STATE_NONE; 56 57 private static final String TAG = OneHandedState.class.getSimpleName(); 58 59 private List<OnStateChangedListener> mStateChangeListeners = new ArrayList<>(); 60 61 /** 62 * Adds listener to be called back when one handed state changed. 63 * @param listener the listener to be called back 64 */ addSListeners(OnStateChangedListener listener)65 public void addSListeners(OnStateChangedListener listener) { 66 mStateChangeListeners.add(listener); 67 } 68 69 /** 70 * Gets current transition state of One handed mode. 71 * @return The bitwise flags representing current states. 72 */ getState()73 public @State int getState() { 74 return sCurrentState; 75 } 76 77 /** 78 * Is the One handed mode is in transitioning state. 79 * @return true if One handed mode is in transitioning states. 80 */ isTransitioning()81 public boolean isTransitioning() { 82 return sCurrentState == STATE_ENTERING || sCurrentState == STATE_EXITING; 83 } 84 85 /** 86 * Is the One handed mode active state. 87 * @return true if One handed mode is active state. 88 */ isInOneHanded()89 public boolean isInOneHanded() { 90 return sCurrentState == STATE_ACTIVE; 91 } 92 93 /** 94 * Sets new state for One handed mode feature. 95 * @param newState The bitwise value to represent current transition states. 96 */ setState(@tate int newState)97 public void setState(@State int newState) { 98 sCurrentState = newState; 99 if (!mStateChangeListeners.isEmpty()) { 100 mStateChangeListeners.forEach((listener) -> listener.onStateChanged(newState)); 101 } 102 } 103 104 /** Dumps internal state. */ dump(PrintWriter pw)105 public void dump(PrintWriter pw) { 106 final String innerPrefix = " "; 107 pw.println(TAG); 108 pw.println(innerPrefix + "sCurrentState=" + sCurrentState); 109 } 110 111 /** 112 * Gets notified when one handed state changed 113 * 114 * @see OneHandedState 115 */ 116 public interface OnStateChangedListener { 117 /** Called when one handed state changed */ onStateChanged(@tate int newState)118 default void onStateChanged(@State int newState) {} 119 } 120 } 121