Skip to content

Commit 2c10311

Browse files
author
Martijn Coenen
committed
NFC API documentation.
Added docs for beam Uri push and added some sanity checks on params. Change-Id: I7c43b71c8a9ca4f742933d2d5b9473e0e2451dd3
1 parent 2411c33 commit 2c10311

File tree

2 files changed

+149
-6
lines changed

2 files changed

+149
-6
lines changed

core/java/android/nfc/NfcActivityManager.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,23 @@ public Uri[] getUris() {
299299
callback = state.uriCallback;
300300
}
301301
if (callback != null) {
302-
return callback.createBeamUris(mDefaultEvent);
302+
uris = callback.createBeamUris(mDefaultEvent);
303+
if (uris != null) {
304+
for (Uri uri : uris) {
305+
if (uri == null) {
306+
Log.e(TAG, "Uri not allowed to be null.");
307+
return null;
308+
}
309+
String scheme = uri.getScheme();
310+
if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
311+
!scheme.equalsIgnoreCase("content"))) {
312+
Log.e(TAG, "Uri needs to have " +
313+
"either scheme file or scheme content");
314+
return null;
315+
}
316+
}
317+
}
318+
return uris;
303319
} else {
304320
return uris;
305321
}

core/java/android/nfc/NfcAdapter.java

Lines changed: 132 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,17 +584,138 @@ public boolean disable() {
584584
}
585585
}
586586

587-
//TODO: make sure NFC service has permission for URI
588-
//TODO: see if we will eventually support multiple URIs
589-
//TODO: javadoc
587+
/**
588+
* Set one or more {@link Uri}s to send using Android Beam (TM). Every
589+
* Uri you provide must have either scheme 'file' or scheme 'content'.
590+
*
591+
* <p>For the data provided through this method, Android Beam tries to
592+
* switch to alternate transports such as Bluetooth to achieve a fast
593+
* transfer speed. Hence this method is very suitable
594+
* for transferring large files such as pictures or songs.
595+
*
596+
* <p>The receiving side will store the content of each Uri in
597+
* a file and present a notification to the user to open the file
598+
* with a {@link android.content.Intent} with action
599+
* {@link android.content.Intent#ACTION_VIEW}.
600+
* If multiple URIs are sent, the {@link android.content.Intent} will refer
601+
* to the first of the stored files.
602+
*
603+
* <p>This method may be called at any time before {@link Activity#onDestroy},
604+
* but the URI(s) are only made available for Android Beam when the
605+
* specified activity(s) are in resumed (foreground) state. The recommended
606+
* approach is to call this method during your Activity's
607+
* {@link Activity#onCreate} - see sample
608+
* code below. This method does not immediately perform any I/O or blocking work,
609+
* so is safe to call on your main thread.
610+
*
611+
* <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
612+
* have priority over both {@link #setNdefPushMessage} and
613+
* {@link #setNdefPushMessageCallback}.
614+
*
615+
* <p>If {@link #setBeamPushUris} is called with a null Uri array,
616+
* and/or {@link #setBeamPushUrisCallback} is called with a null callback,
617+
* then the Uri push will be completely disabled for the specified activity(s).
618+
*
619+
* <p>Code example:
620+
* <pre>
621+
* protected void onCreate(Bundle savedInstanceState) {
622+
* super.onCreate(savedInstanceState);
623+
* NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
624+
* if (nfcAdapter == null) return; // NFC not available on this device
625+
* nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
626+
* }
627+
* </pre>
628+
* And that is it. Only one call per activity is necessary. The Android
629+
* OS will automatically release its references to the Uri(s) and the
630+
* Activity object when it is destroyed if you follow this pattern.
631+
*
632+
* <p>If your Activity wants to dynamically supply Uri(s),
633+
* then set a callback using {@link #setBeamPushUrisCallback} instead
634+
* of using this method.
635+
*
636+
* <p class="note">Do not pass in an Activity that has already been through
637+
* {@link Activity#onDestroy}. This is guaranteed if you call this API
638+
* during {@link Activity#onCreate}.
639+
*
640+
* <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
641+
*
642+
* @param uris an array of Uri(s) to push over Android Beam
643+
* @param activity activity for which the Uri(s) will be pushed
644+
*/
590645
public void setBeamPushUris(Uri[] uris, Activity activity) {
591646
if (activity == null) {
592647
throw new NullPointerException("activity cannot be null");
593648
}
649+
if (uris != null) {
650+
for (Uri uri : uris) {
651+
if (uri == null) throw new NullPointerException("Uri not " +
652+
"allowed to be null");
653+
String scheme = uri.getScheme();
654+
if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
655+
!scheme.equalsIgnoreCase("content"))) {
656+
throw new IllegalArgumentException("URI needs to have " +
657+
"either scheme file or scheme content");
658+
}
659+
}
660+
}
594661
mNfcActivityManager.setNdefPushContentUri(activity, uris);
595662
}
596663

