blob: e3e94a951016f12e363120a3fc8f8607d5b190fc [file] [log] [blame]
Martijn Coenen8bd40652011-06-15 15:55:05 +02001/*
2 * Copyright (C) 2011 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.contacts;
18
19import com.android.contacts.detail.ContactDetailFragment;
20
21import android.app.Activity;
22import android.content.ContentResolver;
Martijn Coenen8bd40652011-06-15 15:55:05 +020023import android.net.Uri;
Martijn Coenen8bd40652011-06-15 15:55:05 +020024import android.nfc.NdefMessage;
25import android.nfc.NdefRecord;
26import android.nfc.NfcAdapter;
Nick Pellya3364bc2011-08-23 18:49:03 -070027import android.nfc.NfcEvent;
Martijn Coenen8bd40652011-06-15 15:55:05 +020028import android.provider.ContactsContract.Contacts;
29
30import android.util.Log;
31
32import java.io.ByteArrayOutputStream;
33import java.io.IOException;
34import java.io.InputStream;
35
Martijn Coenen194cdd32011-06-16 08:51:20 +020036/**
37 * This class implements sharing the currently displayed
38 * contact to another device using NFC. NFC sharing is only
39 * enabled when the activity is in the foreground and resumed.
40 * When an NFC link is established, {@link #createMessage}
41 * will be called to create the data to be sent over the link,
42 * which is a vCard in this case.
43 */
Nick Pellya3364bc2011-08-23 18:49:03 -070044public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback {
45
46 static final String TAG = "ContactNfcHandler";
47
48 final ContactDetailFragment mContactFragment;
49
50 public static void register(Activity activity, ContactDetailFragment contactFragment) {
51 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());
52 if (adapter == null) {
53 return; // NFC not available on this device
54 }
55 adapter.setNdefPushMessageCallback(new NfcHandler(contactFragment), activity);
56 }
Martijn Coenen8bd40652011-06-15 15:55:05 +020057
58 public NfcHandler(ContactDetailFragment contactFragment) {
59 mContactFragment = contactFragment;
Martijn Coenen8bd40652011-06-15 15:55:05 +020060 }
61
62 @Override
Nick Pellya3364bc2011-08-23 18:49:03 -070063 public NdefMessage createNdefMessage(NfcEvent event) {
Martijn Coenen8bd40652011-06-15 15:55:05 +020064 // Get the current contact URI
65 Uri contactUri = mContactFragment.getUri();
66 ContentResolver resolver = mContactFragment.getActivity().getContentResolver();
67 if (contactUri != null) {
68 final String lookupKey = Uri.encode(contactUri.getPathSegments().get(2));
69 final Uri shareUri = Contacts.CONTENT_VCARD_URI.buildUpon().
70 appendPath(lookupKey).
71 appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
72 build();
73 ByteArrayOutputStream ndefBytes = new ByteArrayOutputStream();
74 byte[] buffer = new byte[1024];
75 int r;
76 try {
77 InputStream vcardInputStream = resolver.openInputStream(shareUri);
Martijn Coenen8bd40652011-06-15 15:55:05 +020078 while ((r = vcardInputStream.read(buffer)) > 0) {
79 ndefBytes.write(buffer, 0, r);
80 }
81
82 NdefRecord vcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
83 "text/x-vCard".getBytes(), new byte[]{}, ndefBytes.toByteArray());
84 return new NdefMessage(new NdefRecord[] {vcardRecord});
85 } catch (IOException e) {
86 Log.e(TAG, "IOException creating vcard.");
87 return null;
88 }
89 } else {
90 Log.w(TAG, "No contact URI to share.");
91 return null;
92 }
93 }
Martijn Coenen8bd40652011-06-15 15:55:05 +020094}