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

Commit ae56a851 authored by Nick Pelly's avatar Nick Pelly Committed by Android (Google) Code Review
Browse files

Merge "Make getSystemService(NFC_SERVICE) the NFC entry point." into gingerbread

parents 7a1f15b1 50b4d8f6
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -35673,6 +35673,17 @@
 visibility="public"
>
</field>
<field name="NFC_SERVICE"
 type="java.lang.String"
 transient="false"
 volatile="false"
 value="&quot;nfc&quot;"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="NOTIFICATION_SERVICE"
 type="java.lang.String"
 transient="false"
@@ -100183,6 +100194,19 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="context" type="android.content.Context">
</parameter>
</method>
<method name="getDefaultAdapter"
 return="android.nfc.NfcAdapter"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="deprecated"
 visibility="public"
>
</method>
<method name="isEnabled"
 return="boolean"
@@ -100229,6 +100253,26 @@
>
</field>
</class>
<class name="NfcManager"
 extends="java.lang.Object"
 abstract="false"
 static="false"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
<method name="getDefaultAdapter"
 return="android.nfc.NfcAdapter"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
</class>
</package>
<package name="android.opengl"
>
+13 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ import android.net.IThrottleManager;
import android.net.Uri;
import android.net.wifi.IWifiManager;
import android.net.wifi.WifiManager;
import android.nfc.NfcManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.DropBoxManager;
@@ -201,6 +202,7 @@ class ContextImpl extends Context {
    private DevicePolicyManager mDevicePolicyManager = null;
    private UiModeManager mUiModeManager = null;
    private DownloadManager mDownloadManager = null;
    private NfcManager mNfcManager = null;

    private final Object mSync = new Object();

@@ -977,6 +979,8 @@ class ContextImpl extends Context {
            return getUiModeManager();
        } else if (DOWNLOAD_SERVICE.equals(name)) {
            return getDownloadManager();
        } else if (NFC_SERVICE.equals(name)) {
            return getNfcManager();
        }

        return null;
@@ -1204,6 +1208,15 @@ class ContextImpl extends Context {
        return mDownloadManager;
    }

    private NfcManager getNfcManager() {
        synchronized (mSync) {
            if (mNfcManager == null) {
                mNfcManager = new NfcManager(this);
            }
        }
        return mNfcManager;
    }

    @Override
    public int checkPermission(String permission, int pid, int uid) {
        if (permission == null) {
+8 −0
Original line number Diff line number Diff line
@@ -1549,6 +1549,14 @@ public abstract class Context {
     */
    public static final String DOWNLOAD_SERVICE = "download";

    /**
     * Use with {@link #getSystemService} to retrieve a
     * {@link android.nfc.NfcManager} for using NFC.
     *
     * @see #getSystemService
     */
    public static final String NFC_SERVICE = "nfc";

    /**
     * Use with {@link #getSystemService} to retrieve a
     * {@link android.net.sip.SipManager} for accessing the SIP related service.
+78 −49
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.nfc;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.app.ActivityThread;
import android.content.Context;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.os.IBinder;
@@ -29,11 +30,12 @@ import android.util.Log;
/**
 * Represents the device's local NFC adapter.
 * <p>
 * Use the static {@link #getDefaultAdapter} method to get the default NFC
 * Adapter for this Android device. Most Android devices will have only one NFC
 * Adapter, and {@link #getDefaultAdapter} returns the singleton object.
 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
 * adapter for this Android device.
 */
public final class NfcAdapter {
    private static final String TAG = "NFC";

    /**
     * Intent to start an activity when a tag is discovered.
     */
@@ -161,28 +163,16 @@ public final class NfcAdapter {
     */
    private static final int DISCOVERY_MODE_CARD_EMULATION = 2;

    private static final String TAG = "NFC";

    // Both guarded by NfcAdapter.class:
    // Guarded by NfcAdapter.class
    private static boolean sIsInitialized = false;
    private static NfcAdapter sAdapter;

    // Final after construction, except for attemptDeadServiceRecovery()
    // when NFC crashes.
    // Not locked - we accept a best effort attempt when NFC crashes.
    /*package*/ INfcAdapter mService;
    // Final after first constructor, except for
    // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
    // recovery
    private static INfcAdapter sService;

    private NfcAdapter(INfcAdapter service) {
        mService = service;
    }

    /**
     * Returns the binder interface to the service.
     * @hide
     */
    public INfcAdapter getService() {
        return mService;
    }
    private final Context mContext;

    /**
     * Helper to check if this device has FEATURE_NFC, but without using
@@ -204,8 +194,27 @@ public final class NfcAdapter {
        }
    }

    private static synchronized INfcAdapter setupService() {
        if (!sIsInitialized) {
            sIsInitialized = true;

            /* is this device meant to have NFC */
            if (!hasNfcFeature()) {
                Log.v(TAG, "this device does not have NFC support");
                return null;
            }

            sService = getServiceInterface();
            if (sService == null) {
                Log.e(TAG, "could not retrieve NFC service");
                return null;
            }
        }
        return sService;
    }

    /** get handle to NFC service interface */
    private static synchronized INfcAdapter getServiceInterface() {
    private static INfcAdapter getServiceInterface() {
        /* get a handle to NFC service */
        IBinder b = ServiceManager.getService("nfc");
        if (b == null) {
@@ -214,35 +223,55 @@ public final class NfcAdapter {
        return INfcAdapter.Stub.asInterface(b);
    }

    /**
     * Helper to get the default NFC Adapter.
     * <p>
     * Most Android devices will only have one NFC Adapter (NFC Controller).
     * <p>
     * This helper is the equivalent of:
     * <pre>{@code
     * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
     * NfcAdapter adapter = manager.getDefaultAdapter();
     * }</pre>
     * @param context the calling application's context
     *
     * @return the default NFC adapter, or null if no NFC adapter exists
     */
    public static NfcAdapter getDefaultAdapter(Context context) {
        /* use getSystemService() instead of just instantiating to take
         * advantage of the context's cached NfcManager & NfcAdapter */
        NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
        return manager.getDefaultAdapter();
    }

    /**
     * Get a handle to the default NFC Adapter on this Android device.
     * <p>
     * Most Android devices will only have one NFC Adapter (NFC Controller).
     *
     * @return the default NFC adapter, or null if no NFC adapter exists
     * @deprecated use {@link #getDefaultAdapter(Context)}
     */
    @Deprecated
    public static NfcAdapter getDefaultAdapter() {
        synchronized (NfcAdapter.class) {
            if (sIsInitialized) {
                return sAdapter;
        Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
                "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
        return new NfcAdapter(null);
    }
            sIsInitialized = true;

            /* is this device meant to have NFC */
            if (!hasNfcFeature()) {
                Log.v(TAG, "this device does not have NFC support");
                return null;
    /*package*/ NfcAdapter(Context context) {
        if (setupService() == null) {
            throw new UnsupportedOperationException();
        }

            INfcAdapter service = getServiceInterface();
            if (service == null) {
                Log.e(TAG, "could not retrieve NFC service");
                return null;
        mContext = context;
    }

            sAdapter = new NfcAdapter(service);
            return sAdapter;
        }
    /**
     * Returns the binder interface to the service.
     * @hide
     */
    public INfcAdapter getService() {
        return sService;
    }

    /**
@@ -256,9 +285,9 @@ public final class NfcAdapter {
            Log.e(TAG, "could not retrieve NFC service during service recovery");
            return;
        }
        /* assigning to mService is not thread-safe, but this is best-effort code
        /* assigning to sService is not thread-safe, but this is best-effort code
         * and on a well-behaved system should never happen */
        mService = service;
        sService = service;
        return;
    }

@@ -275,7 +304,7 @@ public final class NfcAdapter {
     */
    public boolean isEnabled() {
        try {
            return mService.isEnabled();
            return sService.isEnabled();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return false;
@@ -292,7 +321,7 @@ public final class NfcAdapter {
     */
    public boolean enable() {
        try {
            return mService.enable();
            return sService.enable();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return false;
@@ -311,7 +340,7 @@ public final class NfcAdapter {
     */
    public boolean disable() {
        try {
            return mService.disable();
            return sService.disable();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return false;
@@ -338,7 +367,7 @@ public final class NfcAdapter {
     */
    public void setLocalNdefMessage(NdefMessage message) {
        try {
            mService.localSet(message);
            sService.localSet(message);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }
@@ -353,7 +382,7 @@ public final class NfcAdapter {
     */
    public NdefMessage getLocalNdefMessage() {
        try {
            return mService.localGet();
            return sService.localGet();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return null;
@@ -366,7 +395,7 @@ public final class NfcAdapter {
     */
    public NfcSecureElement createNfcSecureElementConnection() {
        try {
            return new NfcSecureElement(mService.getNfcSecureElementInterface());
            return new NfcSecureElement(sService.getNfcSecureElementInterface());
        } catch (RemoteException e) {
            Log.e(TAG, "createNfcSecureElementConnection failed", e);
            return null;
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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.content.Context;

/**
 * High level manager used to obtain an instance of an {@link NfcAdapter}.
 * <p>
 * Use {@link android.content.Context#getSystemService(java.lang.String)}
 * with {@link Context#NFC_SERVICE} to create an {@link NfcManager},
 * then call {@link #getDefaultAdapter} to obtain the {@link NfcAdapter}.
 * <p>
 * Alternately, you can just call the static helper
 * {@link NfcAdapter#getDefaultAdapter(android.content.Context)}.
 *
 * @see Context#getSystemService
 * @see NfcAdapter#getDefaultAdapter(android.content.Context)
 */
public final class NfcManager {
    private final NfcAdapter mAdapter;

    /**
     * @hide
     */
    public NfcManager(Context context) {
        NfcAdapter adapter;
        try {
            adapter = new NfcAdapter(context);
        } catch (UnsupportedOperationException e) {
            adapter = null;
        }
        mAdapter = adapter;
    }

    /**
     * Get the default NFC Adapter for this device.
     *
     * @return the default NFC Adapter
     */
    public NfcAdapter getDefaultAdapter() {
        return mAdapter;
    }
}
Loading