Martijn Coenen | 8bd4065 | 2011-06-15 15:55:05 +0200 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import com.android.contacts.detail.ContactDetailFragment; |
| 20 | |
| 21 | import android.app.Activity; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.net.Uri; |
| 25 | import android.net.Uri.Builder; |
| 26 | import android.nfc.NdefMessage; |
| 27 | import android.nfc.NdefRecord; |
| 28 | import android.nfc.NfcAdapter; |
| 29 | import android.provider.ContactsContract; |
| 30 | import android.provider.ContactsContract.Contacts; |
| 31 | |
| 32 | import android.util.Log; |
| 33 | |
| 34 | import java.io.ByteArrayOutputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | |
| 38 | public class NfcHandler implements NfcAdapter.NdefPushCallback { |
| 39 | private NfcAdapter mNfcAdapter; |
| 40 | private ContactDetailFragment mContactFragment; |
| 41 | private static final String TAG = "ContactsNfcHandler"; |
| 42 | |
| 43 | public NfcHandler(ContactDetailFragment contactFragment) { |
| 44 | mContactFragment = contactFragment; |
| 45 | mNfcAdapter = NfcAdapter.getDefaultAdapter( |
| 46 | mContactFragment.getActivity()); |
| 47 | } |
| 48 | |
| 49 | public void onPause() { |
| 50 | if (mNfcAdapter != null) { |
| 51 | mNfcAdapter.disableForegroundNdefPush( |
| 52 | mContactFragment.getActivity()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public void onResume() { |
| 57 | if (mNfcAdapter != null) { |
| 58 | mNfcAdapter.enableForegroundNdefPush( |
| 59 | mContactFragment.getActivity(), this); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public NdefMessage createMessage() { |
| 65 | // Get the current contact URI |
| 66 | Uri contactUri = mContactFragment.getUri(); |
| 67 | ContentResolver resolver = mContactFragment.getActivity().getContentResolver(); |
| 68 | if (contactUri != null) { |
| 69 | final String lookupKey = Uri.encode(contactUri.getPathSegments().get(2)); |
| 70 | final Uri shareUri = Contacts.CONTENT_VCARD_URI.buildUpon(). |
| 71 | appendPath(lookupKey). |
| 72 | appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true"). |
| 73 | build(); |
| 74 | ByteArrayOutputStream ndefBytes = new ByteArrayOutputStream(); |
| 75 | byte[] buffer = new byte[1024]; |
| 76 | int r; |
| 77 | try { |
| 78 | InputStream vcardInputStream = resolver.openInputStream(shareUri); |
| 79 | vcardInputStream = resolver.openInputStream(shareUri); |
| 80 | while ((r = vcardInputStream.read(buffer)) > 0) { |
| 81 | ndefBytes.write(buffer, 0, r); |
| 82 | } |
| 83 | |
| 84 | NdefRecord vcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, |
| 85 | "text/x-vCard".getBytes(), new byte[]{}, ndefBytes.toByteArray()); |
| 86 | return new NdefMessage(new NdefRecord[] {vcardRecord}); |
| 87 | } catch (IOException e) { |
| 88 | Log.e(TAG, "IOException creating vcard."); |
| 89 | return null; |
| 90 | } |
| 91 | } else { |
| 92 | Log.w(TAG, "No contact URI to share."); |
| 93 | return null; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onMessagePushed() { |
| 99 | // We may add a sound/notification here at some point. |
| 100 | } |
| 101 | } |