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.content.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Parcel;
24 import android.provider.Browser;
25 import android.text.ParcelableSpan;
26 import android.text.TextUtils;
27 import android.util.Log;
28 import android.view.View;
29 
30 public class URLSpan extends ClickableSpan implements ParcelableSpan {
31 
32     private final String mURL;
33 
URLSpan(String url)34     public URLSpan(String url) {
35         mURL = url;
36     }
37 
URLSpan(Parcel src)38     public URLSpan(Parcel src) {
39         mURL = src.readString();
40     }
41 
getSpanTypeId()42     public int getSpanTypeId() {
43         return getSpanTypeIdInternal();
44     }
45 
46     /** @hide */
getSpanTypeIdInternal()47     public int getSpanTypeIdInternal() {
48         return TextUtils.URL_SPAN;
49     }
50 
describeContents()51     public int describeContents() {
52         return 0;
53     }
54 
writeToParcel(Parcel dest, int flags)55     public void writeToParcel(Parcel dest, int flags) {
56         writeToParcelInternal(dest, flags);
57     }
58 
59     /** @hide */
writeToParcelInternal(Parcel dest, int flags)60     public void writeToParcelInternal(Parcel dest, int flags) {
61         dest.writeString(mURL);
62     }
63 
getURL()64     public String getURL() {
65         return mURL;
66     }
67 
68     @Override
onClick(View widget)69     public void onClick(View widget) {
70         Uri uri = Uri.parse(getURL());
71         Context context = widget.getContext();
72         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
73         intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
74         try {
75             context.startActivity(intent);
76         } catch (ActivityNotFoundException e) {
77             Log.w("URLSpan", "Actvity was not found for intent, " + intent.toString());
78         }
79     }
80 }
81