Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 1 | /* |
| 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 He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 17 | package android.service.autofill; |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SuppressLint; |
| 21 | import android.content.Context; |
Adam He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 22 | import android.os.RemoteException; |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | import android.util.MathUtils; |
| 25 | import android.view.MotionEvent; |
| 26 | import android.view.ViewConfiguration; |
| 27 | import android.widget.FrameLayout; |
| 28 | |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 29 | /** |
| 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 He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 33 | * |
| 34 | * @hide |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 35 | */ |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 36 | @SuppressLint("ViewConstructor") |
Adam He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 37 | public class InlineSuggestionRoot extends FrameLayout { |
| 38 | private static final String TAG = "InlineSuggestionRoot"; |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 39 | |
Adam He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 40 | private final @NonNull IInlineSuggestionUiCallback mCallback; |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 41 | private final int mTouchSlop; |
| 42 | |
| 43 | private float mDownX; |
| 44 | private float mDownY; |
| 45 | |
Adam He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 46 | public InlineSuggestionRoot(@NonNull Context context, |
| 47 | @NonNull IInlineSuggestionUiCallback callback) { |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 48 | super(context); |
Adam He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 49 | mCallback = callback; |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 50 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Feng Cao | e93d586 | 2020-02-07 17:44:50 -0800 | [diff] [blame] | 51 | setFocusable(false); |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Feng Cao | 5547632 | 2020-03-16 15:58:15 -0700 | [diff] [blame^] | 55 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | @Override |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 60 | @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 He | 4011625 | 2020-02-04 14:55:39 -0800 | [diff] [blame] | 72 | 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 Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 78 | } |
| 79 | } break; |
| 80 | } |
| 81 | return super.onTouchEvent(event); |
| 82 | } |
Svet Ganov | 17d9d2f | 2020-01-18 10:12:11 -0800 | [diff] [blame] | 83 | } |