• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
17 package com.android.permissioncontroller.permission.compat;
18 
19 import android.text.Layout;
20 import android.text.Selection;
21 import android.text.Spannable;
22 import android.text.method.LinkMovementMethod;
23 import android.text.method.Touch;
24 import android.view.MotionEvent;
25 import android.widget.TextView;
26 
27 import androidx.annotation.NonNull;
28 
29 /**
30  * Fixes the issue that links can be triggered for touches outside of line bounds for
31  * {@link LinkMovementMethod}.
32  * <p>
33  * This is based on the fix in ag/22301465.
34  */
35 public class LinkMovementMethodCompat extends LinkMovementMethod {
36     @Override
onTouchEvent(@onNull TextView widget, @NonNull Spannable buffer, @NonNull MotionEvent event)37     public boolean onTouchEvent(@NonNull TextView widget, @NonNull Spannable buffer,
38             @NonNull MotionEvent event) {
39         int action = event.getAction();
40 
41         if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
42             int x = (int) event.getX();
43             int y = (int) event.getY();
44 
45             x -= widget.getTotalPaddingLeft();
46             y -= widget.getTotalPaddingTop();
47 
48             x += widget.getScrollX();
49             y += widget.getScrollY();
50 
51             Layout layout = widget.getLayout();
52             boolean isOutOfLineBounds;
53             if (y < 0 || y > layout.getHeight()) {
54                 isOutOfLineBounds = true;
55             } else {
56                 int line = layout.getLineForVertical(y);
57                 isOutOfLineBounds = x < layout.getLineLeft(line) || x > layout.getLineRight(line);
58             }
59 
60             if (isOutOfLineBounds) {
61                 Selection.removeSelection(buffer);
62 
63                 // return LinkMovementMethod.super.onTouchEvent(widget, buffer, event);
64                 return Touch.onTouchEvent(widget, buffer, event);
65             }
66         }
67 
68         return super.onTouchEvent(widget, buffer, event);
69     }
70 
71     /**
72      * @return a {@link LinkMovementMethodCompat} instance
73      */
74     @NonNull
getInstance()75     public static LinkMovementMethodCompat getInstance() {
76         if (sInstance == null) {
77             sInstance = new LinkMovementMethodCompat();
78         }
79 
80         return sInstance;
81     }
82 
83     private static LinkMovementMethodCompat sInstance;
84 }
85