This commit is contained in:
filmaj
2010-03-04 15:11:49 -08:00
34 changed files with 2972 additions and 97 deletions
+1 -1
View File
@@ -38,6 +38,6 @@
<action android:name="android.intent.action.PICK" />
</activity>
</application>
<uses-sdk android:minSdkVersion="5" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
+33
View File
@@ -0,0 +1,33 @@
var Crypto = function()
{
}
Crypto.prototype.encrypt = function(seed, string, callback)
{
GapCrypto.encrypt(seed, string);
this.encryptWin = callback;
}
Crypto.prototype.decrypt = function(seed, string, callback)
{
GapCrypto.decrypt(seed, string);
this.decryptWin = callback;
}
Crypto.prototype.gotCryptedString = function(string)
{
this.encryptWin(string);
}
Crypto.prototype.getPlainString = function(string)
{
this.decryptWin(string);
}
PhoneGap.addConstructor(function() {
if (typeof navigator.Crypto == "undefined")
{
navigator.Crypto = new Crypto();
}
});
+200 -83
View File
@@ -1,109 +1,226 @@
/**
* This class provides generic read and write access to the mobile device file system.
*/
function File() {
/**
* The data of a file.
*/
this.data = "";
/**
* The name of the file.
*/
this.name = "";
}
PhoneGap.addConstructor(function() { if (typeof navigator.fileMgr == "undefined") navigator.fileMgr = new FileMgr();});
/**
* Reads a file from the mobile device. This function is asyncronous.
* @param {String} fileName The name (including the path) to the file on the mobile device.
* The file name will likely be device dependent.
* @param {Function} successCallback The function to call when the file is successfully read.
* @param {Function} errorCallback The function to call when there is an error reading the file from the device.
* This class provides iPhone read and write access to the mobile device file system.
* Based loosely on http://www.w3.org/TR/2009/WD-FileAPI-20091117/#dfn-empty
*/
File.prototype.read = function(fileName, successCallback, errorCallback) {
function FileMgr()
{
this.fileWriters = {}; // empty maps
this.fileReaders = {};
this.docsFolderPath = "../../Documents";
this.tempFolderPath = "../../tmp";
this.freeDiskSpace = -1;
this.getFileBasePaths();
}
/**
* Writes a file to the mobile device.
* @param {File} file The file to write to the device.
// private, called from Native Code
FileMgr.prototype._setPaths = function(docs,temp)
{
this.docsFolderPath = docs;
this.tempFolderPath = temp;
}
// private, called from Native Code
FileMgr.prototype._setFreeDiskSpace = function(val)
{
this.freeDiskSpace = val;
}
// FileWriters add/remove
// called internally by writers
FileMgr.prototype.addFileWriter = function(filePath,fileWriter)
{
this.fileWriters[filePath] = fileWriter;
}
FileMgr.prototype.removeFileWriter = function(filePath)
{
this.fileWriters[filePath] = null;
}
// File readers add/remove
// called internally by readers
FileMgr.prototype.addFileReader = function(filePath,fileReader)
{
this.fileReaders[filePath] = fileReader;
}
FileMgr.prototype.removeFileReader = function(filePath)
{
this.fileReaders[filePath] = null;
}
/*******************************************
*
* private reader callback delegation
* called from native code
*/
File.prototype.write = function(file) {
}
PhoneGap.addConstructor(function() {
if (typeof navigator.file == "undefined") navigator.file = new File();
});
File.prototype.read = function(fileName, successCallback, errorCallback) {
this.failCallback = errorCallback;
this.winCallback = successCallback;
return FileUtil.read(fileName);
}
File.prototype.hasRead = function(data)
FileMgr.prototype.reader_onloadstart = function(filePath,result)
{
if(data.substr("FAIL"))
this.failCallback(data);
else
this.winCallback(data);
this.fileReaders[filePath].onloadstart(result);
}
/**
* Writes a file to the mobile device.
* @param {File} file The file to write to the device.
*/
File.prototype.write = function(file, str, mode, successCallback, failCallback) {
this.winCallback = successCallback;
this.failCallback = failCallback;
var call = FileUtil.write(file, str, mode);
}
File.prototype.testFileExists = function(file, successCallback, failCallback)
FileMgr.prototype.reader_onprogress = function(filePath,result)
{
var exists = FileUtil.testFileExists(file);
if(exists)
successCallback();
else
failCallback();
return exists;
this.fileReaders[filePath].onprogress(result);
}
File.prototype.testDirectoryExists = function(file, successCallback, failCallback)
FileMgr.prototype.reader_onload = function(filePath,result)
{
var exists = FileUtil.testDirectoryExists(file);
if(exists)
successCallback();
else
failCallback();
return exists;
this.fileReaders[filePath].result = unescape(result);
this.fileReaders[filePath].onload(this.fileReaders[filePath].result);
}
File.prototype.createDirectory = function(dir, successCallback, failCallback)
FileMgr.prototype.reader_onerror = function(filePath,err)
{
var good = FileUtils.createDirectory(dir);
good ? successCallback() : failCallback();
this.fileReaders[filePath].result = err;
this.fileReaders[filePath].onerror(err);
}
File.prototype.deleteDirectory = function(dir, successCallback, failCallback)
FileMgr.prototype.reader_onloadend = function(filePath,result)
{
var good = FileUtils.deleteDirectory(dir);
good ? successCallback() : failCallback();
this.fileReaders[filePath].onloadend(result);
}
File.prototype.deleteFile = function(dir, successCallback, failCallback)
/*******************************************
*
* private writer callback delegation
* called from native code
*/
FileMgr.prototype.writer_onerror = function(filePath,err)
{
var good = FileUtils.deleteFile(dir);
good ? successCallback() : failCallback();
this.fileWriters[filePath].onerror(err);
}
File.prototype.getFreeDiskSpace = function(successCallback, failCallback)
FileMgr.prototype.writer_oncomplete = function(filePath,result)
{
var diskSpace = FileUtils.getFreeDiskSpace();
if(diskSpace > 0)
successCallback();
else
failCallback();
return diskSpace;
this.fileWriters[filePath].oncomplete(result); // result contains bytes written
}
FileMgr.prototype.getFileBasePaths = function()
{
//PhoneGap.exec("File.getFileBasePaths");
}
FileMgr.prototype.testFileExists = function(fileName, successCallback, errorCallback)
{
var test = FileUtil.testFileExists(fileName);
test ? successCallback() : errorCallback();
}
FileMgr.prototype.testDirectoryExists = function(dirName, successCallback, errorCallback)
{
this.successCallback = successCallback;
this.errorCallback = errorCallback;
var test = FileUtil.testDirectoryExists(dirName);
test ? successCallback() : errorCallback();
}
FileMgr.prototype.createDirectory = function(dirName, successCallback, errorCallback)
{
this.successCallback = successCallback;
this.errorCallback = errorCallback;
var test = FileUtils.createDirectory(dirName);
test ? successCallback() : errorCallback();
}
FileMgr.prototype.deleteDirectory = function(dirName, successCallback, errorCallback)
{
this.successCallback = successCallback;
this.errorCallback = errorCallback;
var test = FileUtils.deleteDirectory(dirName);
test ? successCallback() : errorCallback();
}
FileMgr.prototype.deleteFile = function(fileName, successCallback, errorCallback)
{
this.successCallback = successCallback;
this.errorCallback = errorCallback;
FileUtils.deleteFile(fileName);
test ? successCallback() : errorCallback();
}
FileMgr.prototype.getFreeDiskSpace = function(successCallback, errorCallback)
{
if(this.freeDiskSpace > 0)
{
return this.freeDiskSpace;
}
else
{
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.freeDiskSpace = FileUtils.getFreeDiskSpace();
(this.freeDiskSpace > 0) ? successCallback() : errorCallback();
}
}
// File Reader
function FileReader()
{
this.fileName = "";
this.result = null;
this.onloadstart = null;
this.onprogress = null;
this.onload = null;
this.onerror = null;
this.onloadend = null;
}
FileReader.prototype.abort = function()
{
// Not Implemented
}
FileReader.prototype.readAsText = function(file)
{
if(this.fileName && this.fileName.length > 0)
{
navigator.fileMgr.removeFileReader(this.fileName,this);
}
this.fileName = file;
navigator.fileMgr.addFileReader(this.fileName,this);
return FileUtil.read(fileName);
}
// File Writer
function FileWriter()
{
this.fileName = "";
this.result = null;
this.readyState = 0; // EMPTY
this.result = null;
this.onerror = null;
this.oncomplete = null;
}
FileWriter.prototype.writeAsText = function(file,text,bAppend)
{
if(this.fileName && this.fileName.length > 0)
{
navigator.fileMgr.removeFileWriter(this.fileName,this);
}
this.fileName = file;
if(bAppend != true)
{
bAppend = false; // for null values
}
navigator.fileMgr.addFileWriter(file,this);
this.readyState = 0; // EMPTY
var call = FileUtil.write(file, text, bAppend);
this.result = null;
}
@@ -0,0 +1,37 @@
package com.phonegap;
import android.webkit.WebView;
public class CryptoHandler {
WebView mView;
CryptoHandler(WebView view)
{
mView = view;
}
public void encrypt(String pass, String text)
{
try {
String encrypted = SimpleCrypto.encrypt(pass,text);
mView.loadUrl("javascript:Crypto.gotCryptedString('" + text + "')");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void decrypt(String pass, String text)
{
try {
String decrypted = SimpleCrypto.decrypt(pass,text);
mView.loadUrl("javascript:Crypto.gotPlainString('" + text + "')");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
+20 -7
View File
@@ -60,8 +60,7 @@ public class DroidGap extends Activity {
private NetworkManager netMan;
private CompassListener mCompass;
private Storage cupcakeStorage;
private CryptoHandler crypto;
/** Called when the activity is first created. */
@Override
@@ -87,9 +86,8 @@ public class DroidGap extends Activity {
appView.setLayoutParams(webviewParams);
WebViewReflect.checkCompatibility();
/* This changes the setWebChromeClient to log alerts to LogCat! Important for Javascript Debugging */
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR)
if (android.os.Build.VERSION.RELEASE.startsWith("2."))
appView.setWebChromeClient(new EclairClient(this));
else
{
@@ -110,6 +108,9 @@ public class DroidGap extends Activity {
WebViewReflect.setStorage(settings, true, "/data/data/" + appPackage + "/app_database/");
// Turn on DOM storage!
WebViewReflect.setDomStorage(settings);
/* Bind the appView object to the gap class methods */
bindBrowser(appView);
if(cupcakeStorage != null)
@@ -134,7 +135,8 @@ 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);
crypto = new CryptoHandler(appView);
// This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(gap, "DroidGap");
@@ -145,7 +147,10 @@ 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)
appView.addJavascriptInterface(crypto, "GapCrypto");
if (android.os.Build.VERSION.RELEASE.startsWith("1."))
{
cupcakeStorage = new Storage(appView);
appView.addJavascriptInterface(cupcakeStorage, "droidStorage");
@@ -214,6 +219,7 @@ public class DroidGap extends Activity {
public final class EclairClient extends GapClient
{
private String TAG = "PhoneGapLog";
private long MAX_QUOTA = 2000000;
public EclairClient(Context ctx) {
@@ -237,6 +243,13 @@ public class DroidGap extends Activity {
quotaUpdater.updateQuota(currentQuota);
}
}
// This is a test of console.log, because we don't have this in Android 2.01
public void addMessageToConsole(String message, int lineNumber, String sourceID)
{
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
}
}
+5 -3
View File
@@ -97,7 +97,7 @@ public class FileUtils {
data = "FAIL: IO ERROR";
}
mView.loadUrl("javascript:navigator.file.hasRead('" + data + "')");
//mView.loadUrl("javascript:navigator.FileReader.hasRead('" + data + "')");
return data;
}
@@ -113,9 +113,11 @@ public class FileUtils {
out.write(buff, 0, rawData.length);
out.flush();
out.close();
mView.loadUrl("javascript:navigator.file.winCallback('File written')");
//mView.loadUrl("javascript:navigator.FileReader.onsuccess('File written')");
} catch (Exception e) {
mView.loadUrl("javascript:navigator.file.failCallback('Fail')");
//mView.loadUrl("javascript:navigator.FileReader.onerror('Fail')");
// So, do we just return -1 at this point!
return -1;
}
return 0;
}
@@ -32,10 +32,9 @@ public class NetworkManager {
public boolean isWifiActive()
{
NetworkInfo info = sockMan.getActiveNetworkInfo();
String type = "";
if (info!=null)
if(info != null)
{
type = info.getTypeName();
String type = info.getTypeName();
return type.equals("WIFI");
}
return false;
@@ -0,0 +1,96 @@
/*
* Code originally found on Android Snippets
* Contributed to snippets Ferenc Hechler
* Copyright (c) 2009 Ferenc Hechler
*/
package com.phonegap;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class SimpleCrypto {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte [] rawKey = getRawKey(seed.getBytes());
byte [] enc = toByte(encrypted);
byte [] result = decrypt(rawKey, enc);
return new String(result);
}
public static byte[] getRawKey(byte [] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt)
{
return toHex(txt.getBytes());
}
public static String fromHex(String hex)
{
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; ++i)
{
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
}
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
{
return "";
}
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++)
{
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "01234567890ABCDEF";
private static void appendHex(StringBuffer sb, byte b)
{
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
@@ -9,6 +9,8 @@ import android.webkit.WebSettings;
public class WebViewReflect {
private static Method mWebSettings_setDatabaseEnabled;
private static Method mWebSettings_setDatabasePath;
private static Method mWebSettings_setDomStorageEnabled;
static
{
checkCompatibility();
@@ -37,12 +39,15 @@ public class WebViewReflect {
}
}
public static void checkCompatibility() {
try {
mWebSettings_setDatabaseEnabled = WebSettings.class.getMethod(
"setDatabaseEnabled", new Class[] { boolean.class } );
mWebSettings_setDatabasePath = WebSettings.class.getMethod(
"setDatabasePath", new Class[] { String.class });
mWebSettings_setDomStorageEnabled = WebSettings.class.getMethod(
"setDomStorageEnabled", new Class[] { boolean.class });
/* success, this is a newer device */
} catch (NoSuchMethodException nsme) {
/* failure, must be older device */
@@ -72,4 +77,31 @@ public class WebViewReflect {
System.out.println("dump not supported");
}
}
public static void setDomStorage(WebSettings setting)
{
if(mWebSettings_setDomStorageEnabled != null)
{
/* feature is supported */
try {
mWebSettings_setDomStorageEnabled.invoke(setting, true);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//setting.setDatabaseEnabled(enable);
//setting.setDatabasePath(path);
} else {
/* feature not supported, do something else */
System.out.println("dump not supported");
}
}
}