NfcService.java revision bf6e5d1655d5ad524a8ec007413c7011ed969df8
1f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly/*
2f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * Copyright (C) 2010 The Android Open Source Project
3f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly *
4f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
5f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * you may not use this file except in compliance with the License.
6f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * You may obtain a copy of the License at
7f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly *
8f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
9f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly *
10f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * Unless required by applicable law or agreed to in writing, software
11f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
12f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * See the License for the specific language governing permissions and
14f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly * limitations under the License.
15f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly */
16f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
1713d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellypackage com.android.nfc;
18f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
19f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamiltonimport com.android.nfc.DeviceHost.DeviceHostListener;
204a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamiltonimport com.android.nfc.DeviceHost.LlcpServerSocket;
214a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamiltonimport com.android.nfc.DeviceHost.LlcpSocket;
22f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamiltonimport com.android.nfc.DeviceHost.NfcDepEndpoint;
23f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamiltonimport com.android.nfc.DeviceHost.TagEndpoint;
2481c476dd93f059d4082c15369894d5d16fbea05dJeff Hamiltonimport com.android.nfc.nxp.NativeNfcManager;
2581c476dd93f059d4082c15369894d5d16fbea05dJeff Hamiltonimport com.android.nfc.nxp.NativeNfcSecureElement;
268afd14d3b23d3124c48ee275ba2845aede6542a1Jeff Hamiltonimport com.android.nfc3.R;
27d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
282f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pellyimport android.app.Application;
29275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parksimport android.app.KeyguardManager;
3005973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamiltonimport android.app.PendingIntent;
3113d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.BroadcastReceiver;
3205973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamiltonimport android.content.ComponentName;
3331949217328bf2357ff044f0d18677fe588c790cNick Pellyimport android.content.ContentResolver;
3413d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.Context;
3513d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.Intent;
3613d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.content.IntentFilter;
370e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pellyimport android.content.SharedPreferences;
3893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamiltonimport android.content.pm.PackageManager;
39d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamiltonimport android.media.AudioManager;
40d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamiltonimport android.media.SoundPool;
413fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamiltonimport android.net.Uri;
42f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.ErrorCodes;
43f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.FormatException;
442094515fca0cfa0ac87e9cc260d3953d416afe3eJason parksimport android.nfc.INdefPushCallback;
450e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pellyimport android.nfc.INfcAdapter;
4649d53329a0c720a7e430220d77805bc1763545b1Nick Pellyimport android.nfc.INfcAdapterExtras;
47f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.INfcTag;
48f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.NdefMessage;
49f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.nfc.NfcAdapter;
500e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pellyimport android.nfc.Tag;
5124dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamiltonimport android.nfc.TechListParcel;
529d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenenimport android.nfc.TransceiveResult;
53aca0d055a82da850c27f6872405602ad5f3fee7bJeff Hamiltonimport android.nfc.tech.Ndef;
5481c476dd93f059d4082c15369894d5d16fbea05dJeff Hamiltonimport android.nfc.tech.TagTechnology;
557c034a7fe7d36b1ab039af2c44717812ea02657eNick Pellyimport android.os.AsyncTask;
56b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamiltonimport android.os.Bundle;
57b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneauimport android.os.Handler;
5849d53329a0c720a7e430220d77805bc1763545b1Nick Pellyimport android.os.IBinder;
59b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneauimport android.os.Message;
60533043d1003de2f6a20a29201100d94c3c7bc9caNick Pellyimport android.os.PowerManager;
614467dca5650a170af5020c10a8ccb25f86f1007fNick Pellyimport android.os.Process;
62f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.os.RemoteException;
6313d8819d9d716c8f0ba03288d058f0bd462d70a7Nick Pellyimport android.os.ServiceManager;
64d11251b2d1fed7b7325c4fcb0616b2d1c654320fMartijn Coenenimport android.provider.Settings;
65f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pellyimport android.util.Log;
66f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
6731949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.io.FileDescriptor;
6857d376f1ee1a3939977b95759525585abb9601fbJeff Hamiltonimport java.io.IOException;
6931949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.io.PrintWriter;
7031949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.util.Arrays;
713ca6ffff72f4599e80f85de5ae8e7f55012f38d6Jeff Hamiltonimport java.util.HashMap;
7284e1e0adc2516afd35ebab029a52e764e0490559Jason parksimport java.util.HashSet;
7331949217328bf2357ff044f0d18677fe588c790cNick Pellyimport java.util.concurrent.ExecutionException;
743ca6ffff72f4599e80f85de5ae8e7f55012f38d6Jeff Hamilton
7577d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pellypublic class NfcService extends Application implements DeviceHostListener {
76bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks    private static final String ACTION_MASTER_CLEAR_NOTIFICATION = "android.intent.action.MASTER_CLEAR_NOTIFICATION";
77bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks
788d6a8dce6706c6c6b3158101f2f3e94a1e0ad946Ben Dodson    static final boolean DBG = true;
7976a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly    static final String TAG = "NfcService";
80fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks
81d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    public static final String SERVICE_NAME = "nfc";
82fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks
83bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly    private static final String NFC_PERM = android.Manifest.permission.NFC;
84bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly    private static final String NFC_PERM_ERROR = "NFC permission required";
85bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly    private static final String ADMIN_PERM = android.Manifest.permission.WRITE_SECURE_SETTINGS;
86bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly    private static final String ADMIN_PERM_ERROR = "WRITE_SECURE_SETTINGS permission required";
87a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly    private static final String NFCEE_ADMIN_PERM = "com.android.nfc.permission.NFCEE_ADMIN";
8893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton    private static final String NFCEE_ADMIN_PERM_ERROR = "NFCEE_ADMIN permission required";
89bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
9077d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly    public static final String PREF = "NfcServicePrefs";
91f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
920e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    private static final String PREF_NFC_ON = "nfc_on";
930e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    private static final boolean NFC_ON_DEFAULT = true;
94d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen    private static final String PREF_ZEROCLICK_ON = "zeroclick_on";
95d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen    private static final boolean ZEROCLICK_ON_DEFAULT = true;
96f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
97a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly    private static final String PREF_FIRST_BOOT = "first_boot";
98a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly
99b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    static final int MSG_NDEF_TAG = 0;
100b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    static final int MSG_CARD_EMULATION = 1;
101b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    static final int MSG_LLCP_LINK_ACTIVATION = 2;
102b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    static final int MSG_LLCP_LINK_DEACTIVATED = 3;
103b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    static final int MSG_TARGET_DESELECTED = 4;
104b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton    static final int MSG_MOCK_NDEF = 7;
105c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas    static final int MSG_SE_FIELD_ACTIVATED = 8;
106c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas    static final int MSG_SE_FIELD_DEACTIVATED = 9;
1072c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    static final int MSG_SE_APDU_RECEIVED = 10;
1082c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    static final int MSG_SE_EMV_CARD_REMOVAL = 11;
1092c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    static final int MSG_SE_MIFARE_ACCESS = 12;
110b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
11131949217328bf2357ff044f0d18677fe588c790cNick Pelly    static final int TASK_ENABLE = 1;
11231949217328bf2357ff044f0d18677fe588c790cNick Pelly    static final int TASK_DISABLE = 2;
11331949217328bf2357ff044f0d18677fe588c790cNick Pelly    static final int TASK_BOOT = 3;
11431949217328bf2357ff044f0d18677fe588c790cNick Pelly    static final int TASK_EE_WIPE = 4;
11531949217328bf2357ff044f0d18677fe588c790cNick Pelly
11649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    // Copied from com.android.nfc_extras to avoid library dependency
11749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    // Must keep in sync with com.android.nfc_extras
11849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    static final int ROUTE_OFF = 1;
11949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    static final int ROUTE_ON_WHEN_SCREEN_ON = 2;
1207efbf69a37134ccbd86a1f6b4121f16b4a80eaaeNick Pelly
12149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    public static final String ACTION_RF_FIELD_ON_DETECTED =
12249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED";
12349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    public static final String ACTION_RF_FIELD_OFF_DETECTED =
12449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED";
12549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    public static final String ACTION_AID_SELECTED =
12649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        "com.android.nfc_extras.action.AID_SELECTED";
12749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID";
12849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly
1292c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    public static final String ACTION_APDU_RECEIVED =
1302c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas        "com.android.nfc_extras.action.APDU_RECEIVED";
1312c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    public static final String EXTRA_APDU_BYTES =
1322c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas        "com.android.nfc_extras.extra.APDU_BYTES";
1332c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas
1342c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    public static final String ACTION_EMV_CARD_REMOVAL =
1352c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas        "com.android.nfc_extras.action.EMV_CARD_REMOVAL";
1362c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas
1372c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    public static final String ACTION_MIFARE_ACCESS_DETECTED =
1382c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas        "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED";
1392c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas    public static final String EXTRA_MIFARE_BLOCK =
1402c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas        "com.android.nfc_extras.extra.MIFARE_BLOCK";
1412c37e6a839cecf5638911af357a2ea7aec6093a5Daniel Tomas
14231949217328bf2357ff044f0d18677fe588c790cNick Pelly    //TODO: dont hardcode this
14331949217328bf2357ff044f0d18677fe588c790cNick Pelly    private static final byte[][] EE_WIPE_APDUS = {
14431949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x00},
14531949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x07, (byte)0xa0, (byte)0x00,
14631949217328bf2357ff044f0d18677fe588c790cNick Pelly                (byte)0x00, (byte)0x04, (byte)0x76, (byte)0x20, (byte)0x10, (byte)0x00},
14731949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x80, (byte)0xe2, (byte)0x01, (byte)0x03, (byte)0x00},
14831949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x00},
14931949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x07, (byte)0xa0, (byte)0x00,
15031949217328bf2357ff044f0d18677fe588c790cNick Pelly                (byte)0x00, (byte)0x04, (byte)0x76, (byte)0x30, (byte)0x30, (byte)0x00},
15131949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x80, (byte)0xb4, (byte)0x00, (byte)0x00, (byte)0x00},
15231949217328bf2357ff044f0d18677fe588c790cNick Pelly        {(byte)0x00, (byte)0xa4, (byte)0x04, (byte)0x00, (byte)0x00},
15331949217328bf2357ff044f0d18677fe588c790cNick Pelly    };
15449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly
15549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    // NFC Execution Environment
15649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    // fields below are protected by this
1570bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas    private NativeNfcSecureElement mSecureElement;
15849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    private OpenSecureElement mOpenEe;  // null when EE closed
15949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    private int mEeRoutingState;  // contactless interface routing
1600bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
161d2d5dddf17ac2008547172cd72faa034a89d569bJeff Hamilton    // fields below must be used only on the UI thread and therefore aren't synchronized
162d2d5dddf17ac2008547172cd72faa034a89d569bJeff Hamilton    boolean mP2pStarted = false;
163d2d5dddf17ac2008547172cd72faa034a89d569bJeff Hamilton
1642f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly    // fields below are used in multiple threads and protected by synchronized(this)
1652f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly    private final HashMap<Integer, Object> mObjectMap = new HashMap<Integer, Object>();
16684e1e0adc2516afd35ebab029a52e764e0490559Jason parks    private HashSet<String> mSePackages = new HashSet<String>();
16731949217328bf2357ff044f0d18677fe588c790cNick Pelly    private boolean mIsScreenUnlocked;
16831949217328bf2357ff044f0d18677fe588c790cNick Pelly    private boolean mIsZeroClickRequested;
16931949217328bf2357ff044f0d18677fe588c790cNick Pelly
17031949217328bf2357ff044f0d18677fe588c790cNick Pelly    // mState is protected by this, however it is only modified in onCreate()
17131949217328bf2357ff044f0d18677fe588c790cNick Pelly    // and the default AsyncTask thread so it is read unprotected from that
17231949217328bf2357ff044f0d18677fe588c790cNick Pelly    // thread
17331949217328bf2357ff044f0d18677fe588c790cNick Pelly    int mState;  // one of NfcAdapter.STATE_ON, STATE_TURNING_ON, etc
1742f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly
1752f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly    // fields below are final after onCreate()
17605973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton    Context mContext;
1774a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    private DeviceHost mDeviceHost;
1780e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    private SharedPreferences mPrefs;
1790e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    private SharedPreferences.Editor mPrefsEditor;
180533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly    private PowerManager.WakeLock mWakeLock;
181d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    int mStartSound;
182d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    int mEndSound;
183d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    int mErrorSound;
184d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    SoundPool mSoundPool; // playback synchronized on this
18577d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly    P2pLinkManager mP2pLinkManager;
1864a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    TagService mNfcTagService;
1874a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    NfcAdapterService mNfcAdapter;
1884a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    NfcAdapterExtrasService mExtrasService;
18931949217328bf2357ff044f0d18677fe588c790cNick Pelly    boolean mIsAirplaneSensitive;
19031949217328bf2357ff044f0d18677fe588c790cNick Pelly    boolean mIsAirplaneToggleable;
1912ef360deaff9f17aa72d5749ceee283cc80897afBen Dodson
19276a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly    private NfcDispatcher mNfcDispatcher;
193275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks    private KeyguardManager mKeyguard;
194d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
195d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    private static NfcService sService;
196d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
19793d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton    public static void enforceAdminPerm(Context context) {
19893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton        int admin = context.checkCallingOrSelfPermission(ADMIN_PERM);
19993d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton        int nfcee = context.checkCallingOrSelfPermission(NFCEE_ADMIN_PERM);
20093d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton        if (admin != PackageManager.PERMISSION_GRANTED
20193d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton                && nfcee != PackageManager.PERMISSION_GRANTED) {
20293d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            throw new SecurityException(ADMIN_PERM_ERROR);
20393d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton        }
20493d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton    }
20593d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton
20693d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton    public static void enforceNfceeAdminPerm(Context context) {
20793d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton        context.enforceCallingOrSelfPermission(NFCEE_ADMIN_PERM, NFCEE_ADMIN_PERM_ERROR);
20893d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton    }
20993d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton
210d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    public static NfcService getInstance() {
211d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton        return sService;
212d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    }
213f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
2140e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    @Override
215f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onRemoteEndpointDiscovered(TagEndpoint tag) {
216f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_NDEF_TAG, tag);
217f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
218f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
219f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    /**
220f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     * Notifies transaction
221f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     */
222d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    @Override
223f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onCardEmulationDeselected() {
224f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_TARGET_DESELECTED, null);
225f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
226f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
227f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    /**
228f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     * Notifies transaction
229f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     */
230d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    @Override
231f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onCardEmulationAidSelected(byte[] aid) {
232f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_CARD_EMULATION, aid);
233f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
234f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
235f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    /**
236f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     * Notifies P2P Device detected, to activate LLCP link
237f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     */
238f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    @Override
239f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onLlcpLinkActivated(NfcDepEndpoint device) {
240f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_LLCP_LINK_ACTIVATION, device);
241f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
242f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
243f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    /**
244f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     * Notifies P2P Device detected, to activate LLCP link
245f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton     */
246d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    @Override
247f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onLlcpLinkDeactivated(NfcDepEndpoint device) {
248f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_LLCP_LINK_DEACTIVATED, device);
249f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
250f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
251d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    @Override
252f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onRemoteFieldActivated() {
253f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_SE_FIELD_ACTIVATED, null);
254f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
255f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
256d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    @Override
257f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    public void onRemoteFieldDeactivated() {
258f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        sendMessage(NfcService.MSG_SE_FIELD_DEACTIVATED, null);
259f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    }
260f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton
261f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton    @Override
262442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    public void onSeApduReceived(byte[] apdu) {
263442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly        sendMessage(NfcService.MSG_SE_APDU_RECEIVED, apdu);
264442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    }
265442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly
266442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    @Override
267442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    public void onSeEmvCardRemoval() {
268442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly        sendMessage(NfcService.MSG_SE_EMV_CARD_REMOVAL, null);
269442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    }
270442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly
271442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    @Override
272442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    public void onSeMifareAccess(byte[] block) {
273442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly        sendMessage(NfcService.MSG_SE_MIFARE_ACCESS, block);
274442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    }
275442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly
276442cad40a205a63ea8797fbccaee99b2e8ab89abNick Pelly    @Override
2770e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    public void onCreate() {
2782f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly        super.onCreate();
2792f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly
2804a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton        mNfcTagService = new TagService();
2814a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton        mNfcAdapter = new NfcAdapterService();
282ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly        mExtrasService = new NfcAdapterExtrasService();
2834a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton
2842f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly        Log.i(TAG, "Starting NFC service");
2852f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly
286d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton        sService = this;
287d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
288d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
289d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        mStartSound = mSoundPool.load(this, R.raw.start, 1);
290d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        mEndSound = mSoundPool.load(this, R.raw.end, 1);
291d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        mErrorSound = mSoundPool.load(this, R.raw.error, 1);
292d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton
2930e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        mContext = this;
294f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        mDeviceHost = new NativeNfcManager(this, this);
29574180bda362a8bc9d2f701d2c17bec0f63c20bbfBrad Fitzpatrick
29677d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly        mP2pLinkManager = new P2pLinkManager(mContext);
29777d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly        mNfcDispatcher = new NfcDispatcher(this, mP2pLinkManager);
29824dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton
2990bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        mSecureElement = new NativeNfcSecureElement();
300eab09ad7204fe1f0feaca33efccf75c1bb388708Robert Tsai        mEeRoutingState = ROUTE_OFF;
3010bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
302275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        mPrefs = getSharedPreferences(PREF, Context.MODE_PRIVATE);
3030e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        mPrefsEditor = mPrefs.edit();
304f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
30531949217328bf2357ff044f0d18677fe588c790cNick Pelly        mState = NfcAdapter.STATE_OFF;
30631949217328bf2357ff044f0d18677fe588c790cNick Pelly        mIsZeroClickRequested = mPrefs.getBoolean(PREF_ZEROCLICK_ON, ZEROCLICK_ON_DEFAULT);
307f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
308275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
309275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks
310533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NfcService");
311275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        mKeyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
31231949217328bf2357ff044f0d18677fe588c790cNick Pelly        mIsScreenUnlocked = pm.isScreenOn() && !mKeyguard.isKeyguardLocked();
313533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly
314d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton        ServiceManager.addService(SERVICE_NAME, mNfcAdapter);
315f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
316eead88c5e2bdd34eb33fdf2c76717f9edb9e0396Jeff Hamilton        IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION);
31765945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly        filter.addAction(Intent.ACTION_SCREEN_OFF);
31865945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly        filter.addAction(Intent.ACTION_SCREEN_ON);
319bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        filter.addAction(ACTION_MASTER_CLEAR_NOTIFICATION);
320275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        filter.addAction(Intent.ACTION_USER_PRESENT);
32131949217328bf2357ff044f0d18677fe588c790cNick Pelly        registerForAirplaneMode(filter);
322275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        registerReceiver(mReceiver, filter);
323bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks
324bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        filter = new IntentFilter();
325bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
326bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        filter.addDataScheme("package");
327bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks
328275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        registerReceiver(mReceiver, filter);
3290e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
33031949217328bf2357ff044f0d18677fe588c790cNick Pelly        new EnableDisableTask().execute(TASK_BOOT);  // do blocking boot tasks
33131949217328bf2357ff044f0d18677fe588c790cNick Pelly    }
33231949217328bf2357ff044f0d18677fe588c790cNick Pelly
33331949217328bf2357ff044f0d18677fe588c790cNick Pelly    void registerForAirplaneMode(IntentFilter filter) {
33431949217328bf2357ff044f0d18677fe588c790cNick Pelly        final ContentResolver resolver = mContext.getContentResolver();
33531949217328bf2357ff044f0d18677fe588c790cNick Pelly        final String airplaneModeRadios = Settings.System.getString(resolver,
33631949217328bf2357ff044f0d18677fe588c790cNick Pelly                Settings.System.AIRPLANE_MODE_RADIOS);
33731949217328bf2357ff044f0d18677fe588c790cNick Pelly        final String toggleableRadios = Settings.System.getString(resolver,
33831949217328bf2357ff044f0d18677fe588c790cNick Pelly                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
33931949217328bf2357ff044f0d18677fe588c790cNick Pelly
34031949217328bf2357ff044f0d18677fe588c790cNick Pelly        mIsAirplaneSensitive = airplaneModeRadios == null ? true :
34131949217328bf2357ff044f0d18677fe588c790cNick Pelly                airplaneModeRadios.contains(Settings.System.RADIO_NFC);
34231949217328bf2357ff044f0d18677fe588c790cNick Pelly        mIsAirplaneToggleable = toggleableRadios == null ? false :
34331949217328bf2357ff044f0d18677fe588c790cNick Pelly            toggleableRadios.contains(Settings.System.RADIO_NFC);
34431949217328bf2357ff044f0d18677fe588c790cNick Pelly
34531949217328bf2357ff044f0d18677fe588c790cNick Pelly        if (mIsAirplaneSensitive) {
34631949217328bf2357ff044f0d18677fe588c790cNick Pelly            filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
34731949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
34831949217328bf2357ff044f0d18677fe588c790cNick Pelly    }
34931949217328bf2357ff044f0d18677fe588c790cNick Pelly
35031949217328bf2357ff044f0d18677fe588c790cNick Pelly    /**
35131949217328bf2357ff044f0d18677fe588c790cNick Pelly     * Manages tasks that involve turning on/off the NFC controller.
35231949217328bf2357ff044f0d18677fe588c790cNick Pelly     *
35331949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>All work that might turn the NFC adapter on or off must be done
35431949217328bf2357ff044f0d18677fe588c790cNick Pelly     * through this task, to keep the handling of mState simple.
35531949217328bf2357ff044f0d18677fe588c790cNick Pelly     * In other words, mState is only modified in these tasks (and we
35631949217328bf2357ff044f0d18677fe588c790cNick Pelly     * don't need a lock to read it in these tasks).
35731949217328bf2357ff044f0d18677fe588c790cNick Pelly     *
35831949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>These tasks are all done on the same AsyncTask background
35931949217328bf2357ff044f0d18677fe588c790cNick Pelly     * thread, so they are serialized. Each task may temporarily transition
36031949217328bf2357ff044f0d18677fe588c790cNick Pelly     * mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in
36131949217328bf2357ff044f0d18677fe588c790cNick Pelly     * either STATE_ON or STATE_OFF. This way each task can be guaranteed
36231949217328bf2357ff044f0d18677fe588c790cNick Pelly     * of starting in either STATE_OFF or STATE_ON, without needing to hold
36331949217328bf2357ff044f0d18677fe588c790cNick Pelly     * NfcService.this for the entire task.
36431949217328bf2357ff044f0d18677fe588c790cNick Pelly     *
36531949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>AsyncTask's are also implicitly queued. This is useful for corner
36631949217328bf2357ff044f0d18677fe588c790cNick Pelly     * cases like turning airplane mode on while TASK_ENABLE is in progress.
36731949217328bf2357ff044f0d18677fe588c790cNick Pelly     * The TASK_DISABLE triggered by airplane mode will be correctly executed
36831949217328bf2357ff044f0d18677fe588c790cNick Pelly     * immediately after TASK_ENABLE is complete. This seems like the most sane
36931949217328bf2357ff044f0d18677fe588c790cNick Pelly     * way to deal with these situations.
37031949217328bf2357ff044f0d18677fe588c790cNick Pelly     *
37131949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>{@link #TASK_ENABLE} enables the NFC adapter, without changing
37231949217328bf2357ff044f0d18677fe588c790cNick Pelly     * preferences
37331949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>{@link #TASK_DISABLE} disables the NFC adapter, without changing
37431949217328bf2357ff044f0d18677fe588c790cNick Pelly     * preferences
37531949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>{@link #TASK_BOOT} does first boot work and may enable NFC
37631949217328bf2357ff044f0d18677fe588c790cNick Pelly     * <p>{@link #TASK_EE_WIPE} wipes the Execution Environment, and in the
37731949217328bf2357ff044f0d18677fe588c790cNick Pelly     * process may temporarily enable the NFC adapter
37831949217328bf2357ff044f0d18677fe588c790cNick Pelly     */
37931949217328bf2357ff044f0d18677fe588c790cNick Pelly    class EnableDisableTask extends AsyncTask<Integer, Void, Void> {
38031949217328bf2357ff044f0d18677fe588c790cNick Pelly        @Override
38131949217328bf2357ff044f0d18677fe588c790cNick Pelly        protected Void doInBackground(Integer... params) {
38231949217328bf2357ff044f0d18677fe588c790cNick Pelly            // Sanity check mState
38331949217328bf2357ff044f0d18677fe588c790cNick Pelly            switch (mState) {
38431949217328bf2357ff044f0d18677fe588c790cNick Pelly                case NfcAdapter.STATE_TURNING_OFF:
38531949217328bf2357ff044f0d18677fe588c790cNick Pelly                case NfcAdapter.STATE_TURNING_ON:
38631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " +
38731949217328bf2357ff044f0d18677fe588c790cNick Pelly                            mState);
38831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return null;
38931949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
39031949217328bf2357ff044f0d18677fe588c790cNick Pelly
3914467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly            /* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND,
3924467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly             * override with the default. THREAD_PRIORITY_BACKGROUND causes
3934467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly             * us to service software I2C too slow for firmware download
3944467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly             * with the NXP PN544.
3954467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly             * TODO: move this to the DAL I2C layer in libnfc-nxp, since this
3964467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly             * problem only occurs on I2C platforms using PN544
3974467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly             */
3984467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly            Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
3994467dca5650a170af5020c10a8ccb25f86f1007fNick Pelly
40031949217328bf2357ff044f0d18677fe588c790cNick Pelly            switch (params[0].intValue()) {
40131949217328bf2357ff044f0d18677fe588c790cNick Pelly                case TASK_ENABLE:
40231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    enableInternal();
40331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
40431949217328bf2357ff044f0d18677fe588c790cNick Pelly                case TASK_DISABLE:
40531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    disableInternal();
40631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
40731949217328bf2357ff044f0d18677fe588c790cNick Pelly                case TASK_BOOT:
40831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT) &&
40931949217328bf2357ff044f0d18677fe588c790cNick Pelly                            !(mIsAirplaneSensitive && isAirplaneModeOn())) {
41031949217328bf2357ff044f0d18677fe588c790cNick Pelly                        enableInternal();
41131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
41231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) {
41331949217328bf2357ff044f0d18677fe588c790cNick Pelly                        Log.i(TAG, "First Boot");
41431949217328bf2357ff044f0d18677fe588c790cNick Pelly                        mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false);
41531949217328bf2357ff044f0d18677fe588c790cNick Pelly                        mPrefsEditor.apply();
41631949217328bf2357ff044f0d18677fe588c790cNick Pelly                        executeEeWipe();
41731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
41831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
41931949217328bf2357ff044f0d18677fe588c790cNick Pelly                case TASK_EE_WIPE:
42031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    executeEeWipe();
42131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
42231949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
423d1936808c5f37f97fdb876836194ecbfe1cdfff5Nick Pelly
424d1936808c5f37f97fdb876836194ecbfe1cdfff5Nick Pelly            // Restore default AsyncTask priority
425d1936808c5f37f97fdb876836194ecbfe1cdfff5Nick Pelly            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
42631949217328bf2357ff044f0d18677fe588c790cNick Pelly            return null;
42731949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
42831949217328bf2357ff044f0d18677fe588c790cNick Pelly
42931949217328bf2357ff044f0d18677fe588c790cNick Pelly        /**
43031949217328bf2357ff044f0d18677fe588c790cNick Pelly         * Enable NFC adapter functions.
43131949217328bf2357ff044f0d18677fe588c790cNick Pelly         * Does not toggle preferences.
43231949217328bf2357ff044f0d18677fe588c790cNick Pelly         */
43331949217328bf2357ff044f0d18677fe588c790cNick Pelly        boolean enableInternal() {
43431949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (mState == NfcAdapter.STATE_ON) {
43531949217328bf2357ff044f0d18677fe588c790cNick Pelly                return true;
43631949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
43731949217328bf2357ff044f0d18677fe588c790cNick Pelly            Log.i(TAG, "Enabling NFC");
43831949217328bf2357ff044f0d18677fe588c790cNick Pelly            updateState(NfcAdapter.STATE_TURNING_ON);
43931949217328bf2357ff044f0d18677fe588c790cNick Pelly
44031949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!mDeviceHost.initialize()) {
44131949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.w(TAG, "Error enabling NFC");
44231949217328bf2357ff044f0d18677fe588c790cNick Pelly                updateState(NfcAdapter.STATE_OFF);
44331949217328bf2357ff044f0d18677fe588c790cNick Pelly                return false;
44431949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
44531949217328bf2357ff044f0d18677fe588c790cNick Pelly
44631949217328bf2357ff044f0d18677fe588c790cNick Pelly            synchronized(NfcService.this) {
44731949217328bf2357ff044f0d18677fe588c790cNick Pelly                mObjectMap.clear();
44831949217328bf2357ff044f0d18677fe588c790cNick Pelly
44977d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                mP2pLinkManager.enableDisable(mIsZeroClickRequested, true);
45031949217328bf2357ff044f0d18677fe588c790cNick Pelly                updateState(NfcAdapter.STATE_ON);
45131949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
45231949217328bf2357ff044f0d18677fe588c790cNick Pelly
45331949217328bf2357ff044f0d18677fe588c790cNick Pelly            /* Start polling loop */
45431949217328bf2357ff044f0d18677fe588c790cNick Pelly            applyRouting();
45531949217328bf2357ff044f0d18677fe588c790cNick Pelly            return true;
45631949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
45731949217328bf2357ff044f0d18677fe588c790cNick Pelly
45831949217328bf2357ff044f0d18677fe588c790cNick Pelly        /**
45931949217328bf2357ff044f0d18677fe588c790cNick Pelly         * Disable all NFC adapter functions.
46031949217328bf2357ff044f0d18677fe588c790cNick Pelly         * Does not toggle preferences.
46131949217328bf2357ff044f0d18677fe588c790cNick Pelly         */
46231949217328bf2357ff044f0d18677fe588c790cNick Pelly        boolean disableInternal() {
46331949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (mState == NfcAdapter.STATE_OFF) {
46431949217328bf2357ff044f0d18677fe588c790cNick Pelly                return true;
46531949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
46631949217328bf2357ff044f0d18677fe588c790cNick Pelly            Log.i(TAG, "Disabling NFC");
46731949217328bf2357ff044f0d18677fe588c790cNick Pelly            updateState(NfcAdapter.STATE_TURNING_OFF);
46831949217328bf2357ff044f0d18677fe588c790cNick Pelly
46931949217328bf2357ff044f0d18677fe588c790cNick Pelly            /* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog.
47031949217328bf2357ff044f0d18677fe588c790cNick Pelly             * Implemented with a new thread (instead of a Handler or AsyncTask),
47131949217328bf2357ff044f0d18677fe588c790cNick Pelly             * because the UI Thread and AsyncTask thread-pools can also get hung
47231949217328bf2357ff044f0d18677fe588c790cNick Pelly             * when the NFC controller stops responding */
47331949217328bf2357ff044f0d18677fe588c790cNick Pelly            WatchDogThread watchDog = new WatchDogThread();
47431949217328bf2357ff044f0d18677fe588c790cNick Pelly            watchDog.start();
47531949217328bf2357ff044f0d18677fe588c790cNick Pelly
47677d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly            mP2pLinkManager.enableDisable(false, false);
47731949217328bf2357ff044f0d18677fe588c790cNick Pelly
47831949217328bf2357ff044f0d18677fe588c790cNick Pelly            // Stop watchdog if tag present
47931949217328bf2357ff044f0d18677fe588c790cNick Pelly            // A convenient way to stop the watchdog properly consists of
48031949217328bf2357ff044f0d18677fe588c790cNick Pelly            // disconnecting the tag. The polling loop shall be stopped before
48131949217328bf2357ff044f0d18677fe588c790cNick Pelly            // to avoid the tag being discovered again.
48231949217328bf2357ff044f0d18677fe588c790cNick Pelly            applyRouting();
48331949217328bf2357ff044f0d18677fe588c790cNick Pelly            maybeDisconnectTarget();
48431949217328bf2357ff044f0d18677fe588c790cNick Pelly
48531949217328bf2357ff044f0d18677fe588c790cNick Pelly            mNfcDispatcher.disableForegroundDispatch();
48631949217328bf2357ff044f0d18677fe588c790cNick Pelly
48731949217328bf2357ff044f0d18677fe588c790cNick Pelly            boolean result = mDeviceHost.deinitialize();
48831949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result);
48931949217328bf2357ff044f0d18677fe588c790cNick Pelly
49031949217328bf2357ff044f0d18677fe588c790cNick Pelly            watchDog.cancel();
49131949217328bf2357ff044f0d18677fe588c790cNick Pelly
49231949217328bf2357ff044f0d18677fe588c790cNick Pelly            updateState(NfcAdapter.STATE_OFF);
49331949217328bf2357ff044f0d18677fe588c790cNick Pelly
49431949217328bf2357ff044f0d18677fe588c790cNick Pelly            return result;
49531949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
49631949217328bf2357ff044f0d18677fe588c790cNick Pelly
49731949217328bf2357ff044f0d18677fe588c790cNick Pelly        void executeEeWipe() {
49831949217328bf2357ff044f0d18677fe588c790cNick Pelly            // TODO: read SE reset list from /system/etc
49931949217328bf2357ff044f0d18677fe588c790cNick Pelly            byte[][]apdus = EE_WIPE_APDUS;
50031949217328bf2357ff044f0d18677fe588c790cNick Pelly
50131949217328bf2357ff044f0d18677fe588c790cNick Pelly            boolean tempEnable = mState == NfcAdapter.STATE_OFF;
50231949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (tempEnable) {
50331949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (!enableInternal()) {
504ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly                    Log.w(TAG, "Could not enable NFC to wipe NFC-EE");
50531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return;
506f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                }
507f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
50831949217328bf2357ff044f0d18677fe588c790cNick Pelly            Log.i(TAG, "Executing SE wipe");
50931949217328bf2357ff044f0d18677fe588c790cNick Pelly            int handle = mSecureElement.doOpenSecureElementConnection();
51031949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (handle == 0) {
51131949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.w(TAG, "Could not open the secure element");
51231949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (tempEnable) {
51331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    disableInternal();
51431949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
51531949217328bf2357ff044f0d18677fe588c790cNick Pelly                return;
51631949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
51731949217328bf2357ff044f0d18677fe588c790cNick Pelly
51831949217328bf2357ff044f0d18677fe588c790cNick Pelly            mDeviceHost.setTimeout(TagTechnology.ISO_DEP, 10000);
51931949217328bf2357ff044f0d18677fe588c790cNick Pelly
52031949217328bf2357ff044f0d18677fe588c790cNick Pelly            for (byte[] cmd : apdus) {
521ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly                byte[] resp = mSecureElement.doTransceive(handle, cmd);
522ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly                if (resp == null) {
523ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly                    Log.w(TAG, "Transceive failed, could not wipe NFC-EE");
524ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly                    break;
525ca7e72aaac66ce856c32aaffb8fd2163d7bb486aNick Pelly                }
52631949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
52731949217328bf2357ff044f0d18677fe588c790cNick Pelly
52831949217328bf2357ff044f0d18677fe588c790cNick Pelly            mDeviceHost.resetTimeouts();
52931949217328bf2357ff044f0d18677fe588c790cNick Pelly            mSecureElement.doDisconnect(handle);
53031949217328bf2357ff044f0d18677fe588c790cNick Pelly
53131949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (tempEnable) {
53231949217328bf2357ff044f0d18677fe588c790cNick Pelly                disableInternal();
53331949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
53431949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
53531949217328bf2357ff044f0d18677fe588c790cNick Pelly
53631949217328bf2357ff044f0d18677fe588c790cNick Pelly        void updateState(int newState) {
53731949217328bf2357ff044f0d18677fe588c790cNick Pelly            synchronized (this) {
53831949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (newState == mState) {
53931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return;
54031949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
54131949217328bf2357ff044f0d18677fe588c790cNick Pelly                mState = newState;
54231949217328bf2357ff044f0d18677fe588c790cNick Pelly                Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
54331949217328bf2357ff044f0d18677fe588c790cNick Pelly                intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
54431949217328bf2357ff044f0d18677fe588c790cNick Pelly                intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState);
54531949217328bf2357ff044f0d18677fe588c790cNick Pelly                mContext.sendBroadcast(intent);
54631949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
54731949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
54831949217328bf2357ff044f0d18677fe588c790cNick Pelly    }
54931949217328bf2357ff044f0d18677fe588c790cNick Pelly
55031949217328bf2357ff044f0d18677fe588c790cNick Pelly    void saveNfcOnSetting(boolean on) {
55131949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (NfcService.this) {
55231949217328bf2357ff044f0d18677fe588c790cNick Pelly            mPrefsEditor.putBoolean(PREF_NFC_ON, on);
55331949217328bf2357ff044f0d18677fe588c790cNick Pelly            mPrefsEditor.apply();
55431949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
5550e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    }
5560e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
55777d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly    void playSound(int sound) {
558d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        synchronized (this) {
559d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
560d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        }
561d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton    }
562d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton
5630e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    @Override
5642f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly    public void onTerminate() {
5652f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly        super.onTerminate();
5662f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly        // NFC application is persistent, it should not be destroyed by framework
5670e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        Log.wtf(TAG, "NFC service is under attack!");
5680e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    }
5690e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
5704a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    final class NfcAdapterService extends INfcAdapter.Stub {
571fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
5720e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        public boolean enable() throws RemoteException {
57393d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceAdminPerm(mContext);
5740e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
57531949217328bf2357ff044f0d18677fe588c790cNick Pelly            saveNfcOnSetting(true);
57631949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) {
57731949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.i(TAG, "denying enable() request (airplane mode)");
57831949217328bf2357ff044f0d18677fe588c790cNick Pelly                return false;
579f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
58031949217328bf2357ff044f0d18677fe588c790cNick Pelly            new EnableDisableTask().execute(TASK_ENABLE);
58131949217328bf2357ff044f0d18677fe588c790cNick Pelly
58231949217328bf2357ff044f0d18677fe588c790cNick Pelly            return true;
583f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
584f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
585fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
5860e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        public boolean disable() throws RemoteException {
58793d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceAdminPerm(mContext);
5880e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
58931949217328bf2357ff044f0d18677fe588c790cNick Pelly            saveNfcOnSetting(false);
59031949217328bf2357ff044f0d18677fe588c790cNick Pelly            new EnableDisableTask().execute(TASK_DISABLE);
59131949217328bf2357ff044f0d18677fe588c790cNick Pelly
59231949217328bf2357ff044f0d18677fe588c790cNick Pelly            return true;
593f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
594f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
595fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
59631949217328bf2357ff044f0d18677fe588c790cNick Pelly        public boolean isZeroClickEnabled() throws RemoteException {
59731949217328bf2357ff044f0d18677fe588c790cNick Pelly            synchronized (NfcService.this) {
59831949217328bf2357ff044f0d18677fe588c790cNick Pelly                return mIsZeroClickRequested;
59931949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
600d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        }
601d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen
602d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        @Override
603d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        public boolean enableZeroClick() throws RemoteException {
604d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            NfcService.enforceAdminPerm(mContext);
605d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            synchronized(NfcService.this) {
60631949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (mIsZeroClickRequested) {
60731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return true;
60831949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
60931949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.i(TAG, "enabling 0-click");
61031949217328bf2357ff044f0d18677fe588c790cNick Pelly                mPrefsEditor.putBoolean(PREF_ZEROCLICK_ON, true);
61131949217328bf2357ff044f0d18677fe588c790cNick Pelly                mPrefsEditor.apply();
61231949217328bf2357ff044f0d18677fe588c790cNick Pelly                mIsZeroClickRequested = true;
61331949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (isNfcEnabled()) {
61477d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                    mP2pLinkManager.enableDisable(true, true);
615d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen                }
616d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            }
617d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            return true;
618d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        }
619d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen
620d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        @Override
621d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        public boolean disableZeroClick() throws RemoteException {
622d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            NfcService.enforceAdminPerm(mContext);
623d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            synchronized(NfcService.this) {
62431949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (!mIsZeroClickRequested) {
62531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return true;
62631949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
62731949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.i(TAG, "disabling 0-click");
62831949217328bf2357ff044f0d18677fe588c790cNick Pelly                mPrefsEditor.putBoolean(PREF_ZEROCLICK_ON, false);
62931949217328bf2357ff044f0d18677fe588c790cNick Pelly                mPrefsEditor.apply();
63031949217328bf2357ff044f0d18677fe588c790cNick Pelly                mIsZeroClickRequested = false;
63131949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (isNfcEnabled()) {
63277d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                    mP2pLinkManager.enableDisable(false, true);
633d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen                }
634d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            }
635d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen            return true;
636d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        }
637d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen
638d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen        @Override
63905973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton        public void enableForegroundDispatch(ComponentName activity, PendingIntent intent,
64024dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton                IntentFilter[] filters, TechListParcel techListsParcel) {
641a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton            // Permission check
64205973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
643a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton
644a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton            // Argument validation
645a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton            if (activity == null || intent == null) {
646ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton                throw new IllegalArgumentException();
647ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton            }
648a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton
649a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton            // Validate the IntentFilters
650a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton            if (filters != null) {
651a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                if (filters.length == 0) {
652a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                    filters = null;
653a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                } else {
654a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                    for (IntentFilter filter : filters) {
655a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                        if (filter == null) {
656a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                            throw new IllegalArgumentException("null IntentFilter");
657a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                        }
658a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                    }
659a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton                }
660a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton            }
661a1935861ea244b45e29b0ec9b6f263de3c08a2d6Jeff Hamilton
66224dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton            // Validate the tech lists
66324dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton            String[][] techLists = null;
66424dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton            if (techListsParcel != null) {
66524dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton                techLists = techListsParcel.getTechLists();
66624dbea55709219e42aa3b6b6578f29ffd447a786Jeff Hamilton            }
66749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly
66876a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly            mNfcDispatcher.enableForegroundDispatch(intent, filters, techLists);
66905973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton        }
67005973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton
67105973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton        @Override
67205973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton        public void disableForegroundDispatch(ComponentName activity) {
67305973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
67476a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly
67576a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly            mNfcDispatcher.disableForegroundDispatch();
67605973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton        }
67705973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton
67805973d55daf68a286c932ee4e7ffbd6bb53789e0Jeff Hamilton        @Override
679ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton        public void enableForegroundNdefPush(ComponentName activity, NdefMessage msg) {
680ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
681ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton            if (activity == null || msg == null) {
682ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton                throw new IllegalArgumentException();
683ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton            }
68477d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly            mP2pLinkManager.setNdefToSend(msg, null);
685ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton        }
686ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton
687ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton        @Override
6882094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks        public void enableForegroundNdefPushWithCallback(ComponentName activity,
6892094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks                INdefPushCallback callback) {
6902094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
6912094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks            if (activity == null || callback == null) {
6922094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks                throw new IllegalArgumentException();
6932094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks            }
69477d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly            mP2pLinkManager.setNdefToSend(null, callback);
6952094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks        }
6962094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks
6972094515fca0cfa0ac87e9cc260d3953d416afe3eJason parks        @Override
698ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton        public void disableForegroundNdefPush(ComponentName activity) {
699ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
70077d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly            mP2pLinkManager.setNdefToSend(null, null);
701ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton        }
702ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton
703ca1a86ecb8edce740a232c3439355e8d5b706e7aJeff Hamilton        @Override
7040e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        public INfcTag getNfcTagInterface() throws RemoteException {
705d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
7060e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly            return mNfcTagService;
7070e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        }
7080e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
709fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
71049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public INfcAdapterExtras getNfcAdapterExtrasInterface() {
71193d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceNfceeAdminPerm(mContext);
71249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return mExtrasService;
7130bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        }
7140bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
715fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
71631949217328bf2357ff044f0d18677fe588c790cNick Pelly        public int getState() throws RemoteException {
71731949217328bf2357ff044f0d18677fe588c790cNick Pelly            synchronized (NfcService.this) {
71831949217328bf2357ff044f0d18677fe588c790cNick Pelly                return mState;
71931949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
72031949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
72131949217328bf2357ff044f0d18677fe588c790cNick Pelly
72231949217328bf2357ff044f0d18677fe588c790cNick Pelly        @Override
72331949217328bf2357ff044f0d18677fe588c790cNick Pelly        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
72431949217328bf2357ff044f0d18677fe588c790cNick Pelly            NfcService.this.dump(fd, pw, args);
7250e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        }
7260e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    };
7270e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
7284a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    final class TagService extends INfcTag.Stub {
729fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
730f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        public int close(int nativeHandle) throws RemoteException {
731d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
732bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
733f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
734f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
73531949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
736f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return ErrorCodes.ERROR_NOT_INITIALIZED;
737f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
738f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
739f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
740f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
741f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            if (tag != null) {
742b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau                /* Remove the device from the hmap */
743b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau                unregisterObject(nativeHandle);
74421545af22f9b913ec9cb124287aab2fcb0cf2b3bNick Pelly                tag.disconnect();
745b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau                return ErrorCodes.SUCCESS;
746f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
747f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* Restart polling loop for notification */
74849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            applyRouting();
749f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            return ErrorCodes.ERROR_DISCONNECT;
750f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
751f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
752fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
753ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen        public int connect(int nativeHandle, int technology) throws RemoteException {
754d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
755bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
756f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
757f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
75831949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
759f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return ErrorCodes.ERROR_NOT_INITIALIZED;
760f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
761f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
762f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
763f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
764b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau            if (tag == null) {
765b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau                return ErrorCodes.ERROR_DISCONNECT;
766f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
767ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen
7681b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen            if (technology == TagTechnology.NFC_B) {
7691b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen                return ErrorCodes.ERROR_NOT_SUPPORTED;
7701b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen            }
7711b1e68327701225b728d43226049ad7a5dee4bcdMartijn Coenen
772ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen            // Note that on most tags, all technologies are behind a single
773ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen            // handle. This means that the connect at the lower levels
774ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen            // will do nothing, as the tag is already connected to that handle.
775ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen            if (tag.connect(technology)) {
776ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen                return ErrorCodes.SUCCESS;
777ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen            } else {
778ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen                return ErrorCodes.ERROR_DISCONNECT;
779ae7d8d800ba73502d21e54d1deef16be0f061866Martijn Coenen            }
780f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
781f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
782fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
783aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen        public int reconnect(int nativeHandle) throws RemoteException {
784aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
785aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen
786f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
787aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen
788aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            // Check if NFC is enabled
78931949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
790aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen                return ErrorCodes.ERROR_NOT_INITIALIZED;
791aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            }
792aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen
793aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            /* find the tag in the hmap */
794f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
795aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            if (tag != null) {
796aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen                if (tag.reconnect()) {
797aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen                    return ErrorCodes.SUCCESS;
798aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen                } else {
799aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen                    return ErrorCodes.ERROR_DISCONNECT;
800aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen                }
801aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            }
802aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen            return ErrorCodes.ERROR_DISCONNECT;
803aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen        }
804aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen
805aae427142dc22e7e419c146bc7748d9daff518e8Martijn Coenen        @Override
806b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton        public int[] getTechList(int nativeHandle) throws RemoteException {
807d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
808bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
809f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            // Check if NFC is enabled
81031949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
811f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return null;
812f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
813f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
814f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
815f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = (TagEndpoint) findObject(nativeHandle);
816f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            if (tag != null) {
817b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton                return tag.getTechList();
818f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
819f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            return null;
820f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
821f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
822fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
823f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        public byte[] getUid(int nativeHandle) throws RemoteException {
824f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
825f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            byte[] uid;
826f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
827f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            // Check if NFC is enabled
82831949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
829f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return null;
830f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
831f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
832f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
833f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
834f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            if (tag != null) {
835f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                uid = tag.getUid();
836f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return uid;
837f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
838f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            return null;
839f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
840f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
841fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
842b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        public boolean isPresent(int nativeHandle) throws RemoteException {
843f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
844b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
845b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau            // Check if NFC is enabled
84631949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
847b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau                return false;
848b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau            }
849b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
850b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau            /* find the tag in the hmap */
851f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
852b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau            if (tag == null) {
853b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau                return false;
854b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau            }
855b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
856ab2b44b97936d2c5dbf6eda1245ca793e840713fMartijn Coenen            return tag.isPresent();
857b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        }
858b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
859fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
860f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        public boolean isNdef(int nativeHandle) throws RemoteException {
861f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
862f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
863f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            // Check if NFC is enabled
86431949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
8652c3f9be8111dd454e430ffb327c051ff9d2bba21Daniel Tomas                return false;
866f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
867f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
868f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
869f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
8703ba3b10867c36bff57b72ff99c7b56d63d418f3fMartijn Coenen            int[] ndefInfo = new int[2];
8712c3f9be8111dd454e430ffb327c051ff9d2bba21Daniel Tomas            if (tag == null) {
8722c3f9be8111dd454e430ffb327c051ff9d2bba21Daniel Tomas                return false;
873f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
87470bbea61637e3f9eb7202efd243b9d2f9516a06aNick Pelly            return tag.checkNdef(ndefInfo);
875f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
876f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
877fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
8789d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenen        public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw)
87997c6942c7c7f9df3bb8dbcc01cf7bb6e2e090005Martijn Coenen                throws RemoteException {
880d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
881bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
882f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
883f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            byte[] response;
884f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
885f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            // Check if NFC is enabled
88631949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
887f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return null;
888f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
889f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
890f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
891f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
892f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            if (tag != null) {
893bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                // Check if length is within limits
894bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) {
895bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                    return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null);
896bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                }
8979d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenen                int[] targetLost = new int[1];
8989d5511f2640903a79d24578a12a93e50a96f0c0eMartijn Coenen                response = tag.transceive(data, raw, targetLost);
899bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                int result;
900bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                if (response != null) {
901bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                    result = TransceiveResult.RESULT_SUCCESS;
902bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                } else if (targetLost[0] == 1) {
903bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                    result = TransceiveResult.RESULT_TAGLOST;
904bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                } else {
905bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                    result = TransceiveResult.RESULT_FAILURE;
906bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                }
907bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen                return new TransceiveResult(result, response);
908f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
909f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            return null;
910f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
911f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
912fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
9133fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton        public NdefMessage ndefRead(int nativeHandle) throws RemoteException {
914d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
915bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
916f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag;
917f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
918f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            // Check if NFC is enabled
91931949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
920f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return null;
921f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
922f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
923f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
924f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
925f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            if (tag != null) {
926f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                byte[] buf = tag.readNdef();
927f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                if (buf == null) {
928f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                    return null;
929f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                }
930f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
931f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                /* Create an NdefMessage */
932f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                try {
933f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                    return new NdefMessage(buf);
934f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                } catch (FormatException e) {
935f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                    return null;
936f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                }
937f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
938f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            return null;
939f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
940f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
941fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
9423fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton        public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException {
943d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
944bebaa6cc1a1eb2ce0656e17b0e09ed4747878d8eNick Pelly
945f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag;
946f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
947f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            // Check if NFC is enabled
94831949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
949f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return ErrorCodes.ERROR_NOT_INITIALIZED;
950f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
951f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
952f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            /* find the tag in the hmap */
953f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
954f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            if (tag == null) {
955f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return ErrorCodes.ERROR_IO;
956f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
957f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
958f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            if (tag.writeNdef(msg.toByteArray())) {
959f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return ErrorCodes.SUCCESS;
960f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            } else {
961f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                return ErrorCodes.ERROR_IO;
962f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
963f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
964f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
965f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
966fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
967f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        public int getLastError(int nativeHandle) throws RemoteException {
968f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            return(mDeviceHost.doGetLastError());
969f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
970f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
971fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
9723fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton        public boolean ndefIsWritable(int nativeHandle) throws RemoteException {
9733fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton            throw new UnsupportedOperationException();
974f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
975f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
976fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
9773fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton        public int ndefMakeReadOnly(int nativeHandle) throws RemoteException {
97803ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
97903ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen
980f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag;
98103ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen
98203ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            // Check if NFC is enabled
98331949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
98403ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen                return ErrorCodes.ERROR_NOT_INITIALIZED;
98503ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            }
98603ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen
98703ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            /* find the tag in the hmap */
988f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
98903ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            if (tag == null) {
99003ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen                return ErrorCodes.ERROR_IO;
99103ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            }
99203ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen
993f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            if (tag.makeReadOnly()) {
99403ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen                return ErrorCodes.SUCCESS;
995f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            } else {
99603ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen                return ErrorCodes.ERROR_IO;
99703ee488afaa982ce934c5de399db9f9fa88c7d1eMartijn Coenen            }
998f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
999f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
10000aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen        @Override
10010aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen        public int formatNdef(int nativeHandle, byte[] key) throws RemoteException {
10020aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
10030aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen
1004f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag;
10050aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen
10060aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            // Check if NFC is enabled
100731949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
10080aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen                return ErrorCodes.ERROR_NOT_INITIALIZED;
10090aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            }
10100aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen
10110aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            /* find the tag in the hmap */
1012f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
10130aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            if (tag == null) {
10140aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen                return ErrorCodes.ERROR_IO;
10150aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            }
10160aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen
10170aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            if (tag.formatNdef(key)) {
10180aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen                return ErrorCodes.SUCCESS;
1019f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            } else {
10200aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen                return ErrorCodes.ERROR_IO;
10210aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen            }
10220aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen        }
10230aac9419555e69512a886a6b7fa6ce2d1947c72fMartijn Coenen
10241b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen        @Override
10253fb14d0868594c78a777e805545209636814e223Martijn Coenen        public Tag rediscover(int nativeHandle) throws RemoteException {
10263fb14d0868594c78a777e805545209636814e223Martijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
10273fb14d0868594c78a777e805545209636814e223Martijn Coenen
1028f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            TagEndpoint tag = null;
10293fb14d0868594c78a777e805545209636814e223Martijn Coenen
10303fb14d0868594c78a777e805545209636814e223Martijn Coenen            // Check if NFC is enabled
103131949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
10323fb14d0868594c78a777e805545209636814e223Martijn Coenen                return null;
10333fb14d0868594c78a777e805545209636814e223Martijn Coenen            }
10343fb14d0868594c78a777e805545209636814e223Martijn Coenen
10353fb14d0868594c78a777e805545209636814e223Martijn Coenen            /* find the tag in the hmap */
1036f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            tag = (TagEndpoint) findObject(nativeHandle);
10373fb14d0868594c78a777e805545209636814e223Martijn Coenen            if (tag != null) {
10383fb14d0868594c78a777e805545209636814e223Martijn Coenen                // For now the prime usecase for rediscover() is to be able
10393fb14d0868594c78a777e805545209636814e223Martijn Coenen                // to access the NDEF technology after formatting without
10403fb14d0868594c78a777e805545209636814e223Martijn Coenen                // having to remove the tag from the field, or similar
10413fb14d0868594c78a777e805545209636814e223Martijn Coenen                // to have access to NdefFormatable in case low-level commands
10423fb14d0868594c78a777e805545209636814e223Martijn Coenen                // were used to remove NDEF. So instead of doing a full stack
10433fb14d0868594c78a777e805545209636814e223Martijn Coenen                // rediscover (which is poorly supported at the moment anyway),
10443fb14d0868594c78a777e805545209636814e223Martijn Coenen                // we simply remove these two technologies and detect them
10453fb14d0868594c78a777e805545209636814e223Martijn Coenen                // again.
10463fb14d0868594c78a777e805545209636814e223Martijn Coenen                tag.removeTechnology(TagTechnology.NDEF);
10473fb14d0868594c78a777e805545209636814e223Martijn Coenen                tag.removeTechnology(TagTechnology.NDEF_FORMATABLE);
1048f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                NdefMessage[] msgs = tag.findAndReadNdef();
10493fb14d0868594c78a777e805545209636814e223Martijn Coenen                // Build a new Tag object to return
10503fb14d0868594c78a777e805545209636814e223Martijn Coenen                Tag newTag = new Tag(tag.getUid(), tag.getTechList(),
10514a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton                        tag.getTechExtras(), tag.getHandle(), this);
10523fb14d0868594c78a777e805545209636814e223Martijn Coenen                return newTag;
10533fb14d0868594c78a777e805545209636814e223Martijn Coenen            }
10543fb14d0868594c78a777e805545209636814e223Martijn Coenen            return null;
10553fb14d0868594c78a777e805545209636814e223Martijn Coenen        }
10563fb14d0868594c78a777e805545209636814e223Martijn Coenen
10571b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen        @Override
1058fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen        public int setTimeout(int tech, int timeout) throws RemoteException {
10591b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
1060f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            boolean success = mDeviceHost.setTimeout(tech, timeout);
1061fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen            if (success) {
1062fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen                return ErrorCodes.SUCCESS;
1063fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen            } else {
1064fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen                return ErrorCodes.ERROR_INVALID_PARAM;
1065fbd90779b1525b254726eb58d07883ca74e1b21cMartijn Coenen            }
1066dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen        }
1067dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen
1068dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen        @Override
1069358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen        public int getTimeout(int tech) throws RemoteException {
1070358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
1071358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen
1072358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen            return mDeviceHost.getTimeout(tech);
1073358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen        }
1074358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen
1075358d8b6ad611aba11e69a3b1dd9d132dbc9a7605Martijn Coenen        @Override
1076dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen        public void resetTimeouts() throws RemoteException {
1077dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
1078dfebdbf803bf01404255f964e571056aa84173caMartijn Coenen
1079f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            mDeviceHost.resetTimeouts();
10801b61f1dee91101e249b9be65d95366fa745b3b78Martijn Coenen        }
1081bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen
1082bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen        @Override
1083bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen        public boolean canMakeReadOnly(int ndefType) throws RemoteException {
1084bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
1085bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen
1086bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen            return mDeviceHost.canMakeReadOnly(ndefType);
1087bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen        }
1088bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen
1089bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen        @Override
1090bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen        public int getMaxTransceiveLength(int tech) throws RemoteException {
1091bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
1092bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen
1093bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen            return mDeviceHost.getMaxTransceiveLength(tech);
1094bf6e5d1655d5ad524a8ec007413c7011ed969df8Martijn Coenen        }
10950e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    };
1096f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
1097dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly    private void _nfcEeClose(boolean checkPid, int callingPid) throws IOException {
1098dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly        // Blocks until a pending open() or transceive() times out.
1099dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly        //TODO: This is incorrect behavior - the close should interrupt pending
1100dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly        // operations. However this is not supported by current hardware.
1101dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly
1102dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly        synchronized(NfcService.this) {
110331949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (!isNfcEnabled()) {
1104dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly                throw new IOException("NFC adapter is disabled");
1105dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            }
1106dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            if (mOpenEe == null) {
1107dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly                throw new IOException("NFC EE closed");
1108dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            }
1109dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            if (checkPid && mOpenEe.pid != -1 && callingPid != mOpenEe.pid) {
1110dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly                throw new SecurityException("Wrong PID");
1111dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            }
1112dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly
1113f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            mDeviceHost.resetTimeouts();
1114dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            mSecureElement.doDisconnect(mOpenEe.handle);
1115dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            mOpenEe = null;
1116dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly
1117dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly            applyRouting();
1118dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly        }
1119dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly    }
1120dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly
11214a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    final class NfcAdapterExtrasService extends INfcAdapterExtras.Stub {
112249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        private Bundle writeNoException() {
112349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            Bundle p = new Bundle();
112449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            p.putInt("e", 0);
112549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return p;
112649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        }
112749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        private Bundle writeIoException(IOException e) {
112849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            Bundle p = new Bundle();
112949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            p.putInt("e", -1);
113049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            p.putString("m", e.getMessage());
113149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return p;
113249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        }
11330bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
1134bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
113549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public Bundle open(IBinder b) throws RemoteException {
113693d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceNfceeAdminPerm(mContext);
1137bd555ee64250126b60b24814120a2049943920caNick Pelly
113849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            Bundle result;
113949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            try {
114049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                _open(b);
114149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result = writeNoException();
114249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            } catch (IOException e) {
114349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result = writeIoException(e);
11440bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas            }
114549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return result;
114649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        }
11470bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
114849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        private void _open(IBinder b) throws IOException, RemoteException {
114949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            synchronized(NfcService.this) {
115031949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (!isNfcEnabled()) {
115149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    throw new IOException("NFC adapter is disabled");
115249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
115349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                if (mOpenEe != null) {
115449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    throw new IOException("NFC EE already open");
115549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
11560bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
115749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                int handle = mSecureElement.doOpenSecureElementConnection();
115849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                if (handle == 0) {
115949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    throw new IOException("NFC EE failed to open");
116049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
1161f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                mDeviceHost.setTimeout(TagTechnology.ISO_DEP, 10000);
1162ba6401757f8017faeb77423f2d08fd51be1d1051Nick Pelly
116349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                mOpenEe = new OpenSecureElement(getCallingPid(), handle);
116449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                try {
116549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    b.linkToDeath(mOpenEe, 0);
116649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                } catch (RemoteException e) {
116749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    mOpenEe.binderDied();
116849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
116984e1e0adc2516afd35ebab029a52e764e0490559Jason parks
117084e1e0adc2516afd35ebab029a52e764e0490559Jason parks                // Add the calling package to the list of packages that have accessed
117184e1e0adc2516afd35ebab029a52e764e0490559Jason parks                // the secure element.
117284e1e0adc2516afd35ebab029a52e764e0490559Jason parks                for (String packageName : getPackageManager().getPackagesForUid(getCallingUid())) {
117384e1e0adc2516afd35ebab029a52e764e0490559Jason parks                    mSePackages.add(packageName);
117484e1e0adc2516afd35ebab029a52e764e0490559Jason parks                }
117549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly           }
11760bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        }
11770bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
1178bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
117949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public Bundle close() throws RemoteException {
118093d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceNfceeAdminPerm(mContext);
11810bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
118249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            Bundle result;
118349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            try {
1184dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly                _nfcEeClose(true, getCallingPid());
118549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result = writeNoException();
118649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            } catch (IOException e) {
118749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result = writeIoException(e);
11880bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas            }
118949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return result;
119049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        }
11910bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
1192bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
119349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public Bundle transceive(byte[] in) throws RemoteException {
119493d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceNfceeAdminPerm(mContext);
1195bd555ee64250126b60b24814120a2049943920caNick Pelly
119649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            Bundle result;
119749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            byte[] out;
119849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            try {
119949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                out = _transceive(in);
120049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result = writeNoException();
120149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result.putByteArray("out", out);
120249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            } catch (IOException e) {
120349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                result = writeIoException(e);
12040bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas            }
120549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return result;
120649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        }
12070bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
120849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        private byte[] _transceive(byte[] data) throws IOException, RemoteException {
120949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            synchronized(NfcService.this) {
121031949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (!isNfcEnabled()) {
121149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    throw new IOException("NFC is not enabled");
121249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
121349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                if (mOpenEe == null){
121449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    throw new IOException("NFC EE is not open");
121549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
121649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                if (getCallingPid() != mOpenEe.pid) {
121749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                    throw new SecurityException("Wrong PID");
121849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                }
12190bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas            }
12200bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
122149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return mSecureElement.doTransceive(mOpenEe.handle, data);
12220bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        }
12230bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
1224bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
122549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public int getCardEmulationRoute() throws RemoteException {
122693d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceNfceeAdminPerm(mContext);
122749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            return mEeRoutingState;
12280bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        }
12290bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
1230bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
123149d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public void setCardEmulationRoute(int route) throws RemoteException {
123293d8a69ccadfa01b0a5ec3d7edeb921a1da4bce8Jeff Hamilton            NfcService.enforceNfceeAdminPerm(mContext);
123349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            mEeRoutingState = route;
123449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            applyRouting();
12350bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        }
1236bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks
1237bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
12385a8844da9bc569aa7289426bf7b96e7eef90abf5Nick Pelly        public void authenticate(byte[] token) throws RemoteException {
1239bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks            NfcService.enforceNfceeAdminPerm(mContext);
1240bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        }
12410bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas    };
12420bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
124349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    /** resources kept while secure element is open */
124449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    private class OpenSecureElement implements IBinder.DeathRecipient {
124549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public int pid;  // pid that opened SE
124649d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public int handle; // low-level handle
124749d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public OpenSecureElement(int pid, int handle) {
124849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            this.pid = pid;
124949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            this.handle = handle;
125049d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        }
1251bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        @Override
125249d53329a0c720a7e430220d77805bc1763545b1Nick Pelly        public void binderDied() {
125349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly            synchronized (NfcService.this) {
125449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                if (DBG) Log.d(TAG, "Tracked app " + pid + " died");
125549d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                pid = -1;
12560bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas                try {
1257dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly                    _nfcEeClose(false, -1);
1258dfac80d25dd2816ad5af74cc4131b74134cc81b7Nick Pelly                } catch (IOException e) { /* already closed */ }
12590bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas            }
12600bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas        }
12610bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas    }
12620bd11735e8a28db1692f28abcc3e065abae0e8ddDaniel Tomas
126331949217328bf2357ff044f0d18677fe588c790cNick Pelly    boolean isNfcEnabled() {
126431949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (this) {
126531949217328bf2357ff044f0d18677fe588c790cNick Pelly            return mState == NfcAdapter.STATE_ON;
1266e7a398f2f0256a4a80a4ee08b70d48dbfd8da6d2Nick Pelly        }
1267aa122139d77645149c09c9815fd45e7b87ce7170Nick Pelly    }
1268aa122139d77645149c09c9815fd45e7b87ce7170Nick Pelly
126931949217328bf2357ff044f0d18677fe588c790cNick Pelly    class WatchDogThread extends Thread {
12702edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly        boolean mWatchDogCanceled = false;
12712edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly        @Override
12722edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly        public void run() {
12732edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly            boolean slept = false;
12742edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly            while (!slept) {
12752edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                try {
12762edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                    Thread.sleep(10000);
12772edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                    slept = true;
12782edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                } catch (InterruptedException e) { }
12792edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly            }
12802edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly            synchronized (this) {
12812edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                if (!mWatchDogCanceled) {
12822edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                    // Trigger watch-dog
12832edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                    Log.e(TAG, "Watch dog triggered");
12842083287b83a587d8f6e9ad829ea18041dc17d842Nick Pelly                    mDeviceHost.doAbort();
12852edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly                }
12862edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly            }
12872edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly        }
12882edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly        public synchronized void cancel() {
12892edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly            mWatchDogCanceled = true;
12902edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly        }
12912edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly    }
12922edb3ee5e28ab719a3bb17b8d76b2b588405be9aNick Pelly
129349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    /** apply NFC discovery and EE routing */
129449d53329a0c720a7e430220d77805bc1763545b1Nick Pelly    private synchronized void applyRouting() {
129531949217328bf2357ff044f0d18677fe588c790cNick Pelly        if (!isNfcEnabled() || mOpenEe != null) {
129631949217328bf2357ff044f0d18677fe588c790cNick Pelly            return;
129731949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
129831949217328bf2357ff044f0d18677fe588c790cNick Pelly        if (mIsScreenUnlocked) {
129931949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) {
130031949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.d(TAG, "NFC-EE routing ON");
130131949217328bf2357ff044f0d18677fe588c790cNick Pelly                mDeviceHost.doSelectSecureElement();
1302221b4d6ee301fbfe19402798f7d3c11e6878c888daniel_tomas            } else {
130349d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                Log.d(TAG, "NFC-EE routing OFF");
1304f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                mDeviceHost.doDeselectSecureElement();
1305221b4d6ee301fbfe19402798f7d3c11e6878c888daniel_tomas            }
130631949217328bf2357ff044f0d18677fe588c790cNick Pelly            Log.d(TAG, "NFC-C polling ON");
130731949217328bf2357ff044f0d18677fe588c790cNick Pelly            mDeviceHost.enableDiscovery();
130831949217328bf2357ff044f0d18677fe588c790cNick Pelly        } else {
130931949217328bf2357ff044f0d18677fe588c790cNick Pelly            Log.d(TAG, "NFC-EE routing OFF");
131031949217328bf2357ff044f0d18677fe588c790cNick Pelly            mDeviceHost.doDeselectSecureElement();
131131949217328bf2357ff044f0d18677fe588c790cNick Pelly            Log.d(TAG, "NFC-C polling OFF");
131231949217328bf2357ff044f0d18677fe588c790cNick Pelly            mDeviceHost.disableDiscovery();
131365945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly        }
131465945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly    }
131565945ad77cadb7a3bdf171497877d2325b23def5Nick Pelly
13162436ffe91853535fad87b7a8e03d8883bae20f20Arnaud Ferir    /** Disconnect any target if present */
131731949217328bf2357ff044f0d18677fe588c790cNick Pelly    void maybeDisconnectTarget() {
131831949217328bf2357ff044f0d18677fe588c790cNick Pelly        if (!isNfcEnabled()) {
1319a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly            return;
1320a8b7cca914e4a17f5c432f7bbeed0f1b236c55b4Nick Pelly        }
132131949217328bf2357ff044f0d18677fe588c790cNick Pelly        Object[] objectsToDisconnect;
132231949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (this) {
132331949217328bf2357ff044f0d18677fe588c790cNick Pelly            Object[] objectValues = mObjectMap.values().toArray();
132431949217328bf2357ff044f0d18677fe588c790cNick Pelly            // Copy the array before we clear mObjectMap,
132531949217328bf2357ff044f0d18677fe588c790cNick Pelly            // just in case the HashMap values are backed by the same array
132631949217328bf2357ff044f0d18677fe588c790cNick Pelly            objectsToDisconnect = Arrays.copyOf(objectValues, objectValues.length);
132731949217328bf2357ff044f0d18677fe588c790cNick Pelly            mObjectMap.clear();
132831949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
132931949217328bf2357ff044f0d18677fe588c790cNick Pelly        for (Object o : objectsToDisconnect) {
133031949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (DBG) Log.d(TAG, "disconnecting " + o.getClass().getName());
133131949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (o instanceof TagEndpoint) {
133231949217328bf2357ff044f0d18677fe588c790cNick Pelly                // Disconnect from tags
133331949217328bf2357ff044f0d18677fe588c790cNick Pelly                TagEndpoint tag = (TagEndpoint) o;
133431949217328bf2357ff044f0d18677fe588c790cNick Pelly                tag.disconnect();
133531949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (o instanceof NfcDepEndpoint) {
133631949217328bf2357ff044f0d18677fe588c790cNick Pelly                // Disconnect from P2P devices
133731949217328bf2357ff044f0d18677fe588c790cNick Pelly                NfcDepEndpoint device = (NfcDepEndpoint) o;
133831949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) {
133931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    // Remote peer is target, request disconnection
134031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    device.disconnect();
134131949217328bf2357ff044f0d18677fe588c790cNick Pelly                } else {
134231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    // Remote peer is initiator, we cannot disconnect
134331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    // Just wait for field removal
1344bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks                }
1345bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks            }
1346bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks        }
1347bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks    }
1348bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks
134931949217328bf2357ff044f0d18677fe588c790cNick Pelly    Object findObject(int key) {
135031949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (this) {
135131949217328bf2357ff044f0d18677fe588c790cNick Pelly            Object device = mObjectMap.get(key);
135231949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (device == null) {
135331949217328bf2357ff044f0d18677fe588c790cNick Pelly                Log.w(TAG, "Handle not found");
13542f8ac1e6cdeb32569bc6477d53a2d0d5608758b1Nick Pelly            }
135531949217328bf2357ff044f0d18677fe588c790cNick Pelly            return device;
13560e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        }
1357f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly    }
1358f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
135931949217328bf2357ff044f0d18677fe588c790cNick Pelly    void registerTagObject(TagEndpoint tag) {
136031949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (this) {
136131949217328bf2357ff044f0d18677fe588c790cNick Pelly            mObjectMap.put(tag.getHandle(), tag);
1362f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
1363b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    }
1364b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
136531949217328bf2357ff044f0d18677fe588c790cNick Pelly    void unregisterObject(int handle) {
136631949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (this) {
136731949217328bf2357ff044f0d18677fe588c790cNick Pelly            mObjectMap.remove(handle);
136831949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
1369f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly    }
1370f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
1371d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    /** For use by code in this process */
13724a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    public LlcpSocket createLlcpSocket(int sap, int miu, int rw, int linearBufferLength)
13734a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton            throws IOException, LlcpException {
13744a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton        return mDeviceHost.createLlcpSocket(sap, miu, rw, linearBufferLength);
1375d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    }
1376d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
1377d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    /** For use by code in this process */
13784a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton    public LlcpServerSocket createLlcpServerSocket(int sap, String sn, int miu, int rw,
13794a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton            int linearBufferLength) throws IOException, LlcpException {
13804a61d3b45e81c0070538f94747a70a49c78f12faJeff Hamilton        return mDeviceHost.createLlcpServerSocket(sap, sn, miu, rw, linearBufferLength);
1381d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton    }
1382d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
138357d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton    public void sendMockNdefTag(NdefMessage msg) {
1384b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton        sendMessage(MSG_MOCK_NDEF, msg);
138557d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton    }
138657d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton
1387b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    void sendMessage(int what, Object obj) {
1388b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        Message msg = mHandler.obtainMessage();
1389b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        msg.what = what;
1390b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        msg.obj = obj;
1391b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        mHandler.sendMessage(msg);
1392b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau    }
1393b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
13943fb14d0868594c78a777e805545209636814e223Martijn Coenen    final class NfcServiceHandler extends Handler {
1395b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        @Override
1396b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        public void handleMessage(Message msg) {
139731949217328bf2357ff044f0d18677fe588c790cNick Pelly            switch (msg.what) {
139831949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_MOCK_NDEF: {
139931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    NdefMessage ndefMsg = (NdefMessage) msg.obj;
140031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Bundle extras = new Bundle();
140131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    extras.putParcelable(Ndef.EXTRA_NDEF_MSG, ndefMsg);
140231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    extras.putInt(Ndef.EXTRA_NDEF_MAXLENGTH, 0);
140331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    extras.putInt(Ndef.EXTRA_NDEF_CARDSTATE, Ndef.NDEF_MODE_READ_ONLY);
140431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    extras.putInt(Ndef.EXTRA_NDEF_TYPE, Ndef.TYPE_OTHER);
140531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Tag tag = Tag.createMockTag(new byte[] { 0x00 },
140631949217328bf2357ff044f0d18677fe588c790cNick Pelly                            new int[] { TagTechnology.NDEF },
140731949217328bf2357ff044f0d18677fe588c790cNick Pelly                            new Bundle[] { extras });
140831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.d(TAG, "mock NDEF tag, starting corresponding activity");
140931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.d(TAG, tag.toString());
141031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    boolean delivered = mNfcDispatcher.dispatchTag(tag,
141131949217328bf2357ff044f0d18677fe588c790cNick Pelly                            new NdefMessage[] { ndefMsg });
141231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (delivered) {
141377d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                        playSound(mEndSound);
141477d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                    } else {
141577d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                        playSound(mErrorSound);
141631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
141731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
141831949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
141957d376f1ee1a3939977b95759525585abb9601fbJeff Hamilton
142031949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_NDEF_TAG:
142131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Tag detected, notifying applications");
142231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    TagEndpoint tag = (TagEndpoint) msg.obj;
142331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    playSound(mStartSound);
142431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    NdefMessage[] ndefMsgs = tag.findAndReadNdef();
1425c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas
142631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (ndefMsgs != null) {
142731949217328bf2357ff044f0d18677fe588c790cNick Pelly                        tag.startPresenceChecking();
142831949217328bf2357ff044f0d18677fe588c790cNick Pelly                        dispatchTagEndpoint(tag, ndefMsgs);
142931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    } else {
143031949217328bf2357ff044f0d18677fe588c790cNick Pelly                        if (tag.reconnect()) {
143131949217328bf2357ff044f0d18677fe588c790cNick Pelly                            tag.startPresenceChecking();
143231949217328bf2357ff044f0d18677fe588c790cNick Pelly                            dispatchTagEndpoint(tag, null);
143331949217328bf2357ff044f0d18677fe588c790cNick Pelly                        } else {
143431949217328bf2357ff044f0d18677fe588c790cNick Pelly                            tag.disconnect();
143531949217328bf2357ff044f0d18677fe588c790cNick Pelly                            playSound(mErrorSound);
143631949217328bf2357ff044f0d18677fe588c790cNick Pelly                        }
143731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
143831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
143931949217328bf2357ff044f0d18677fe588c790cNick Pelly
144031949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_CARD_EMULATION:
144131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Card Emulation message");
144231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    byte[] aid = (byte[]) msg.obj;
144331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    /* Send broadcast */
144431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent aidIntent = new Intent();
144531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    aidIntent.setAction(ACTION_AID_SELECTED);
144631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    aidIntent.putExtra(EXTRA_AID, aid);
144731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Broadcasting " + ACTION_AID_SELECTED);
144831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendBroadcast(aidIntent, NFCEE_ADMIN_PERM);
144931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
145031949217328bf2357ff044f0d18677fe588c790cNick Pelly
145131949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_SE_EMV_CARD_REMOVAL:
145231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Card Removal message");
145331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    /* Send broadcast */
145431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent cardRemovalIntent = new Intent();
145531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    cardRemovalIntent.setAction(ACTION_EMV_CARD_REMOVAL);
145631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Broadcasting " + ACTION_EMV_CARD_REMOVAL);
145731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendBroadcast(cardRemovalIntent, NFCEE_ADMIN_PERM);
145831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
145931949217328bf2357ff044f0d18677fe588c790cNick Pelly
146031949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_SE_APDU_RECEIVED:
146131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "APDU Received message");
146231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    byte[] apduBytes = (byte[]) msg.obj;
146331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    /* Send broadcast */
146431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent apduReceivedIntent = new Intent();
146531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    apduReceivedIntent.setAction(ACTION_APDU_RECEIVED);
146631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (apduBytes != null && apduBytes.length > 0) {
146731949217328bf2357ff044f0d18677fe588c790cNick Pelly                        apduReceivedIntent.putExtra(EXTRA_APDU_BYTES, apduBytes);
146831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
146931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Broadcasting " + ACTION_APDU_RECEIVED);
147031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendBroadcast(apduReceivedIntent, NFCEE_ADMIN_PERM);
147131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
147231949217328bf2357ff044f0d18677fe588c790cNick Pelly
147331949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_SE_MIFARE_ACCESS:
147431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "MIFARE access message");
147531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    /* Send broadcast */
147631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    byte[] mifareCmd = (byte[]) msg.obj;
147731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent mifareAccessIntent = new Intent();
147831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mifareAccessIntent.setAction(ACTION_MIFARE_ACCESS_DETECTED);
147931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (mifareCmd != null && mifareCmd.length > 1) {
148031949217328bf2357ff044f0d18677fe588c790cNick Pelly                        int mifareBlock = mifareCmd[1] & 0xff;
148131949217328bf2357ff044f0d18677fe588c790cNick Pelly                        if (DBG) Log.d(TAG, "Mifare Block=" + mifareBlock);
148231949217328bf2357ff044f0d18677fe588c790cNick Pelly                        mifareAccessIntent.putExtra(EXTRA_MIFARE_BLOCK, mifareBlock);
148331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
148431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Broadcasting " + ACTION_MIFARE_ACCESS_DETECTED);
148531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendBroadcast(mifareAccessIntent, NFCEE_ADMIN_PERM);
148631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
1487c9a2ae7cb238e4c72818d084cba0b05e76cba1efdaniel_tomas
148831949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_LLCP_LINK_ACTIVATION:
148931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    llcpActivated((NfcDepEndpoint) msg.obj);
149031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
149131949217328bf2357ff044f0d18677fe588c790cNick Pelly
149231949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_LLCP_LINK_DEACTIVATED:
149331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    NfcDepEndpoint device = (NfcDepEndpoint) msg.obj;
149431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    boolean needsDisconnect = false;
149531949217328bf2357ff044f0d18677fe588c790cNick Pelly
149631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.d(TAG, "LLCP Link Deactivated message. Restart polling loop.");
149731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    synchronized (NfcService.this) {
149831949217328bf2357ff044f0d18677fe588c790cNick Pelly                        /* Check if the device has been already unregistered */
149931949217328bf2357ff044f0d18677fe588c790cNick Pelly                        if (mObjectMap.remove(device.getHandle()) != null) {
150031949217328bf2357ff044f0d18677fe588c790cNick Pelly                            /* Disconnect if we are initiator */
150131949217328bf2357ff044f0d18677fe588c790cNick Pelly                            if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) {
150231949217328bf2357ff044f0d18677fe588c790cNick Pelly                                if (DBG) Log.d(TAG, "disconnecting from target");
150331949217328bf2357ff044f0d18677fe588c790cNick Pelly                                needsDisconnect = true;
150431949217328bf2357ff044f0d18677fe588c790cNick Pelly                            } else {
150531949217328bf2357ff044f0d18677fe588c790cNick Pelly                                if (DBG) Log.d(TAG, "not disconnecting from initiator");
150631949217328bf2357ff044f0d18677fe588c790cNick Pelly                            }
150731949217328bf2357ff044f0d18677fe588c790cNick Pelly                        }
150831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
150931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (needsDisconnect) {
151031949217328bf2357ff044f0d18677fe588c790cNick Pelly                        device.disconnect();  // restarts polling loop
151131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    }
151231949217328bf2357ff044f0d18677fe588c790cNick Pelly
151377d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                    mP2pLinkManager.onLlcpDeactivated();
151431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
151531949217328bf2357ff044f0d18677fe588c790cNick Pelly
151631949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_TARGET_DESELECTED:
151731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    /* Broadcast Intent Target Deselected */
151831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Target Deselected");
151931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent intent = new Intent();
152031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    intent.setAction(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION);
152131949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "Broadcasting Intent");
152231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendOrderedBroadcast(intent, NFC_PERM);
152331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
152431949217328bf2357ff044f0d18677fe588c790cNick Pelly
152531949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_SE_FIELD_ACTIVATED: {
152631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "SE FIELD ACTIVATED");
152731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent eventFieldOnIntent = new Intent();
152831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    eventFieldOnIntent.setAction(ACTION_RF_FIELD_ON_DETECTED);
152931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendBroadcast(eventFieldOnIntent, NFCEE_ADMIN_PERM);
153031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
153131949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
153231949217328bf2357ff044f0d18677fe588c790cNick Pelly
153331949217328bf2357ff044f0d18677fe588c790cNick Pelly                case MSG_SE_FIELD_DEACTIVATED: {
153431949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (DBG) Log.d(TAG, "SE FIELD DEACTIVATED");
153531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Intent eventFieldOffIntent = new Intent();
153631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    eventFieldOffIntent.setAction(ACTION_RF_FIELD_OFF_DETECTED);
153731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    mContext.sendBroadcast(eventFieldOffIntent, NFCEE_ADMIN_PERM);
153831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
153931949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
154031949217328bf2357ff044f0d18677fe588c790cNick Pelly
154131949217328bf2357ff044f0d18677fe588c790cNick Pelly                default:
154231949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.e(TAG, "Unknown message received");
154331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    break;
154431949217328bf2357ff044f0d18677fe588c790cNick Pelly            }
1545b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau        }
1546d0ec3981792e38afd119fc1c995f111f6182f6c8Jeff Hamilton
1547d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        private boolean llcpActivated(NfcDepEndpoint device) {
1548d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            Log.d(TAG, "LLCP Activation message");
1549d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton
1550d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            if (device.getMode() == NfcDepEndpoint.MODE_P2P_TARGET) {
1551d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                if (DBG) Log.d(TAG, "NativeP2pDevice.MODE_P2P_TARGET");
1552d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                if (device.connect()) {
1553d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    /* Check LLCP compliancy */
1554d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    if (mDeviceHost.doCheckLlcp()) {
1555d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        /* Activate LLCP Link */
1556d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        if (mDeviceHost.doActivateLlcp()) {
1557d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                            if (DBG) Log.d(TAG, "Initiator Activate LLCP OK");
155831949217328bf2357ff044f0d18677fe588c790cNick Pelly                            boolean isZeroClickOn;
155931949217328bf2357ff044f0d18677fe588c790cNick Pelly                            synchronized (NfcService.this) {
156031949217328bf2357ff044f0d18677fe588c790cNick Pelly                                // Register P2P device
156131949217328bf2357ff044f0d18677fe588c790cNick Pelly                                mObjectMap.put(device.getHandle(), device);
1562d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen                            }
156377d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                            mP2pLinkManager.onLlcpActivated();
1564d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                            return true;
1565d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        } else {
1566d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                            /* should not happen */
1567d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                            Log.w(TAG, "Initiator LLCP activation failed. Disconnect.");
1568d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                            device.disconnect();
1569d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        }
1570d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    } else {
1571d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        if (DBG) Log.d(TAG, "Remote Target does not support LLCP. Disconnect.");
1572d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        device.disconnect();
1573d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    }
1574d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                } else {
1575d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    if (DBG) Log.d(TAG, "Cannot connect remote Target. Polling loop restarted.");
1576d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    /*
1577d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                     * The polling loop should have been restarted in failing
1578d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                     * doConnect
1579d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                     */
1580d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                }
1581d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            } else if (device.getMode() == NfcDepEndpoint.MODE_P2P_INITIATOR) {
1582d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                if (DBG) Log.d(TAG, "NativeP2pDevice.MODE_P2P_INITIATOR");
1583d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                /* Check LLCP compliancy */
1584d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                if (mDeviceHost.doCheckLlcp()) {
1585d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    /* Activate LLCP Link */
1586d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    if (mDeviceHost.doActivateLlcp()) {
1587d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        if (DBG) Log.d(TAG, "Target Activate LLCP OK");
158831949217328bf2357ff044f0d18677fe588c790cNick Pelly                        boolean isZeroClickOn;
158931949217328bf2357ff044f0d18677fe588c790cNick Pelly                        synchronized (NfcService.this) {
159031949217328bf2357ff044f0d18677fe588c790cNick Pelly                            // Register P2P device
159131949217328bf2357ff044f0d18677fe588c790cNick Pelly                            mObjectMap.put(device.getHandle(), device);
1592d9567994fefe21743131adc7390acdb97f81ed67Martijn Coenen                        }
159377d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly                        mP2pLinkManager.onLlcpActivated();
1594d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                        return true;
1595d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    }
1596d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                } else {
1597d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                    Log.w(TAG, "checkLlcp failed");
1598d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                }
1599d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            }
1600d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton
1601d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            return false;
1602d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton        }
1603d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton
1604f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton        private void dispatchTagEndpoint(TagEndpoint tagEndpoint, NdefMessage[] msgs) {
1605f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            Tag tag = new Tag(tagEndpoint.getUid(), tagEndpoint.getTechList(),
1606f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                    tagEndpoint.getTechExtras(), tagEndpoint.getHandle(), mNfcTagService);
1607f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton            registerTagObject(tagEndpoint);
160876a412f47ff57ce05d84fd51adbf8e72fd37a448Nick Pelly            if (!mNfcDispatcher.dispatchTag(tag, msgs)) {
1609f6c56a2191d58e3f9f68e3e7d94908b04b9b66ffJeff Hamilton                unregisterObject(tagEndpoint.getHandle());
1610d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                playSound(mErrorSound);
1611d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton            } else {
1612d704c298a5a1e783c71db6f39b2eef0a909b0e88Jeff Hamilton                playSound(mEndSound);
16133fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton            }
16143fb30ae5bf51d9ffe6271a345d55905dade8040dJeff Hamilton        }
1615b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton    }
1616b230214bcb557184cb54174889cbede53b92d54fSylvain Fonteneau
1617b74200f40f9d4f536b8782974d444f1f9178076fJeff Hamilton    private NfcServiceHandler mHandler = new NfcServiceHandler();
161849d53329a0c720a7e430220d77805bc1763545b1Nick Pelly
161931949217328bf2357ff044f0d18677fe588c790cNick Pelly    class EnableDisableDiscoveryTask extends AsyncTask<Boolean, Void, Void> {
1620fa746bcc16d57ff12d373b9139558f5bc7164b30Jason parks        @Override
1621275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks        protected Void doInBackground(Boolean... params) {
1622275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks            if (DBG) Log.d(TAG, "EnableDisableDiscoveryTask: enable = " + params[0]);
1623275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks
1624275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks            if (params != null && params.length > 0 && params[0]) {
1625161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly                synchronized (NfcService.this) {
162631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (!mIsScreenUnlocked) {
162731949217328bf2357ff044f0d18677fe588c790cNick Pelly                        mIsScreenUnlocked = true;
1628275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                        applyRouting();
1629275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                    } else {
1630275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                        if (DBG) Log.d(TAG, "Ignoring enable request");
1631275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                    }
1632161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly                }
16337c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly            } else {
1634533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly                mWakeLock.acquire();
1635161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly                synchronized (NfcService.this) {
163631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    if (mIsScreenUnlocked) {
163731949217328bf2357ff044f0d18677fe588c790cNick Pelly                        mIsScreenUnlocked = false;
1638275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                        applyRouting();
1639275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                        maybeDisconnectTarget();
1640275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                    } else {
1641275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                        if (DBG) Log.d(TAG, "Ignoring disable request");
1642275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                    }
1643161f84b5487ce4c1ebef9fe24ba4de00f6f756eaNick Pelly                }
1644533043d1003de2f6a20a29201100d94c3c7bc9caNick Pelly                mWakeLock.release();
16457c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly            }
16467c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly            return null;
16477c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly        }
16487c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly    }
16497c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly
16500e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
16510e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        @Override
16520e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly        public void onReceive(Context context, Intent intent) {
165331949217328bf2357ff044f0d18677fe588c790cNick Pelly            String action = intent.getAction();
165431949217328bf2357ff044f0d18677fe588c790cNick Pelly            if (action.equals(
1655f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                    NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION)) {
1656ef92efa7fd86d75cc35465b84e8740f941ff0f0aJeff Hamilton                if (DBG) Log.d(TAG, "INERNAL_TARGET_DESELECTED_ACTION");
16570e6a0a0f50132e14d5ecad61c46e07c67fbb26fbNick Pelly
1658f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly                /* Restart polling loop for notification */
165949d53329a0c720a7e430220d77805bc1763545b1Nick Pelly                applyRouting();
1660f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly
166131949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1662275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                // Only enable if the screen is unlocked. If the screen is locked
1663275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                // Intent.ACTION_USER_PRESENT will be broadcast when the screen is
1664275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                // unlocked.
16656ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly                boolean enable = !mKeyguard.isKeyguardSecure() || !mKeyguard.isKeyguardLocked();
1666275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks
16676ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly                // Perform discovery enable in thread to protect against ANR when the
16686ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly                // NFC stack wedges. This is *not* the correct way to fix this issue -
16696ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly                // configuration of the local NFC adapter should be very quick and should
16706ec87aa54aad2c340c4ea0247ba4a8e0d9f10573Nick Pelly                // be safe on the main thread, and the NFC stack should not wedge.
1671275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                new EnableDisableDiscoveryTask().execute(enable);
167231949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
16737c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly                // Perform discovery disable in thread to protect against ANR when the
16747c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly                // NFC stack wedges. This is *not* the correct way to fix this issue -
16757c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly                // configuration of the local NFC adapter should be very quick and should
16767c034a7fe7d36b1ab039af2c44717812ea02657eNick Pelly                // be safe on the main thread, and the NFC stack should not wedge.
1677275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                new EnableDisableDiscoveryTask().execute(Boolean.FALSE);
167831949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1679275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                // The user has unlocked the screen. Enabled!
1680275fd2b59ae8a9e3eb6e77e8663aea9ca08f1159Jason parks                new EnableDisableDiscoveryTask().execute(Boolean.TRUE);
168131949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (action.equals(ACTION_MASTER_CLEAR_NOTIFICATION)) {
168231949217328bf2357ff044f0d18677fe588c790cNick Pelly                EnableDisableTask eeWipeTask = new EnableDisableTask();
168331949217328bf2357ff044f0d18677fe588c790cNick Pelly                eeWipeTask.execute(TASK_EE_WIPE);
168431949217328bf2357ff044f0d18677fe588c790cNick Pelly                try {
168531949217328bf2357ff044f0d18677fe588c790cNick Pelly                    eeWipeTask.get();  // blocks until EE wipe is complete
168631949217328bf2357ff044f0d18677fe588c790cNick Pelly                } catch (ExecutionException e) {
168731949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.w(TAG, "failed to wipe NFC-EE");
168831949217328bf2357ff044f0d18677fe588c790cNick Pelly                } catch (InterruptedException e) {
168931949217328bf2357ff044f0d18677fe588c790cNick Pelly                    Log.w(TAG, "failed to wipe NFC-EE");
169031949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
169131949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
16927a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                boolean dataRemoved = intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false);
16937a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                if (dataRemoved) {
16947a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                    Uri data = intent.getData();
16957a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                    if (data == null) return;
16967a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                    String packageName = data.getSchemeSpecificPart();
16977a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton
16987a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                    synchronized (NfcService.this) {
169984e1e0adc2516afd35ebab029a52e764e0490559Jason parks                        if (mSePackages.contains(packageName)) {
170031949217328bf2357ff044f0d18677fe588c790cNick Pelly                            new EnableDisableTask().execute(TASK_EE_WIPE);
170184e1e0adc2516afd35ebab029a52e764e0490559Jason parks                            mSePackages.remove(packageName);
17027a7f8f8fd82936f0ee005ccfa7ac5c36760ed902Jeff Hamilton                        }
1703bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks                    }
1704bcd6a9954c5abafc6b14aabcc7768d0f03cc956cJason parks                }
170531949217328bf2357ff044f0d18677fe588c790cNick Pelly            } else if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
170631949217328bf2357ff044f0d18677fe588c790cNick Pelly                boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
170731949217328bf2357ff044f0d18677fe588c790cNick Pelly                // Query the airplane mode from Settings.System just to make sure that
170831949217328bf2357ff044f0d18677fe588c790cNick Pelly                // some random app is not sending this intent
170931949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (isAirplaneModeOn != isAirplaneModeOn()) {
171031949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return;
171131949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
171231949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (!mIsAirplaneSensitive) {
171331949217328bf2357ff044f0d18677fe588c790cNick Pelly                    return;
171431949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
171531949217328bf2357ff044f0d18677fe588c790cNick Pelly                if (isAirplaneModeOn) {
171631949217328bf2357ff044f0d18677fe588c790cNick Pelly                    new EnableDisableTask().execute(TASK_DISABLE);
171731949217328bf2357ff044f0d18677fe588c790cNick Pelly                } else if (!isAirplaneModeOn && mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT)) {
171831949217328bf2357ff044f0d18677fe588c790cNick Pelly                    new EnableDisableTask().execute(TASK_ENABLE);
171931949217328bf2357ff044f0d18677fe588c790cNick Pelly                }
1720f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly            }
1721f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly        }
1722f067256d9556b1e01143f9ae2fd824fa5dc03138Nick Pelly    };
172331949217328bf2357ff044f0d18677fe588c790cNick Pelly
172431949217328bf2357ff044f0d18677fe588c790cNick Pelly    /** Returns true if airplane mode is currently on */
172531949217328bf2357ff044f0d18677fe588c790cNick Pelly    boolean isAirplaneModeOn() {
172631949217328bf2357ff044f0d18677fe588c790cNick Pelly        return Settings.System.getInt(mContext.getContentResolver(),
172731949217328bf2357ff044f0d18677fe588c790cNick Pelly                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
172831949217328bf2357ff044f0d18677fe588c790cNick Pelly    }
172931949217328bf2357ff044f0d18677fe588c790cNick Pelly
173031949217328bf2357ff044f0d18677fe588c790cNick Pelly    /** for debugging only - no il8n */
173131949217328bf2357ff044f0d18677fe588c790cNick Pelly    static String stateToString(int state) {
173231949217328bf2357ff044f0d18677fe588c790cNick Pelly        switch (state) {
173331949217328bf2357ff044f0d18677fe588c790cNick Pelly            case NfcAdapter.STATE_OFF:
173431949217328bf2357ff044f0d18677fe588c790cNick Pelly                return "off";
173531949217328bf2357ff044f0d18677fe588c790cNick Pelly            case NfcAdapter.STATE_TURNING_ON:
173631949217328bf2357ff044f0d18677fe588c790cNick Pelly                return "turning on";
173731949217328bf2357ff044f0d18677fe588c790cNick Pelly            case NfcAdapter.STATE_ON:
173831949217328bf2357ff044f0d18677fe588c790cNick Pelly                return "on";
173931949217328bf2357ff044f0d18677fe588c790cNick Pelly            case NfcAdapter.STATE_TURNING_OFF:
174031949217328bf2357ff044f0d18677fe588c790cNick Pelly                return "turning off";
174131949217328bf2357ff044f0d18677fe588c790cNick Pelly            default:
174231949217328bf2357ff044f0d18677fe588c790cNick Pelly                return "<error>";
174331949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
174431949217328bf2357ff044f0d18677fe588c790cNick Pelly    }
174531949217328bf2357ff044f0d18677fe588c790cNick Pelly
174631949217328bf2357ff044f0d18677fe588c790cNick Pelly    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
174731949217328bf2357ff044f0d18677fe588c790cNick Pelly        synchronized (this) {
174831949217328bf2357ff044f0d18677fe588c790cNick Pelly            pw.println("mState=" + stateToString(mState));
174931949217328bf2357ff044f0d18677fe588c790cNick Pelly            pw.println("mIsZeroClickRequested=" + mIsZeroClickRequested);
175031949217328bf2357ff044f0d18677fe588c790cNick Pelly            pw.println("mIsScreenUnlocked=" + mIsScreenUnlocked);
175131949217328bf2357ff044f0d18677fe588c790cNick Pelly            pw.println("mIsAirplaneSensitive=" + mIsAirplaneSensitive);
175231949217328bf2357ff044f0d18677fe588c790cNick Pelly            pw.println("mIsAirplaneToggleable=" + mIsAirplaneToggleable);
175377d0b56bfa6a4e4e1449c476a1141a7486f34ad9Nick Pelly            mP2pLinkManager.dump(fd, pw, args);
175431949217328bf2357ff044f0d18677fe588c790cNick Pelly        }
175531949217328bf2357ff044f0d18677fe588c790cNick Pelly    }
175674180bda362a8bc9d2f701d2c17bec0f63c20bbfBrad Fitzpatrick}
1757