blob: bc19950dc2c8edd8dc5d7223bcb8f60afa0d5b48 [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;
Martijn Coenenb2f93552011-06-14 10:48:35 +020024
25/** This class implements sharing the URL of the currently
26 * shown browser page over NFC. Sharing is only active
27 * when the activity is in the foreground and resumed.
28 * Incognito tabs will not be shared over NFC.
29 */
30public class NfcHandler implements NfcAdapter.NdefPushCallback {
31 private NfcAdapter mNfcAdapter;
32 private Activity mActivity;
33 private Controller mController;
34
Martijn Coeneneee21262011-07-04 11:16:08 +020035 /** We need an async task to check whether the tab is private
36 * on the UI thread.
37 */
38 private class CreateMessageTask extends AsyncTask<Void, Void, NdefMessage> {
39 private boolean mIsPrivate = false;
40 private Tab mCurrentTab;
41
42 @Override
43 protected void onPreExecute() {
44 mCurrentTab = mController.getCurrentTab();
45 if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) {
46 mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled();
47 }
48 }
49
50 @Override
51 protected NdefMessage doInBackground(Void... params) {
52 if ((mCurrentTab == null) || mIsPrivate) {
53 return null;
54 }
55 String currentUrl = mCurrentTab.getUrl();
56 if (currentUrl != null) {
57 NdefRecord record = NdefRecord.createUri(currentUrl);
58 NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
59 return msg;
60 } else {
61 return null;
62 }
63 }
64 }
65
Martijn Coenenb2f93552011-06-14 10:48:35 +020066 public NfcHandler(Activity browser, Controller controller) {
67 mActivity = browser;
68 mController = controller;
69 mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
70 }
71
72 void onPause() {
73 if (mNfcAdapter != null) {
74 mNfcAdapter.disableForegroundNdefPush(mActivity);
75 }
76 }
77
78 void onResume() {
79 if (mNfcAdapter != null) {
80 mNfcAdapter.enableForegroundNdefPush(mActivity, this);
81 }
82 }
83
84 @Override
85 public NdefMessage createMessage() {
Martijn Coeneneee21262011-07-04 11:16:08 +020086 CreateMessageTask task = new CreateMessageTask();
87 task.execute();
88 try {
89 return task.get();
90 } catch (Exception e) {
Martijn Coenenb2f93552011-06-14 10:48:35 +020091 return null;
92 }
93 }
94
95 @Override
96 public void onMessagePushed() {
97 }
98}