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