Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.nfc.NdefMessage; |
| 21 | import android.nfc.NdefRecord; |
| 22 | import android.nfc.NfcAdapter; |
Martijn Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame^] | 23 | import android.os.AsyncTask; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 24 | |
| 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 | */ |
| 30 | public class NfcHandler implements NfcAdapter.NdefPushCallback { |
| 31 | private NfcAdapter mNfcAdapter; |
| 32 | private Activity mActivity; |
| 33 | private Controller mController; |
| 34 | |
Martijn Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame^] | 35 | /** 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 Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 66 | 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 Coenen | eee2126 | 2011-07-04 11:16:08 +0200 | [diff] [blame^] | 86 | CreateMessageTask task = new CreateMessageTask(); |
| 87 | task.execute(); |
| 88 | try { |
| 89 | return task.get(); |
| 90 | } catch (Exception e) { |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 91 | return null; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onMessagePushed() { |
| 97 | } |
| 98 | } |