-
Notifications
You must be signed in to change notification settings - Fork 3
/
CreateNfcBeamUrisCallback.java
81 lines (67 loc) · 2.19 KB
/
CreateNfcBeamUrisCallback.java
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
package org.test;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.webkit.MimeTypeMap;
public class CreateNfcBeamUrisCallback implements NfcAdapter.CreateBeamUrisCallback {
boolean changed = false;
ArrayList<Uri> uris = new ArrayList<Uri>();
Activity context;
File currentApp;
/* Method that allows the python code to attach its Activity to this Class. */
public void addContext(Activity act){
context = act;
currentApp = new File(context.getPackageResourcePath());
uris.add(Uri.fromFile(currentApp));
System.out.println("Added APK Uri");
System.out.println((Uri.fromFile(currentApp)).toString());
}
/* Method that adds an Uri to the list of Files to be sent through Android Beam. */
public void addUris(String fileUri){
if (!changed) {
uris.remove(Uri.fromFile(currentApp));
changed = true;
}
System.out.println("Add Uri: " + fileUri);
uris.add(Uri.fromFile(new File(fileUri)));
}
public void removeUris(String fileUri){
System.out.println("Remove Uri: " + fileUri);
uris.remove(Uri.fromFile(new File(fileUri)));
if (uris.isEmpty()) {
uris.add(Uri.fromFile(currentApp));
System.out.println("Added APK Uri");
System.out.println((Uri.fromFile(currentApp)).toString());
changed = false;
}
}
public void clearUris(){
System.out.println("Clearing NFC stack.");
uris.clear();
uris.add(Uri.fromFile(currentApp));
System.out.println("Added APK Uri");
System.out.println((Uri.fromFile(currentApp)).toString());
}
public String getMimeType(String file){
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(file);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
/* Method that either sends the specified Files through Android Beam or the App itself, if no Files were specified. */
@Override
public Uri[] createBeamUris(NfcEvent event) {
Uri[] res = new Uri[uris.size()];
uris.toArray(res);
System.out.print("Sending: ");
for(int i = 0; i < res.length; i++) {
System.out.println(res[i].toString());
}
return res;
}
}