blob: e059a3dfc693fff39ec8b0fc7dfcdd858e8dc321 [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;
23import android.content.Context;
24import android.net.Uri;
25import android.net.Uri.Builder;
26import android.nfc.NdefMessage;
27import android.nfc.NdefRecord;
28import android.nfc.NfcAdapter;
29import android.provider.ContactsContract;
30import android.provider.ContactsContract.Contacts;
31
32import android.util.Log;
33
34import java.io.ByteArrayOutputStream;
35import java.io.IOException;
36import java.io.InputStream;
37
Martijn Coenen194cdd32011-06-16 08:51:20 +020038/**
39 * This class implements sharing the currently displayed
40 * contact to another device using NFC. NFC sharing is only
41 * enabled when the activity is in the foreground and resumed.
42 * When an NFC link is established, {@link #createMessage}
43 * will be called to create the data to be sent over the link,
44 * which is a vCard in this case.
45 */
Martijn Coenen8bd40652011-06-15 15:55:05 +020046public class NfcHandler implements NfcAdapter.NdefPushCallback {
47 private NfcAdapter mNfcAdapter;
48 private ContactDetailFragment mContactFragment;
49 private static final String TAG = "ContactsNfcHandler";
50
51 public NfcHandler(ContactDetailFragment contactFragment) {
52 mContactFragment = contactFragment;
53 mNfcAdapter = NfcAdapter.getDefaultAdapter(
54 mContactFragment.getActivity());
55 }
56
57 public void onPause() {
58 if (mNfcAdapter != null) {
59 mNfcAdapter.disableForegroundNdefPush(
60 mContactFragment.getActivity());
61 }
62 }
63
64 public void onResume() {
65 if (mNfcAdapter != null) {
66 mNfcAdapter.enableForegroundNdefPush(
67 mContactFragment.getActivity(), this);
68 }
69 }
70
71 @Override
72 public NdefMessage createMessage() {
73 // Get the current contact URI
74 Uri contactUri = mContactFragment.getUri();
75 ContentResolver resolver = mContactFragment.getActivity().getContentResolver();
76 if (contactUri != null) {
77 final String lookupKey = Uri.encode(contactUri.getPathSegments().get(2));
78 final Uri shareUri = Contacts.CONTENT_VCARD_URI.buildUpon().
79 appendPath(lookupKey).
80 appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
81 build();
82 ByteArrayOutputStream ndefBytes = new ByteArrayOutputStream();
83 byte[] buffer = new byte[1024];
84 int r;
85 try {
86 InputStream vcardInputStream = resolver.openInputStream(shareUri);
Martijn Coenen8bd40652011-06-15 15:55:05 +020087 while ((r = vcardInputStream.read(buffer)) > 0) {
88 ndefBytes.write(buffer, 0, r);
89 }
90
91 NdefRecord vcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
92 "text/x-vCard".getBytes(), new byte[]{}, ndefBytes.toByteArray());
93 return new NdefMessage(new NdefRecord[] {vcardRecord});
94 } catch (IOException e) {
95 Log.e(TAG, "IOException creating vcard.");
96 return null;
97 }
98 } else {
99 Log.w(TAG, "No contact URI to share.");
100 return null;
101 }
102 }
103
104 @Override
105 public void onMessagePushed() {
Martijn Coenen8bd40652011-06-15 15:55:05 +0200106 }
107}