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