Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 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; |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 23 | import android.nfc.NfcEvent; |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | |
| 27 | import java.util.concurrent.CountDownLatch; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 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 | */ |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 34 | public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback { |
| 35 | static final int GET_PRIVATE_BROWSING_STATE_MSG = 100; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 36 | |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 37 | final Controller mController; |
Martijn Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame] | 38 | |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 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 | adapter.setNdefPushMessageCallback(new NfcHandler(controller), activity); |
| 49 | } |
| 50 | |
| 51 | public NfcHandler(Controller controller) { |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 52 | mController = controller; |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | final Handler mHandler = new Handler() { |
| 56 | @Override |
| 57 | public void handleMessage(Message msg) { |
| 58 | if (msg.what == GET_PRIVATE_BROWSING_STATE_MSG) { |
| 59 | mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); |
| 60 | mPrivateBrowsingSignal.countDown(); |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 61 | } |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 62 | } |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 63 | }; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 64 | |
| 65 | @Override |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 66 | public NdefMessage createNdefMessage(NfcEvent event) { |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 67 | mCurrentTab = mController.getCurrentTab(); |
| 68 | if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { |
| 69 | // We can only read the WebView state on the UI thread, so post |
| 70 | // a message and wait. |
| 71 | mPrivateBrowsingSignal = new CountDownLatch(1); |
| 72 | mHandler.sendMessage(mHandler.obtainMessage(GET_PRIVATE_BROWSING_STATE_MSG)); |
| 73 | try { |
| 74 | mPrivateBrowsingSignal.await(); |
| 75 | } catch (InterruptedException e) { |
| 76 | return null; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if ((mCurrentTab == null) || mIsPrivate) { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | String currentUrl = mCurrentTab.getUrl(); |
| 85 | if (currentUrl != null) { |
| 86 | NdefRecord record = NdefRecord.createUri(currentUrl); |
| 87 | NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); |
| 88 | return msg; |
| 89 | } else { |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 90 | return null; |
| 91 | } |
| 92 | } |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 93 | } |