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