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.browser; 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.os.Handler; 25 import android.os.Message; 26 import android.util.Log; 27 28 import java.util.concurrent.CountDownLatch; 29 30 /** This class implements sharing the URL of the currently 31 * shown browser page over NFC. Sharing is only active 32 * when the activity is in the foreground and resumed. 33 * Incognito tabs will not be shared over NFC. 34 */ 35 public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback { 36 static final String TAG = "BrowserNfcHandler"; 37 static final int GET_PRIVATE_BROWSING_STATE_MSG = 100; 38 39 final Controller mController; 40 41 Tab mCurrentTab; 42 boolean mIsPrivate; 43 CountDownLatch mPrivateBrowsingSignal; 44 45 public static void register(Activity activity, Controller controller) { 46 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity.getApplicationContext()); 47 if (adapter == null) { 48 return; // NFC not available on this device 49 } 50 NfcHandler handler = null; 51 if (controller != null) { 52 handler = new NfcHandler(controller); 53 } 54 55 adapter.setNdefPushMessageCallback(handler, activity); 56 } 57 58 public static void unregister(Activity activity) { 59 // Passing a null controller causes us to disable 60 // the callback and release the ref to out activity. 61 register(activity, null); 62 } 63 64 public NfcHandler(Controller controller) { 65 mController = controller; 66 } 67 68 final Handler mHandler = new Handler() { 69 @Override 70 public void handleMessage(Message msg) { 71 if (msg.what == GET_PRIVATE_BROWSING_STATE_MSG) { 72 mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); 73 mPrivateBrowsingSignal.countDown(); 74 } 75 } 76 }; 77 78 @Override 79 public NdefMessage createNdefMessage(NfcEvent event) { 80 mCurrentTab = mController.getCurrentTab(); 81 if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { 82 // We can only read the WebView state on the UI thread, so post 83 // a message and wait. 84 mPrivateBrowsingSignal = new CountDownLatch(1); 85 mHandler.sendMessage(mHandler.obtainMessage(GET_PRIVATE_BROWSING_STATE_MSG)); 86 try { 87 mPrivateBrowsingSignal.await(); 88 } catch (InterruptedException e) { 89 return null; 90 } 91 } 92 93 if ((mCurrentTab == null) || mIsPrivate) { 94 return null; 95 } 96 97 String currentUrl = mCurrentTab.getUrl(); 98 if (currentUrl != null) { 99 try { 100 return new NdefMessage(NdefRecord.createUri(currentUrl)); 101 } catch (IllegalArgumentException e) { 102 Log.e(TAG, "IllegalArgumentException creating URI NdefRecord", e); 103 return null; 104 } 105 } else { 106 return null; 107 } 108 } 109 } 110