NfcExecutionEnvironment.java revision cc9ee72bd42bb40b1852f907f58305adde12ecc2
1367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly/*
2367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * Copyright (C) 2011 The Android Open Source Project
3367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *
4367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
5367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * you may not use this file except in compliance with the License.
6367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * You may obtain a copy of the License at
7367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *
8367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
9367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *
10367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * Unless required by applicable law or agreed to in writing, software
11367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
12367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * See the License for the specific language governing permissions and
14367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * limitations under the License.
15367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly */
16367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
17367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellypackage com.android.nfc_extras;
18367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
19367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport java.io.IOException;
20367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
21367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.annotation.SdkConstant;
22367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.annotation.SdkConstant.SdkConstantType;
23367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.content.Context;
24367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.nfc.INfcAdapterExtras;
25367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.nfc.NfcAdapter;
26367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.Binder;
27367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.Bundle;
28367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.IBinder;
29367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.RemoteException;
30367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
31367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellypublic class NfcExecutionEnvironment {
32cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly    private final NfcAdapterExtras mExtras;
33367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
34367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
35367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Broadcast Action: An ISO-DEP AID was selected.
36367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
37367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>This happens as the result of a 'SELECT AID' command from an
38367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * external NFC reader/writer.
39367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
40367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Always contains the extra field {@link #EXTRA_AID}
41367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
42367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
43367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission
44367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * to receive.
45367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
46367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
47367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public static final String ACTION_AID_SELECTED =
48367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        "com.android.nfc_extras.action.AID_SELECTED";
49367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
50367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
51367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Mandatory byte array extra field in {@link #ACTION_AID_SELECTED}.
52367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
53367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Contains the AID selected.
54367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @hide
55367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
56367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID";
57367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
58cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly    NfcExecutionEnvironment(NfcAdapterExtras extras) {
59cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly        mExtras = extras;
60367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
61367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
62367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
63367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Open the NFC Execution Environment on its contact interface.
64367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
65367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Only one process may open the secure element at a time. If it is
66367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * already open, an {@link IOException} is thrown.
67367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
68367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>All other NFC functionality is disabled while the NFC-EE is open
69367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * on its contact interface, so make sure to call {@link #close} once complete.
70367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
71367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
72367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
73367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
74367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is already open, or some other error occurs
75367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
76367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public void open() throws IOException {
77367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
78cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            Bundle b = mExtras.getService().open(new Binder());
79367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly            throwBundle(b);
80367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
81cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
82cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead, try again");
83367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
84367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
85367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
86367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
87367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Close the NFC Execution Environment on its contact interface.
88367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
89367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
90367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
91367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
92367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is already open, or some other error occurs
93367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
94367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public void close() throws IOException {
95367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
96cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throwBundle(mExtras.getService().close());
97367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
98cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
99cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead");
100367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
101367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
102367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
103367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
104367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Send raw commands to the NFC-EE and receive the response.
105367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
106367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
107367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
108367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
109367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is not open, or some other error occurs
110367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
111367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public byte[] transceive(byte[] in) throws IOException {
112367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        Bundle b;
113367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
114cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            b = mExtras.getService().transceive(in);
115367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
116cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
117cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead, need to re-open");
118367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
119367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        throwBundle(b);
120367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        return b.getByteArray("out");
121367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
122367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
123367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    private static void throwBundle(Bundle b) throws IOException {
124367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        if (b.getInt("e") == -1) {
125367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly            throw new IOException(b.getString("m"));
126367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
127367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
128367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly}
129