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; |
Nick Pelly | 8e20143 | 2012-01-09 10:49:53 -0800 | [diff] [blame] | 26 | import android.util.Log; |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 27 | |
| 28 | import java.util.concurrent.CountDownLatch; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 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 | */ |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 35 | public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback { |
Nick Pelly | 8e20143 | 2012-01-09 10:49:53 -0800 | [diff] [blame] | 36 | static final String TAG = "BrowserNfcHandler"; |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 37 | static final int GET_PRIVATE_BROWSING_STATE_MSG = 100; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 38 | |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 39 | final Controller mController; |
Martijn Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame] | 40 | |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 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 | } |
Ben Murdoch | 015e1e3 | 2011-09-01 23:45:31 +0100 | [diff] [blame] | 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); |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public NfcHandler(Controller controller) { |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 65 | mController = controller; |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 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(); |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 74 | } |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 75 | } |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 76 | }; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 77 | |
| 78 | @Override |
Nick Pelly | b5fd116 | 2011-08-23 18:46:34 -0700 | [diff] [blame] | 79 | public NdefMessage createNdefMessage(NfcEvent event) { |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 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) { |
Nick Pelly | 8e20143 | 2012-01-09 10:49:53 -0800 | [diff] [blame] | 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 | } |
Ben Murdoch | 6b6a3bf | 2011-07-25 12:49:46 +0100 | [diff] [blame] | 105 | } else { |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 106 | return null; |
| 107 | } |
| 108 | } |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 109 | } |