forked from phonegap/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourcesPlugin.java
More file actions
93 lines (73 loc) · 2.62 KB
/
Copy pathResourcesPlugin.java
File metadata and controls
93 lines (73 loc) · 2.62 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
package com.phonegap.plugin.resources;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ResourcesPlugin extends Plugin {
/*
* (non-Javadoc)
*
* @see com.phonegap.api.Plugin#isSynch(java.lang.String)
*/
@Override
public boolean isSynch(String action) {
if (action.equals("getStringResources")) {
return true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see com.phonegap.api.Plugin#execute(java.lang.String,
* org.json.JSONArray, java.lang.String)
*/
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult result = null;
if (action.equals("getStringResources")) {
JSONObject obj = null;
JSONArray resourceNames = null;
String pkg = null;
try {
obj = args.getJSONObject(0);
if (obj != null) {
resourceNames = obj.has("resources") ? obj.getJSONArray("resources") : null;
pkg = obj.has("package") ? obj.getString("package") : ctx.getPackageName();
}
if (resourceNames != null && resourceNames.length() > 0 && pkg != null) {
JSONObject JSONresult = new JSONObject();
JSONObject resources = new JSONObject();
for (int nbElem = 0; nbElem < resourceNames.length(); nbElem++) {
resources.put(resourceNames.getString(nbElem),
this.getStringResource(resourceNames.getString(nbElem), pkg));
}
JSONresult.put("resources", resources);
result = new PluginResult(Status.OK, JSONresult);
}
} catch (JSONException jsonEx) {
result = new PluginResult(Status.JSON_EXCEPTION);
}
}
return result;
}
/**
* Gets the string resource for a given package
*
* @param name
* the name
* @param packageName
* the package name
* @return the string resource
*/
private String getStringResource(String name, String packageName) {
String resource = null;
int id = ctx.getResources().getIdentifier(name, "string", packageName);
if (id != 0) {
resource = ctx.getContext().getString(id);
}
return resource;
}
}