| 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 static android.view.Display.DEFAULT_DISPLAY; |
| 20 |
|
| 21 |
import android.graphics.Rect; |
| 22 |
import android.hardware.input.InputManager; |
| 23 |
import android.os.Looper; |
| 24 |
import android.view.InputChannel; |
| 25 |
import android.view.InputEvent; |
| 26 |
import android.view.InputEventReceiver; |
| 27 |
import android.view.InputMonitor; |
| 28 |
import android.view.MotionEvent; |
| 29 |
|
| 30 |
import androidx.annotation.NonNull; |
| 31 |
import androidx.annotation.VisibleForTesting; |
| 32 |
|
| 33 |
import com.android.wm.shell.common.ShellExecutor; |
| 34 |
|
| 35 |
import java.io.PrintWriter; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class OneHandedTouchHandler implements OneHandedTransitionCallback { |
| 42 |
private static final String TAG = "OneHandedTouchHandler"; |
| 43 |
private final Rect mLastUpdatedBounds = new Rect(); |
| 44 |
|
| 45 |
private final OneHandedTimeoutHandler mTimeoutHandler; |
| 46 |
private final ShellExecutor mMainExecutor; |
| 47 |
|
| 48 |
@VisibleForTesting |
| 49 |
InputMonitor mInputMonitor; |
| 50 |
@VisibleForTesting |
| 51 |
InputEventReceiver mInputEventReceiver; |
| 52 |
@VisibleForTesting |
| 53 |
OneHandedTouchEventCallback mTouchEventCallback; |
| 54 |
|
| 55 |
private boolean mIsEnabled; |
| 56 |
private boolean mIsOnStopTransitioning; |
| 57 |
private boolean mIsInOutsideRegion; |
| 58 |
|
| 59 |
public OneHandedTouchHandler(OneHandedTimeoutHandler timeoutHandler, |
| 60 |
ShellExecutor mainExecutor) { |
| 61 |
mTimeoutHandler = timeoutHandler; |
| 62 |
mMainExecutor = mainExecutor; |
| 63 |
updateIsEnabled(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public void onOneHandedEnabled(boolean isEnabled) { |
| 72 |
mIsEnabled = isEnabled; |
| 73 |
updateIsEnabled(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void registerTouchEventListener(OneHandedTouchEventCallback callback) { |
| 80 |
mTouchEventCallback = callback; |
| 81 |
} |
| 82 |
|
| 83 |
private boolean onMotionEvent(MotionEvent ev) { |
| 84 |
mIsInOutsideRegion = isWithinTouchOutsideRegion(ev.getX(), ev.getY()); |
| 85 |
switch (ev.getAction()) { |
| 86 |
case MotionEvent.ACTION_DOWN: |
| 87 |
case MotionEvent.ACTION_MOVE: { |
| 88 |
if (!mIsInOutsideRegion) { |
| 89 |
mTimeoutHandler.resetTimer(); |
| 90 |
} |
| 91 |
break; |
| 92 |
} |
| 93 |
case MotionEvent.ACTION_UP: |
| 94 |
case MotionEvent.ACTION_CANCEL: { |
| 95 |
mTimeoutHandler.resetTimer(); |
| 96 |
if (mIsInOutsideRegion && !mIsOnStopTransitioning) { |
| 97 |
mTouchEventCallback.onStop(); |
| 98 |
mIsOnStopTransitioning = true; |
| 99 |
} |
| 100 |
|
| 101 |
mIsInOutsideRegion = false; |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
private void disposeInputChannel() { |
| 109 |
if (mInputEventReceiver != null) { |
| 110 |
mInputEventReceiver.dispose(); |
| 111 |
mInputEventReceiver = null; |
| 112 |
} |
| 113 |
if (mInputMonitor != null) { |
| 114 |
mInputMonitor.dispose(); |
| 115 |
mInputMonitor = null; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private boolean isWithinTouchOutsideRegion(float x, float y) { |
| 120 |
return Math.round(y) < mLastUpdatedBounds.top; |
| 121 |
} |
| 122 |
|
| 123 |
private void onInputEvent(InputEvent ev) { |
| 124 |
if (ev instanceof MotionEvent) { |
| 125 |
onMotionEvent((MotionEvent) ev); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
private void updateIsEnabled() { |
| 130 |
disposeInputChannel(); |
| 131 |
if (mIsEnabled) { |
| 132 |
mInputMonitor = InputManager.getInstance().monitorGestureInput( |
| 133 |
"onehanded-touch", DEFAULT_DISPLAY); |
| 134 |
try { |
| 135 |
mMainExecutor.executeBlocking(() -> { |
| 136 |
mInputEventReceiver = new EventReceiver( |
| 137 |
mInputMonitor.getInputChannel(), Looper.myLooper()); |
| 138 |
}); |
| 139 |
} catch (InterruptedException e) { |
| 140 |
throw new RuntimeException("Failed to create input event receiver", e); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void onStartFinished(Rect bounds) { |
| 147 |
mLastUpdatedBounds.set(bounds); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void onStopFinished(Rect bounds) { |
| 152 |
mLastUpdatedBounds.set(bounds); |
| 153 |
mIsOnStopTransitioning = false; |
| 154 |
} |
| 155 |
|
| 156 |
void dump(@NonNull PrintWriter pw) { |
| 157 |
final String innerPrefix = " "; |
| 158 |
pw.println(TAG); |
| 159 |
pw.print(innerPrefix + "mLastUpdatedBounds="); |
| 160 |
pw.println(mLastUpdatedBounds); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
private class EventReceiver extends InputEventReceiver { |
| 165 |
EventReceiver(InputChannel channel, Looper looper) { |
| 166 |
super(channel, looper); |
| 167 |
} |
| 168 |
|
| 169 |
public void onInputEvent(InputEvent event) { |
| 170 |
OneHandedTouchHandler.this.onInputEvent(event); |
| 171 |
finishInputEvent(event, true); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public interface OneHandedTouchEventCallback { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
void onStop(); |
| 183 |
} |
| 184 |
} |