blob: bdfe25ef71f894d1175ba678079f8390074dc6b7 [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;
23
24/** This class implements sharing the URL of the currently
25 * shown browser page over NFC. Sharing is only active
26 * when the activity is in the foreground and resumed.
27 * Incognito tabs will not be shared over NFC.
28 */
29public class NfcHandler implements NfcAdapter.NdefPushCallback {
30 private NfcAdapter mNfcAdapter;
31 private Activity mActivity;
32 private Controller mController;
33
34 public NfcHandler(Activity browser, Controller controller) {
35 mActivity = browser;
36 mController = controller;
37 mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
38 }
39
40 void onPause() {
41 if (mNfcAdapter != null) {
42 mNfcAdapter.disableForegroundNdefPush(mActivity);
43 }
44 }
45
46 void onResume() {
47 if (mNfcAdapter != null) {
48 mNfcAdapter.enableForegroundNdefPush(mActivity, this);
49 }
50 }
51
52 @Override
53 public NdefMessage createMessage() {
54 Tab currentTab = mController.getCurrentTab();
55 if (currentTab == null) {
56 return null;
57 }
58 String currentUrl = currentTab.getUrl();
59 if (currentUrl != null && currentTab.getWebView() != null &&
60 !currentTab.getWebView().isPrivateBrowsingEnabled()) {
61 NdefRecord record = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI,
62 NdefRecord.RTD_URI, new byte[] {}, currentUrl.getBytes());
63 NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
64 return msg;
65 } else {
66 return null;
67 }
68 }
69
70 @Override
71 public void onMessagePushed() {
72 }
73}