/src/com/android/nfc/NfcService.java
Java | 3101 lines | 2338 code | 465 blank | 298 comment | 533 complexity | fe76b2efa156b3871c8c0abb7ef7e4b7 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * 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 com.android.nfc;
- import com.android.internal.nfc.LlcpServiceSocket;
- import com.android.internal.nfc.LlcpSocket;
- import com.android.nfc.RegisteredComponentCache.ComponentInfo;
- import com.android.nfc.ndefpush.NdefPushClient;
- import com.android.nfc.ndefpush.NdefPushServer;
- import com.android.nfc3.R;
- import android.app.Activity;
- import android.app.ActivityManagerNative;
- import android.app.Application;
- import android.app.IActivityManager;
- import android.app.PendingIntent;
- import android.app.PendingIntent.CanceledException;
- import android.app.StatusBarManager;
- import android.content.ActivityNotFoundException;
- import android.content.BroadcastReceiver;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.net.Uri;
- import android.nfc.ApduList;
- import android.nfc.ErrorCodes;
- import android.nfc.FormatException;
- import android.nfc.ILlcpConnectionlessSocket;
- import android.nfc.ILlcpServiceSocket;
- import android.nfc.ILlcpSocket;
- import android.nfc.INfcAdapter;
- import android.nfc.INfcAdapterExtras;
- import android.nfc.INfcTag;
- import android.nfc.IP2pInitiator;
- import android.nfc.IP2pTarget;
- import android.nfc.LlcpPacket;
- import android.nfc.NdefMessage;
- import android.nfc.NdefRecord;
- import android.nfc.NfcAdapter;
- import android.nfc.Tag;
- import android.nfc.TechListParcel;
- import android.nfc.TransceiveResult;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.os.PowerManager;
- import android.os.RemoteException;
- import android.os.ServiceManager;
- import android.util.Log;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.charset.Charsets;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.List;
- public class NfcService extends Application {
- private static final String ACTION_MASTER_CLEAR_NOTIFICATION = "android.intent.action.MASTER_CLEAR_NOTIFICATION";
- static final boolean DBG = false;
- private static final String MY_TAG_FILE_NAME = "mytag";
- private static final String SE_RESET_SCRIPT_FILE_NAME = "/system/etc/se-reset-script";
- private static final String NFC_CONTROLLER_FIRMWARE_FILE_NAME = "/system/lib/libpn544_fw.so";
- static {
- System.loadLibrary("nfc_jni");
- }
- /**
- * NFC Forum "URI Record Type Definition"
- *
- * This is a mapping of "URI Identifier Codes" to URI string prefixes,
- * per section 3.2.2 of the NFC Forum URI Record Type Definition document.
- */
- private static final String[] URI_PREFIX_MAP = new String[] {
- "", // 0x00
- "http://www.", // 0x01
- "https://www.", // 0x02
- "http://", // 0x03
- "https://", // 0x04
- "tel:", // 0x05
- "mailto:", // 0x06
- "ftp://anonymous:anonymous@", // 0x07
- "ftp://ftp.", // 0x08
- "ftps://", // 0x09
- "sftp://", // 0x0A
- "smb://", // 0x0B
- "nfs://", // 0x0C
- "ftp://", // 0x0D
- "dav://", // 0x0E
- "news:", // 0x0F
- "telnet://", // 0x10
- "imap:", // 0x11
- "rtsp://", // 0x12
- "urn:", // 0x13
- "pop:", // 0x14
- "sip:", // 0x15
- "sips:", // 0x16
- "tftp:", // 0x17
- "btspp://", // 0x18
- "btl2cap://", // 0x19
- "btgoep://", // 0x1A
- "tcpobex://", // 0x1B
- "irdaobex://", // 0x1C
- "file://", // 0x1D
- "urn:epc:id:", // 0x1E
- "urn:epc:tag:", // 0x1F
- "urn:epc:pat:", // 0x20
- "urn:epc:raw:", // 0x21
- "urn:epc:", // 0x22
- };
- public static final String SERVICE_NAME = "nfc";
- private static final String TAG = "NfcService";
- private static final String NFC_PERM = android.Manifest.permission.NFC;
- private static final String NFC_PERM_ERROR = "NFC permission required";
- private static final String ADMIN_PERM = android.Manifest.permission.WRITE_SECURE_SETTINGS;
- private static final String ADMIN_PERM_ERROR = "WRITE_SECURE_SETTINGS permission required";
- private static final String NFCEE_ADMIN_PERM = "com.android.nfc.permission.NFCEE_ADMIN";
- private static final String NFCEE_ADMIN_PERM_ERROR = "NFCEE_ADMIN permission required";
- private static final String PREF = "NfcServicePrefs";
- private static final String PREF_NFC_ON = "nfc_on";
- private static final boolean NFC_ON_DEFAULT = true;
- private static final String PREF_FIRST_BOOT = "first_boot";
- private static final String PREF_LLCP_LTO = "llcp_lto";
- private static final int LLCP_LTO_DEFAULT = 150;
- private static final int LLCP_LTO_MAX = 255;
- /** Maximum Information Unit */
- private static final String PREF_LLCP_MIU = "llcp_miu";
- private static final int LLCP_MIU_DEFAULT = 128;
- private static final int LLCP_MIU_MAX = 2176;
- /** Well Known Service List */
- private static final String PREF_LLCP_WKS = "llcp_wks";
- private static final int LLCP_WKS_DEFAULT = 1;
- private static final int LLCP_WKS_MAX = 15;
- private static final String PREF_LLCP_OPT = "llcp_opt";
- private static final int LLCP_OPT_DEFAULT = 0;
- private static final int LLCP_OPT_MAX = 3;
- private static final String PREF_DISCOVERY_A = "discovery_a";
- private static final boolean DISCOVERY_A_DEFAULT = true;
- private static final String PREF_DISCOVERY_B = "discovery_b";
- private static final boolean DISCOVERY_B_DEFAULT = true;
- private static final String PREF_DISCOVERY_F = "discovery_f";
- private static final boolean DISCOVERY_F_DEFAULT = true;
- private static final String PREF_DISCOVERY_15693 = "discovery_15693";
- private static final boolean DISCOVERY_15693_DEFAULT = true;
- private static final String PREF_DISCOVERY_NFCIP = "discovery_nfcip";
- private static final boolean DISCOVERY_NFCIP_DEFAULT = true;
- private static final String PREF_FIRMWARE_MODTIME = "firmware_modtime";
- private static final long FIRMWARE_MODTIME_DEFAULT = -1;
- /** NFC Reader Discovery mode for enableDiscovery() */
- private static final int DISCOVERY_MODE_READER = 0;
- private static final int PROPERTY_LLCP_LTO = 0;
- private static final String PROPERTY_LLCP_LTO_VALUE = "llcp.lto";
- private static final int PROPERTY_LLCP_MIU = 1;
- private static final String PROPERTY_LLCP_MIU_VALUE = "llcp.miu";
- private static final int PROPERTY_LLCP_WKS = 2;
- private static final String PROPERTY_LLCP_WKS_VALUE = "llcp.wks";
- private static final int PROPERTY_LLCP_OPT = 3;
- private static final String PROPERTY_LLCP_OPT_VALUE = "llcp.opt";
- private static final int PROPERTY_NFC_DISCOVERY_A = 4;
- private static final String PROPERTY_NFC_DISCOVERY_A_VALUE = "discovery.iso14443A";
- private static final int PROPERTY_NFC_DISCOVERY_B = 5;
- private static final String PROPERTY_NFC_DISCOVERY_B_VALUE = "discovery.iso14443B";
- private static final int PROPERTY_NFC_DISCOVERY_F = 6;
- private static final String PROPERTY_NFC_DISCOVERY_F_VALUE = "discovery.felica";
- private static final int PROPERTY_NFC_DISCOVERY_15693 = 7;
- private static final String PROPERTY_NFC_DISCOVERY_15693_VALUE = "discovery.iso15693";
- private static final int PROPERTY_NFC_DISCOVERY_NFCIP = 8;
- private static final String PROPERTY_NFC_DISCOVERY_NFCIP_VALUE = "discovery.nfcip";
- static final int MSG_NDEF_TAG = 0;
- static final int MSG_CARD_EMULATION = 1;
- static final int MSG_LLCP_LINK_ACTIVATION = 2;
- static final int MSG_LLCP_LINK_DEACTIVATED = 3;
- static final int MSG_TARGET_DESELECTED = 4;
- static final int MSG_SHOW_MY_TAG_ICON = 5;
- static final int MSG_HIDE_MY_TAG_ICON = 6;
- static final int MSG_MOCK_NDEF = 7;
- static final int MSG_SE_FIELD_ACTIVATED = 8;
- static final int MSG_SE_FIELD_DEACTIVATED = 9;
- static final int MSG_SE_APDU_RECEIVED = 10;
- static final int MSG_SE_EMV_CARD_REMOVAL = 11;
- static final int MSG_SE_MIFARE_ACCESS = 12;
- static final int STATUS_CODE_TARGET_LOST = 146;
- // Copied from com.android.nfc_extras to avoid library dependency
- // Must keep in sync with com.android.nfc_extras
- static final int ROUTE_OFF = 1;
- static final int ROUTE_ON_WHEN_SCREEN_ON = 2;
- public static final String ACTION_RF_FIELD_ON_DETECTED =
- "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED";
- public static final String ACTION_RF_FIELD_OFF_DETECTED =
- "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED";
- public static final String ACTION_AID_SELECTED =
- "com.android.nfc_extras.action.AID_SELECTED";
- public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID";
- public static final String ACTION_APDU_RECEIVED =
- "com.android.nfc_extras.action.APDU_RECEIVED";
- public static final String EXTRA_APDU_BYTES =
- "com.android.nfc_extras.extra.APDU_BYTES";
- public static final String ACTION_EMV_CARD_REMOVAL =
- "com.android.nfc_extras.action.EMV_CARD_REMOVAL";
- public static final String ACTION_MIFARE_ACCESS_DETECTED =
- "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED";
- public static final String EXTRA_MIFARE_BLOCK =
- "com.android.nfc_extras.extra.MIFARE_BLOCK";
- // Locked on mNfcAdapter
- PendingIntent mDispatchOverrideIntent;
- IntentFilter[] mDispatchOverrideFilters;
- String[][] mDispatchOverrideTechLists;
- // TODO: none of these appear to be synchronized but are
- // read/written from different threads (notably Binder threads)...
- private int mGeneratedSocketHandle = 0;
- private volatile boolean mIsNfcEnabled = false;
- private boolean mIsDiscoveryOn = false;
- // NFC Execution Environment
- // fields below are protected by this
- private static final int SECURE_ELEMENT_ID = 11259375; //TODO: remove hard-coded value
- private NativeNfcSecureElement mSecureElement;
- private OpenSecureElement mOpenEe; // null when EE closed
- private int mEeRoutingState; // contactless interface routing
- // fields below are used in multiple threads and protected by synchronized(this)
- private final HashMap<Integer, Object> mObjectMap = new HashMap<Integer, Object>();
- private final HashMap<Integer, Object> mSocketMap = new HashMap<Integer, Object>();
- private boolean mScreenOn;
- private HashSet<String> mSePackages = new HashSet<String>();
- // fields below are final after onCreate()
- Context mContext;
- private NativeNfcManager mManager;
- private SharedPreferences mPrefs;
- private SharedPreferences.Editor mPrefsEditor;
- private PowerManager.WakeLock mWakeLock;
- private IActivityManager mIActivityManager;
- NdefPushClient mNdefPushClient;
- NdefPushServer mNdefPushServer;
- RegisteredComponentCache mTechListFilters;
- private static NfcService sService;
- public static void enforceAdminPerm(Context context) {
- int admin = context.checkCallingOrSelfPermission(ADMIN_PERM);
- int nfcee = context.checkCallingOrSelfPermission(NFCEE_ADMIN_PERM);
- if (admin != PackageManager.PERMISSION_GRANTED
- && nfcee != PackageManager.PERMISSION_GRANTED) {
- throw new SecurityException(ADMIN_PERM_ERROR);
- }
- }
- public static void enforceNfceeAdminPerm(Context context) {
- context.enforceCallingOrSelfPermission(NFCEE_ADMIN_PERM, NFCEE_ADMIN_PERM_ERROR);
- }
- public static NfcService getInstance() {
- return sService;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- Log.i(TAG, "Starting NFC service");
- sService = this;
- mContext = this;
- mManager = new NativeNfcManager(mContext, this);
- mManager.initializeNativeStructure();
- mNdefPushClient = new NdefPushClient(this);
- mNdefPushServer = new NdefPushServer();
- mTechListFilters = new RegisteredComponentCache(this,
- NfcAdapter.ACTION_TECH_DISCOVERED, NfcAdapter.ACTION_TECH_DISCOVERED);
- mSecureElement = new NativeNfcSecureElement();
- mEeRoutingState = ROUTE_OFF;
- mPrefs = mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE);
- mPrefsEditor = mPrefs.edit();
- mIsNfcEnabled = false; // real preference read later
- PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
- mScreenOn = pm.isScreenOn();
- mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NfcService");
- mIActivityManager = ActivityManagerNative.getDefault();
- ServiceManager.addService(SERVICE_NAME, mNfcAdapter);
- IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION);
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- filter.addAction(Intent.ACTION_SCREEN_ON);
- filter.addAction(ACTION_MASTER_CLEAR_NOTIFICATION);
- mContext.registerReceiver(mReceiver, filter);
- filter = new IntentFilter();
- filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
- filter.addDataScheme("package");
- mContext.registerReceiver(mReceiver, filter);
- Thread t = new Thread() {
- @Override
- public void run() {
- Log.d(TAG,"checking on firmware download");
- boolean nfc_on = mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT);
- if (nfc_on) {
- Log.d(TAG,"NFC is on. Doing normal stuff");
- _enable(false, true);
- } else {
- Log.d(TAG,"NFC is off. Checking firmware version");
- _maybeUpdateFirmware();
- }
- resetSeOnFirstBoot();
- }
- };
- t.start();
- }
- @Override
- public void onTerminate() {
- super.onTerminate();
- // NFC application is persistent, it should not be destroyed by framework
- Log.wtf(TAG, "NFC service is under attack!");
- }
- private final INfcAdapter.Stub mNfcAdapter = new INfcAdapter.Stub() {
- /** Protected by "this" */
- NdefMessage mLocalMessage = null;
- @Override
- public boolean enable() throws RemoteException {
- NfcService.enforceAdminPerm(mContext);
- boolean isSuccess = false;
- boolean previouslyEnabled = isEnabled();
- if (!previouslyEnabled) {
- reset();
- isSuccess = _enable(previouslyEnabled, true);
- }
- return isSuccess;
- }
- @Override
- public boolean disable() throws RemoteException {
- boolean isSuccess = false;
- NfcService.enforceAdminPerm(mContext);
- boolean previouslyEnabled = isEnabled();
- if (DBG) Log.d(TAG, "Disabling NFC. previous=" + previouslyEnabled);
- if (previouslyEnabled) {
- isSuccess = _disable(previouslyEnabled, true);
- }
- return isSuccess;
- }
- @Override
- public void enableForegroundDispatch(ComponentName activity, PendingIntent intent,
- IntentFilter[] filters, TechListParcel techListsParcel) {
- // Permission check
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- // Argument validation
- if (activity == null || intent == null) {
- throw new IllegalArgumentException();
- }
- // Validate the IntentFilters
- if (filters != null) {
- if (filters.length == 0) {
- filters = null;
- } else {
- for (IntentFilter filter : filters) {
- if (filter == null) {
- throw new IllegalArgumentException("null IntentFilter");
- }
- }
- }
- }
- // Validate the tech lists
- String[][] techLists = null;
- if (techListsParcel != null) {
- techLists = techListsParcel.getTechLists();
- }
- synchronized (this) {
- if (mDispatchOverrideIntent != null) {
- Log.e(TAG, "Replacing active dispatch overrides");
- }
- mDispatchOverrideIntent = intent;
- mDispatchOverrideFilters = filters;
- mDispatchOverrideTechLists = techLists;
- }
- }
- @Override
- public void disableForegroundDispatch(ComponentName activity) {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- synchronized (this) {
- if (mDispatchOverrideIntent == null) {
- Log.e(TAG, "No active foreground dispatching");
- }
- mDispatchOverrideIntent = null;
- mDispatchOverrideFilters = null;
- }
- }
- @Override
- public void enableForegroundNdefPush(ComponentName activity, NdefMessage msg) {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- if (activity == null || msg == null) {
- throw new IllegalArgumentException();
- }
- if (mNdefPushClient.setForegroundMessage(msg)) {
- Log.e(TAG, "Replacing active NDEF push message");
- }
- }
- @Override
- public void disableForegroundNdefPush(ComponentName activity) {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- if (!mNdefPushClient.setForegroundMessage(null)) {
- Log.e(TAG, "No active foreground NDEF push message");
- }
- }
- @Override
- public int createLlcpConnectionlessSocket(int sap) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* Check SAP is not already used */
- /* Store the socket handle */
- int sockeHandle = mGeneratedSocketHandle;
- NativeLlcpConnectionlessSocket socket;
- socket = mManager.doCreateLlcpConnectionlessSocket(sap);
- if (socket != null) {
- synchronized(NfcService.this) {
- /* update socket handle generation */
- mGeneratedSocketHandle++;
- /* Add the socket into the socket map */
- mSocketMap.put(mGeneratedSocketHandle, socket);
- return mGeneratedSocketHandle;
- }
- } else {
- /* Get Error Status */
- int errorStatus = mManager.doGetLastError();
- switch (errorStatus) {
- case ErrorCodes.ERROR_BUFFER_TO_SMALL:
- return ErrorCodes.ERROR_BUFFER_TO_SMALL;
- case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
- return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES;
- default:
- return ErrorCodes.ERROR_SOCKET_CREATION;
- }
- }
- }
- @Override
- public int createLlcpServiceSocket(int sap, String sn, int miu, int rw, int linearBufferLength)
- throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- NativeLlcpServiceSocket socket;
- socket = mManager.doCreateLlcpServiceSocket(sap, sn, miu, rw, linearBufferLength);
- if (socket != null) {
- synchronized(NfcService.this) {
- /* update socket handle generation */
- mGeneratedSocketHandle++;
- /* Add the socket into the socket map */
- mSocketMap.put(mGeneratedSocketHandle, socket);
- return mGeneratedSocketHandle;
- }
- } else {
- /* Get Error Status */
- int errorStatus = mManager.doGetLastError();
- switch (errorStatus) {
- case ErrorCodes.ERROR_BUFFER_TO_SMALL:
- return ErrorCodes.ERROR_BUFFER_TO_SMALL;
- case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
- return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES;
- default:
- return ErrorCodes.ERROR_SOCKET_CREATION;
- }
- }
- }
- @Override
- public int createLlcpSocket(int sap, int miu, int rw, int linearBufferLength)
- throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- if (DBG) Log.d(TAG, "creating llcp socket");
- NativeLlcpSocket socket;
- socket = mManager.doCreateLlcpSocket(sap, miu, rw, linearBufferLength);
- if (socket != null) {
- synchronized(NfcService.this) {
- /* update socket handle generation */
- mGeneratedSocketHandle++;
- /* Add the socket into the socket map */
- mSocketMap.put(mGeneratedSocketHandle, socket);
- return mGeneratedSocketHandle;
- }
- } else {
- /* Get Error Status */
- int errorStatus = mManager.doGetLastError();
- Log.d(TAG, "failed to create llcp socket: " + ErrorCodes.asString(errorStatus));
- switch (errorStatus) {
- case ErrorCodes.ERROR_BUFFER_TO_SMALL:
- return ErrorCodes.ERROR_BUFFER_TO_SMALL;
- case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
- return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES;
- default:
- return ErrorCodes.ERROR_SOCKET_CREATION;
- }
- }
- }
- @Override
- public ILlcpConnectionlessSocket getLlcpConnectionlessInterface() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- return mLlcpConnectionlessSocketService;
- }
- @Override
- public ILlcpSocket getLlcpInterface() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- return mLlcpSocket;
- }
- @Override
- public ILlcpServiceSocket getLlcpServiceInterface() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- return mLlcpServerSocketService;
- }
- @Override
- public INfcTag getNfcTagInterface() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- return mNfcTagService;
- }
- @Override
- public IP2pInitiator getP2pInitiatorInterface() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- return mP2pInitiatorService;
- }
- @Override
- public IP2pTarget getP2pTargetInterface() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- return mP2pTargetService;
- }
- @Override
- public INfcAdapterExtras getNfcAdapterExtrasInterface() {
- NfcService.enforceNfceeAdminPerm(mContext);
- return mExtrasService;
- }
- @Override
- public String getProperties(String param) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- if (param == null) {
- return null;
- }
- if (param.equals(PROPERTY_LLCP_LTO_VALUE)) {
- return Integer.toString(mPrefs.getInt(PREF_LLCP_LTO, LLCP_LTO_DEFAULT));
- } else if (param.equals(PROPERTY_LLCP_MIU_VALUE)) {
- return Integer.toString(mPrefs.getInt(PREF_LLCP_MIU, LLCP_MIU_DEFAULT));
- } else if (param.equals(PROPERTY_LLCP_WKS_VALUE)) {
- return Integer.toString(mPrefs.getInt(PREF_LLCP_WKS, LLCP_WKS_DEFAULT));
- } else if (param.equals(PROPERTY_LLCP_OPT_VALUE)) {
- return Integer.toString(mPrefs.getInt(PREF_LLCP_OPT, LLCP_OPT_DEFAULT));
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_A_VALUE)) {
- return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_A, DISCOVERY_A_DEFAULT));
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_B_VALUE)) {
- return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_B, DISCOVERY_B_DEFAULT));
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_F_VALUE)) {
- return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_F, DISCOVERY_F_DEFAULT));
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_NFCIP_VALUE)) {
- return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_NFCIP, DISCOVERY_NFCIP_DEFAULT));
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_15693_VALUE)) {
- return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_15693, DISCOVERY_15693_DEFAULT));
- } else {
- return "Unknown property";
- }
- }
- @Override
- public boolean isEnabled() throws RemoteException {
- return mIsNfcEnabled;
- }
- @Override
- public int setProperties(String param, String value) throws RemoteException {
- NfcService.enforceAdminPerm(mContext);
- if (isEnabled()) {
- return ErrorCodes.ERROR_NFC_ON;
- }
- int val;
- /* Check params validity */
- if (param == null || value == null) {
- return ErrorCodes.ERROR_INVALID_PARAM;
- }
- if (param.equals(PROPERTY_LLCP_LTO_VALUE)) {
- val = Integer.parseInt(value);
- /* Check params */
- if (val > LLCP_LTO_MAX)
- return ErrorCodes.ERROR_INVALID_PARAM;
- /* Store value */
- mPrefsEditor.putInt(PREF_LLCP_LTO, val);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_LLCP_LTO, val);
- } else if (param.equals(PROPERTY_LLCP_MIU_VALUE)) {
- val = Integer.parseInt(value);
- /* Check params */
- if ((val < LLCP_MIU_DEFAULT) || (val > LLCP_MIU_MAX))
- return ErrorCodes.ERROR_INVALID_PARAM;
- /* Store value */
- mPrefsEditor.putInt(PREF_LLCP_MIU, val);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_LLCP_MIU, val);
- } else if (param.equals(PROPERTY_LLCP_WKS_VALUE)) {
- val = Integer.parseInt(value);
- /* Check params */
- if (val > LLCP_WKS_MAX)
- return ErrorCodes.ERROR_INVALID_PARAM;
- /* Store value */
- mPrefsEditor.putInt(PREF_LLCP_WKS, val);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_LLCP_WKS, val);
- } else if (param.equals(PROPERTY_LLCP_OPT_VALUE)) {
- val = Integer.parseInt(value);
- /* Check params */
- if (val > LLCP_OPT_MAX)
- return ErrorCodes.ERROR_INVALID_PARAM;
- /* Store value */
- mPrefsEditor.putInt(PREF_LLCP_OPT, val);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_LLCP_OPT, val);
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_A_VALUE)) {
- boolean b = Boolean.parseBoolean(value);
- /* Store value */
- mPrefsEditor.putBoolean(PREF_DISCOVERY_A, b);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_A, b ? 1 : 0);
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_B_VALUE)) {
- boolean b = Boolean.parseBoolean(value);
- /* Store value */
- mPrefsEditor.putBoolean(PREF_DISCOVERY_B, b);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_B, b ? 1 : 0);
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_F_VALUE)) {
- boolean b = Boolean.parseBoolean(value);
- /* Store value */
- mPrefsEditor.putBoolean(PREF_DISCOVERY_F, b);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_F, b ? 1 : 0);
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_15693_VALUE)) {
- boolean b = Boolean.parseBoolean(value);
- /* Store value */
- mPrefsEditor.putBoolean(PREF_DISCOVERY_15693, b);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_15693, b ? 1 : 0);
- } else if (param.equals(PROPERTY_NFC_DISCOVERY_NFCIP_VALUE)) {
- boolean b = Boolean.parseBoolean(value);
- /* Store value */
- mPrefsEditor.putBoolean(PREF_DISCOVERY_NFCIP, b);
- mPrefsEditor.apply();
- /* Update JNI */
- mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_NFCIP, b ? 1 : 0);
- } else {
- return ErrorCodes.ERROR_INVALID_PARAM;
- }
- return ErrorCodes.SUCCESS;
- }
- @Override
- public NdefMessage localGet() throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- synchronized (this) {
- return mLocalMessage;
- }
- }
- @Override
- public void localSet(NdefMessage message) throws RemoteException {
- NfcService.enforceAdminPerm(mContext);
- synchronized (this) {
- mLocalMessage = message;
- Context context = NfcService.this.getApplicationContext();
- // Send a message to the UI thread to show or hide the icon so the requests are
- // serialized and the icon can't get out of sync with reality.
- if (message != null) {
- FileOutputStream out = null;
- try {
- out = context.openFileOutput(MY_TAG_FILE_NAME, Context.MODE_PRIVATE);
- byte[] bytes = message.toByteArray();
- if (bytes.length == 0) {
- Log.w(TAG, "Setting a empty mytag");
- }
- out.write(bytes);
- } catch (IOException e) {
- Log.e(TAG, "Could not write mytag file", e);
- } finally {
- try {
- if (out != null) {
- out.flush();
- out.close();
- }
- } catch (IOException e) {
- // Ignore
- }
- }
- // Only show the icon if NFC is enabled.
- if (mIsNfcEnabled) {
- sendMessage(MSG_SHOW_MY_TAG_ICON, null);
- }
- } else {
- context.deleteFile(MY_TAG_FILE_NAME);
- sendMessage(MSG_HIDE_MY_TAG_ICON, null);
- }
- }
- }
- };
- private final ILlcpSocket mLlcpSocket = new ILlcpSocket.Stub() {
- private NativeLlcpSocket findSocket(int nativeHandle) {
- Object socket = NfcService.this.findSocket(nativeHandle);
- if (!(socket instanceof NativeLlcpSocket)) {
- return null;
- }
- return (NativeLlcpSocket) socket;
- }
- @Override
- public int close(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- socket.doClose();
- /* Remove the socket closed from the hmap */
- RemoveSocket(nativeHandle);
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_IO;
- }
- }
- @Override
- public int connect(int nativeHandle, int sap) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- boolean isSuccess = false;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- isSuccess = socket.doConnect(sap);
- if (isSuccess) {
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_IO;
- }
- } else {
- return ErrorCodes.ERROR_IO;
- }
- }
- @Override
- public int connectByName(int nativeHandle, String sn) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- boolean isSuccess = false;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- isSuccess = socket.doConnectBy(sn);
- if (isSuccess) {
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_IO;
- }
- } else {
- return ErrorCodes.ERROR_IO;
- }
- }
- @Override
- public int getLocalSap(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- return socket.getSap();
- } else {
- return 0;
- }
- }
- @Override
- public int getLocalSocketMiu(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- return socket.getMiu();
- } else {
- return 0;
- }
- }
- @Override
- public int getLocalSocketRw(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- return socket.getRw();
- } else {
- return 0;
- }
- }
- @Override
- public int getRemoteSocketMiu(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- if (socket.doGetRemoteSocketMiu() != 0) {
- return socket.doGetRemoteSocketMiu();
- } else {
- return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
- }
- } else {
- return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
- }
- }
- @Override
- public int getRemoteSocketRw(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- if (socket.doGetRemoteSocketRw() != 0) {
- return socket.doGetRemoteSocketRw();
- } else {
- return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
- }
- } else {
- return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
- }
- }
- @Override
- public int receive(int nativeHandle, byte[] receiveBuffer) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- return socket.doReceive(receiveBuffer);
- } else {
- return 0;
- }
- }
- @Override
- public int send(int nativeHandle, byte[] data) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpSocket socket = null;
- boolean isSuccess = false;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- isSuccess = socket.doSend(data);
- if (isSuccess) {
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_IO;
- }
- } else {
- return ErrorCodes.ERROR_IO;
- }
- }
- };
- private final ILlcpServiceSocket mLlcpServerSocketService = new ILlcpServiceSocket.Stub() {
- private NativeLlcpServiceSocket findSocket(int nativeHandle) {
- Object socket = NfcService.this.findSocket(nativeHandle);
- if (!(socket instanceof NativeLlcpServiceSocket)) {
- return null;
- }
- return (NativeLlcpServiceSocket) socket;
- }
- @Override
- public int accept(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpServiceSocket socket = null;
- NativeLlcpSocket clientSocket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- clientSocket = socket.doAccept(socket.getMiu(),
- socket.getRw(), socket.getLinearBufferLength());
- if (clientSocket != null) {
- /* Add the socket into the socket map */
- synchronized(this) {
- mGeneratedSocketHandle++;
- mSocketMap.put(mGeneratedSocketHandle, clientSocket);
- return mGeneratedSocketHandle;
- }
- } else {
- return ErrorCodes.ERROR_IO;
- }
- } else {
- return ErrorCodes.ERROR_IO;
- }
- }
- @Override
- public void close(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpServiceSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- socket.doClose();
- synchronized (this) {
- /* Remove the socket closed from the hmap */
- RemoveSocket(nativeHandle);
- }
- }
- }
- };
- private final ILlcpConnectionlessSocket mLlcpConnectionlessSocketService = new ILlcpConnectionlessSocket.Stub() {
- private NativeLlcpConnectionlessSocket findSocket(int nativeHandle) {
- Object socket = NfcService.this.findSocket(nativeHandle);
- if (!(socket instanceof NativeLlcpConnectionlessSocket)) {
- return null;
- }
- return (NativeLlcpConnectionlessSocket) socket;
- }
- @Override
- public void close(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpConnectionlessSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- socket.doClose();
- /* Remove the socket closed from the hmap */
- RemoveSocket(nativeHandle);
- }
- }
- @Override
- public int getSap(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpConnectionlessSocket socket = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- return socket.getSap();
- } else {
- return 0;
- }
- }
- @Override
- public LlcpPacket receiveFrom(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpConnectionlessSocket socket = null;
- LlcpPacket packet;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return null;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- packet = socket.doReceiveFrom(socket.getLinkMiu());
- if (packet != null) {
- return packet;
- }
- return null;
- } else {
- return null;
- }
- }
- @Override
- public int sendTo(int nativeHandle, LlcpPacket packet) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeLlcpConnectionlessSocket socket = null;
- boolean isSuccess = false;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the socket in the hmap */
- socket = findSocket(nativeHandle);
- if (socket != null) {
- isSuccess = socket.doSendTo(packet.getRemoteSap(), packet.getDataBuffer());
- if (isSuccess) {
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_IO;
- }
- } else {
- return ErrorCodes.ERROR_IO;
- }
- }
- };
- private final INfcTag mNfcTagService = new INfcTag.Stub() {
- @Override
- public int close(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeNfcTag tag = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the tag in the hmap */
- tag = (NativeNfcTag) findObject(nativeHandle);
- if (tag != null) {
- /* Remove the device from the hmap */
- unregisterObject(nativeHandle);
- tag.disconnect();
- return ErrorCodes.SUCCESS;
- }
- /* Restart polling loop for notification */
- applyRouting();
- return ErrorCodes.ERROR_DISCONNECT;
- }
- @Override
- public int connect(int nativeHandle, int technology) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeNfcTag tag = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the tag in the hmap */
- tag = (NativeNfcTag) findObject(nativeHandle);
- if (tag == null) {
- return ErrorCodes.ERROR_DISCONNECT;
- }
- // Note that on most tags, all technologies are behind a single
- // handle. This means that the connect at the lower levels
- // will do nothing, as the tag is already connected to that handle.
- if (tag.connect(technology) == 0) {
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_DISCONNECT;
- }
- }
- @Override
- public int reconnect(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- NativeNfcTag tag = null;
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return ErrorCodes.ERROR_NOT_INITIALIZED;
- }
- /* find the tag in the hmap */
- tag = (NativeNfcTag) findObject(nativeHandle);
- if (tag != null) {
- if (tag.reconnect() == 0) {
- return ErrorCodes.SUCCESS;
- } else {
- return ErrorCodes.ERROR_DISCONNECT;
- }
- }
- return ErrorCodes.ERROR_DISCONNECT;
- }
- @Override
- public int[] getTechList(int nativeHandle) throws RemoteException {
- mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
- // Check if NFC is enabled
- if (!mIsNfcEnabled) {
- return null;
- }
- /* find the tag in the hmap */
- NativeNfcTag tag = (NativeNfcTag) findObject(nativeHandle);
- if (tag != null) {
- return tag.getTechList();
- …
Large files files are truncated, but you can click here to view the full file