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

* commit 'ae56a851624c2b2e78da8a2c339d2caa64c4f981':
  Make getSystemService(NFC_SERVICE) the NFC entry point.
This commit is contained in:
Nick Pelly
2010-12-10 17:19:45 -08:00
committed by Android Git Automerger
9 changed files with 216 additions and 61 deletions

View File

@ -35685,6 +35685,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"
@ -102203,6 +102214,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"
@ -102249,6 +102273,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"
>

View File

@ -71,6 +71,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;
@ -198,6 +199,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();
@ -974,6 +976,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;
@ -1201,6 +1205,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) {

View File

@ -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.

View File

@ -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;
}
sIsInitialized = true;
/* is this device meant to have NFC */
if (!hasNfcFeature()) {
Log.v(TAG, "this device does not have NFC support");
return null;
Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
"NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
return new NfcAdapter(null);
}
INfcAdapter service = getServiceInterface();
if (service == null) {
Log.e(TAG, "could not retrieve NFC service");
return null;
/*package*/ NfcAdapter(Context context) {
if (setupService() == null) {
throw new UnsupportedOperationException();
}
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;

View File

@ -0,0 +1,58 @@
/*
* 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;
}
}

View File

@ -127,7 +127,7 @@ public class Tag implements Parcelable {
/**
* Returns the technology, or null if not present
*/
public TagTechnology getTechnology(int tech) {
public TagTechnology getTechnology(NfcAdapter adapter, int tech) {
int pos = -1;
for (int idx = 0; idx < mTechList.length; idx++) {
if (mTechList[idx] == tech) {
@ -140,7 +140,6 @@ public class Tag implements Parcelable {
}
Bundle extras = mTechExtras[pos];
NfcAdapter adapter = NfcAdapter.getDefaultAdapter();
try {
switch (tech) {
case TagTechnology.NFC_A: {

View File

@ -6,9 +6,13 @@ NDEF message in NFC tags. A "tag" may actually be another device that appears as
<p>Here's a summary of the classes:</p>
<dl>
<dt>{@link android.nfc.NfcManager}</dt>
<dd>This is the high level manager, used to obtain this device's {@link android.nfc.NfcAdapter}. You can
acquire an instance using {@link android.content.Context#getSystemService}.</dd>
<dt>{@link android.nfc.NfcAdapter}</dt>
<dd>This represents the device's NFC adapter, which is your entry-point to performing NFC
operations. You can acquire an instance with {@link android.nfc.NfcAdapter#getDefaultAdapter}.</dd>
operations. You can acquire an instance with {@link android.nfc.NfcManager#getDefaultAdapter}, or
{@link android.nfc.NfcAdapter#getDefaultAdapter(android.content.Context)}.</dd>
<dt>{@link android.nfc.NdefMessage}</dt>
<dd>Represents an NDEF data message, which is the standard format in which "records"
carrying data are transmitted between devices and tags. Your application can receive these

View File

@ -74,7 +74,7 @@ public final class MifareClassic extends BasicTagTechnology {
super(adapter, tag, TagTechnology.MIFARE_CLASSIC);
// Check if this could actually be a Mifare
NfcA a = (NfcA) tag.getTechnology(TagTechnology.NFC_A);
NfcA a = (NfcA) tag.getTechnology(adapter, TagTechnology.NFC_A);
//short[] ATQA = getATQA(tag);
mIsEmulated = false;

View File

@ -16,13 +16,13 @@
package android.nfc.technology;
import java.io.IOException;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.RemoteException;
import java.io.IOException;
/**
* Concrete class for TagTechnology.MIFARE_ULTRALIGHT
*
@ -47,7 +47,7 @@ public final class MifareUltralight extends BasicTagTechnology {
super(adapter, tag, TagTechnology.MIFARE_ULTRALIGHT);
// Check if this could actually be a Mifare
NfcA a = (NfcA) tag.getTechnology(TagTechnology.NFC_A);
NfcA a = (NfcA) tag.getTechnology(adapter, TagTechnology.NFC_A);
mType = TYPE_UNKNOWN;
@ -73,9 +73,9 @@ public final class MifareUltralight extends BasicTagTechnology {
/**
* @throws IOException
*/
/*
/*
public byte[] readOTP();
public void writePage(int block, byte[] data);
public void writeBlock(int block, byte[] data);
*/
*/
}