diff options
author | Ben Murdoch <benm@google.com> | 2011-07-25 09:38:43 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-07-25 09:38:43 -0700 |
commit | 420d149566e19f1663db69420375b6dd1f0acd6f (patch) | |
tree | 5ae789cb68c7b9d91f7a510461157a2d45d3e128 /src/com/android/browser/NfcHandler.java | |
parent | c8b708c379ec8ef47772d186fefe385ef7e12359 (diff) | |
parent | aaa1f375bc9d6a6a175819fac34d39202f69a166 (diff) | |
download | packages_apps_browser-420d149566e19f1663db69420375b6dd1f0acd6f.zip packages_apps_browser-420d149566e19f1663db69420375b6dd1f0acd6f.tar.gz packages_apps_browser-420d149566e19f1663db69420375b6dd1f0acd6f.tar.bz2 |
Merge changes I78550965,I6e0fe0ba,Ia6db6cf3
* changes:
Cache the factory reset URL.
Fix StrictMode violation in InstantSearchEngine
Query Private Browsing state for NFC on UI thread.
Diffstat (limited to 'src/com/android/browser/NfcHandler.java')
-rw-r--r-- | src/com/android/browser/NfcHandler.java | 76 |
1 files changed, 41 insertions, 35 deletions
diff --git a/src/com/android/browser/NfcHandler.java b/src/com/android/browser/NfcHandler.java index bc19950..a8e11d3 100644 --- a/src/com/android/browser/NfcHandler.java +++ b/src/com/android/browser/NfcHandler.java @@ -21,6 +21,10 @@ import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.os.AsyncTask; +import android.os.Handler; +import android.os.Message; + +import java.util.concurrent.CountDownLatch; /** This class implements sharing the URL of the currently * shown browser page over NFC. Sharing is only active @@ -31,42 +35,26 @@ public class NfcHandler implements NfcAdapter.NdefPushCallback { private NfcAdapter mNfcAdapter; private Activity mActivity; private Controller mController; + private Handler mHandler; + private Tab mCurrentTab; + private boolean mIsPrivate; + private CountDownLatch mPrivateBrowsingSignal; - /** We need an async task to check whether the tab is private - * on the UI thread. - */ - private class CreateMessageTask extends AsyncTask<Void, Void, NdefMessage> { - private boolean mIsPrivate = false; - private Tab mCurrentTab; - - @Override - protected void onPreExecute() { - mCurrentTab = mController.getCurrentTab(); - if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { - mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); - } - } - - @Override - protected NdefMessage doInBackground(Void... params) { - if ((mCurrentTab == null) || mIsPrivate) { - return null; - } - String currentUrl = mCurrentTab.getUrl(); - if (currentUrl != null) { - NdefRecord record = NdefRecord.createUri(currentUrl); - NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); - return msg; - } else { - return null; - } - } - } + private static final int GET_PRIVATE_BROWSING_STATE_MSG = 100; public NfcHandler(Activity browser, Controller controller) { mActivity = browser; mController = controller; mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity); + mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + if (msg.what == GET_PRIVATE_BROWSING_STATE_MSG) { + mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); + mPrivateBrowsingSignal.countDown(); + } + } + }; } void onPause() { @@ -83,11 +71,29 @@ public class NfcHandler implements NfcAdapter.NdefPushCallback { @Override public NdefMessage createMessage() { - CreateMessageTask task = new CreateMessageTask(); - task.execute(); - try { - return task.get(); - } catch (Exception e) { + mCurrentTab = mController.getCurrentTab(); + if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { + // We can only read the WebView state on the UI thread, so post + // a message and wait. + mPrivateBrowsingSignal = new CountDownLatch(1); + mHandler.sendMessage(mHandler.obtainMessage(GET_PRIVATE_BROWSING_STATE_MSG)); + try { + mPrivateBrowsingSignal.await(); + } catch (InterruptedException e) { + return null; + } + } + + if ((mCurrentTab == null) || mIsPrivate) { + return null; + } + + String currentUrl = mCurrentTab.getUrl(); + if (currentUrl != null) { + NdefRecord record = NdefRecord.createUri(currentUrl); + NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); + return msg; + } else { return null; } } |