1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.nfc;
import com.android.nfc.ndefpush.NdefPushClient;
import com.android.nfc.ndefpush.NdefPushServer;
import com.android.nfc.snep.SnepClient;
import com.android.nfc.snep.SnepMessage;
import com.android.nfc.snep.SnepServer;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.nfc.INdefPushCallback;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Profile;
import android.util.Log;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charsets;
import java.util.Arrays;
import java.util.List;
/**
* Interface to listen for P2P events.
* All callbacks are made from the UI thread.
*/
interface P2pEventListener {
/**
* Indicates a P2P device is in range.
* <p>onP2pInRange() and onP2pOutOfRange() will always be called
* alternately.
* <p>All other callbacks will only occur while a P2P device is in range.
*/
public void onP2pInRange();
/**
* Called when a NDEF payload is prepared to send, and confirmation is
* required. Call Callback.onP2pSendConfirmed() to make the confirmation.
*/
public void onP2pSendConfirmationRequested();
/**
* Called to indicate a send was successful.
*/
public void onP2pSendComplete();
/**
* Called to indicate a receive was successful.
*/
public void onP2pReceiveComplete();
/**
* Indicates the P2P device went out of range.
*/
public void onP2pOutOfRange();
public interface Callback {
public void onP2pSendConfirmed();
}
}
/**
* Manages sending and receiving NDEF message over LLCP link.
* Does simple debouncing of the LLCP link - so that even if the link
* drops and returns the user does not know.
*/
public class P2pLinkManager implements Handler.Callback, P2pEventListener.Callback {
static final String TAG = "NfcP2pLinkManager";
static final boolean DBG = true;
// TODO dynamically assign SAP values
static final int NDEFPUSH_SAP = 0x10;
static final int LINK_DEBOUNCE_MS = 750;
static final int MSG_DEBOUNCE_TIMEOUT = 1;
static final int MSG_RECEIVE_COMPLETE = 2;
static final int MSG_SEND_COMPLETE = 3;
// values for mLinkState
static final int LINK_STATE_DOWN = 1;
static final int LINK_STATE_UP = 2;
static final int LINK_STATE_DEBOUNCE =3;
// values for mSendState
static final int SEND_STATE_NOTHING_TO_SEND = 1;
static final int SEND_STATE_NEED_CONFIRMATION = 2;
static final int SEND_STATE_SENDING = 3;
static final Uri PROFILE_URI = Profile.CONTENT_VCARD_URI.buildUpon().
appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
build();
final NdefPushServer mNdefPushServer;
final SnepServer mDefaultSnepServer;
final ActivityManager mActivityManager;
final PackageManager mPackageManager;
final Context mContext;
final P2pEventListener mEventListener;
final Handler mHandler;
// Locked on NdefP2pManager.this
int mLinkState;
int mSendState; // valid during LINK_STATE_UP or LINK_STATE_DEBOUNCE
boolean mIsSendEnabled;
boolean mIsReceiveEnabled;
NdefMessage mMessageToSend; // valid during SEND_STATE_NEED_CONFIRMATION or SEND_STATE_SENDING
NdefMessage mStaticNdef;
INdefPushCallback mCallbackNdef;
SendTask mSendTask;
SharedPreferences mPrefs;
boolean mFirstBeam;
public P2pLinkManager(Context context) {
mNdefPushServer = new NdefPushServer(NDEFPUSH_SAP, mNppCallback);
mDefaultSnepServer = new SnepServer(mDefaultSnepCallback);
mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mPackageManager = context.getPackageManager();
mContext = context;
mEventListener = new P2pEventManager(context, this);
mHandler = new Handler(this);
mLinkState = LINK_STATE_DOWN;
mSendState = SEND_STATE_NOTHING_TO_SEND;
mIsSendEnabled = false;
mIsReceiveEnabled = false;
mPrefs = context.getSharedPreferences(NfcService.PREF, Context.MODE_PRIVATE);
mFirstBeam = mPrefs.getBoolean(NfcService.PREF_FIRST_BEAM, true);
}
/**
* May be called from any thread.
* Assumes that NFC is already on if any parameter is true.
*/
public void enableDisable(boolean sendEnable, boolean receiveEnable) {
synchronized (this) {
if (!mIsReceiveEnabled && receiveEnable) {
mDefaultSnepServer.start();
mNdefPushServer.start();
} else if (mIsReceiveEnabled && !receiveEnable) {
mDefaultSnepServer.stop();
mNdefPushServer.stop();
}
mIsSendEnabled = sendEnable;
mIsReceiveEnabled = receiveEnable;
}
}
/**
* Set NDEF message or callback for sending.
* May be called from any thread.
* NDEF messages or callbacks may be set at any time (even if NFC is
* currently off or P2P send is currently off). They will become
* active as soon as P2P send is enabled.
*/
public void setNdefToSend(NdefMessage staticNdef, INdefPushCallback callbackNdef) {
synchronized (this) {
mStaticNdef = staticNdef;
mCallbackNdef = callbackNdef;
}
}
/**
* Must be called on UI Thread.
*/
public void onLlcpActivated() {
Log.i(TAG, "LLCP activated");
synchronized (P2pLinkManager.this) {
switch (mLinkState) {
case LINK_STATE_DOWN:
mLinkState = LINK_STATE_UP;
mSendState = SEND_STATE_NOTHING_TO_SEND;
if (DBG) Log.d(TAG, "onP2pInRange()");
mEventListener.onP2pInRange();
prepareMessageToSend();
if (mMessageToSend != null) {
mSendState = SEND_STATE_NEED_CONFIRMATION;
if (DBG) Log.d(TAG, "onP2pSendConfirmationRequested()");
mEventListener.onP2pSendConfirmationRequested();
}
break;
case LINK_STATE_UP:
if (DBG) Log.d(TAG, "Duplicate onLlcpActivated()");
return;
case LINK_STATE_DEBOUNCE:
mLinkState = LINK_STATE_UP;
mHandler.removeMessages(MSG_DEBOUNCE_TIMEOUT);
if (mSendState == SEND_STATE_SENDING) {
Log.i(TAG, "Retry send...");
sendNdefMessage();
}
break;
}
}
}
void prepareMessageToSend() {
synchronized (P2pLinkManager.this) {
if (!mIsSendEnabled) {
mMessageToSend = null;
return;
}
NdefMessage messageToSend = mStaticNdef;
INdefPushCallback callback = mCallbackNdef;
if (callback != null) {
try {
messageToSend = callback.createMessage();
} catch (RemoteException e) {
// Ignore
}
}
if (messageToSend == null) {
messageToSend = createDefaultNdef();
}
mMessageToSend = messageToSend;
}
}
NdefMessage createDefaultNdef() {
List<RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
if (tasks.size() > 0) {
String pkg = tasks.get(0).baseActivity.getPackageName();
try {
ApplicationInfo appInfo = mPackageManager.getApplicationInfo(pkg, 0);
if (0 == (appInfo.flags & ApplicationInfo.FLAG_SYSTEM)) {
NdefRecord appUri = NdefRecord.createUri(
Uri.parse("http://market.android.com/search?q=pname:" + pkg));
NdefRecord appRecord = NdefRecord.createApplicationRecord(pkg);
return new NdefMessage(new NdefRecord[] { appUri, appRecord });
}
} catch (NameNotFoundException e) {
Log.e(TAG, "Bad package returned from ActivityManager: " + pkg);
}
} else {
Log.d(TAG, "no foreground activity");
}
return null;
}
/**
* Must be called on UI Thread.
*/
public void onLlcpDeactivated() {
Log.i(TAG, "LLCP deactivated.");
synchronized (this) {
switch (mLinkState) {
case LINK_STATE_DOWN:
case LINK_STATE_DEBOUNCE:
Log.i(TAG, "Duplicate onLlcpDectivated()");
break;
case LINK_STATE_UP:
// Debounce
mLinkState = LINK_STATE_DEBOUNCE;
mHandler.sendEmptyMessageDelayed(MSG_DEBOUNCE_TIMEOUT, LINK_DEBOUNCE_MS);
cancelSendNdefMessage();
break;
}
}
}
void onSendComplete(NdefMessage msg, long elapsedRealtime) {
if (mFirstBeam) {
EventLogTags.writeNfcFirstShare();
mPrefs.edit().putBoolean(NfcService.PREF_FIRST_BEAM, false).apply();
mFirstBeam = false;
}
EventLogTags.writeNfcShare(getMessageSize(msg), getMessageTnf(msg), getMessageType(msg),
getMessageAarPresent(msg), (int) elapsedRealtime);
// Make callbacks on UI thread
mHandler.sendEmptyMessage(MSG_SEND_COMPLETE);
}
void sendNdefMessage() {
synchronized (this) {
cancelSendNdefMessage();
mSendTask = new SendTask();
mSendTask.execute();
}
}
void cancelSendNdefMessage() {
synchronized (P2pLinkManager.this) {
if (mSendTask != null) {
mSendTask.cancel(true);
}
}
}
final class SendTask extends AsyncTask<Void, Void, Void> {
@Override
public Void doInBackground(Void... args) {
NdefMessage m;
boolean result;
synchronized (P2pLinkManager.this) {
if (mLinkState != LINK_STATE_UP || mSendState != SEND_STATE_SENDING) {
return null;
}
m = mMessageToSend;
}
long time = SystemClock.elapsedRealtime();
try {
if (DBG) Log.d(TAG, "Sending ndef via SNEP");
result = doSnepProtocol(m);
} catch (IOException e) {
Log.i(TAG, "Failed to connect over SNEP, trying NPP");
if (isCancelled()) {
return null;
}
result = new NdefPushClient().push(m);
}
time = SystemClock.elapsedRealtime() - time;
if (DBG) Log.d(TAG, "SendTask result=" + result + ", time ms=" + time);
if (result) {
onSendComplete(m, time);
}
return null;
}
}
static boolean doSnepProtocol(NdefMessage msg) throws IOException {
SnepClient snepClient = new SnepClient();
try {
snepClient.connect();
} catch (IOException e) {
// Throw exception to fall back to NPP.
snepClient.close();
throw new IOException("SNEP not available.", e);
}
try {
snepClient.put(msg);
return true;
} catch (IOException e) {
// SNEP available but had errors, don't fall back to NPP.
} finally {
snepClient.close();
}
return false;
}
final NdefPushServer.Callback mNppCallback = new NdefPushServer.Callback() {
@Override
public void onMessageReceived(NdefMessage msg) {
onReceiveComplete(msg);
}
};
final SnepServer.Callback mDefaultSnepCallback = new SnepServer.Callback() {
@Override
public SnepMessage doPut(NdefMessage msg) {
onReceiveComplete(msg);
return SnepMessage.getMessage(SnepMessage.RESPONSE_SUCCESS);
}
@Override
public SnepMessage doGet(int acceptableLength, NdefMessage msg) {
if (DBG) Log.d(TAG, "GET not supported.");
return SnepMessage.getMessage(SnepMessage.RESPONSE_NOT_IMPLEMENTED);
}
};
void onReceiveComplete(NdefMessage msg) {
EventLogTags.writeNfcNdefReceived(getMessageSize(msg), getMessageTnf(msg),
getMessageType(msg), getMessageAarPresent(msg));
// Make callbacks on UI thread
mHandler.obtainMessage(MSG_RECEIVE_COMPLETE, msg).sendToTarget();
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_DEBOUNCE_TIMEOUT:
synchronized (this) {
if (mLinkState != LINK_STATE_DEBOUNCE) {
break;
}
if (mSendState == SEND_STATE_SENDING) {
EventLogTags.writeNfcShareFail(getMessageSize(mMessageToSend),
getMessageTnf(mMessageToSend), getMessageType(mMessageToSend),
getMessageAarPresent(mMessageToSend));
}
if (DBG) Log.d(TAG, "Debounce timeout");
mLinkState = LINK_STATE_DOWN;
mSendState = SEND_STATE_NOTHING_TO_SEND;
mMessageToSend = null;
if (DBG) Log.d(TAG, "onP2pOutOfRange()");
mEventListener.onP2pOutOfRange();
}
break;
case MSG_RECEIVE_COMPLETE:
NdefMessage m = (NdefMessage) msg.obj;
synchronized (this) {
if (mLinkState == LINK_STATE_DOWN) {
break;
}
if (mSendState == SEND_STATE_SENDING) {
cancelSendNdefMessage();
}
mSendState = SEND_STATE_NOTHING_TO_SEND;
if (DBG) Log.d(TAG, "onP2pReceiveComplete()");
mEventListener.onP2pReceiveComplete();
NfcService.getInstance().sendMockNdefTag(m);
}
break;
case MSG_SEND_COMPLETE:
synchronized (P2pLinkManager.this) {
mSendTask = null;
if (mLinkState == LINK_STATE_DOWN || mSendState != SEND_STATE_SENDING) {
break;
}
mSendState = SEND_STATE_NOTHING_TO_SEND;
if (DBG) Log.d(TAG, "onP2pSendComplete()");
mEventListener.onP2pSendComplete();
if (mCallbackNdef != null) {
try {
mCallbackNdef.onNdefPushComplete();
} catch (RemoteException e) { }
}
mSendTask = null;
}
break;
}
return true;
}
int getMessageSize(NdefMessage msg) {
if (msg != null) {
return msg.toByteArray().length;
} else {
return 0;
}
}
int getMessageTnf(NdefMessage msg) {
if (msg == null) {
return NdefRecord.TNF_EMPTY;
}
NdefRecord records[] = msg.getRecords();
if (records == null || records.length == 0) {
return NdefRecord.TNF_EMPTY;
}
return records[0].getTnf();
}
String getMessageType(NdefMessage msg) {
if (msg == null) {
return "null";
}
NdefRecord records[] = msg.getRecords();
if (records == null || records.length == 0) {
return "null";
}
NdefRecord record = records[0];
switch (record.getTnf()) {
case NdefRecord.TNF_ABSOLUTE_URI:
// The actual URI is in the type field, don't log it
return "uri";
case NdefRecord.TNF_EXTERNAL_TYPE:
case NdefRecord.TNF_MIME_MEDIA:
case NdefRecord.TNF_WELL_KNOWN:
return new String(record.getType(), Charsets.UTF_8);
default:
return "unknown";
}
}
int getMessageAarPresent(NdefMessage msg) {
if (msg == null) {
return 0;
}
NdefRecord records[] = msg.getRecords();
if (records == null) {
return 0;
}
for (NdefRecord record : records) {
if (record.getTnf() == NdefRecord.TNF_EXTERNAL_TYPE &&
Arrays.equals(NdefRecord.RTD_ANDROID_APP, record.getType())) {
return 1;
}
}
return 0;
}
@Override
public void onP2pSendConfirmed() {
if (DBG) Log.d(TAG, "onP2pSendConfirmed()");
synchronized (this) {
if (mLinkState == LINK_STATE_DOWN || mSendState != SEND_STATE_NEED_CONFIRMATION) {
return;
}
mSendState = SEND_STATE_SENDING;
if (mLinkState == LINK_STATE_UP) {
sendNdefMessage();
}
}
}
static String sendStateToString(int state) {
switch (state) {
case SEND_STATE_NOTHING_TO_SEND:
return "SEND_STATE_NOTHING_TO_SEND";
case SEND_STATE_NEED_CONFIRMATION:
return "SEND_STATE_NEED_CONFIRMATION";
case SEND_STATE_SENDING:
return "SEND_STATE_SENDING";
default:
return "<error>";
}
}
static String linkStateToString(int state) {
switch (state) {
case LINK_STATE_DOWN:
return "LINK_STATE_DOWN";
case LINK_STATE_DEBOUNCE:
return "LINK_STATE_DEBOUNCE";
case LINK_STATE_UP:
return "LINK_STATE_UP";
default:
return "<error>";
}
}
void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
synchronized (this) {
pw.println("mIsSendEnabled=" + mIsSendEnabled);
pw.println("mIsReceiveEnabled=" + mIsReceiveEnabled);
pw.println("mLinkState=" + linkStateToString(mLinkState));
pw.println("mSendState=" + sendStateToString(mSendState));
pw.println("mStaticNdef=" + mStaticNdef);
pw.println("mCallbackNdef=" + mCallbackNdef);
pw.println("mMessageToSend=" + mMessageToSend);
}
}
}
|