blob: 0dd85769a1536908d94ae241f334102ea93d1a6f [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;
Nick Pelly8e201432012-01-09 10:49:53 -080026import android.util.Log;
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010027
28import java.util.concurrent.CountDownLatch;
Martijn Coenenb2f93552011-06-14 10:48:35 +020029
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 Pellyb5fd1162011-08-23 18:46:34 -070035public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback {
Nick Pelly8e201432012-01-09 10:49:53 -080036 static final String TAG = "BrowserNfcHandler";
Nick Pellyb5fd1162011-08-23 18:46:34 -070037 static final int GET_PRIVATE_BROWSING_STATE_MSG = 100;
Martijn Coenenb2f93552011-06-14 10:48:35 +020038
Nick Pellyb5fd1162011-08-23 18:46:34 -070039 final Controller mController;
Martijn Coeneneee21262011-07-04 11:16:08 +020040
Nick Pellyb5fd1162011-08-23 18:46:34 -070041 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 Murdoch015e1e32011-09-01 23:45:31 +010050 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 Pellyb5fd1162011-08-23 18:46:34 -070062 }
63
64 public NfcHandler(Controller controller) {
Martijn Coenenb2f93552011-06-14 10:48:35 +020065 mController = controller;
Nick Pellyb5fd1162011-08-23 18:46:34 -070066 }
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 Murdoch6b6a3bf2011-07-25 12:49:46 +010074 }
Martijn Coenenb2f93552011-06-14 10:48:35 +020075 }
Nick Pellyb5fd1162011-08-23 18:46:34 -070076 };
Martijn Coenenb2f93552011-06-14 10:48:35 +020077
78 @Override
Nick Pellyb5fd1162011-08-23 18:46:34 -070079 public NdefMessage createNdefMessage(NfcEvent event) {
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010080 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 Pelly8e201432012-01-09 10:49:53 -080099 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 Murdoch6b6a3bf2011-07-25 12:49:46 +0100105 } else {
Martijn Coenenb2f93552011-06-14 10:48:35 +0200106 return null;
107 }
108 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200109}