Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.dta; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.nfc.INfcDta; |
| 21 | import android.nfc.NfcAdapter; |
| 22 | import android.os.RemoteException; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import java.util.HashMap; |
| 26 | |
| 27 | /** |
| 28 | * This class provides the primary API for DTA operations. |
| 29 | * @hide |
| 30 | */ |
| 31 | public final class NfcDta { |
| 32 | private static final String TAG = "NfcDta"; |
| 33 | |
| 34 | private static INfcDta sService; |
| 35 | private static HashMap<Context, NfcDta> sNfcDtas = new HashMap<Context, NfcDta>(); |
| 36 | |
| 37 | private final Context mContext; |
| 38 | |
| 39 | private NfcDta(Context context, INfcDta service) { |
| 40 | mContext = context.getApplicationContext(); |
| 41 | sService = service; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Helper to get an instance of this class. |
| 46 | * |
| 47 | * @param adapter A reference to an NfcAdapter object. |
| 48 | * @return |
| 49 | */ |
| 50 | public static synchronized NfcDta getInstance(NfcAdapter adapter) { |
| 51 | if (adapter == null) throw new NullPointerException("NfcAdapter is null"); |
| 52 | Context context = adapter.getContext(); |
| 53 | if (context == null) { |
| 54 | Log.e(TAG, "NfcAdapter context is null."); |
| 55 | throw new UnsupportedOperationException(); |
| 56 | } |
| 57 | |
| 58 | NfcDta manager = sNfcDtas.get(context); |
| 59 | if (manager == null) { |
| 60 | INfcDta service = adapter.getNfcDtaInterface(); |
| 61 | if (service == null) { |
| 62 | Log.e(TAG, "This device does not implement the INfcDta interface."); |
| 63 | throw new UnsupportedOperationException(); |
| 64 | } |
| 65 | manager = new NfcDta(context, service); |
| 66 | sNfcDtas.put(context, manager); |
| 67 | } |
| 68 | return manager; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Enables DTA mode |
| 73 | * |
| 74 | * @return true/false if enabling was successful |
| 75 | */ |
| 76 | public boolean enableDta() { |
| 77 | try { |
| 78 | sService.enableDta(); |
| 79 | } catch (RemoteException e) { |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Disables DTA mode |
| 87 | * |
| 88 | * @return true/false if disabling was successful |
| 89 | */ |
| 90 | public boolean disableDta() { |
| 91 | try { |
| 92 | sService.disableDta(); |
| 93 | } catch (RemoteException e) { |
| 94 | return false; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Enables Server |
| 101 | * |
| 102 | * @return true/false if enabling was successful |
| 103 | */ |
| 104 | public boolean enableServer(String serviceName, int serviceSap, int miu, |
| 105 | int rwSize, int testCaseId) { |
| 106 | try { |
| 107 | return sService.enableServer(serviceName, serviceSap, miu, rwSize, testCaseId); |
| 108 | } catch (RemoteException e) { |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Disables Server |
| 115 | * |
| 116 | * @return true/false if disabling was successful |
| 117 | */ |
| 118 | public boolean disableServer() { |
| 119 | try { |
| 120 | sService.disableServer(); |
| 121 | } catch (RemoteException e) { |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Enables Client |
| 129 | * |
| 130 | * @return true/false if enabling was successful |
| 131 | */ |
| 132 | public boolean enableClient(String serviceName, int miu, int rwSize, |
| 133 | int testCaseId) { |
| 134 | try { |
| 135 | return sService.enableClient(serviceName, miu, rwSize, testCaseId); |
| 136 | } catch (RemoteException e) { |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Disables client |
| 143 | * |
| 144 | * @return true/false if disabling was successful |
| 145 | */ |
| 146 | public boolean disableClient() { |
| 147 | try { |
| 148 | sService.disableClient(); |
| 149 | } catch (RemoteException e) { |
| 150 | return false; |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Registers Message Service |
| 157 | * |
| 158 | * @return true/false if registration was successful |
| 159 | */ |
| 160 | public boolean registerMessageService(String msgServiceName) { |
| 161 | try { |
| 162 | return sService.registerMessageService(msgServiceName); |
| 163 | } catch (RemoteException e) { |
| 164 | return false; |
| 165 | } |
| 166 | } |
| 167 | } |