1c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito/*
2c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * Copyright (C) 2015 The Android Open Source Project
3c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito *
4c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * Licensed under the Apache License, Version 2.0 (the "License");
5c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * you may not use this file except in compliance with the License.
6c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * You may obtain a copy of the License at
7c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito *
8c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito *      http://www.apache.org/licenses/LICENSE-2.0
9c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito *
10c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * Unless required by applicable law or agreed to in writing, software
11c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * distributed under the License is distributed on an "AS IS" BASIS,
12c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * See the License for the specific language governing permissions and
14c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * limitations under the License.
15c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito */
16c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
17c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itopackage android.nfc.cardemulation;
18c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
19c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.annotation.SdkConstant;
20c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.annotation.SdkConstant.SdkConstantType;
21c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.app.Service;
22007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenenimport android.content.ComponentName;
23c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.content.Intent;
24c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.os.Bundle;
25c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.os.Handler;
26c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.os.IBinder;
27c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.os.Message;
28c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.os.Messenger;
29c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.os.RemoteException;
30c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itoimport android.util.Log;
31c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
32c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito/**
33c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * <p>HostNfcFService is a convenience {@link Service} class that can be
34c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito * extended to emulate an NFC-F card inside an Android service component.
35007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
36007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <h3>NFC Protocols</h3>
37007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>Cards emulated by this class are based on the NFC-Forum NFC-F
38007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * protocol (based on the JIS-X 6319-4 specification.)</p>
39007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
40007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <h3>System Code and NFCID2 registration</h3>
41007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>A {@link HostNfcFService HostNfcFService service} can register
42007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * exactly one System Code and one NFCID2. For details about the use of
43007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * System Code and NFCID2, see the NFC Forum Digital specification.</p>
44007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>To statically register a System Code and NFCID2 with the service, a {@link #SERVICE_META_DATA}
456136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen * entry must be included in the declaration of the service.
466136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen *
476136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen * <p>All {@link HostNfcFService HostNfcFService} declarations in the manifest must require the
486136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen * {@link android.Manifest.permission#BIND_NFC_SERVICE} permission
496136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen * in their &lt;service&gt; tag, to ensure that only the platform can bind to your service.</p>
506136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen *
516136730e3bf463f744d22d4d5a236d9183b9064eMartijn Coenen * <p>An example of a HostNfcFService manifest declaration is shown below:
52007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
53007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <pre> &lt;service android:name=".MyHostNfcFService" android:exported="true" android:permission="android.permission.BIND_NFC_SERVICE"&gt;
54007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *     &lt;intent-filter&gt;
55007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *         &lt;action android:name="android.nfc.cardemulation.action.HOST_NFCF_SERVICE"/&gt;
56007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *     &lt;/intent-filter&gt;
57007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *     &lt;meta-data android:name="android.nfc.cardemulation.host_nfcf_service" android:resource="@xml/nfcfservice"/&gt;
58007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * &lt;/service&gt;</pre>
59007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
60007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * This meta-data tag points to an nfcfservice.xml file.
61007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * An example of this file with a System Code and NFCID2 declaration is shown below:
62007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <pre>
63007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * &lt;host-nfcf-service xmlns:android="http://schemas.android.com/apk/res/android"
64007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *           android:description="@string/servicedesc"&gt;
65007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *       &lt;system-code-filter android:name="4000"/&gt;
66007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *       &lt;nfcid2-filter android:name="02FE000000000000"/&gt;
67007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * &lt;/host-nfcf-service&gt;
68007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * </pre>
69007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
70007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>The {@link android.R.styleable#HostNfcFService &lt;host-nfcf-service&gt;} is required
71007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * to contain a
72007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * {@link android.R.styleable#HostApduService_description &lt;android:description&gt;}
73007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * attribute that contains a user-friendly description of the service that may be shown in UI.
74007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>The {@link android.R.styleable#HostNfcFService &lt;host-nfcf-service&gt;} must
75007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * contain:
76007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <ul>
77007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <li>Exactly one {@link android.R.styleable#SystemCodeFilter &lt;system-code-filter&gt;} tag.</li>
78007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <li>Exactly one {@link android.R.styleable#Nfcid2Filter &lt;nfcid2-filter&gt;} tag.</li>
79007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * </ul>
80007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * </p>
81007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
82007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>Alternatively, the System Code and NFCID2 can be dynamically registererd for a service
83007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * by using the {@link NfcFCardEmulation#registerSystemCodeForService(ComponentName, String)} and
84007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * {@link NfcFCardEmulation#setNfcid2ForService(ComponentName, String)} methods.
85007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * </p>
86007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
87007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <h3>Service selection</h3>
88007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>When a remote NFC devices wants to communicate with your service, it
89007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * sends a SENSF_REQ command to the NFC controller, requesting a System Code.
90007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * If a {@link NfcFCardEmulation NfcFCardEmulation service} has registered
91007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * this system code and has been enabled by the foreground application, the
92007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * NFC controller will respond with the NFCID2 that is registered for this service.
93007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * The reader can then continue data exchange with this service by using the NFCID2.</p>
94007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
95007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <h3>Data exchange</h3>
96007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>After service selection, all frames addressed to the NFCID2 of this service will
97007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * be sent through {@link #processNfcFPacket(byte[], Bundle)}, until the NFC link is
98007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * broken.<p>
99007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen *
100007e0292bc1b7172c27b0a588613738d02c3c809Martijn Coenen * <p>When the NFC link is broken, {@link #onDeactivated(int)} will be called.</p>
101c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito */
102c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Itopublic abstract class HostNfcFService extends Service {
103c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
104c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * The {@link Intent} action that must be declared as handled by the service.
105c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
106c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    @SdkConstant(SdkConstantType.SERVICE_ACTION)
107c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final String SERVICE_INTERFACE =
108c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            "android.nfc.cardemulation.action.HOST_NFCF_SERVICE";
109c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
110c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
111c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * The name of the meta-data element that contains
112c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * more information about this service.
113c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
114c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final String SERVICE_META_DATA =
115c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            "android.nfc.cardemulation.host_nfcf_service";
116c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
117c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
118c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * Reason for {@link #onDeactivated(int)}.
119c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * Indicates deactivation was due to the NFC link
120c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * being lost.
121c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
122c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final int DEACTIVATION_LINK_LOSS = 0;
123c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
124c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    static final String TAG = "NfcFService";
125c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
126c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
127c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * MSG_COMMAND_PACKET is sent by NfcService when
128c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * a NFC-F command packet has been received.
129c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
130c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @hide
131c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
132c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final int MSG_COMMAND_PACKET = 0;
133c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
134c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
135c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * MSG_RESPONSE_PACKET is sent to NfcService to send
136c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * a response packet back to the remote device.
137c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
138c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @hide
139c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
140c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final int MSG_RESPONSE_PACKET = 1;
141c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
142c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
143c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * MSG_DEACTIVATED is sent by NfcService when
144c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * the current session is finished; because
145c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * the NFC link was deactivated.
146c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
147c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @hide
148c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
149c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final int MSG_DEACTIVATED = 2;
150c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
151c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito   /**
152c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @hide
153c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
154c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final String KEY_DATA = "data";
155c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
156c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
157c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @hide
158c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
159c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public static final String KEY_MESSENGER = "messenger";
160c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
161c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
162c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * Messenger interface to NfcService for sending responses.
163c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * Only accessed on main thread by the message handler.
164c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
165c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @hide
166c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
167c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    Messenger mNfcService = null;
168c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
169c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    final Messenger mMessenger = new Messenger(new MsgHandler());
170c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
171c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    final class MsgHandler extends Handler {
172c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        @Override
173c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        public void handleMessage(Message msg) {
174c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            switch (msg.what) {
175c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            case MSG_COMMAND_PACKET:
176c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                Bundle dataBundle = msg.getData();
177c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                if (dataBundle == null) {
178c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    return;
179c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                }
180c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                if (mNfcService == null) mNfcService = msg.replyTo;
181c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
182c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                byte[] packet = dataBundle.getByteArray(KEY_DATA);
183c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                if (packet != null) {
184c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    byte[] responsePacket = processNfcFPacket(packet, null);
185c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    if (responsePacket != null) {
186c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        if (mNfcService == null) {
187c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                            Log.e(TAG, "Response not sent; service was deactivated.");
188c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                            return;
189c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        }
190c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        Message responseMsg = Message.obtain(null, MSG_RESPONSE_PACKET);
191c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        Bundle responseBundle = new Bundle();
192c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        responseBundle.putByteArray(KEY_DATA, responsePacket);
193c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        responseMsg.setData(responseBundle);
194c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        responseMsg.replyTo = mMessenger;
195c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        try {
196c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                            mNfcService.send(responseMsg);
197c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        } catch (RemoteException e) {
198c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                            Log.e("TAG", "Response not sent; RemoteException calling into " +
199c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                                    "NfcService.");
200c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                        }
201c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    }
202c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                } else {
203c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    Log.e(TAG, "Received MSG_COMMAND_PACKET without data.");
204c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                }
205c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                break;
206c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            case MSG_RESPONSE_PACKET:
207c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                if (mNfcService == null) {
208c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    Log.e(TAG, "Response not sent; service was deactivated.");
209c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    return;
210c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                }
211c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                try {
212c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    msg.replyTo = mMessenger;
213c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    mNfcService.send(msg);
214c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                } catch (RemoteException e) {
215c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                    Log.e(TAG, "RemoteException calling into NfcService.");
216c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                }
217c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                break;
218c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            case MSG_DEACTIVATED:
219c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                // Make sure we won't call into NfcService again
220c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                mNfcService = null;
221c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                onDeactivated(msg.arg1);
222c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                break;
223c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            default:
224c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito                super.handleMessage(msg);
225c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            }
226c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        }
227c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    }
228c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
229c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    @Override
230c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public final IBinder onBind(Intent intent) {
231c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        return mMessenger.getBinder();
232c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    }
233c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
234c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
235c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * Sends a response packet back to the remote device.
236c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
237c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * <p>Note: this method may be called from any thread and will not block.
238c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @param responsePacket A byte-array containing the response packet.
239c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
240c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public final void sendResponsePacket(byte[] responsePacket) {
241c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        Message responseMsg = Message.obtain(null, MSG_RESPONSE_PACKET);
242c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        Bundle dataBundle = new Bundle();
243c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        dataBundle.putByteArray(KEY_DATA, responsePacket);
244c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        responseMsg.setData(dataBundle);
245c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        try {
246c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            mMessenger.send(responseMsg);
247c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        } catch (RemoteException e) {
248c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito            Log.e("TAG", "Local messenger has died.");
249c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito        }
250c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    }
251c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
252c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
253c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * <p>This method will be called when a NFC-F packet has been received
254c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * from a remote device. A response packet can be provided directly
255c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * by returning a byte-array in this method. Note that in general
256c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * response packets must be sent as quickly as possible, given the fact
257c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * that the user is likely holding his device over an NFC reader
258c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * when this method is called.
259c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
260c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * <p class="note">This method is running on the main thread of your application.
261c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * If you cannot return a response packet immediately, return null
262c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * and use the {@link #sendResponsePacket(byte[])} method later.
263c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *
264c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @param commandPacket The NFC-F packet that was received from the remote device
265c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @param extras A bundle containing extra data. May be null.
266c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @return a byte-array containing the response packet, or null if no
267c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     *         response packet can be sent at this point.
268c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
269c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public abstract byte[] processNfcFPacket(byte[] commandPacket, Bundle extras);
270c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito
271c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    /**
272c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * This method will be called in following possible scenarios:
273c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * <li>The NFC link has been lost
274c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     * @param reason {@link #DEACTIVATION_LINK_LOSS}
275c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito     */
276c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito    public abstract void onDeactivated(int reason);
277c52adfe12469a1d1bcd5b7119b83363e094a7b84Yoshinobu Ito}
278