NfcHandler.java revision e5b15cd3c8450c935ce43ca9be83a23bcdd565a2
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 17package com.android.email.activity; 18 19import com.android.emailcommon.provider.Account; 20 21import android.app.Activity; 22import android.content.Context; 23import android.nfc.NdefMessage; 24import android.nfc.NdefRecord; 25import android.nfc.NfcAdapter; 26import android.text.TextUtils; 27import android.util.Log; 28 29import java.io.UnsupportedEncodingException; 30import java.net.URLEncoder; 31 32/** 33 * This class implements sharing the e-mail address of the 34 * active account to another device using NFC. NFC sharing is only 35 * enabled when the activity is in the foreground and resumed. 36 * When an NFC link is established, {@link #createMessage} 37 * will be called to create the data to be sent over the link, 38 * which is a vCard in this case. 39 */ 40public class NfcHandler implements NfcAdapter.NdefPushCallback { 41 private NfcAdapter mNfcAdapter; 42 private UIControllerBase mUiController; 43 private Activity mActivity; 44 private String mCurrentEmail; 45 private static final String TAG = "ContactsNfcHandler"; 46 47 public NfcHandler(UIControllerBase controller, Activity 48 activity) { 49 mUiController = controller; 50 mActivity = activity; 51 mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity); 52 } 53 54 public void onAccountChanged() { 55 if (mUiController.isActualAccountSelected()) { 56 final long accountId = mUiController.getActualAccountId(); 57 final Account account = Account.restoreAccountWithId( 58 mActivity, accountId); 59 mCurrentEmail = account.mEmailAddress; 60 } else { 61 mCurrentEmail = null; 62 } 63 64 } 65 66 public void onPause() { 67 if (mNfcAdapter != null) { 68 mNfcAdapter.disableForegroundNdefPush( 69 mActivity); 70 } 71 } 72 73 public void onResume() { 74 if (mNfcAdapter != null) { 75 mNfcAdapter.enableForegroundNdefPush( 76 mActivity, this); 77 onAccountChanged(); // Fetch current account 78 } 79 } 80 81 private static NdefMessage buildMailtoNdef(String address) { 82 if (TextUtils.isEmpty(address)) { 83 return null; 84 } 85 byte[] accountBytes; 86 try { 87 accountBytes = URLEncoder.encode(address, "UTF-8") 88 .getBytes("UTF-8"); 89 } catch (UnsupportedEncodingException e) { 90 return null; 91 } 92 byte[] recordBytes = new byte[accountBytes.length + 1]; 93 recordBytes[0] = 0x06; // NDEF mailto: prefix 94 System.arraycopy(accountBytes, 0, recordBytes, 1, accountBytes.length); 95 NdefRecord mailto = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, 96 new byte[0], recordBytes); 97 return new NdefMessage(new NdefRecord[] { mailto }); 98 } 99 100 @Override 101 public NdefMessage createMessage() { 102 if (mCurrentEmail != null) { 103 return buildMailtoNdef(mCurrentEmail); 104 } else { 105 return null; 106 } 107 } 108 109 @Override 110 public void onMessagePushed() { 111 } 112} 113