blob: 1b34df2b9e52bd5be14b69c9655e7b0fa4192b8c [file] [log] [blame]
Martijn Coenenb2f93552011-06-14 10:48:35 +02001/*
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;
Nick Pellyb5fd1162011-08-23 18:46:34 -070023import android.nfc.NfcEvent;
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010024import android.os.Handler;
25import android.os.Message;
26
27import java.util.concurrent.CountDownLatch;
Martijn Coenenb2f93552011-06-14 10:48:35 +020028
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 Pellyb5fd1162011-08-23 18:46:34 -070034public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback {
35 static final int GET_PRIVATE_BROWSING_STATE_MSG = 100;
Martijn Coenenb2f93552011-06-14 10:48:35 +020036
Nick Pellyb5fd1162011-08-23 18:46:34 -070037 final Controller mController;
Martijn Coeneneee21262011-07-04 11:16:08 +020038
Nick Pellyb5fd1162011-08-23 18:46:34 -070039 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 }
Ben Murdoch015e1e32011-09-01 23:45:31 +010048 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);
Nick Pellyb5fd1162011-08-23 18:46:34 -070060 }
61
62 public NfcHandler(Controller controller) {
Martijn Coenenb2f93552011-06-14 10:48:35 +020063 mController = controller;
Nick Pellyb5fd1162011-08-23 18:46:34 -070064 }
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();
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010072 }
Martijn Coenenb2f93552011-06-14 10:48:35 +020073 }
Nick Pellyb5fd1162011-08-23 18:46:34 -070074 };
Martijn Coenenb2f93552011-06-14 10:48:35 +020075
76 @Override
Nick Pellyb5fd1162011-08-23 18:46:34 -070077 public NdefMessage createNdefMessage(NfcEvent event) {
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010078 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 {
Martijn Coenenb2f93552011-06-14 10:48:35 +0200101 return null;
102 }
103 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200104}