blob: 07b3f06699734d67bacb5d41688c4b014757eafb [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
Martijn Coenen8bd40652011-06-15 15:55:05 +020019import android.app.Activity;
20import android.content.ContentResolver;
Brian Attwell2d150da2014-07-09 22:35:56 -070021import android.content.Context;
Martijn Coenen8bd40652011-06-15 15:55:05 +020022import android.net.Uri;
Martijn Coenen8bd40652011-06-15 15:55:05 +020023import android.nfc.NdefMessage;
24import android.nfc.NdefRecord;
25import android.nfc.NfcAdapter;
Nick Pellya3364bc2011-08-23 18:49:03 -070026import android.nfc.NfcEvent;
Martijn Coenen8bd40652011-06-15 15:55:05 +020027import android.provider.ContactsContract.Contacts;
Martijn Coenen92165c62011-09-13 16:03:18 +020028import android.provider.ContactsContract.Profile;
Martijn Coenen8bd40652011-06-15 15:55:05 +020029import android.util.Log;
30
31import java.io.ByteArrayOutputStream;
32import java.io.IOException;
33import java.io.InputStream;
34
Martijn Coenen194cdd32011-06-16 08:51:20 +020035/**
36 * This class implements sharing the currently displayed
37 * contact to another device using NFC. NFC sharing is only
38 * enabled when the activity is in the foreground and resumed.
39 * When an NFC link is established, {@link #createMessage}
40 * will be called to create the data to be sent over the link,
41 * which is a vCard in this case.
42 */
Nick Pellya3364bc2011-08-23 18:49:03 -070043public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback {
44
Martijn Coenen92165c62011-09-13 16:03:18 +020045 private static final String TAG = "ContactNfcHandler";
46 private static final String PROFILE_LOOKUP_KEY = "profile";
Brian Attwell2d150da2014-07-09 22:35:56 -070047 private final Context mContext;
48 private final Uri mContactUri;
Nick Pellya3364bc2011-08-23 18:49:03 -070049
Brian Attwell2d150da2014-07-09 22:35:56 -070050 /* Register NFC handler. This should be called in activities' onCreate(), or similar methods */
51 public static void register(Activity activity, Uri contactUri) {
Nick Pellya3364bc2011-08-23 18:49:03 -070052 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());
53 if (adapter == null) {
54 return; // NFC not available on this device
55 }
Brian Attwell2d150da2014-07-09 22:35:56 -070056 adapter.setNdefPushMessageCallback(new NfcHandler(activity, contactUri), activity);
Nick Pellya3364bc2011-08-23 18:49:03 -070057 }
Martijn Coenen8bd40652011-06-15 15:55:05 +020058
Brian Attwell2d150da2014-07-09 22:35:56 -070059 public NfcHandler(Context context, Uri contactUri) {
60 mContext = context;
61 mContactUri = contactUri;
Martijn Coenen8bd40652011-06-15 15:55:05 +020062 }
63
64 @Override
Nick Pellya3364bc2011-08-23 18:49:03 -070065 public NdefMessage createNdefMessage(NfcEvent event) {
Brian Attwell2d150da2014-07-09 22:35:56 -070066 ContentResolver resolver = mContext.getContentResolver();
67 if (mContactUri != null) {
68 final String lookupKey = Uri.encode(mContactUri.getPathSegments().get(2));
Martijn Coenen92165c62011-09-13 16:03:18 +020069 final Uri shareUri;
70 // TODO find out where to get this constant from, or find another way
71 // of determining this.
72 if (lookupKey.equals(PROFILE_LOOKUP_KEY)) {
73 shareUri = Profile.CONTENT_VCARD_URI.buildUpon().
74 appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
75 build();
76 } else {
77 shareUri = Contacts.CONTENT_VCARD_URI.buildUpon().
78 appendPath(lookupKey).
79 appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
80 build();
81 }
Martijn Coenen8bd40652011-06-15 15:55:05 +020082 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
Nick Pellycd564c92012-01-09 10:50:12 -080091 NdefRecord record = NdefRecord.createMime("text/x-vcard", ndefBytes.toByteArray());
92 return new NdefMessage(record);
Martijn Coenen8bd40652011-06-15 15:55:05 +020093 } catch (IOException e) {
94 Log.e(TAG, "IOException creating vcard.");
95 return null;
96 }
97 } else {
98 Log.w(TAG, "No contact URI to share.");
99 return null;
100 }
101 }
Martijn Coenen8bd40652011-06-15 15:55:05 +0200102}