| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.style; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.content.ActivityNotFoundException; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.Intent; |
| 23 |
import android.net.Uri; |
| 24 |
import android.os.Parcel; |
| 25 |
import android.provider.Browser; |
| 26 |
import android.text.ParcelableSpan; |
| 27 |
import android.text.TextUtils; |
| 28 |
import android.util.Log; |
| 29 |
import android.view.View; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class URLSpan extends ClickableSpan implements ParcelableSpan { |
| 45 |
|
| 46 |
private final String mURL; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public URLSpan(String url) { |
| 54 |
mURL = url; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public URLSpan(@NonNull Parcel src) { |
| 61 |
mURL = src.readString(); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public int getSpanTypeId() { |
| 66 |
return getSpanTypeIdInternal(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
@Override |
| 71 |
public int getSpanTypeIdInternal() { |
| 72 |
return TextUtils.URL_SPAN; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public int describeContents() { |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 82 |
writeToParcelInternal(dest, flags); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
@Override |
| 87 |
public void writeToParcelInternal(@NonNull Parcel dest, int flags) { |
| 88 |
dest.writeString(mURL); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public String getURL() { |
| 97 |
return mURL; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onClick(View widget) { |
| 102 |
Uri uri = Uri.parse(getURL()); |
| 103 |
Context context = widget.getContext(); |
| 104 |
Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
| 105 |
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); |
| 106 |
try { |
| 107 |
context.startActivity(intent); |
| 108 |
} catch (ActivityNotFoundException e) { |
| 109 |
Log.w("URLSpan", "Actvity was not found for intent, " + intent.toString()); |
| 110 |
} |
| 111 |
} |
| 112 |
} |