// 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.app.Activity;
import android.nfc.NfcAdapter;
import android.util.Log;

/**
 * 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;
        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 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.app.Activity;
import android.nfc.NfcAdapter;
import android.util.Log;

/**
 * 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;
        try {
            final BeamCallback beamCallback = new BeamCallback(activity, provider);
            nfcAdapter.setNdefPushMessageCallback(beamCallback, activity);
            nfcAdapter.setOnNdefPushCompleteCallback(beamCallback, activity);
        } catch (IllegalStateException | SecurityException e) {
            Log.w("BeamController", "NFC registration failure. Can't retry, giving up.");
        }
    }
}

Handle NFC permission being unavailable.
Handle NFC permission being unavailable.

With a rooted device, some apps can remove permissions. Prevents chrome
from crashing if NFC is revoked.

BUG=481979

Review URL: https://codereview.chromium.org/1104053003

Cr-Commit-Position: 972c6d2dc6dd5efdad1377c0d224e03eb8f276f7@{#327571}
