| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
public class OneHandedState { |
| 32 |
|
| 33 |
public static final int STATE_NONE = 0; |
| 34 |
|
| 35 |
public static final int STATE_ENTERING = 1; |
| 36 |
|
| 37 |
public static final int STATE_ACTIVE = 2; |
| 38 |
|
| 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 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 65 |
public void addSListeners(OnStateChangedListener listener) { |
| 66 |
mStateChangeListeners.add(listener); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public @State int getState() { |
| 74 |
return sCurrentState; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public boolean isTransitioning() { |
| 82 |
return sCurrentState == STATE_ENTERING || sCurrentState == STATE_EXITING; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public boolean isInOneHanded() { |
| 90 |
return sCurrentState == STATE_ACTIVE; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 105 |
public void dump(PrintWriter pw) { |
| 106 |
final String innerPrefix = " "; |
| 107 |
pw.println(TAG); |
| 108 |
pw.println(innerPrefix + "sCurrentState=" + sCurrentState); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public interface OnStateChangedListener { |
| 117 |
|
| 118 |
default void onStateChanged(@State int newState) {} |
| 119 |
} |
| 120 |
} |