Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [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 android.nfc; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.os.RemoteException; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | import java.util.HashMap; |
| 24 | |
| 25 | /** |
| 26 | * Manages NFC API's that are coupled to the life-cycle of an Activity. |
| 27 | * |
| 28 | * <p>Uses a fragment to hook into onPause() and onResume() of the host |
| 29 | * activities. |
| 30 | * |
| 31 | * <p>Ideally all of this management would be done in the NFC Service, |
| 32 | * but right now it is much easier to do it in the application process. |
| 33 | * |
| 34 | * @hide |
| 35 | */ |
| 36 | public final class NfcActivityManager extends INdefPushCallback.Stub { |
| 37 | static final String TAG = NfcAdapter.TAG; |
| 38 | static final Boolean DBG = false; |
| 39 | |
| 40 | final NfcAdapter mAdapter; |
| 41 | final HashMap<Activity, NfcActivityState> mNfcState; // contents protected by this |
| 42 | final NfcEvent mDefaultEvent; // can re-use one NfcEvent because it just contains adapter |
| 43 | |
| 44 | /** |
| 45 | * NFC state associated with an {@link Activity} |
| 46 | */ |
| 47 | class NfcActivityState { |
| 48 | boolean resumed = false; // is the activity resumed |
| 49 | NdefMessage ndefMessage; |
| 50 | NfcAdapter.CreateNdefMessageCallback ndefMessageCallback; |
| 51 | NfcAdapter.OnNdefPushCompleteCallback onNdefPushCompleteCallback; |
| 52 | @Override |
| 53 | public String toString() { |
| 54 | StringBuilder s = new StringBuilder("[").append(resumed).append(" "); |
| 55 | s.append(ndefMessage).append(" ").append(ndefMessageCallback).append(" "); |
| 56 | s.append(onNdefPushCompleteCallback).append("]"); |
| 57 | return s.toString(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public NfcActivityManager(NfcAdapter adapter) { |
| 62 | mAdapter = adapter; |
| 63 | mNfcState = new HashMap<Activity, NfcActivityState>(); |
| 64 | mDefaultEvent = new NfcEvent(mAdapter); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * onResume hook from fragment attached to activity |
| 69 | */ |
| 70 | public synchronized void onResume(Activity activity) { |
| 71 | NfcActivityState state = mNfcState.get(activity); |
| 72 | if (DBG) Log.d(TAG, "onResume() for " + activity + " " + state); |
| 73 | if (state != null) { |
| 74 | state.resumed = true; |
| 75 | updateNfcService(state); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * onPause hook from fragment attached to activity |
| 81 | */ |
| 82 | public synchronized void onPause(Activity activity) { |
| 83 | NfcActivityState state = mNfcState.get(activity); |
| 84 | if (DBG) Log.d(TAG, "onPause() for " + activity + " " + state); |
| 85 | if (state != null) { |
| 86 | state.resumed = false; |
| 87 | updateNfcService(state); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public synchronized void setNdefPushMessage(Activity activity, NdefMessage message) { |
| 92 | NfcActivityState state = getOrCreateState(activity, message != null); |
| 93 | if (state == null || state.ndefMessage == message) { |
| 94 | return; // nothing more to do; |
| 95 | } |
| 96 | state.ndefMessage = message; |
| 97 | if (message == null) { |
| 98 | maybeRemoveState(activity, state); |
| 99 | } |
| 100 | if (state.resumed) { |
| 101 | updateNfcService(state); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public synchronized void setNdefPushMessageCallback(Activity activity, |
| 106 | NfcAdapter.CreateNdefMessageCallback callback) { |
| 107 | NfcActivityState state = getOrCreateState(activity, callback != null); |
| 108 | if (state == null || state.ndefMessageCallback == callback) { |
| 109 | return; // nothing more to do; |
| 110 | } |
| 111 | state.ndefMessageCallback = callback; |
| 112 | if (callback == null) { |
| 113 | maybeRemoveState(activity, state); |
| 114 | } |
| 115 | if (state.resumed) { |
| 116 | updateNfcService(state); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public synchronized void setOnNdefPushCompleteCallback(Activity activity, |
| 121 | NfcAdapter.OnNdefPushCompleteCallback callback) { |
| 122 | NfcActivityState state = getOrCreateState(activity, callback != null); |
| 123 | if (state == null || state.onNdefPushCompleteCallback == callback) { |
| 124 | return; // nothing more to do; |
| 125 | } |
| 126 | state.onNdefPushCompleteCallback = callback; |
| 127 | if (callback == null) { |
| 128 | maybeRemoveState(activity, state); |
| 129 | } |
| 130 | if (state.resumed) { |
| 131 | updateNfcService(state); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the NfcActivityState for the specified Activity. |
| 137 | * If create is true, then create it if it doesn't already exist, |
| 138 | * and ensure the NFC fragment is attached to the activity. |
| 139 | */ |
| 140 | synchronized NfcActivityState getOrCreateState(Activity activity, boolean create) { |
| 141 | if (DBG) Log.d(TAG, "getOrCreateState " + activity + " " + create); |
| 142 | NfcActivityState state = mNfcState.get(activity); |
| 143 | if (state == null && create) { |
| 144 | state = new NfcActivityState(); |
| 145 | mNfcState.put(activity, state); |
| 146 | NfcFragment.attach(activity); |
| 147 | } |
| 148 | return state; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * If the NfcActivityState is empty then remove it, and |
| 153 | * detach it from the Activity. |
| 154 | */ |
| 155 | synchronized void maybeRemoveState(Activity activity, NfcActivityState state) { |
| 156 | if (state.ndefMessage == null && state.ndefMessageCallback == null && |
| 157 | state.onNdefPushCompleteCallback == null) { |
| 158 | mNfcState.remove(activity); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Register NfcActivityState with the NFC service. |
| 164 | */ |
| 165 | synchronized void updateNfcService(NfcActivityState state) { |
| 166 | boolean serviceCallbackNeeded = state.ndefMessageCallback != null || |
| 167 | state.onNdefPushCompleteCallback != null; |
| 168 | |
| 169 | try { |
| 170 | NfcAdapter.sService.setForegroundNdefPush(state.resumed ? state.ndefMessage : null, |
| 171 | state.resumed && serviceCallbackNeeded ? this : null); |
| 172 | } catch (RemoteException e) { |
| 173 | mAdapter.attemptDeadServiceRecovery(e); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Callback from NFC service |
| 179 | */ |
| 180 | @Override |
| 181 | public NdefMessage createMessage() { |
| 182 | NfcAdapter.CreateNdefMessageCallback callback = null; |
| 183 | synchronized (NfcActivityManager.this) { |
| 184 | for (NfcActivityState state : mNfcState.values()) { |
| 185 | if (state.resumed) { |
| 186 | callback = state.ndefMessageCallback; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // drop lock before making callback |
| 192 | if (callback != null) { |
| 193 | return callback.createNdefMessage(mDefaultEvent); |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Callback from NFC service |
| 200 | */ |
| 201 | @Override |
| 202 | public void onNdefPushComplete() { |
| 203 | NfcAdapter.OnNdefPushCompleteCallback callback = null; |
| 204 | synchronized (NfcActivityManager.this) { |
| 205 | for (NfcActivityState state : mNfcState.values()) { |
| 206 | if (state.resumed) { |
| 207 | callback = state.onNdefPushCompleteCallback; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // drop lock before making callback |
| 213 | if (callback != null) { |
| 214 | callback.onNdefPushComplete(mDefaultEvent); |
| 215 | } |
| 216 | } |
| 217 | } |