blob: a8e11d38e3e3b197e4ff23b11d46db6ebb9b71a2 [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;
Martijn Coeneneee21262011-07-04 11:16:08 +020023import android.os.AsyncTask;
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 */
34public class NfcHandler implements NfcAdapter.NdefPushCallback {
35 private NfcAdapter mNfcAdapter;
36 private Activity mActivity;
37 private Controller mController;
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010038 private Handler mHandler;
39 private Tab mCurrentTab;
40 private boolean mIsPrivate;
41 private CountDownLatch mPrivateBrowsingSignal;
Martijn Coenenb2f93552011-06-14 10:48:35 +020042
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010043 private static final int GET_PRIVATE_BROWSING_STATE_MSG = 100;
Martijn Coeneneee21262011-07-04 11:16:08 +020044
Martijn Coenenb2f93552011-06-14 10:48:35 +020045 public NfcHandler(Activity browser, Controller controller) {
46 mActivity = browser;
47 mController = controller;
48 mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
Ben Murdoch6b6a3bf2011-07-25 12:49:46 +010049 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 Coenenb2f93552011-06-14 10:48:35 +020058 }
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 Murdoch6b6a3bf2011-07-25 12:49:46 +010074 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 Coenenb2f93552011-06-14 10:48:35 +020097 return null;
98 }
99 }
100
101 @Override
102 public void onMessagePushed() {
103 }
104}