1 /* 2 * Copyright (C) 2016 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.setupwizardlib.span; 18 19 import android.content.Context; 20 import android.content.ContextWrapper; 21 import android.graphics.Typeface; 22 import android.os.Build; 23 import androidx.annotation.Nullable; 24 import android.text.Selection; 25 import android.text.Spannable; 26 import android.text.TextPaint; 27 import android.text.style.ClickableSpan; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.TextView; 31 32 /** 33 * A clickable span that will listen for click events and send it back to the context. To use this 34 * class, implement {@link OnLinkClickListener} in your TextView, or use {@link 35 * com.android.setupwizardlib.view.RichTextView#setOnClickListener(View.OnClickListener)}. 36 * 37 * <p>Note on accessibility: For TalkBack to be able to traverse and interact with the links, you 38 * should use {@code LinkAccessibilityHelper} in your {@code TextView} subclass. Optionally you can 39 * also use {@code RichTextView}, which includes link support. 40 */ 41 public class LinkSpan extends ClickableSpan { 42 43 /* 44 * Implementation note: When the orientation changes, TextView retains a reference to this span 45 * instead of writing it to a parcel (ClickableSpan is not Parcelable). If this class has any 46 * reference to the containing Activity (i.e. the activity context, or any views in the 47 * activity), it will cause memory leak. 48 */ 49 50 /* static section */ 51 52 private static final String TAG = "LinkSpan"; 53 54 private static final Typeface TYPEFACE_MEDIUM = 55 Typeface.create("sans-serif-medium", Typeface.NORMAL); 56 57 /** @deprecated Use {@link OnLinkClickListener} */ 58 @Deprecated 59 public interface OnClickListener { onClick(LinkSpan span)60 void onClick(LinkSpan span); 61 } 62 63 /** 64 * Listener that is invoked when a link span is clicked. If the containing view of this span 65 * implements this interface, this will be invoked when the link is clicked. 66 */ 67 public interface OnLinkClickListener { 68 69 /** 70 * Called when a link has been clicked. 71 * 72 * @param span The span that was clicked. 73 * @return True if the click was handled, stopping further propagation of the click event. 74 */ onLinkClick(LinkSpan span)75 boolean onLinkClick(LinkSpan span); 76 } 77 78 /* non-static section */ 79 80 private final String id; 81 LinkSpan(String id)82 public LinkSpan(String id) { 83 this.id = id; 84 } 85 86 @Override onClick(View view)87 public void onClick(View view) { 88 if (dispatchClick(view)) { 89 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 90 // Prevent the touch event from bubbling up to the parent views. 91 view.cancelPendingInputEvents(); 92 } 93 } else { 94 Log.w(TAG, "Dropping click event. No listener attached."); 95 } 96 if (view instanceof TextView) { 97 // Remove the highlight effect when the click happens by clearing the selection 98 CharSequence text = ((TextView) view).getText(); 99 if (text instanceof Spannable) { 100 Selection.setSelection((Spannable) text, 0); 101 } 102 } 103 } 104 dispatchClick(View view)105 private boolean dispatchClick(View view) { 106 boolean handled = false; 107 if (view instanceof OnLinkClickListener) { 108 handled = ((OnLinkClickListener) view).onLinkClick(this); 109 } 110 if (!handled) { 111 final OnClickListener listener = getLegacyListenerFromContext(view.getContext()); 112 if (listener != null) { 113 listener.onClick(this); 114 handled = true; 115 } 116 } 117 return handled; 118 } 119 120 /** @deprecated Deprecated together with {@link OnClickListener} */ 121 @Nullable 122 @Deprecated getLegacyListenerFromContext(@ullable Context context)123 private OnClickListener getLegacyListenerFromContext(@Nullable Context context) { 124 while (true) { 125 if (context instanceof OnClickListener) { 126 return (OnClickListener) context; 127 } else if (context instanceof ContextWrapper) { 128 // Unwrap any context wrapper, in base the base context implements onClickListener. 129 // ContextWrappers cannot have circular base contexts, so at some point this will 130 // reach the one of the other cases and return. 131 context = ((ContextWrapper) context).getBaseContext(); 132 } else { 133 return null; 134 } 135 } 136 } 137 138 @Override updateDrawState(TextPaint drawState)139 public void updateDrawState(TextPaint drawState) { 140 super.updateDrawState(drawState); 141 drawState.setUnderlineText(false); 142 drawState.setTypeface(TYPEFACE_MEDIUM); 143 } 144 getId()145 public String getId() { 146 return id; 147 } 148 } 149