1c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenpackage android.nfc.cardemulation;
2c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen
3c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenimport android.annotation.SdkConstant;
4c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenimport android.annotation.SdkConstant.SdkConstantType;
5c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenimport android.app.Service;
6c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenimport android.content.Intent;
735bf6288527b177a04100585321a1266f020004aMartijn Coenenimport android.content.pm.PackageManager;
8c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenimport android.os.IBinder;
9c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen
10c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen/**
1135bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>OffHostApduService is a convenience {@link Service} class that can be
1235bf6288527b177a04100585321a1266f020004aMartijn Coenen * extended to describe one or more NFC applications that are residing
1335bf6288527b177a04100585321a1266f020004aMartijn Coenen * off-host, for example on an embedded secure element or a UICC.
1435bf6288527b177a04100585321a1266f020004aMartijn Coenen *
1535bf6288527b177a04100585321a1266f020004aMartijn Coenen * <div class="special reference">
1635bf6288527b177a04100585321a1266f020004aMartijn Coenen * <h3>Developer Guide</h3>
1735bf6288527b177a04100585321a1266f020004aMartijn Coenen * For a general introduction into the topic of card emulation,
1835bf6288527b177a04100585321a1266f020004aMartijn Coenen * please read the <a href="{@docRoot}guide/topics/nfc/ce.html">
1935bf6288527b177a04100585321a1266f020004aMartijn Coenen * NFC card emulation developer guide.</a></p>
2035bf6288527b177a04100585321a1266f020004aMartijn Coenen * </div>
2135bf6288527b177a04100585321a1266f020004aMartijn Coenen *
2235bf6288527b177a04100585321a1266f020004aMartijn Coenen * <h3>NFC Protocols</h3>
2335bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>Off-host applications represented by this class are based on the NFC-Forum ISO-DEP
2435bf6288527b177a04100585321a1266f020004aMartijn Coenen * protocol (based on ISO/IEC 14443-4) and support processing
2535bf6288527b177a04100585321a1266f020004aMartijn Coenen * command Application Protocol Data Units (APDUs) as
2635bf6288527b177a04100585321a1266f020004aMartijn Coenen * defined in the ISO/IEC 7816-4 specification.
2735bf6288527b177a04100585321a1266f020004aMartijn Coenen *
2835bf6288527b177a04100585321a1266f020004aMartijn Coenen * <h3>Service selection</h3>
2935bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>When a remote NFC device wants to talk to your
3035bf6288527b177a04100585321a1266f020004aMartijn Coenen * off-host NFC application, it sends a so-called
3135bf6288527b177a04100585321a1266f020004aMartijn Coenen * "SELECT AID" APDU as defined in the ISO/IEC 7816-4 specification.
3235bf6288527b177a04100585321a1266f020004aMartijn Coenen * The AID is an application identifier defined in ISO/IEC 7816-4.
3335bf6288527b177a04100585321a1266f020004aMartijn Coenen *
3435bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>The registration procedure for AIDs is defined in the
3535bf6288527b177a04100585321a1266f020004aMartijn Coenen * ISO/IEC 7816-5 specification. If you don't want to register an
3635bf6288527b177a04100585321a1266f020004aMartijn Coenen * AID, you are free to use AIDs in the proprietary range:
3735bf6288527b177a04100585321a1266f020004aMartijn Coenen * bits 8-5 of the first byte must each be set to '1'. For example,
3835bf6288527b177a04100585321a1266f020004aMartijn Coenen * "0xF00102030405" is a proprietary AID. If you do use proprietary
3935bf6288527b177a04100585321a1266f020004aMartijn Coenen * AIDs, it is recommended to choose an AID of at least 6 bytes,
4035bf6288527b177a04100585321a1266f020004aMartijn Coenen * to reduce the risk of collisions with other applications that
4135bf6288527b177a04100585321a1266f020004aMartijn Coenen * might be using proprietary AIDs as well.
4235bf6288527b177a04100585321a1266f020004aMartijn Coenen *
4335bf6288527b177a04100585321a1266f020004aMartijn Coenen * <h3>AID groups</h3>
4435bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>In some cases, an off-host environment may need to register multiple AIDs
4535bf6288527b177a04100585321a1266f020004aMartijn Coenen * to implement a certain application, and it needs to be sure
4635bf6288527b177a04100585321a1266f020004aMartijn Coenen * that it is the default handler for all of these AIDs (as opposed
4735bf6288527b177a04100585321a1266f020004aMartijn Coenen * to some AIDs in the group going to another service).
4835bf6288527b177a04100585321a1266f020004aMartijn Coenen *
4935bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>An AID group is a list of AIDs that should be considered as
5035bf6288527b177a04100585321a1266f020004aMartijn Coenen * belonging together by the OS. For all AIDs in an AID group, the
5135bf6288527b177a04100585321a1266f020004aMartijn Coenen * OS will guarantee one of the following:
5235bf6288527b177a04100585321a1266f020004aMartijn Coenen * <ul>
5335bf6288527b177a04100585321a1266f020004aMartijn Coenen * <li>All AIDs in the group are routed to the off-host execution environment
5435bf6288527b177a04100585321a1266f020004aMartijn Coenen * <li>No AIDs in the group are routed to the off-host execution environment
5535bf6288527b177a04100585321a1266f020004aMartijn Coenen * </ul>
5635bf6288527b177a04100585321a1266f020004aMartijn Coenen * In other words, there is no in-between state, where some AIDs
5735bf6288527b177a04100585321a1266f020004aMartijn Coenen * in the group can be routed to this off-host execution environment,
5835bf6288527b177a04100585321a1266f020004aMartijn Coenen * and some to another or a host-based {@link HostApduService}.
5935bf6288527b177a04100585321a1266f020004aMartijn Coenen * <h3>AID groups and categories</h3>
6035bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>Each AID group can be associated with a category. This allows
6135bf6288527b177a04100585321a1266f020004aMartijn Coenen * the Android OS to classify services, and it allows the user to
6235bf6288527b177a04100585321a1266f020004aMartijn Coenen * set defaults at the category level instead of the AID level.
6335bf6288527b177a04100585321a1266f020004aMartijn Coenen *
6435bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>You can use
6535bf6288527b177a04100585321a1266f020004aMartijn Coenen * {@link CardEmulation#isDefaultServiceForCategory(android.content.ComponentName, String)}
6635bf6288527b177a04100585321a1266f020004aMartijn Coenen * to determine if your off-host service is the default handler for a category.
6735bf6288527b177a04100585321a1266f020004aMartijn Coenen *
6835bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>In this version of the platform, the only known categories
6935bf6288527b177a04100585321a1266f020004aMartijn Coenen * are {@link CardEmulation#CATEGORY_PAYMENT} and {@link CardEmulation#CATEGORY_OTHER}.
7035bf6288527b177a04100585321a1266f020004aMartijn Coenen * AID groups without a category, or with a category that is not recognized
7135bf6288527b177a04100585321a1266f020004aMartijn Coenen * by the current platform version, will automatically be
7235bf6288527b177a04100585321a1266f020004aMartijn Coenen * grouped into the {@link CardEmulation#CATEGORY_OTHER} category.
7335bf6288527b177a04100585321a1266f020004aMartijn Coenen *
7435bf6288527b177a04100585321a1266f020004aMartijn Coenen * <h3>Service AID registration</h3>
7535bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>To tell the platform which AIDs
7635bf6288527b177a04100585321a1266f020004aMartijn Coenen * reside off-host and are managed by this service, a {@link #SERVICE_META_DATA}
7735bf6288527b177a04100585321a1266f020004aMartijn Coenen * entry must be included in the declaration of the service. An
7835bf6288527b177a04100585321a1266f020004aMartijn Coenen * example of a OffHostApduService manifest declaration is shown below:
7935bf6288527b177a04100585321a1266f020004aMartijn Coenen * <pre> &lt;service android:name=".MyOffHostApduService" android:exported="true" android:permission="android.permission.BIND_NFC_SERVICE"&gt;
8035bf6288527b177a04100585321a1266f020004aMartijn Coenen *     &lt;intent-filter&gt;
8135bf6288527b177a04100585321a1266f020004aMartijn Coenen *         &lt;action android:name="android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE"/&gt;
8235bf6288527b177a04100585321a1266f020004aMartijn Coenen *     &lt;/intent-filter&gt;
8335bf6288527b177a04100585321a1266f020004aMartijn Coenen *     &lt;meta-data android:name="android.nfc.cardemulation.off_host_apdu_ervice" android:resource="@xml/apduservice"/&gt;
8435bf6288527b177a04100585321a1266f020004aMartijn Coenen * &lt;/service&gt;</pre>
8535bf6288527b177a04100585321a1266f020004aMartijn Coenen *
8635bf6288527b177a04100585321a1266f020004aMartijn Coenen * This meta-data tag points to an apduservice.xml file.
8735bf6288527b177a04100585321a1266f020004aMartijn Coenen * An example of this file with a single AID group declaration is shown below:
8835bf6288527b177a04100585321a1266f020004aMartijn Coenen * <pre>
8935bf6288527b177a04100585321a1266f020004aMartijn Coenen * &lt;offhost-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
9035bf6288527b177a04100585321a1266f020004aMartijn Coenen *           android:description="@string/servicedesc"&gt;
9135bf6288527b177a04100585321a1266f020004aMartijn Coenen *       &lt;aid-group android:description="@string/subscription" android:category="other">
9235bf6288527b177a04100585321a1266f020004aMartijn Coenen *           &lt;aid-filter android:name="F0010203040506"/&gt;
9335bf6288527b177a04100585321a1266f020004aMartijn Coenen *           &lt;aid-filter android:name="F0394148148100"/&gt;
9435bf6288527b177a04100585321a1266f020004aMartijn Coenen *       &lt;/aid-group&gt;
9535bf6288527b177a04100585321a1266f020004aMartijn Coenen * &lt;/offhost-apdu-service&gt;
9635bf6288527b177a04100585321a1266f020004aMartijn Coenen * </pre>
9735bf6288527b177a04100585321a1266f020004aMartijn Coenen *
9835bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>The {@link android.R.styleable#OffHostApduService &lt;offhost-apdu-service&gt;} is required
9935bf6288527b177a04100585321a1266f020004aMartijn Coenen * to contain a
10035bf6288527b177a04100585321a1266f020004aMartijn Coenen * {@link android.R.styleable#OffHostApduService_description &lt;android:description&gt;}
10135bf6288527b177a04100585321a1266f020004aMartijn Coenen * attribute that contains a user-friendly description of the service that may be shown in UI.
10235bf6288527b177a04100585321a1266f020004aMartijn Coenen *
10335bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p>The {@link android.R.styleable#OffHostApduService &lt;offhost-apdu-service&gt;} must
10435bf6288527b177a04100585321a1266f020004aMartijn Coenen * contain one or more {@link android.R.styleable#AidGroup &lt;aid-group&gt;} tags.
10535bf6288527b177a04100585321a1266f020004aMartijn Coenen * Each {@link android.R.styleable#AidGroup &lt;aid-group&gt;} must contain one or
10635bf6288527b177a04100585321a1266f020004aMartijn Coenen * more {@link android.R.styleable#AidFilter &lt;aid-filter&gt;} tags, each of which
10735bf6288527b177a04100585321a1266f020004aMartijn Coenen * contains a single AID. The AID must be specified in hexadecimal format, and contain
10835bf6288527b177a04100585321a1266f020004aMartijn Coenen * an even number of characters.
109c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen *
110c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen * <p>This registration will allow the service to be included
11135bf6288527b177a04100585321a1266f020004aMartijn Coenen * as an option for being the default handler for categories.
11235bf6288527b177a04100585321a1266f020004aMartijn Coenen * The Android OS will take care of correctly
11335bf6288527b177a04100585321a1266f020004aMartijn Coenen * routing the AIDs to the off-host execution environment,
11435bf6288527b177a04100585321a1266f020004aMartijn Coenen * based on which service the user has selected to be the handler for a certain category.
115c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen *
116c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen * <p>The service may define additional actions outside of the
117c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen * Android namespace that provide further interaction with
118c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen * the off-host execution environment.
119c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen *
12035bf6288527b177a04100585321a1266f020004aMartijn Coenen * <p class="note">Use of this class requires the
12135bf6288527b177a04100585321a1266f020004aMartijn Coenen * {@link PackageManager#FEATURE_NFC_HOST_CARD_EMULATION} to be present
12235bf6288527b177a04100585321a1266f020004aMartijn Coenen * on the device.
123c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen */
124c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenenpublic abstract class OffHostApduService extends Service {
125c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen    /**
12635bf6288527b177a04100585321a1266f020004aMartijn Coenen     * The {@link Intent} action that must be declared as handled by the service.
127c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     */
128c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen    @SdkConstant(SdkConstantType.SERVICE_ACTION)
129c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen    public static final String SERVICE_INTERFACE =
13052246087f4e2b5ad62b9cd6ea8c2cb58f624d4e7Martijn Coenen            "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE";
131c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen
132c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen    /**
133c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * The name of the meta-data element that contains
134c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * more information about this service.
135c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     */
13652246087f4e2b5ad62b9cd6ea8c2cb58f624d4e7Martijn Coenen    public static final String SERVICE_META_DATA =
13752246087f4e2b5ad62b9cd6ea8c2cb58f624d4e7Martijn Coenen            "android.nfc.cardemulation.off_host_apdu_service";
138c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen
139c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen    /**
140c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * The Android platform itself will not bind to this service,
141c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * but merely uses its declaration to keep track of what AIDs
142c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * the service is interested in. This information is then used
143c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * to present the user with a list of applications that can handle
144c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * an AID, as well as correctly route those AIDs either to the host (in case
145c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * the user preferred a {@link HostApduService}), or to an off-host
146c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * execution environment (in case the user preferred a {@link OffHostApduService}.
147c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     *
148c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * Implementers may define additional actions outside of the
149c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * Android namespace that allow further interactions with
150c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * the off-host execution environment. Such implementations
151c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     * would need to override this method.
152c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen     */
153c3178e5f1c346ad5faa926a6765147840116f4a7Martijn Coenen    public abstract IBinder onBind(Intent intent);
154a7397883de67d674970d91f86d46ccf637e5e543Martijn Coenen}
155