nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | /****************************************************************************** |
| 17 | * |
| 18 | * The original Work has been changed by NXP Semiconductors. |
| 19 | * |
nxf35421 | aca166a | 2018-02-06 16:00:50 +0530 | [diff] [blame] | 20 | * Copyright (C) 2015-2018 NXP Semiconductors |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 | * you may not use this file except in compliance with the License. |
| 24 | * You may obtain a copy of the License at |
| 25 | * |
| 26 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | * |
| 28 | * Unless required by applicable law or agreed to in writing, software |
| 29 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 30 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | * See the License for the specific language governing permissions and |
| 32 | * limitations under the License. |
| 33 | * |
| 34 | ******************************************************************************/ |
| 35 | package com.android.nfc; |
| 36 | |
| 37 | import android.app.ActivityManager; |
| 38 | import android.app.Application; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 39 | import android.app.backup.BackupManager; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 40 | import android.app.KeyguardManager; |
| 41 | import android.app.PendingIntent; |
| 42 | import android.app.admin.DevicePolicyManager; |
| 43 | import android.content.BroadcastReceiver; |
| 44 | import android.content.ComponentName; |
| 45 | import android.content.ContentResolver; |
| 46 | import android.content.Context; |
| 47 | import android.content.Intent; |
| 48 | import android.content.IntentFilter; |
| 49 | import android.content.SharedPreferences; |
nxpandroid | 3649b76 | 2017-02-24 15:44:54 +0530 | [diff] [blame] | 50 | |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 51 | import android.content.pm.ApplicationInfo; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 52 | import android.content.pm.IPackageManager; |
| 53 | import android.content.pm.PackageInfo; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 54 | import android.content.pm.PackageManager; |
| 55 | import android.content.pm.UserInfo; |
| 56 | import android.content.res.Resources.NotFoundException; |
| 57 | import android.media.AudioManager; |
| 58 | import android.media.SoundPool; |
| 59 | import android.net.Uri; |
| 60 | import android.nfc.BeamShareData; |
| 61 | import android.nfc.ErrorCodes; |
| 62 | import android.nfc.FormatException; |
| 63 | import android.nfc.IAppCallback; |
| 64 | import android.nfc.INfcAdapter; |
| 65 | import android.nfc.INfcAdapterExtras; |
| 66 | import android.nfc.INfcCardEmulation; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 67 | import android.nfc.INfcFCardEmulation; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 68 | import android.nfc.INfcTag; |
| 69 | import android.nfc.INfcUnlockHandler; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 70 | import android.nfc.ITagRemovedCallback; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 71 | import android.nfc.NdefMessage; |
| 72 | import android.nfc.NfcAdapter; |
| 73 | import android.nfc.Tag; |
| 74 | import android.nfc.TechListParcel; |
| 75 | import android.nfc.TransceiveResult; |
| 76 | import android.nfc.tech.Ndef; |
| 77 | import android.nfc.tech.TagTechnology; |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 78 | import android.nfc.INfcDta; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 79 | import android.os.AsyncTask; |
| 80 | import android.os.Binder; |
| 81 | import android.os.Build; |
| 82 | import android.os.Bundle; |
| 83 | import android.os.Handler; |
Suhas Suresh | e75588d | 2018-07-11 15:22:24 +0530 | [diff] [blame] | 84 | import android.os.HwBinder; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 85 | import android.os.IBinder; |
| 86 | import android.os.Message; |
| 87 | import android.os.PowerManager; |
| 88 | import android.os.Process; |
| 89 | import android.os.RemoteException; |
| 90 | import android.os.ServiceManager; |
| 91 | import android.os.SystemClock; |
Nikhil Chhabra | a9e399a | 2018-01-09 11:47:13 +0530 | [diff] [blame] | 92 | import android.os.SystemProperties; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 93 | import android.os.UserHandle; |
| 94 | import android.os.UserManager; |
Suhas Suresh | 6e05ee0 | 2018-04-25 12:19:35 +0530 | [diff] [blame] | 95 | import android.os.VibrationEffect; |
| 96 | import android.os.Vibrator; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 97 | import android.provider.Settings; |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 98 | import android.se.omapi.ISecureElementService; |
Nikhil Chhabra | fc8f3f6 | 2018-01-08 20:48:49 +0530 | [diff] [blame] | 99 | import android.service.vr.IVrManager; |
| 100 | import android.service.vr.IVrStateCallbacks; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 101 | import android.util.Log; |
| 102 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 103 | import com.android.internal.logging.MetricsLogger; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 104 | import com.android.nfc.DeviceHost.DeviceHostListener; |
| 105 | import com.android.nfc.DeviceHost.LlcpConnectionlessSocket; |
| 106 | import com.android.nfc.DeviceHost.LlcpServerSocket; |
| 107 | import com.android.nfc.DeviceHost.LlcpSocket; |
| 108 | import com.android.nfc.DeviceHost.NfcDepEndpoint; |
| 109 | import com.android.nfc.DeviceHost.TagEndpoint; |
| 110 | |
| 111 | import com.android.nfc.dhimpl.NativeNfcSecureElement; |
| 112 | import com.android.nfc.dhimpl.NativeNfcAla; |
| 113 | import java.security.MessageDigest; |
| 114 | |
| 115 | import android.widget.Toast; |
| 116 | |
| 117 | import com.android.nfc.cardemulation.AidRoutingManager; |
| 118 | import com.android.nfc.cardemulation.CardEmulationManager; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 119 | import com.android.nfc.cardemulation.RegisteredAidCache; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 120 | import com.android.nfc.dhimpl.NativeNfcManager; |
| 121 | import com.android.nfc.handover.HandoverDataParser; |
| 122 | |
| 123 | import java.io.FileDescriptor; |
| 124 | import java.io.PrintWriter; |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 125 | import java.io.UnsupportedEncodingException; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 126 | import java.nio.ByteBuffer; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 127 | import java.util.concurrent.atomic.AtomicInteger; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 128 | import java.io.InputStream; |
| 129 | import java.io.OutputStream; |
| 130 | import java.io.FileInputStream; |
| 131 | import java.io.FileOutputStream; |
| 132 | import java.io.File; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 133 | import java.io.FileWriter; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 134 | import java.io.BufferedReader; |
| 135 | import java.io.FileReader; |
| 136 | import java.util.Arrays; |
| 137 | import java.util.ArrayList; |
| 138 | import java.util.HashMap; |
| 139 | import java.util.List; |
| 140 | import java.util.Map; |
| 141 | import java.util.NoSuchElementException; |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 142 | import java.util.TimerTask; |
| 143 | import java.util.Timer; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 144 | import java.io.IOException; |
| 145 | import java.io.FileNotFoundException; |
nxpandroid | 3649b76 | 2017-02-24 15:44:54 +0530 | [diff] [blame] | 146 | |
Ganesh Deva | f550e96 | 2018-07-24 11:59:09 +0530 | [diff] [blame] | 147 | import java.lang.reflect.Field; |
| 148 | import java.lang.reflect.Method; |
| 149 | import java.lang.reflect.InvocationTargetException; |
| 150 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 151 | import android.util.Pair; |
| 152 | import java.util.HashSet; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 153 | import java.util.concurrent.ExecutionException; |
| 154 | |
| 155 | import com.nxp.nfc.INxpNfcAdapter; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 156 | import com.nxp.nfc.INfcVzw; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 157 | import com.nxp.nfc.NxpConstants; |
| 158 | import com.vzw.nfc.RouteEntry; |
| 159 | import com.gsma.nfc.internal.NxpNfcController; |
| 160 | import com.nxp.nfc.gsma.internal.INxpNfcController; |
nxf26763 | be2bd68 | 2018-11-12 17:02:43 +0530 | [diff] [blame] | 161 | import com.nxp.nfc.NxpAidServiceInfo; |
Shashank vimal | 8377908 | 2018-02-06 18:10:31 +0530 | [diff] [blame] | 162 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 163 | public class NfcService implements DeviceHostListener { |
| 164 | private static final String ACTION_MASTER_CLEAR_NOTIFICATION = "android.intent.action.MASTER_CLEAR_NOTIFICATION"; |
| 165 | |
| 166 | static final boolean DBG = true; |
| 167 | static final String TAG = "NfcService"; |
| 168 | |
| 169 | public static final String SERVICE_NAME = "nfc"; |
| 170 | |
| 171 | /** Regular NFC permission */ |
| 172 | private static final String NFC_PERM = android.Manifest.permission.NFC; |
| 173 | private static final String NFC_PERM_ERROR = "NFC permission required"; |
| 174 | |
| 175 | public static final String PREF = "NfcServicePrefs"; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 176 | public static final String NXP_PREF = "NfcServiceNxpPrefs"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 177 | |
| 178 | static final String PREF_NFC_ON = "nfc_on"; |
| 179 | static final boolean NFC_ON_DEFAULT = true; |
| 180 | static final String PREF_NDEF_PUSH_ON = "ndef_push_on"; |
| 181 | static final boolean NDEF_PUSH_ON_DEFAULT = true; |
| 182 | static final String PREF_FIRST_BEAM = "first_beam"; |
| 183 | static final String PREF_FIRST_BOOT = "first_boot"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 184 | private static final String PREF_SECURE_ELEMENT_ON = "secure_element_on"; |
| 185 | private boolean SECURE_ELEMENT_ON_DEFAULT = false; |
| 186 | private int SECURE_ELEMENT_ID_DEFAULT = 0; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 187 | private int SECURE_ELEMENT_UICC_SLOT_DEFAULT = 1; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 188 | private static final String PREF_DEFAULT_ROUTE_ID = "default_route_id"; |
| 189 | private static final String PREF_MIFARE_DESFIRE_PROTO_ROUTE_ID = "mifare_desfire_proto_route"; |
| 190 | private static final String PREF_SET_DEFAULT_ROUTE_ID ="set_default_route"; |
| 191 | private static final String PREF_MIFARE_CLT_ROUTE_ID= "mifare_clt_route"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 192 | |
Suhas Suresh | 9150277 | 2018-08-31 20:28:13 +0530 | [diff] [blame] | 193 | private static final String[] path = {"/data/nfc/JcopOs_Update1.apdu", |
| 194 | "/data/nfc/JcopOs_Update2.apdu", |
| 195 | "/data/nfc/JcopOs_Update3.apdu"}; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 196 | |
| 197 | private static final String[] PREF_JCOP_MODTIME = {"jcop file1 modtime", |
| 198 | "jcop file2 modtime", |
| 199 | "jcop file3 modtime"}; |
| 200 | private static final long[] JCOP_MODTIME_DEFAULT = {-1,-1,-1}; |
| 201 | private static final long[] JCOP_MODTIME_TEMP = {-1,-1,-1}; |
| 202 | |
| 203 | private boolean ETSI_STOP_CONFIG = false; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 204 | private int ROUTE_ID_HOST = 0x00; |
| 205 | private int ROUTE_ID_SMX = 0x01; |
| 206 | private int ROUTE_ID_UICC = 0x02; |
| 207 | private int ROUTE_ID_UICC2 = 0x04; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 208 | |
| 209 | private int ROUTE_SWITCH_ON = 0x01; |
| 210 | private int ROUTE_SWITCH_OFF = 0x02; |
| 211 | private int ROUTE_BATT_OFF= 0x04; |
| 212 | |
| 213 | private int TECH_TYPE_A= 0x01; |
| 214 | private int TECH_TYPE_B= 0x02; |
| 215 | private int TECH_TYPE_F= 0x04; |
| 216 | |
| 217 | //TODO: Refer L_OSP_EXT [PN547C2] |
| 218 | // private int DEFAULT_ROUTE_ID_DEFAULT = AidRoutingManager.DEFAULT_ROUTE; |
| 219 | private int DEFAULT_ROUTE_ID_DEFAULT = 0x00; |
| 220 | static final boolean SE_BROADCASTS_WITH_HCE = true; |
| 221 | |
| 222 | private static final String PREF_SECURE_ELEMENT_ID = "secure_element_id"; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 223 | private static final String PREF_CUR_SELECTED_UICC_ID = "current_selected_uicc_id"; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 224 | public static final int ROUTE_LOC_MASK=8; |
| 225 | public static final int TECH_TYPE_MASK=11; |
| 226 | |
| 227 | static final String TRON_NFC_CE = "nfc_ce"; |
| 228 | static final String TRON_NFC_P2P = "nfc_p2p"; |
| 229 | static final String TRON_NFC_TAG = "nfc_tag"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 230 | |
| 231 | static final int MSG_NDEF_TAG = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 232 | static final int MSG_LLCP_LINK_ACTIVATION = 2; |
| 233 | static final int MSG_LLCP_LINK_DEACTIVATED = 3; |
| 234 | static final int MSG_TARGET_DESELECTED = 4; |
| 235 | static final int MSG_MOCK_NDEF = 7; |
| 236 | static final int MSG_SE_FIELD_ACTIVATED = 8; |
| 237 | static final int MSG_SE_FIELD_DEACTIVATED = 9; |
| 238 | static final int MSG_SE_APDU_RECEIVED = 10; |
| 239 | static final int MSG_SE_EMV_CARD_REMOVAL = 11; |
| 240 | static final int MSG_SE_MIFARE_ACCESS = 12; |
| 241 | static final int MSG_SE_LISTEN_ACTIVATED = 13; |
| 242 | static final int MSG_SE_LISTEN_DEACTIVATED = 14; |
| 243 | static final int MSG_LLCP_LINK_FIRST_PACKET = 15; |
| 244 | static final int MSG_ROUTE_AID = 16; |
| 245 | static final int MSG_UNROUTE_AID = 17; |
| 246 | static final int MSG_COMMIT_ROUTING = 18; |
| 247 | static final int MSG_INVOKE_BEAM = 19; |
| 248 | |
| 249 | static final int MSG_SWP_READER_REQUESTED = 20; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 250 | static final int MSG_SWP_READER_DEACTIVATED = 22; |
| 251 | static final int MSG_CLEAR_ROUTING = 23; |
| 252 | static final int MSG_SET_SCREEN_STATE = 25; |
| 253 | |
| 254 | |
| 255 | static final int MSG_RF_FIELD_ACTIVATED = 26; |
| 256 | static final int MSG_RF_FIELD_DEACTIVATED = 27; |
| 257 | static final int MSG_RESUME_POLLING = 28; |
| 258 | static final int MSG_SWP_READER_REQUESTED_FAIL =29 ; |
| 259 | static final int MSG_SWP_READER_TAG_PRESENT = 30; |
| 260 | static final int MSG_SWP_READER_TAG_REMOVE = 31; |
| 261 | static final int MSG_CONNECTIVITY_EVENT = 40; |
| 262 | static final int MSG_VZW_ROUTE_AID = 41; |
| 263 | static final int MSG_VZW_COMMIT_ROUTING = 42; |
| 264 | static final int MSG_ROUTE_NFCID2 = 43; |
| 265 | static final int MSG_UNROUTE_NFCID2 = 44; |
| 266 | static final int MSG_COMMITINF_FELICA_ROUTING = 45; |
| 267 | static final int MSG_COMMITED_FELICA_ROUTING = 46; |
| 268 | static final int MSG_EMVCO_MULTI_CARD_DETECTED_EVENT = 47; |
| 269 | static final int MSG_ETSI_START_CONFIG = 48; |
| 270 | static final int MSG_ETSI_STOP_CONFIG = 49; |
| 271 | static final int MSG_ETSI_SWP_TIMEOUT = 50; |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 272 | static final int MSG_APPLY_SCREEN_STATE = 51; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 273 | static final int MSG_REGISTER_T3T_IDENTIFIER = 54; |
| 274 | static final int MSG_DEREGISTER_T3T_IDENTIFIER = 55; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 275 | static final int MSG_TAG_DEBOUNCE = 56; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 276 | static final int MSG_UPDATE_STATS = 57; |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 277 | static final int MSG_SWP_READER_RESTART = 58; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 278 | /*Restart Nfc disbale watchdog timer*/ |
| 279 | static final int MSG_RESTART_WATCHDOG = 60; |
nxpandroid | a5fd662 | 2017-07-31 16:15:18 +0530 | [diff] [blame] | 280 | static final int MSG_ROUTE_APDU = 61; |
| 281 | static final int MSG_UNROUTE_APDU = 62; |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 282 | static final int MSG_TRANSACTION_EVENT = 63; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 283 | // Update stats every 4 hours |
| 284 | static final long STATS_UPDATE_INTERVAL_MS = 4 * 60 * 60 * 1000; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 285 | static final long MAX_POLLING_PAUSE_TIMEOUT = 40000; |
| 286 | static final int TASK_ENABLE = 1; |
| 287 | static final int TASK_DISABLE = 2; |
| 288 | static final int TASK_BOOT = 3; |
| 289 | static final int TASK_EE_WIPE = 4; |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 290 | static final int TASK_RESTART = 0x1F; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 291 | static final int MSG_CHANGE_DEFAULT_ROUTE = 52; |
| 292 | static final int MSG_SE_DELIVER_INTENT = 53; |
| 293 | |
| 294 | // Copied from com.android.nfc_extras to avoid library dependency |
| 295 | // Must keep in sync with com.android.nfc_extras |
| 296 | static final int ROUTE_OFF = 1; |
| 297 | static final int ROUTE_ON_WHEN_SCREEN_ON = 2; |
| 298 | |
| 299 | // Return values from NfcEe.open() - these are 1:1 mapped |
| 300 | // to the thrown EE_EXCEPTION_ exceptions in nfc-extras. |
| 301 | static final int EE_ERROR_IO = -1; |
| 302 | static final int EE_ERROR_ALREADY_OPEN = -2; |
| 303 | static final int EE_ERROR_INIT = -3; |
| 304 | static final int EE_ERROR_LISTEN_MODE = -4; |
| 305 | static final int EE_ERROR_EXT_FIELD = -5; |
| 306 | static final int EE_ERROR_NFC_DISABLED = -6; |
| 307 | |
| 308 | // Polling technology masks |
| 309 | static final int NFC_POLL_A = 0x01; |
| 310 | static final int NFC_POLL_B = 0x02; |
| 311 | static final int NFC_POLL_F = 0x04; |
Nikhil Chhabra | 288edb0 | 2018-01-10 19:36:21 +0530 | [diff] [blame] | 312 | static final int NFC_POLL_V = 0x08; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 313 | static final int NFC_POLL_B_PRIME = 0x10; |
| 314 | static final int NFC_POLL_KOVIO = 0x20; |
| 315 | |
| 316 | // minimum screen state that enables NFC polling |
| 317 | static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; |
| 318 | |
| 319 | // Time to wait for NFC controller to initialize before watchdog |
| 320 | // goes off. This time is chosen large, because firmware download |
| 321 | // may be a part of initialization. |
| 322 | static final int INIT_WATCHDOG_MS = 90000; |
| 323 | static final int INIT_WATCHDOG_LS_MS = 180000; |
| 324 | // Time to wait for routing to be applied before watchdog |
| 325 | // goes off |
| 326 | static final int ROUTING_WATCHDOG_MS = 10000; |
| 327 | |
| 328 | // Amount of time to wait before closing the NFCEE connection |
| 329 | // in a disable/shutdown scenario. |
| 330 | static final int WAIT_FOR_NFCEE_OPERATIONS_MS = 5000; |
| 331 | // Polling interval for waiting on NFCEE operations |
| 332 | static final int WAIT_FOR_NFCEE_POLL_MS = 100; |
| 333 | |
| 334 | // Default delay used for presence checks |
| 335 | static final int DEFAULT_PRESENCE_CHECK_DELAY = 125; |
| 336 | |
| 337 | //Delay used for presence checks of NFC_F non-Ndef |
| 338 | //Make secure communication done or tranceive next request response command |
| 339 | //to pause timer before presence check command is sent |
| 340 | static final int NFC_F_TRANSCEIVE_PRESENCE_CHECK_DELAY = 500; |
| 341 | |
| 342 | // The amount of time we wait before manually launching |
| 343 | // the Beam animation when called through the share menu. |
| 344 | static final int INVOKE_BEAM_DELAY_MS = 1000; |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 345 | // Default delay used for presence checks in ETSI mode |
| 346 | static final int ETSI_PRESENCE_CHECK_DELAY = 1000; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 347 | // for use with playSound() |
| 348 | public static final int SOUND_START = 0; |
| 349 | public static final int SOUND_END = 1; |
| 350 | public static final int SOUND_ERROR = 2; |
| 351 | |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 352 | public static final int NCI_VERSION_2_0 = 0x20; |
| 353 | |
| 354 | public static final int NCI_VERSION_1_0 = 0x10; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 355 | //ETSI Reader Events |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 356 | public static final int ETSI_READER_START_SUCCESS = 0; |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 357 | public static final int ETSI_READER_START_FAIL = 1; |
| 358 | public static final int ETSI_READER_ACTIVATED = 2; |
| 359 | public static final int ETSI_READER_STOP = 3; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 360 | |
| 361 | //ETSI Reader Req States |
| 362 | public static final int STATE_SE_RDR_MODE_INVALID = 0x00; |
| 363 | public static final int STATE_SE_RDR_MODE_START_CONFIG = 0x01; |
| 364 | public static final int STATE_SE_RDR_MODE_START_IN_PROGRESS = 0x02; |
| 365 | public static final int STATE_SE_RDR_MODE_STARTED = 0x03; |
| 366 | public static final int STATE_SE_RDR_MODE_ACTIVATED = 0x04; |
| 367 | public static final int STATE_SE_RDR_MODE_STOP_CONFIG = 0x05; |
| 368 | public static final int STATE_SE_RDR_MODE_STOP_IN_PROGRESS = 0x06; |
| 369 | public static final int STATE_SE_RDR_MODE_STOPPED = 0x07; |
| 370 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 371 | //Transit setconfig status |
| 372 | public static final int TRANSIT_SETCONFIG_STAT_SUCCESS = 0x00; |
| 373 | public static final int TRANSIT_SETCONFIG_STAT_FAILED = 0xFF; |
| 374 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 375 | public static final String ACTION_RF_FIELD_ON_DETECTED = |
| 376 | "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED"; |
| 377 | public static final String ACTION_RF_FIELD_OFF_DETECTED = |
| 378 | "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; |
| 379 | public static final String ACTION_AID_SELECTED = |
| 380 | "com.android.nfc_extras.action.AID_SELECTED"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 381 | |
| 382 | public static final String ACTION_LLCP_UP = |
| 383 | "com.android.nfc.action.LLCP_UP"; |
| 384 | |
| 385 | public static final String ACTION_LLCP_DOWN = |
| 386 | "com.android.nfc.action.LLCP_DOWN"; |
| 387 | |
| 388 | public static final String ACTION_APDU_RECEIVED = |
| 389 | "com.android.nfc_extras.action.APDU_RECEIVED"; |
| 390 | public static final String EXTRA_APDU_BYTES = |
| 391 | "com.android.nfc_extras.extra.APDU_BYTES"; |
| 392 | |
| 393 | public static final String ACTION_EMV_CARD_REMOVAL = |
| 394 | "com.android.nfc_extras.action.EMV_CARD_REMOVAL"; |
| 395 | |
| 396 | public static final String ACTION_MIFARE_ACCESS_DETECTED = |
| 397 | "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED"; |
| 398 | public static final String EXTRA_MIFARE_BLOCK = |
| 399 | "com.android.nfc_extras.extra.MIFARE_BLOCK"; |
| 400 | |
| 401 | public static final String ACTION_SE_LISTEN_ACTIVATED = |
| 402 | "com.android.nfc_extras.action.SE_LISTEN_ACTIVATED"; |
| 403 | public static final String ACTION_SE_LISTEN_DEACTIVATED = |
| 404 | "com.android.nfc_extras.action.SE_LISTEN_DEACTIVATED"; |
| 405 | |
| 406 | public static final String ACTION_EMVCO_MULTIPLE_CARD_DETECTED = |
| 407 | "com.nxp.action.EMVCO_MULTIPLE_CARD_DETECTED"; |
| 408 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 409 | public static final String ACTION_UICC_STATUS_RECEIVED = |
| 410 | "com.nxp.action.UICC_STATUS_RECEIVED"; |
| 411 | |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 412 | public static final String ACTION_FLASH_SUCCESS = |
| 413 | "com.android.nfc_extras.action.ACTION_FLASH_SUCCESS"; |
| 414 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 415 | public static final String EXTRA_UICC_STATUS = "com.nxp.extra.UICC_STATUS"; |
| 416 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 417 | private static final String PACKAGE_SMART_CARD_SERVICE = "org.simalliance.openmobileapi.service"; |
| 418 | /** |
| 419 | * SMART MX ID to be able to select it as the default Secure Element |
| 420 | */ |
| 421 | public static final int SMART_MX_ID_TYPE = 1; |
| 422 | |
| 423 | /** |
| 424 | * UICC ID to be able to select it as the default Secure Element |
| 425 | */ |
| 426 | public static final int UICC_ID_TYPE = 2; |
| 427 | |
| 428 | /** |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 429 | * UICC2 ID to be able to select it as the default Secure Element |
| 430 | */ |
| 431 | public static final int UICC2_ID_TYPE = 4; |
| 432 | /** |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 433 | * ID to be able to select all Secure Elements |
| 434 | */ |
nxpandroid | 0232af2 | 2017-07-12 21:40:33 +0530 | [diff] [blame] | 435 | private static int ALL_SE_ID_TYPE = 7; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 436 | |
| 437 | public static final int PN547C2_ID = 1; |
| 438 | public static final int PN65T_ID = 2; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 439 | public static final int PN548C2_ID = 3; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 440 | public static final int PN66T_ID = 4; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 441 | public static final int PN551_ID = 5; |
| 442 | public static final int PN67T_ID = 6; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 443 | public static final int PN553_ID = 7; |
| 444 | public static final int PN80T_ID = 8; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 445 | |
| 446 | public static final int LS_RETRY_CNT = 3; |
nxpandroid | 5d3fdf8 | 2017-07-31 16:11:33 +0530 | [diff] [blame] | 447 | public static final int LOADER_SERVICE_VERSION_LOW_LIMIT = 0x21; |
| 448 | public static final int LOADER_SERVICE_VERSION_HIGH_LIMIT = 0x24; |
| 449 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 450 | private int mSelectedSeId = 0; |
| 451 | private boolean mNfcSecureElementState; |
| 452 | private boolean mIsSmartCardServiceSupported = false; |
| 453 | // Timeout to re-apply routing if a tag was present and we postponed it |
| 454 | private static final int APPLY_ROUTING_RETRY_TIMEOUT_MS = 5000; |
| 455 | |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 456 | // these states are for making enable and disable nfc atomic |
| 457 | private int NXP_NFC_STATE_OFF = 0; |
| 458 | private int NXP_NFC_STATE_TURNING_ON = 1; |
| 459 | private int NXP_NFC_STATE_ON = 2; |
| 460 | private int NXP_NFC_STATE_TURNING_OFF = 3; |
| 461 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 462 | private final UserManager mUserManager; |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 463 | private static int nci_version = NCI_VERSION_1_0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 464 | // NFC Execution Environment |
| 465 | // fields below are protected by this |
Ganesh Deva | f550e96 | 2018-07-24 11:59:09 +0530 | [diff] [blame] | 466 | public NativeNfcSecureElement mSecureElement; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 467 | private OpenSecureElement mOpenEe; // null when EE closed |
| 468 | private final ReaderModeDeathRecipient mReaderModeDeathRecipient = |
| 469 | new ReaderModeDeathRecipient(); |
| 470 | private final NfcUnlockManager mNfcUnlockManager; |
| 471 | |
| 472 | private int mEeRoutingState; // contactless interface routing |
| 473 | private int mLockscreenPollMask; |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 474 | // cached version of installed packages requesting Android.permission.NFC_TRANSACTION_EVENTS |
Suhas Suresh | 140f7ac | 2018-05-15 14:59:11 +0530 | [diff] [blame] | 475 | List<String> mNfcEventInstalledPackages = new ArrayList<String>(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 476 | private NativeNfcAla mNfcAla; |
| 477 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 478 | private final BackupManager mBackupManager; |
| 479 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 480 | // fields below are used in multiple threads and protected by synchronized(this) |
| 481 | final HashMap<Integer, Object> mObjectMap = new HashMap<Integer, Object>(); |
| 482 | // mSePackages holds packages that accessed the SE, but only for the owner user, |
| 483 | // as SE access is not granted for non-owner users. |
| 484 | HashSet<String> mSePackages = new HashSet<String>(); |
| 485 | int mScreenState; |
nxf32288 | d12785b | 2017-11-17 15:18:31 +0530 | [diff] [blame] | 486 | int mChipVer; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 487 | boolean mIsTaskBoot = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 488 | boolean mInProvisionMode; // whether we're in setup wizard and enabled NFC provisioning |
| 489 | boolean mIsNdefPushEnabled; |
| 490 | boolean mNfcPollingEnabled; // current Device Host state of NFC-C polling |
| 491 | boolean mHostRouteEnabled; // current Device Host state of host-based routing |
| 492 | boolean mReaderModeEnabled; // current Device Host state of reader mode |
| 493 | boolean mNfceeRouteEnabled; // current Device Host state of NFC-EE routing |
| 494 | NfcDiscoveryParameters mCurrentDiscoveryParameters = |
| 495 | NfcDiscoveryParameters.getNfcOffParameters(); |
| 496 | ReaderModeParams mReaderModeParams; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 497 | private int mUserId; |
| 498 | boolean mPollingPaused; |
| 499 | |
Nikhil Chhabra | 1b2368f | 2018-01-09 18:34:21 +0530 | [diff] [blame] | 500 | static final int INVALID_NATIVE_HANDLE = -1; |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 501 | static final int SE_ACCESS_DENIED = -2; |
nxpandroid | 623a363 | 2017-04-10 18:27:16 +0530 | [diff] [blame] | 502 | byte[] mDebounceTagUid; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 503 | int mDebounceTagDebounceMs; |
Nikhil Chhabra | 1b2368f | 2018-01-09 18:34:21 +0530 | [diff] [blame] | 504 | int mDebounceTagNativeHandle = INVALID_NATIVE_HANDLE; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 505 | ITagRemovedCallback mDebounceTagRemovedCallback; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 506 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 507 | // Only accessed on one thread so doesn't need locking |
| 508 | NdefMessage mLastReadNdefMessage; |
| 509 | |
| 510 | // Metrics |
| 511 | AtomicInteger mNumTagsDetected; |
| 512 | AtomicInteger mNumP2pDetected; |
| 513 | AtomicInteger mNumHceDetected; |
| 514 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 515 | // mState is protected by this, however it is only modified in onCreate() |
| 516 | // and the default AsyncTask thread so it is read unprotected from that |
| 517 | // thread |
| 518 | int mState; // one of NfcAdapter.STATE_ON, STATE_TURNING_ON, etc |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 519 | int mNxpNfcState = NXP_NFC_STATE_OFF; |
| 520 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 521 | boolean mPowerShutDown = false; // State for power shut down state |
| 522 | |
| 523 | // fields below are final after onCreate() |
| 524 | Context mContext; |
| 525 | private DeviceHost mDeviceHost; |
| 526 | private SharedPreferences mPrefs; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 527 | private SharedPreferences mNxpPrefs; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 528 | private SharedPreferences.Editor mPrefsEditor; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 529 | private SharedPreferences.Editor mNxpPrefsEditor; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 530 | private PowerManager.WakeLock mRoutingWakeLock; |
| 531 | private PowerManager.WakeLock mEeWakeLock; |
| 532 | |
| 533 | int mStartSound; |
| 534 | int mEndSound; |
| 535 | int mErrorSound; |
| 536 | SoundPool mSoundPool; // playback synchronized on this |
| 537 | P2pLinkManager mP2pLinkManager; |
| 538 | TagService mNfcTagService; |
| 539 | NfcAdapterService mNfcAdapter; |
| 540 | NfcAdapterExtrasService mExtrasService; |
| 541 | // CardEmulationService mCardEmulationService; |
| 542 | NxpNfcAdapterService mNxpNfcAdapter; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 543 | boolean mIsDebugBuild; |
| 544 | boolean mIsHceCapable; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 545 | boolean mIsHceFCapable; |
Pratap Reddy | 8f473f0 | 2018-04-23 15:18:30 +0530 | [diff] [blame] | 546 | public boolean mIsRoutingTableDirty; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 547 | boolean mIsFelicaOnHostConfigured; |
| 548 | boolean mIsFelicaOnHostConfiguring; |
| 549 | |
| 550 | public boolean mIsRouteForced; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 551 | NfcSccAccessControl mNfcSccAccessControl; |
| 552 | NfcSeAccessControl mNfcSeAccessControl; |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 553 | NfcDtaService mNfcDtaService; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 554 | NfcVzwService mVzwService; |
| 555 | private NfcDispatcher mNfcDispatcher; |
| 556 | private PowerManager mPowerManager; |
| 557 | private KeyguardManager mKeyguard; |
| 558 | ToastHandler mToastHandler; |
| 559 | private HandoverDataParser mHandoverDataParser; |
| 560 | private ContentResolver mContentResolver; |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 561 | private RegisteredAidCache mAidCache; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 562 | private CardEmulationManager mCardEmulationManager; |
Suhas Suresh | 6e05ee0 | 2018-04-25 12:19:35 +0530 | [diff] [blame] | 563 | private Vibrator mVibrator; |
| 564 | private VibrationEffect mVibrationEffect; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 565 | private AidRoutingManager mAidRoutingManager; |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 566 | private ISecureElementService mSEService; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 567 | private ScreenStateHelper mScreenStateHelper; |
| 568 | private ForegroundUtils mForegroundUtils; |
| 569 | private boolean mClearNextTapDefault; |
| 570 | private NxpNfcController mNxpNfcController; |
| 571 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 572 | private static NfcService sService; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 573 | public static boolean sIsDtaMode = false; |
| 574 | public static boolean sIsShortRecordLayout = false; |
| 575 | public static boolean sAidTableFull = false; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 576 | private WatchDogThread disableInternalwatchDog; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 577 | |
| 578 | //GSMA |
| 579 | private final Boolean defaultTransactionEventReceptionMode = Boolean.FALSE; |
nxpandroid | 623a363 | 2017-04-10 18:27:16 +0530 | [diff] [blame] | 580 | private static final Boolean multiReceptionMode = Boolean.TRUE; |
| 581 | private static final Boolean unicastReceptionMode = Boolean.FALSE; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 582 | boolean mIsSentUnicastReception = false; |
| 583 | |
Ganesh Deva | f550e96 | 2018-07-24 11:59:09 +0530 | [diff] [blame] | 584 | //WiredSe |
| 585 | Class mWiredSeClass; |
| 586 | Method mWiredSeInitMethod, mWiredSeDeInitMwthod; |
| 587 | Object mWiredSeObj; |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 588 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 589 | public void enforceNfcSeAdminPerm(String pkg) { |
| 590 | if (pkg == null) { |
| 591 | throw new SecurityException("caller must pass a package name"); |
| 592 | } |
| 593 | mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); |
| 594 | if (!mNfcSeAccessControl.check(Binder.getCallingUid(), pkg)) { |
| 595 | throw new SecurityException(NfcSeAccessControl.NFCSE_ACCESS_PATH + |
| 596 | " denies NFCSe access to " + pkg); |
| 597 | } |
| 598 | if (UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
| 599 | throw new SecurityException("only the owner is allowed to act as SCC admin"); |
| 600 | } |
| 601 | } |
| 602 | public void enforceNfceeAdminPerm(String pkg) { |
| 603 | if (pkg == null) { |
| 604 | throw new SecurityException("caller must pass a package name"); |
| 605 | } |
| 606 | NfcPermissions.enforceUserPermissions(mContext); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 607 | if (UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
| 608 | throw new SecurityException("only the owner is allowed to call SE APIs"); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /* SCC Access Control */ |
| 614 | public void enforceNfcSccAdminPerm(String pkg) { |
| 615 | if (pkg == null) { |
| 616 | throw new SecurityException("caller must pass a package name"); |
| 617 | } |
| 618 | mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); |
| 619 | if (!mNfcSccAccessControl.check(Binder.getCallingUid(), pkg)) { |
| 620 | throw new SecurityException(NfcSccAccessControl.NFCSCC_ACCESS_PATH + |
| 621 | " denies NFCSCC access to " + pkg); |
| 622 | } |
| 623 | if (UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
| 624 | throw new SecurityException("only the owner is allowed to act as SCC admin"); |
| 625 | } |
| 626 | } |
| 627 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 628 | boolean mIsLiveCaseEnabled; // whether live cases are enabled |
| 629 | int mLiveCaseTechnology; // Technology mask of accepted NFC tags |
| 630 | |
Nikhil Chhabra | fc8f3f6 | 2018-01-08 20:48:49 +0530 | [diff] [blame] | 631 | private IVrManager vrManager; |
| 632 | boolean mIsVrModeEnabled; |
| 633 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 634 | public static NfcService getInstance() { |
| 635 | return sService; |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public void onRemoteEndpointDiscovered(TagEndpoint tag) { |
| 640 | sendMessage(NfcService.MSG_NDEF_TAG, tag); |
| 641 | } |
| 642 | |
| 643 | public int getRemainingAidTableSize() { |
| 644 | return mDeviceHost.getRemainingAidTableSize(); |
| 645 | } |
| 646 | |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 647 | public boolean getLastCommitRoutingStatus() { |
| 648 | return mAidRoutingManager.getLastCommitRoutingStatus(); |
| 649 | } |
| 650 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 651 | public int getChipVer() { |
| 652 | return mDeviceHost.getChipVer(); |
| 653 | } |
nxf38293 | 4d174fd | 2018-06-29 13:02:43 +0530 | [diff] [blame] | 654 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 655 | /** |
| 656 | * Notifies Card emulation deselect |
| 657 | */ |
| 658 | @Override |
| 659 | public void onCardEmulationDeselected() { |
| 660 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 661 | sendMessage(NfcService.MSG_TARGET_DESELECTED, null); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /** |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 666 | * Notifies connectivity |
| 667 | */ |
| 668 | @Override |
| 669 | public void onConnectivityEvent(int evtSrc) { |
| 670 | Log.d(TAG, "onConnectivityEvent : Source" + evtSrc); |
| 671 | sendMessage(NfcService.MSG_CONNECTIVITY_EVENT, evtSrc); |
| 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public void onEmvcoMultiCardDetectedEvent() { |
| 676 | Log.d(TAG, "onEmvcoMultiCardDetectedEvent"); |
| 677 | sendMessage(NfcService.MSG_EMVCO_MULTI_CARD_DETECTED_EVENT,null); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Notifies transaction |
| 682 | */ |
| 683 | @Override |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 684 | public void onHostCardEmulationActivated(int technology) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 685 | if (mCardEmulationManager != null) { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 686 | mCardEmulationManager.onHostCardEmulationActivated(technology); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
| 690 | @Override |
| 691 | public void onAidRoutingTableFull() { |
| 692 | Log.d(TAG, "NxpNci: onAidRoutingTableFull: AID Routing Table is FULL!"); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 693 | /*if((ROUTE_ID_HOST != GetDefaultRouteLoc())&&(sAidTableFull == false)) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 694 | { |
| 695 | Log.d(TAG, "NxpNci: onAidRoutingTableFull: Making Default Route to HOST!"); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 696 | sAidTableFull = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 697 | mHandler.sendEmptyMessage(NfcService.MSG_CHANGE_DEFAULT_ROUTE); |
| 698 | }*/ |
| 699 | if (mIsHceCapable) { |
| 700 | mAidRoutingManager.onNfccRoutingTableCleared(); |
| 701 | mCardEmulationManager.onRoutingTableChanged(); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | @Override |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 706 | public void onNotifyT3tConfigure() { |
| 707 | if (mCardEmulationManager != null) { |
| 708 | mCardEmulationManager.onT3tConfigure(); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | @Override |
| 713 | public void onNotifyReRoutingEntry() { |
| 714 | if (mCardEmulationManager != null) { |
| 715 | mCardEmulationManager.onReRoutingEntry(); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | @Override |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 720 | public void onHostCardEmulationData(int technology, byte[] data) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 721 | if (mCardEmulationManager != null) { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 722 | mCardEmulationManager.onHostCardEmulationData(technology, data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | @Override |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 727 | public void onHostCardEmulationDeactivated(int technology) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 728 | if (mCardEmulationManager != null) { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 729 | // Do metrics here so we don't slow the CE path down |
| 730 | mNumHceDetected.incrementAndGet(); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 731 | mCardEmulationManager.onHostCardEmulationDeactivated(technology); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Notifies P2P Device detected, to activate LLCP link |
| 737 | */ |
| 738 | @Override |
| 739 | public void onLlcpLinkActivated(NfcDepEndpoint device) { |
| 740 | sendMessage(NfcService.MSG_LLCP_LINK_ACTIVATION, device); |
| 741 | } |
| 742 | |
| 743 | /** |
| 744 | * Notifies P2P Device detected, to activate LLCP link |
| 745 | */ |
| 746 | @Override |
| 747 | public void onLlcpLinkDeactivated(NfcDepEndpoint device) { |
| 748 | sendMessage(NfcService.MSG_LLCP_LINK_DEACTIVATED, device); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Notifies P2P Device detected, first packet received over LLCP link |
| 753 | */ |
| 754 | @Override |
| 755 | public void onLlcpFirstPacketReceived(NfcDepEndpoint device) { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 756 | mNumP2pDetected.incrementAndGet(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 757 | sendMessage(NfcService.MSG_LLCP_LINK_FIRST_PACKET, device); |
| 758 | } |
| 759 | |
| 760 | @Override |
| 761 | public void onRemoteFieldActivated() { |
| 762 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 763 | sendMessage(NfcService.MSG_SE_FIELD_ACTIVATED, null); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | @Override |
| 768 | public void onRemoteFieldDeactivated() { |
| 769 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 770 | sendMessage(NfcService.MSG_SE_FIELD_DEACTIVATED, null); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public void onSeListenActivated() { |
| 776 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 777 | sendMessage(NfcService.MSG_SE_LISTEN_ACTIVATED, null); |
| 778 | } |
| 779 | if (mIsHceCapable) { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 780 | mCardEmulationManager.onHostCardEmulationActivated(TagTechnology.NFC_A); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public void onSeListenDeactivated() { |
| 786 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 787 | sendMessage(NfcService.MSG_SE_LISTEN_DEACTIVATED, null); |
| 788 | } |
| 789 | if( mIsHceCapable) { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 790 | mCardEmulationManager.onHostCardEmulationDeactivated(TagTechnology.NFC_A); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 794 | @Override |
| 795 | public void onSeApduReceived(byte[] apdu) { |
| 796 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 797 | sendMessage(NfcService.MSG_SE_APDU_RECEIVED, apdu); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | @Override |
| 802 | public void onSeEmvCardRemoval() { |
| 803 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 804 | sendMessage(NfcService.MSG_SE_EMV_CARD_REMOVAL, null); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | @Override |
| 809 | public void onSeMifareAccess(byte[] block) { |
| 810 | if (!mIsHceCapable || SE_BROADCASTS_WITH_HCE) { |
| 811 | sendMessage(NfcService.MSG_SE_MIFARE_ACCESS, block); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | @Override |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 816 | public void onETSIReaderRequestedEvent(boolean istechA, boolean istechB) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 817 | { |
| 818 | int size=0; |
| 819 | ArrayList<Integer> techList = new ArrayList<Integer>(); |
| 820 | if(istechA) |
| 821 | techList.add(TagTechnology.NFC_A); |
| 822 | if(istechB) |
| 823 | techList.add(TagTechnology.NFC_B); |
| 824 | |
| 825 | sendMessage(NfcService.MSG_SWP_READER_REQUESTED , techList); |
| 826 | } |
| 827 | |
| 828 | @Override |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 829 | public void onETSIReaderRequestedFail(int FailCause) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 830 | { |
| 831 | sendMessage(NfcService.MSG_SWP_READER_REQUESTED_FAIL , FailCause); |
| 832 | } |
| 833 | |
| 834 | @Override |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 835 | public void onETSIReaderModeStartConfig(int eeHandle) |
| 836 | { |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 837 | // Check if NFC is enabled |
| 838 | if (!isNfcEnabled()) { |
| 839 | return; |
| 840 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 841 | ArrayList<Integer> configList = new ArrayList<Integer>(); |
| 842 | configList.add(eeHandle); |
| 843 | sendMessage(NfcService.MSG_ETSI_START_CONFIG, configList); |
| 844 | } |
| 845 | |
| 846 | @Override |
| 847 | public void onETSIReaderModeStopConfig(int disc_ntf_timeout) |
| 848 | { |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 849 | // Check if NFC is enabled |
| 850 | if (!isNfcEnabled()) { |
| 851 | return; |
| 852 | } |
| 853 | EtsiStopConfigTask task = new EtsiStopConfigTask(); |
| 854 | task.execute(disc_ntf_timeout); |
| 855 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | @Override |
| 859 | public void onETSIReaderModeSwpTimeout(int disc_ntf_timeout) |
| 860 | { |
| 861 | sendMessage(NfcService.MSG_ETSI_SWP_TIMEOUT, disc_ntf_timeout); |
| 862 | } |
| 863 | |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 864 | @Override |
| 865 | public void onETSIReaderModeRestart() { |
| 866 | sendMessage(NfcService.MSG_SWP_READER_RESTART, null); |
| 867 | } |
| 868 | |
| 869 | @Override |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 870 | public void onUiccStatusEvent(int uiccStat) |
| 871 | { |
| 872 | Log.i(TAG, "Broadcasting UICC Status : " + uiccStat); |
| 873 | Intent uiccStatusIntent = new Intent(); |
| 874 | uiccStatusIntent.setAction(ACTION_UICC_STATUS_RECEIVED); |
| 875 | uiccStatusIntent.putExtra(EXTRA_UICC_STATUS, uiccStat); |
| 876 | mContext.sendBroadcast(uiccStatusIntent); |
| 877 | } |
| 878 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 879 | @Override |
| 880 | public void onRestartWatchDog(int enable) { |
| 881 | Log.d(TAG, "Restart Watchdog: WatchDog Thread ID is "+ disableInternalwatchDog.getId()); |
| 882 | sendMessage(NfcService.MSG_RESTART_WATCHDOG, enable); |
| 883 | } |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 884 | @Override |
| 885 | public void onFwDwnldReqRestartNfc() { |
| 886 | Log.d(TAG, "Restart NFC:When Fw dwnld request was stored during SPI onGoing"); |
| 887 | new EnableDisableTask().execute(TASK_RESTART); |
| 888 | } |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 889 | |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 890 | @Override |
| 891 | public void onNfcTransactionEvent(byte[] aid, byte[] data, String seName) { |
| 892 | byte[][] dataObj = {aid, data, seName.getBytes()}; |
| 893 | sendMessage(NfcService.MSG_TRANSACTION_EVENT, dataObj); |
| 894 | } |
| 895 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 896 | final class ReaderModeParams { |
| 897 | public int flags; |
| 898 | public IAppCallback callback; |
| 899 | public int presenceCheckDelay; |
| 900 | } |
| 901 | |
| 902 | public NfcService(Application nfcApplication) { |
| 903 | mUserId = ActivityManager.getCurrentUser(); |
| 904 | mContext = nfcApplication; |
| 905 | |
| 906 | mNfcTagService = new TagService(); |
| 907 | mNfcAdapter = new NfcAdapterService(); |
| 908 | mNxpNfcAdapter = new NxpNfcAdapterService(); |
| 909 | mExtrasService = new NfcAdapterExtrasService(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 910 | // mCardEmulationService = new CardEmulationService(); |
| 911 | |
Ganesh Deva | f550e96 | 2018-07-24 11:59:09 +0530 | [diff] [blame] | 912 | try { |
| 913 | mWiredSeClass = Class.forName("com.android.nfc.WiredSeService"); |
| 914 | mWiredSeObj = mWiredSeClass.newInstance(); |
| 915 | } catch (ClassNotFoundException | IllegalAccessException e){ |
| 916 | Log.e(TAG, "WiredSeService Class not found"); |
| 917 | } catch (InstantiationException e) { |
| 918 | Log.e(TAG, "WiredSeService object Instantiation failed"); |
| 919 | } |
| 920 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 921 | Log.i(TAG, "Starting NFC service"); |
| 922 | |
| 923 | sService = this; |
| 924 | |
| 925 | mScreenStateHelper = new ScreenStateHelper(mContext); |
| 926 | mContentResolver = mContext.getContentResolver(); |
| 927 | mDeviceHost = new NativeNfcManager(mContext, this); |
| 928 | |
| 929 | mNfcUnlockManager = NfcUnlockManager.getInstance(); |
| 930 | |
| 931 | mHandoverDataParser = new HandoverDataParser(); |
| 932 | boolean isNfcProvisioningEnabled = false; |
| 933 | try { |
| 934 | isNfcProvisioningEnabled = mContext.getResources().getBoolean( |
| 935 | R.bool.enable_nfc_provisioning); |
| 936 | } catch (NotFoundException e) { |
| 937 | } |
| 938 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 939 | try { |
| 940 | mIsLiveCaseEnabled = mContext.getResources().getBoolean(R.bool.enable_live_cases); |
| 941 | } catch (NotFoundException e) { |
| 942 | mIsLiveCaseEnabled = false; |
| 943 | } |
| 944 | |
| 945 | mLiveCaseTechnology = 0; |
| 946 | String[] liveCaseTechList; |
| 947 | try { |
| 948 | liveCaseTechList = mContext.getResources().getStringArray(R.array.live_case_tag_types); |
| 949 | for (int i=0; i < liveCaseTechList.length; i++) { |
| 950 | if (liveCaseTechList[i].equals("TypeA")) { |
| 951 | mLiveCaseTechnology |= NFC_POLL_A; |
| 952 | } else if (liveCaseTechList[i].equals("TypeB")) { |
| 953 | mLiveCaseTechnology |= NFC_POLL_B; |
| 954 | } else if (liveCaseTechList[i].equals("TypeF")) { |
| 955 | mLiveCaseTechnology |= NFC_POLL_F; |
| 956 | } else if (liveCaseTechList[i].equals("TypeV")) { |
Nikhil Chhabra | 288edb0 | 2018-01-10 19:36:21 +0530 | [diff] [blame] | 957 | mLiveCaseTechnology |= NFC_POLL_V; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } catch (NotFoundException e) { |
| 961 | mLiveCaseTechnology = 0; |
| 962 | } |
| 963 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 964 | if (isNfcProvisioningEnabled) { |
| 965 | mInProvisionMode = Settings.Secure.getInt(mContentResolver, |
| 966 | Settings.Global.DEVICE_PROVISIONED, 0) == 0; |
| 967 | } else { |
| 968 | mInProvisionMode = false; |
| 969 | } |
| 970 | |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 971 | if(mInProvisionMode) |
| 972 | { |
| 973 | /* if device is in provision mode, set this mode at lower layers */ |
| 974 | mDeviceHost.doSetProvisionMode(mInProvisionMode); |
| 975 | } |
| 976 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 977 | mNfcDispatcher = new NfcDispatcher(mContext, mHandoverDataParser, mInProvisionMode, |
| 978 | mIsLiveCaseEnabled); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 979 | mP2pLinkManager = new P2pLinkManager(mContext, mHandoverDataParser, |
| 980 | mDeviceHost.getDefaultLlcpMiu(), mDeviceHost.getDefaultLlcpRwSize()); |
| 981 | |
| 982 | mSecureElement = new NativeNfcSecureElement(mContext); |
| 983 | mEeRoutingState = ROUTE_OFF; |
| 984 | mToastHandler = new ToastHandler(mContext); |
| 985 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 986 | mNfcSccAccessControl = new NfcSccAccessControl(mContext); |
| 987 | mNfcSeAccessControl = new NfcSeAccessControl(mContext); |
| 988 | mNfcAla = new NativeNfcAla(); |
| 989 | |
| 990 | mPrefs = mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE); |
| 991 | mPrefsEditor = mPrefs.edit(); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 992 | mNxpPrefs = mContext.getSharedPreferences(NXP_PREF, Context.MODE_PRIVATE); |
| 993 | mNxpPrefsEditor = mNxpPrefs.edit(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 994 | |
| 995 | mState = NfcAdapter.STATE_OFF; |
| 996 | mIsNdefPushEnabled = mPrefs.getBoolean(PREF_NDEF_PUSH_ON, NDEF_PUSH_ON_DEFAULT); |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 997 | enforceBeamShareActivityPolicy(mContext, new UserHandle(mUserId)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 998 | |
| 999 | mIsDebugBuild = "userdebug".equals(Build.TYPE) || "eng".equals(Build.TYPE); |
| 1000 | |
| 1001 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 1002 | |
| 1003 | mRoutingWakeLock = mPowerManager.newWakeLock( |
| 1004 | PowerManager.PARTIAL_WAKE_LOCK, "NfcService:mRoutingWakeLock"); |
| 1005 | mEeWakeLock = mPowerManager.newWakeLock( |
| 1006 | PowerManager.PARTIAL_WAKE_LOCK, "NfcService:mEeWakeLock"); |
| 1007 | |
| 1008 | mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 1009 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Suhas Suresh | 6e05ee0 | 2018-04-25 12:19:35 +0530 | [diff] [blame] | 1010 | mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); |
| 1011 | mVibrationEffect = VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1012 | |
| 1013 | mScreenState = mScreenStateHelper.checkScreenState(); |
| 1014 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1015 | mNumTagsDetected = new AtomicInteger(); |
| 1016 | mNumP2pDetected = new AtomicInteger(); |
| 1017 | mNumHceDetected = new AtomicInteger(); |
| 1018 | |
| 1019 | mBackupManager = new BackupManager(mContext); |
| 1020 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1021 | // Intents for all users |
| 1022 | IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION); |
| 1023 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1024 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1025 | filter.addAction(Intent.ACTION_USER_PRESENT); |
| 1026 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1027 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); |
| 1028 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1029 | filter = new IntentFilter(Intent.ACTION_SHUTDOWN); |
| 1030 | mContext.registerReceiver(mOwnerReceiver, filter); |
| 1031 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1032 | IntentFilter ownerFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 1033 | ownerFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Suhas Suresh | c9772c8 | 2018-04-27 15:41:19 +0530 | [diff] [blame] | 1034 | ownerFilter.addAction(Intent.ACTION_SHUTDOWN); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1035 | mContext.registerReceiver(mOwnerReceiver, ownerFilter); |
| 1036 | |
| 1037 | ownerFilter = new IntentFilter(); |
| 1038 | ownerFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 1039 | ownerFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1040 | ownerFilter.addDataScheme("package"); |
| 1041 | mContext.registerReceiver(mOwnerReceiver, ownerFilter); |
| 1042 | |
| 1043 | IntentFilter x509CertificateFilter = new IntentFilter(); |
| 1044 | x509CertificateFilter.addAction(NxpConstants.ACTION_CHECK_X509_RESULT); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1045 | mContext.registerReceiverAsUser(x509CertificateReceiver, UserHandle.ALL, |
| 1046 | x509CertificateFilter, NfcPermissions.ADMIN_PERM, null); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1047 | |
Shashank vimal | 8377908 | 2018-02-06 18:10:31 +0530 | [diff] [blame] | 1048 | IntentFilter activateStkFilter = new IntentFilter(); |
| 1049 | activateStkFilter.addAction(NxpConstants.CAT_ACTIVATE_NOTIFY_ACTION); |
| 1050 | mContext.registerReceiver(mActivateSwpInterface, activateStkFilter); |
| 1051 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1052 | IntentFilter enableNfc = new IntentFilter(); |
| 1053 | enableNfc.addAction(NxpConstants.ACTION_GSMA_ENABLE_NFC); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1054 | mContext.registerReceiverAsUser(mEnableNfc, UserHandle.ALL, enableNfc, null, null); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1055 | |
| 1056 | IntentFilter lsFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); |
| 1057 | //mContext.registerReceiver(mAlaReceiver, lsFilter); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1058 | |
| 1059 | IntentFilter policyFilter = new IntentFilter(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 1060 | mContext.registerReceiverAsUser(mPolicyReceiver, UserHandle.ALL, policyFilter, null, null); |
| 1061 | |
| 1062 | updatePackageCache(); |
| 1063 | |
| 1064 | PackageManager pm = mContext.getPackageManager(); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 1065 | mIsHceCapable = |
| 1066 | pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION) || |
| 1067 | pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF); |
| 1068 | mIsHceFCapable = |
| 1069 | pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF); |
| 1070 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1071 | if (mIsHceCapable) { |
| 1072 | mAidRoutingManager = new AidRoutingManager(); |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1073 | mCardEmulationManager = new CardEmulationManager(mContext, mAidRoutingManager); |
| 1074 | mAidCache = mCardEmulationManager.getRegisteredAidCache(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1075 | //mCardEmulationManager = new CardEmulationManager(mContext); |
| 1076 | Log.d("NfcService", "Before mIsHceCapable"); |
| 1077 | mNxpNfcController = new NxpNfcController(mContext, mCardEmulationManager); |
| 1078 | } |
| 1079 | |
| 1080 | mForegroundUtils = ForegroundUtils.getInstance(); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1081 | |
| 1082 | // Make sure this is only called when object construction is complete. |
| 1083 | ServiceManager.addService(SERVICE_NAME, mNfcAdapter); |
| 1084 | |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 1085 | ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Suhas Suresh | e89490a | 2018-07-06 23:36:16 +0530 | [diff] [blame] | 1086 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1087 | new EnableDisableTask().execute(TASK_BOOT); // do blocking boot tasks |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1088 | |
| 1089 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_STATS, STATS_UPDATE_INTERVAL_MS); |
Nikhil Chhabra | fc8f3f6 | 2018-01-08 20:48:49 +0530 | [diff] [blame] | 1090 | |
| 1091 | IVrManager mVrManager = IVrManager.Stub.asInterface(ServiceManager.getService( |
| 1092 | mContext.VR_SERVICE)); |
| 1093 | if (mVrManager != null) { |
| 1094 | try { |
| 1095 | mVrManager.registerListener(mVrStateCallbacks); |
| 1096 | mIsVrModeEnabled = mVrManager.getVrModeState(); |
| 1097 | } catch (RemoteException e) { |
| 1098 | Log.e(TAG, "Failed to register VR mode state listener: " + e); |
| 1099 | } |
| 1100 | } |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 1101 | mSEService = ISecureElementService.Stub.asInterface(ServiceManager.getService( |
| 1102 | Context.SECURE_ELEMENT_SERVICE)); |
nxpandroid | 5c5b215 | 2017-09-14 12:30:17 +0530 | [diff] [blame] | 1103 | /*SoundPool clean up before NFC state updated*/ |
| 1104 | initSoundPool(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1105 | } |
| 1106 | |
Ganesh Deva | 272ad05 | 2018-07-09 15:56:45 +0530 | [diff] [blame] | 1107 | private boolean isSEServiceAvailable() { |
| 1108 | if (mSEService == null) { |
| 1109 | mSEService = ISecureElementService.Stub.asInterface(ServiceManager.getService( |
| 1110 | Context.SECURE_ELEMENT_SERVICE)); |
| 1111 | } |
| 1112 | return (mSEService != null); |
| 1113 | } |
| 1114 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1115 | void initSoundPool() { |
| 1116 | synchronized(this) { |
| 1117 | if (mSoundPool == null) { |
| 1118 | mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); |
| 1119 | mStartSound = mSoundPool.load(mContext, R.raw.start, 1); |
| 1120 | mEndSound = mSoundPool.load(mContext, R.raw.end, 1); |
| 1121 | mErrorSound = mSoundPool.load(mContext, R.raw.error, 1); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | void releaseSoundPool() { |
| 1127 | synchronized (this) { |
| 1128 | if (mSoundPool != null) { |
| 1129 | mSoundPool.release(); |
| 1130 | mSoundPool = null; |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1135 | void updatePackageCache() { |
| 1136 | PackageManager pm = mContext.getPackageManager(); |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 1137 | List<PackageInfo> packagesNfcEvents = pm.getPackagesHoldingPermissions( |
| 1138 | new String[] {android.Manifest.permission.NFC_TRANSACTION_EVENT}, |
| 1139 | PackageManager.GET_ACTIVITIES); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1140 | synchronized (this) { |
Suhas Suresh | 140f7ac | 2018-05-15 14:59:11 +0530 | [diff] [blame] | 1141 | mNfcEventInstalledPackages.clear(); |
| 1142 | for (int i = 0; i < packagesNfcEvents.size(); i++) { |
Sachin Dhivare | 7b0bf0d | 2018-09-20 11:38:53 +0530 | [diff] [blame] | 1143 | int hasPerm = pm.checkPermission(android.Manifest.permission.NFC, |
| 1144 | packagesNfcEvents.get(i).packageName); |
| 1145 | if (hasPerm == PackageManager.PERMISSION_GRANTED){ |
| 1146 | mNfcEventInstalledPackages.add(packagesNfcEvents.get(i).packageName); |
| 1147 | } |
Suhas Suresh | 140f7ac | 2018-05-15 14:59:11 +0530 | [diff] [blame] | 1148 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1152 | int doOpenSecureElementConnection(int seId) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1153 | mEeWakeLock.acquire(); |
| 1154 | try { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1155 | return mSecureElement.doOpenSecureElementConnection(seId); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1156 | } finally { |
| 1157 | mEeWakeLock.release(); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | byte[] doTransceive(int handle, byte[] cmd) { |
| 1162 | mEeWakeLock.acquire(); |
| 1163 | try { |
| 1164 | return doTransceiveNoLock(handle, cmd); |
| 1165 | } finally { |
| 1166 | mEeWakeLock.release(); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | byte[] doTransceiveNoLock(int handle, byte[] cmd) { |
| 1171 | return mSecureElement.doTransceive(handle, cmd); |
| 1172 | } |
| 1173 | |
| 1174 | void doDisconnect(int handle) { |
| 1175 | mEeWakeLock.acquire(); |
| 1176 | try { |
| 1177 | mSecureElement.doDisconnect(handle); |
| 1178 | } finally { |
| 1179 | mEeWakeLock.release(); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | boolean doReset(int handle) { |
| 1184 | mEeWakeLock.acquire(); |
| 1185 | try { |
| 1186 | return mSecureElement.doReset(handle); |
| 1187 | } finally { |
| 1188 | mEeWakeLock.release(); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | public static byte[] CreateSHA(String pkg, int alaVer){ |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1193 | String localTAG = "Utils:CreateSHA"; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1194 | StringBuffer sb = new StringBuffer(); |
| 1195 | try { |
| 1196 | MessageDigest md; |
| 1197 | if(alaVer == 1) |
| 1198 | md = MessageDigest.getInstance("SHA-256"); |
| 1199 | else |
| 1200 | md = MessageDigest.getInstance("SHA-1"); |
| 1201 | |
| 1202 | md.update(pkg.getBytes()); |
| 1203 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1204 | byte[] byteData = md.digest(); |
| 1205 | Log.i(localTAG, "byteData len : " + byteData.length); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1206 | /* |
| 1207 | for (int i = 0; i < byteData.length; i++) { |
| 1208 | sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); |
| 1209 | } |
| 1210 | // Log.i(TAG, "sb.toString()" + sb.toString());*/ |
| 1211 | return byteData; |
| 1212 | } catch (Exception e) { |
| 1213 | e.printStackTrace(); |
| 1214 | } |
| 1215 | return null; |
| 1216 | } |
| 1217 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1218 | /** |
| 1219 | * Manages tasks that involve turning on/off the NFC controller. |
| 1220 | * <p/> |
| 1221 | * <p>All work that might turn the NFC adapter on or off must be done |
| 1222 | * through this task, to keep the handling of mState simple. |
| 1223 | * In other words, mState is only modified in these tasks (and we |
| 1224 | * don't need a lock to read it in these tasks). |
| 1225 | * <p/> |
| 1226 | * <p>These tasks are all done on the same AsyncTask background |
| 1227 | * thread, so they are serialized. Each task may temporarily transition |
| 1228 | * mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in |
| 1229 | * either STATE_ON or STATE_OFF. This way each task can be guaranteed |
| 1230 | * of starting in either STATE_OFF or STATE_ON, without needing to hold |
| 1231 | * NfcService.this for the entire task. |
| 1232 | * <p/> |
| 1233 | * <p>AsyncTask's are also implicitly queued. This is useful for corner |
| 1234 | * cases like turning airplane mode on while TASK_ENABLE is in progress. |
| 1235 | * The TASK_DISABLE triggered by airplane mode will be correctly executed |
| 1236 | * immediately after TASK_ENABLE is complete. This seems like the most sane |
| 1237 | * way to deal with these situations. |
| 1238 | * <p/> |
| 1239 | * <p>{@link #TASK_ENABLE} enables the NFC adapter, without changing |
| 1240 | * preferences |
| 1241 | * <p>{@link #TASK_DISABLE} disables the NFC adapter, without changing |
| 1242 | * preferences |
| 1243 | * <p>{@link #TASK_BOOT} does first boot work and may enable NFC |
| 1244 | */ |
| 1245 | class EnableDisableTask extends AsyncTask<Integer, Void, Void> { |
| 1246 | @Override |
| 1247 | protected Void doInBackground(Integer... params) { |
| 1248 | // Sanity check mState |
| 1249 | switch (mState) { |
| 1250 | case NfcAdapter.STATE_TURNING_OFF: |
| 1251 | case NfcAdapter.STATE_TURNING_ON: |
| 1252 | Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " + |
| 1253 | mState); |
| 1254 | return null; |
| 1255 | } |
| 1256 | |
| 1257 | /* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND, |
| 1258 | * override with the default. THREAD_PRIORITY_BACKGROUND causes |
| 1259 | * us to service software I2C too slow for firmware download |
| 1260 | * with the NXP PN544. |
| 1261 | * TODO: move this to the DAL I2C layer in libnfc-nxp, since this |
| 1262 | * problem only occurs on I2C platforms using PN544 |
| 1263 | */ |
| 1264 | Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 1265 | |
| 1266 | switch (params[0].intValue()) { |
| 1267 | case TASK_ENABLE: |
| 1268 | enableInternal(); |
| 1269 | break; |
| 1270 | case TASK_DISABLE: |
| 1271 | disableInternal(); |
| 1272 | break; |
| 1273 | case TASK_BOOT: |
Suhas Suresh | 1fe09dc | 2018-05-07 11:55:06 +0530 | [diff] [blame] | 1274 | if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) { |
| 1275 | Log.i(TAG, "First Boot"); |
| 1276 | mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false); |
| 1277 | mPrefsEditor.apply(); |
| 1278 | mDeviceHost.factoryReset(); |
| 1279 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1280 | Log.d(TAG, "checking on firmware download"); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1281 | if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT)) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1282 | Log.d(TAG, "NFC is on. Doing normal stuff"); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1283 | mIsTaskBoot = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1284 | enableInternal(); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1285 | mIsTaskBoot = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1286 | } else { |
| 1287 | Log.d(TAG, "NFC is off. Checking firmware version"); |
| 1288 | mDeviceHost.checkFirmware(); |
| 1289 | } |
Nikhil Chhabra | a9e399a | 2018-01-09 11:47:13 +0530 | [diff] [blame] | 1290 | SystemProperties.set("nfc.initialized", "true"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1291 | break; |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1292 | case TASK_RESTART: |
| 1293 | restartInternal(); |
| 1294 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | // Restore default AsyncTask priority |
| 1298 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 1299 | return null; |
| 1300 | } |
| 1301 | |
| 1302 | @Override |
| 1303 | protected void onPostExecute(Void result) { |
| 1304 | |
| 1305 | if(mState == NfcAdapter.STATE_ON){ |
| 1306 | IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION); |
| 1307 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1308 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1309 | filter.addAction(Intent.ACTION_USER_PRESENT); |
| 1310 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1311 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); |
| 1312 | }else if (mState == NfcAdapter.STATE_OFF){ |
| 1313 | mContext.unregisterReceiver(mReceiver); |
| 1314 | IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION); |
| 1315 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1316 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * Check the default Secure Element configuration. |
| 1324 | */ |
| 1325 | void checkSecureElementConfuration() { |
| 1326 | |
| 1327 | /* Get SE List */ |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1328 | int[] seList = mDeviceHost.doGetSecureElementList(); |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 1329 | int uiccSlot = 0; |
| 1330 | uiccSlot = mPrefs.getInt(PREF_CUR_SELECTED_UICC_ID, SECURE_ELEMENT_UICC_SLOT_DEFAULT); |
| 1331 | int status = mDeviceHost.setPreferredSimSlot(uiccSlot); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1332 | /* Check Secure Element setting */ |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1333 | int seNum=mDeviceHost.GetDefaultSE(); |
| 1334 | if(seNum != 0) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1335 | { |
| 1336 | SECURE_ELEMENT_ON_DEFAULT=true; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1337 | SECURE_ELEMENT_ID_DEFAULT=seNum; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1338 | } else { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1339 | if (seList != null) { |
| 1340 | for (int i = 0; i < seList.length; i++) { |
| 1341 | mDeviceHost.doDeselectSecureElement(seList[i]); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | mNfcSecureElementState = |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1347 | mNxpPrefs.getBoolean(PREF_SECURE_ELEMENT_ON, SECURE_ELEMENT_ON_DEFAULT); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1348 | |
| 1349 | if (mNfcSecureElementState) { |
| 1350 | int secureElementId = |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1351 | mNxpPrefs.getInt(PREF_SECURE_ELEMENT_ID, SECURE_ELEMENT_ID_DEFAULT); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1352 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1353 | if (seList != null) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1354 | if (secureElementId != ALL_SE_ID_TYPE/* SECURE_ELEMENT_ALL */) { |
| 1355 | mDeviceHost.doDeselectSecureElement(UICC_ID_TYPE); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1356 | mDeviceHost.doDeselectSecureElement(UICC2_ID_TYPE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1357 | mDeviceHost.doDeselectSecureElement(SMART_MX_ID_TYPE); |
| 1358 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1359 | for (int i = 0; i < seList.length; i++) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1360 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1361 | if (seList[i] == secureElementId) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1362 | if (secureElementId == SMART_MX_ID_TYPE) { // SECURE_ELEMENT_SMX_ID |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1363 | if (seList.length > 1) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1364 | if (DBG) { |
| 1365 | Log.d(TAG, "Deselect UICC"); |
| 1366 | } |
| 1367 | } |
| 1368 | Log.d(TAG, "Select SMX"); |
| 1369 | mDeviceHost.doSelectSecureElement(secureElementId); |
| 1370 | mSelectedSeId = secureElementId; |
| 1371 | break; |
| 1372 | } else if (secureElementId == UICC_ID_TYPE/* SECURE_ELEMENT_UICC_ID */) { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1373 | if (seList.length > 1) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1374 | if (DBG) { |
| 1375 | Log.d(TAG, "Deselect SMX"); |
| 1376 | } |
| 1377 | } |
| 1378 | Log.d(TAG, "Select UICC"); |
| 1379 | mDeviceHost.doSelectSecureElement(secureElementId); |
| 1380 | mSelectedSeId = secureElementId; |
| 1381 | break; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1382 | }else if (secureElementId == UICC2_ID_TYPE/* SECURE_ELEMENT_UICC_ID */) { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1383 | if (seList.length > 1) { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1384 | if (DBG) { |
| 1385 | Log.d(TAG, "Deselect SMX"); |
| 1386 | } |
| 1387 | } |
| 1388 | Log.d(TAG, "Select UICC2"); |
| 1389 | mDeviceHost.doSelectSecureElement(secureElementId); |
| 1390 | mSelectedSeId = secureElementId; |
| 1391 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1392 | } else if (secureElementId == SECURE_ELEMENT_ID_DEFAULT) { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1393 | if (seList.length > 1) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1394 | if (DBG) { |
| 1395 | Log.d(TAG, "UICC deselected by default"); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | } else { |
| 1402 | if (DBG) { |
| 1403 | Log.d(TAG, "Select ALL_SE"); |
| 1404 | } |
| 1405 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1406 | if (seList.length > 1) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1407 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1408 | for (int i = 0; i < seList.length; i++) { |
| 1409 | mDeviceHost.doSelectSecureElement(seList[i]); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1410 | try{ |
| 1411 | //Delay b/w two SE selection. |
| 1412 | Thread.sleep(200); |
| 1413 | } catch(Exception e) { |
| 1414 | e.printStackTrace(); |
| 1415 | } |
| 1416 | } |
| 1417 | mSelectedSeId = secureElementId; |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | } else { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1422 | if (seList != null && seList.length > 0) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1423 | if (DBG) { |
| 1424 | Log.d(TAG, "UICC/eSE deselected by default"); |
| 1425 | } |
| 1426 | mDeviceHost.doDeselectSecureElement(UICC_ID_TYPE); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 1427 | mDeviceHost.doDeselectSecureElement(UICC2_ID_TYPE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1428 | mDeviceHost.doDeselectSecureElement(SMART_MX_ID_TYPE); |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | boolean getJcopOsFileInfo() { |
| 1434 | File jcopOsFile; |
| 1435 | Log.i(TAG, "getJcopOsFileInfo"); |
| 1436 | |
| 1437 | for (int num = 0; num < 3; num++) { |
| 1438 | try{ |
| 1439 | jcopOsFile = new File(path[num]); |
| 1440 | }catch(NullPointerException npe) { |
| 1441 | Log.e(TAG,"path to jcop os file was null"); |
| 1442 | return false; |
| 1443 | } |
| 1444 | long modtime = jcopOsFile.lastModified(); |
| 1445 | SharedPreferences prefs = mContext.getSharedPreferences(PREF,Context.MODE_PRIVATE); |
| 1446 | long prev_modtime = prefs.getLong(PREF_JCOP_MODTIME[num], JCOP_MODTIME_DEFAULT[num]); |
| 1447 | Log.d(TAG,"prev_modtime:" + prev_modtime); |
| 1448 | Log.d(TAG,"new_modtime:" + modtime); |
| 1449 | if(prev_modtime == modtime){ |
| 1450 | return false; |
| 1451 | } |
| 1452 | JCOP_MODTIME_TEMP[num] = modtime; |
| 1453 | } |
| 1454 | return true; |
| 1455 | } |
| 1456 | |
| 1457 | /* jcop os Download at boot time */ |
| 1458 | void jcopOsDownload() { |
| 1459 | int status = ErrorCodes.SUCCESS; |
| 1460 | boolean jcopStatus; |
| 1461 | Log.i(TAG, "Jcop Download starts"); |
| 1462 | |
| 1463 | SharedPreferences prefs = mContext.getSharedPreferences(PREF,Context.MODE_PRIVATE); |
| 1464 | jcopStatus = getJcopOsFileInfo(); |
| 1465 | |
| 1466 | if( jcopStatus == true) { |
| 1467 | Log.i(TAG, "Starting getChipName"); |
| 1468 | int Ver = mDeviceHost.getChipVer(); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1469 | if(Ver == PN80T_ID || Ver == PN67T_ID || Ver == PN66T_ID || Ver == PN65T_ID) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1470 | status = mDeviceHost.JCOSDownload(); |
| 1471 | } |
| 1472 | if(status != ErrorCodes.SUCCESS) { |
| 1473 | Log.i(TAG, "Jcop Download failed"); |
| 1474 | } |
| 1475 | else { |
| 1476 | Log.i(TAG, "Jcop Download success"); |
| 1477 | prefs.edit().putLong(PREF_JCOP_MODTIME[0],JCOP_MODTIME_TEMP[0]).apply(); |
| 1478 | prefs.edit().putLong(PREF_JCOP_MODTIME[1],JCOP_MODTIME_TEMP[1]).apply(); |
| 1479 | prefs.edit().putLong(PREF_JCOP_MODTIME[2],JCOP_MODTIME_TEMP[2]).apply(); |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | /** |
| 1484 | * Enable NFC adapter functions. |
| 1485 | * Does not toggle preferences. |
| 1486 | */ |
| 1487 | boolean enableInternal() { |
| 1488 | if (mState == NfcAdapter.STATE_ON) { |
| 1489 | return true; |
| 1490 | } |
| 1491 | Log.i(TAG, "Enabling NFC"); |
| 1492 | updateState(NfcAdapter.STATE_TURNING_ON); |
| 1493 | int timeout = mDeviceHost.getNfcInitTimeout(); |
| 1494 | if (timeout < INIT_WATCHDOG_MS) |
| 1495 | { |
| 1496 | timeout = INIT_WATCHDOG_MS; |
| 1497 | } |
| 1498 | Log.i(TAG, "Enabling NFC timeout" +timeout); |
| 1499 | WatchDogThread watchDog = new WatchDogThread("enableInternal", timeout); |
| 1500 | watchDog.start(); |
| 1501 | try { |
| 1502 | mRoutingWakeLock.acquire(); |
| 1503 | try { |
| 1504 | if (!mDeviceHost.initialize()) { |
| 1505 | Log.w(TAG, "Error enabling NFC"); |
| 1506 | updateState(NfcAdapter.STATE_OFF); |
Ganesh Deva | 2b05625 | 2018-10-25 18:13:13 +0530 | [diff] [blame] | 1507 | watchDog.cancel(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1508 | return false; |
| 1509 | } |
| 1510 | } finally { |
| 1511 | mRoutingWakeLock.release(); |
| 1512 | } |
| 1513 | } finally { |
| 1514 | watchDog.cancel(); |
| 1515 | } |
nxf32288 | d12785b | 2017-11-17 15:18:31 +0530 | [diff] [blame] | 1516 | mChipVer = mDeviceHost.getChipVer(); |
| 1517 | if(mChipVer < PN553_ID) { |
nxf32288 | 1754ee0 | 2017-10-24 13:33:24 +0530 | [diff] [blame] | 1518 | ALL_SE_ID_TYPE &= ~UICC2_ID_TYPE; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1519 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1520 | checkSecureElementConfuration(); |
| 1521 | |
| 1522 | mIsRouteForced = true; |
| 1523 | if (mIsHceCapable) { |
| 1524 | // Generate the initial card emulation routing table |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1525 | sAidTableFull = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1526 | mCardEmulationManager.onNfcEnabled(); |
| 1527 | } |
| 1528 | mIsRouteForced = false; |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 1529 | nci_version = getNciVersion(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1530 | |
| 1531 | synchronized (NfcService.this) { |
| 1532 | mObjectMap.clear(); |
| 1533 | mP2pLinkManager.enableDisable(mIsNdefPushEnabled, true); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1534 | } |
| 1535 | |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 1536 | synchronized (NfcService.this) { |
Suhas Suresh | e75588d | 2018-07-11 15:22:24 +0530 | [diff] [blame] | 1537 | updateState(NfcAdapter.STATE_ON); |
| 1538 | try { |
Ganesh Deva | f550e96 | 2018-07-24 11:59:09 +0530 | [diff] [blame] | 1539 | mWiredSeInitMethod = mWiredSeClass.getDeclaredMethod("wiredSeInitialize"); |
| 1540 | mWiredSeInitMethod.invoke(mWiredSeObj); |
| 1541 | } catch (NoSuchElementException | NoSuchMethodException e) { |
| 1542 | Log.i(TAG, "No such Method WiredSeInitialize"); |
| 1543 | } catch (RuntimeException | IllegalAccessException | InvocationTargetException e) { |
| 1544 | Log.e(TAG, "Error in invoking wiredSeInitialize invocation"); |
| 1545 | } catch (Exception e) { |
| 1546 | Log.e(TAG, "caught Exception during wiredSeInitialize"); |
| 1547 | e.printStackTrace(); |
Suhas Suresh | e75588d | 2018-07-11 15:22:24 +0530 | [diff] [blame] | 1548 | } |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 1549 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1550 | /* Start polling loop */ |
| 1551 | Log.e(TAG, "applyRouting -3"); |
| 1552 | mScreenState = mScreenStateHelper.checkScreenState(); |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 1553 | int screen_state_mask = (mNfcUnlockManager.isLockscreenPollingEnabled()) ? |
| 1554 | (ScreenStateHelper.SCREEN_POLLING_TAG_MASK | mScreenState) : mScreenState; |
| 1555 | |
| 1556 | if(mNfcUnlockManager.isLockscreenPollingEnabled()) |
| 1557 | applyRouting(false); |
| 1558 | |
| 1559 | mDeviceHost.doSetScreenOrPowerState(screen_state_mask); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1560 | mIsRoutingTableDirty = true; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1561 | if((mScreenState < NFC_POLLING_MODE) && mIsTaskBoot) |
| 1562 | { |
| 1563 | /*During device boot if screen state is other ON_UNLOCKED, |
| 1564 | *first send discovery command with poll and linsten enabled |
| 1565 | *for DC/DC pass through mode activation. |
| 1566 | *Then send normal discovery according to screen state*/ |
| 1567 | applyRouting(true); |
| 1568 | mIsTaskBoot = false; |
| 1569 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1570 | applyRouting(true); |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 1571 | synchronized (NfcService.this) { |
| 1572 | mNxpNfcState = NXP_NFC_STATE_ON; |
| 1573 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1574 | return true; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * Disable all NFC adapter functions. |
| 1579 | * Does not toggle preferences. |
| 1580 | */ |
| 1581 | boolean disableInternal() { |
| 1582 | if (mState == NfcAdapter.STATE_OFF) { |
| 1583 | return true; |
| 1584 | } |
| 1585 | Log.i(TAG, "Disabling NFC"); |
| 1586 | updateState(NfcAdapter.STATE_TURNING_OFF); |
Ganesh Deva | f550e96 | 2018-07-24 11:59:09 +0530 | [diff] [blame] | 1587 | |
| 1588 | try{ |
| 1589 | mWiredSeDeInitMwthod = mWiredSeClass.getDeclaredMethod("wiredSeDeInitialize"); |
| 1590 | mWiredSeDeInitMwthod.invoke(mWiredSeObj); |
| 1591 | } catch (NoSuchElementException | NoSuchMethodException e) { |
| 1592 | Log.i(TAG, "No such Method WiredSeInitialize"); |
| 1593 | } catch (RuntimeException | IllegalAccessException | InvocationTargetException e) { |
| 1594 | Log.e(TAG, "Error in invoking wiredSeInitialize invocation"); |
| 1595 | } catch (Exception e) { |
| 1596 | Log.e(TAG, "caught Exception during wiredSeInitialize"); |
| 1597 | e.printStackTrace(); |
| 1598 | } |
| 1599 | |
nxpandroid | 5c5b215 | 2017-09-14 12:30:17 +0530 | [diff] [blame] | 1600 | /*SoundPool clean up before NFC state updated |
| 1601 | releaseSoundPool();*/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1602 | |
| 1603 | /* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog. |
| 1604 | * Implemented with a new thread (instead of a Handler or AsyncTask), |
| 1605 | * because the UI Thread and AsyncTask thread-pools can also get hung |
| 1606 | * when the NFC controller stops responding */ |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1607 | disableInternalwatchDog = new WatchDogThread("disableInternal", ROUTING_WATCHDOG_MS); |
| 1608 | disableInternalwatchDog.start(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1609 | |
| 1610 | if (mIsHceCapable) { |
| 1611 | mCardEmulationManager.onNfcDisabled(); |
| 1612 | } |
| 1613 | |
| 1614 | mP2pLinkManager.enableDisable(false, false); |
| 1615 | |
| 1616 | /* The NFC-EE may still be opened by another process, |
| 1617 | * and a transceive() could still be in progress on |
| 1618 | * another Binder thread. |
| 1619 | * Give it a while to finish existing operations |
| 1620 | * before we close it. |
| 1621 | */ |
| 1622 | Long startTime = SystemClock.elapsedRealtime(); |
| 1623 | do { |
| 1624 | synchronized (NfcService.this) { |
| 1625 | if (mOpenEe == null) |
| 1626 | break; |
| 1627 | } |
| 1628 | try { |
| 1629 | Thread.sleep(WAIT_FOR_NFCEE_POLL_MS); |
| 1630 | } catch (InterruptedException e) { |
| 1631 | // Ignore |
| 1632 | } |
| 1633 | } while (SystemClock.elapsedRealtime() - startTime < WAIT_FOR_NFCEE_OPERATIONS_MS); |
| 1634 | |
| 1635 | synchronized (NfcService.this) { |
| 1636 | if (mOpenEe != null) { |
| 1637 | try { |
| 1638 | _nfcEeClose(-1, mOpenEe.binder); |
| 1639 | } catch (IOException e) { } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | // Stop watchdog if tag present |
| 1644 | // A convenient way to stop the watchdog properly consists of |
| 1645 | // disconnecting the tag. The polling loop shall be stopped before |
| 1646 | // to avoid the tag being discovered again. |
| 1647 | maybeDisconnectTarget(); |
| 1648 | |
| 1649 | mNfcDispatcher.setForegroundDispatch(null, null, null); |
| 1650 | |
| 1651 | boolean result = mDeviceHost.deinitialize(); |
| 1652 | if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result); |
| 1653 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1654 | disableInternalwatchDog.cancel(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1655 | |
| 1656 | synchronized (NfcService.this) { |
| 1657 | mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters(); |
| 1658 | updateState(NfcAdapter.STATE_OFF); |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 1659 | mNxpNfcState = NXP_NFC_STATE_OFF; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1660 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1661 | return result; |
| 1662 | } |
| 1663 | |
nxpandroid | 1680a6d | 2017-01-13 19:13:14 +0530 | [diff] [blame] | 1664 | boolean restartInternal() |
| 1665 | { |
| 1666 | boolean result; |
| 1667 | result = disableInternal(); |
| 1668 | if (DBG) Log.d(TAG, "disableInternal status = " + result); |
| 1669 | while(true) |
| 1670 | { |
| 1671 | if(mState == NfcAdapter.STATE_OFF) |
| 1672 | { |
| 1673 | if (DBG) Log.d(TAG, "disableInternal is success = " + result); |
| 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | result = enableInternal(); |
| 1678 | if (DBG) Log.d(TAG, "enableInternal status = " + result); |
| 1679 | while(true) |
| 1680 | { |
| 1681 | if(mState == NfcAdapter.STATE_ON) |
| 1682 | { |
| 1683 | if (DBG) Log.d(TAG, "enableInternal is success = " + result); |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 | Intent flashIntent = new Intent(); |
| 1688 | flashIntent.setAction(ACTION_FLASH_SUCCESS); |
| 1689 | if (DBG) Log.d(TAG, "Broadcasting " + ACTION_FLASH_SUCCESS); |
| 1690 | mContext.sendBroadcast(flashIntent); |
| 1691 | return result; |
| 1692 | } |
| 1693 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1694 | void updateState(int newState) { |
| 1695 | synchronized (NfcService.this) { |
| 1696 | if (newState == mState) { |
| 1697 | return; |
| 1698 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1699 | mState = newState; |
| 1700 | Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); |
| 1701 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1702 | intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState); |
| 1703 | mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | void saveNfcOnSetting(boolean on) { |
| 1709 | synchronized (NfcService.this) { |
| 1710 | mPrefsEditor.putBoolean(PREF_NFC_ON, on); |
| 1711 | mPrefsEditor.apply(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1712 | mBackupManager.dataChanged(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | public void playSound(int sound) { |
| 1717 | synchronized (this) { |
| 1718 | if (mSoundPool == null) { |
| 1719 | Log.w(TAG, "Not playing sound when NFC is disabled"); |
| 1720 | return; |
| 1721 | } |
Nikhil Chhabra | fc8f3f6 | 2018-01-08 20:48:49 +0530 | [diff] [blame] | 1722 | |
| 1723 | if (mIsVrModeEnabled) { |
| 1724 | Log.d(TAG, "Not playing NFC sound when Vr Mode is enabled"); |
| 1725 | return; |
| 1726 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1727 | switch (sound) { |
| 1728 | case SOUND_START: |
| 1729 | mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f); |
| 1730 | break; |
| 1731 | case SOUND_END: |
| 1732 | mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f); |
| 1733 | break; |
| 1734 | case SOUND_ERROR: |
| 1735 | mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f); |
| 1736 | break; |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | synchronized int getUserId() { |
| 1742 | return mUserId; |
| 1743 | } |
| 1744 | |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 1745 | void enforceBeamShareActivityPolicy(Context context, UserHandle uh) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1746 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 1747 | IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 1748 | boolean isGlobalEnabled = mIsNdefPushEnabled; |
| 1749 | if (uh.getIdentifier() != mUserId) { |
| 1750 | try { |
| 1751 | int userSetting = mIpm.getComponentEnabledSetting(new ComponentName( |
| 1752 | BeamShareActivity.class.getPackageName$(), |
| 1753 | BeamShareActivity.class.getName()), uh.getIdentifier()); |
| 1754 | isGlobalEnabled = (userSetting == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) ? false : true; |
| 1755 | } catch (RemoteException e) { |
| 1756 | Log.w(TAG, "Unable to get Beam status for user " + uh); |
| 1757 | } |
| 1758 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1759 | boolean isActiveForUser = |
| 1760 | (!um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM, uh)) && |
| 1761 | isGlobalEnabled; |
Suhas Suresh | e2a2ff0 | 2018-04-27 12:21:19 +0530 | [diff] [blame] | 1762 | if (DBG) { |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 1763 | Log.d(TAG, "Enforcing a policy change on user: " + uh.toString() + |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1764 | ", isActiveForUser = " + isActiveForUser); |
| 1765 | } |
| 1766 | try { |
| 1767 | mIpm.setComponentEnabledSetting(new ComponentName( |
| 1768 | BeamShareActivity.class.getPackageName$(), |
| 1769 | BeamShareActivity.class.getName()), |
| 1770 | isActiveForUser ? |
| 1771 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED : |
| 1772 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 1773 | PackageManager.DONT_KILL_APP, |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 1774 | uh.getIdentifier()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1775 | } catch (RemoteException e) { |
| 1776 | Log.w(TAG, "Unable to change Beam status for user " + uh); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | final class NfcAdapterService extends INfcAdapter.Stub { |
| 1781 | @Override |
| 1782 | public boolean enable() throws RemoteException { |
nxf38293 | dcc6746 | 2018-08-31 15:57:08 +0530 | [diff] [blame] | 1783 | NfcPermissions.enforceAdminPermissions(mContext); |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 1784 | if (mNxpNfcState != NXP_NFC_STATE_OFF) { |
| 1785 | return true; |
| 1786 | } else { |
| 1787 | // do nothing |
| 1788 | } |
| 1789 | |
| 1790 | synchronized (NfcService.this) { |
| 1791 | mNxpNfcState = NXP_NFC_STATE_TURNING_ON; |
| 1792 | } |
| 1793 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1794 | int val = mDeviceHost.GetDefaultSE(); |
| 1795 | Log.i(TAG, "getDefaultSE " + val); |
| 1796 | |
| 1797 | saveNfcOnSetting(true); |
| 1798 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1799 | new EnableDisableTask().execute(TASK_ENABLE); |
| 1800 | |
| 1801 | return true; |
| 1802 | } |
| 1803 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1804 | @Override |
| 1805 | public boolean disable(boolean saveState) throws RemoteException { |
nxf38293 | dcc6746 | 2018-08-31 15:57:08 +0530 | [diff] [blame] | 1806 | NfcPermissions.enforceAdminPermissions(mContext); |
Nikhil Chhabra | d6957c7 | 2018-03-09 11:44:48 +0530 | [diff] [blame] | 1807 | if (mNxpNfcState != NXP_NFC_STATE_ON) { |
| 1808 | return true; |
| 1809 | } else { |
| 1810 | // do nothing |
| 1811 | } |
| 1812 | |
| 1813 | synchronized (NfcService.this) { |
| 1814 | mNxpNfcState = NXP_NFC_STATE_TURNING_OFF; |
| 1815 | } |
| 1816 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1817 | Log.d(TAG,"Disabling Nfc."); |
| 1818 | |
| 1819 | //Check if this a device shutdown or Nfc only Nfc disable. |
| 1820 | if(!mPowerShutDown) |
| 1821 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1822 | int[] seList = mDeviceHost.doGetSecureElementList(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1823 | Log.i(TAG, "Disabling NFC Disabling ESE/UICC"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1824 | //Since only Nfc is getting disabled so disable CE from EE. |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 1825 | mDeviceHost.doSetScreenOrPowerState(ScreenStateHelper.POWER_STATE_ON); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1826 | if (seList != null) { |
| 1827 | for (int i = 0; i < seList.length; i++) { |
| 1828 | mDeviceHost.doDeselectSecureElement(seList[i]); |
| 1829 | } |
| 1830 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1831 | } else { |
| 1832 | Log.i(TAG, "Power off : Disabling NFC Disabling ESE/UICC"); |
| 1833 | mPowerShutDown = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1834 | mCardEmulationManager.onPreferredForegroundServiceChanged(null); |
| 1835 | } |
| 1836 | |
| 1837 | if (saveState) { |
| 1838 | saveNfcOnSetting(false); |
| 1839 | } |
| 1840 | |
| 1841 | new EnableDisableTask().execute(TASK_DISABLE); |
| 1842 | |
| 1843 | return true; |
| 1844 | } |
| 1845 | |
| 1846 | @Override |
| 1847 | public void pausePolling(int timeoutInMs) { |
| 1848 | NfcPermissions.enforceAdminPermissions(mContext); |
| 1849 | |
| 1850 | if (timeoutInMs <= 0 || timeoutInMs > MAX_POLLING_PAUSE_TIMEOUT) { |
| 1851 | Log.e(TAG, "Refusing to pause polling for " + timeoutInMs + "ms."); |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | synchronized (NfcService.this) { |
| 1856 | mPollingPaused = true; |
| 1857 | mDeviceHost.disableDiscovery(); |
| 1858 | mHandler.sendMessageDelayed( |
| 1859 | mHandler.obtainMessage(MSG_RESUME_POLLING), timeoutInMs); |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | @Override |
| 1864 | public void resumePolling() { |
| 1865 | NfcPermissions.enforceAdminPermissions(mContext); |
| 1866 | |
| 1867 | synchronized (NfcService.this) { |
| 1868 | if (!mPollingPaused) { |
| 1869 | return; |
| 1870 | } |
| 1871 | |
| 1872 | mHandler.removeMessages(MSG_RESUME_POLLING); |
| 1873 | mPollingPaused = false; |
| 1874 | new ApplyRoutingTask().execute(); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | @Override |
| 1879 | public boolean isNdefPushEnabled() throws RemoteException { |
| 1880 | synchronized (NfcService.this) { |
| 1881 | return mState == NfcAdapter.STATE_ON && mIsNdefPushEnabled; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | @Override |
| 1886 | public boolean enableNdefPush() throws RemoteException { |
| 1887 | NfcPermissions.enforceAdminPermissions(mContext); |
| 1888 | synchronized (NfcService.this) { |
| 1889 | if (mIsNdefPushEnabled) { |
| 1890 | return true; |
| 1891 | } |
| 1892 | Log.i(TAG, "enabling NDEF Push"); |
| 1893 | mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, true); |
| 1894 | mPrefsEditor.apply(); |
| 1895 | mIsNdefPushEnabled = true; |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 1896 | enforceBeamShareActivityPolicy(mContext, new UserHandle(mUserId)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1897 | if (isNfcEnabled()) { |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 1898 | mDeviceHost.doEnablep2p(mIsNdefPushEnabled); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1899 | mP2pLinkManager.enableDisable(true, true); |
| 1900 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1901 | mBackupManager.dataChanged(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1902 | } |
| 1903 | return true; |
| 1904 | } |
| 1905 | |
| 1906 | @Override |
| 1907 | public boolean disableNdefPush() throws RemoteException { |
| 1908 | NfcPermissions.enforceAdminPermissions(mContext); |
| 1909 | synchronized (NfcService.this) { |
| 1910 | if (!mIsNdefPushEnabled) { |
| 1911 | return true; |
| 1912 | } |
| 1913 | Log.i(TAG, "disabling NDEF Push"); |
| 1914 | mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, false); |
| 1915 | mPrefsEditor.apply(); |
| 1916 | mIsNdefPushEnabled = false; |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 1917 | enforceBeamShareActivityPolicy(mContext, new UserHandle(mUserId)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1918 | if (isNfcEnabled()) { |
| 1919 | mP2pLinkManager.enableDisable(false, true); |
| 1920 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 1921 | mBackupManager.dataChanged(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1922 | } |
| 1923 | return true; |
| 1924 | } |
| 1925 | |
| 1926 | @Override |
| 1927 | public void setForegroundDispatch(PendingIntent intent, |
| 1928 | IntentFilter[] filters, TechListParcel techListsParcel) { |
| 1929 | NfcPermissions.enforceUserPermissions(mContext); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1930 | if (!mForegroundUtils.isInForeground(Binder.getCallingUid())) { |
| 1931 | Log.e(TAG, "setForegroundDispatch: Caller not in foreground."); |
| 1932 | return; |
| 1933 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1934 | // Short-cut the disable path |
| 1935 | if (intent == null && filters == null && techListsParcel == null) { |
| 1936 | mNfcDispatcher.setForegroundDispatch(null, null, null); |
| 1937 | return; |
| 1938 | } |
| 1939 | |
| 1940 | // Validate the IntentFilters |
| 1941 | if (filters != null) { |
| 1942 | if (filters.length == 0) { |
| 1943 | filters = null; |
| 1944 | } else { |
| 1945 | for (IntentFilter filter : filters) { |
| 1946 | if (filter == null) { |
| 1947 | throw new IllegalArgumentException("null IntentFilter"); |
| 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | // Validate the tech lists |
| 1954 | String[][] techLists = null; |
| 1955 | if (techListsParcel != null) { |
| 1956 | techLists = techListsParcel.getTechLists(); |
| 1957 | } |
| 1958 | |
| 1959 | mNfcDispatcher.setForegroundDispatch(intent, filters, techLists); |
| 1960 | } |
| 1961 | |
| 1962 | @Override |
| 1963 | public void setAppCallback(IAppCallback callback) { |
| 1964 | NfcPermissions.enforceUserPermissions(mContext); |
| 1965 | |
| 1966 | // don't allow Beam for managed profiles, or devices with a device owner or policy owner |
| 1967 | UserInfo userInfo = mUserManager.getUserInfo(UserHandle.getCallingUserId()); |
| 1968 | if(!mUserManager.hasUserRestriction( |
| 1969 | UserManager.DISALLOW_OUTGOING_BEAM, userInfo.getUserHandle())) { |
| 1970 | mP2pLinkManager.setNdefCallback(callback, Binder.getCallingUid()); |
| 1971 | } else if (DBG) { |
| 1972 | Log.d(TAG, "Disabling default Beam behavior"); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | @Override |
| 1977 | public INfcAdapterExtras getNfcAdapterExtrasInterface(String pkg) { |
nxpandroid | a51b5bd | 2017-04-10 18:28:21 +0530 | [diff] [blame] | 1978 | |
| 1979 | if (pkg.equals(PACKAGE_SMART_CARD_SERVICE)){ |
| 1980 | Log.d(TAG, "wildcard for SmartcardService"); |
| 1981 | return mExtrasService; |
| 1982 | } |
| 1983 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1984 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 1985 | return mExtrasService; |
| 1986 | } |
| 1987 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 1988 | @Override |
| 1989 | public INfcDta getNfcDtaInterface(String pkg) throws RemoteException { |
| 1990 | NfcPermissions.enforceAdminPermissions(mContext); |
| 1991 | if (mNfcDtaService == null) { |
| 1992 | mNfcDtaService = new NfcDtaService(); |
| 1993 | } |
| 1994 | return mNfcDtaService; |
| 1995 | } |
| 1996 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 1997 | @Override |
| 1998 | public boolean ignore(int nativeHandle, int debounceMs, ITagRemovedCallback callback) |
| 1999 | throws RemoteException { |
| 2000 | NfcPermissions.enforceUserPermissions(mContext); |
| 2001 | |
Nikhil Chhabra | 1b2368f | 2018-01-09 18:34:21 +0530 | [diff] [blame] | 2002 | if (debounceMs == 0 && mDebounceTagNativeHandle != INVALID_NATIVE_HANDLE |
| 2003 | && nativeHandle == mDebounceTagNativeHandle) { |
| 2004 | // Remove any previous messages and immediately debounce. |
| 2005 | mHandler.removeMessages(MSG_TAG_DEBOUNCE); |
| 2006 | mHandler.sendEmptyMessage(MSG_TAG_DEBOUNCE); |
| 2007 | return true; |
| 2008 | } |
| 2009 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2010 | TagEndpoint tag = (TagEndpoint) findAndRemoveObject(nativeHandle); |
| 2011 | if (tag != null) { |
| 2012 | // Store UID and params |
| 2013 | int uidLength = tag.getUid().length; |
| 2014 | synchronized (NfcService.this) { |
| 2015 | mDebounceTagDebounceMs = debounceMs; |
Nikhil Chhabra | 1b2368f | 2018-01-09 18:34:21 +0530 | [diff] [blame] | 2016 | mDebounceTagNativeHandle = nativeHandle; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2017 | mDebounceTagUid = new byte[uidLength]; |
| 2018 | mDebounceTagRemovedCallback = callback; |
| 2019 | System.arraycopy(tag.getUid(), 0, mDebounceTagUid, 0, uidLength); |
| 2020 | } |
| 2021 | |
| 2022 | // Disconnect from this tag; this should resume the normal |
| 2023 | // polling loop (and enter listen mode for a while), before |
| 2024 | // we pick up any tags again. |
| 2025 | tag.disconnect(); |
| 2026 | mHandler.sendEmptyMessageDelayed(MSG_TAG_DEBOUNCE, debounceMs); |
| 2027 | return true; |
| 2028 | } else { |
| 2029 | return false; |
| 2030 | } |
| 2031 | } |
| 2032 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2033 | @Override |
| 2034 | public void verifyNfcPermission() { |
| 2035 | NfcPermissions.enforceUserPermissions(mContext); |
| 2036 | } |
| 2037 | |
| 2038 | @Override |
| 2039 | public void invokeBeam() { |
| 2040 | NfcPermissions.enforceUserPermissions(mContext); |
| 2041 | |
| 2042 | if (mForegroundUtils.isInForeground(Binder.getCallingUid())) { |
| 2043 | mP2pLinkManager.onManualBeamInvoke(null); |
| 2044 | } else { |
| 2045 | Log.e(TAG, "Calling activity not in foreground."); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | @Override |
| 2050 | public void invokeBeamInternal(BeamShareData shareData) { |
| 2051 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2052 | Message msg = Message.obtain(); |
| 2053 | msg.what = MSG_INVOKE_BEAM; |
| 2054 | msg.obj = shareData; |
| 2055 | // We have to send this message delayed for two reasons: |
| 2056 | // 1) This is an IPC call from BeamShareActivity, which is |
| 2057 | // running when the user has invoked Beam through the |
| 2058 | // share menu. As soon as BeamShareActivity closes, the UI |
| 2059 | // will need some time to rebuild the original Activity. |
| 2060 | // Waiting here for a while gives a better chance of the UI |
| 2061 | // having been rebuilt, which means the screenshot that the |
| 2062 | // Beam animation is using will be more accurate. |
| 2063 | // 2) Similarly, because the Activity that launched BeamShareActivity |
| 2064 | // with an ACTION_SEND intent is now in paused state, the NDEF |
| 2065 | // callbacks that it has registered may no longer be valid. |
| 2066 | // Allowing the original Activity to resume will make sure we |
| 2067 | // it has a chance to re-register the NDEF message / callback, |
| 2068 | // so we share the right data. |
| 2069 | // |
| 2070 | // Note that this is somewhat of a hack because the delay may not actually |
| 2071 | // be long enough for 2) on very slow devices, but there's no better |
| 2072 | // way to do this right now without additional framework changes. |
| 2073 | mHandler.sendMessageDelayed(msg, INVOKE_BEAM_DELAY_MS); |
| 2074 | } |
| 2075 | |
| 2076 | @Override |
| 2077 | public INfcTag getNfcTagInterface() throws RemoteException { |
| 2078 | return mNfcTagService; |
| 2079 | } |
| 2080 | |
| 2081 | @Override |
| 2082 | public INfcCardEmulation getNfcCardEmulationInterface() { |
| 2083 | if (mIsHceCapable) { |
| 2084 | return mCardEmulationManager.getNfcCardEmulationInterface(); |
| 2085 | } else { |
| 2086 | return null; |
| 2087 | } |
| 2088 | } |
| 2089 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2090 | @Override |
| 2091 | public INfcFCardEmulation getNfcFCardEmulationInterface() { |
| 2092 | if (mIsHceFCapable) { |
| 2093 | return mCardEmulationManager.getNfcFCardEmulationInterface(); |
| 2094 | } else { |
| 2095 | return null; |
| 2096 | } |
| 2097 | } |
| 2098 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2099 | |
| 2100 | @Override |
| 2101 | public int getState() throws RemoteException { |
| 2102 | synchronized (NfcService.this) { |
| 2103 | return mState; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | @Override |
| 2108 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2109 | NfcService.this.dump(fd, pw, args); |
| 2110 | } |
| 2111 | |
| 2112 | @Override |
| 2113 | public void dispatch(Tag tag) throws RemoteException { |
| 2114 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2115 | mNfcDispatcher.dispatchTag(tag); |
| 2116 | } |
| 2117 | |
| 2118 | @Override |
| 2119 | public void setP2pModes(int initiatorModes, int targetModes) throws RemoteException { |
| 2120 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2121 | mDeviceHost.setP2pInitiatorModes(initiatorModes); |
| 2122 | mDeviceHost.setP2pTargetModes(targetModes); |
| 2123 | applyRouting(true); |
| 2124 | } |
| 2125 | |
| 2126 | @Override |
| 2127 | public void setReaderMode(IBinder binder, IAppCallback callback, int flags, Bundle extras) |
| 2128 | throws RemoteException { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2129 | int callingUid = Binder.getCallingUid(); |
| 2130 | if (callingUid != Process.SYSTEM_UID && !mForegroundUtils.isInForeground(callingUid)) { |
| 2131 | Log.e(TAG, "setReaderMode: Caller is not in foreground and is not system process."); |
| 2132 | return; |
| 2133 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2134 | synchronized (NfcService.this) { |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2135 | if (!isNfcEnabled()) { |
| 2136 | Log.e(TAG, "setReaderMode() called while NFC is not enabled."); |
| 2137 | return; |
| 2138 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2139 | if (flags != 0) { |
| 2140 | try { |
| 2141 | mReaderModeParams = new ReaderModeParams(); |
| 2142 | mReaderModeParams.callback = callback; |
| 2143 | mReaderModeParams.flags = flags; |
| 2144 | mReaderModeParams.presenceCheckDelay = extras != null |
| 2145 | ? (extras.getInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, |
| 2146 | DEFAULT_PRESENCE_CHECK_DELAY)) |
| 2147 | : DEFAULT_PRESENCE_CHECK_DELAY; |
| 2148 | binder.linkToDeath(mReaderModeDeathRecipient, 0); |
| 2149 | } catch (RemoteException e) { |
| 2150 | Log.e(TAG, "Remote binder has already died."); |
| 2151 | return; |
| 2152 | } |
| 2153 | } else { |
| 2154 | try { |
| 2155 | mReaderModeParams = null; |
Suhas Suresh | 5efc543 | 2018-04-27 15:31:02 +0530 | [diff] [blame] | 2156 | StopPresenceChecking(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2157 | binder.unlinkToDeath(mReaderModeDeathRecipient, 0); |
| 2158 | } catch (NoSuchElementException e) { |
| 2159 | Log.e(TAG, "Reader mode Binder was never registered."); |
| 2160 | } |
| 2161 | } |
| 2162 | Log.e(TAG, "applyRouting -4"); |
| 2163 | applyRouting(false); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | @Override |
| 2169 | public void addNfcUnlockHandler(INfcUnlockHandler unlockHandler, int[] techList) { |
| 2170 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2171 | |
| 2172 | int lockscreenPollMask = computeLockscreenPollMask(techList); |
| 2173 | synchronized (NfcService.this) { |
| 2174 | mNfcUnlockManager.addUnlockHandler(unlockHandler, lockscreenPollMask); |
| 2175 | } |
| 2176 | |
| 2177 | applyRouting(false); |
| 2178 | } |
| 2179 | |
| 2180 | @Override |
| 2181 | public void removeNfcUnlockHandler(INfcUnlockHandler token) throws RemoteException { |
| 2182 | synchronized (NfcService.this) { |
| 2183 | mNfcUnlockManager.removeUnlockHandler(token.asBinder()); |
| 2184 | } |
| 2185 | |
| 2186 | applyRouting(false); |
| 2187 | } |
| 2188 | private int computeLockscreenPollMask(int[] techList) { |
| 2189 | |
| 2190 | Map<Integer, Integer> techCodeToMask = new HashMap<Integer, Integer>(); |
| 2191 | |
| 2192 | techCodeToMask.put(TagTechnology.NFC_A, NfcService.NFC_POLL_A); |
| 2193 | techCodeToMask.put(TagTechnology.NFC_B, NfcService.NFC_POLL_B); |
Nikhil Chhabra | 288edb0 | 2018-01-10 19:36:21 +0530 | [diff] [blame] | 2194 | techCodeToMask.put(TagTechnology.NFC_V, NfcService.NFC_POLL_V); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2195 | techCodeToMask.put(TagTechnology.NFC_F, NfcService.NFC_POLL_F); |
| 2196 | techCodeToMask.put(TagTechnology.NFC_BARCODE, NfcService.NFC_POLL_KOVIO); |
| 2197 | techCodeToMask.put(TagTechnology.MIFARE_CLASSIC, NfcService.NFC_POLL_A); |
| 2198 | techCodeToMask.put(TagTechnology.MIFARE_ULTRALIGHT, NfcService.NFC_POLL_A); |
| 2199 | |
| 2200 | int mask = 0; |
| 2201 | |
| 2202 | for (int i = 0; i < techList.length; i++) { |
| 2203 | if (techCodeToMask.containsKey(techList[i])) { |
| 2204 | mask |= techCodeToMask.get(techList[i]).intValue(); |
| 2205 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 2206 | int screenState = mScreenStateHelper.checkScreenState(); |
| 2207 | if (screenState != mScreenState) { |
| 2208 | new ApplyRoutingTask().execute(Integer.valueOf(screenState)); |
| 2209 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | return mask; |
| 2213 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 2214 | |
| 2215 | /** |
| 2216 | * An interface for nxp extensions |
| 2217 | */ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2218 | @Override |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 2219 | public IBinder getNfcAdapterVendorInterface(String vendor) { |
| 2220 | if(vendor.equalsIgnoreCase("nxp")) { |
| 2221 | return (IBinder) mNxpNfcAdapter; |
| 2222 | } else { |
| 2223 | return null; |
| 2224 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2225 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 2226 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2227 | } |
| 2228 | final class NxpNfcAdapterService extends INxpNfcAdapter.Stub { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2229 | //GSMA Changes |
| 2230 | @Override |
| 2231 | public INxpNfcController getNxpNfcControllerInterface() { |
| 2232 | return mNxpNfcController.getNxpNfcControllerInterface(); |
| 2233 | } |
| 2234 | |
| 2235 | @Override |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2236 | public INfcVzw getNfcVzwInterface() { |
| 2237 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2238 | //begin |
| 2239 | if(mVzwService == null){ |
| 2240 | mVzwService = new NfcVzwService(); |
| 2241 | } |
| 2242 | //end |
| 2243 | return mVzwService; |
| 2244 | } |
| 2245 | |
| 2246 | @Override |
| 2247 | public int setEmvCoPollProfile(boolean enable, int route) throws RemoteException { |
| 2248 | return mDeviceHost.setEmvCoPollProfile(enable, route); |
| 2249 | } |
| 2250 | |
| 2251 | @Override |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2252 | public int[] getActiveSecureElementList(String pkg) throws RemoteException { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2253 | |
| 2254 | int[] list = null; |
| 2255 | if (isNfcEnabled()) { |
| 2256 | list = mDeviceHost.doGetActiveSecureElementList(); |
| 2257 | } |
| 2258 | for(int i=0; i< list.length; i++) { |
| 2259 | Log.d(TAG, "Active element = "+ list[i]); |
| 2260 | } |
| 2261 | return list; |
| 2262 | } |
| 2263 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2264 | @Override |
Ganesh Deva | ee0da3a | 2018-11-27 20:29:40 +0530 | [diff] [blame^] | 2265 | public void MifareDesfireRouteSet(int routeLoc, boolean fullPower, boolean lowPower, boolean noPower) |
| 2266 | throws RemoteException |
| 2267 | { |
| 2268 | if((mChipVer < PN553_ID) && (routeLoc == UICC2_ID_TYPE)) { |
| 2269 | throw new RemoteException("UICC2 is not supported"); |
| 2270 | } |
| 2271 | int protoRouteEntry = 0; |
| 2272 | /*UICC2 ID-4(fromApp) mapped to 3 (JNI)*/ |
| 2273 | protoRouteEntry=((routeLoc & 0x07) == 0x04) ? (0x03 << ROUTE_LOC_MASK) : /*UICC2*/ |
| 2274 | ((routeLoc & 0x07) == 0x02) ? (0x02 << ROUTE_LOC_MASK) : /*UICC1*/ |
| 2275 | ((routeLoc & 0x07) == 0x01) ? (0x01 << ROUTE_LOC_MASK) : /*eSE*/ |
| 2276 | 0x00; |
| 2277 | protoRouteEntry |= ((fullPower ? (mDeviceHost.getDefaultDesfirePowerState() & 0x1F) | 0x01 : 0) | (lowPower ? 0x01 << 1 :0 ) | (noPower ? 0x01 << 2 :0)); |
| 2278 | |
| 2279 | if(routeLoc == 0x00) |
| 2280 | { |
| 2281 | /* |
| 2282 | bit pos 1 = Power Off |
| 2283 | bit pos 2 = Battery Off |
| 2284 | bit pos 4 = Screen Off |
| 2285 | Set these bits to 0 because in case routeLoc = HOST it can not work on POWER_OFF, BATTERY_OFF and SCREEN_OFF*/ |
| 2286 | protoRouteEntry &= 0xE9; |
| 2287 | } |
| 2288 | |
| 2289 | Log.i(TAG,"MifareDesfireRouteSet : " + protoRouteEntry); |
| 2290 | mNxpPrefsEditor = mNxpPrefs.edit(); |
| 2291 | mNxpPrefsEditor.putInt("PREF_MIFARE_DESFIRE_PROTO_ROUTE_ID", protoRouteEntry); |
| 2292 | mNxpPrefsEditor.commit(); |
| 2293 | Log.i(TAG,"MifareDesfireRouteSet function in"); |
| 2294 | commitRouting(); |
| 2295 | } |
| 2296 | |
| 2297 | @Override |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2298 | public void DefaultRouteSet(int routeLoc, boolean fullPower, boolean lowPower, boolean noPower) |
| 2299 | throws RemoteException |
| 2300 | { |
anil.hiranniah | 3cb5a1f | 2018-02-07 19:29:55 +0530 | [diff] [blame] | 2301 | if((mChipVer < PN553_ID) && (routeLoc == UICC2_ID_TYPE)) { |
| 2302 | throw new RemoteException("UICC2 is not supported"); |
| 2303 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2304 | if (mIsHceCapable) { |
| 2305 | int protoRouteEntry = 0; |
anil.hiranniah | 3cb5a1f | 2018-02-07 19:29:55 +0530 | [diff] [blame] | 2306 | protoRouteEntry=((routeLoc & 0x07) == 0x04) ? (0x03 << ROUTE_LOC_MASK) : /*UICC2*/ |
| 2307 | ((routeLoc & 0x07) == 0x02) ? (0x02 << ROUTE_LOC_MASK) : /*UICC1*/ |
| 2308 | ((routeLoc & 0x07) == 0x01) ? (0x01 << ROUTE_LOC_MASK) : /*eSE*/ |
| 2309 | 0x00; |
nxpandroid | 8aecbf8 | 2016-09-16 20:21:47 +0530 | [diff] [blame] | 2310 | protoRouteEntry |= ((fullPower ? (mDeviceHost.getDefaultAidPowerState() & 0x1F) | 0x01 : 0) | (lowPower ? 0x01 << 1 :0 ) | (noPower ? 0x01 << 2 :0)); |
| 2311 | |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 2312 | if(routeLoc == 0x00) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 2313 | { |
nxpandroid | 8aecbf8 | 2016-09-16 20:21:47 +0530 | [diff] [blame] | 2314 | /* |
| 2315 | bit pos 1 = Power Off |
| 2316 | bit pos 2 = Battery Off |
| 2317 | bit pos 4 = Screen Off |
| 2318 | Set these bits to 0 because in case routeLoc = HOST it can not work on POWER_OFF, BATTERY_OFF and SCREEN_OFF*/ |
| 2319 | protoRouteEntry &= 0xE9; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 2320 | } |
| 2321 | Log.i(TAG,"DefaultRouteSet : " + protoRouteEntry); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2322 | if(GetDefaultRouteLoc() != routeLoc) |
| 2323 | { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 2324 | mNxpPrefsEditor = mNxpPrefs.edit(); |
| 2325 | mNxpPrefsEditor.putInt("PREF_SET_DEFAULT_ROUTE_ID", protoRouteEntry ); |
| 2326 | mNxpPrefsEditor.commit(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2327 | mIsRouteForced = true; |
| 2328 | if (mIsHceCapable) { |
| 2329 | mAidRoutingManager.onNfccRoutingTableCleared(); |
| 2330 | mCardEmulationManager.onRoutingTableChanged(); |
| 2331 | } |
| 2332 | mIsRouteForced = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2333 | } |
| 2334 | } |
| 2335 | else{ |
| 2336 | Log.i(TAG,"DefaultRoute can not be set. mIsHceCapable = flase"); |
| 2337 | } |
| 2338 | } |
| 2339 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2340 | @Override |
Ganesh Deva | ee0da3a | 2018-11-27 20:29:40 +0530 | [diff] [blame^] | 2341 | public void MifareCLTRouteSet(int routeLoc, boolean fullPower, boolean lowPower, boolean noPower) |
| 2342 | throws RemoteException |
| 2343 | { |
| 2344 | if((mChipVer < PN553_ID) && (routeLoc == UICC2_ID_TYPE)) { |
| 2345 | throw new RemoteException("UICC2 is not supported"); |
| 2346 | } |
| 2347 | |
| 2348 | int techRouteEntry=0; |
| 2349 | techRouteEntry=((routeLoc & 0x07) == 0x04) ? (0x03 << ROUTE_LOC_MASK) : /*UICC2*/ |
| 2350 | ((routeLoc & 0x07) == 0x02) ? (0x02 << ROUTE_LOC_MASK) : /*UICC1*/ |
| 2351 | ((routeLoc & 0x07) == 0x01) ? (0x01 << ROUTE_LOC_MASK) : /*eSE*/ |
| 2352 | 0x00; |
| 2353 | techRouteEntry |= ((fullPower ? (mDeviceHost.getDefaultMifareCLTPowerState() & 0x1F) | 0x01 : 0) | (lowPower ? 0x01 << 1 :0 ) | (noPower ? 0x01 << 2 :0)); |
| 2354 | techRouteEntry |= (TECH_TYPE_A << TECH_TYPE_MASK); |
| 2355 | |
| 2356 | Log.i(TAG,"MifareCLTRouteSet : " + techRouteEntry); |
| 2357 | mNxpPrefsEditor = mNxpPrefs.edit(); |
| 2358 | mNxpPrefsEditor.putInt("PREF_MIFARE_CLT_ROUTE_ID", techRouteEntry); |
| 2359 | mNxpPrefsEditor.commit(); |
| 2360 | commitRouting(); |
| 2361 | } |
| 2362 | |
| 2363 | @Override |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2364 | public byte[] getFWVersion() |
| 2365 | { |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 2366 | byte[] buf = new byte[3]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2367 | Log.i(TAG, "Starting getFwVersion"); |
| 2368 | int fwver = mDeviceHost.getFWVersion(); |
| 2369 | buf[0] = (byte)((fwver&0xFF00)>>8); |
| 2370 | buf[1] = (byte)((fwver&0xFF)); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 2371 | buf[2] = (byte)((fwver&0xFF0000)>>16); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2372 | Log.i(TAG, "Firmware version is 0x"+ buf[0]+" 0x"+buf[1]); |
| 2373 | return buf; |
| 2374 | } |
| 2375 | |
| 2376 | @Override |
nxf38293 | 16ee8ee | 2018-06-29 12:05:36 +0530 | [diff] [blame] | 2377 | public byte[] readerPassThruMode(byte status, byte modulationTyp) |
| 2378 | throws RemoteException { |
| 2379 | |
| 2380 | Log.i(TAG, "Reader pass through mode request: 0x" + status + |
| 2381 | " with modulation: 0x" + modulationTyp); |
| 2382 | return mDeviceHost.readerPassThruMode(status, modulationTyp); |
| 2383 | } |
| 2384 | |
| 2385 | @Override |
| 2386 | public byte[] transceiveAppData(byte[] data) throws RemoteException { |
| 2387 | |
| 2388 | Log.i(TAG, "Transceive requested on reader pass through mode"); |
| 2389 | return mDeviceHost.transceiveAppData(data); |
| 2390 | } |
| 2391 | |
| 2392 | @Override |
nxf26763 | be2bd68 | 2018-11-12 17:02:43 +0530 | [diff] [blame] | 2393 | public List<NxpAidServiceInfo> getServicesAidInfo(int userId, String category){ |
| 2394 | return mCardEmulationManager.getServicesAidInfo(userId, category); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2395 | } |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 2396 | |
| 2397 | @Override |
| 2398 | public int updateServiceState(int userId , Map serviceState) { |
| 2399 | return mCardEmulationManager.updateServiceState(userId ,serviceState); |
| 2400 | } |
| 2401 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2402 | @Override |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2403 | public int getMaxAidRoutingTableSize() throws RemoteException { |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 2404 | NfcPermissions.enforceUserPermissions(mContext); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2405 | return getAidRoutingTableSize(); |
| 2406 | } |
| 2407 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2408 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2409 | @Override |
| 2410 | public int getCommittedAidRoutingTableSize() throws RemoteException { |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 2411 | NfcPermissions.enforceUserPermissions(mContext); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2412 | return (getAidRoutingTableSize() - getRemainingAidTableSize()); |
| 2413 | } |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2414 | |
| 2415 | @Override |
| 2416 | public int setConfig(String configs , String pkg) { |
| 2417 | Log.e(TAG, "Setting configs for Transit" ); |
| 2418 | /*Check permissions*/ |
| 2419 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2420 | /*Check if any NFC transactions are ongoing*/ |
| 2421 | if(mDeviceHost.isNfccBusy()) |
| 2422 | { |
| 2423 | Log.e(TAG, "NFCC is busy.." ); |
| 2424 | return TRANSIT_SETCONFIG_STAT_FAILED; |
| 2425 | } |
| 2426 | /*check if format of configs is fine*/ |
| 2427 | /*Save configurations to file*/ |
| 2428 | try { |
Ganesh Deva | 382b106 | 2018-07-27 23:09:03 +0530 | [diff] [blame] | 2429 | File newTextFile = new File("/data/nfc/libnfc-nxpTransit.conf"); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2430 | if(configs == null) |
| 2431 | { |
| 2432 | if(newTextFile.delete()){ |
| 2433 | Log.e(TAG, "Removing transit config file. Taking default Value" ); |
| 2434 | }else{ |
| 2435 | System.out.println("Error taking defualt value"); |
| 2436 | } |
| 2437 | } |
| 2438 | else |
| 2439 | { |
| 2440 | FileWriter fw = new FileWriter(newTextFile); |
| 2441 | fw.write(configs); |
| 2442 | fw.close(); |
| 2443 | Log.e(TAG, "File Written to libnfc-nxpTransit.conf successfully" ); |
| 2444 | } |
Ganesh Deva | 382b106 | 2018-07-27 23:09:03 +0530 | [diff] [blame] | 2445 | mDeviceHost.setTransitConfig(configs); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2446 | } catch (Exception e) { |
| 2447 | e.printStackTrace(); |
| 2448 | return TRANSIT_SETCONFIG_STAT_FAILED; |
| 2449 | } |
| 2450 | |
| 2451 | /*restart NFC service*/ |
| 2452 | try { |
| 2453 | mNfcAdapter.disable(true); |
Ganesh Deva | 382b106 | 2018-07-27 23:09:03 +0530 | [diff] [blame] | 2454 | WaitForAdapterChange(NfcAdapter.STATE_OFF); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2455 | mNfcAdapter.enable(); |
Ganesh Deva | 382b106 | 2018-07-27 23:09:03 +0530 | [diff] [blame] | 2456 | WaitForAdapterChange(NfcAdapter.STATE_ON); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2457 | } catch (Exception e) { |
| 2458 | Log.e(TAG, "Unable to restart NFC Service"); |
| 2459 | e.printStackTrace(); |
| 2460 | return TRANSIT_SETCONFIG_STAT_FAILED; |
| 2461 | } |
| 2462 | return TRANSIT_SETCONFIG_STAT_SUCCESS; |
| 2463 | } |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 2464 | |
Ganesh Deva | 382b106 | 2018-07-27 23:09:03 +0530 | [diff] [blame] | 2465 | private void WaitForAdapterChange(int state) { |
| 2466 | while (true) { |
| 2467 | if(mState == state) { |
| 2468 | break; |
| 2469 | } |
| 2470 | try { |
| 2471 | Thread.sleep(100); |
| 2472 | } catch (Exception e) { |
| 2473 | e.printStackTrace(); |
| 2474 | } |
| 2475 | } |
| 2476 | return; |
| 2477 | } |
| 2478 | |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 2479 | @Override |
| 2480 | public int mPOSSetReaderMode (String pkg, boolean on) { |
| 2481 | NfcService.this.enforceNfceeAdminPerm(pkg); |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 2482 | // Check if NFC is enabled |
| 2483 | if (!isNfcEnabled()) { |
| 2484 | return NxpConstants.MPOS_STATUS_REJECTED; |
| 2485 | } |
| 2486 | |
| 2487 | synchronized(NfcService.this) { |
| 2488 | int status = mDeviceHost.mposSetReaderMode(on); |
| 2489 | if(!on) { |
| 2490 | if(nci_version != NCI_VERSION_2_0) { |
| 2491 | applyRouting(true); |
| 2492 | } else if(mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED |
| 2493 | || mNfcUnlockManager.isLockscreenPollingEnabled()) { |
| 2494 | applyRouting(false); |
| 2495 | } |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 2496 | } |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 2497 | return status; |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | @Override |
| 2502 | public boolean mPOSGetReaderMode (String pkg) { |
| 2503 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 2504 | // Check if NFC is enabled |
| 2505 | if (!isNfcEnabled()) { |
| 2506 | return false; |
| 2507 | } |
| 2508 | |
| 2509 | boolean status = false; |
| 2510 | synchronized(NfcService.this) { |
| 2511 | status = mDeviceHost.mposGetReaderMode(); |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 2512 | } |
| 2513 | return status; |
| 2514 | } |
| 2515 | |
| 2516 | @Override |
| 2517 | public void stopPoll(String pkg, int mode) { |
| 2518 | NfcService.this.enforceNfceeAdminPerm(pkg); |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 2519 | // Check if NFC is enabled |
| 2520 | if (!isNfcEnabled()) { |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | synchronized(NfcService.this) { |
| 2525 | mDeviceHost.stopPoll(mode); |
| 2526 | } |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | @Override |
| 2530 | public void startPoll(String pkg) { |
| 2531 | NfcService.this.enforceNfceeAdminPerm(pkg); |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 2532 | // Check if NFC is enabled |
| 2533 | if (!isNfcEnabled()) { |
| 2534 | return; |
| 2535 | } |
| 2536 | |
| 2537 | synchronized(NfcService.this) { |
| 2538 | mDeviceHost.startPoll(); |
| 2539 | } |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 2540 | } |
Ganesh Deva | 876c65b | 2018-06-01 18:51:30 +0530 | [diff] [blame] | 2541 | |
| 2542 | @Override |
nxf38293 | cd49562 | 2018-06-29 10:25:10 +0530 | [diff] [blame] | 2543 | public int nfcSelfTest(String pkg, int type) { |
| 2544 | int status = 0xFF; |
| 2545 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 2546 | |
| 2547 | synchronized(NfcService.this) { |
| 2548 | status = mDeviceHost.doNfcSelfTest(type); |
| 2549 | } |
| 2550 | return status; |
| 2551 | } |
| 2552 | |
| 2553 | @Override |
Ganesh Deva | 876c65b | 2018-06-01 18:51:30 +0530 | [diff] [blame] | 2554 | public int getSelectedUicc() throws RemoteException { |
| 2555 | if (!isNfcEnabled()) { |
| 2556 | throw new RemoteException("NFC is not enabled"); |
| 2557 | } |
| 2558 | return mDeviceHost.doGetSelectedUicc(); |
| 2559 | } |
| 2560 | |
| 2561 | @Override |
| 2562 | public int selectUicc(int uiccSlot) throws RemoteException { |
| 2563 | synchronized(NfcService.this) { |
| 2564 | if (!isNfcEnabled()) { |
| 2565 | throw new RemoteException("NFC is not enabled"); |
| 2566 | } |
| 2567 | int status = mDeviceHost.doselectUicc(uiccSlot); |
| 2568 | Log.i(TAG, "Update routing table"); |
| 2569 | /*In case of UICC connected and Enabled or Removed , |
| 2570 | *Reconfigure the routing table based on current UICC parameters |
| 2571 | **/ |
| 2572 | if((status == 0x00)||(status == 0x01)) |
| 2573 | { |
| 2574 | mPrefsEditor.putInt(PREF_CUR_SELECTED_UICC_ID, uiccSlot); |
| 2575 | mPrefsEditor.apply(); |
| 2576 | if((mAidRoutingManager != null) && (mCardEmulationManager != null)) |
| 2577 | { |
| 2578 | Log.i(TAG, "Update routing table"); |
| 2579 | mAidRoutingManager.onNfccRoutingTableCleared(); |
| 2580 | mIsRoutingTableDirty = true; |
| 2581 | mCardEmulationManager.onNfcEnabled(); |
| 2582 | } |
| 2583 | else |
| 2584 | { |
| 2585 | Log.i(TAG, "Update only Mifare and Desfire route"); |
| 2586 | mIsRoutingTableDirty = true; |
| 2587 | applyRouting(false); |
| 2588 | } |
| 2589 | } |
| 2590 | return status; |
| 2591 | } |
| 2592 | } |
| 2593 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | final class ReaderModeDeathRecipient implements IBinder.DeathRecipient { |
| 2597 | @Override |
| 2598 | public void binderDied() { |
| 2599 | synchronized (NfcService.this) { |
| 2600 | if (mReaderModeParams != null) { |
| 2601 | mReaderModeParams = null; |
| 2602 | Log.e(TAG, "applyRouting -5"); |
| 2603 | applyRouting(false); |
| 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | final class TagService extends INfcTag.Stub { |
| 2610 | @Override |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2611 | public int connect(int nativeHandle, int technology) throws RemoteException { |
| 2612 | NfcPermissions.enforceUserPermissions(mContext); |
| 2613 | |
| 2614 | TagEndpoint tag = null; |
| 2615 | |
| 2616 | if (!isNfcEnabled()) { |
| 2617 | return ErrorCodes.ERROR_NOT_INITIALIZED; |
| 2618 | } |
| 2619 | |
| 2620 | /* find the tag in the hmap */ |
| 2621 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2622 | if (tag == null) { |
| 2623 | return ErrorCodes.ERROR_DISCONNECT; |
| 2624 | } |
| 2625 | |
| 2626 | if (!tag.isPresent()) { |
| 2627 | return ErrorCodes.ERROR_DISCONNECT; |
| 2628 | } |
| 2629 | |
| 2630 | // Note that on most tags, all technologies are behind a single |
| 2631 | // handle. This means that the connect at the lower levels |
| 2632 | // will do nothing, as the tag is already connected to that handle. |
| 2633 | if (tag.connect(technology)) { |
| 2634 | return ErrorCodes.SUCCESS; |
| 2635 | } else { |
| 2636 | return ErrorCodes.ERROR_DISCONNECT; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | @Override |
| 2641 | public int reconnect(int nativeHandle) throws RemoteException { |
| 2642 | NfcPermissions.enforceUserPermissions(mContext); |
| 2643 | |
| 2644 | TagEndpoint tag = null; |
| 2645 | |
| 2646 | // Check if NFC is enabled |
| 2647 | if (!isNfcEnabled()) { |
| 2648 | return ErrorCodes.ERROR_NOT_INITIALIZED; |
| 2649 | } |
| 2650 | |
| 2651 | /* find the tag in the hmap */ |
| 2652 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2653 | if (tag != null) { |
| 2654 | if (tag.reconnect()) { |
| 2655 | return ErrorCodes.SUCCESS; |
| 2656 | } else { |
| 2657 | return ErrorCodes.ERROR_DISCONNECT; |
| 2658 | } |
| 2659 | } |
| 2660 | return ErrorCodes.ERROR_DISCONNECT; |
| 2661 | } |
| 2662 | |
| 2663 | @Override |
| 2664 | public int[] getTechList(int nativeHandle) throws RemoteException { |
| 2665 | NfcPermissions.enforceUserPermissions(mContext); |
| 2666 | |
| 2667 | // Check if NFC is enabled |
| 2668 | if (!isNfcEnabled()) { |
| 2669 | return null; |
| 2670 | } |
| 2671 | |
| 2672 | /* find the tag in the hmap */ |
| 2673 | TagEndpoint tag = (TagEndpoint) findObject(nativeHandle); |
| 2674 | if (tag != null) { |
| 2675 | return tag.getTechList(); |
| 2676 | } |
| 2677 | return null; |
| 2678 | } |
| 2679 | |
| 2680 | @Override |
| 2681 | public boolean isPresent(int nativeHandle) throws RemoteException { |
| 2682 | TagEndpoint tag = null; |
| 2683 | |
| 2684 | // Check if NFC is enabled |
| 2685 | if (!isNfcEnabled()) { |
| 2686 | return false; |
| 2687 | } |
| 2688 | |
| 2689 | /* find the tag in the hmap */ |
| 2690 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2691 | if (tag == null) { |
| 2692 | return false; |
| 2693 | } |
| 2694 | |
| 2695 | return tag.isPresent(); |
| 2696 | } |
| 2697 | |
| 2698 | @Override |
| 2699 | public boolean isNdef(int nativeHandle) throws RemoteException { |
| 2700 | NfcPermissions.enforceUserPermissions(mContext); |
| 2701 | |
| 2702 | TagEndpoint tag = null; |
| 2703 | |
| 2704 | // Check if NFC is enabled |
| 2705 | if (!isNfcEnabled()) { |
| 2706 | return false; |
| 2707 | } |
| 2708 | |
| 2709 | /* find the tag in the hmap */ |
| 2710 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2711 | int[] ndefInfo = new int[2]; |
| 2712 | if (tag == null) { |
| 2713 | return false; |
| 2714 | } |
| 2715 | return tag.checkNdef(ndefInfo); |
| 2716 | } |
| 2717 | |
| 2718 | @Override |
| 2719 | public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw) |
| 2720 | throws RemoteException { |
| 2721 | NfcPermissions.enforceUserPermissions(mContext); |
| 2722 | |
| 2723 | TagEndpoint tag = null; |
| 2724 | byte[] response; |
| 2725 | |
| 2726 | // Check if NFC is enabled |
| 2727 | if (!isNfcEnabled()) { |
| 2728 | return null; |
| 2729 | } |
| 2730 | |
| 2731 | /* find the tag in the hmap */ |
| 2732 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2733 | if (tag != null) { |
| 2734 | // Check if length is within limits |
| 2735 | if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) { |
| 2736 | return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null); |
| 2737 | } |
| 2738 | int[] targetLost = new int[1]; |
| 2739 | response = tag.transceive(data, raw, targetLost); |
| 2740 | int result; |
| 2741 | if (response != null) { |
| 2742 | result = TransceiveResult.RESULT_SUCCESS; |
| 2743 | } else if (targetLost[0] == 1) { |
| 2744 | result = TransceiveResult.RESULT_TAGLOST; |
| 2745 | } else { |
| 2746 | result = TransceiveResult.RESULT_FAILURE; |
| 2747 | } |
| 2748 | return new TransceiveResult(result, response); |
| 2749 | } |
| 2750 | return null; |
| 2751 | } |
| 2752 | |
| 2753 | @Override |
| 2754 | public NdefMessage ndefRead(int nativeHandle) throws RemoteException { |
| 2755 | NfcPermissions.enforceUserPermissions(mContext); |
| 2756 | |
| 2757 | TagEndpoint tag; |
| 2758 | |
| 2759 | // Check if NFC is enabled |
| 2760 | if (!isNfcEnabled()) { |
| 2761 | return null; |
| 2762 | } |
| 2763 | |
| 2764 | /* find the tag in the hmap */ |
| 2765 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2766 | if (tag != null) { |
| 2767 | byte[] buf = tag.readNdef(); |
| 2768 | if (buf == null) { |
| 2769 | return null; |
| 2770 | } |
| 2771 | |
| 2772 | /* Create an NdefMessage */ |
| 2773 | try { |
| 2774 | return new NdefMessage(buf); |
| 2775 | } catch (FormatException e) { |
| 2776 | return null; |
| 2777 | } |
| 2778 | } |
| 2779 | return null; |
| 2780 | } |
| 2781 | |
| 2782 | @Override |
| 2783 | public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException { |
| 2784 | NfcPermissions.enforceUserPermissions(mContext); |
| 2785 | |
| 2786 | TagEndpoint tag; |
| 2787 | |
| 2788 | // Check if NFC is enabled |
| 2789 | if (!isNfcEnabled()) { |
| 2790 | return ErrorCodes.ERROR_NOT_INITIALIZED; |
| 2791 | } |
| 2792 | |
| 2793 | /* find the tag in the hmap */ |
| 2794 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2795 | if (tag == null) { |
| 2796 | return ErrorCodes.ERROR_IO; |
| 2797 | } |
| 2798 | |
| 2799 | if (msg == null) return ErrorCodes.ERROR_INVALID_PARAM; |
| 2800 | |
| 2801 | if (tag.writeNdef(msg.toByteArray())) { |
| 2802 | return ErrorCodes.SUCCESS; |
| 2803 | } else { |
| 2804 | return ErrorCodes.ERROR_IO; |
| 2805 | } |
| 2806 | |
| 2807 | } |
| 2808 | |
| 2809 | @Override |
| 2810 | public boolean ndefIsWritable(int nativeHandle) throws RemoteException { |
| 2811 | throw new UnsupportedOperationException(); |
| 2812 | } |
| 2813 | |
| 2814 | @Override |
| 2815 | public int ndefMakeReadOnly(int nativeHandle) throws RemoteException { |
| 2816 | NfcPermissions.enforceUserPermissions(mContext); |
| 2817 | |
| 2818 | TagEndpoint tag; |
| 2819 | |
| 2820 | // Check if NFC is enabled |
| 2821 | if (!isNfcEnabled()) { |
| 2822 | return ErrorCodes.ERROR_NOT_INITIALIZED; |
| 2823 | } |
| 2824 | |
| 2825 | /* find the tag in the hmap */ |
| 2826 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2827 | if (tag == null) { |
| 2828 | return ErrorCodes.ERROR_IO; |
| 2829 | } |
| 2830 | |
| 2831 | if (tag.makeReadOnly()) { |
| 2832 | return ErrorCodes.SUCCESS; |
| 2833 | } else { |
| 2834 | return ErrorCodes.ERROR_IO; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | @Override |
| 2839 | public int formatNdef(int nativeHandle, byte[] key) throws RemoteException { |
| 2840 | NfcPermissions.enforceUserPermissions(mContext); |
| 2841 | |
| 2842 | TagEndpoint tag; |
| 2843 | |
| 2844 | // Check if NFC is enabled |
| 2845 | if (!isNfcEnabled()) { |
| 2846 | return ErrorCodes.ERROR_NOT_INITIALIZED; |
| 2847 | } |
| 2848 | |
| 2849 | /* find the tag in the hmap */ |
| 2850 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2851 | if (tag == null) { |
| 2852 | return ErrorCodes.ERROR_IO; |
| 2853 | } |
| 2854 | |
| 2855 | if (tag.formatNdef(key)) { |
| 2856 | return ErrorCodes.SUCCESS; |
| 2857 | } else { |
| 2858 | return ErrorCodes.ERROR_IO; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | @Override |
| 2863 | public Tag rediscover(int nativeHandle) throws RemoteException { |
| 2864 | NfcPermissions.enforceUserPermissions(mContext); |
| 2865 | |
| 2866 | TagEndpoint tag = null; |
| 2867 | |
| 2868 | // Check if NFC is enabled |
| 2869 | if (!isNfcEnabled()) { |
| 2870 | return null; |
| 2871 | } |
| 2872 | |
| 2873 | /* find the tag in the hmap */ |
| 2874 | tag = (TagEndpoint) findObject(nativeHandle); |
| 2875 | if (tag != null) { |
| 2876 | // For now the prime usecase for rediscover() is to be able |
| 2877 | // to access the NDEF technology after formatting without |
| 2878 | // having to remove the tag from the field, or similar |
| 2879 | // to have access to NdefFormatable in case low-level commands |
| 2880 | // were used to remove NDEF. So instead of doing a full stack |
| 2881 | // rediscover (which is poorly supported at the moment anyway), |
| 2882 | // we simply remove these two technologies and detect them |
| 2883 | // again. |
| 2884 | tag.removeTechnology(TagTechnology.NDEF); |
| 2885 | tag.removeTechnology(TagTechnology.NDEF_FORMATABLE); |
| 2886 | tag.findAndReadNdef(); |
| 2887 | // Build a new Tag object to return |
| 2888 | Tag newTag = new Tag(tag.getUid(), tag.getTechList(), |
| 2889 | tag.getTechExtras(), tag.getHandle(), this); |
| 2890 | return newTag; |
| 2891 | } |
| 2892 | return null; |
| 2893 | } |
| 2894 | |
| 2895 | @Override |
| 2896 | public int setTimeout(int tech, int timeout) throws RemoteException { |
| 2897 | NfcPermissions.enforceUserPermissions(mContext); |
| 2898 | boolean success = mDeviceHost.setTimeout(tech, timeout); |
| 2899 | if (success) { |
| 2900 | return ErrorCodes.SUCCESS; |
| 2901 | } else { |
| 2902 | return ErrorCodes.ERROR_INVALID_PARAM; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | @Override |
| 2907 | public int getTimeout(int tech) throws RemoteException { |
| 2908 | NfcPermissions.enforceUserPermissions(mContext); |
| 2909 | |
| 2910 | return mDeviceHost.getTimeout(tech); |
| 2911 | } |
| 2912 | |
| 2913 | @Override |
| 2914 | public void resetTimeouts() throws RemoteException { |
| 2915 | NfcPermissions.enforceUserPermissions(mContext); |
| 2916 | |
| 2917 | mDeviceHost.resetTimeouts(); |
| 2918 | } |
| 2919 | |
| 2920 | @Override |
| 2921 | public boolean canMakeReadOnly(int ndefType) throws RemoteException { |
| 2922 | return mDeviceHost.canMakeReadOnly(ndefType); |
| 2923 | } |
| 2924 | |
| 2925 | @Override |
| 2926 | public int getMaxTransceiveLength(int tech) throws RemoteException { |
| 2927 | return mDeviceHost.getMaxTransceiveLength(tech); |
| 2928 | } |
| 2929 | |
| 2930 | @Override |
| 2931 | public boolean getExtendedLengthApdusSupported() throws RemoteException { |
| 2932 | return mDeviceHost.getExtendedLengthApdusSupported(); |
| 2933 | } |
| 2934 | } |
| 2935 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 2936 | final class NfcDtaService extends INfcDta.Stub { |
| 2937 | public void enableDta() throws RemoteException { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2938 | NfcPermissions.enforceAdminPermissions(mContext); |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 2939 | if(!sIsDtaMode) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2940 | mDeviceHost.enableDtaMode(); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2941 | sIsDtaMode = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2942 | Log.d(TAG, "DTA Mode is Enabled "); |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | public void disableDta() throws RemoteException { |
| 2947 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2948 | if(sIsDtaMode) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2949 | mDeviceHost.disableDtaMode(); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 2950 | sIsDtaMode = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2951 | } |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | public boolean enableServer(String serviceName, int serviceSap, int miu, |
| 2955 | int rwSize,int testCaseId) throws RemoteException { |
| 2956 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2957 | |
| 2958 | if(serviceName.equals(null)) |
| 2959 | return false; |
| 2960 | |
| 2961 | mP2pLinkManager.enableExtDtaSnepServer(serviceName, serviceSap, miu, rwSize,testCaseId); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 2962 | return true; |
| 2963 | } |
| 2964 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 2965 | public void disableServer() throws RemoteException { |
| 2966 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2967 | mP2pLinkManager.disableExtDtaSnepServer(); |
| 2968 | } |
| 2969 | |
| 2970 | public boolean enableClient(String serviceName, int miu, int rwSize, |
| 2971 | int testCaseId) throws RemoteException { |
| 2972 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2973 | |
| 2974 | if(testCaseId == 0) |
| 2975 | return false; |
| 2976 | |
| 2977 | if (testCaseId>20){ |
| 2978 | sIsShortRecordLayout=true; |
| 2979 | testCaseId=testCaseId-20; |
| 2980 | } else { |
| 2981 | sIsShortRecordLayout=false; |
| 2982 | } |
| 2983 | Log.d("testCaseId", ""+testCaseId); |
| 2984 | mP2pLinkManager.enableDtaSnepClient(serviceName, miu, rwSize, testCaseId); |
| 2985 | return true; |
| 2986 | } |
| 2987 | |
| 2988 | public void disableClient() throws RemoteException { |
| 2989 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2990 | mP2pLinkManager.disableDtaSnepClient(); |
| 2991 | } |
| 2992 | |
| 2993 | public boolean registerMessageService(String msgServiceName) |
| 2994 | throws RemoteException { |
| 2995 | NfcPermissions.enforceAdminPermissions(mContext); |
| 2996 | if(msgServiceName.equals(null)) |
| 2997 | return false; |
| 2998 | |
| 2999 | DtaServiceConnector.setMessageService(msgServiceName); |
| 3000 | return true; |
| 3001 | } |
| 3002 | }; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3003 | |
| 3004 | final class NfcVzwService extends INfcVzw.Stub { |
| 3005 | @Override |
| 3006 | public void setScreenOffCondition(boolean enable) throws RemoteException { |
| 3007 | |
| 3008 | Message msg = mHandler.obtainMessage(); |
| 3009 | msg.what=MSG_SET_SCREEN_STATE; |
| 3010 | msg.arg1= (enable)?1:0; |
| 3011 | mHandler.sendMessage(msg); |
| 3012 | |
| 3013 | } |
| 3014 | |
| 3015 | @Override |
| 3016 | public boolean setVzwAidList(RouteEntry[] entries) |
| 3017 | throws RemoteException { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3018 | Log.i(TAG, "setVzwAidList enter"); |
| 3019 | Log.i(TAG, "setVzwAidList entries length =" + entries.length); |
| 3020 | if (mIsHceCapable) { |
| 3021 | mAidRoutingManager.ClearVzwCache(); |
| 3022 | for (int i = 0; i < entries.length; i++) { |
| 3023 | RouteEntry routeEntry = entries[i]; |
| 3024 | mAidRoutingManager.UpdateVzwCache(routeEntry.getAid(), |
| 3025 | routeEntry.getLocation(), routeEntry.getPowerState(), |
| 3026 | routeEntry.isAllowed()); |
| 3027 | |
| 3028 | Log.i(TAG, |
| 3029 | "AID" + routeEntry.getAid() + "Location " |
| 3030 | + routeEntry.getLocation() + "powerstate " |
| 3031 | + routeEntry.getPowerState()); |
| 3032 | } |
| 3033 | mAidRoutingManager.onNfccRoutingTableCleared(); |
| 3034 | mCardEmulationManager.onRoutingTableChanged(); |
| 3035 | return true; |
| 3036 | } else { |
| 3037 | return false; |
| 3038 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | }; |
| 3042 | |
| 3043 | void _nfcEeClose(int callingPid, IBinder binder) throws IOException { |
| 3044 | // Blocks until a pending open() or transceive() times out. |
| 3045 | //TODO: This is incorrect behavior - the close should interrupt pending |
| 3046 | // operations. However this is not supported by current hardware. |
| 3047 | |
| 3048 | synchronized (NfcService.this) { |
| 3049 | if (!isNfcEnabledOrShuttingDown()) { |
| 3050 | throw new IOException("NFC adapter is disabled"); |
| 3051 | } |
| 3052 | if (mOpenEe == null) { |
| 3053 | throw new IOException("NFC EE closed"); |
| 3054 | } |
| 3055 | if (callingPid != -1 && callingPid != mOpenEe.pid) { |
| 3056 | throw new SecurityException("Wrong PID"); |
| 3057 | } |
| 3058 | if (mOpenEe.binder != binder) { |
| 3059 | throw new SecurityException("Wrong binder handle"); |
| 3060 | } |
| 3061 | |
| 3062 | binder.unlinkToDeath(mOpenEe, 0); |
| 3063 | mDeviceHost.resetTimeouts(); |
| 3064 | doDisconnect(mOpenEe.handle); |
| 3065 | mOpenEe = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | boolean _nfcEeReset() throws IOException { |
| 3070 | synchronized (NfcService.this) { |
| 3071 | if (!isNfcEnabledOrShuttingDown()) { |
| 3072 | throw new IOException("NFC adapter is disabled"); |
| 3073 | } |
| 3074 | if (mOpenEe == null) { |
| 3075 | throw new IOException("NFC EE closed"); |
| 3076 | } |
| 3077 | return mSecureElement.doReset(mOpenEe.handle); |
| 3078 | } |
| 3079 | } |
| 3080 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3081 | final class NfcAdapterExtrasService extends INfcAdapterExtras.Stub { |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 3082 | ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3083 | private Bundle writeNoException() { |
| 3084 | Bundle p = new Bundle(); |
| 3085 | p.putInt("e", 0); |
| 3086 | return p; |
| 3087 | } |
| 3088 | |
| 3089 | private Bundle writeEeException(int exceptionType, String message) { |
| 3090 | Bundle p = new Bundle(); |
| 3091 | p.putInt("e", exceptionType); |
| 3092 | p.putString("m", message); |
| 3093 | return p; |
| 3094 | } |
| 3095 | |
| 3096 | @Override |
| 3097 | public Bundle open(String pkg, IBinder b) throws RemoteException { |
| 3098 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3099 | |
| 3100 | Bundle result; |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 3101 | if (activityManager.isLowRamDevice()) { |
| 3102 | result = writeEeException(SE_ACCESS_DENIED, "SE open access denied."); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3103 | } else { |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 3104 | int handle = _open(b); |
| 3105 | if (handle < 0) { |
| 3106 | result = writeEeException(handle, "NFCEE open exception."); |
| 3107 | } else { |
| 3108 | result = writeNoException(); |
| 3109 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3110 | } |
| 3111 | return result; |
| 3112 | } |
| 3113 | |
| 3114 | /** |
| 3115 | * Opens a connection to the secure element. |
| 3116 | * |
| 3117 | * @return A handle with a value >= 0 in case of success, or a |
| 3118 | * negative value in case of failure. |
| 3119 | */ |
| 3120 | private int _open(IBinder b) { |
| 3121 | synchronized(NfcService.this) { |
| 3122 | if (!isNfcEnabled()) { |
| 3123 | return EE_ERROR_NFC_DISABLED; |
| 3124 | } |
| 3125 | if (mInProvisionMode) { |
| 3126 | // Deny access to the NFCEE as long as the device is being setup |
| 3127 | return EE_ERROR_IO; |
| 3128 | } |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3129 | /*Concurrent access for DWP transactions to be allowed even when P2P is already ongoing */ |
| 3130 | /* |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3131 | if (mP2pLinkManager.isLlcpActive()) { |
| 3132 | // Don't allow PN544-based devices to open the SE while the LLCP |
| 3133 | // link is still up or in a debounce state. This avoids race |
| 3134 | // conditions in the NXP stack around P2P/SMX switching. |
| 3135 | return EE_ERROR_EXT_FIELD; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3136 | }*/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3137 | if (mOpenEe != null) { |
| 3138 | Log.i(TAG, "SE is Busy. returning.."); |
| 3139 | return EE_ERROR_ALREADY_OPEN; |
| 3140 | } |
| 3141 | boolean restorePolling = false; |
| 3142 | if (mNfcPollingEnabled) { |
| 3143 | // Disable polling for tags/P2P when connecting to the SMX |
| 3144 | // on PN544-based devices. Whenever nfceeClose is called, |
| 3145 | // the polling configuration will be restored. |
| 3146 | mDeviceHost.disableDiscovery(); |
| 3147 | mNfcPollingEnabled = false; |
| 3148 | restorePolling = true; |
| 3149 | } |
| 3150 | |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3151 | int handle = doOpenSecureElementConnection(0xF3); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3152 | if (handle < 0) { |
| 3153 | |
| 3154 | if (restorePolling) { |
| 3155 | mDeviceHost.enableDiscovery(mCurrentDiscoveryParameters, true); |
| 3156 | mNfcPollingEnabled = true; |
| 3157 | } |
| 3158 | return handle; |
| 3159 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3160 | mOpenEe = new OpenSecureElement(getCallingPid(), handle, b); |
| 3161 | try { |
| 3162 | b.linkToDeath(mOpenEe, 0); |
| 3163 | } catch (RemoteException e) { |
| 3164 | mOpenEe.binderDied(); |
| 3165 | } |
| 3166 | |
| 3167 | // Add the calling package to the list of packages that have accessed |
| 3168 | // the secure element. |
| 3169 | for (String packageName : mContext.getPackageManager().getPackagesForUid(getCallingUid())) { |
| 3170 | mSePackages.add(packageName); |
| 3171 | } |
| 3172 | |
| 3173 | return handle; |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | @Override |
| 3178 | public Bundle close(String pkg, IBinder binder) throws RemoteException { |
| 3179 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3180 | |
| 3181 | Bundle result; |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 3182 | |
| 3183 | if (activityManager.isLowRamDevice()) { |
| 3184 | result = writeEeException(SE_ACCESS_DENIED, "SE close access denied."); |
| 3185 | } else { |
| 3186 | try { |
| 3187 | _nfcEeClose(getCallingPid(), binder); |
| 3188 | result = writeNoException(); |
| 3189 | } catch (IOException e) { |
| 3190 | result = writeEeException(EE_ERROR_IO, e.getMessage()); |
| 3191 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3192 | } |
| 3193 | return result; |
| 3194 | } |
| 3195 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3196 | @Override |
| 3197 | public Bundle transceive(String pkg, byte[] in) throws RemoteException { |
| 3198 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3199 | |
| 3200 | Bundle result; |
Nikhil Chhabra | efd6b09 | 2018-04-11 15:28:06 +0530 | [diff] [blame] | 3201 | |
| 3202 | if (activityManager.isLowRamDevice()) { |
| 3203 | result = writeEeException(SE_ACCESS_DENIED, "SE transceive access denied."); |
| 3204 | } else { |
| 3205 | byte[] out; |
| 3206 | try { |
| 3207 | out = _transceive(in); |
| 3208 | result = writeNoException(); |
| 3209 | result.putByteArray("out", out); |
| 3210 | } catch (IOException e) { |
| 3211 | result = writeEeException(EE_ERROR_IO, e.getMessage()); |
| 3212 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3213 | } |
| 3214 | return result; |
| 3215 | } |
| 3216 | |
| 3217 | private byte[] _transceive(byte[] data) throws IOException { |
| 3218 | synchronized(NfcService.this) { |
| 3219 | if (!isNfcEnabled()) { |
| 3220 | throw new IOException("NFC is not enabled"); |
| 3221 | } |
| 3222 | if (mOpenEe == null) { |
| 3223 | throw new IOException("NFC EE is not open"); |
| 3224 | } |
| 3225 | if (getCallingPid() != mOpenEe.pid) { |
| 3226 | throw new SecurityException("Wrong PID"); |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | return doTransceive(mOpenEe.handle, data); |
| 3231 | } |
| 3232 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3233 | @Override |
| 3234 | public int getCardEmulationRoute(String pkg) throws RemoteException { |
| 3235 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3236 | return mEeRoutingState; |
| 3237 | } |
| 3238 | |
| 3239 | @Override |
| 3240 | public void setCardEmulationRoute(String pkg, int route) throws RemoteException { |
| 3241 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3242 | mEeRoutingState = route; |
| 3243 | ApplyRoutingTask applyRoutingTask = new ApplyRoutingTask(); |
| 3244 | applyRoutingTask.execute(); |
| 3245 | try { |
| 3246 | // Block until route is set |
| 3247 | applyRoutingTask.get(); |
| 3248 | } catch (ExecutionException e) { |
| 3249 | Log.e(TAG, "failed to set card emulation mode"); |
| 3250 | } catch (InterruptedException e) { |
| 3251 | Log.e(TAG, "failed to set card emulation mode"); |
| 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | @Override |
| 3256 | public void authenticate(String pkg, byte[] token) throws RemoteException { |
| 3257 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3258 | } |
| 3259 | |
| 3260 | @Override |
| 3261 | public String getDriverName(String pkg) throws RemoteException { |
| 3262 | NfcService.this.enforceNfceeAdminPerm(pkg); |
| 3263 | return mDeviceHost.getName(); |
| 3264 | } |
| 3265 | |
| 3266 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3267 | |
| 3268 | /** resources kept while secure element is open */ |
| 3269 | private class OpenSecureElement implements IBinder.DeathRecipient { |
| 3270 | public int pid; // pid that opened SE |
| 3271 | // binder handle used for DeathReceipient. Must keep |
| 3272 | // a reference to this, otherwise it can get GC'd and |
| 3273 | // the binder stub code might create a different BinderProxy |
| 3274 | // for the same remote IBinder, causing mismatched |
| 3275 | // link()/unlink() |
| 3276 | public IBinder binder; |
| 3277 | public int handle; // low-level handle |
| 3278 | public OpenSecureElement(int pid, int handle, IBinder binder) { |
| 3279 | this.pid = pid; |
| 3280 | this.handle = handle; |
| 3281 | this.binder = binder; |
| 3282 | } |
| 3283 | @Override |
| 3284 | public void binderDied() { |
| 3285 | synchronized (NfcService.this) { |
| 3286 | Log.i(TAG, "Tracked app " + pid + " died"); |
| 3287 | pid = -1; |
| 3288 | try { |
| 3289 | _nfcEeClose(-1, binder); |
| 3290 | } catch (IOException e) { /* already closed */ } |
| 3291 | } |
| 3292 | } |
| 3293 | @Override |
| 3294 | public String toString() { |
| 3295 | return new StringBuilder('@').append(Integer.toHexString(hashCode())).append("[pid=") |
| 3296 | .append(pid).append(" handle=").append(handle).append("]").toString(); |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | boolean isNfcEnabledOrShuttingDown() { |
| 3301 | synchronized (this) { |
| 3302 | return (mState == NfcAdapter.STATE_ON || mState == NfcAdapter.STATE_TURNING_OFF); |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | boolean isNfcEnabled() { |
| 3307 | synchronized (this) { |
| 3308 | return mState == NfcAdapter.STATE_ON; |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | class WatchDogThread extends Thread { |
| 3313 | final Object mCancelWaiter = new Object(); |
| 3314 | final int mTimeout; |
| 3315 | boolean mCanceled = false; |
| 3316 | |
| 3317 | public WatchDogThread(String threadName, int timeout) { |
| 3318 | super(threadName); |
| 3319 | mTimeout = timeout; |
| 3320 | } |
| 3321 | |
| 3322 | @Override |
| 3323 | public void run() { |
| 3324 | try { |
| 3325 | synchronized (mCancelWaiter) { |
| 3326 | mCancelWaiter.wait(mTimeout); |
| 3327 | if (mCanceled) { |
| 3328 | return; |
| 3329 | } |
| 3330 | } |
| 3331 | } catch (InterruptedException e) { |
| 3332 | // Should not happen; fall-through to abort. |
| 3333 | Log.w(TAG, "Watchdog thread interruped."); |
| 3334 | interrupt(); |
| 3335 | } |
| 3336 | Log.e(TAG, "Watchdog triggered, aborting."); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3337 | mDeviceHost.doAbort(getName()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | public synchronized void cancel() { |
| 3341 | synchronized (mCancelWaiter) { |
| 3342 | mCanceled = true; |
| 3343 | mCancelWaiter.notify(); |
| 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | /* For Toast from background process*/ |
| 3349 | |
| 3350 | public class ToastHandler |
| 3351 | { |
| 3352 | // General attributes |
| 3353 | private Context mContext; |
| 3354 | private Handler mHandler; |
| 3355 | |
| 3356 | public ToastHandler(Context _context) |
| 3357 | { |
| 3358 | this.mContext = _context; |
| 3359 | this.mHandler = new Handler(); |
| 3360 | } |
| 3361 | |
| 3362 | /** |
| 3363 | * Runs the <code>Runnable</code> in a separate <code>Thread</code>. |
| 3364 | * |
| 3365 | * @param _runnable |
| 3366 | * The <code>Runnable</code> containing the <code>Toast</code> |
| 3367 | */ |
| 3368 | private void runRunnable(final Runnable _runnable) |
| 3369 | { |
| 3370 | Thread thread = new Thread() |
| 3371 | { |
| 3372 | public void run() |
| 3373 | { |
| 3374 | mHandler.post(_runnable); |
| 3375 | } |
| 3376 | }; |
| 3377 | |
| 3378 | thread.start(); |
| 3379 | thread.interrupt(); |
| 3380 | thread = null; |
| 3381 | } |
| 3382 | |
| 3383 | public void showToast(final CharSequence _text, final int _duration) |
| 3384 | { |
| 3385 | final Runnable runnable = new Runnable() |
| 3386 | { |
| 3387 | @Override |
| 3388 | public void run() |
| 3389 | { |
| 3390 | Toast.makeText(mContext, _text, _duration).show(); |
| 3391 | } |
| 3392 | }; |
| 3393 | |
| 3394 | runRunnable(runnable); |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | static byte[] hexStringToBytes(String s) { |
| 3399 | if (s == null || s.length() == 0) return null; |
| 3400 | int len = s.length(); |
| 3401 | if (len % 2 != 0) { |
| 3402 | s = '0' + s; |
| 3403 | len++; |
| 3404 | } |
| 3405 | byte[] data = new byte[len / 2]; |
| 3406 | for (int i = 0; i < len; i += 2) { |
| 3407 | data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
| 3408 | + Character.digit(s.charAt(i + 1), 16)); |
| 3409 | } |
| 3410 | return data; |
| 3411 | } |
| 3412 | |
| 3413 | static String toHexString(byte[] buffer, int offset, int length) { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3414 | final char[] hexChars = "0123456789abcdef".toCharArray(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3415 | char[] chars = new char[2 * length]; |
| 3416 | for (int j = offset; j < offset + length; ++j) { |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3417 | chars[2 * (j-offset)] = hexChars[(buffer[j] & 0xF0) >>> 4]; |
| 3418 | chars[2 * (j-offset) + 1] = hexChars[buffer[j] & 0x0F]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3419 | } |
| 3420 | return new String(chars); |
| 3421 | } |
| 3422 | |
| 3423 | /** |
| 3424 | * Read mScreenState and apply NFC-C polling and NFC-EE routing |
| 3425 | */ |
| 3426 | void applyRouting(boolean force) { |
| 3427 | Log.d(TAG, "applyRouting - enter force = " + force + " mScreenState = " + mScreenState); |
| 3428 | |
| 3429 | synchronized (this) { |
| 3430 | //Since Reader mode during wired mode is supported |
| 3431 | //enableDiscovery or disableDiscovery is allowed |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3432 | if (!isNfcEnabledOrShuttingDown()) { |
| 3433 | // PN544 cannot be reconfigured while EE is open |
| 3434 | return; |
| 3435 | } |
| 3436 | WatchDogThread watchDog = new WatchDogThread("applyRouting", ROUTING_WATCHDOG_MS); |
| 3437 | if (mInProvisionMode) { |
| 3438 | mInProvisionMode = Settings.Secure.getInt(mContentResolver, |
| 3439 | Settings.Global.DEVICE_PROVISIONED, 0) == 0; |
| 3440 | if (!mInProvisionMode) { |
| 3441 | // Notify dispatcher it's fine to dispatch to any package now |
| 3442 | // and allow handover transfers. |
| 3443 | mNfcDispatcher.disableProvisioningMode(); |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 3444 | /* if provision mode is disabled, then send this info to lower layers as well */ |
| 3445 | mDeviceHost.doSetProvisionMode(mInProvisionMode); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3446 | } |
| 3447 | } |
| 3448 | // Special case: if we're transitioning to unlocked state while |
| 3449 | // still talking to a tag, postpone re-configuration. |
| 3450 | if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED && isTagPresent()) { |
| 3451 | Log.d(TAG, "Not updating discovery parameters, tag connected."); |
| 3452 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RESUME_POLLING), |
| 3453 | APPLY_ROUTING_RETRY_TIMEOUT_MS); |
| 3454 | return; |
| 3455 | } |
| 3456 | |
| 3457 | try { |
| 3458 | watchDog.start(); |
| 3459 | // Compute new polling parameters |
| 3460 | NfcDiscoveryParameters newParams = computeDiscoveryParameters(mScreenState); |
| 3461 | if (force || !newParams.equals(mCurrentDiscoveryParameters)) { |
| 3462 | if (newParams.shouldEnableDiscovery()) { |
| 3463 | boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery(); |
| 3464 | mDeviceHost.enableDiscovery(newParams, shouldRestart); |
| 3465 | } else { |
| 3466 | mDeviceHost.disableDiscovery(); |
| 3467 | } |
| 3468 | mCurrentDiscoveryParameters = newParams; |
| 3469 | } else { |
| 3470 | Log.d(TAG, "Discovery configuration equal, not updating."); |
| 3471 | } |
| 3472 | } finally { |
| 3473 | watchDog.cancel(); |
| 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) { |
| 3479 | // Recompute discovery parameters based on screen state |
| 3480 | NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder(); |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 3481 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3482 | // Polling |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 3483 | if ((screenState >= NFC_POLLING_MODE)||mIsTaskBoot) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3484 | // Check if reader-mode is enabled |
| 3485 | if (mReaderModeParams != null) { |
| 3486 | int techMask = 0; |
| 3487 | if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_A) != 0) |
| 3488 | techMask |= NFC_POLL_A; |
| 3489 | if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_B) != 0) |
| 3490 | techMask |= NFC_POLL_B; |
| 3491 | if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_F) != 0) |
| 3492 | techMask |= NFC_POLL_F; |
| 3493 | if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_V) != 0) |
Nikhil Chhabra | 288edb0 | 2018-01-10 19:36:21 +0530 | [diff] [blame] | 3494 | techMask |= NFC_POLL_V; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3495 | if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_BARCODE) != 0) |
| 3496 | techMask |= NFC_POLL_KOVIO; |
| 3497 | |
| 3498 | paramsBuilder.setTechMask(techMask); |
| 3499 | paramsBuilder.setEnableReaderMode(true); |
| 3500 | } else { |
| 3501 | paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3502 | paramsBuilder.setEnableP2p(true); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3503 | } |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 3504 | } |
| 3505 | if ((screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED && mInProvisionMode) && |
| 3506 | !mNfcUnlockManager.isLockscreenPollingEnabled()) { |
| 3507 | if (mReaderModeParams != null) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3508 | paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT); |
| 3509 | // enable P2P for MFM/EDU/Corp provisioning |
| 3510 | paramsBuilder.setEnableP2p(true); |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 3511 | } else if ((screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED) && |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3512 | (mIsLiveCaseEnabled || mNfcUnlockManager.isLockscreenPollingEnabled())) { |
| 3513 | int techMask = 0; |
| 3514 | // enable polling for Live Case technologies |
| 3515 | if (mIsLiveCaseEnabled) |
| 3516 | techMask |= mLiveCaseTechnology; |
| 3517 | if (mNfcUnlockManager.isLockscreenPollingEnabled()) |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 3518 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3519 | techMask |= mNfcUnlockManager.getLockscreenPollMask(); |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 3520 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3521 | paramsBuilder.setTechMask(techMask); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3522 | paramsBuilder.setEnableLowPowerDiscovery(false); |
| 3523 | paramsBuilder.setEnableP2p(false); |
| 3524 | } |
| 3525 | |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 3526 | if (mIsHceCapable && mReaderModeParams == null) { |
Nikhil Chhabra | e1d07ba | 2018-01-10 11:33:17 +0530 | [diff] [blame] | 3527 | // Host routing is always enabled at lock screen or later, provided we aren't in reader mode |
| 3528 | paramsBuilder.setEnableHostRouting(true); |
| 3529 | } |
| 3530 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3531 | //To make routing table update. |
| 3532 | if(mIsRoutingTableDirty) { |
| 3533 | mIsRoutingTableDirty = false; |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 3534 | int protoRoute = mNxpPrefs.getInt("PREF_MIFARE_DESFIRE_PROTO_ROUTE_ID", GetDefaultMifareDesfireRouteEntry()); |
| 3535 | int defaultRoute=mNxpPrefs.getInt("PREF_SET_DEFAULT_ROUTE_ID", GetDefaultRouteEntry()); |
| 3536 | int techRoute=mNxpPrefs.getInt("PREF_MIFARE_CLT_ROUTE_ID", GetDefaultMifateCLTRouteEntry()); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3537 | if (DBG) Log.d(TAG, "Set default Route Entry"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3538 | setDefaultRoute(defaultRoute, protoRoute, techRoute); |
| 3539 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3540 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3541 | return paramsBuilder.build(); |
| 3542 | } |
| 3543 | |
| 3544 | private boolean isTagPresent() { |
| 3545 | for (Object object : mObjectMap.values()) { |
| 3546 | if (object instanceof TagEndpoint) { |
| 3547 | return ((TagEndpoint) object).isPresent(); |
| 3548 | } |
| 3549 | } |
| 3550 | return false; |
| 3551 | } |
Suhas Suresh | 5efc543 | 2018-04-27 15:31:02 +0530 | [diff] [blame] | 3552 | |
| 3553 | private void StopPresenceChecking() { |
| 3554 | Object[] objectValues = mObjectMap.values().toArray(); |
| 3555 | for (Object object : objectValues) { |
| 3556 | if (object instanceof TagEndpoint) { |
| 3557 | TagEndpoint tag = (TagEndpoint)object; |
| 3558 | ((TagEndpoint) object).stopPresenceChecking(); |
| 3559 | } |
| 3560 | } |
| 3561 | } |
| 3562 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3563 | /** |
| 3564 | * Disconnect any target if present |
| 3565 | */ |
| 3566 | void maybeDisconnectTarget() { |
| 3567 | if (!isNfcEnabledOrShuttingDown()) { |
| 3568 | return; |
| 3569 | } |
| 3570 | Object[] objectsToDisconnect; |
| 3571 | synchronized (this) { |
| 3572 | Object[] objectValues = mObjectMap.values().toArray(); |
| 3573 | // Copy the array before we clear mObjectMap, |
| 3574 | // just in case the HashMap values are backed by the same array |
| 3575 | objectsToDisconnect = Arrays.copyOf(objectValues, objectValues.length); |
| 3576 | mObjectMap.clear(); |
| 3577 | } |
| 3578 | for (Object o : objectsToDisconnect) { |
| 3579 | if (DBG) Log.d(TAG, "disconnecting " + o.getClass().getName()); |
| 3580 | if (o instanceof TagEndpoint) { |
| 3581 | // Disconnect from tags |
| 3582 | TagEndpoint tag = (TagEndpoint) o; |
| 3583 | tag.disconnect(); |
| 3584 | } else if (o instanceof NfcDepEndpoint) { |
| 3585 | // Disconnect from P2P devices |
| 3586 | NfcDepEndpoint device = (NfcDepEndpoint) o; |
| 3587 | if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) { |
| 3588 | // Remote peer is target, request disconnection |
| 3589 | device.disconnect(); |
| 3590 | } else { |
| 3591 | // Remote peer is initiator, we cannot disconnect |
| 3592 | // Just wait for field removal |
| 3593 | } |
| 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | Object findObject(int key) { |
| 3599 | synchronized (this) { |
| 3600 | Object device = mObjectMap.get(key); |
| 3601 | if (device == null) { |
| 3602 | Log.w(TAG, "Handle not found"); |
| 3603 | } |
| 3604 | return device; |
| 3605 | } |
| 3606 | } |
| 3607 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3608 | Object findAndRemoveObject(int handle) { |
| 3609 | synchronized (this) { |
| 3610 | Object device = mObjectMap.get(handle); |
| 3611 | if (device == null) { |
| 3612 | Log.w(TAG, "Handle not found"); |
| 3613 | } else { |
| 3614 | mObjectMap.remove(handle); |
| 3615 | } |
| 3616 | return device; |
| 3617 | } |
| 3618 | } |
| 3619 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3620 | void registerTagObject(TagEndpoint tag) { |
| 3621 | synchronized (this) { |
| 3622 | mObjectMap.put(tag.getHandle(), tag); |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | void unregisterObject(int handle) { |
| 3627 | synchronized (this) { |
| 3628 | mObjectMap.remove(handle); |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | /** |
| 3633 | * For use by code in this process |
| 3634 | */ |
| 3635 | public LlcpSocket createLlcpSocket(int sap, int miu, int rw, int linearBufferLength) |
| 3636 | throws LlcpException { |
| 3637 | return mDeviceHost.createLlcpSocket(sap, miu, rw, linearBufferLength); |
| 3638 | } |
| 3639 | |
| 3640 | /** |
| 3641 | * For use by code in this process |
| 3642 | */ |
| 3643 | public LlcpConnectionlessSocket createLlcpConnectionLessSocket(int sap, String sn) |
| 3644 | throws LlcpException { |
| 3645 | return mDeviceHost.createLlcpConnectionlessSocket(sap, sn); |
| 3646 | } |
| 3647 | |
| 3648 | /** |
| 3649 | * For use by code in this process |
| 3650 | */ |
| 3651 | public LlcpServerSocket createLlcpServerSocket(int sap, String sn, int miu, int rw, |
| 3652 | int linearBufferLength) throws LlcpException { |
| 3653 | return mDeviceHost.createLlcpServerSocket(sap, sn, miu, rw, linearBufferLength); |
| 3654 | } |
| 3655 | |
| 3656 | public void sendMockNdefTag(NdefMessage msg) { |
| 3657 | sendMessage(MSG_MOCK_NDEF, msg); |
| 3658 | } |
| 3659 | |
| 3660 | public void notifyRoutingTableFull() |
| 3661 | { |
nxf26763 | 86ae0fb | 2018-11-19 17:33:54 +0530 | [diff] [blame] | 3662 | mToastHandler.showToast("Last installed NFC Service is not enabled due to limited resources. To enable this service, " + |
| 3663 | "please disable other servives in Settings Menu", 20); |
| 3664 | |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 3665 | if(!mNxpNfcController.isGsmaCommitOffhostService()) { |
| 3666 | ComponentName prevPaymentComponent = mAidCache.getPreviousPreferredPaymentService(); |
| 3667 | |
| 3668 | mNxpPrefsEditor = mNxpPrefs.edit(); |
| 3669 | mNxpPrefsEditor.putInt("PREF_SET_AID_ROUTING_TABLE_FULL",0x01); |
| 3670 | mNxpPrefsEditor.commit(); |
| 3671 | //broadcast Aid Routing Table Full intent to the user |
| 3672 | Intent aidTableFull = new Intent(); |
| 3673 | aidTableFull.putExtra(NxpConstants.EXTRA_GSMA_PREV_PAYMENT_COMPONENT,prevPaymentComponent); |
| 3674 | aidTableFull.setAction(NxpConstants.ACTION_ROUTING_TABLE_FULL); |
| 3675 | if (DBG) { |
| 3676 | Log.d(TAG, "notify aid routing table full to the user"); |
| 3677 | } |
| 3678 | mContext.sendBroadcastAsUser(aidTableFull, UserHandle.CURRENT); |
| 3679 | mAidCache.setPreviousPreferredPaymentService(null); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3680 | } |
nxf26763 | 86ae0fb | 2018-11-19 17:33:54 +0530 | [diff] [blame] | 3681 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3682 | } |
| 3683 | /** |
| 3684 | * set default Aid route entry in case application does not configure this route entry |
| 3685 | */ |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 3686 | public void setDefaultAidRouteLoc( int routeLoc) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3687 | { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 3688 | mNxpPrefsEditor = mNxpPrefs.edit(); |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 3689 | Log.d(TAG, "writing to preferences setDefaultAidRouteLoc :" + routeLoc); |
| 3690 | |
| 3691 | int defaultAidRoute = ((mDeviceHost.getDefaultAidPowerState() & 0x1F) | (routeLoc << ROUTE_LOC_MASK)); |
| 3692 | if(routeLoc == 0x00) |
| 3693 | { |
| 3694 | /* |
| 3695 | bit pos 1 = Power Off |
| 3696 | bit pos 2 = Battery Off |
| 3697 | bit pos 4 = Screen Off |
| 3698 | Set these bits to 0 because in case routeLoc = HOST it can not work on POWER_OFF, BATTERY_OFF and SCREEN_OFF*/ |
| 3699 | defaultAidRoute &= 0xE9; |
| 3700 | } |
| 3701 | |
| 3702 | mNxpPrefsEditor.putInt("PREF_SET_DEFAULT_ROUTE_ID", defaultAidRoute); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 3703 | mNxpPrefsEditor.commit(); |
| 3704 | int defaultRoute=mNxpPrefs.getInt("PREF_SET_DEFAULT_ROUTE_ID",0xFF); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3705 | Log.d(TAG, "reading preferences from user :" + defaultRoute); |
| 3706 | } |
| 3707 | |
| 3708 | public int getAidRoutingTableSize () |
| 3709 | { |
| 3710 | int aidTableSize = 0x00; |
| 3711 | aidTableSize = mDeviceHost.getAidTableSize(); |
| 3712 | return aidTableSize; |
| 3713 | } |
| 3714 | |
nxpandroid | cbf2482 | 2017-07-12 21:37:17 +0530 | [diff] [blame] | 3715 | public void routeAids(String aid, int route, int powerState, int aidInfo) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3716 | Message msg = mHandler.obtainMessage(); |
| 3717 | msg.what = MSG_ROUTE_AID; |
| 3718 | msg.arg1 = route; |
| 3719 | msg.arg2 = powerState; |
nxpandroid | cbf2482 | 2017-07-12 21:37:17 +0530 | [diff] [blame] | 3720 | Bundle aidbundle = new Bundle(); |
| 3721 | aidbundle.putInt("aidinfo",aidInfo); |
| 3722 | msg.setData(aidbundle); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3723 | msg.obj = aid; |
| 3724 | mHandler.sendMessage(msg); |
| 3725 | } |
| 3726 | |
| 3727 | public void unrouteAids(String aid) { |
| 3728 | sendMessage(MSG_UNROUTE_AID, aid); |
| 3729 | } |
nxpandroid | a5fd662 | 2017-07-31 16:15:18 +0530 | [diff] [blame] | 3730 | |
| 3731 | public void routeApduPattern(String apdu, String mask ,int route, int powerState) { |
| 3732 | Message msg = mHandler.obtainMessage(); |
| 3733 | msg.what = MSG_ROUTE_APDU; |
| 3734 | msg.arg1 = route; |
| 3735 | msg.arg2 = powerState; |
| 3736 | Bundle apduPatternbundle = new Bundle(); |
| 3737 | apduPatternbundle.putString("apduData",apdu); |
| 3738 | apduPatternbundle.putString("apduMask",mask); |
| 3739 | msg.setData(apduPatternbundle); |
| 3740 | mHandler.sendMessage(msg); |
| 3741 | } |
| 3742 | |
| 3743 | public void unrouteApduPattern(String apdu) { |
| 3744 | //sendMessage(MSG_UNROUTE_APDU, apdu); |
| 3745 | mDeviceHost.unrouteApduPattern(hexStringToBytes(apdu)); |
| 3746 | } |
| 3747 | |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 3748 | public int getNciVersion() { |
| 3749 | return mDeviceHost.getNciVersion(); |
| 3750 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3751 | private byte[] getT3tIdentifierBytes(String systemCode, String nfcId2, String t3tPmm) { |
| 3752 | ByteBuffer buffer = ByteBuffer.allocate(2 + 8 + 8); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 3753 | buffer.put(hexStringToBytes(systemCode)); |
| 3754 | buffer.put(hexStringToBytes(nfcId2)); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3755 | buffer.put(hexStringToBytes(t3tPmm)); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 3756 | byte[] t3tIdBytes = new byte[buffer.position()]; |
| 3757 | buffer.position(0); |
| 3758 | buffer.get(t3tIdBytes); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3759 | |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 3760 | return t3tIdBytes; |
| 3761 | } |
| 3762 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3763 | public void registerT3tIdentifier(String systemCode, String nfcId2, String t3tPmm) { |
| 3764 | Log.d(TAG, "request to register LF_T3T_IDENTIFIER"); |
| 3765 | |
| 3766 | byte[] t3tIdentifier = getT3tIdentifierBytes(systemCode, nfcId2, t3tPmm); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 3767 | sendMessage(MSG_REGISTER_T3T_IDENTIFIER, t3tIdentifier); |
| 3768 | } |
| 3769 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3770 | public void deregisterT3tIdentifier(String systemCode, String nfcId2, String t3tPmm) { |
| 3771 | Log.d(TAG, "request to deregister LF_T3T_IDENTIFIER"); |
| 3772 | |
| 3773 | byte[] t3tIdentifier = getT3tIdentifierBytes(systemCode, nfcId2, t3tPmm); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 3774 | sendMessage(MSG_DEREGISTER_T3T_IDENTIFIER, t3tIdentifier); |
| 3775 | } |
| 3776 | |
| 3777 | public void clearT3tIdentifiersCache() { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3778 | Log.d(TAG, "clear T3t Identifiers Cache"); |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 3779 | mDeviceHost.clearT3tIdentifiersCache(); |
| 3780 | } |
| 3781 | |
| 3782 | public int getLfT3tMax() { |
| 3783 | return mDeviceHost.getLfT3tMax(); |
| 3784 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3785 | |
| 3786 | public void commitRouting() { |
| 3787 | mHandler.sendEmptyMessage(MSG_COMMIT_ROUTING); |
| 3788 | } |
| 3789 | /** |
| 3790 | * get default Aid route entry in case application does not configure this route entry |
| 3791 | */ |
| 3792 | public int GetDefaultRouteLoc() |
| 3793 | { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 3794 | int defaultRouteLoc = mNxpPrefs.getInt("PREF_SET_DEFAULT_ROUTE_ID", GetDefaultRouteEntry()) >> ROUTE_LOC_MASK; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3795 | Log.d(TAG, "GetDefaultRouteLoc :" + defaultRouteLoc); |
| 3796 | return defaultRouteLoc ; |
| 3797 | } |
| 3798 | |
| 3799 | /** |
| 3800 | * get default MifareDesfireRoute route entry in case application does not configure this route entry |
| 3801 | */ |
| 3802 | public int GetDefaultMifareDesfireRouteEntry() |
| 3803 | { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3804 | int routeLoc = mDeviceHost.getDefaultDesfireRoute(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3805 | int defaultMifareDesfireRoute = ((mDeviceHost.getDefaultDesfirePowerState() & 0x3F) | (routeLoc << ROUTE_LOC_MASK)); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3806 | if(routeLoc == 0x00) |
| 3807 | { |
nxpandroid | 8aecbf8 | 2016-09-16 20:21:47 +0530 | [diff] [blame] | 3808 | /* |
| 3809 | bit pos 1 = Power Off |
| 3810 | bit pos 2 = Battery Off |
| 3811 | bit pos 4 = Screen Off |
| 3812 | Set these bits to 0 because in case routeLoc = HOST it can not work on POWER_OFF, BATTERY_OFF and SCREEN_OFF*/ |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3813 | defaultMifareDesfireRoute &= 0xF9; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3814 | } |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3815 | if (DBG) Log.d(TAG, "defaultMifareDesfireRoute : " + defaultMifareDesfireRoute); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3816 | return defaultMifareDesfireRoute; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3817 | } |
| 3818 | /** |
| 3819 | * set default Aid route entry in case application does not configure this route entry |
| 3820 | */ |
| 3821 | |
| 3822 | public int GetDefaultRouteEntry() |
| 3823 | { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3824 | int routeLoc = mDeviceHost.getDefaultAidRoute(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3825 | int defaultAidRoute = ((mDeviceHost.getDefaultAidPowerState() & 0x3F) | (routeLoc << ROUTE_LOC_MASK)); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3826 | if(routeLoc == 0x00) |
| 3827 | { |
nxpandroid | 8aecbf8 | 2016-09-16 20:21:47 +0530 | [diff] [blame] | 3828 | /* |
| 3829 | bit pos 1 = Power Off |
| 3830 | bit pos 2 = Battery Off |
| 3831 | bit pos 4 = Screen Off |
| 3832 | Set these bits to 0 because in case routeLoc = HOST it can not work on POWER_OFF, BATTERY_OFF and SCREEN_OFF*/ |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3833 | defaultAidRoute &= 0xF9; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3834 | } |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3835 | if (DBG) Log.d(TAG, "defaultAidRoute : " + defaultAidRoute); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3836 | return defaultAidRoute; |
| 3837 | } |
| 3838 | |
| 3839 | /** |
| 3840 | * get default MifateCLT route entry in case application does not configure this route entry |
| 3841 | */ |
| 3842 | public int GetDefaultMifateCLTRouteEntry() |
| 3843 | { |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3844 | int routeLoc = mDeviceHost.getDefaultMifareCLTRoute(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 3845 | int defaultMifateCLTRoute = ((mDeviceHost.getDefaultMifareCLTPowerState() & 0x3F) | (routeLoc << ROUTE_LOC_MASK) | (TECH_TYPE_A << TECH_TYPE_MASK)); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3846 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 3847 | if (DBG) Log.d(TAG, "defaultMifateCLTRoute : " + defaultMifateCLTRoute); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 3848 | return defaultMifateCLTRoute; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | public boolean setDefaultRoute(int defaultRouteEntry, int defaultProtoRouteEntry, int defaultTechRouteEntry) { |
| 3852 | boolean ret = mDeviceHost.setDefaultRoute(defaultRouteEntry, defaultProtoRouteEntry, defaultTechRouteEntry); |
| 3853 | return ret; |
| 3854 | } |
| 3855 | |
| 3856 | public int getDefaultRoute() { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 3857 | return mNxpPrefs.getInt(PREF_DEFAULT_ROUTE_ID, DEFAULT_ROUTE_ID_DEFAULT); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | |
| 3861 | public void commitingFelicaRouting() { |
| 3862 | mHandler.sendEmptyMessage(MSG_COMMITINF_FELICA_ROUTING); |
| 3863 | } |
| 3864 | |
| 3865 | public void commitedFelicaRouting() { |
| 3866 | mHandler.sendEmptyMessage(MSG_COMMITED_FELICA_ROUTING); |
| 3867 | } |
| 3868 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 3869 | public int getAidRoutingTableStatus() { |
| 3870 | int aidTableStatus = 0x00; |
| 3871 | aidTableStatus = mNxpPrefs.getInt("PREF_SET_AID_ROUTING_TABLE_FULL",0x00); |
| 3872 | return aidTableStatus; |
| 3873 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3874 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3875 | public void clearRouting() { |
| 3876 | mHandler.sendEmptyMessage(MSG_CLEAR_ROUTING); |
| 3877 | } |
| 3878 | |
| 3879 | public boolean isVzwFeatureEnabled(){ |
| 3880 | return mDeviceHost.isVzwFeatureEnabled(); |
| 3881 | } |
| 3882 | |
| 3883 | public boolean sendData(byte[] data) { |
| 3884 | return mDeviceHost.sendRawFrame(data); |
| 3885 | } |
| 3886 | |
| 3887 | public int getDefaultSecureElement() { |
| 3888 | int[] seList = mDeviceHost.doGetSecureElementList(); |
| 3889 | if ( seList == null || seList.length != 1) { |
| 3890 | //use default value |
| 3891 | return -1; |
| 3892 | } else { |
| 3893 | return seList[0]; |
| 3894 | } |
| 3895 | } |
| 3896 | |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 3897 | public void updateLastScreenState() |
| 3898 | { |
| 3899 | Log.d(TAG, "updateLastScreenState"); |
| 3900 | int screen_state_mask = (mNfcUnlockManager.isLockscreenPollingEnabled()) ? |
| 3901 | (ScreenStateHelper.SCREEN_POLLING_TAG_MASK | mScreenState) : mScreenState; |
| 3902 | mDeviceHost.doSetScreenOrPowerState(screen_state_mask); |
| 3903 | } |
| 3904 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3905 | public void etsiStartConfig(int eeHandle) { |
| 3906 | Log.d(TAG, "etsiStartConfig Enter"); |
| 3907 | |
| 3908 | Log.d(TAG, "etsiStartConfig : etsiInitConfig"); |
| 3909 | mDeviceHost.etsiInitConfig(); |
| 3910 | |
| 3911 | Log.d(TAG, "etsiStartConfig : disableDiscovery"); |
| 3912 | mDeviceHost.disableDiscovery(); |
| 3913 | |
| 3914 | Log.d(TAG, "etsiStartConfig : etsiReaderConfig"); |
| 3915 | mDeviceHost.etsiReaderConfig(eeHandle); |
| 3916 | |
| 3917 | Log.d(TAG, "etsiStartConfig : notifyEEReaderEvent"); |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 3918 | mDeviceHost.notifyEEReaderEvent(ETSI_READER_START_SUCCESS); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3919 | |
| 3920 | Log.d(TAG, "etsiStartConfig : setEtsiReaederState"); |
| 3921 | mDeviceHost.setEtsiReaederState(STATE_SE_RDR_MODE_STARTED); |
| 3922 | //broadcast SWP_READER_ACTIVATED evt |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 3923 | Intent swpReaderRequestedIntent = new Intent(); |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 3924 | swpReaderRequestedIntent.setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_START_SUCCESS); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3925 | if (DBG) { |
| 3926 | Log.d(TAG, "SWP READER - Requested"); |
| 3927 | } |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 3928 | mContext.sendBroadcast(swpReaderRequestedIntent); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3929 | |
| 3930 | Log.d(TAG, "etsiStartConfig : enableDiscovery"); |
| 3931 | mDeviceHost.enableDiscovery(mCurrentDiscoveryParameters, true); |
| 3932 | |
| 3933 | Log.d(TAG, "etsiStartConfig Exit"); |
| 3934 | } |
| 3935 | |
| 3936 | public void etsiStopConfig(int discNtfTimeout) { |
| 3937 | Log.d(TAG, "etsiStopConfig Enter"); |
| 3938 | if( mDeviceHost.getEtsiReaederState() == STATE_SE_RDR_MODE_STOP_IN_PROGRESS) |
| 3939 | { |
| 3940 | Log.d(TAG, "Attempting etsiStopConfig while STATE_SE_RDR_MODE_STOP_IN_PROGRESS. Returning.."); |
| 3941 | return; |
| 3942 | } |
| 3943 | ETSI_STOP_CONFIG = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3944 | Log.d(TAG, "etsiStopConfig : etsiInitConfig"); |
| 3945 | mDeviceHost.etsiInitConfig(); |
| 3946 | |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 3947 | Timer mTimer = new Timer(); |
| 3948 | TagRemoveTaskTimer tagRemoveTask = new TagRemoveTaskTimer(); |
| 3949 | mTimer.schedule(tagRemoveTask, ETSI_PRESENCE_CHECK_DELAY, ETSI_PRESENCE_CHECK_DELAY); |
| 3950 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3951 | Log.d(TAG, "etsiStopConfig : disableDiscovery"); |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 3952 | mDeviceHost.stopPoll(NxpConstants.ULTRA_LOW_POWER); |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 3953 | mTimer.cancel(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3954 | |
| 3955 | if(mDeviceHost.getEtsiReaederState() == STATE_SE_RDR_MODE_STOPPED) |
| 3956 | { |
| 3957 | Log.d(TAG, "etsiStopConfig :etsi reader already Stopped. Returning.."); |
| 3958 | ETSI_STOP_CONFIG = false; |
| 3959 | return; |
| 3960 | } |
| 3961 | Log.d(TAG, "etsiStopConfig : etsiResetReaderConfig"); |
| 3962 | mDeviceHost.etsiResetReaderConfig(); |
| 3963 | |
Suhas Suresh | e8aa9cf | 2018-08-01 15:16:59 +0530 | [diff] [blame] | 3964 | Log.d(TAG, "etsiStopConfig : notifyEEReaderEvent"); |
| 3965 | mDeviceHost.notifyEEReaderEvent(ETSI_READER_STOP); |
| 3966 | |
Ganesh Deva | 525a2d5 | 2018-06-01 21:39:38 +0530 | [diff] [blame] | 3967 | Log.d(TAG, "etsiStopConfig : enable discovery"); |
| 3968 | NfcDiscoveryParameters params = computeDiscoveryParameters(mScreenState); |
| 3969 | boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery(); |
| 3970 | mDeviceHost.enableDiscovery(params, shouldRestart); |
| 3971 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 3972 | Intent swpReaderDeActivatedIntent = new Intent(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3973 | |
| 3974 | //broadcast SWP_READER_DEACTIVATED evt |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 3975 | swpReaderDeActivatedIntent |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 3976 | .setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_STOP_SUCCESS); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3977 | if (DBG) { |
| 3978 | Log.d(TAG, "SWP READER - DeActivated"); |
| 3979 | } |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 3980 | mContext.sendBroadcast(swpReaderDeActivatedIntent); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3981 | |
| 3982 | Log.d(TAG, "etsiStopConfig : setEtsiReaederState"); |
| 3983 | mDeviceHost.setEtsiReaederState(STATE_SE_RDR_MODE_STOPPED); |
| 3984 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3985 | ETSI_STOP_CONFIG = false; |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 3986 | updateLastScreenState(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3987 | |
| 3988 | Log.d(TAG, "etsiStopConfig Exit"); |
| 3989 | } |
| 3990 | |
| 3991 | void sendMessage(int what, Object obj) { |
| 3992 | Message msg = mHandler.obtainMessage(); |
| 3993 | msg.what = what; |
| 3994 | msg.obj = obj; |
| 3995 | mHandler.sendMessage(msg); |
| 3996 | } |
| 3997 | |
| 3998 | final class NfcServiceHandler extends Handler { |
| 3999 | @Override |
| 4000 | public void handleMessage(Message msg) { |
| 4001 | switch (msg.what) { |
| 4002 | case MSG_ROUTE_AID: { |
| 4003 | int route = msg.arg1; |
| 4004 | int power = msg.arg2; |
nxpandroid | cbf2482 | 2017-07-12 21:37:17 +0530 | [diff] [blame] | 4005 | int aidInfo = 0x00; |
| 4006 | Bundle dataBundle = msg.getData(); |
| 4007 | if (dataBundle != null) |
| 4008 | aidInfo = dataBundle.getInt("aidinfo"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4009 | String aid = (String) msg.obj; |
nxpandroid | cbf2482 | 2017-07-12 21:37:17 +0530 | [diff] [blame] | 4010 | String cuttedAid = aid; |
| 4011 | if(aid.endsWith("*")||aid.endsWith("#")) { |
| 4012 | cuttedAid = aid.substring(0, aid.length() - 1); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4013 | } |
nxpandroid | cbf2482 | 2017-07-12 21:37:17 +0530 | [diff] [blame] | 4014 | mDeviceHost.routeAid(hexStringToBytes(cuttedAid), route, power, aidInfo); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4015 | // Restart polling config |
| 4016 | break; |
| 4017 | } |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 4018 | case MSG_REGISTER_T3T_IDENTIFIER: { |
| 4019 | Log.d(TAG, "message to register LF_T3T_IDENTIFIER"); |
| 4020 | mDeviceHost.disableDiscovery(); |
| 4021 | |
| 4022 | byte[] t3tIdentifier = (byte[]) msg.obj; |
| 4023 | mDeviceHost.registerT3tIdentifier(t3tIdentifier); |
| 4024 | |
| 4025 | NfcDiscoveryParameters params = computeDiscoveryParameters(mScreenState); |
| 4026 | boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery(); |
| 4027 | mDeviceHost.enableDiscovery(params, shouldRestart); |
| 4028 | break; |
| 4029 | } |
| 4030 | case MSG_DEREGISTER_T3T_IDENTIFIER: { |
| 4031 | Log.d(TAG, "message to deregister LF_T3T_IDENTIFIER"); |
| 4032 | mDeviceHost.disableDiscovery(); |
| 4033 | |
| 4034 | byte[] t3tIdentifier = (byte[]) msg.obj; |
| 4035 | mDeviceHost.deregisterT3tIdentifier(t3tIdentifier); |
| 4036 | |
| 4037 | NfcDiscoveryParameters params = computeDiscoveryParameters(mScreenState); |
| 4038 | boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery(); |
| 4039 | mDeviceHost.enableDiscovery(params, shouldRestart); |
| 4040 | break; |
| 4041 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4042 | case MSG_INVOKE_BEAM: { |
| 4043 | mP2pLinkManager.onManualBeamInvoke((BeamShareData)msg.obj); |
| 4044 | break; |
| 4045 | } |
nxpandroid | a5fd662 | 2017-07-31 16:15:18 +0530 | [diff] [blame] | 4046 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4047 | case MSG_UNROUTE_AID: { |
| 4048 | String aid = (String) msg.obj; |
| 4049 | mDeviceHost.unrouteAid(hexStringToBytes(aid)); |
| 4050 | break; |
| 4051 | } |
| 4052 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4053 | case MSG_COMMITINF_FELICA_ROUTING: { |
| 4054 | Log.e(TAG, "applyRouting -10"); |
| 4055 | mIsFelicaOnHostConfiguring = true; |
| 4056 | applyRouting(true); |
| 4057 | break; |
| 4058 | } |
| 4059 | |
| 4060 | case MSG_COMMITED_FELICA_ROUTING: { |
| 4061 | Log.e(TAG, "applyRouting -11"); |
| 4062 | mIsFelicaOnHostConfigured = true; |
| 4063 | applyRouting(true); |
| 4064 | break; |
| 4065 | } |
| 4066 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4067 | case MSG_COMMIT_ROUTING: { |
| 4068 | Log.e(TAG, "applyRouting -9"); |
| 4069 | boolean commit = false; |
nxpandroid | 07d1cc2 | 2017-09-14 12:20:58 +0530 | [diff] [blame] | 4070 | boolean enForced = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4071 | synchronized (NfcService.this) { |
| 4072 | if (mCurrentDiscoveryParameters.shouldEnableDiscovery()) { |
| 4073 | commit = true; |
nxpandroid | 07d1cc2 | 2017-09-14 12:20:58 +0530 | [diff] [blame] | 4074 | }else if(mAidRoutingManager.isRoutingTableUpdated()){ |
| 4075 | commit = true; |
| 4076 | enForced = true; |
| 4077 | Log.d(TAG, "Routing table is updated thus needs to be committed."); |
| 4078 | } |
| 4079 | else { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4080 | Log.d(TAG, "Not committing routing because discovery is disabled."); |
| 4081 | } |
| 4082 | } |
| 4083 | if (commit) { |
| 4084 | mIsRoutingTableDirty = true; |
nxpandroid | 07d1cc2 | 2017-09-14 12:20:58 +0530 | [diff] [blame] | 4085 | applyRouting(enForced); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | |
| 4089 | break; |
| 4090 | } |
| 4091 | case MSG_CLEAR_ROUTING: { |
| 4092 | mDeviceHost.clearAidTable(); |
| 4093 | break; |
| 4094 | } |
| 4095 | |
| 4096 | case MSG_CHANGE_DEFAULT_ROUTE: |
| 4097 | Log.d(TAG, "Handler: Change default route"); |
| 4098 | try{ |
| 4099 | mNxpNfcAdapter.DefaultRouteSet(ROUTE_ID_HOST, true, false, false); |
| 4100 | } catch(RemoteException re) { |
| 4101 | Log.d(TAG, "NxpNci: onAidRoutingTableFull: Exception to change default route to host!"); |
| 4102 | } |
| 4103 | break; |
| 4104 | |
| 4105 | case MSG_MOCK_NDEF: { |
| 4106 | NdefMessage ndefMsg = (NdefMessage) msg.obj; |
| 4107 | Bundle extras = new Bundle(); |
| 4108 | extras.putParcelable(Ndef.EXTRA_NDEF_MSG, ndefMsg); |
| 4109 | extras.putInt(Ndef.EXTRA_NDEF_MAXLENGTH, 0); |
| 4110 | extras.putInt(Ndef.EXTRA_NDEF_CARDSTATE, Ndef.NDEF_MODE_READ_ONLY); |
| 4111 | extras.putInt(Ndef.EXTRA_NDEF_TYPE, Ndef.TYPE_OTHER); |
| 4112 | Tag tag = Tag.createMockTag(new byte[]{0x00}, |
| 4113 | new int[]{TagTechnology.NDEF}, |
| 4114 | new Bundle[]{extras}); |
| 4115 | Log.d(TAG, "mock NDEF tag, starting corresponding activity"); |
| 4116 | Log.d(TAG, tag.toString()); |
| 4117 | int dispatchStatus = mNfcDispatcher.dispatchTag(tag); |
| 4118 | if (dispatchStatus == NfcDispatcher.DISPATCH_SUCCESS) { |
| 4119 | playSound(SOUND_END); |
| 4120 | } else if (dispatchStatus == NfcDispatcher.DISPATCH_FAIL) { |
| 4121 | playSound(SOUND_ERROR); |
| 4122 | } |
| 4123 | break; |
| 4124 | } |
| 4125 | |
| 4126 | case MSG_SE_DELIVER_INTENT: { |
| 4127 | Log.d(TAG, "SE DELIVER INTENT"); |
| 4128 | Intent seIntent = (Intent) msg.obj; |
| 4129 | |
| 4130 | String action = seIntent.getAction(); |
| 4131 | if (action.equals("com.gsma.services.nfc.action.TRANSACTION_EVENT")) { |
| 4132 | byte[] byteAid = seIntent.getByteArrayExtra("com.android.nfc_extras.extra.AID"); |
| 4133 | byte[] data = seIntent.getByteArrayExtra("com.android.nfc_extras.extra.DATA"); |
Ganesh Deva | 2c02705 | 2018-08-07 15:18:32 +0530 | [diff] [blame] | 4134 | String seName = seIntent.getStringExtra("com.android.nfc_extras.extra.SECURE_ELEMENT_NAME"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4135 | StringBuffer strAid = new StringBuffer(); |
| 4136 | for (int i = 0; i < byteAid.length; i++) { |
| 4137 | String hex = Integer.toHexString(0xFF & byteAid[i]); |
| 4138 | if (hex.length() == 1) |
| 4139 | strAid.append('0'); |
| 4140 | strAid.append(hex); |
| 4141 | } |
| 4142 | Intent gsmaIntent = new Intent(); |
| 4143 | gsmaIntent.setAction("com.gsma.services.nfc.action.TRANSACTION_EVENT"); |
| 4144 | if (byteAid != null) |
| 4145 | gsmaIntent.putExtra("com.gsma.services.nfc.extra.AID", byteAid); |
| 4146 | if (data != null) |
| 4147 | gsmaIntent.putExtra("com.gsma.services.nfc.extra.DATA", data); |
| 4148 | |
| 4149 | //"nfc://secure:0/<seName>/<strAid>" |
| 4150 | String url = new String ("nfc://secure:0/" + seName + "/" + strAid); |
| 4151 | gsmaIntent.setData(Uri.parse(url)); |
| 4152 | gsmaIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 4153 | gsmaIntent.setPackage(seIntent.getPackage()); |
| 4154 | |
| 4155 | Boolean receptionMode = mNxpNfcController.mMultiReceptionMap.get(seName); |
| 4156 | if (receptionMode == null) |
| 4157 | receptionMode = defaultTransactionEventReceptionMode; |
| 4158 | |
| 4159 | if (receptionMode == multiReceptionMode) { |
| 4160 | // if multicast reception for GSMA |
| 4161 | mContext.sendBroadcast(gsmaIntent); |
| 4162 | } else { |
| 4163 | // if unicast reception for GSMA |
| 4164 | try { |
| 4165 | if (mIsSentUnicastReception == false) { |
| 4166 | //start gsma |
| 4167 | gsmaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4168 | mContext.startActivity(gsmaIntent); |
| 4169 | mIsSentUnicastReception = true; |
| 4170 | } |
| 4171 | } catch (Exception e) { |
| 4172 | if (DBG) Log.d(TAG, "Exception: " + e.getMessage()); |
| 4173 | } |
| 4174 | } |
| 4175 | } else { |
| 4176 | mContext.sendBroadcast(seIntent); |
| 4177 | } |
| 4178 | break; |
| 4179 | } |
| 4180 | |
| 4181 | case MSG_NDEF_TAG: |
| 4182 | if (DBG) Log.d(TAG, "Tag detected, notifying applications"); |
Suhas Suresh | 31963a0 | 2018-04-25 12:14:23 +0530 | [diff] [blame] | 4183 | mPowerManager.userActivity(SystemClock.uptimeMillis(), |
| 4184 | PowerManager.USER_ACTIVITY_EVENT_OTHER, 0); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4185 | mNumTagsDetected.incrementAndGet(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4186 | TagEndpoint tag = (TagEndpoint) msg.obj; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4187 | byte[] debounceTagUid; |
| 4188 | int debounceTagMs; |
| 4189 | ITagRemovedCallback debounceTagRemovedCallback; |
| 4190 | synchronized (NfcService.this) { |
| 4191 | debounceTagUid = mDebounceTagUid; |
| 4192 | debounceTagMs = mDebounceTagDebounceMs; |
| 4193 | debounceTagRemovedCallback = mDebounceTagRemovedCallback; |
| 4194 | } |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4195 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4196 | ReaderModeParams readerParams = null; |
| 4197 | int presenceCheckDelay = DEFAULT_PRESENCE_CHECK_DELAY; |
| 4198 | DeviceHost.TagDisconnectedCallback callback = |
| 4199 | new DeviceHost.TagDisconnectedCallback() { |
| 4200 | @Override |
| 4201 | public void onTagDisconnected(long handle) { |
nxpandroid | 03323c8 | 2017-09-14 11:13:16 +0530 | [diff] [blame] | 4202 | if(nci_version != NCI_VERSION_2_0) { |
| 4203 | applyRouting(false); |
| 4204 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4205 | } |
| 4206 | }; |
| 4207 | synchronized (NfcService.this) { |
| 4208 | readerParams = mReaderModeParams; |
| 4209 | } |
| 4210 | if (readerParams != null) { |
| 4211 | presenceCheckDelay = readerParams.presenceCheckDelay; |
| 4212 | if ((readerParams.flags & NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK) != 0) { |
| 4213 | if (DBG) Log.d(TAG, "Skipping NDEF detection in reader mode"); |
| 4214 | tag.startPresenceChecking(presenceCheckDelay, callback); |
| 4215 | dispatchTagEndpoint(tag, readerParams); |
| 4216 | break; |
| 4217 | } |
| 4218 | } |
| 4219 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4220 | if (tag.getConnectedTechnology() == TagTechnology.NFC_BARCODE) { |
| 4221 | // When these tags start containing NDEF, they will require |
| 4222 | // the stack to deal with them in a different way, since |
| 4223 | // they are activated only really shortly. |
| 4224 | // For now, don't consider NDEF on these. |
| 4225 | if (DBG) Log.d(TAG, "Skipping NDEF detection for NFC Barcode"); |
| 4226 | tag.startPresenceChecking(presenceCheckDelay, callback); |
| 4227 | dispatchTagEndpoint(tag, readerParams); |
| 4228 | break; |
| 4229 | } |
| 4230 | NdefMessage ndefMsg = tag.findAndReadNdef(); |
| 4231 | |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4232 | if (ndefMsg == null) { |
| 4233 | // First try to see if this was a bad tag read |
| 4234 | if (!tag.reconnect()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4235 | tag.disconnect(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4236 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4237 | } |
| 4238 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4239 | |
| 4240 | if (debounceTagUid != null) { |
| 4241 | // If we're debouncing and the UID or the NDEF message of the tag match, |
| 4242 | // don't dispatch but drop it. |
| 4243 | if (Arrays.equals(debounceTagUid, tag.getUid()) || |
| 4244 | (ndefMsg != null && ndefMsg.equals(mLastReadNdefMessage))) { |
| 4245 | mHandler.removeMessages(MSG_TAG_DEBOUNCE); |
| 4246 | mHandler.sendEmptyMessageDelayed(MSG_TAG_DEBOUNCE, debounceTagMs); |
| 4247 | tag.disconnect(); |
| 4248 | return; |
| 4249 | } else { |
| 4250 | synchronized (NfcService.this) { |
| 4251 | mDebounceTagUid = null; |
| 4252 | mDebounceTagRemovedCallback = null; |
Nikhil Chhabra | 1b2368f | 2018-01-09 18:34:21 +0530 | [diff] [blame] | 4253 | mDebounceTagNativeHandle = INVALID_NATIVE_HANDLE; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4254 | } |
| 4255 | if (debounceTagRemovedCallback != null) { |
| 4256 | try { |
| 4257 | debounceTagRemovedCallback.onTagRemoved(); |
| 4258 | } catch (RemoteException e) { |
| 4259 | // Ignore |
| 4260 | } |
| 4261 | } |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | mLastReadNdefMessage = ndefMsg; |
| 4266 | |
| 4267 | tag.startPresenceChecking(presenceCheckDelay, callback); |
| 4268 | dispatchTagEndpoint(tag, readerParams); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4269 | break; |
| 4270 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4271 | case MSG_CONNECTIVITY_EVENT: |
| 4272 | if (DBG) { |
| 4273 | Log.d(TAG, "SE EVENT CONNECTIVITY"); |
| 4274 | } |
| 4275 | Integer evtSrcInfo = (Integer) msg.obj; |
| 4276 | Log.d(TAG, "Event source " + evtSrcInfo); |
| 4277 | String evtSrc = ""; |
| 4278 | if(evtSrcInfo == UICC_ID_TYPE) { |
| 4279 | evtSrc = NxpConstants.UICC_ID; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 4280 | } else if(evtSrcInfo == UICC2_ID_TYPE) { |
| 4281 | evtSrc = NxpConstants.UICC2_ID; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4282 | } else if(evtSrcInfo == SMART_MX_ID_TYPE) { |
| 4283 | evtSrc = NxpConstants.SMART_MX_ID; |
| 4284 | } |
| 4285 | |
| 4286 | Intent eventConnectivityIntent = new Intent(); |
| 4287 | eventConnectivityIntent.setAction(NxpConstants.ACTION_CONNECTIVITY_EVENT_DETECTED); |
| 4288 | eventConnectivityIntent.putExtra(NxpConstants.EXTRA_SOURCE, evtSrc); |
| 4289 | if (DBG) { |
| 4290 | Log.d(TAG, "Broadcasting Intent"); |
| 4291 | } |
| 4292 | mContext.sendBroadcast(eventConnectivityIntent, NfcPermissions.NFC_PERMISSION); |
| 4293 | break; |
| 4294 | |
| 4295 | case MSG_EMVCO_MULTI_CARD_DETECTED_EVENT: |
| 4296 | if (DBG) { |
| 4297 | Log.d(TAG, "EMVCO MULTI CARD DETECTED EVENT"); |
| 4298 | } |
| 4299 | |
| 4300 | Intent eventEmvcoMultiCardIntent = new Intent(); |
| 4301 | eventEmvcoMultiCardIntent.setAction(ACTION_EMVCO_MULTIPLE_CARD_DETECTED); |
| 4302 | if (DBG) { |
| 4303 | Log.d(TAG, "Broadcasting Intent"); |
| 4304 | } |
| 4305 | mContext.sendBroadcast(eventEmvcoMultiCardIntent, NFC_PERM); |
| 4306 | break; |
| 4307 | |
| 4308 | case MSG_SE_EMV_CARD_REMOVAL: |
| 4309 | if (DBG) Log.d(TAG, "Card Removal message"); |
| 4310 | /* Send broadcast */ |
| 4311 | Intent cardRemovalIntent = new Intent(); |
| 4312 | cardRemovalIntent.setAction(ACTION_EMV_CARD_REMOVAL); |
| 4313 | if (DBG) Log.d(TAG, "Broadcasting " + ACTION_EMV_CARD_REMOVAL); |
| 4314 | sendSeBroadcast(cardRemovalIntent); |
| 4315 | break; |
| 4316 | |
| 4317 | case MSG_SE_APDU_RECEIVED: |
| 4318 | if (DBG) Log.d(TAG, "APDU Received message"); |
| 4319 | byte[] apduBytes = (byte[]) msg.obj; |
| 4320 | /* Send broadcast */ |
| 4321 | Intent apduReceivedIntent = new Intent(); |
| 4322 | apduReceivedIntent.setAction(ACTION_APDU_RECEIVED); |
| 4323 | if (apduBytes != null && apduBytes.length > 0) { |
| 4324 | apduReceivedIntent.putExtra(EXTRA_APDU_BYTES, apduBytes); |
| 4325 | } |
| 4326 | if (DBG) Log.d(TAG, "Broadcasting " + ACTION_APDU_RECEIVED); |
| 4327 | sendSeBroadcast(apduReceivedIntent); |
| 4328 | break; |
| 4329 | |
| 4330 | case MSG_SE_MIFARE_ACCESS: |
| 4331 | if (DBG) Log.d(TAG, "MIFARE access message"); |
| 4332 | /* Send broadcast */ |
| 4333 | byte[] mifareCmd = (byte[]) msg.obj; |
| 4334 | Intent mifareAccessIntent = new Intent(); |
| 4335 | mifareAccessIntent.setAction(ACTION_MIFARE_ACCESS_DETECTED); |
| 4336 | if (mifareCmd != null && mifareCmd.length > 1) { |
| 4337 | int mifareBlock = mifareCmd[1] & 0xff; |
| 4338 | if (DBG) Log.d(TAG, "Mifare Block=" + mifareBlock); |
| 4339 | mifareAccessIntent.putExtra(EXTRA_MIFARE_BLOCK, mifareBlock); |
| 4340 | } |
| 4341 | if (DBG) Log.d(TAG, "Broadcasting " + ACTION_MIFARE_ACCESS_DETECTED); |
| 4342 | sendSeBroadcast(mifareAccessIntent); |
| 4343 | break; |
| 4344 | |
| 4345 | case MSG_LLCP_LINK_ACTIVATION: |
Suhas Suresh | 31963a0 | 2018-04-25 12:14:23 +0530 | [diff] [blame] | 4346 | mPowerManager.userActivity(SystemClock.uptimeMillis(), |
| 4347 | PowerManager.USER_ACTIVITY_EVENT_OTHER, 0); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4348 | if (mIsDebugBuild) { |
| 4349 | Intent actIntent = new Intent(ACTION_LLCP_UP); |
| 4350 | mContext.sendBroadcast(actIntent); |
| 4351 | } |
| 4352 | llcpActivated((NfcDepEndpoint) msg.obj); |
| 4353 | break; |
| 4354 | |
| 4355 | case MSG_LLCP_LINK_DEACTIVATED: |
| 4356 | if (mIsDebugBuild) { |
| 4357 | Intent deactIntent = new Intent(ACTION_LLCP_DOWN); |
| 4358 | mContext.sendBroadcast(deactIntent); |
| 4359 | } |
| 4360 | NfcDepEndpoint device = (NfcDepEndpoint) msg.obj; |
| 4361 | boolean needsDisconnect = false; |
| 4362 | |
| 4363 | Log.d(TAG, "LLCP Link Deactivated message. Restart polling loop."); |
| 4364 | synchronized (NfcService.this) { |
| 4365 | /* Check if the device has been already unregistered */ |
| 4366 | if (mObjectMap.remove(device.getHandle()) != null) { |
| 4367 | /* Disconnect if we are initiator */ |
| 4368 | if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) { |
| 4369 | if (DBG) Log.d(TAG, "disconnecting from target"); |
| 4370 | needsDisconnect = true; |
| 4371 | } else { |
| 4372 | if (DBG) Log.d(TAG, "not disconnecting from initiator"); |
| 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | if (needsDisconnect) { |
| 4377 | device.disconnect(); // restarts polling loop |
| 4378 | } |
| 4379 | |
| 4380 | mP2pLinkManager.onLlcpDeactivated(); |
| 4381 | break; |
| 4382 | case MSG_LLCP_LINK_FIRST_PACKET: |
| 4383 | mP2pLinkManager.onLlcpFirstPacketReceived(); |
| 4384 | break; |
| 4385 | case MSG_TARGET_DESELECTED: |
| 4386 | /* Broadcast Intent Target Deselected */ |
| 4387 | if (DBG) Log.d(TAG, "Target Deselected"); |
| 4388 | Intent intent = new Intent(); |
| 4389 | intent.setAction(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION); |
| 4390 | if (DBG) Log.d(TAG, "Broadcasting Intent"); |
| 4391 | mContext.sendOrderedBroadcast(intent, NfcPermissions.NFC_PERMISSION); |
| 4392 | break; |
| 4393 | |
| 4394 | case MSG_SE_FIELD_ACTIVATED: { |
| 4395 | if (DBG) Log.d(TAG, "SE FIELD ACTIVATED"); |
| 4396 | Intent eventFieldOnIntent = new Intent(); |
| 4397 | eventFieldOnIntent.setAction(ACTION_RF_FIELD_ON_DETECTED); |
| 4398 | sendSeBroadcast(eventFieldOnIntent); |
| 4399 | break; |
| 4400 | } |
| 4401 | case MSG_RESUME_POLLING: |
| 4402 | mNfcAdapter.resumePolling(); |
| 4403 | break; |
| 4404 | |
| 4405 | case MSG_SE_FIELD_DEACTIVATED: { |
| 4406 | if (DBG) Log.d(TAG, "SE FIELD DEACTIVATED"); |
| 4407 | Intent eventFieldOffIntent = new Intent(); |
| 4408 | eventFieldOffIntent.setAction(ACTION_RF_FIELD_OFF_DETECTED); |
| 4409 | sendSeBroadcast(eventFieldOffIntent); |
| 4410 | break; |
| 4411 | } |
| 4412 | |
| 4413 | case MSG_SE_LISTEN_ACTIVATED: { |
| 4414 | if (DBG) Log.d(TAG, "SE LISTEN MODE ACTIVATED"); |
| 4415 | Intent listenModeActivated = new Intent(); |
| 4416 | listenModeActivated.setAction(ACTION_SE_LISTEN_ACTIVATED); |
| 4417 | sendSeBroadcast(listenModeActivated); |
| 4418 | break; |
| 4419 | } |
| 4420 | |
| 4421 | case MSG_SE_LISTEN_DEACTIVATED: { |
| 4422 | if (DBG) Log.d(TAG, "SE LISTEN MODE DEACTIVATED"); |
| 4423 | Intent listenModeDeactivated = new Intent(); |
| 4424 | listenModeDeactivated.setAction(ACTION_SE_LISTEN_DEACTIVATED); |
| 4425 | sendSeBroadcast(listenModeDeactivated); |
| 4426 | break; |
| 4427 | } |
| 4428 | |
| 4429 | case MSG_SWP_READER_REQUESTED: |
| 4430 | |
| 4431 | /* Send broadcast ordered */ |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4432 | Intent swpReaderRequestedIntent = new Intent(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4433 | ArrayList<Integer> techList = (ArrayList<Integer>) msg.obj; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4434 | Integer[] techs = techList.toArray(new Integer[techList.size()]); |
| 4435 | swpReaderRequestedIntent |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 4436 | .setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_START_SUCCESS); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4437 | if (DBG) { |
| 4438 | Log.d(TAG, "SWP READER - Requested"); |
| 4439 | } |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4440 | mContext.sendBroadcast(swpReaderRequestedIntent); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4441 | break; |
| 4442 | |
| 4443 | case MSG_SWP_READER_REQUESTED_FAIL: |
| 4444 | |
| 4445 | /* Send broadcast ordered */ |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4446 | Intent swpReaderRequestedFailIntent = new Intent(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4447 | |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4448 | swpReaderRequestedFailIntent |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 4449 | .setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_START_FAIL); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4450 | if (DBG) { |
| 4451 | Log.d(TAG, "SWP READER - Requested Fail"); |
| 4452 | } |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4453 | mContext.sendBroadcast(swpReaderRequestedFailIntent); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4454 | break; |
| 4455 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4456 | case MSG_ETSI_START_CONFIG: |
| 4457 | { |
| 4458 | Log.d(TAG, "NfcServiceHandler - MSG_ETSI_START_CONFIG"); |
| 4459 | ArrayList<Integer> configList = (ArrayList<Integer>) msg.obj; |
| 4460 | int eeHandle; |
| 4461 | if(configList.contains(0x402)) |
| 4462 | { |
| 4463 | eeHandle = 0x402; |
| 4464 | } |
| 4465 | else{ |
| 4466 | eeHandle = 0x4C0; |
| 4467 | } |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 4468 | synchronized (NfcService.this) { |
| 4469 | etsiStartConfig(eeHandle); |
| 4470 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4471 | } |
| 4472 | break; |
| 4473 | |
| 4474 | case MSG_ETSI_STOP_CONFIG: |
| 4475 | |
| 4476 | Log.d(TAG, "NfcServiceHandler - MSG_ETSI_STOP_CONFIG"); |
| 4477 | |
| 4478 | etsiStopConfig((int)msg.obj); |
| 4479 | break; |
| 4480 | |
| 4481 | case MSG_ETSI_SWP_TIMEOUT: |
| 4482 | |
| 4483 | Log.d(TAG, "NfcServiceHandler - MSG_ETSI_SWP_TIMEOUT"); |
| 4484 | |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 4485 | /* Send broadcast ordered */ |
| 4486 | Intent swpReaderTimeoutIntent = new Intent(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4487 | |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 4488 | swpReaderTimeoutIntent |
| 4489 | .setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_TIMEOUT); |
| 4490 | if (DBG) { |
| 4491 | Log.d(TAG, "SWP READER - Timeout"); |
| 4492 | } |
| 4493 | mContext.sendBroadcast(swpReaderTimeoutIntent); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4494 | break; |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 4495 | |
| 4496 | case MSG_SWP_READER_RESTART: |
| 4497 | |
| 4498 | Log.d(TAG, "NfcServiceHandler - MSG_SWP_READER_RESTART"); |
| 4499 | |
| 4500 | /* Send broadcast ordered */ |
| 4501 | Intent swpReaderRestartIntent = new Intent(); |
| 4502 | |
| 4503 | swpReaderRestartIntent |
| 4504 | .setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_RESTART); |
| 4505 | if (DBG) { |
| 4506 | Log.d(TAG, "SWP READER - RESTART"); |
| 4507 | } |
| 4508 | mContext.sendBroadcast(swpReaderRestartIntent); |
| 4509 | break; |
| 4510 | |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 4511 | case MSG_APPLY_SCREEN_STATE: |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4512 | |
| 4513 | mScreenState = (int)msg.obj; |
Nikhil Chhabra | 20be8d9 | 2018-01-09 18:32:58 +0530 | [diff] [blame] | 4514 | |
| 4515 | // If NFC is turning off, we shouldn't need any changes here |
| 4516 | synchronized (NfcService.this) { |
| 4517 | if (mState == NfcAdapter.STATE_TURNING_OFF) |
| 4518 | return; |
| 4519 | } |
| 4520 | |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 4521 | int screen_state_mask = (mNfcUnlockManager.isLockscreenPollingEnabled()) ? |
| 4522 | (ScreenStateHelper.SCREEN_POLLING_TAG_MASK | mScreenState) : mScreenState; |
Suhas Suresh | d8a71d2 | 2017-11-06 18:48:14 +0530 | [diff] [blame] | 4523 | mDeviceHost.doSetScreenOrPowerState(screen_state_mask); |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 4524 | |
Bhupendra Pawar | e1c7ede | 2018-02-26 11:51:32 +0530 | [diff] [blame] | 4525 | if(nci_version != NCI_VERSION_2_0) { |
| 4526 | applyRouting(false); |
| 4527 | } else if(mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED |
Suhas Suresh | d8a71d2 | 2017-11-06 18:48:14 +0530 | [diff] [blame] | 4528 | || mNfcUnlockManager.isLockscreenPollingEnabled()) { |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 4529 | applyRouting(false); |
Suhas Suresh | d8a71d2 | 2017-11-06 18:48:14 +0530 | [diff] [blame] | 4530 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4531 | break; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4532 | case MSG_TAG_DEBOUNCE: |
| 4533 | // Didn't see the tag again, tag is gone |
| 4534 | ITagRemovedCallback tagRemovedCallback; |
| 4535 | synchronized (NfcService.this) { |
| 4536 | mDebounceTagUid = null; |
| 4537 | tagRemovedCallback = mDebounceTagRemovedCallback; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4538 | mDebounceTagRemovedCallback = null; |
Nikhil Chhabra | 1b2368f | 2018-01-09 18:34:21 +0530 | [diff] [blame] | 4539 | mDebounceTagNativeHandle = INVALID_NATIVE_HANDLE; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 4540 | } |
| 4541 | if (tagRemovedCallback != null) { |
| 4542 | try { |
| 4543 | tagRemovedCallback.onTagRemoved(); |
| 4544 | } catch (RemoteException e) { |
| 4545 | // Ignore |
| 4546 | } |
| 4547 | } |
| 4548 | break; |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 4549 | case MSG_RESTART_WATCHDOG: |
| 4550 | int enable = msg.arg1; |
| 4551 | disableInternalwatchDog.cancel(); |
| 4552 | try{ |
| 4553 | disableInternalwatchDog.join(); |
| 4554 | } catch (java.lang.InterruptedException e) { |
| 4555 | } |
| 4556 | disableInternalwatchDog = new WatchDogThread("disableInternal", ROUTING_WATCHDOG_MS); |
| 4557 | Log.d(TAG, "New Watchdog: WatchDog Thread ID is "+ disableInternalwatchDog.getId()); |
| 4558 | disableInternalwatchDog.start(); |
| 4559 | break; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4560 | case MSG_UPDATE_STATS: |
| 4561 | if (mNumTagsDetected.get() > 0) { |
| 4562 | MetricsLogger.count(mContext, TRON_NFC_TAG, mNumTagsDetected.get()); |
| 4563 | mNumTagsDetected.set(0); |
| 4564 | } |
| 4565 | if (mNumHceDetected.get() > 0) { |
| 4566 | MetricsLogger.count(mContext, TRON_NFC_CE, mNumHceDetected.get()); |
| 4567 | mNumHceDetected.set(0); |
| 4568 | } |
| 4569 | if (mNumP2pDetected.get() > 0) { |
| 4570 | MetricsLogger.count(mContext, TRON_NFC_P2P, mNumP2pDetected.get()); |
| 4571 | mNumP2pDetected.set(0); |
| 4572 | } |
| 4573 | removeMessages(MSG_UPDATE_STATS); |
| 4574 | sendEmptyMessageDelayed(MSG_UPDATE_STATS, STATS_UPDATE_INTERVAL_MS); |
| 4575 | break; |
nxpandroid | a5fd662 | 2017-07-31 16:15:18 +0530 | [diff] [blame] | 4576 | |
| 4577 | case MSG_ROUTE_APDU:{ |
| 4578 | int route = msg.arg1; |
| 4579 | int power = msg.arg2; |
| 4580 | String apduData = null; |
| 4581 | String apduMask = null; |
| 4582 | Bundle dataBundle = msg.getData(); |
| 4583 | if (dataBundle != null) { |
| 4584 | apduData = dataBundle.getString("apduData"); |
| 4585 | apduMask = dataBundle.getString("apduMask"); |
| 4586 | } |
| 4587 | // Send the APDU |
| 4588 | if(apduData != null && dataBundle != null) |
| 4589 | mDeviceHost.routeApduPattern(route, power, hexStringToBytes(apduData) ,hexStringToBytes(apduMask)); |
| 4590 | break; |
| 4591 | } |
| 4592 | case MSG_UNROUTE_APDU: { |
| 4593 | String apdu = (String) msg.obj; |
| 4594 | mDeviceHost.unrouteApduPattern(hexStringToBytes(apdu)); |
| 4595 | break; |
| 4596 | } |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4597 | case MSG_TRANSACTION_EVENT: |
| 4598 | if (mCardEmulationManager != null) { |
| 4599 | mCardEmulationManager.onOffHostAidSelected(); |
| 4600 | } |
| 4601 | byte[][] data = (byte[][]) msg.obj; |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 4602 | sendOffHostTransactionEvent(data[0], data[1], data[2]); |
| 4603 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4604 | default: |
| 4605 | Log.e(TAG, "Unknown message received"); |
| 4606 | break; |
| 4607 | } |
| 4608 | } |
| 4609 | |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 4610 | private void sendOffHostTransactionEvent(byte[] aid, byte[] data, byte[] readerByteArray) { |
Suhas Suresh | 3824995 | 2018-05-18 00:03:00 +0530 | [diff] [blame] | 4611 | |
Ganesh Deva | 272ad05 | 2018-07-09 15:56:45 +0530 | [diff] [blame] | 4612 | if (!isSEServiceAvailable() || mNfcEventInstalledPackages.isEmpty()) { |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4613 | return; |
| 4614 | } |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4615 | try { |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 4616 | String reader = new String(readerByteArray, "UTF-8"); |
| 4617 | String[] installedPackages = new String[mNfcEventInstalledPackages.size()]; |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4618 | boolean[] nfcAccess = mSEService.isNFCEventAllowed(reader, aid, |
| 4619 | mNfcEventInstalledPackages.toArray(installedPackages)); |
| 4620 | if (nfcAccess == null) { |
| 4621 | return; |
| 4622 | } |
| 4623 | ArrayList<String> packages = new ArrayList<String>(); |
nxf38293 | fa39fab | 2018-07-23 15:50:08 +0530 | [diff] [blame] | 4624 | Intent intent = new Intent(NfcAdapter.ACTION_TRANSACTION_DETECTED); |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4625 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 4626 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
nxf38293 | fa39fab | 2018-07-23 15:50:08 +0530 | [diff] [blame] | 4627 | intent.putExtra(NfcAdapter.EXTRA_AID, aid); |
| 4628 | intent.putExtra(NfcAdapter.EXTRA_DATA, data); |
Ganesh Deva | 2c02705 | 2018-08-07 15:18:32 +0530 | [diff] [blame] | 4629 | intent.putExtra(NfcAdapter.EXTRA_SECURE_ELEMENT_NAME, reader); |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 4630 | StringBuilder aidString = new StringBuilder(aid.length); |
| 4631 | for (byte b : aid) { |
| 4632 | aidString.append(String.format("%02X", b)); |
| 4633 | } |
| 4634 | String url = new String ("nfc://secure:0/" + reader + "/" + aidString.toString()); |
| 4635 | intent.setData(Uri.parse(url)); |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4636 | for (int i = 0; i < nfcAccess.length; i++) { |
| 4637 | if (nfcAccess[i]) { |
Suhas Suresh | 140f7ac | 2018-05-15 14:59:11 +0530 | [diff] [blame] | 4638 | intent.setPackage(mNfcEventInstalledPackages.get(i)); |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4639 | mContext.sendBroadcast(intent); |
| 4640 | } |
| 4641 | } |
| 4642 | } catch (RemoteException e) { |
| 4643 | Log.e(TAG, "Error in isNFCEventAllowed() " + e); |
Suhas Suresh | 061c9b0 | 2018-05-15 17:45:06 +0530 | [diff] [blame] | 4644 | } catch (UnsupportedEncodingException e) { |
| 4645 | Log.e(TAG, "Incorrect format for Secure Element name" + e); |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | /* Returns the list of packages that have access to NFC Events on any SE */ |
| 4650 | private ArrayList<String> getSEAccessAllowedPackages() { |
Ganesh Deva | 272ad05 | 2018-07-09 15:56:45 +0530 | [diff] [blame] | 4651 | if (!isSEServiceAvailable() || mNfcEventInstalledPackages.isEmpty()) { |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4652 | return null; |
| 4653 | } |
| 4654 | String[] readers = null; |
| 4655 | try { |
| 4656 | readers = mSEService.getReaders(); |
| 4657 | } catch (RemoteException e) { |
| 4658 | Log.e(TAG, "Error in getReaders() " + e); |
| 4659 | return null; |
| 4660 | } |
| 4661 | |
| 4662 | if (readers == null || readers.length == 0) { |
| 4663 | return null; |
| 4664 | } |
| 4665 | boolean[] nfcAccessFinal = null; |
| 4666 | String[] installedPackages = new String[mNfcEventInstalledPackages.size()]; |
| 4667 | for (String reader : readers) { |
| 4668 | try { |
| 4669 | boolean[] accessList = mSEService.isNFCEventAllowed(reader, null, |
| 4670 | mNfcEventInstalledPackages.toArray(installedPackages)); |
| 4671 | if (accessList == null) { |
| 4672 | continue; |
| 4673 | } |
| 4674 | if (nfcAccessFinal == null) { |
| 4675 | nfcAccessFinal = accessList; |
| 4676 | } |
| 4677 | for (int i = 0; i < accessList.length; i++) { |
| 4678 | if (accessList[i]) { |
| 4679 | nfcAccessFinal[i] = true; |
| 4680 | } |
| 4681 | } |
| 4682 | } catch (RemoteException e) { |
| 4683 | Log.e(TAG, "Error in isNFCEventAllowed() " + e); |
| 4684 | } |
| 4685 | } |
| 4686 | if (nfcAccessFinal == null) { |
| 4687 | return null; |
| 4688 | } |
| 4689 | ArrayList<String> packages = new ArrayList<String>(); |
| 4690 | for (int i = 0; i < nfcAccessFinal.length; i++) { |
| 4691 | if (nfcAccessFinal[i]) { |
Suhas Suresh | 140f7ac | 2018-05-15 14:59:11 +0530 | [diff] [blame] | 4692 | packages.add(mNfcEventInstalledPackages.get(i)); |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | return packages; |
| 4696 | } |
| 4697 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4698 | private void sendSeBroadcast(Intent intent) { |
| 4699 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 4700 | // Resume app switches so the receivers can start activites without delay |
| 4701 | mNfcDispatcher.resumeAppSwitches(); |
nxpandroid | a51b5bd | 2017-04-10 18:28:21 +0530 | [diff] [blame] | 4702 | Log.d(TAG, "NFCINTENT sendNfcEeAccessProtectedBroadcast"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4703 | synchronized(this) { |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4704 | ArrayList<String> SEPackages = getSEAccessAllowedPackages(); |
| 4705 | if (SEPackages!= null && !SEPackages.isEmpty()) { |
| 4706 | for (String packageName : SEPackages) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4707 | intent.setPackage(packageName); |
nxpandroid | a51b5bd | 2017-04-10 18:28:21 +0530 | [diff] [blame] | 4708 | Log.d(TAG, "NFCINTENT SENT TO PACKAGE" + packageName); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4709 | mContext.sendBroadcast(intent); |
| 4710 | } |
Suhas Suresh | ca6584b | 2018-04-27 17:17:22 +0530 | [diff] [blame] | 4711 | } |
Suhas Suresh | 140f7ac | 2018-05-15 14:59:11 +0530 | [diff] [blame] | 4712 | PackageManager pm = mContext.getPackageManager(); |
| 4713 | for (String packageName : mNfcEventInstalledPackages) { |
| 4714 | try { |
| 4715 | PackageInfo info = pm.getPackageInfo(packageName, 0); |
| 4716 | if (SEPackages != null && SEPackages.contains(packageName)) { |
| 4717 | continue; |
| 4718 | } |
| 4719 | if (info.applicationInfo != null && |
| 4720 | ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 || |
| 4721 | (info.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0)) { |
| 4722 | intent.setPackage(packageName); |
| 4723 | mContext.sendBroadcast(intent); |
| 4724 | } |
| 4725 | } catch (Exception e) { |
| 4726 | Log.e(TAG, "Exception in getPackageInfo " + e); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | } |
| 4731 | |
| 4732 | private void sendMultiEvtBroadcast(Intent intent) { |
| 4733 | |
| 4734 | ArrayList<String> packageList = mNxpNfcController.getEnabledMultiEvtsPackageList(); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 4735 | ComponentName unicastComponent = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4736 | if(packageList.size() == 0) { |
| 4737 | Log.d(TAG, "No packages to send broadcast."); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 4738 | unicastComponent = mNxpNfcController.getUnicastPackage(); |
| 4739 | if(unicastComponent != null) |
| 4740 | { |
| 4741 | intent.setComponent(unicastComponent); |
| 4742 | try { |
| 4743 | //start gsma |
| 4744 | Log.d(TAG, "Starting activity uincast Pkg"+unicastComponent.flattenToString()); |
| 4745 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 4746 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4747 | if(mContext.getPackageManager().resolveActivity(intent, 0) != null) |
| 4748 | { |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 4749 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 4750 | } else { |
| 4751 | Log.d(TAG, "Intent not resolved"); |
| 4752 | } |
| 4753 | } catch (Exception e) { |
| 4754 | if (DBG) Log.d(TAG, "Exception: " + e.getMessage()); |
| 4755 | } |
| 4756 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4757 | return; |
| 4758 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4759 | for(int i=0; i<packageList.size(); i++) { |
| 4760 | Log.d(TAG,"MultiEvt Enabled Application packageName: " + packageList.get(i)); |
| 4761 | intent.setPackage(packageList.get(i)); |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 4762 | mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT, |
| 4763 | NxpConstants.PERMISSIONS_TRANSACTION_EVENT); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | |
| 4767 | private boolean llcpActivated(NfcDepEndpoint device) { |
| 4768 | Log.d(TAG, "LLCP Activation message"); |
| 4769 | |
| 4770 | if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) { |
| 4771 | if (DBG) Log.d(TAG, "NativeP2pDevice.MODE_P2P_TARGET"); |
| 4772 | if (device.connect()) { |
| 4773 | /* Check LLCP compliance */ |
| 4774 | if (mDeviceHost.doCheckLlcp()) { |
| 4775 | /* Activate LLCP Link */ |
| 4776 | if (mDeviceHost.doActivateLlcp()) { |
| 4777 | if (DBG) Log.d(TAG, "Initiator Activate LLCP OK"); |
| 4778 | synchronized (NfcService.this) { |
| 4779 | // Register P2P device |
| 4780 | mObjectMap.put(device.getHandle(), device); |
| 4781 | } |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 4782 | mP2pLinkManager.onLlcpActivated(device.getLlcpVersion()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4783 | return true; |
| 4784 | } else { |
| 4785 | /* should not happen */ |
| 4786 | Log.w(TAG, "Initiator LLCP activation failed. Disconnect."); |
| 4787 | device.disconnect(); |
| 4788 | } |
| 4789 | } else { |
| 4790 | if (DBG) Log.d(TAG, "Remote Target does not support LLCP. Disconnect."); |
| 4791 | device.disconnect(); |
| 4792 | } |
| 4793 | } else { |
| 4794 | if (DBG) Log.d(TAG, "Cannot connect remote Target. Polling loop restarted."); |
| 4795 | /* |
| 4796 | * The polling loop should have been restarted in failing |
| 4797 | * doConnect |
| 4798 | */ |
| 4799 | } |
| 4800 | } else if (device.getMode() == NfcDepEndpoint.MODE_P2P_INITIATOR) { |
| 4801 | if (DBG) Log.d(TAG, "NativeP2pDevice.MODE_P2P_INITIATOR"); |
| 4802 | /* Check LLCP compliancy */ |
| 4803 | if (mDeviceHost.doCheckLlcp()) { |
| 4804 | /* Activate LLCP Link */ |
| 4805 | if (mDeviceHost.doActivateLlcp()) { |
| 4806 | if (DBG) Log.d(TAG, "Target Activate LLCP OK"); |
| 4807 | synchronized (NfcService.this) { |
| 4808 | // Register P2P device |
| 4809 | mObjectMap.put(device.getHandle(), device); |
| 4810 | } |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 4811 | mP2pLinkManager.onLlcpActivated(device.getLlcpVersion()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4812 | return true; |
| 4813 | } |
| 4814 | } else { |
| 4815 | Log.w(TAG, "checkLlcp failed"); |
| 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | return false; |
| 4820 | } |
| 4821 | |
| 4822 | private void dispatchTagEndpoint(TagEndpoint tagEndpoint, ReaderModeParams readerParams) { |
| 4823 | Tag tag = new Tag(tagEndpoint.getUid(), tagEndpoint.getTechList(), |
| 4824 | tagEndpoint.getTechExtras(), tagEndpoint.getHandle(), mNfcTagService); |
| 4825 | registerTagObject(tagEndpoint); |
| 4826 | if (readerParams != null) { |
| 4827 | try { |
| 4828 | if ((readerParams.flags & NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS) == 0) { |
Suhas Suresh | 6e05ee0 | 2018-04-25 12:19:35 +0530 | [diff] [blame] | 4829 | mVibrator.vibrate(mVibrationEffect); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4830 | playSound(SOUND_END); |
| 4831 | } |
| 4832 | if (readerParams.callback != null) { |
| 4833 | readerParams.callback.onTagDiscovered(tag); |
| 4834 | return; |
| 4835 | } else { |
| 4836 | // Follow normal dispatch below |
| 4837 | } |
| 4838 | } catch (RemoteException e) { |
| 4839 | Log.e(TAG, "Reader mode remote has died, falling back.", e); |
| 4840 | // Intentional fall-through |
| 4841 | } catch (Exception e) { |
| 4842 | // Catch any other exception |
| 4843 | Log.e(TAG, "App exception, not dispatching.", e); |
| 4844 | return; |
| 4845 | } |
| 4846 | } |
| 4847 | int dispatchResult = mNfcDispatcher.dispatchTag(tag); |
| 4848 | if (dispatchResult == NfcDispatcher.DISPATCH_FAIL) { |
| 4849 | unregisterObject(tagEndpoint.getHandle()); |
| 4850 | playSound(SOUND_ERROR); |
| 4851 | } else if (dispatchResult == NfcDispatcher.DISPATCH_SUCCESS) { |
Suhas Suresh | 6e05ee0 | 2018-04-25 12:19:35 +0530 | [diff] [blame] | 4852 | mVibrator.vibrate(mVibrationEffect); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4853 | playSound(SOUND_END); |
| 4854 | } |
| 4855 | } |
| 4856 | } |
| 4857 | |
| 4858 | private NfcServiceHandler mHandler = new NfcServiceHandler(); |
| 4859 | |
| 4860 | class ApplyRoutingTask extends AsyncTask<Integer, Void, Void> { |
| 4861 | @Override |
| 4862 | protected Void doInBackground(Integer... params) { |
| 4863 | synchronized (NfcService.this) { |
| 4864 | if (params == null || params.length != 1) { |
| 4865 | // force apply current routing |
| 4866 | Log.e(TAG, "applyRouting -1"); |
| 4867 | applyRouting(true); |
| 4868 | return null; |
| 4869 | } |
| 4870 | mScreenState = params[0].intValue(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4871 | mRoutingWakeLock.acquire(); |
| 4872 | try { |
| 4873 | Log.e(TAG, "applyRouting -2"); |
| 4874 | applyRouting(false); |
| 4875 | } finally { |
| 4876 | mRoutingWakeLock.release(); |
| 4877 | } |
| 4878 | return null; |
| 4879 | } |
| 4880 | } |
| 4881 | } |
| 4882 | |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 4883 | class EtsiStopConfigTask extends AsyncTask<Integer, Void, Void> { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4884 | @Override |
| 4885 | protected Void doInBackground(Integer... params) { |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 4886 | synchronized (NfcService.this) { |
| 4887 | etsiStopConfig(params[0].intValue()); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4888 | return null; |
Pratap Reddy | 49abbe3 | 2018-03-27 16:51:59 +0530 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | class TagRemoveTaskTimer extends TimerTask { |
| 4894 | public void run() |
| 4895 | { |
| 4896 | Intent swpReaderTagRemoveIntent = new Intent(); |
| 4897 | swpReaderTagRemoveIntent.setAction(NxpConstants.ACTION_NFC_MPOS_READER_MODE_REMOVE_CARD); |
| 4898 | if (DBG) { |
| 4899 | Log.d(TAG, "SWP READER - Tag Remove"); |
| 4900 | } |
| 4901 | mContext.sendBroadcast(swpReaderTagRemoveIntent); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | |
| 4906 | private final BroadcastReceiver x509CertificateReceiver = new BroadcastReceiver() { |
| 4907 | @Override |
| 4908 | public void onReceive(Context context, Intent intent) { |
| 4909 | boolean result = intent.getBooleanExtra(NxpConstants.EXTRA_RESULT, false); |
| 4910 | mNxpNfcController.setResultForCertificates(result); |
| 4911 | } |
| 4912 | }; |
| 4913 | |
| 4914 | private final BroadcastReceiver mEnableNfc = new BroadcastReceiver() { |
| 4915 | @Override |
| 4916 | public void onReceive(Context context, Intent intent) { |
| 4917 | String action = intent.getAction(); |
| 4918 | Intent nfcDialogIntent = new Intent(mContext, EnableNfcDialogActivity.class); |
| 4919 | nfcDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 4920 | mContext.startActivityAsUser(nfcDialogIntent, UserHandle.CURRENT); |
| 4921 | } |
| 4922 | }; |
| 4923 | |
Shashank vimal | 8377908 | 2018-02-06 18:10:31 +0530 | [diff] [blame] | 4924 | private final BroadcastReceiver mActivateSwpInterface = new BroadcastReceiver() { |
| 4925 | @Override |
| 4926 | public void onReceive(Context context, Intent intent) { |
| 4927 | |
| 4928 | String action = intent.getAction(); |
| 4929 | |
| 4930 | if(NxpConstants.CAT_ACTIVATE_NOTIFY_ACTION.equals(action)){ |
| 4931 | |
| 4932 | Log.i(TAG, "Received ACTIVATE intent:" + action); |
| 4933 | |
| 4934 | if(mSelectedSeId != ALL_SE_ID_TYPE){ |
| 4935 | mDeviceHost.doSelectSecureElement(mSelectedSeId); |
| 4936 | Log.i(TAG, "Activated:" + mSelectedSeId); |
| 4937 | } |
| 4938 | else{ |
| 4939 | int[] seList = mDeviceHost.doGetSecureElementList(); |
| 4940 | Log.i(TAG, "Activating all SE:"); |
| 4941 | |
| 4942 | for(int i = 0; i < seList.length; i++){ |
| 4943 | mDeviceHost.doActivateSecureElement(seList[i]); |
| 4944 | try{ |
| 4945 | //Delay between two SE selection |
| 4946 | Thread.sleep(200); |
| 4947 | }catch(Exception e){ |
| 4948 | e.printStackTrace(); |
| 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | } |
| 4953 | } |
| 4954 | }; |
| 4955 | |
| 4956 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4957 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 4958 | @Override |
| 4959 | public void onReceive(Context context, Intent intent) { |
| 4960 | String action = intent.getAction(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4961 | Log.e(TAG, "screen state "+action); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4962 | if (action.equals( |
| 4963 | NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION)) { |
| 4964 | // Perform applyRouting() in AsyncTask to serialize blocking calls |
| 4965 | new ApplyRoutingTask().execute(); |
| 4966 | } else if ((action.equals(Intent.ACTION_SCREEN_ON) |
| 4967 | || action.equals(Intent.ACTION_SCREEN_OFF) |
| 4968 | || action.equals(Intent.ACTION_USER_PRESENT)) && |
| 4969 | mState == NfcAdapter.STATE_ON) { |
| 4970 | // Perform applyRouting() in AsyncTask to serialize blocking calls |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4971 | int screenState = mScreenStateHelper.checkScreenState(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4972 | if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4973 | if(mScreenState != ScreenStateHelper.SCREEN_STATE_OFF_LOCKED) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4974 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4975 | Log.e(TAG, "screen state OFF required"); |
| 4976 | screenState = mKeyguard.isKeyguardLocked() ? |
| 4977 | ScreenStateHelper.SCREEN_STATE_OFF_LOCKED : |
| 4978 | ScreenStateHelper.SCREEN_STATE_OFF_UNLOCKED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4979 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4980 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 4981 | screenState = mKeyguard.isKeyguardLocked() ? |
| 4982 | ScreenStateHelper.SCREEN_STATE_ON_LOCKED : ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4983 | Log.e(TAG, "screen state on"); |
| 4984 | /* |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4985 | if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED && mScreenState == ScreenStateHelper.SCREEN_STATE_OFF) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4986 | } else if (screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED && mScreenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED) { |
| 4987 | return; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4988 | }*/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4989 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 4990 | if (mScreenState != ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED) { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 4991 | Log.e(TAG, "screen state user present"); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4992 | screenState = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 4993 | } else { |
| 4994 | return; |
| 4995 | } |
| 4996 | } |
Bhupendra Pawar | e1c7ede | 2018-02-26 11:51:32 +0530 | [diff] [blame] | 4997 | /* This piece of code is duplicate as MSG_APPLY_SCREEN_STATE |
| 4998 | is already calling applyRouting api */ |
| 4999 | /*if(nci_version != NCI_VERSION_2_0) { |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 5000 | new ApplyRoutingTask().execute(Integer.valueOf(screenState)); |
Bhupendra Pawar | e1c7ede | 2018-02-26 11:51:32 +0530 | [diff] [blame] | 5001 | }*/ |
Pratap Reddy | 7e57f0d | 2018-03-15 16:54:33 +0530 | [diff] [blame] | 5002 | |
| 5003 | if( mDeviceHost.getEtsiReaederState() == STATE_SE_RDR_MODE_STOPPED || |
| 5004 | mDeviceHost.getEtsiReaederState() == STATE_SE_RDR_MODE_INVALID) { |
| 5005 | sendMessage(NfcService.MSG_APPLY_SCREEN_STATE, screenState); |
| 5006 | } else { |
| 5007 | Log.e(TAG, "mPOS in progress holding screen state "+screenState); |
| 5008 | mScreenState = screenState; |
| 5009 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 5010 | Log.e(TAG, "screen state "+screenState); |
| 5011 | Log.e(TAG, "screen state mScreenState "+mScreenState); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5012 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 5013 | int screenState = mScreenStateHelper.checkScreenState(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5014 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
Suhas Suresh | e2a2ff0 | 2018-04-27 12:21:19 +0530 | [diff] [blame] | 5015 | int beamSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; |
| 5016 | try { |
| 5017 | IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); |
| 5018 | beamSetting = mIpm.getComponentEnabledSetting(new ComponentName( |
| 5019 | BeamShareActivity.class.getPackageName$(), |
| 5020 | BeamShareActivity.class.getName()), |
| 5021 | userId); |
| 5022 | } catch(RemoteException e) { |
| 5023 | Log.e(TAG, "Error int getComponentEnabledSetting for BeamShareActivity"); |
| 5024 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5025 | synchronized (this) { |
| 5026 | mUserId = userId; |
Suhas Suresh | e2a2ff0 | 2018-04-27 12:21:19 +0530 | [diff] [blame] | 5027 | if (beamSetting == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) { |
| 5028 | mIsNdefPushEnabled = false; |
| 5029 | } else { |
| 5030 | mIsNdefPushEnabled = true; |
| 5031 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5032 | } |
| 5033 | mP2pLinkManager.onUserSwitched(getUserId()); |
| 5034 | if (mIsHceCapable) { |
| 5035 | mCardEmulationManager.onUserSwitched(getUserId()); |
| 5036 | } |
nxpandroid | e66eb09 | 2017-07-12 21:36:08 +0530 | [diff] [blame] | 5037 | screenState = mScreenStateHelper.checkScreenState(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5038 | new ApplyRoutingTask().execute(Integer.valueOf(screenState)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5039 | } |
| 5040 | } |
| 5041 | }; |
| 5042 | |
| 5043 | private final BroadcastReceiver mOwnerReceiver = new BroadcastReceiver() { |
| 5044 | @Override |
| 5045 | public void onReceive(Context context, Intent intent) { |
| 5046 | String action = intent.getAction(); |
| 5047 | if (action.equals(Intent.ACTION_PACKAGE_REMOVED) || |
| 5048 | action.equals(Intent.ACTION_PACKAGE_ADDED) || |
| 5049 | action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE) || |
| 5050 | action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| 5051 | updatePackageCache(); |
| 5052 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 5053 | } else if(action.equals(Intent.ACTION_SHUTDOWN)) { |
| 5054 | mPowerShutDown = true; |
| 5055 | if (DBG) Log.d(TAG,"Device is shutting down."); |
| 5056 | mDeviceHost.doSetScreenOrPowerState(ScreenStateHelper.POWER_STATE_OFF); |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 5057 | if (isNfcEnabled()) { |
| 5058 | mDeviceHost.shutdown(); |
| 5059 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5060 | } |
| 5061 | } |
| 5062 | }; |
| 5063 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5064 | private final BroadcastReceiver mPolicyReceiver = new BroadcastReceiver() { |
| 5065 | @Override |
| 5066 | public void onReceive(Context context, Intent intent){ |
| 5067 | String action = intent.getAction(); |
| 5068 | if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED |
| 5069 | .equals(action)) { |
| 5070 | enforceBeamShareActivityPolicy(context, |
Suhas Suresh | a18dee0 | 2018-04-27 15:28:04 +0530 | [diff] [blame] | 5071 | new UserHandle(getSendingUserId())); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | }; |
| 5075 | |
Nikhil Chhabra | fc8f3f6 | 2018-01-08 20:48:49 +0530 | [diff] [blame] | 5076 | private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() { |
| 5077 | @Override |
| 5078 | public void onVrStateChanged(boolean enabled) { |
| 5079 | synchronized (this) { |
| 5080 | mIsVrModeEnabled = enabled; |
| 5081 | } |
| 5082 | } |
| 5083 | }; |
| 5084 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5085 | /** |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5086 | * for debugging only - no i18n |
| 5087 | */ |
| 5088 | static String stateToString(int state) { |
| 5089 | switch (state) { |
| 5090 | case NfcAdapter.STATE_OFF: |
| 5091 | return "off"; |
| 5092 | case NfcAdapter.STATE_TURNING_ON: |
| 5093 | return "turning on"; |
| 5094 | case NfcAdapter.STATE_ON: |
| 5095 | return "on"; |
| 5096 | case NfcAdapter.STATE_TURNING_OFF: |
| 5097 | return "turning off"; |
| 5098 | default: |
| 5099 | return "<error>"; |
| 5100 | } |
| 5101 | } |
| 5102 | |
| 5103 | void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 5104 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 5105 | != PackageManager.PERMISSION_GRANTED) { |
| 5106 | pw.println("Permission Denial: can't dump nfc from from pid=" |
| 5107 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 5108 | + " without permission " + android.Manifest.permission.DUMP); |
| 5109 | return; |
| 5110 | } |
| 5111 | |
| 5112 | synchronized (this) { |
| 5113 | pw.println("mState=" + stateToString(mState)); |
| 5114 | pw.println("mIsZeroClickRequested=" + mIsNdefPushEnabled); |
| 5115 | pw.println("mScreenState=" + ScreenStateHelper.screenStateToString(mScreenState)); |
| 5116 | pw.println("mNfcPollingEnabled=" + mNfcPollingEnabled); |
| 5117 | pw.println("mNfceeRouteEnabled=" + mNfceeRouteEnabled); |
| 5118 | pw.println("mOpenEe=" + mOpenEe); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5119 | pw.println("mLockscreenPollMask=" + mLockscreenPollMask); |
| 5120 | pw.println(mCurrentDiscoveryParameters); |
| 5121 | mP2pLinkManager.dump(fd, pw, args); |
| 5122 | if (mIsHceCapable) { |
| 5123 | mCardEmulationManager.dump(fd, pw, args); |
| 5124 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5125 | mNfcDispatcher.dump(fd, pw, args); |
Nikhil Chhabra | 9645ab6 | 2018-01-09 18:44:05 +0530 | [diff] [blame] | 5126 | pw.flush(); |
Nikhil Chhabra | 1e1ac46 | 2018-01-10 12:53:43 +0530 | [diff] [blame] | 5127 | mDeviceHost.dump(fd); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5128 | |
| 5129 | } |
| 5130 | } |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 5131 | /** |
| 5132 | * Update the status of all the services which were populated to commit to routing table |
| 5133 | */ |
| 5134 | public void updateStatusOfServices(boolean commitStatus) { |
nxpandroid | ebf53fb | 2016-12-22 18:48:59 +0530 | [diff] [blame] | 5135 | if(commitStatus == true) |
| 5136 | { |
| 5137 | mAidCache.setPreviousPreferredPaymentService(null); |
| 5138 | } |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 5139 | mCardEmulationManager.updateStatusOfServices(commitStatus); |
| 5140 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 5141 | } |