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