forked from phonegap/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikitudeCamera.java
More file actions
178 lines (154 loc) · 5.12 KB
/
WikitudeCamera.java
File metadata and controls
178 lines (154 loc) · 5.12 KB
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
/**
* Phonegap Wikitude AR camera view plugin
* Copyright (c) Spletart 2011
*
*/
package com.spletart.mobile;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openintents.intents.WikitudeARIntent;
import org.openintents.intents.WikitudePOI;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
/**
* This calls out to the Wikitude SDK and returns the result.
*/
public class WikitudeCamera extends Plugin {
public static final String ACTION = "show";
public static final int REQUEST_CODE = 0x0ba7c0de;
public String callback;
public static final String defaultInstallTitle = "Install Wikitude Browser?";
public static final String defaultInstallMessage = "This requires the free Wikitude Browser app. Would you like to install it now?";
public static final String defaultYesString = "Yes";
public static final String defaultNoString = "No";
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
this.callback = callbackId;
Log.d("WikitudeARCamera Plugin", "Plugin Called");
PluginResult result = null;
// extract data and options...
JSONArray data;
JSONObject options;
try {
data = args.getJSONArray(0);
options = args.getJSONObject(1);
} catch (JSONException e1) {
e1.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
if (ACTION.equals(action)) {
// prepare pois list
List<WikitudePOI> pois = new ArrayList<WikitudePOI>();
for (int i = 0; i < data.length(); i++) {
try {
JSONObject obj = data.getJSONObject(i);
Double latitude = obj.getDouble("latitude");
Double longitude = obj.getDouble("longitude");
WikitudePOI poi = new WikitudePOI(latitude, longitude);
poi.setName(obj.getString("name"));
poi.setDescription(obj.getString("description"));
pois.add(poi);
} catch (JSONException e) {
e.printStackTrace();
// Go on...
}
}
// add POIs to AR intent
WikitudeARIntent intent = new WikitudeARIntent(
this.ctx.getActivity().getApplication(), null, null);
intent.addPOIs(pois);
intent.addTitleText(extract(options, "title"));
try {
this.ctx.startActivityForResult((Plugin) this, intent,
REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// Extract installation dialog strings...
String installTitle = defaultInstallTitle;
if(options.length() > 1) {
installTitle = extract(options, "installTitle");
}
String installMessage = defaultInstallMessage;
if(options.length() > 2) {
installMessage = extract(options, "installMessage");
}
String yesString = defaultYesString;
if(options.length() > 3) {
yesString = extract(options, "yesString");
}
String noString = defaultNoString;
if(options.length() > 4) {
noString = extract(options, "noString");
}
// Installation dialog
showDownloadDialog(installTitle, installMessage, yesString, noString);
}
return new PluginResult(PluginResult.Status.NO_RESULT);
}
return null;
}
private String extract(JSONObject options, String key) {
try {
return options.getString(key);
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
this.success(
new PluginResult(PluginResult.Status.OK, contents),
this.callback);
} else {
this.error(new PluginResult(PluginResult.Status.ERROR),
this.callback);
}
}
}
private void showDownloadDialog(final String title, final String message, final String yesString, final String noString) {
final Context context = this.ctx.getContext();
final Activity activity = this.ctx.getActivity();
Runnable runnable = new Runnable() {
public void run() {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setPositiveButton(yesString, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int i) {
dlg.dismiss();
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:com.wikitude")
);
try {
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
// We don't have the market app installed.
e.printStackTrace();
}
}
});
dialog.setNegativeButton(noString, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int i) {
dlg.dismiss();
}
});
dialog.create();
dialog.show();
}
};
activity.runOnUiThread(runnable);
}
}