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