Adding Cupcake Storage

This commit is contained in:
Joe Bowser 2009-12-08 14:08:48 -08:00
parent f6e2e129b1
commit b0a69f5cc5
3 changed files with 160 additions and 3 deletions

View File

@ -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");
}
}

View File

@ -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 = "";
}
}
}

77
js/storage.js Normal file
View File

@ -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();
}
}
});