summaryrefslogtreecommitdiff
path: root/library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java
blob: 526883cf7ad9b2a52cdf531135b2625ef37599fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2018 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.android.setupwizardlib.view;

import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * A movement method that tracks the last result of whether touch events are handled. This is used
 * to patch the return value of {@link TextView#onTouchEvent} so that it consumes the touch events
 * only when the movement method says the event is consumed.
 */
public interface TouchableMovementMethod {

  /** @return The last touch event received in {@link MovementMethod#onTouchEvent} */
  MotionEvent getLastTouchEvent();

  /**
   * @return The return value of the last {@link MovementMethod#onTouchEvent}, or whether the last
   *     touch event should be considered handled by the text view
   */
  boolean isLastTouchEventHandled();

  /**
   * An extension of LinkMovementMethod that tracks whether the event is handled when it is touched.
   */
  class TouchableLinkMovementMethod extends LinkMovementMethod implements TouchableMovementMethod {

    public static TouchableLinkMovementMethod getInstance() {
      return new TouchableLinkMovementMethod();
    }

    boolean lastEventResult = false;
    MotionEvent lastEvent;

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
      lastEvent = event;
      boolean result = super.onTouchEvent(widget, buffer, event);
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // Unfortunately, LinkMovementMethod extends ScrollMovementMethod, and it always
        // consume the down event. So here we use the selection instead as a hint of whether
        // the down event landed on a link.
        lastEventResult = Selection.getSelectionStart(buffer) != -1;
      } else {
        lastEventResult = result;
      }
      return result;
    }

    @Override
    public MotionEvent getLastTouchEvent() {
      return lastEvent;
    }

    @Override
    public boolean isLastTouchEventHandled() {
      return lastEventResult;
    }
  }
}