1 /* 2 * Copyright (C) 2014 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.settings.widget; 18 19 import android.content.Context; 20 import android.support.annotation.NonNull; 21 import android.text.Spanned; 22 import android.text.method.LinkMovementMethod; 23 import android.text.style.ClickableSpan; 24 import android.util.AttributeSet; 25 import android.view.MotionEvent; 26 import android.widget.TextView; 27 28 /** 29 * Copied from setup wizard. 30 */ 31 public class LinkTextView extends TextView { 32 33 private LinkAccessibilityHelper mAccessibilityHelper; 34 LinkTextView(Context context)35 public LinkTextView(Context context) { 36 this(context, null); 37 } 38 LinkTextView(Context context, AttributeSet attrs)39 public LinkTextView(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 mAccessibilityHelper = new LinkAccessibilityHelper(this); 42 setAccessibilityDelegate(mAccessibilityHelper); 43 } 44 45 @Override setText(CharSequence text, BufferType type)46 public void setText(CharSequence text, BufferType type) { 47 super.setText(text, type); 48 if (text instanceof Spanned) { 49 final ClickableSpan[] spans = 50 ((Spanned) text).getSpans(0, text.length(), ClickableSpan.class); 51 if (spans.length > 0) { 52 setMovementMethod(LinkMovementMethod.getInstance()); 53 } 54 } 55 } 56 57 @Override dispatchHoverEvent(@onNull MotionEvent event)58 protected boolean dispatchHoverEvent(@NonNull MotionEvent event) { 59 if (mAccessibilityHelper.dispatchHoverEvent(event)) { 60 return true; 61 } 62 return super.dispatchHoverEvent(event); 63 } 64 } 65