Permalink
Browse files

Merge pull request #578 from obrand/strings

Added resources plugin and doc
  • Loading branch information...
2 parents 938fd9d + 51bc47e commit 4273a701222ca4426bddff890ed0b5b4e5782314 @macdonst macdonst committed May 25, 2012
Showing with 172 additions and 0 deletions.
  1. +45 −0 Android/Resources/README.md
  2. +34 −0 Android/Resources/Resources.js
  3. +93 −0 Android/Resources/ResourcesPlugin.java
@@ -0,0 +1,45 @@
+# Resources plugin for Phonegap (Android) #
+By Olivier Brand
+
+## Adding the Plugin to your project ##
+1. To install the plugin, move `Resources.js` to your project's www folder and include a reference to it
+in your html files.
+
+ <script src="Resources.js"></script>
+
+2. Create a folder called 'com/phonegap/plugin/resources' within your project's src folder.
+3. And copy the java file into that new folder.
+
+<pre>
+ mkdir -p <your_project>/src/com/phonegap/plugin/resources
+ cp ./ResourcesPlugin.java <your_project>/src/com/phonegap/plugin/resources
+</pre>
+
+4. Add a plugin line to `res/xml/plugins.xml`
+
+ &lt;plugin name="Resources" value="com.phonegap.plugin.resources.ResourcesPlugin" /&gt;
+
+## Using the plugin ##
+
+<pre>
+
+res/values/strings.xml
+
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="login">mylogin</string>
+ <string name="base_uri">/someuri</string>
+</resources>
+
+
+
+this.resources = window.plugins.resources.getStringResources({
+ 'resources': [
+ 'base_uri', 'login'
+ ],
+ 'package': 'com.androidapp.app'
+ });
+
+console.log(this.resources.base_uri);
+
+</pre>
@@ -0,0 +1,34 @@
+/*
+ * Resources
+ * Implements the javascript access to the phonegap plugin for retrieving specific resource information
+ * @author Olivier Brand
+ */
+
+/**
+ * @return the Resources class instance
+ */
+var Resources = function() {
+};
+
+/**
+ * Returns the named resources defined in R.strings Note the last parameter,
+ * this method is synchronous
+ *
+ * @param successCallback
+ * The callback which will be called when directory listing is
+ * successful
+ * @param failureCallback
+ * The callback which will be called when directory listing encouters
+ * an error
+ */
+Resources.prototype.getStringResources = function(params, successCallback,
+ failureCallback) {
+ return PhoneGap.exec(successCallback, failureCallback, 'Resources',
+ 'getStringResources', [ params ], false);
+};
+
+
+
+PhoneGap.addConstructor(function() {
+ PhoneGap.addPlugin('resources', new Resources());
+});
@@ -0,0 +1,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") : null;
+ }
+
+ 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;
+ }
+
+
+}

0 comments on commit 4273a70

Please sign in to comment.