Nick Pelly | 367f41f | 2011-03-08 11:43:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 com.android.nfc_extras; |
| 18 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
| 21 | import android.nfc.INfcAdapterExtras; |
| 22 | import android.nfc.NfcAdapter; |
| 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | /** |
| 27 | * Provides additional methods on an {@link NfcAdapter} for Card Emulation |
| 28 | * and management of {@link NfcExecutionEnvironment}'s. |
| 29 | * |
| 30 | * There is a 1-1 relationship between an {@link NfcAdapterExtras} object and |
| 31 | * a {@link NfcAdapter} object. |
| 32 | */ |
| 33 | public final class NfcAdapterExtras { |
| 34 | private static final String TAG = "NfcAdapterExtras"; |
| 35 | |
| 36 | /** |
| 37 | * Broadcast Action: an RF field ON has been detected. |
| 38 | * |
| 39 | * <p class="note">This is an unreliable signal, and will be removed. |
| 40 | * <p class="note"> |
| 41 | * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission |
| 42 | * to receive. |
| 43 | */ |
| 44 | public static final String ACTION_RF_FIELD_ON_DETECTED = |
| 45 | "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED"; |
| 46 | |
| 47 | /** |
| 48 | * Broadcast Action: an RF field OFF has been detected. |
| 49 | * |
| 50 | * <p class="note">This is an unreliable signal, and will be removed. |
| 51 | * <p class="note"> |
| 52 | * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission |
| 53 | * to receive. |
| 54 | */ |
| 55 | public static final String ACTION_RF_FIELD_OFF_DETECTED = |
| 56 | "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; |
| 57 | |
| 58 | // protected by NfcAdapterExtras.class, and final after first construction |
| 59 | private static INfcAdapterExtras sService; |
Nick Pelly | 367f41f | 2011-03-08 11:43:30 -0800 | [diff] [blame] | 60 | private static NfcAdapterExtras sSingleton; |
| 61 | private static NfcExecutionEnvironment sEmbeddedEe; |
| 62 | private static CardEmulationRoute sRouteOff; |
| 63 | private static CardEmulationRoute sRouteOnWhenScreenOn; |
| 64 | |
| 65 | /** |
| 66 | * Get the {@link NfcAdapterExtras} for the given {@link NfcAdapter}. |
| 67 | * |
| 68 | * <p class="note"> |
| 69 | * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. |
| 70 | * |
| 71 | * @param adapter a {@link NfcAdapter}, must not be null |
| 72 | * @return the {@link NfcAdapterExtras} object for the given {@link NfcAdapter} |
| 73 | */ |
| 74 | public static NfcAdapterExtras get(NfcAdapter adapter) { |
| 75 | synchronized(NfcAdapterExtras.class) { |
mike wakerly | 0bddf0d | 2011-03-21 16:51:06 -0700 | [diff] [blame] | 76 | if (sSingleton == null) { |
| 77 | try { |
| 78 | sService = adapter.getNfcAdapterExtrasInterface(); |
| 79 | sEmbeddedEe = new NfcExecutionEnvironment(sService); |
| 80 | sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); |
| 81 | sRouteOnWhenScreenOn = new CardEmulationRoute( |
| 82 | CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, sEmbeddedEe); |
| 83 | sSingleton = new NfcAdapterExtras(); |
| 84 | } finally { |
| 85 | if (sSingleton == null) { |
| 86 | sService = null; |
| 87 | sEmbeddedEe = null; |
| 88 | sRouteOff = null; |
| 89 | sRouteOnWhenScreenOn = null; |
| 90 | } |
| 91 | } |
Nick Pelly | 367f41f | 2011-03-08 11:43:30 -0800 | [diff] [blame] | 92 | } |
| 93 | return sSingleton; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private NfcAdapterExtras() {} |
| 98 | |
| 99 | /** |
| 100 | * Immutable data class that describes a card emulation route. |
| 101 | */ |
| 102 | public final static class CardEmulationRoute { |
| 103 | /** |
| 104 | * Card Emulation is turned off on this NfcAdapter. |
| 105 | * <p>This is the default routing state after boot. |
| 106 | */ |
| 107 | public static final int ROUTE_OFF = 1; |
| 108 | |
| 109 | /** |
| 110 | * Card Emulation is routed to {@link #nfcEe} only when the screen is on, |
| 111 | * otherwise it is turned off. |
| 112 | */ |
| 113 | public static final int ROUTE_ON_WHEN_SCREEN_ON = 2; |
| 114 | |
| 115 | /** |
| 116 | * A route such as {@link #ROUTE_OFF} or {@link #ROUTE_ON_WHEN_SCREEN_ON}. |
| 117 | */ |
| 118 | public final int route; |
| 119 | |
| 120 | /** |
| 121 | * The {@link NFcExecutionEnvironment} that is Card Emulation is routed to. |
| 122 | * <p>null if {@link #route} is {@link #ROUTE_OFF}, otherwise not null. |
| 123 | */ |
| 124 | public final NfcExecutionEnvironment nfcEe; |
| 125 | |
| 126 | public CardEmulationRoute(int route, NfcExecutionEnvironment nfcEe) { |
| 127 | if (route == ROUTE_OFF && nfcEe != null) { |
| 128 | throw new IllegalArgumentException("must not specifiy a NFC-EE with ROUTE_OFF"); |
| 129 | } else if (route != ROUTE_OFF && nfcEe == null) { |
| 130 | throw new IllegalArgumentException("must specifiy a NFC-EE for this route"); |
| 131 | } |
| 132 | this.route = route; |
| 133 | this.nfcEe = nfcEe; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the routing state of this NFC EE. |
| 139 | * |
| 140 | * <p class="note"> |
| 141 | * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. |
| 142 | * |
| 143 | * @return |
| 144 | */ |
| 145 | public CardEmulationRoute getCardEmulationRoute() { |
| 146 | try { |
| 147 | int route = sService.getCardEmulationRoute(); |
| 148 | return route == CardEmulationRoute.ROUTE_OFF ? |
| 149 | sRouteOff : |
| 150 | sRouteOnWhenScreenOn; |
| 151 | } catch (RemoteException e) { |
| 152 | Log.e(TAG, "", e); |
| 153 | return sRouteOff; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Set the routing state of this NFC EE. |
| 159 | * |
| 160 | * <p>This routing state is not persisted across reboot. |
| 161 | * |
| 162 | * <p class="note"> |
| 163 | * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. |
| 164 | * |
| 165 | * @param route a {@link #CardEmulationRoute} |
| 166 | */ |
| 167 | public void setCardEmulationRoute(CardEmulationRoute route) { |
| 168 | try { |
| 169 | sService.setCardEmulationRoute(route.route); |
| 170 | } catch (RemoteException e) { |
| 171 | Log.e(TAG, "", e); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the {@link NfcExecutionEnvironment} that is embedded with the |
| 177 | * {@link NFcAdapter}. |
| 178 | * |
| 179 | * <p class="note"> |
| 180 | * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. |
| 181 | * |
| 182 | * @return a {@link NfcExecutionEnvironment}, or null if there is no embedded NFC-EE |
| 183 | */ |
| 184 | public NfcExecutionEnvironment getEmbeddedExecutionEnvironment() { |
| 185 | return sEmbeddedEe; |
| 186 | } |
| 187 | } |