Commit 05e61eae authored by Francois Beaufort's avatar Francois Beaufort Committed by Commit Bot

Remove Android Beam support in Chrome for Android

This CL removes support for sharing URLs through NFC aka Android Beam as
it is being deprecating.

https://android-review.googlesource.com/q/topic:%22Deprecate-android-beam%22)

Change-Id: Idb8445e962cdb8d52d870c2f919b67e1b302520a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1989754
Commit-Queue: François Beaufort <beaufort.francois@gmail.com>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#735331}
parent f7fb02b8
......@@ -941,9 +941,6 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/net/nqe/NetworkQualityObserver.java",
"java/src/org/chromium/chrome/browser/net/nqe/NetworkQualityProvider.java",
"java/src/org/chromium/chrome/browser/net/spdyproxy/DataReductionProxySettings.java",
"java/src/org/chromium/chrome/browser/nfc/BeamCallback.java",
"java/src/org/chromium/chrome/browser/nfc/BeamController.java",
"java/src/org/chromium/chrome/browser/nfc/BeamProvider.java",
"java/src/org/chromium/chrome/browser/nfc/NfcSystemLevelPrompt.java",
"java/src/org/chromium/chrome/browser/night_mode/GlobalNightModeStateController.java",
"java/src/org/chromium/chrome/browser/night_mode/GlobalNightModeStateProviderHolder.java",
......
......@@ -106,7 +106,6 @@ import org.chromium.chrome.browser.metrics.ActivityTabStartupMetricsTracker;
import org.chromium.chrome.browser.metrics.LaunchMetrics;
import org.chromium.chrome.browser.metrics.UmaSessionStats;
import org.chromium.chrome.browser.multiwindow.MultiWindowUtils;
import org.chromium.chrome.browser.nfc.BeamController;
import org.chromium.chrome.browser.night_mode.NightModeReparentingController;
import org.chromium.chrome.browser.ntp.NewTabPage;
import org.chromium.chrome.browser.ntp.NewTabPageUma;
......@@ -1055,16 +1054,6 @@ public abstract class ChromeActivity<C extends ChromeActivityComponent>
UpdateMenuItemHelper.getInstance().registerObserver(mUpdateStateChangedListener);
});
DeferredStartupHandler.getInstance().addDeferredTask(() -> {
if (isActivityFinishingOrDestroyed()) return;
BeamController.registerForBeam(ChromeActivity.this, () -> {
Tab currentTab = getActivityTab();
if (currentTab == null) return null;
if (!currentTab.isUserInteractable()) return null;
return currentTab.getUrl();
});
});
final String simpleName = getClass().getSimpleName();
DeferredStartupHandler.getInstance().addDeferredTask(() -> {
if (isActivityFinishingOrDestroyed()) return;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.nfc;
import android.app.Activity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.util.UrlConstants;
import org.chromium.ui.widget.Toast;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Beam callback that gets passed to Android to get triggered when devices are tapped to
* each other.
*/
class BeamCallback implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {
private static class Status {
public final Integer errorStrID;
public final String result;
Status(Integer errorStrID) {
assert errorStrID != null;
this.errorStrID = errorStrID;
this.result = null;
}
Status(String result) {
assert result != null;
this.result = result;
this.errorStrID = null;
}
}
// Arbitrarily chosen interval to delay toast to allow NFC animations to finish
// and our app to return to foreground.
private static final int TOAST_ERROR_DELAY_MS = 400;
private final Activity mActivity;
private final BeamProvider mProvider;
// We use this to delay the error message in ICS because it would be hidden behind
// the system beam overlay. It is only accessed by the NFC thread
private Runnable mErrorRunnableIfBeamSent;
BeamCallback(Activity activity, BeamProvider provider) {
mActivity = activity;
mProvider = provider;
}
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
// Default status is an error
Status status = new Status(R.string.nfc_beam_error_bad_url);
try {
status = ThreadUtils.runOnUiThread(new Callable<Status>() {
@Override
public Status call() {
String url = mProvider.getTabUrlForBeam();
if (url == null) return new Status(R.string.nfc_beam_error_overlay_active);
if (!isValidUrl(url)) return new Status(R.string.nfc_beam_error_bad_url);
return new Status(url);
}
}).get(2000, TimeUnit.MILLISECONDS); // Arbitrarily chosen timeout for query.
} catch (TimeoutException e) {
// Squelch this exception, we'll treat it as a bad tab
} catch (ExecutionException e) {
// And this
} catch (InterruptedException e) {
// And squelch this one too
}
if (status.errorStrID != null) {
onInvalidBeam(status.errorStrID);
return null;
}
RecordUserAction.record("MobileBeamCallbackSuccess");
mErrorRunnableIfBeamSent = null;
return new NdefMessage(new NdefRecord[] {NdefRecord.createUri(status.result)});
}
/**
* Trigger an error about NFC if we don't want to send anything. Also
* records a UMA stat. On ICS we only show the error if they attempt to
* beam, since the recipient will receive the market link. On JB we'll
* always show the error, since the beam animation won't trigger, which
* could be confusing to the user.
*
* @param errorStringId The resid of the string to display as error.
*/
private void onInvalidBeam(final int errorStringId) {
RecordUserAction.record("MobileBeamInvalidAppState");
Runnable errorRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, errorStringId, Toast.LENGTH_SHORT).show();
}
};
ThreadUtils.runOnUiThread(errorRunnable);
}
@Override
public void onNdefPushComplete(NfcEvent event) {
if (mErrorRunnableIfBeamSent != null) {
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(mErrorRunnableIfBeamSent, TOAST_ERROR_DELAY_MS);
mErrorRunnableIfBeamSent = null;
}
}
/**
* @return Whether given URL is valid and sharable via Beam.
*/
private static boolean isValidUrl(String url) {
if (TextUtils.isEmpty(url)) return false;
try {
String urlProtocol = (new URL(url)).getProtocol();
return (UrlConstants.HTTP_SCHEME.equals(urlProtocol)
|| UrlConstants.HTTPS_SCHEME.equals(urlProtocol));
} catch (MalformedURLException e) {
return false;
}
}
}
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.nfc;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.os.Process;
import android.util.Log;
import org.chromium.base.ApiCompatibilityUtils;
/**
* Initializes Android Beam (sharing URL via NFC) for devices that have NFC. If user taps their
* device with another Beam capable device, then Chrome gets the current URL, filters for security
* and returns the result to Android.
*/
public final class BeamController {
/**
* If the device has NFC, construct a BeamCallback and pass it to Android.
*
* @param activity Activity that is sending out beam messages.
* @param provider Provider that returns the URL that should be shared.
*/
public static void registerForBeam(final Activity activity, final BeamProvider provider) {
final NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdapter == null) return;
if (ApiCompatibilityUtils.checkPermission(
activity, Manifest.permission.NFC, Process.myPid(), Process.myUid())
== PackageManager.PERMISSION_DENIED) {
return;
}
try {
final BeamCallback beamCallback = new BeamCallback(activity, provider);
nfcAdapter.setNdefPushMessageCallback(beamCallback, activity);
nfcAdapter.setOnNdefPushCompleteCallback(beamCallback, activity);
} catch (IllegalStateException e) {
Log.w("BeamController", "NFC registration failure. Can't retry, giving up.");
}
}
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.nfc;
/**
* Interface for Activities that use Beam (sharing URL via NFC) controller.
*/
public interface BeamProvider {
/**
* @return URL of the current tab, null otherwise (e.g. user is in tab switcher).
*/
String getTabUrlForBeam();
}
......@@ -4,18 +4,14 @@
package org.chromium.chrome.browser.settings;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.app.Fragment;
import android.support.v7.preference.Preference;
......@@ -111,15 +107,6 @@ public class SettingsActivity extends ChromeBaseAppCompatActivity
.commit();
}
if (ApiCompatibilityUtils.checkPermission(
this, Manifest.permission.NFC, Process.myPid(), Process.myUid())
== PackageManager.PERMISSION_GRANTED) {
// Disable Android Beam on JB and later devices.
// In ICS it does nothing - i.e. we will send a Play Store link if NFC is used.
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter != null) nfcAdapter.setNdefPushMessage(null, this);
}
Resources res = getResources();
ApiCompatibilityUtils.setTaskDescription(this, res.getString(R.string.app_name),
BitmapFactory.decodeResource(res, R.mipmap.app_icon),
......
......@@ -2134,14 +2134,6 @@ To change this setting, <ph name="BEGIN_LINK">&lt;resetlink&gt;</ph>reset sync<p
A
</message>
<!-- Android NFC Beam strings -->
<message name="IDS_NFC_BEAM_ERROR_OVERLAY_ACTIVE" desc="Android Beam error - a tab is not in the foreground [CHAR-LIMIT=40]">
Select a tab to beam
</message>
<message name="IDS_NFC_BEAM_ERROR_BAD_URL" desc="Android Beam error - the current tab has an invalid url [CHAR-LIMIT=40]">
Can’t beam current tab
</message>
<!-- Add to Home screen strings -->
<message name="IDS_MENU_ADD_TO_HOMESCREEN" desc="Menu item for adding a shortcut to the Home screen (default title). [CHAR-LIMIT=27]">
Add to Home screen
......
......@@ -11811,11 +11811,13 @@ should be able to be added at any place in this file.
</action>
<action name="MobileBeamCallbackSuccess">
<obsolete>Deprecated as of 1/2020</obsolete>
<owner>Please list the metric's owners. Add more owner tags as needed.</owner>
<description>Please enter the description of this user action.</description>
</action>
<action name="MobileBeamInvalidAppState">
<obsolete>Deprecated as of 1/2020</obsolete>
<owner>Please list the metric's owners. Add more owner tags as needed.</owner>
<description>Please enter the description of this user action.</description>
</action>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment