blob: 6c9d36b329ffabc0b07381e98625ee235ab5fac2 [file] [log] [blame]
Svet Ganov17d9d2f2020-01-18 10:12:11 -08001/*
2 * Copyright (C) 2020 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
Adam He40116252020-02-04 14:55:39 -080017package android.service.autofill;
Svet Ganov17d9d2f2020-01-18 10:12:11 -080018
19import android.annotation.NonNull;
20import android.annotation.SuppressLint;
21import android.content.Context;
Adam He40116252020-02-04 14:55:39 -080022import android.os.RemoteException;
Svet Ganov17d9d2f2020-01-18 10:12:11 -080023import android.util.Log;
24import android.util.MathUtils;
25import android.view.MotionEvent;
26import android.view.ViewConfiguration;
27import android.widget.FrameLayout;
28
Svet Ganov17d9d2f2020-01-18 10:12:11 -080029/**
30 * This class is the root view for an inline suggestion. It is responsible for
31 * detecting the click on the item and to also transfer input focus to the IME
32 * window if we detect the user is scrolling.
Adam He40116252020-02-04 14:55:39 -080033 *
34 * @hide
Svet Ganov17d9d2f2020-01-18 10:12:11 -080035 */
Svet Ganov17d9d2f2020-01-18 10:12:11 -080036@SuppressLint("ViewConstructor")
Adam He40116252020-02-04 14:55:39 -080037public class InlineSuggestionRoot extends FrameLayout {
38 private static final String TAG = "InlineSuggestionRoot";
Svet Ganov17d9d2f2020-01-18 10:12:11 -080039
Adam He40116252020-02-04 14:55:39 -080040 private final @NonNull IInlineSuggestionUiCallback mCallback;
Svet Ganov17d9d2f2020-01-18 10:12:11 -080041 private final int mTouchSlop;
42
43 private float mDownX;
44 private float mDownY;
45
Adam He40116252020-02-04 14:55:39 -080046 public InlineSuggestionRoot(@NonNull Context context,
47 @NonNull IInlineSuggestionUiCallback callback) {
Svet Ganov17d9d2f2020-01-18 10:12:11 -080048 super(context);
Adam He40116252020-02-04 14:55:39 -080049 mCallback = callback;
Svet Ganov17d9d2f2020-01-18 10:12:11 -080050 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Feng Caoe93d5862020-02-07 17:44:50 -080051 setFocusable(false);
Svet Ganov17d9d2f2020-01-18 10:12:11 -080052 }
53
54 @Override
Feng Cao55476322020-03-16 15:58:15 -070055 public boolean onInterceptTouchEvent(MotionEvent ev) {
56 return true;
57 }
58
59 @Override
Svet Ganov17d9d2f2020-01-18 10:12:11 -080060 @SuppressLint("ClickableViewAccessibility")
61 public boolean onTouchEvent(@NonNull MotionEvent event) {
62 switch (event.getActionMasked()) {
63 case MotionEvent.ACTION_DOWN: {
64 mDownX = event.getX();
65 mDownY = event.getY();
66 } break;
67
68 case MotionEvent.ACTION_MOVE: {
69 final float distance = MathUtils.dist(mDownX, mDownY,
70 event.getX(), event.getY());
71 if (distance > mTouchSlop) {
Adam He40116252020-02-04 14:55:39 -080072 try {
73 mCallback.onTransferTouchFocusToImeWindow(getViewRootImpl().getInputToken(),
74 getContext().getDisplayId());
75 } catch (RemoteException e) {
76 Log.w(TAG, "RemoteException transferring touch focus to IME");
77 }
Svet Ganov17d9d2f2020-01-18 10:12:11 -080078 }
79 } break;
80 }
81 return super.onTouchEvent(event);
82 }
Svet Ganov17d9d2f2020-01-18 10:12:11 -080083}