NfcService.java revision e0b5fcb7e6422d1a788c48dd8f2936832ab8b397
1f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly/* 2f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * Copyright (C) 2010 The Android Open Source Project 3f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * 4f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License"); 5f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * you may not use this file except in compliance with the License. 6f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * You may obtain a copy of the License at 7f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * 8f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * http://www.apache.org/licenses/LICENSE-2.0 9f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * 10f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * Unless required by applicable law or agreed to in writing, software 11f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS, 12f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * See the License for the specific language governing permissions and 14f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * limitations under the License. 15f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly */ 16f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 1713d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellypackage com.android.nfc; 18f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 19f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamiltonimport com.android.nfc.DeviceHost.DeviceHostListener; 204a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamiltonimport com.android.nfc.DeviceHost.LlcpServerSocket; 214a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamiltonimport com.android.nfc.DeviceHost.LlcpSocket; 22f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamiltonimport com.android.nfc.DeviceHost.NfcDepEndpoint; 23f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamiltonimport com.android.nfc.DeviceHost.TagEndpoint; 2481c476dd93f059d4082c15369894d5d16fbea05dJeff Hamiltonimport com.android.nfc.nxp.NativeNfcManager; 2581c476dd93f059d4082c15369894d5d16fbea05dJeff Hamiltonimport com.android.nfc.nxp.NativeNfcSecureElement; 268afd14d3b23d3124c48ee275ba2845aede6542a1Jeff Hamiltonimport com.android.nfc3.R; 27d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 282f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pellyimport android.app.Application; 29275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parksimport android.app.KeyguardManager; 3005973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamiltonimport android.app.PendingIntent; 3113d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.BroadcastReceiver; 3205973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamiltonimport android.content.ComponentName; 3331949217328bf2357ff044f0d18677fe588c790cNick Pellyimport android.content.ContentResolver; 3413d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.Context; 3513d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.Intent; 3613d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.IntentFilter; 370e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pellyimport android.content.SharedPreferences; 3893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamiltonimport android.content.pm.PackageManager; 39d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamiltonimport android.media.AudioManager; 40d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamiltonimport android.media.SoundPool; 413fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamiltonimport android.net.Uri; 42f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.ErrorCodes; 43f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.FormatException; 442094515fca0cfa0ac87e9cc260d3953d416afe3eJason parksimport android.nfc.INdefPushCallback; 450e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pellyimport android.nfc.INfcAdapter; 4649d53329a0c720a7e430220d77805bc1763545b1Nick Pellyimport android.nfc.INfcAdapterExtras; 47f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.INfcTag; 48f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.NdefMessage; 49f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.NfcAdapter; 500e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pellyimport android.nfc.Tag; 5124dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamiltonimport android.nfc.TechListParcel; 529d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenenimport android.nfc.TransceiveResult; 53aca0d055a82da850c27f6872405602ad5f3fee7bJeff Hamiltonimport android.nfc.tech.Ndef; 5481c476dd93f059d4082c15369894d5d16fbea05dJeff Hamiltonimport android.nfc.tech.TagTechnology; 557c034a7fe7d36b1ab039af2c44717812ea02657eNick Pellyimport android.os.AsyncTask; 5650effe4645b6ea57a1dc90777995f41dd9624e55Kenny Rootimport android.os.Binder; 57b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamiltonimport android.os.Bundle; 58b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneauimport android.os.Handler; 5949d53329a0c720a7e430220d77805bc1763545b1Nick Pellyimport android.os.IBinder; 60b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneauimport android.os.Message; 61533043d1003de2f6a20a29201100d94c3c7bc9caNick Pellyimport android.os.PowerManager; 624467dca5650a170af5020c10a8ccb25f86f1007fNick Pellyimport android.os.Process; 63f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.os.RemoteException; 6413d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.os.ServiceManager; 65d11251b2d1fed7b7325c4fcb0616b2d1c654320fMartijn Coenenimport android.provider.Settings; 66f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.util.Log; 67f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 6831949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.io.FileDescriptor; 6957d376f1ee1a3939977b95759525585abb9601fbJeff Hamiltonimport java.io.IOException; 7031949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.io.PrintWriter; 7131949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.util.Arrays; 723ca6ffff72f4599e80f85de5ae8e7f55012f38d6Jeff Hamiltonimport java.util.HashMap; 7384e1e0adc2516afd35ebab029a52e764e0490559Jason parksimport java.util.HashSet; 7431949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.util.concurrent.ExecutionException; 753ca6ffff72f4599e80f85de5ae8e7f55012f38d6Jeff Hamilton 7677d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pellypublic class NfcService extends Application implements DeviceHostListener { 77bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks private static final String ACTION_MASTER_CLEAR_NOTIFICATION = "android.intent.action.MASTER_CLEAR_NOTIFICATION"; 78bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks 798d6a8dce6706c6c6b3158101f2f3e94a1e0ad946Ben Dodson static final boolean DBG = true; 8076a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly static final String TAG = "NfcService"; 81fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks 82d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton public static final String SERVICE_NAME = "nfc"; 83fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks 84bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly private static final String NFC_PERM = android.Manifest.permission.NFC; 85bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly private static final String NFC_PERM_ERROR = "NFC permission required"; 86bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly private static final String ADMIN_PERM = android.Manifest.permission.WRITE_SECURE_SETTINGS; 87bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly private static final String ADMIN_PERM_ERROR = "WRITE_SECURE_SETTINGS permission required"; 88a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly private static final String NFCEE_ADMIN_PERM = "com.android.nfc.permission.NFCEE_ADMIN"; 8993d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton private static final String NFCEE_ADMIN_PERM_ERROR = "NFCEE_ADMIN permission required"; 90bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 9177d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly public static final String PREF = "NfcServicePrefs"; 92f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 930e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly private static final String PREF_NFC_ON = "nfc_on"; 940e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly private static final boolean NFC_ON_DEFAULT = true; 950b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly private static final String PREF_NDEF_PUSH_ON = "ndef_push_on"; 960b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly private static final boolean NDEF_PUSH_ON_DEFAULT = true; 97f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 98a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly private static final String PREF_FIRST_BOOT = "first_boot"; 99a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly 100b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau static final int MSG_NDEF_TAG = 0; 101b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau static final int MSG_CARD_EMULATION = 1; 102b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau static final int MSG_LLCP_LINK_ACTIVATION = 2; 103b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau static final int MSG_LLCP_LINK_DEACTIVATED = 3; 104b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau static final int MSG_TARGET_DESELECTED = 4; 105b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton static final int MSG_MOCK_NDEF = 7; 106c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas static final int MSG_SE_FIELD_ACTIVATED = 8; 107c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas static final int MSG_SE_FIELD_DEACTIVATED = 9; 1082c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas static final int MSG_SE_APDU_RECEIVED = 10; 1092c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas static final int MSG_SE_EMV_CARD_REMOVAL = 11; 1102c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas static final int MSG_SE_MIFARE_ACCESS = 12; 111b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 11231949217328bf2357ff044f0d18677fe588c790cNick Pelly static final int TASK_ENABLE = 1; 11331949217328bf2357ff044f0d18677fe588c790cNick Pelly static final int TASK_DISABLE = 2; 11431949217328bf2357ff044f0d18677fe588c790cNick Pelly static final int TASK_BOOT = 3; 11531949217328bf2357ff044f0d18677fe588c790cNick Pelly static final int TASK_EE_WIPE = 4; 11631949217328bf2357ff044f0d18677fe588c790cNick Pelly 11749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly // Copied from com.android.nfc_extras to avoid library dependency 11849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly // Must keep in sync with com.android.nfc_extras 11949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly static final int ROUTE_OFF = 1; 12049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly static final int ROUTE_ON_WHEN_SCREEN_ON = 2; 1217efbf69a37134ccbd86a1f6b4121f16b4a80eaaeNick Pelly 12249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public static final String ACTION_RF_FIELD_ON_DETECTED = 12349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED"; 12449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public static final String ACTION_RF_FIELD_OFF_DETECTED = 12549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; 12649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public static final String ACTION_AID_SELECTED = 12749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly "com.android.nfc_extras.action.AID_SELECTED"; 12849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID"; 12949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly 1302c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas public static final String ACTION_APDU_RECEIVED = 1312c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas "com.android.nfc_extras.action.APDU_RECEIVED"; 1322c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas public static final String EXTRA_APDU_BYTES = 1332c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas "com.android.nfc_extras.extra.APDU_BYTES"; 1342c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas 1352c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas public static final String ACTION_EMV_CARD_REMOVAL = 1362c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas "com.android.nfc_extras.action.EMV_CARD_REMOVAL"; 1372c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas 1382c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas public static final String ACTION_MIFARE_ACCESS_DETECTED = 1392c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED"; 1402c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas public static final String EXTRA_MIFARE_BLOCK = 1412c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas "com.android.nfc_extras.extra.MIFARE_BLOCK"; 1422c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas 14331949217328bf2357ff044f0d18677fe588c790cNick Pelly //TODO: dont hardcode this 14431949217328bf2357ff044f0d18677fe588c790cNick Pelly private static final byte[][] EE_WIPE_APDUS = { 14531949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x00}, 14631949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x07, (byte)0xa0, (byte)0x00, 14731949217328bf2357ff044f0d18677fe588c790cNick Pelly (byte)0x00, (byte)0x04, (byte)0x76, (byte)0x20, (byte)0x10, (byte)0x00}, 14831949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x80, (byte)0xe2, (byte)0x01, (byte)0x03, (byte)0x00}, 14931949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x00}, 15031949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x07, (byte)0xa0, (byte)0x00, 15131949217328bf2357ff044f0d18677fe588c790cNick Pelly (byte)0x00, (byte)0x04, (byte)0x76, (byte)0x30, (byte)0x30, (byte)0x00}, 15231949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x80, (byte)0xb4, (byte)0x00, (byte)0x00, (byte)0x00}, 15331949217328bf2357ff044f0d18677fe588c790cNick Pelly {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x00}, 15431949217328bf2357ff044f0d18677fe588c790cNick Pelly }; 15549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly 15649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly // NFC Execution Environment 15749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly // fields below are protected by this 1580bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas private NativeNfcSecureElement mSecureElement; 15949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private OpenSecureElement mOpenEe; // null when EE closed 16049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private int mEeRoutingState; // contactless interface routing 1610bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 162d2d5dddf17ac2008547172cd72faa034a89d569bJeff Hamilton // fields below must be used only on the UI thread and therefore aren't synchronized 163d2d5dddf17ac2008547172cd72faa034a89d569bJeff Hamilton boolean mP2pStarted = false; 164d2d5dddf17ac2008547172cd72faa034a89d569bJeff Hamilton 1652f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly // fields below are used in multiple threads and protected by synchronized(this) 1662f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly private final HashMap<Integer, Object> mObjectMap = new HashMap<Integer, Object>(); 16784e1e0adc2516afd35ebab029a52e764e0490559Jason parks private HashSet<String> mSePackages = new HashSet<String>(); 16831949217328bf2357ff044f0d18677fe588c790cNick Pelly private boolean mIsScreenUnlocked; 1690b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly private boolean mIsNdefPushEnabled; 17031949217328bf2357ff044f0d18677fe588c790cNick Pelly 17131949217328bf2357ff044f0d18677fe588c790cNick Pelly // mState is protected by this, however it is only modified in onCreate() 17231949217328bf2357ff044f0d18677fe588c790cNick Pelly // and the default AsyncTask thread so it is read unprotected from that 17331949217328bf2357ff044f0d18677fe588c790cNick Pelly // thread 17431949217328bf2357ff044f0d18677fe588c790cNick Pelly int mState; // one of NfcAdapter.STATE_ON, STATE_TURNING_ON, etc 1752f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly 1762f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly // fields below are final after onCreate() 17705973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton Context mContext; 1784a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton private DeviceHost mDeviceHost; 1790e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly private SharedPreferences mPrefs; 1800e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly private SharedPreferences.Editor mPrefsEditor; 181533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly private PowerManager.WakeLock mWakeLock; 182d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton int mStartSound; 183d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton int mEndSound; 184d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton int mErrorSound; 185d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton SoundPool mSoundPool; // playback synchronized on this 18677d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly P2pLinkManager mP2pLinkManager; 1874a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton TagService mNfcTagService; 1884a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton NfcAdapterService mNfcAdapter; 1894a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton NfcAdapterExtrasService mExtrasService; 19031949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean mIsAirplaneSensitive; 19131949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean mIsAirplaneToggleable; 1922ef360deaff9f17aa72d5749ceee283cc80897afBen Dodson 19376a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly private NfcDispatcher mNfcDispatcher; 194275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks private KeyguardManager mKeyguard; 195d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 196d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton private static NfcService sService; 197d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 19893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton public static void enforceAdminPerm(Context context) { 19993d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton int admin = context.checkCallingOrSelfPermission(ADMIN_PERM); 20093d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton int nfcee = context.checkCallingOrSelfPermission(NFCEE_ADMIN_PERM); 20193d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton if (admin != PackageManager.PERMISSION_GRANTED 20293d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton && nfcee != PackageManager.PERMISSION_GRANTED) { 20393d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton throw new SecurityException(ADMIN_PERM_ERROR); 20493d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton } 20593d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton } 20693d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton 20793d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton public static void enforceNfceeAdminPerm(Context context) { 20893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton context.enforceCallingOrSelfPermission(NFCEE_ADMIN_PERM, NFCEE_ADMIN_PERM_ERROR); 20993d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton } 21093d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton 211d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton public static NfcService getInstance() { 212d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton return sService; 213d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton } 214f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 2150e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly @Override 216f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onRemoteEndpointDiscovered(TagEndpoint tag) { 217f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_NDEF_TAG, tag); 218f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 219f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 220f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton /** 221f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton * Notifies transaction 222f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton */ 223d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton @Override 224f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onCardEmulationDeselected() { 225f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_TARGET_DESELECTED, null); 226f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 227f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 228f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton /** 229f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton * Notifies transaction 230f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton */ 231d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton @Override 232f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onCardEmulationAidSelected(byte[] aid) { 233f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_CARD_EMULATION, aid); 234f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 235f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 236f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton /** 237f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton * Notifies P2P Device detected, to activate LLCP link 238f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton */ 239f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton @Override 240f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onLlcpLinkActivated(NfcDepEndpoint device) { 241f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_LLCP_LINK_ACTIVATION, device); 242f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 243f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 244f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton /** 245f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton * Notifies P2P Device detected, to activate LLCP link 246f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton */ 247d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton @Override 248f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onLlcpLinkDeactivated(NfcDepEndpoint device) { 249f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_LLCP_LINK_DEACTIVATED, device); 250f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 251f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 252d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton @Override 253f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onRemoteFieldActivated() { 254f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_SE_FIELD_ACTIVATED, null); 255f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 256f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 257d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton @Override 258f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton public void onRemoteFieldDeactivated() { 259f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton sendMessage(NfcService.MSG_SE_FIELD_DEACTIVATED, null); 260f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 261f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton 262f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton @Override 263442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly public void onSeApduReceived(byte[] apdu) { 264442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly sendMessage(NfcService.MSG_SE_APDU_RECEIVED, apdu); 265442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly } 266442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly 267442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly @Override 268442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly public void onSeEmvCardRemoval() { 269442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly sendMessage(NfcService.MSG_SE_EMV_CARD_REMOVAL, null); 270442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly } 271442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly 272442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly @Override 273442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly public void onSeMifareAccess(byte[] block) { 274442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly sendMessage(NfcService.MSG_SE_MIFARE_ACCESS, block); 275442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly } 276442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly 277442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly @Override 2780e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly public void onCreate() { 2792f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly super.onCreate(); 2802f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly 2814a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton mNfcTagService = new TagService(); 2824a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton mNfcAdapter = new NfcAdapterService(); 283ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly mExtrasService = new NfcAdapterExtrasService(); 2844a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton 2852f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly Log.i(TAG, "Starting NFC service"); 2862f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly 287d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton sService = this; 288d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 289d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); 290d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton mStartSound = mSoundPool.load(this, R.raw.start, 1); 291d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton mEndSound = mSoundPool.load(this, R.raw.end, 1); 292d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton mErrorSound = mSoundPool.load(this, R.raw.error, 1); 293d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton 2940e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly mContext = this; 295f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton mDeviceHost = new NativeNfcManager(this, this); 29674180bda362a8bc9d2f701d2c17bec0f63c20bbfBrad Fitzpatrick 29777d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager = new P2pLinkManager(mContext); 29877d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mNfcDispatcher = new NfcDispatcher(this, mP2pLinkManager); 29924dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton 3000bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas mSecureElement = new NativeNfcSecureElement(); 301eab09ad7204fe1f0feaca33efccf75c1bb388708Robert Tsai mEeRoutingState = ROUTE_OFF; 3020bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 303275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks mPrefs = getSharedPreferences(PREF, Context.MODE_PRIVATE); 3040e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly mPrefsEditor = mPrefs.edit(); 305f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 30631949217328bf2357ff044f0d18677fe588c790cNick Pelly mState = NfcAdapter.STATE_OFF; 3070b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mIsNdefPushEnabled = mPrefs.getBoolean(PREF_NDEF_PUSH_ON, NDEF_PUSH_ON_DEFAULT); 308f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 309275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 310275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks 311533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NfcService"); 312275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks mKeyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 31331949217328bf2357ff044f0d18677fe588c790cNick Pelly mIsScreenUnlocked = pm.isScreenOn() && !mKeyguard.isKeyguardLocked(); 314533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly 315d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton ServiceManager.addService(SERVICE_NAME, mNfcAdapter); 316f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 317eead88c5e2bdd34eb33fdf2c76717f9edb9e0396Jeff Hamilton IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION); 31865945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly filter.addAction(Intent.ACTION_SCREEN_OFF); 31965945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly filter.addAction(Intent.ACTION_SCREEN_ON); 320bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks filter.addAction(ACTION_MASTER_CLEAR_NOTIFICATION); 321275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks filter.addAction(Intent.ACTION_USER_PRESENT); 32231949217328bf2357ff044f0d18677fe588c790cNick Pelly registerForAirplaneMode(filter); 323275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks registerReceiver(mReceiver, filter); 324bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks 325bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks filter = new IntentFilter(); 326bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 327bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks filter.addDataScheme("package"); 328bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks 329275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks registerReceiver(mReceiver, filter); 3300e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 33131949217328bf2357ff044f0d18677fe588c790cNick Pelly new EnableDisableTask().execute(TASK_BOOT); // do blocking boot tasks 33231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 33331949217328bf2357ff044f0d18677fe588c790cNick Pelly 33431949217328bf2357ff044f0d18677fe588c790cNick Pelly void registerForAirplaneMode(IntentFilter filter) { 33531949217328bf2357ff044f0d18677fe588c790cNick Pelly final ContentResolver resolver = mContext.getContentResolver(); 33631949217328bf2357ff044f0d18677fe588c790cNick Pelly final String airplaneModeRadios = Settings.System.getString(resolver, 33731949217328bf2357ff044f0d18677fe588c790cNick Pelly Settings.System.AIRPLANE_MODE_RADIOS); 33831949217328bf2357ff044f0d18677fe588c790cNick Pelly final String toggleableRadios = Settings.System.getString(resolver, 33931949217328bf2357ff044f0d18677fe588c790cNick Pelly Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); 34031949217328bf2357ff044f0d18677fe588c790cNick Pelly 34131949217328bf2357ff044f0d18677fe588c790cNick Pelly mIsAirplaneSensitive = airplaneModeRadios == null ? true : 34231949217328bf2357ff044f0d18677fe588c790cNick Pelly airplaneModeRadios.contains(Settings.System.RADIO_NFC); 34331949217328bf2357ff044f0d18677fe588c790cNick Pelly mIsAirplaneToggleable = toggleableRadios == null ? false : 34431949217328bf2357ff044f0d18677fe588c790cNick Pelly toggleableRadios.contains(Settings.System.RADIO_NFC); 34531949217328bf2357ff044f0d18677fe588c790cNick Pelly 34631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mIsAirplaneSensitive) { 34731949217328bf2357ff044f0d18677fe588c790cNick Pelly filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); 34831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 34931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 35031949217328bf2357ff044f0d18677fe588c790cNick Pelly 35131949217328bf2357ff044f0d18677fe588c790cNick Pelly /** 35231949217328bf2357ff044f0d18677fe588c790cNick Pelly * Manages tasks that involve turning on/off the NFC controller. 35331949217328bf2357ff044f0d18677fe588c790cNick Pelly * 35431949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>All work that might turn the NFC adapter on or off must be done 35531949217328bf2357ff044f0d18677fe588c790cNick Pelly * through this task, to keep the handling of mState simple. 35631949217328bf2357ff044f0d18677fe588c790cNick Pelly * In other words, mState is only modified in these tasks (and we 35731949217328bf2357ff044f0d18677fe588c790cNick Pelly * don't need a lock to read it in these tasks). 35831949217328bf2357ff044f0d18677fe588c790cNick Pelly * 35931949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>These tasks are all done on the same AsyncTask background 36031949217328bf2357ff044f0d18677fe588c790cNick Pelly * thread, so they are serialized. Each task may temporarily transition 36131949217328bf2357ff044f0d18677fe588c790cNick Pelly * mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in 36231949217328bf2357ff044f0d18677fe588c790cNick Pelly * either STATE_ON or STATE_OFF. This way each task can be guaranteed 36331949217328bf2357ff044f0d18677fe588c790cNick Pelly * of starting in either STATE_OFF or STATE_ON, without needing to hold 36431949217328bf2357ff044f0d18677fe588c790cNick Pelly * NfcService.this for the entire task. 36531949217328bf2357ff044f0d18677fe588c790cNick Pelly * 36631949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>AsyncTask's are also implicitly queued. This is useful for corner 36731949217328bf2357ff044f0d18677fe588c790cNick Pelly * cases like turning airplane mode on while TASK_ENABLE is in progress. 36831949217328bf2357ff044f0d18677fe588c790cNick Pelly * The TASK_DISABLE triggered by airplane mode will be correctly executed 36931949217328bf2357ff044f0d18677fe588c790cNick Pelly * immediately after TASK_ENABLE is complete. This seems like the most sane 37031949217328bf2357ff044f0d18677fe588c790cNick Pelly * way to deal with these situations. 37131949217328bf2357ff044f0d18677fe588c790cNick Pelly * 37231949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>{@link #TASK_ENABLE} enables the NFC adapter, without changing 37331949217328bf2357ff044f0d18677fe588c790cNick Pelly * preferences 37431949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>{@link #TASK_DISABLE} disables the NFC adapter, without changing 37531949217328bf2357ff044f0d18677fe588c790cNick Pelly * preferences 37631949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>{@link #TASK_BOOT} does first boot work and may enable NFC 37731949217328bf2357ff044f0d18677fe588c790cNick Pelly * <p>{@link #TASK_EE_WIPE} wipes the Execution Environment, and in the 37831949217328bf2357ff044f0d18677fe588c790cNick Pelly * process may temporarily enable the NFC adapter 37931949217328bf2357ff044f0d18677fe588c790cNick Pelly */ 38031949217328bf2357ff044f0d18677fe588c790cNick Pelly class EnableDisableTask extends AsyncTask<Integer, Void, Void> { 38131949217328bf2357ff044f0d18677fe588c790cNick Pelly @Override 38231949217328bf2357ff044f0d18677fe588c790cNick Pelly protected Void doInBackground(Integer... params) { 38331949217328bf2357ff044f0d18677fe588c790cNick Pelly // Sanity check mState 38431949217328bf2357ff044f0d18677fe588c790cNick Pelly switch (mState) { 38531949217328bf2357ff044f0d18677fe588c790cNick Pelly case NfcAdapter.STATE_TURNING_OFF: 38631949217328bf2357ff044f0d18677fe588c790cNick Pelly case NfcAdapter.STATE_TURNING_ON: 38731949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " + 38831949217328bf2357ff044f0d18677fe588c790cNick Pelly mState); 38931949217328bf2357ff044f0d18677fe588c790cNick Pelly return null; 39031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 39131949217328bf2357ff044f0d18677fe588c790cNick Pelly 3924467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly /* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND, 3934467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly * override with the default. THREAD_PRIORITY_BACKGROUND causes 3944467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly * us to service software I2C too slow for firmware download 3954467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly * with the NXP PN544. 3964467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly * TODO: move this to the DAL I2C layer in libnfc-nxp, since this 3974467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly * problem only occurs on I2C platforms using PN544 3984467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly */ 3994467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); 4004467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly 40131949217328bf2357ff044f0d18677fe588c790cNick Pelly switch (params[0].intValue()) { 40231949217328bf2357ff044f0d18677fe588c790cNick Pelly case TASK_ENABLE: 40331949217328bf2357ff044f0d18677fe588c790cNick Pelly enableInternal(); 40431949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 40531949217328bf2357ff044f0d18677fe588c790cNick Pelly case TASK_DISABLE: 40631949217328bf2357ff044f0d18677fe588c790cNick Pelly disableInternal(); 40731949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 40831949217328bf2357ff044f0d18677fe588c790cNick Pelly case TASK_BOOT: 4090fe7049a3224aa7b29cc980be07387e17607b0deJeff Hamilton Log.d(TAG,"checking on firmware download"); 41031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT) && 41131949217328bf2357ff044f0d18677fe588c790cNick Pelly !(mIsAirplaneSensitive && isAirplaneModeOn())) { 4120fe7049a3224aa7b29cc980be07387e17607b0deJeff Hamilton Log.d(TAG,"NFC is on. Doing normal stuff"); 41331949217328bf2357ff044f0d18677fe588c790cNick Pelly enableInternal(); 4140fe7049a3224aa7b29cc980be07387e17607b0deJeff Hamilton } else { 4150fe7049a3224aa7b29cc980be07387e17607b0deJeff Hamilton Log.d(TAG,"NFC is off. Checking firmware version"); 4160fe7049a3224aa7b29cc980be07387e17607b0deJeff Hamilton mDeviceHost.checkFirmware(); 41731949217328bf2357ff044f0d18677fe588c790cNick Pelly } 41831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) { 41931949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.i(TAG, "First Boot"); 42031949217328bf2357ff044f0d18677fe588c790cNick Pelly mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false); 42131949217328bf2357ff044f0d18677fe588c790cNick Pelly mPrefsEditor.apply(); 42231949217328bf2357ff044f0d18677fe588c790cNick Pelly executeEeWipe(); 42331949217328bf2357ff044f0d18677fe588c790cNick Pelly } 42431949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 42531949217328bf2357ff044f0d18677fe588c790cNick Pelly case TASK_EE_WIPE: 42631949217328bf2357ff044f0d18677fe588c790cNick Pelly executeEeWipe(); 42731949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 42831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 429d1936808c5f37f97fdb876836194ecbfe1cdfff5Nick Pelly 430d1936808c5f37f97fdb876836194ecbfe1cdfff5Nick Pelly // Restore default AsyncTask priority 431d1936808c5f37f97fdb876836194ecbfe1cdfff5Nick Pelly Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 43231949217328bf2357ff044f0d18677fe588c790cNick Pelly return null; 43331949217328bf2357ff044f0d18677fe588c790cNick Pelly } 43431949217328bf2357ff044f0d18677fe588c790cNick Pelly 43531949217328bf2357ff044f0d18677fe588c790cNick Pelly /** 43631949217328bf2357ff044f0d18677fe588c790cNick Pelly * Enable NFC adapter functions. 43731949217328bf2357ff044f0d18677fe588c790cNick Pelly * Does not toggle preferences. 43831949217328bf2357ff044f0d18677fe588c790cNick Pelly */ 43931949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean enableInternal() { 44031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mState == NfcAdapter.STATE_ON) { 44131949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 44231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 44331949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.i(TAG, "Enabling NFC"); 44431949217328bf2357ff044f0d18677fe588c790cNick Pelly updateState(NfcAdapter.STATE_TURNING_ON); 44531949217328bf2357ff044f0d18677fe588c790cNick Pelly 44631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!mDeviceHost.initialize()) { 44731949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.w(TAG, "Error enabling NFC"); 44831949217328bf2357ff044f0d18677fe588c790cNick Pelly updateState(NfcAdapter.STATE_OFF); 44931949217328bf2357ff044f0d18677fe588c790cNick Pelly return false; 45031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 45131949217328bf2357ff044f0d18677fe588c790cNick Pelly 45231949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized(NfcService.this) { 45331949217328bf2357ff044f0d18677fe588c790cNick Pelly mObjectMap.clear(); 45431949217328bf2357ff044f0d18677fe588c790cNick Pelly 4550b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mP2pLinkManager.enableDisable(mIsNdefPushEnabled, true); 45631949217328bf2357ff044f0d18677fe588c790cNick Pelly updateState(NfcAdapter.STATE_ON); 45731949217328bf2357ff044f0d18677fe588c790cNick Pelly } 45831949217328bf2357ff044f0d18677fe588c790cNick Pelly 45931949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Start polling loop */ 46031949217328bf2357ff044f0d18677fe588c790cNick Pelly applyRouting(); 46131949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 46231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 46331949217328bf2357ff044f0d18677fe588c790cNick Pelly 46431949217328bf2357ff044f0d18677fe588c790cNick Pelly /** 46531949217328bf2357ff044f0d18677fe588c790cNick Pelly * Disable all NFC adapter functions. 46631949217328bf2357ff044f0d18677fe588c790cNick Pelly * Does not toggle preferences. 46731949217328bf2357ff044f0d18677fe588c790cNick Pelly */ 46831949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean disableInternal() { 46931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mState == NfcAdapter.STATE_OFF) { 47031949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 47131949217328bf2357ff044f0d18677fe588c790cNick Pelly } 47231949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.i(TAG, "Disabling NFC"); 47331949217328bf2357ff044f0d18677fe588c790cNick Pelly updateState(NfcAdapter.STATE_TURNING_OFF); 47431949217328bf2357ff044f0d18677fe588c790cNick Pelly 47531949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog. 47631949217328bf2357ff044f0d18677fe588c790cNick Pelly * Implemented with a new thread (instead of a Handler or AsyncTask), 47731949217328bf2357ff044f0d18677fe588c790cNick Pelly * because the UI Thread and AsyncTask thread-pools can also get hung 47831949217328bf2357ff044f0d18677fe588c790cNick Pelly * when the NFC controller stops responding */ 47931949217328bf2357ff044f0d18677fe588c790cNick Pelly WatchDogThread watchDog = new WatchDogThread(); 48031949217328bf2357ff044f0d18677fe588c790cNick Pelly watchDog.start(); 48131949217328bf2357ff044f0d18677fe588c790cNick Pelly 48277d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.enableDisable(false, false); 48331949217328bf2357ff044f0d18677fe588c790cNick Pelly 48431949217328bf2357ff044f0d18677fe588c790cNick Pelly // Stop watchdog if tag present 48531949217328bf2357ff044f0d18677fe588c790cNick Pelly // A convenient way to stop the watchdog properly consists of 48631949217328bf2357ff044f0d18677fe588c790cNick Pelly // disconnecting the tag. The polling loop shall be stopped before 48731949217328bf2357ff044f0d18677fe588c790cNick Pelly // to avoid the tag being discovered again. 48831949217328bf2357ff044f0d18677fe588c790cNick Pelly applyRouting(); 48931949217328bf2357ff044f0d18677fe588c790cNick Pelly maybeDisconnectTarget(); 49031949217328bf2357ff044f0d18677fe588c790cNick Pelly 4910b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mNfcDispatcher.setForegroundDispatch(null, null, null); 49231949217328bf2357ff044f0d18677fe588c790cNick Pelly 49331949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean result = mDeviceHost.deinitialize(); 49431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result); 49531949217328bf2357ff044f0d18677fe588c790cNick Pelly 49631949217328bf2357ff044f0d18677fe588c790cNick Pelly watchDog.cancel(); 49731949217328bf2357ff044f0d18677fe588c790cNick Pelly 49831949217328bf2357ff044f0d18677fe588c790cNick Pelly updateState(NfcAdapter.STATE_OFF); 49931949217328bf2357ff044f0d18677fe588c790cNick Pelly 50031949217328bf2357ff044f0d18677fe588c790cNick Pelly return result; 50131949217328bf2357ff044f0d18677fe588c790cNick Pelly } 50231949217328bf2357ff044f0d18677fe588c790cNick Pelly 50331949217328bf2357ff044f0d18677fe588c790cNick Pelly void executeEeWipe() { 50431949217328bf2357ff044f0d18677fe588c790cNick Pelly // TODO: read SE reset list from /system/etc 50531949217328bf2357ff044f0d18677fe588c790cNick Pelly byte[][]apdus = EE_WIPE_APDUS; 50631949217328bf2357ff044f0d18677fe588c790cNick Pelly 50731949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean tempEnable = mState == NfcAdapter.STATE_OFF; 50831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (tempEnable) { 50931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!enableInternal()) { 510ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly Log.w(TAG, "Could not enable NFC to wipe NFC-EE"); 51131949217328bf2357ff044f0d18677fe588c790cNick Pelly return; 512f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 513f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 51431949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.i(TAG, "Executing SE wipe"); 51531949217328bf2357ff044f0d18677fe588c790cNick Pelly int handle = mSecureElement.doOpenSecureElementConnection(); 51631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (handle == 0) { 51731949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.w(TAG, "Could not open the secure element"); 51831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (tempEnable) { 51931949217328bf2357ff044f0d18677fe588c790cNick Pelly disableInternal(); 52031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 52131949217328bf2357ff044f0d18677fe588c790cNick Pelly return; 52231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 52331949217328bf2357ff044f0d18677fe588c790cNick Pelly 52431949217328bf2357ff044f0d18677fe588c790cNick Pelly mDeviceHost.setTimeout(TagTechnology.ISO_DEP, 10000); 52531949217328bf2357ff044f0d18677fe588c790cNick Pelly 52631949217328bf2357ff044f0d18677fe588c790cNick Pelly for (byte[] cmd : apdus) { 527ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly byte[] resp = mSecureElement.doTransceive(handle, cmd); 528ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly if (resp == null) { 529ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly Log.w(TAG, "Transceive failed, could not wipe NFC-EE"); 530ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly break; 531ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly } 53231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 53331949217328bf2357ff044f0d18677fe588c790cNick Pelly 53431949217328bf2357ff044f0d18677fe588c790cNick Pelly mDeviceHost.resetTimeouts(); 53531949217328bf2357ff044f0d18677fe588c790cNick Pelly mSecureElement.doDisconnect(handle); 53631949217328bf2357ff044f0d18677fe588c790cNick Pelly 53731949217328bf2357ff044f0d18677fe588c790cNick Pelly if (tempEnable) { 53831949217328bf2357ff044f0d18677fe588c790cNick Pelly disableInternal(); 53931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 54031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 54131949217328bf2357ff044f0d18677fe588c790cNick Pelly 54231949217328bf2357ff044f0d18677fe588c790cNick Pelly void updateState(int newState) { 54331949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 54431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (newState == mState) { 54531949217328bf2357ff044f0d18677fe588c790cNick Pelly return; 54631949217328bf2357ff044f0d18677fe588c790cNick Pelly } 54731949217328bf2357ff044f0d18677fe588c790cNick Pelly mState = newState; 54831949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); 54931949217328bf2357ff044f0d18677fe588c790cNick Pelly intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 55031949217328bf2357ff044f0d18677fe588c790cNick Pelly intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState); 55131949217328bf2357ff044f0d18677fe588c790cNick Pelly mContext.sendBroadcast(intent); 55231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 55331949217328bf2357ff044f0d18677fe588c790cNick Pelly } 55431949217328bf2357ff044f0d18677fe588c790cNick Pelly } 55531949217328bf2357ff044f0d18677fe588c790cNick Pelly 55631949217328bf2357ff044f0d18677fe588c790cNick Pelly void saveNfcOnSetting(boolean on) { 55731949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (NfcService.this) { 55831949217328bf2357ff044f0d18677fe588c790cNick Pelly mPrefsEditor.putBoolean(PREF_NFC_ON, on); 55931949217328bf2357ff044f0d18677fe588c790cNick Pelly mPrefsEditor.apply(); 56031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 5610e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly } 5620e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 56377d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly void playSound(int sound) { 564d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton synchronized (this) { 565d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f); 566d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 567d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 568d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton 5690e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly @Override 5702f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly public void onTerminate() { 5712f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly super.onTerminate(); 5722f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly // NFC application is persistent, it should not be destroyed by framework 5730e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly Log.wtf(TAG, "NFC service is under attack!"); 5740e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly } 5750e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 5764a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton final class NfcAdapterService extends INfcAdapter.Stub { 577fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 5780e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly public boolean enable() throws RemoteException { 57993d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceAdminPerm(mContext); 5800e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 58131949217328bf2357ff044f0d18677fe588c790cNick Pelly saveNfcOnSetting(true); 58231949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) { 58331949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.i(TAG, "denying enable() request (airplane mode)"); 58431949217328bf2357ff044f0d18677fe588c790cNick Pelly return false; 585f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 58631949217328bf2357ff044f0d18677fe588c790cNick Pelly new EnableDisableTask().execute(TASK_ENABLE); 58731949217328bf2357ff044f0d18677fe588c790cNick Pelly 58831949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 589f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 590f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 591fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 5920e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly public boolean disable() throws RemoteException { 59393d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceAdminPerm(mContext); 5940e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 59531949217328bf2357ff044f0d18677fe588c790cNick Pelly saveNfcOnSetting(false); 59631949217328bf2357ff044f0d18677fe588c790cNick Pelly new EnableDisableTask().execute(TASK_DISABLE); 59731949217328bf2357ff044f0d18677fe588c790cNick Pelly 59831949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 599f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 600f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 601fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 6020b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly public boolean isNdefPushEnabled() throws RemoteException { 60331949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (NfcService.this) { 6040b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly return mIsNdefPushEnabled; 60531949217328bf2357ff044f0d18677fe588c790cNick Pelly } 606d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 607d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen 608d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen @Override 6090b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly public boolean enableNdefPush() throws RemoteException { 610d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen NfcService.enforceAdminPerm(mContext); 611d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen synchronized(NfcService.this) { 6120b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly if (mIsNdefPushEnabled) { 61331949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 61431949217328bf2357ff044f0d18677fe588c790cNick Pelly } 6150b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly Log.i(TAG, "enabling NDEF Push"); 6160b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, true); 61731949217328bf2357ff044f0d18677fe588c790cNick Pelly mPrefsEditor.apply(); 6180b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mIsNdefPushEnabled = true; 61931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (isNfcEnabled()) { 62077d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.enableDisable(true, true); 621d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 622d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 623d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen return true; 624d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 625d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen 626d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen @Override 6270b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly public boolean disableNdefPush() throws RemoteException { 628d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen NfcService.enforceAdminPerm(mContext); 629d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen synchronized(NfcService.this) { 6300b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly if (!mIsNdefPushEnabled) { 63131949217328bf2357ff044f0d18677fe588c790cNick Pelly return true; 63231949217328bf2357ff044f0d18677fe588c790cNick Pelly } 6330b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly Log.i(TAG, "disabling NDEF Push"); 6340b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, false); 63531949217328bf2357ff044f0d18677fe588c790cNick Pelly mPrefsEditor.apply(); 6360b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mIsNdefPushEnabled = false; 63731949217328bf2357ff044f0d18677fe588c790cNick Pelly if (isNfcEnabled()) { 63877d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.enableDisable(false, true); 639d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 640d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 641d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen return true; 642d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 643d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen 644d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen @Override 6450b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly public void setForegroundDispatch(PendingIntent intent, 64624dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton IntentFilter[] filters, TechListParcel techListsParcel) { 64705973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 648a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton 6490b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly // Short-cut the disable path 6500b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly if (intent == null && filters == null && techListsParcel == null) { 6510b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mNfcDispatcher.setForegroundDispatch(null, null, null); 6520b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly return; 653ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton } 654a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton 655a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton // Validate the IntentFilters 656a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton if (filters != null) { 657a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton if (filters.length == 0) { 658a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton filters = null; 659a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton } else { 660a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton for (IntentFilter filter : filters) { 661a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton if (filter == null) { 662a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton throw new IllegalArgumentException("null IntentFilter"); 663a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton } 664a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton } 665a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton } 666a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton } 667a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton 66824dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton // Validate the tech lists 66924dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton String[][] techLists = null; 67024dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton if (techListsParcel != null) { 67124dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton techLists = techListsParcel.getTechLists(); 67224dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton } 67349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly 6740b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mNfcDispatcher.setForegroundDispatch(intent, filters, techLists); 6752094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks } 6762094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks 6772094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks @Override 6780b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly public void setForegroundNdefPush(NdefMessage msg, INdefPushCallback callback) { 679ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 6800b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly mP2pLinkManager.setNdefToSend(msg, callback); 681ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton } 682ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton 683ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton @Override 6840e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly public INfcTag getNfcTagInterface() throws RemoteException { 685d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 6860e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly return mNfcTagService; 6870e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly } 6880e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 689fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 69049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public INfcAdapterExtras getNfcAdapterExtrasInterface() { 69193d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceNfceeAdminPerm(mContext); 69249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return mExtrasService; 6930bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 6940bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 695fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 69631949217328bf2357ff044f0d18677fe588c790cNick Pelly public int getState() throws RemoteException { 69731949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (NfcService.this) { 69831949217328bf2357ff044f0d18677fe588c790cNick Pelly return mState; 69931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 70031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 70131949217328bf2357ff044f0d18677fe588c790cNick Pelly 70231949217328bf2357ff044f0d18677fe588c790cNick Pelly @Override 70331949217328bf2357ff044f0d18677fe588c790cNick Pelly protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 70431949217328bf2357ff044f0d18677fe588c790cNick Pelly NfcService.this.dump(fd, pw, args); 7050e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly } 7060e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly }; 7070e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 7084a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton final class TagService extends INfcTag.Stub { 709fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 710f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly public int close(int nativeHandle) throws RemoteException { 711d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 712bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 713f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 714f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 71531949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 716f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.ERROR_NOT_INITIALIZED; 717f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 718f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 719f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 720f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 721f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly if (tag != null) { 722b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau /* Remove the device from the hmap */ 723b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau unregisterObject(nativeHandle); 72421545af22f9b913ec9cb124287aab2fcb0cf2b3bNick Pelly tag.disconnect(); 725b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau return ErrorCodes.SUCCESS; 726f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 727f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* Restart polling loop for notification */ 72849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly applyRouting(); 729f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.ERROR_DISCONNECT; 730f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 731f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 732fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 733ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen public int connect(int nativeHandle, int technology) throws RemoteException { 734d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 735bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 736f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 737f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 73831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 739f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.ERROR_NOT_INITIALIZED; 740f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 741f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 742f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 743f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 744b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau if (tag == null) { 745b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau return ErrorCodes.ERROR_DISCONNECT; 746f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 747ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen 7481b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen if (technology == TagTechnology.NFC_B) { 7491b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen return ErrorCodes.ERROR_NOT_SUPPORTED; 7501b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen } 7511b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen 752ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen // Note that on most tags, all technologies are behind a single 753ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen // handle. This means that the connect at the lower levels 754ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen // will do nothing, as the tag is already connected to that handle. 755ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen if (tag.connect(technology)) { 756ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen return ErrorCodes.SUCCESS; 757ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen } else { 758ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen return ErrorCodes.ERROR_DISCONNECT; 759ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen } 760f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 761f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 762fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 763aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen public int reconnect(int nativeHandle) throws RemoteException { 764aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 765aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen 766f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 767aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen 768aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen // Check if NFC is enabled 76931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 770aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen return ErrorCodes.ERROR_NOT_INITIALIZED; 771aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen } 772aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen 773aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen /* find the tag in the hmap */ 774f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 775aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen if (tag != null) { 776aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen if (tag.reconnect()) { 777aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen return ErrorCodes.SUCCESS; 778aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen } else { 779aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen return ErrorCodes.ERROR_DISCONNECT; 780aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen } 781aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen } 782aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen return ErrorCodes.ERROR_DISCONNECT; 783aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen } 784aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen 785aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen @Override 786b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton public int[] getTechList(int nativeHandle) throws RemoteException { 787d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 788bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 789f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly // Check if NFC is enabled 79031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 791f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 792f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 793f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 794f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 795f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = (TagEndpoint) findObject(nativeHandle); 796f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly if (tag != null) { 797b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton return tag.getTechList(); 798f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 799f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 800f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 801f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 802fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 803f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly public byte[] getUid(int nativeHandle) throws RemoteException { 804f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 805f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly byte[] uid; 806f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 807f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly // Check if NFC is enabled 80831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 809f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 810f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 811f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 812f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 813f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 814f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly if (tag != null) { 815f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly uid = tag.getUid(); 816f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return uid; 817f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 818f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 819f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 820f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 821fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 822b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau public boolean isPresent(int nativeHandle) throws RemoteException { 823f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 824b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 825b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau // Check if NFC is enabled 82631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 827b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau return false; 828b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau } 829b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 830b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau /* find the tag in the hmap */ 831f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 832b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau if (tag == null) { 833b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau return false; 834b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau } 835b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 836ab2b44b97936d2c5dbf6eda1245ca793e840713fMartijn Coenen return tag.isPresent(); 837b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau } 838b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 839fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 840f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly public boolean isNdef(int nativeHandle) throws RemoteException { 841f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 842f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 843f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly // Check if NFC is enabled 84431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 8452c3f9be8111dd454e430ffb327c051ff9d2bba21Daniel Tomas return false; 846f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 847f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 848f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 849f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 8503ba3b10867c36bff57b72ff99c7b56d63d418f3fMartijn Coenen int[] ndefInfo = new int[2]; 8512c3f9be8111dd454e430ffb327c051ff9d2bba21Daniel Tomas if (tag == null) { 8522c3f9be8111dd454e430ffb327c051ff9d2bba21Daniel Tomas return false; 853f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 85470bbea61637e3f9eb7202efd243b9d2f9516a06aNick Pelly return tag.checkNdef(ndefInfo); 855f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 856f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 857fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 8589d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenen public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw) 85997c6942c7c7f9df3bb8dbcc01cf7bb6e2e090005Martijn Coenen throws RemoteException { 860d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 861bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 862f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 863f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly byte[] response; 864f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 865f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly // Check if NFC is enabled 86631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 867f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 868f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 869f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 870f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 871f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 872f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly if (tag != null) { 873bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen // Check if length is within limits 874bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) { 875bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null); 876bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen } 8779d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenen int[] targetLost = new int[1]; 8789d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenen response = tag.transceive(data, raw, targetLost); 879bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen int result; 880bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen if (response != null) { 881bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen result = TransceiveResult.RESULT_SUCCESS; 882bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen } else if (targetLost[0] == 1) { 883bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen result = TransceiveResult.RESULT_TAGLOST; 884bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen } else { 885bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen result = TransceiveResult.RESULT_FAILURE; 886bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen } 887bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen return new TransceiveResult(result, response); 888f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 889f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 890f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 891f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 892fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 8933fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton public NdefMessage ndefRead(int nativeHandle) throws RemoteException { 894d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 895bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 896f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag; 897f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 898f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly // Check if NFC is enabled 89931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 900f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 901f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 902f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 903f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 904f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 905f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly if (tag != null) { 906f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton byte[] buf = tag.readNdef(); 907f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton if (buf == null) { 908f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 909f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } 910f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 911f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* Create an NdefMessage */ 912f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly try { 913f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return new NdefMessage(buf); 914f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } catch (FormatException e) { 915f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 916f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 917f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 918f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return null; 919f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 920f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 921fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 9223fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException { 923d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 924bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly 925f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag; 926f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 927f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly // Check if NFC is enabled 92831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 929f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.ERROR_NOT_INITIALIZED; 930f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 931f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 932f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* find the tag in the hmap */ 933f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 934f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly if (tag == null) { 935f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.ERROR_IO; 936f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 937f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 938f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton if (tag.writeNdef(msg.toByteArray())) { 939f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.SUCCESS; 940f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } else { 941f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly return ErrorCodes.ERROR_IO; 942f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 943f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 944f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 945f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 946fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 947f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly public int getLastError(int nativeHandle) throws RemoteException { 948f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton return(mDeviceHost.doGetLastError()); 949f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 950f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 951fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 9523fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton public boolean ndefIsWritable(int nativeHandle) throws RemoteException { 9533fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton throw new UnsupportedOperationException(); 954f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 955f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 956fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 9573fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton public int ndefMakeReadOnly(int nativeHandle) throws RemoteException { 95803ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 95903ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen 960f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag; 96103ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen 96203ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen // Check if NFC is enabled 96331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 96403ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen return ErrorCodes.ERROR_NOT_INITIALIZED; 96503ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen } 96603ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen 96703ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen /* find the tag in the hmap */ 968f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 96903ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen if (tag == null) { 97003ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen return ErrorCodes.ERROR_IO; 97103ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen } 97203ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen 973f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton if (tag.makeReadOnly()) { 97403ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen return ErrorCodes.SUCCESS; 975f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } else { 97603ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen return ErrorCodes.ERROR_IO; 97703ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen } 978f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 979f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 9800aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen @Override 9810aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen public int formatNdef(int nativeHandle, byte[] key) throws RemoteException { 9820aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 9830aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen 984f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag; 9850aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen 9860aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen // Check if NFC is enabled 98731949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 9880aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen return ErrorCodes.ERROR_NOT_INITIALIZED; 9890aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen } 9900aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen 9910aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen /* find the tag in the hmap */ 992f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 9930aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen if (tag == null) { 9940aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen return ErrorCodes.ERROR_IO; 9950aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen } 9960aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen 9970aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen if (tag.formatNdef(key)) { 9980aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen return ErrorCodes.SUCCESS; 999f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton } else { 10000aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen return ErrorCodes.ERROR_IO; 10010aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen } 10020aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen } 10030aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen 10041b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen @Override 10053fb14d0868594c78a777e805545209636814e223Martijn Coenen public Tag rediscover(int nativeHandle) throws RemoteException { 10063fb14d0868594c78a777e805545209636814e223Martijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 10073fb14d0868594c78a777e805545209636814e223Martijn Coenen 1008f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton TagEndpoint tag = null; 10093fb14d0868594c78a777e805545209636814e223Martijn Coenen 10103fb14d0868594c78a777e805545209636814e223Martijn Coenen // Check if NFC is enabled 101131949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 10123fb14d0868594c78a777e805545209636814e223Martijn Coenen return null; 10133fb14d0868594c78a777e805545209636814e223Martijn Coenen } 10143fb14d0868594c78a777e805545209636814e223Martijn Coenen 10153fb14d0868594c78a777e805545209636814e223Martijn Coenen /* find the tag in the hmap */ 1016f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tag = (TagEndpoint) findObject(nativeHandle); 10173fb14d0868594c78a777e805545209636814e223Martijn Coenen if (tag != null) { 10183fb14d0868594c78a777e805545209636814e223Martijn Coenen // For now the prime usecase for rediscover() is to be able 10193fb14d0868594c78a777e805545209636814e223Martijn Coenen // to access the NDEF technology after formatting without 10203fb14d0868594c78a777e805545209636814e223Martijn Coenen // having to remove the tag from the field, or similar 10213fb14d0868594c78a777e805545209636814e223Martijn Coenen // to have access to NdefFormatable in case low-level commands 10223fb14d0868594c78a777e805545209636814e223Martijn Coenen // were used to remove NDEF. So instead of doing a full stack 10233fb14d0868594c78a777e805545209636814e223Martijn Coenen // rediscover (which is poorly supported at the moment anyway), 10243fb14d0868594c78a777e805545209636814e223Martijn Coenen // we simply remove these two technologies and detect them 10253fb14d0868594c78a777e805545209636814e223Martijn Coenen // again. 10263fb14d0868594c78a777e805545209636814e223Martijn Coenen tag.removeTechnology(TagTechnology.NDEF); 10273fb14d0868594c78a777e805545209636814e223Martijn Coenen tag.removeTechnology(TagTechnology.NDEF_FORMATABLE); 1028f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton NdefMessage[] msgs = tag.findAndReadNdef(); 10293fb14d0868594c78a777e805545209636814e223Martijn Coenen // Build a new Tag object to return 10303fb14d0868594c78a777e805545209636814e223Martijn Coenen Tag newTag = new Tag(tag.getUid(), tag.getTechList(), 10314a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton tag.getTechExtras(), tag.getHandle(), this); 10323fb14d0868594c78a777e805545209636814e223Martijn Coenen return newTag; 10333fb14d0868594c78a777e805545209636814e223Martijn Coenen } 10343fb14d0868594c78a777e805545209636814e223Martijn Coenen return null; 10353fb14d0868594c78a777e805545209636814e223Martijn Coenen } 10363fb14d0868594c78a777e805545209636814e223Martijn Coenen 10371b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen @Override 1038fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen public int setTimeout(int tech, int timeout) throws RemoteException { 10391b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 1040f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton boolean success = mDeviceHost.setTimeout(tech, timeout); 1041fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen if (success) { 1042fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen return ErrorCodes.SUCCESS; 1043fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen } else { 1044fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen return ErrorCodes.ERROR_INVALID_PARAM; 1045fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen } 1046dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen } 1047dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen 1048dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen @Override 1049358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen public int getTimeout(int tech) throws RemoteException { 1050358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 1051358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen 1052358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen return mDeviceHost.getTimeout(tech); 1053358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen } 1054358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen 1055358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen @Override 1056dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen public void resetTimeouts() throws RemoteException { 1057dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 1058dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen 1059f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton mDeviceHost.resetTimeouts(); 10601b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen } 1061bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen 1062bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen @Override 1063bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen public boolean canMakeReadOnly(int ndefType) throws RemoteException { 1064bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 1065bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen 1066bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen return mDeviceHost.canMakeReadOnly(ndefType); 1067bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen } 1068bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen 1069bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen @Override 1070bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen public int getMaxTransceiveLength(int tech) throws RemoteException { 1071bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); 1072bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen 1073bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen return mDeviceHost.getMaxTransceiveLength(tech); 1074bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen } 10750e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly }; 1076f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 1077dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly private void _nfcEeClose(boolean checkPid, int callingPid) throws IOException { 1078dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly // Blocks until a pending open() or transceive() times out. 1079dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly //TODO: This is incorrect behavior - the close should interrupt pending 1080dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly // operations. However this is not supported by current hardware. 1081dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly 1082dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly synchronized(NfcService.this) { 108331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 1084dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly throw new IOException("NFC adapter is disabled"); 1085dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly } 1086dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly if (mOpenEe == null) { 1087dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly throw new IOException("NFC EE closed"); 1088dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly } 1089dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly if (checkPid && mOpenEe.pid != -1 && callingPid != mOpenEe.pid) { 1090dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly throw new SecurityException("Wrong PID"); 1091dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly } 1092dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly 1093f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton mDeviceHost.resetTimeouts(); 1094dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly mSecureElement.doDisconnect(mOpenEe.handle); 1095dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly mOpenEe = null; 1096dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly 1097dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly applyRouting(); 1098dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly } 1099dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly } 1100dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly 11014a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton final class NfcAdapterExtrasService extends INfcAdapterExtras.Stub { 110249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private Bundle writeNoException() { 110349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly Bundle p = new Bundle(); 110449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly p.putInt("e", 0); 110549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return p; 110649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 110749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private Bundle writeIoException(IOException e) { 110849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly Bundle p = new Bundle(); 110949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly p.putInt("e", -1); 111049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly p.putString("m", e.getMessage()); 111149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return p; 111249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11130bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 1114bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 111549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public Bundle open(IBinder b) throws RemoteException { 111693d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceNfceeAdminPerm(mContext); 1117bd555ee64250126b60b24814120a2049943920caNick Pelly 111849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly Bundle result; 111949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly try { 112049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly _open(b); 112149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result = writeNoException(); 112249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } catch (IOException e) { 112349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result = writeIoException(e); 11240bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 112549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return result; 112649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11270bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 112849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private void _open(IBinder b) throws IOException, RemoteException { 112949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly synchronized(NfcService.this) { 113031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 113149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly throw new IOException("NFC adapter is disabled"); 113249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 113349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly if (mOpenEe != null) { 113449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly throw new IOException("NFC EE already open"); 113549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11360bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 113749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly int handle = mSecureElement.doOpenSecureElementConnection(); 113849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly if (handle == 0) { 113949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly throw new IOException("NFC EE failed to open"); 114049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 1141f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton mDeviceHost.setTimeout(TagTechnology.ISO_DEP, 10000); 1142ba6401757f8017faeb77423f2d08fd51be1d1051Nick Pelly 114349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly mOpenEe = new OpenSecureElement(getCallingPid(), handle); 114449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly try { 114549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly b.linkToDeath(mOpenEe, 0); 114649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } catch (RemoteException e) { 114749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly mOpenEe.binderDied(); 114849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 114984e1e0adc2516afd35ebab029a52e764e0490559Jason parks 115084e1e0adc2516afd35ebab029a52e764e0490559Jason parks // Add the calling package to the list of packages that have accessed 115184e1e0adc2516afd35ebab029a52e764e0490559Jason parks // the secure element. 115284e1e0adc2516afd35ebab029a52e764e0490559Jason parks for (String packageName : getPackageManager().getPackagesForUid(getCallingUid())) { 115384e1e0adc2516afd35ebab029a52e764e0490559Jason parks mSePackages.add(packageName); 115484e1e0adc2516afd35ebab029a52e764e0490559Jason parks } 115549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11560bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 11570bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 1158bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 115949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public Bundle close() throws RemoteException { 116093d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceNfceeAdminPerm(mContext); 11610bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 116249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly Bundle result; 116349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly try { 1164dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly _nfcEeClose(true, getCallingPid()); 116549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result = writeNoException(); 116649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } catch (IOException e) { 116749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result = writeIoException(e); 11680bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 116949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return result; 117049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11710bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 1172bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 117349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public Bundle transceive(byte[] in) throws RemoteException { 117493d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceNfceeAdminPerm(mContext); 1175bd555ee64250126b60b24814120a2049943920caNick Pelly 117649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly Bundle result; 117749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly byte[] out; 117849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly try { 117949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly out = _transceive(in); 118049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result = writeNoException(); 118149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result.putByteArray("out", out); 118249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } catch (IOException e) { 118349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly result = writeIoException(e); 11840bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 118549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return result; 118649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11870bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 118849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private byte[] _transceive(byte[] data) throws IOException, RemoteException { 118949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly synchronized(NfcService.this) { 119031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 119149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly throw new IOException("NFC is not enabled"); 119249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 119349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly if (mOpenEe == null){ 119449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly throw new IOException("NFC EE is not open"); 119549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 119649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly if (getCallingPid() != mOpenEe.pid) { 119749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly throw new SecurityException("Wrong PID"); 119849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 11990bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 12000bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 120149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return mSecureElement.doTransceive(mOpenEe.handle, data); 12020bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 12030bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 1204bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 120549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public int getCardEmulationRoute() throws RemoteException { 120693d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceNfceeAdminPerm(mContext); 120749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly return mEeRoutingState; 12080bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 12090bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 1210bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 121149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public void setCardEmulationRoute(int route) throws RemoteException { 121293d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton NfcService.enforceNfceeAdminPerm(mContext); 121349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly mEeRoutingState = route; 121449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly applyRouting(); 12150bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 1216bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks 1217bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 12185a8844da9bc569aa7289426bf7b96e7eef90abf5Nick Pelly public void authenticate(byte[] token) throws RemoteException { 1219bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks NfcService.enforceNfceeAdminPerm(mContext); 1220bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 12210bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas }; 12220bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 122349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly /** resources kept while secure element is open */ 122449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly private class OpenSecureElement implements IBinder.DeathRecipient { 122549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public int pid; // pid that opened SE 122649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public int handle; // low-level handle 122749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public OpenSecureElement(int pid, int handle) { 122849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly this.pid = pid; 122949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly this.handle = handle; 123049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly } 1231bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks @Override 123249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly public void binderDied() { 123349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly synchronized (NfcService.this) { 123449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly if (DBG) Log.d(TAG, "Tracked app " + pid + " died"); 123549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly pid = -1; 12360bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas try { 1237dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly _nfcEeClose(false, -1); 1238dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly } catch (IOException e) { /* already closed */ } 12390bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 12400bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 12410bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas } 12420bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas 124331949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean isNfcEnabled() { 124431949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 124531949217328bf2357ff044f0d18677fe588c790cNick Pelly return mState == NfcAdapter.STATE_ON; 1246e7a398f2f0256a4a80a4ee08b70d48dbfd8da6d2Nick Pelly } 1247aa122139d77645149c09c9815fd45e7b87ce7170Nick Pelly } 1248aa122139d77645149c09c9815fd45e7b87ce7170Nick Pelly 124931949217328bf2357ff044f0d18677fe588c790cNick Pelly class WatchDogThread extends Thread { 12502edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly boolean mWatchDogCanceled = false; 12512edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly @Override 12522edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly public void run() { 12532edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly boolean slept = false; 12542edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly while (!slept) { 12552edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly try { 12562edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly Thread.sleep(10000); 12572edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly slept = true; 12582edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } catch (InterruptedException e) { } 12592edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } 12602edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly synchronized (this) { 12612edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly if (!mWatchDogCanceled) { 12622edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly // Trigger watch-dog 12632edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly Log.e(TAG, "Watch dog triggered"); 12642083287b83a587d8f6e9ad829ea18041dc17d842Nick Pelly mDeviceHost.doAbort(); 12652edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } 12662edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } 12672edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } 12682edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly public synchronized void cancel() { 12692edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly mWatchDogCanceled = true; 12702edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } 12712edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly } 12722edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly 127349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly /** apply NFC discovery and EE routing */ 1274e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton void applyRouting() { 1275e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton synchronized (this) { 1276e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton if (!isNfcEnabled() || mOpenEe != null) { 1277e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton return; 1278e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton } 1279e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton if (mIsScreenUnlocked) { 1280e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton if (mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) { 1281e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "NFC-EE routing ON"); 1282e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton mDeviceHost.doSelectSecureElement(); 1283e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton } else { 1284e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "NFC-EE routing OFF"); 1285e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton mDeviceHost.doDeselectSecureElement(); 1286e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton } 1287e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "NFC-C polling ON"); 1288e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton mDeviceHost.enableDiscovery(); 1289221b4d6ee301fbfe19402798f7d3c11e6878c888daniel_tomas } else { 129049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly Log.d(TAG, "NFC-EE routing OFF"); 1291f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton mDeviceHost.doDeselectSecureElement(); 1292e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "NFC-C polling OFF"); 1293e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton mDeviceHost.disableDiscovery(); 1294221b4d6ee301fbfe19402798f7d3c11e6878c888daniel_tomas } 129565945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly } 129665945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly } 129765945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly 12982436ffe91853535fad87b7a8e03d8883bae20f20Arnaud Ferir /** Disconnect any target if present */ 129931949217328bf2357ff044f0d18677fe588c790cNick Pelly void maybeDisconnectTarget() { 130031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!isNfcEnabled()) { 1301a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly return; 1302a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly } 130331949217328bf2357ff044f0d18677fe588c790cNick Pelly Object[] objectsToDisconnect; 130431949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 130531949217328bf2357ff044f0d18677fe588c790cNick Pelly Object[] objectValues = mObjectMap.values().toArray(); 130631949217328bf2357ff044f0d18677fe588c790cNick Pelly // Copy the array before we clear mObjectMap, 130731949217328bf2357ff044f0d18677fe588c790cNick Pelly // just in case the HashMap values are backed by the same array 130831949217328bf2357ff044f0d18677fe588c790cNick Pelly objectsToDisconnect = Arrays.copyOf(objectValues, objectValues.length); 130931949217328bf2357ff044f0d18677fe588c790cNick Pelly mObjectMap.clear(); 131031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 131131949217328bf2357ff044f0d18677fe588c790cNick Pelly for (Object o : objectsToDisconnect) { 131231949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "disconnecting " + o.getClass().getName()); 131331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (o instanceof TagEndpoint) { 131431949217328bf2357ff044f0d18677fe588c790cNick Pelly // Disconnect from tags 131531949217328bf2357ff044f0d18677fe588c790cNick Pelly TagEndpoint tag = (TagEndpoint) o; 131631949217328bf2357ff044f0d18677fe588c790cNick Pelly tag.disconnect(); 131731949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (o instanceof NfcDepEndpoint) { 131831949217328bf2357ff044f0d18677fe588c790cNick Pelly // Disconnect from P2P devices 131931949217328bf2357ff044f0d18677fe588c790cNick Pelly NfcDepEndpoint device = (NfcDepEndpoint) o; 132031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) { 132131949217328bf2357ff044f0d18677fe588c790cNick Pelly // Remote peer is target, request disconnection 132231949217328bf2357ff044f0d18677fe588c790cNick Pelly device.disconnect(); 132331949217328bf2357ff044f0d18677fe588c790cNick Pelly } else { 132431949217328bf2357ff044f0d18677fe588c790cNick Pelly // Remote peer is initiator, we cannot disconnect 132531949217328bf2357ff044f0d18677fe588c790cNick Pelly // Just wait for field removal 1326bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 1327bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 1328bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 1329bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 1330bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks 133131949217328bf2357ff044f0d18677fe588c790cNick Pelly Object findObject(int key) { 133231949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 133331949217328bf2357ff044f0d18677fe588c790cNick Pelly Object device = mObjectMap.get(key); 133431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (device == null) { 133531949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.w(TAG, "Handle not found"); 13362f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly } 133731949217328bf2357ff044f0d18677fe588c790cNick Pelly return device; 13380e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly } 1339f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 1340f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 134131949217328bf2357ff044f0d18677fe588c790cNick Pelly void registerTagObject(TagEndpoint tag) { 134231949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 134331949217328bf2357ff044f0d18677fe588c790cNick Pelly mObjectMap.put(tag.getHandle(), tag); 1344f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 1345b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau } 1346b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 134731949217328bf2357ff044f0d18677fe588c790cNick Pelly void unregisterObject(int handle) { 134831949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 134931949217328bf2357ff044f0d18677fe588c790cNick Pelly mObjectMap.remove(handle); 135031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 1351f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 1352f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 1353d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton /** For use by code in this process */ 13544a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton public LlcpSocket createLlcpSocket(int sap, int miu, int rw, int linearBufferLength) 13554a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton throws IOException, LlcpException { 13564a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton return mDeviceHost.createLlcpSocket(sap, miu, rw, linearBufferLength); 1357d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton } 1358d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 1359d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton /** For use by code in this process */ 13604a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton public LlcpServerSocket createLlcpServerSocket(int sap, String sn, int miu, int rw, 13614a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton int linearBufferLength) throws IOException, LlcpException { 13624a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton return mDeviceHost.createLlcpServerSocket(sap, sn, miu, rw, linearBufferLength); 1363d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton } 1364d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 136557d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton public void sendMockNdefTag(NdefMessage msg) { 1366b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton sendMessage(MSG_MOCK_NDEF, msg); 136757d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton } 136857d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton 1369b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau void sendMessage(int what, Object obj) { 1370b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau Message msg = mHandler.obtainMessage(); 1371b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau msg.what = what; 1372b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau msg.obj = obj; 1373b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau mHandler.sendMessage(msg); 1374b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau } 1375b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 13763fb14d0868594c78a777e805545209636814e223Martijn Coenen final class NfcServiceHandler extends Handler { 1377b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau @Override 1378b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau public void handleMessage(Message msg) { 137931949217328bf2357ff044f0d18677fe588c790cNick Pelly switch (msg.what) { 138031949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_MOCK_NDEF: { 138131949217328bf2357ff044f0d18677fe588c790cNick Pelly NdefMessage ndefMsg = (NdefMessage) msg.obj; 138231949217328bf2357ff044f0d18677fe588c790cNick Pelly Bundle extras = new Bundle(); 138331949217328bf2357ff044f0d18677fe588c790cNick Pelly extras.putParcelable(Ndef.EXTRA_NDEF_MSG, ndefMsg); 138431949217328bf2357ff044f0d18677fe588c790cNick Pelly extras.putInt(Ndef.EXTRA_NDEF_MAXLENGTH, 0); 138531949217328bf2357ff044f0d18677fe588c790cNick Pelly extras.putInt(Ndef.EXTRA_NDEF_CARDSTATE, Ndef.NDEF_MODE_READ_ONLY); 138631949217328bf2357ff044f0d18677fe588c790cNick Pelly extras.putInt(Ndef.EXTRA_NDEF_TYPE, Ndef.TYPE_OTHER); 138731949217328bf2357ff044f0d18677fe588c790cNick Pelly Tag tag = Tag.createMockTag(new byte[] { 0x00 }, 138831949217328bf2357ff044f0d18677fe588c790cNick Pelly new int[] { TagTechnology.NDEF }, 138931949217328bf2357ff044f0d18677fe588c790cNick Pelly new Bundle[] { extras }); 139031949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.d(TAG, "mock NDEF tag, starting corresponding activity"); 139131949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.d(TAG, tag.toString()); 139231949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean delivered = mNfcDispatcher.dispatchTag(tag, 139331949217328bf2357ff044f0d18677fe588c790cNick Pelly new NdefMessage[] { ndefMsg }); 139431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (delivered) { 139577d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly playSound(mEndSound); 139677d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly } else { 139777d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly playSound(mErrorSound); 139831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 139931949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 140031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 140157d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton 140231949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_NDEF_TAG: 140331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Tag detected, notifying applications"); 140431949217328bf2357ff044f0d18677fe588c790cNick Pelly TagEndpoint tag = (TagEndpoint) msg.obj; 140531949217328bf2357ff044f0d18677fe588c790cNick Pelly playSound(mStartSound); 140631949217328bf2357ff044f0d18677fe588c790cNick Pelly NdefMessage[] ndefMsgs = tag.findAndReadNdef(); 1407c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas 140831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (ndefMsgs != null) { 140931949217328bf2357ff044f0d18677fe588c790cNick Pelly tag.startPresenceChecking(); 141031949217328bf2357ff044f0d18677fe588c790cNick Pelly dispatchTagEndpoint(tag, ndefMsgs); 141131949217328bf2357ff044f0d18677fe588c790cNick Pelly } else { 141231949217328bf2357ff044f0d18677fe588c790cNick Pelly if (tag.reconnect()) { 141331949217328bf2357ff044f0d18677fe588c790cNick Pelly tag.startPresenceChecking(); 141431949217328bf2357ff044f0d18677fe588c790cNick Pelly dispatchTagEndpoint(tag, null); 141531949217328bf2357ff044f0d18677fe588c790cNick Pelly } else { 141631949217328bf2357ff044f0d18677fe588c790cNick Pelly tag.disconnect(); 141731949217328bf2357ff044f0d18677fe588c790cNick Pelly playSound(mErrorSound); 141831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 141931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 142031949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 142131949217328bf2357ff044f0d18677fe588c790cNick Pelly 142231949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_CARD_EMULATION: 142331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Card Emulation message"); 142431949217328bf2357ff044f0d18677fe588c790cNick Pelly byte[] aid = (byte[]) msg.obj; 142531949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Send broadcast */ 142631949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent aidIntent = new Intent(); 142731949217328bf2357ff044f0d18677fe588c790cNick Pelly aidIntent.setAction(ACTION_AID_SELECTED); 142831949217328bf2357ff044f0d18677fe588c790cNick Pelly aidIntent.putExtra(EXTRA_AID, aid); 142931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Broadcasting " + ACTION_AID_SELECTED); 143014a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton sendSeBroadcast(aidIntent); 143131949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 143231949217328bf2357ff044f0d18677fe588c790cNick Pelly 143331949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_SE_EMV_CARD_REMOVAL: 143431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Card Removal message"); 143531949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Send broadcast */ 143631949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent cardRemovalIntent = new Intent(); 143731949217328bf2357ff044f0d18677fe588c790cNick Pelly cardRemovalIntent.setAction(ACTION_EMV_CARD_REMOVAL); 143831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Broadcasting " + ACTION_EMV_CARD_REMOVAL); 143914a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton sendSeBroadcast(cardRemovalIntent); 144031949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 144131949217328bf2357ff044f0d18677fe588c790cNick Pelly 144231949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_SE_APDU_RECEIVED: 144331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "APDU Received message"); 144431949217328bf2357ff044f0d18677fe588c790cNick Pelly byte[] apduBytes = (byte[]) msg.obj; 144531949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Send broadcast */ 144631949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent apduReceivedIntent = new Intent(); 144731949217328bf2357ff044f0d18677fe588c790cNick Pelly apduReceivedIntent.setAction(ACTION_APDU_RECEIVED); 144831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (apduBytes != null && apduBytes.length > 0) { 144931949217328bf2357ff044f0d18677fe588c790cNick Pelly apduReceivedIntent.putExtra(EXTRA_APDU_BYTES, apduBytes); 145031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 145131949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Broadcasting " + ACTION_APDU_RECEIVED); 145214a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton sendSeBroadcast(apduReceivedIntent); 145331949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 145431949217328bf2357ff044f0d18677fe588c790cNick Pelly 145531949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_SE_MIFARE_ACCESS: 145631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "MIFARE access message"); 145731949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Send broadcast */ 145831949217328bf2357ff044f0d18677fe588c790cNick Pelly byte[] mifareCmd = (byte[]) msg.obj; 145931949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent mifareAccessIntent = new Intent(); 146031949217328bf2357ff044f0d18677fe588c790cNick Pelly mifareAccessIntent.setAction(ACTION_MIFARE_ACCESS_DETECTED); 146131949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mifareCmd != null && mifareCmd.length > 1) { 146231949217328bf2357ff044f0d18677fe588c790cNick Pelly int mifareBlock = mifareCmd[1] & 0xff; 146331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Mifare Block=" + mifareBlock); 146431949217328bf2357ff044f0d18677fe588c790cNick Pelly mifareAccessIntent.putExtra(EXTRA_MIFARE_BLOCK, mifareBlock); 146531949217328bf2357ff044f0d18677fe588c790cNick Pelly } 146631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Broadcasting " + ACTION_MIFARE_ACCESS_DETECTED); 146714a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton sendSeBroadcast(mifareAccessIntent); 146831949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 1469c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas 147031949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_LLCP_LINK_ACTIVATION: 147131949217328bf2357ff044f0d18677fe588c790cNick Pelly llcpActivated((NfcDepEndpoint) msg.obj); 147231949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 147331949217328bf2357ff044f0d18677fe588c790cNick Pelly 147431949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_LLCP_LINK_DEACTIVATED: 147531949217328bf2357ff044f0d18677fe588c790cNick Pelly NfcDepEndpoint device = (NfcDepEndpoint) msg.obj; 147631949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean needsDisconnect = false; 147731949217328bf2357ff044f0d18677fe588c790cNick Pelly 147831949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.d(TAG, "LLCP Link Deactivated message. Restart polling loop."); 147931949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (NfcService.this) { 148031949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Check if the device has been already unregistered */ 148131949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mObjectMap.remove(device.getHandle()) != null) { 148231949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Disconnect if we are initiator */ 148331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) { 148431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "disconnecting from target"); 148531949217328bf2357ff044f0d18677fe588c790cNick Pelly needsDisconnect = true; 148631949217328bf2357ff044f0d18677fe588c790cNick Pelly } else { 148731949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "not disconnecting from initiator"); 148831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 148931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 149031949217328bf2357ff044f0d18677fe588c790cNick Pelly } 149131949217328bf2357ff044f0d18677fe588c790cNick Pelly if (needsDisconnect) { 149231949217328bf2357ff044f0d18677fe588c790cNick Pelly device.disconnect(); // restarts polling loop 149331949217328bf2357ff044f0d18677fe588c790cNick Pelly } 149431949217328bf2357ff044f0d18677fe588c790cNick Pelly 149577d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.onLlcpDeactivated(); 149631949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 149731949217328bf2357ff044f0d18677fe588c790cNick Pelly 149831949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_TARGET_DESELECTED: 149931949217328bf2357ff044f0d18677fe588c790cNick Pelly /* Broadcast Intent Target Deselected */ 150031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Target Deselected"); 150131949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent intent = new Intent(); 150231949217328bf2357ff044f0d18677fe588c790cNick Pelly intent.setAction(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION); 150331949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "Broadcasting Intent"); 150431949217328bf2357ff044f0d18677fe588c790cNick Pelly mContext.sendOrderedBroadcast(intent, NFC_PERM); 150531949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 150631949217328bf2357ff044f0d18677fe588c790cNick Pelly 150731949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_SE_FIELD_ACTIVATED: { 150831949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "SE FIELD ACTIVATED"); 150931949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent eventFieldOnIntent = new Intent(); 151031949217328bf2357ff044f0d18677fe588c790cNick Pelly eventFieldOnIntent.setAction(ACTION_RF_FIELD_ON_DETECTED); 151114a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton sendSeBroadcast(eventFieldOnIntent); 151231949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 151331949217328bf2357ff044f0d18677fe588c790cNick Pelly } 151431949217328bf2357ff044f0d18677fe588c790cNick Pelly 151531949217328bf2357ff044f0d18677fe588c790cNick Pelly case MSG_SE_FIELD_DEACTIVATED: { 151631949217328bf2357ff044f0d18677fe588c790cNick Pelly if (DBG) Log.d(TAG, "SE FIELD DEACTIVATED"); 151731949217328bf2357ff044f0d18677fe588c790cNick Pelly Intent eventFieldOffIntent = new Intent(); 151831949217328bf2357ff044f0d18677fe588c790cNick Pelly eventFieldOffIntent.setAction(ACTION_RF_FIELD_OFF_DETECTED); 151914a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton sendSeBroadcast(eventFieldOffIntent); 152031949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 152131949217328bf2357ff044f0d18677fe588c790cNick Pelly } 152231949217328bf2357ff044f0d18677fe588c790cNick Pelly 152331949217328bf2357ff044f0d18677fe588c790cNick Pelly default: 152431949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.e(TAG, "Unknown message received"); 152531949217328bf2357ff044f0d18677fe588c790cNick Pelly break; 152631949217328bf2357ff044f0d18677fe588c790cNick Pelly } 1527b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau } 1528d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton 152914a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton private void sendSeBroadcast(Intent intent) { 15308e85fe445d229fba5d101b4f95fe0dfa34a8601emike wakerly mNfcDispatcher.resumeAppSwitches(); 153114a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 153214a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton mContext.sendBroadcast(intent, NFCEE_ADMIN_PERM); 153314a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton } 153414a4b124592b50dcec47d9b07232fff0188a4e02Jeff Hamilton 1535d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton private boolean llcpActivated(NfcDepEndpoint device) { 1536d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton Log.d(TAG, "LLCP Activation message"); 1537d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton 1538d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) { 1539d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (DBG) Log.d(TAG, "NativeP2pDevice.MODE_P2P_TARGET"); 1540d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (device.connect()) { 1541d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton /* Check LLCP compliancy */ 1542d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (mDeviceHost.doCheckLlcp()) { 1543d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton /* Activate LLCP Link */ 1544d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (mDeviceHost.doActivateLlcp()) { 1545d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (DBG) Log.d(TAG, "Initiator Activate LLCP OK"); 154631949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean isZeroClickOn; 154731949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (NfcService.this) { 154831949217328bf2357ff044f0d18677fe588c790cNick Pelly // Register P2P device 154931949217328bf2357ff044f0d18677fe588c790cNick Pelly mObjectMap.put(device.getHandle(), device); 1550d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 155177d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.onLlcpActivated(); 1552d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton return true; 1553d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } else { 1554d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton /* should not happen */ 1555d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton Log.w(TAG, "Initiator LLCP activation failed. Disconnect."); 1556d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton device.disconnect(); 1557d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1558d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } else { 1559d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (DBG) Log.d(TAG, "Remote Target does not support LLCP. Disconnect."); 1560d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton device.disconnect(); 1561d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1562d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } else { 1563d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (DBG) Log.d(TAG, "Cannot connect remote Target. Polling loop restarted."); 1564d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton /* 1565d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton * The polling loop should have been restarted in failing 1566d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton * doConnect 1567d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton */ 1568d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1569d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } else if (device.getMode() == NfcDepEndpoint.MODE_P2P_INITIATOR) { 1570d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (DBG) Log.d(TAG, "NativeP2pDevice.MODE_P2P_INITIATOR"); 1571d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton /* Check LLCP compliancy */ 1572d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (mDeviceHost.doCheckLlcp()) { 1573d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton /* Activate LLCP Link */ 1574d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (mDeviceHost.doActivateLlcp()) { 1575d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton if (DBG) Log.d(TAG, "Target Activate LLCP OK"); 157631949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean isZeroClickOn; 157731949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (NfcService.this) { 157831949217328bf2357ff044f0d18677fe588c790cNick Pelly // Register P2P device 157931949217328bf2357ff044f0d18677fe588c790cNick Pelly mObjectMap.put(device.getHandle(), device); 1580d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen } 158177d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.onLlcpActivated(); 1582d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton return true; 1583d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1584d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } else { 1585d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton Log.w(TAG, "checkLlcp failed"); 1586d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1587d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1588d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton 1589d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton return false; 1590d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } 1591d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton 1592f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton private void dispatchTagEndpoint(TagEndpoint tagEndpoint, NdefMessage[] msgs) { 1593f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton Tag tag = new Tag(tagEndpoint.getUid(), tagEndpoint.getTechList(), 1594f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton tagEndpoint.getTechExtras(), tagEndpoint.getHandle(), mNfcTagService); 1595f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton registerTagObject(tagEndpoint); 159676a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly if (!mNfcDispatcher.dispatchTag(tag, msgs)) { 1597f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton unregisterObject(tagEndpoint.getHandle()); 1598d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton playSound(mErrorSound); 1599d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton } else { 1600d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton playSound(mEndSound); 16013fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton } 16023fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton } 1603b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton } 1604b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau 1605b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton private NfcServiceHandler mHandler = new NfcServiceHandler(); 160649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly 160731949217328bf2357ff044f0d18677fe588c790cNick Pelly class EnableDisableDiscoveryTask extends AsyncTask<Boolean, Void, Void> { 1608fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks @Override 1609275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks protected Void doInBackground(Boolean... params) { 1610275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks if (DBG) Log.d(TAG, "EnableDisableDiscoveryTask: enable = " + params[0]); 1611275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks 1612275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks if (params != null && params.length > 0 && params[0]) { 1613161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly synchronized (NfcService.this) { 161431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!mIsScreenUnlocked) { 161531949217328bf2357ff044f0d18677fe588c790cNick Pelly mIsScreenUnlocked = true; 1616275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks applyRouting(); 1617275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks } else { 1618275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks if (DBG) Log.d(TAG, "Ignoring enable request"); 1619275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks } 1620161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly } 16217c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly } else { 1622533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly mWakeLock.acquire(); 1623161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly synchronized (NfcService.this) { 162431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (mIsScreenUnlocked) { 162531949217328bf2357ff044f0d18677fe588c790cNick Pelly mIsScreenUnlocked = false; 1626e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton// applyRouting(); 1627e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton /* 1628e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * TODO undo this after the LLCP stack is fixed. 1629e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * This is done locally here since the LLCP stack is still using 1630e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * globals without holding any locks, and if we attempt to change 1631e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * the NFCEE routing while the target is still connected (and it's 1632e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * a P2P target) the async LLCP callbacks will crash since the routing 1633e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * manipulation code is overwriting globals it relies on. This hack should 1634e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton * be removed when the LLCP stack is fixed. 1635e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton */ 1636e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "NFC-C polling OFF"); 1637e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton mDeviceHost.disableDiscovery(); 1638275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks maybeDisconnectTarget(); 1639e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton if (mOpenEe == null) { 1640e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "NFC-EE routing OFF"); 1641e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton mDeviceHost.doDeselectSecureElement(); 1642e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton } else { 1643e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton Log.d(TAG, "Skipping request for NFC-EE routing OFF"); 1644e0b5fcb7e6422d1a788c48dd8f2936832ab8b397Jeff Hamilton } 1645275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks } else { 1646275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks if (DBG) Log.d(TAG, "Ignoring disable request"); 1647275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks } 1648161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly } 1649533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly mWakeLock.release(); 16507c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly } 16517c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly return null; 16527c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly } 16537c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly } 16547c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly 16550e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 16560e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly @Override 16570e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly public void onReceive(Context context, Intent intent) { 165831949217328bf2357ff044f0d18677fe588c790cNick Pelly String action = intent.getAction(); 165931949217328bf2357ff044f0d18677fe588c790cNick Pelly if (action.equals( 1660f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION)) { 1661ef92efa7fd86d75cc35465b84e8740f941ff0f0aJeff Hamilton if (DBG) Log.d(TAG, "INERNAL_TARGET_DESELECTED_ACTION"); 16620e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly 1663f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly /* Restart polling loop for notification */ 166449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly applyRouting(); 1665f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly 166631949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (action.equals(Intent.ACTION_SCREEN_ON)) { 1667275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks // Only enable if the screen is unlocked. If the screen is locked 1668275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks // Intent.ACTION_USER_PRESENT will be broadcast when the screen is 1669275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks // unlocked. 167036a1e42f1811692fb734bc5f015e294eab0bed4cJeff Hamilton boolean enable = !mKeyguard.isKeyguardLocked(); 1671275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks 16726ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly // Perform discovery enable in thread to protect against ANR when the 16736ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly // NFC stack wedges. This is *not* the correct way to fix this issue - 16746ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly // configuration of the local NFC adapter should be very quick and should 16756ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly // be safe on the main thread, and the NFC stack should not wedge. 1676275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks new EnableDisableDiscoveryTask().execute(enable); 167731949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { 16787c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly // Perform discovery disable in thread to protect against ANR when the 16797c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly // NFC stack wedges. This is *not* the correct way to fix this issue - 16807c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly // configuration of the local NFC adapter should be very quick and should 16817c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly // be safe on the main thread, and the NFC stack should not wedge. 1682275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks new EnableDisableDiscoveryTask().execute(Boolean.FALSE); 168331949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (action.equals(Intent.ACTION_USER_PRESENT)) { 1684275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks // The user has unlocked the screen. Enabled! 1685275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks new EnableDisableDiscoveryTask().execute(Boolean.TRUE); 168631949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (action.equals(ACTION_MASTER_CLEAR_NOTIFICATION)) { 168731949217328bf2357ff044f0d18677fe588c790cNick Pelly EnableDisableTask eeWipeTask = new EnableDisableTask(); 168831949217328bf2357ff044f0d18677fe588c790cNick Pelly eeWipeTask.execute(TASK_EE_WIPE); 168931949217328bf2357ff044f0d18677fe588c790cNick Pelly try { 169031949217328bf2357ff044f0d18677fe588c790cNick Pelly eeWipeTask.get(); // blocks until EE wipe is complete 169131949217328bf2357ff044f0d18677fe588c790cNick Pelly } catch (ExecutionException e) { 169231949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.w(TAG, "failed to wipe NFC-EE"); 169331949217328bf2357ff044f0d18677fe588c790cNick Pelly } catch (InterruptedException e) { 169431949217328bf2357ff044f0d18677fe588c790cNick Pelly Log.w(TAG, "failed to wipe NFC-EE"); 169531949217328bf2357ff044f0d18677fe588c790cNick Pelly } 169631949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) { 16977a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton boolean dataRemoved = intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false); 16987a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton if (dataRemoved) { 16997a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton Uri data = intent.getData(); 17007a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton if (data == null) return; 17017a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton String packageName = data.getSchemeSpecificPart(); 17027a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton 17037a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton synchronized (NfcService.this) { 170484e1e0adc2516afd35ebab029a52e764e0490559Jason parks if (mSePackages.contains(packageName)) { 170531949217328bf2357ff044f0d18677fe588c790cNick Pelly new EnableDisableTask().execute(TASK_EE_WIPE); 170684e1e0adc2516afd35ebab029a52e764e0490559Jason parks mSePackages.remove(packageName); 17077a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton } 1708bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 1709bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks } 171031949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) { 171131949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean isAirplaneModeOn = intent.getBooleanExtra("state", false); 171231949217328bf2357ff044f0d18677fe588c790cNick Pelly // Query the airplane mode from Settings.System just to make sure that 171331949217328bf2357ff044f0d18677fe588c790cNick Pelly // some random app is not sending this intent 171431949217328bf2357ff044f0d18677fe588c790cNick Pelly if (isAirplaneModeOn != isAirplaneModeOn()) { 171531949217328bf2357ff044f0d18677fe588c790cNick Pelly return; 171631949217328bf2357ff044f0d18677fe588c790cNick Pelly } 171731949217328bf2357ff044f0d18677fe588c790cNick Pelly if (!mIsAirplaneSensitive) { 171831949217328bf2357ff044f0d18677fe588c790cNick Pelly return; 171931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 172031949217328bf2357ff044f0d18677fe588c790cNick Pelly if (isAirplaneModeOn) { 172131949217328bf2357ff044f0d18677fe588c790cNick Pelly new EnableDisableTask().execute(TASK_DISABLE); 172231949217328bf2357ff044f0d18677fe588c790cNick Pelly } else if (!isAirplaneModeOn && mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT)) { 172331949217328bf2357ff044f0d18677fe588c790cNick Pelly new EnableDisableTask().execute(TASK_ENABLE); 172431949217328bf2357ff044f0d18677fe588c790cNick Pelly } 1725f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 1726f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly } 1727f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly }; 172831949217328bf2357ff044f0d18677fe588c790cNick Pelly 172931949217328bf2357ff044f0d18677fe588c790cNick Pelly /** Returns true if airplane mode is currently on */ 173031949217328bf2357ff044f0d18677fe588c790cNick Pelly boolean isAirplaneModeOn() { 173131949217328bf2357ff044f0d18677fe588c790cNick Pelly return Settings.System.getInt(mContext.getContentResolver(), 173231949217328bf2357ff044f0d18677fe588c790cNick Pelly Settings.System.AIRPLANE_MODE_ON, 0) == 1; 173331949217328bf2357ff044f0d18677fe588c790cNick Pelly } 173431949217328bf2357ff044f0d18677fe588c790cNick Pelly 173531949217328bf2357ff044f0d18677fe588c790cNick Pelly /** for debugging only - no il8n */ 173631949217328bf2357ff044f0d18677fe588c790cNick Pelly static String stateToString(int state) { 173731949217328bf2357ff044f0d18677fe588c790cNick Pelly switch (state) { 173831949217328bf2357ff044f0d18677fe588c790cNick Pelly case NfcAdapter.STATE_OFF: 173931949217328bf2357ff044f0d18677fe588c790cNick Pelly return "off"; 174031949217328bf2357ff044f0d18677fe588c790cNick Pelly case NfcAdapter.STATE_TURNING_ON: 174131949217328bf2357ff044f0d18677fe588c790cNick Pelly return "turning on"; 174231949217328bf2357ff044f0d18677fe588c790cNick Pelly case NfcAdapter.STATE_ON: 174331949217328bf2357ff044f0d18677fe588c790cNick Pelly return "on"; 174431949217328bf2357ff044f0d18677fe588c790cNick Pelly case NfcAdapter.STATE_TURNING_OFF: 174531949217328bf2357ff044f0d18677fe588c790cNick Pelly return "turning off"; 174631949217328bf2357ff044f0d18677fe588c790cNick Pelly default: 174731949217328bf2357ff044f0d18677fe588c790cNick Pelly return "<error>"; 174831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 174931949217328bf2357ff044f0d18677fe588c790cNick Pelly } 175031949217328bf2357ff044f0d18677fe588c790cNick Pelly 175131949217328bf2357ff044f0d18677fe588c790cNick Pelly void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 175250effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 175350effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root != PackageManager.PERMISSION_GRANTED) { 175450effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root pw.println("Permission Denial: can't dump nfc from from pid=" 175550effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() 175650effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root + " without permission " + android.Manifest.permission.DUMP); 175750effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root return; 175850effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root } 175950effe4645b6ea57a1dc90777995f41dd9624e55Kenny Root 176031949217328bf2357ff044f0d18677fe588c790cNick Pelly synchronized (this) { 176131949217328bf2357ff044f0d18677fe588c790cNick Pelly pw.println("mState=" + stateToString(mState)); 17620b3b8ab69835cb66c96691dae3ba9b75705980a5Nick Pelly pw.println("mIsZeroClickRequested=" + mIsNdefPushEnabled); 176331949217328bf2357ff044f0d18677fe588c790cNick Pelly pw.println("mIsScreenUnlocked=" + mIsScreenUnlocked); 176431949217328bf2357ff044f0d18677fe588c790cNick Pelly pw.println("mIsAirplaneSensitive=" + mIsAirplaneSensitive); 176531949217328bf2357ff044f0d18677fe588c790cNick Pelly pw.println("mIsAirplaneToggleable=" + mIsAirplaneToggleable); 176677d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly mP2pLinkManager.dump(fd, pw, args); 176731949217328bf2357ff044f0d18677fe588c790cNick Pelly } 176831949217328bf2357ff044f0d18677fe588c790cNick Pelly } 176974180bda362a8bc9d2f701d2c17bec0f63c20bbfBrad Fitzpatrick} 1770