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 getSpanTypeIdInternal(); 44 } 45 46 /** @hide */ 47 public int getSpanTypeIdInternal() { 48 return TextUtils.URL_SPAN; 49 } 50 51 public int describeContents() { 52 return 0; 53 } 54 55 public void writeToParcel(Parcel dest, int flags) { 56 writeToParcelInternal(dest, flags); 57 } 58 59 /** @hide */ 60 public void writeToParcelInternal(Parcel dest, int flags) { 61 dest.writeString(mURL); 62 } 63 64 public String getURL() { 65 return mURL; 66 } 67 68 @Override 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