• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 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  * Implementation of the {@link ClickableSpan} that allows setting a url string. When
33  * selecting and clicking on the text to which the span is attached, the <code>URLSpan</code>
34  * will try to open the url, by launching an an Activity with an {@link Intent#ACTION_VIEW} intent.
35  * <p>
36  * For example, a <code>URLSpan</code> can be used like this:
37  * <pre>
38  * SpannableString string = new SpannableString("Text with a url span");
39  * string.setSpan(new URLSpan("http://www.developer.android.com"), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
40  * </pre>
41  * <img src="{@docRoot}reference/android/images/text/style/urlspan.png" />
42  * <figcaption>Text with <code>URLSpan</code>.</figcaption>
43  */
44 @android.ravenwood.annotation.RavenwoodKeepWholeClass
45 public class URLSpan extends ClickableSpan implements ParcelableSpan {
46 
47     private final String mURL;
48 
49     /**
50      * Constructs a {@link URLSpan} from a url string.
51      *
52      * @param url the url string
53      */
URLSpan(String url)54     public URLSpan(String url) {
55         mURL = url;
56     }
57 
58     /**
59      * Constructs a {@link URLSpan} from a parcel.
60      */
URLSpan(@onNull Parcel src)61     public URLSpan(@NonNull Parcel src) {
62         mURL = src.readString();
63     }
64 
65     @Override
getSpanTypeId()66     public int getSpanTypeId() {
67         return getSpanTypeIdInternal();
68     }
69 
70     /** @hide */
71     @Override
getSpanTypeIdInternal()72     public int getSpanTypeIdInternal() {
73         return TextUtils.URL_SPAN;
74     }
75 
76     @Override
describeContents()77     public int describeContents() {
78         return 0;
79     }
80 
81     @Override
writeToParcel(@onNull Parcel dest, int flags)82     public void writeToParcel(@NonNull Parcel dest, int flags) {
83         writeToParcelInternal(dest, flags);
84     }
85 
86     /** @hide */
87     @Override
writeToParcelInternal(@onNull Parcel dest, int flags)88     public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
89         dest.writeString(mURL);
90     }
91 
92     /**
93      * Get the url string for this span.
94      *
95      * @return the url string.
96      */
getURL()97     public String getURL() {
98         return mURL;
99     }
100 
101     @Override
onClick(View widget)102     public void onClick(View widget) {
103         Uri uri = Uri.parse(getURL());
104         Context context = widget.getContext();
105         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
106         intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
107         try {
108             context.startActivity(intent);
109         } catch (ActivityNotFoundException e) {
110             Log.w("URLSpan", "Activity was not found for intent, " + intent.toString());
111         }
112     }
113 
114     @Override
toString()115     public String toString() {
116         return "URLSpan{" + "URL='" + getURL() + '\'' + '}';
117     }
118 }
119