597-
// TODO javadoc
664+
/**
665+
* Set a callback that will dynamically generate one or more {@link Uri}s
666+
* to send using Android Beam (TM). Every Uri the callback provides
667+
* must have either scheme 'file' or scheme 'content'.
668+
*
669+
* <p>For the data provided through this callback, Android Beam tries to
670+
* switch to alternate transports such as Bluetooth to achieve a fast
671+
* transfer speed. Hence this method is very suitable
672+
* for transferring large files such as pictures or songs.
673+
*
674+
* <p>The receiving side will store the content of each Uri in
675+
* a file and present a notification to the user to open the file
676+
* with a {@link android.content.Intent} with action
677+
* {@link android.content.Intent#ACTION_VIEW}.
678+
* If multiple URIs are sent, the {@link android.content.Intent} will refer
679+
* to the first of the stored files.
680+
*
681+
* <p>This method may be called at any time before {@link Activity#onDestroy},
682+
* but the URI(s) are only made available for Android Beam when the
683+
* specified activity(s) are in resumed (foreground) state. The recommended
684+
* approach is to call this method during your Activity's
685+
* {@link Activity#onCreate} - see sample
686+
* code below. This method does not immediately perform any I/O or blocking work,
687+
* so is safe to call on your main thread.
688+
*
689+
* <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
690+
* have priority over both {@link #setNdefPushMessage} and
691+
* {@link #setNdefPushMessageCallback}.
692+
*
693+
* <p>If {@link #setBeamPushUris} is called with a null Uri array,
694+
* and/or {@link #setBeamPushUrisCallback} is called with a null callback,
695+
* then the Uri push will be completely disabled for the specified activity(s).
696+
*
697+
* <p>Code example:
698+
* <pre>
699+
* protected void onCreate(Bundle savedInstanceState) {
700+
* super.onCreate(savedInstanceState);
701+
* NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
702+
* if (nfcAdapter == null) return; // NFC not available on this device
703+
* nfcAdapter.setBeamPushUrisCallback(callback, this);
704+
* }
705+
* </pre>
706+
* And that is it. Only one call per activity is necessary. The Android
707+
* OS will automatically release its references to the Uri(s) and the
708+
* Activity object when it is destroyed if you follow this pattern.
709+
*
710+
* <p class="note">Do not pass in an Activity that has already been through
711+
* {@link Activity#onDestroy}. This is guaranteed if you call this API
712+
* during {@link Activity#onCreate}.
713+
*
714+
* <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
715+
*
716+
* @param callback callback, or null to disable
717+
* @param activity activity for which the Uri(s) will be pushed
718+
*/
598719
public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
599720
if (activity == null) {
600721
throw new NullPointerException("activity cannot be null");
@@ -663,6 +784,10 @@ public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity ac
663784
* {@link Activity#onDestroy}. This is guaranteed if you call this API
664785
* during {@link Activity#onCreate}.
665786
*
787+
* <p class="note">For sending large content such as pictures and songs,
788+
* consider using {@link #setBeamPushUris}, which switches to alternate transports
789+
* such as Bluetooth to achieve a fast transfer rate.
790+
*
666791
* <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
667792
*
668793
* @param message NDEF message to push over NFC, or null to disable
@@ -753,7 +878,9 @@ public void setNdefPushMessage(NdefMessage message, Activity activity,
753878
* <p class="note">Do not pass in an Activity that has already been through
754879
* {@link Activity#onDestroy}. This is guaranteed if you call this API
755880
* during {@link Activity#onCreate}.
756-
*
881+
* <p class="note">For sending large content such as pictures and songs,
882+
* consider using {@link #setBeamPushUris}, which switches to alternate transports
883+
* such as Bluetooth to achieve a fast transfer rate.
757884
* <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
758885
*
759886
* @param callback callback, or null to disable

0 commit comments

Comments
 (0)