blob: 08e4b20ac52a7bda75b2370a828f1d691e4dc930 [file] [log] [blame]
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.sampleinputmethodaccessibilityservice;
import android.view.MotionEvent;
import android.view.View;
final class DragToMoveTouchListener implements View.OnTouchListener {
@FunctionalInterface
interface OnMoveCallback {
void onMove(int dx, int dy);
}
private final OnMoveCallback mCallback;
private int mPointId = -1;
private float mLastTouchX;
private float mLastTouchY;
DragToMoveTouchListener(OnMoveCallback callback) {
mCallback = callback;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
final int pointId = event.getPointerId(event.getActionIndex());
switch (event.getAction()) {
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN: {
if (mPointId != -1) {
break;
}
v.setPressed(true);
mPointId = pointId;
mLastTouchX = event.getRawX();
mLastTouchY = event.getRawY();
break;
}
case MotionEvent.ACTION_MOVE: {
if (pointId != mPointId) {
break;
}
final float x = event.getRawX();
final float y = event.getRawY();
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mCallback.onMove((int) dx, (int) dy);
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
if (pointId != mPointId) {
break;
}
mPointId = -1;
v.setPressed(false);
break;
}
default:
break;
}
return true;
}
}