From b0a69f5cc5f45edb1b80100c12dcc0a4d2a3a271 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 8 Dec 2009 14:08:48 -0800 Subject: [PATCH] Adding Cupcake Storage --- framework/src/com/phonegap/DroidGap.java | 15 ++++- framework/src/com/phonegap/Storage.java | 71 ++++++++++++++++++++++ js/storage.js | 77 ++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 framework/src/com/phonegap/Storage.java create mode 100644 js/storage.js diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 7df77a8d..4dc9c1cf 100644 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -49,7 +49,6 @@ public class DroidGap extends Activity { protected WebView appView; private LinearLayout root; - private String uri; private PhoneGap gap; private GeoBroker geo; private AccelListener accel; @@ -58,7 +57,9 @@ public class DroidGap extends Activity { private FileUtils fs; private NetworkManager netMan; private CompassListener mCompass; - private WebViewReflect eclairCheck; + private Storage cupcakeStorage; + + /** Called when the activity is first created. */ @Override @@ -89,7 +90,10 @@ public class DroidGap extends Activity { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR) appView.setWebChromeClient(new EclairClient(this)); else + { appView.setWebChromeClient(new GapClient(this)); + cupcakeStorage = new Storage(appView); + } appView.setInitialScale(100); appView.setVerticalScrollBarEnabled(false); @@ -127,7 +131,7 @@ public class DroidGap extends Activity { mContacts = new ContactManager(this, appView); fs = new FileUtils(appView); netMan = new NetworkManager(this, appView); - mCompass = new CompassListener(this, appView); + mCompass = new CompassListener(this, appView); // This creates the new javascript interfaces for PhoneGap appView.addJavascriptInterface(gap, "DroidGap"); @@ -138,6 +142,11 @@ public class DroidGap extends Activity { appView.addJavascriptInterface(fs, "FileUtil"); appView.addJavascriptInterface(netMan, "NetworkManager"); appView.addJavascriptInterface(mCompass, "CompassHook"); + if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.DONUT) + { + cupcakeStorage = new Storage(appView); + appView.addJavascriptInterface(cupcakeStorage, "droidStorage"); + } } diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java new file mode 100644 index 00000000..71d55600 --- /dev/null +++ b/framework/src/com/phonegap/Storage.java @@ -0,0 +1,71 @@ +package com.phonegap; + +import android.database.Cursor; +import android.database.sqlite.*; +import android.util.Log; +import android.webkit.WebView; + +public class Storage { + + private static final String LOG_TAG = "SQLite Storage:"; + SQLiteDatabase myDb; + String path; + String txid = ""; + WebView appView; + + Storage(WebView view) + { + Package pack = this.getClass().getPackage(); + String appPackage = pack.getName(); + path = "/data/data/" + appPackage + "/databases/"; + appView = view; + } + + public void openDatabase(String db, String version, String display_name, long size) + { + path += db + ".db"; + myDb = SQLiteDatabase.openOrCreateDatabase(path, null); + } + + public void executeSql(String query, String[] params, String tx_id) + { + if(txid.length() == 0) + { + try{ + txid = tx_id; + Cursor myCursor = myDb.rawQuery(query, params); + processResults(myCursor); + } + catch (SQLiteException ex) + { + Log.d(LOG_TAG, ex.getMessage()); + } + } + } + + public void processResults(Cursor cur) + { + String key = ""; + String value = ""; + String resultString = ""; + if (cur.moveToFirst()) { + int colCount = cur.getColumnCount(); + do { + resultString = "{"; + for(int i = 0; i < colCount; ++i) + { + key = cur.getColumnName(i); + value = cur.getString(i); + resultString += " \"" + key + "\" : \"" + value + "\""; + if (i != (colCount - 1)) + resultString += ","; + } + resultString += "}"; + appView.loadUrl("javascript:droiddb.addResult('" + resultString + "')"); + } while (cur.moveToNext()); + appView.loadUrl("javascript:droiddb.completeQuery()"); + txid = ""; + } + } + +} \ No newline at end of file diff --git a/js/storage.js b/js/storage.js new file mode 100644 index 00000000..0650c465 --- /dev/null +++ b/js/storage.js @@ -0,0 +1,77 @@ +/* + * This is purely for the Android 1.5/1.6 HTML 5 Storage + * I was hoping that Android 2.0 would deprecate this, but given the fact that + * most manufacturers ship with Android 1.5 and do not do OTA Updates, this is required + */ + +var droiddb = new function() +{ + this.txQueue = []; +} + +droiddb.prototype.addResult(rawdata, tx_id) +{ + eval("var data = " + rawdata); + var tx = this.txQueue(tx_id); + tx.resultSet.push(data); +} + +droiddb.prototype.completeQuery(tx_id) +{ + var tx = this.txQueue(tx_id); + var r = new result(); + r.rows.resultSet = tx.resultSet; + r.rows.length = resultSet.length; + tx.win(r); +} + +var DatabaseShell = function() +{ + +} + +DatabaseShell.transaction(process) +{ + tx = new Tx(); + process(tx); +} + +var Tx = function() +{ + droiddb.txQueue.push(this); + this.id = droiddb.txQueue.length - 1; + this.resultSet = []; +} + +Tx.prototype.executeSql = function(query, params, win, fail) +{ + droidStorage.executeSql(query, params, tx_id); + tx.win = win; + tx.fail = fail; +} + +var result = function() +{ + this.rows = new Rows(); +} + +var Rows = function() +{ + this.resultSet = []; + this.length = 0; +} + +Rows.prototype.item = function(row_id) +{ + return this.resultSet[id]; +} + +PhoneGap.addConstructor(function() { + if (typeof navigator.openDatabase == "undefined") { + var openDatabase = function(name, version, display_name, size) + { + droidStorage.openDatabase(name, version, display_name, size) + db_object = new DatabaseShell(); + } + } +});