blob: 6147aea40f3d80e5daa7c41be5280efcfc428832 [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 }
48 adapter.setNdefPushMessageCallback(new NfcHandler(controller), activity);
49 }
50
51 public NfcHandler(Controller controller) {
Martijn Coenenb2f93552011-06-14 10:48:35 +020052 mController = controller;
Nick Pellyb5fd1162011-08-23 18:46:34 -070053 }
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 Murdoch6b6a3bf2011-07-25 12:49:46 +010061 }
Martijn Coenenb2f93552011-06-14 10:48:35 +020062 }
Nick Pellyb5fd1162011-08-23 18:46:34 -070063 };
Martijn Coenenb2f93552011-06-14 10:48:35 +020064
65 @Override
Nick Pellyb5fd1162011-08-23 18:46:34 -070066 public NdefMessage createNdefMessage(NfcEvent event) {
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010067 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 Coenenb2f93552011-06-14 10:48:35 +020090 return null;
91 }
92 }
Martijn Coenenb2f93552011-06-14 10:48:35 +020093}