1/* 2 * Copyright (C) 2010 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 com.android.apps.tag.record; 18 19import com.android.apps.tag.R; 20import com.google.common.annotations.VisibleForTesting; 21import com.google.common.base.Preconditions; 22import com.google.common.collect.BiMap; 23import com.google.common.collect.ImmutableBiMap; 24import com.google.common.primitives.Bytes; 25 26import android.app.Activity; 27import android.content.ActivityNotFoundException; 28import android.content.Context; 29import android.content.Intent; 30import android.content.pm.PackageManager; 31import android.content.pm.ResolveInfo; 32import android.net.Uri; 33import android.nfc.NdefRecord; 34import android.os.Parcel; 35import android.os.Parcelable; 36import android.telephony.PhoneNumberUtils; 37import android.text.TextUtils; 38import android.util.Log; 39import android.view.LayoutInflater; 40import android.view.View; 41import android.view.View.OnClickListener; 42import android.view.ViewGroup; 43import android.widget.ImageView; 44import android.widget.TextView; 45 46import java.nio.charset.Charset; 47import java.util.Arrays; 48import java.util.List; 49import java.util.Locale; 50 51/** 52 * A parsed record containing a Uri. 53 */ 54public class UriRecord extends ParsedNdefRecord implements OnClickListener { 55 private static final String TAG = "UriRecord"; 56 57 public static final String RECORD_TYPE = "UriRecord"; 58 59 private final Uri mUri; 60 61 private UriRecord(Uri uri) { 62 this.mUri = Preconditions.checkNotNull(uri); 63 } 64 65 public Intent getIntentForUri() { 66 String scheme = mUri.getScheme(); 67 if ("tel".equals(scheme)) { 68 return new Intent(Intent.ACTION_CALL, mUri); 69 } else if ("sms".equals(scheme) || "smsto".equals(scheme)) { 70 return new Intent(Intent.ACTION_SENDTO, mUri); 71 } else { 72 return new Intent(Intent.ACTION_VIEW, mUri); 73 } 74 } 75 76 public String getPrettyUriString(Context context) { 77 String scheme = mUri.getScheme(); 78 boolean tel = "tel".equals(scheme); 79 boolean sms = "sms".equals(scheme) || "smsto".equals(scheme); 80 if (tel || sms) { 81 String ssp = mUri.getSchemeSpecificPart(); 82 int offset = ssp.indexOf('?'); 83 if (offset >= 0) { 84 ssp = ssp.substring(0, offset); 85 } 86 if (tel) { 87 return context.getString(R.string.action_call, PhoneNumberUtils.formatNumber(ssp)); 88 } else { 89 return context.getString(R.string.action_text, PhoneNumberUtils.formatNumber(ssp)); 90 } 91 } else { 92 return mUri.toString(); 93 } 94 } 95 96 @Override 97 public View getView(Activity activity, LayoutInflater inflater, ViewGroup parent, int offset) { 98 return RecordUtils.getViewsForIntent(activity, inflater, parent, this, getIntentForUri(), 99 getPrettyUriString(activity)); 100 } 101 102 @Override 103 public String getSnippet(Context context, Locale locale) { 104 return getPrettyUriString(context); 105 } 106 107 @Override 108 public void onClick(View view) { 109 RecordUtils.ClickInfo info = (RecordUtils.ClickInfo) view.getTag(); 110 try { 111 info.activity.startActivity(info.intent); 112 info.activity.finish(); 113 } catch (ActivityNotFoundException e) { 114 // The activity wansn't found for some reason. Don't crash, but don't do anything. 115 Log.e(TAG, "Failed to launch activity for intent " + info.intent, e); 116 } 117 } 118 119 @VisibleForTesting 120 public Uri getUri() { 121 return mUri; 122 } 123 124 /** 125 * Convert {@link android.nfc.NdefRecord} into a {@link android.net.Uri}. This will handle 126 * both TNF_WELL_KNOWN / RTD_URI and TNF_ABSOLUTE_URI. 127 * 128 * @throws IllegalArgumentException if the NdefRecord is not a 129 * record containing a URI. 130 */ 131 public static UriRecord parse(NdefRecord record) { 132 Uri uri = record.toUri(); 133 if (uri == null) throw new IllegalArgumentException("not a uri"); 134 return new UriRecord(uri); 135 } 136 137 public static boolean isUri(NdefRecord record) { 138 return record.toUri() != null; 139 } 140 141 /** 142 * Convert a {@link Uri} to an {@link NdefRecord} 143 */ 144 public static NdefRecord newUriRecord(Uri uri) { 145 return NdefRecord.createUri(uri); 146 } 147} 148