OSDN Git Service

Merge "cherrypick from master: Change-Id: I169749dc594ca1d79a802db4c53ec330924fdd2c...
[android-x86/frameworks-base.git] / core / java / android / nfc / NfcSecureElement.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package android.nfc;
18
19 import android.nfc.tech.TagTechnology;
20 import android.os.RemoteException;
21 import android.util.Log;
22
23 import java.io.IOException;
24
25 //import android.util.Log;
26
27 /**
28  * This class provides the primary API for managing all aspects Secure Element.
29  * Get an instance of this class by calling
30  * Context.getSystemService(Context.NFC_SERVICE).
31  * @hide
32  */
33 public final class NfcSecureElement {
34
35     private static final String TAG = "NfcSecureElement";
36
37     private INfcSecureElement mService;
38
39
40     /**
41      * @hide
42      */
43     public NfcSecureElement(INfcSecureElement mSecureElementService) {
44         mService = mSecureElementService;
45     }
46
47     public int openSecureElementConnection(String seType) throws IOException {
48         if (seType.equals("SmartMX")) {
49             try {
50                 int handle = mService.openSecureElementConnection();
51                 // Handle potential errors
52                 if (handle != 0) {
53                     return handle;
54                 } else {
55                     throw new IOException("SmartMX connection not allowed");
56                 }
57             } catch (RemoteException e) {
58                 Log.e(TAG, "RemoteException in openSecureElementConnection(): ", e);
59                 return 0;
60             }
61
62         } else if (seType.equals("UICC")) {
63             return 0;
64         } else {
65                 throw new IOException("Wrong Secure Element type");
66         }
67     }
68
69
70     public byte [] exchangeAPDU(int handle,byte [] data) throws IOException {
71
72
73         // Perform exchange APDU
74         try {
75             byte[] response = mService.exchangeAPDU(handle, data);
76             // Handle potential errors
77             if (response == null) {
78                 throw new IOException("Exchange APDU failed");
79             }
80             return response;
81         } catch (RemoteException e) {
82             Log.e(TAG, "RemoteException in exchangeAPDU(): ", e);
83             return null;
84         }
85     }
86
87     public void closeSecureElementConnection(int handle) throws IOException {
88
89         try {
90             int status = mService.closeSecureElementConnection(handle);
91             // Handle potential errors
92             if (ErrorCodes.isError(status)) {
93                 throw new IOException("Error during the conection close");
94             };
95         } catch (RemoteException e) {
96             Log.e(TAG, "RemoteException in closeSecureElement(): ", e);
97         }
98     }
99
100
101     /**
102      * Returns target type. constants.
103      *
104      * @return Secure Element technology type. The possible values are defined in
105      * {@link TagTechnology}
106      *
107      */
108     public int[] getSecureElementTechList(int handle) throws IOException {
109         try {
110             return mService.getSecureElementTechList(handle);
111         } catch (RemoteException e) {
112             Log.e(TAG, "RemoteException in getType(): ", e);
113             return null;
114         }
115     }
116
117     /**
118      * Returns Secure Element UID.
119      *
120      * @return Secure Element UID.
121      */
122     public byte[] getSecureElementUid(int handle) throws IOException {
123
124         byte[] uid = null;
125         try {
126             uid = mService.getSecureElementUid(handle);
127             // Handle potential errors
128             if (uid == null) {
129                 throw new IOException("Get Secure Element UID failed");
130             }
131             return uid;
132         } catch (RemoteException e) {
133             Log.e(TAG, "RemoteException in getType(): ", e);
134             return null;
135         }
136     }
137
138 }