Extends CordovaPlugin to fix #8, minor cleanups

This commit is contained in:
Chris Brody 2012-12-16 15:46:02 +01:00
parent 2433bccf3c
commit 102d732102

View File

@ -13,8 +13,8 @@ import org.json.JSONObject;
import java.lang.Number; import java.lang.Number;
import org.apache.cordova.api.Plugin; import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult; import org.apache.cordova.api.CallbackContext;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -24,7 +24,8 @@ import java.util.HashMap;
import android.util.Log; import android.util.Log;
public class SQLitePlugin extends Plugin { public class SQLitePlugin extends CordovaPlugin
{
/** /**
* Multiple database map. * Multiple database map.
*/ */
@ -44,14 +45,13 @@ public class SQLitePlugin extends Plugin {
* The action to execute. * The action to execute.
* @param args * @param args
* JSONArry of arguments for the plugin. * JSONArry of arguments for the plugin.
* @param callbackId * @param cbc
* The callback id used when calling back into JavaScript. * Callback context from Cordova API (not used here)
* @return A PluginResult object with a status and message. *
*/ */
public PluginResult execute(String action, JSONArray args, String callbackId) { @Override
PluginResult.Status status = PluginResult.Status.OK; public boolean execute(String action, JSONArray args, CallbackContext cbc)
String result = ""; {
try { try {
if (action.equals("open")) { if (action.equals("open")) {
this.openDatabase(args.getString(0), "1", this.openDatabase(args.getString(0), "1",
@ -108,36 +108,29 @@ public class SQLitePlugin extends Plugin {
else else
Log.v("error", "null trans_id"); Log.v("error", "null trans_id");
} }
return new PluginResult(status, result);
return true;
} catch (JSONException e) { } catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION); // TODO: signal JSON problem to JS
return false;
} }
} }
/** /**
* Identifies if action to be executed returns a value and should be run * XXX TODO:
* synchronously.
*
* @param action
* The action to execute
* @return T=returns value
*/
public boolean isSynch(String action) {
return true;
}
/**
* Clean up and close database. * Clean up and close database.
*
*/ */
@Override //@Override
public void onDestroy() { //public void onDestroy() {
/** XXX TODO : /** XXX TODO :
if (this.myDb != null) { if (this.myDb != null) {
this.myDb.close(); this.myDb.close();
this.myDb = null; this.myDb = null;
} }
**/ **/
} //}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// LOCAL METHODS // LOCAL METHODS
@ -197,7 +190,7 @@ public class SQLitePlugin extends Plugin {
long insertId = myStatement.executeInsert(); long insertId = myStatement.executeInsert();
String result = "{'insertId':'" + insertId + "'}"; String result = "{'insertId':'" + insertId + "'}";
this.sendJavascript("SQLitePluginTransactionCB.queryCompleteCallback('" + this.sendJavascriptCB("window.SQLitePluginTransactionCB.queryCompleteCallback('" +
tx_id + "','" + query_id + "', " + result + ");"); tx_id + "','" + query_id + "', " + result + ");");
} else { } else {
String[] params = null; String[] params = null;
@ -226,16 +219,16 @@ public class SQLitePlugin extends Plugin {
catch (SQLiteException ex) { catch (SQLiteException ex) {
ex.printStackTrace(); ex.printStackTrace();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage()); Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
this.sendJavascript("SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');"); this.sendJavascriptCB("window.SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');");
} catch (JSONException ex) { } catch (JSONException ex) {
ex.printStackTrace(); ex.printStackTrace();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage()); Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
this.sendJavascript("SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');"); this.sendJavascriptCB("window.SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');");
} }
finally { finally {
myDb.endTransaction(); myDb.endTransaction();
Log.v("executeSqlBatch", tx_id); Log.v("executeSqlBatch", tx_id);
this.sendJavascript("SQLitePluginTransactionCB.txCompleteCallback('" + tx_id + "');"); this.sendJavascriptCB("window.SQLitePluginTransactionCB.txCompleteCallback('" + tx_id + "');");
} }
} }
@ -253,7 +246,7 @@ public class SQLitePlugin extends Plugin {
{ {
String result = this.results2string(cur); String result = this.results2string(cur);
this.sendJavascript("SQLitePluginTransactionCB.queryCompleteCallback('" + this.webView.sendJavascript("window.SQLitePluginTransactionCB.queryCompleteCallback('" +
tx_id + "','" + query_id + "', " + result + ");"); tx_id + "','" + query_id + "', " + result + ");");
} }
@ -269,7 +262,7 @@ public class SQLitePlugin extends Plugin {
{ {
String result = this.results2string(cur); String result = this.results2string(cur);
this.sendJavascript("SQLitePluginCallback.p1('" + id + "', " + result + ");"); this.webView.sendJavascript("window.SQLitePluginCallback.p1('" + id + "', " + result + ");");
} }
/** /**
@ -337,4 +330,16 @@ public class SQLitePlugin extends Plugin {
return result; return result;
} }
/**
* Send Javascript callback.
*
* @param cb
* Javascript callback command to send
*
*/
private void sendJavascriptCB(String cb)
{
this.webView.sendJavascript(cb);
}
} }