Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e372c17b authored by Jason parks's avatar Jason parks Committed by Android (Google) Code Review
Browse files

Merge "Add public API to do NDEF push."

parents 4b4e3a0b 01425365
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ LOCAL_SRC_FILES += \
	core/java/android/nfc/ILlcpConnectionlessSocket.aidl \
	core/java/android/nfc/ILlcpServiceSocket.aidl \
	core/java/android/nfc/ILlcpSocket.aidl \
	core/java/android/nfc/INdefPushCallback.aidl \
	core/java/android/nfc/INfcAdapter.aidl \
	core/java/android/nfc/INfcAdapterExtras.aidl \
	core/java/android/nfc/INfcTag.aidl \
+6 −0
Original line number Diff line number Diff line
@@ -11643,6 +11643,7 @@ package android.nfc {
    method public void disableForegroundNdefPush(android.app.Activity);
    method public void enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][]);
    method public void enableForegroundNdefPush(android.app.Activity, android.nfc.NdefMessage);
    method public void enableForegroundNdefPush(android.app.Activity, android.nfc.NfcAdapter.NdefPushCallback);
    method public static android.nfc.NfcAdapter getDefaultAdapter(android.content.Context);
    method public static deprecated android.nfc.NfcAdapter getDefaultAdapter();
    method public boolean isEnabled();
@@ -11654,6 +11655,11 @@ package android.nfc {
    field public static final java.lang.String EXTRA_TAG = "android.nfc.extra.TAG";
  }
  public static abstract interface NfcAdapter.NdefPushCallback {
    method public abstract android.nfc.NdefMessage createMessage();
    method public abstract void onMessagePushed();
  }
  public final class NfcManager {
    method public android.nfc.NfcAdapter getDefaultAdapter();
  }
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.nfc;

import android.nfc.NdefMessage;

/**
 * @hide
 */
interface INdefPushCallback
{
    NdefMessage onConnect();
    void onMessagePushed();
}
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.nfc.TechListParcel;
import android.nfc.ILlcpSocket;
import android.nfc.ILlcpServiceSocket;
import android.nfc.ILlcpConnectionlessSocket;
import android.nfc.INdefPushCallback;
import android.nfc.INfcTag;
import android.nfc.IP2pTarget;
import android.nfc.IP2pInitiator;
@@ -51,6 +52,7 @@ interface INfcAdapter
            in IntentFilter[] filters, in TechListParcel techLists);
    void disableForegroundDispatch(in ComponentName activity);
    void enableForegroundNdefPush(in ComponentName activity, in NdefMessage msg);
    void enableForegroundNdefPushWithCallback(in ComponentName activity, in INdefPushCallback callback);
    void disableForegroundNdefPush(in ComponentName activity);

    // Non-public methods
+70 −1
Original line number Diff line number Diff line
@@ -235,6 +235,37 @@ public final class NfcAdapter {
     */
    private static final int DISCOVERY_MODE_CARD_EMULATION = 2;

    /**
     * Callback passed into {@link #enableForegroundNdefPush(Activity,NdefPushCallback)}. This
     */
    public interface NdefPushCallback {
        /**
         * Called when a P2P connection is created.
         */
        NdefMessage createMessage();
        /**
         * Called when the message is pushed.
         */
        void onMessagePushed();
    }

    private static class NdefPushCallbackWrapper extends INdefPushCallback.Stub {
        private NdefPushCallback mCallback;

        public NdefPushCallbackWrapper(NdefPushCallback callback) {
            mCallback = callback;
        }

        @Override
        public NdefMessage onConnect() {
            return mCallback.createMessage();
        }

        @Override
        public void onMessagePushed() {
            mCallback.onMessagePushed();
        }
    }

    // Guarded by NfcAdapter.class
    private static boolean sIsInitialized = false;
@@ -574,6 +605,44 @@ public final class NfcAdapter {
        }
    }

    /**
     * Enable NDEF message push over P2P while this Activity is in the foreground.
     *
     * <p>For this to function properly the other NFC device being scanned must
     * support the "com.android.npp" NDEF push protocol. Support for this
     * protocol is currently optional for Android NFC devices.
     *
     * <p>This method must be called from the main thread.
     *
     * <p class="note"><em>NOTE:</em> While foreground NDEF push is active standard tag dispatch is disabled.
     * Only the foreground activity may receive tag discovered dispatches via
     * {@link #enableForegroundDispatch}.
     *
     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
     *
     * @param activity the foreground Activity
     * @param callback is called on when the P2P connection is established
     * @throws IllegalStateException if the Activity is not currently in the foreground
     * @throws OperationNotSupportedException if this Android device does not support NDEF push
     */
    public void enableForegroundNdefPush(Activity activity, NdefPushCallback callback) {
        if (activity == null || callback == null) {
            throw new NullPointerException();
        }
        if (!activity.isResumed()) {
            throw new IllegalStateException("Foregorund NDEF push can only be enabled " +
                    "when your activity is resumed");
        }
        try {
            ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
                    mForegroundNdefPushListener);
            sService.enableForegroundNdefPushWithCallback(activity.getComponentName(),
                    new NdefPushCallbackWrapper(callback));
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }
    }

    /**
     * Disable NDEF message push over P2P.
     *