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; |
Martijn Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame] | 23 | import android.os.AsyncTask; |
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 | */ |
| 34 | public class NfcHandler implements NfcAdapter.NdefPushCallback { |
| 35 | private NfcAdapter mNfcAdapter; |
| 36 | private Activity mActivity; |
| 37 | private Controller mController; |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame^] | 38 | private Handler mHandler; |
| 39 | private Tab mCurrentTab; |
| 40 | private boolean mIsPrivate; |
| 41 | private CountDownLatch mPrivateBrowsingSignal; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 42 | |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame^] | 43 | private static final int GET_PRIVATE_BROWSING_STATE_MSG = 100; |
Martijn Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame] | 44 | |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 45 | public NfcHandler(Activity browser, Controller controller) { |
| 46 | mActivity = browser; |
| 47 | mController = controller; |
| 48 | mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity); |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame^] | 49 | mHandler = new Handler() { |
| 50 | @Override |
| 51 | public void handleMessage(Message msg) { |
| 52 | if (msg.what == GET_PRIVATE_BROWSING_STATE_MSG) { |
| 53 | mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); |
| 54 | mPrivateBrowsingSignal.countDown(); |
| 55 | } |
| 56 | } |
| 57 | }; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void onPause() { |
| 61 | if (mNfcAdapter != null) { |
| 62 | mNfcAdapter.disableForegroundNdefPush(mActivity); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void onResume() { |
| 67 | if (mNfcAdapter != null) { |
| 68 | mNfcAdapter.enableForegroundNdefPush(mActivity, this); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public NdefMessage createMessage() { |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame^] | 74 | mCurrentTab = mController.getCurrentTab(); |
| 75 | if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { |
| 76 | // We can only read the WebView state on the UI thread, so post |
| 77 | // a message and wait. |
| 78 | mPrivateBrowsingSignal = new CountDownLatch(1); |
| 79 | mHandler.sendMessage(mHandler.obtainMessage(GET_PRIVATE_BROWSING_STATE_MSG)); |
| 80 | try { |
| 81 | mPrivateBrowsingSignal.await(); |
| 82 | } catch (InterruptedException e) { |
| 83 | return null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if ((mCurrentTab == null) || mIsPrivate) { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | String currentUrl = mCurrentTab.getUrl(); |
| 92 | if (currentUrl != null) { |
| 93 | NdefRecord record = NdefRecord.createUri(currentUrl); |
| 94 | NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); |
| 95 | return msg; |
| 96 | } else { |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 97 | return null; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void onMessagePushed() { |
| 103 | } |
| 104 | } |