diff --git a/bin/create b/bin/create index 84122a54..e7670441 100755 --- a/bin/create +++ b/bin/create @@ -38,6 +38,11 @@ fi # update the cordova-android framework for the desired target android update project --target $TARGET --path ./framework +# Use curl to get the jar (TODO: Support Apache Mirrors) +curl -OL http://mirror.symnds.com/software/Apache//commons/codec/binaries/commons-codec-1.6-bin.zip +unzip commons-codec-1.6-bin.zip +cp commons-codec-1.6/commons-codec-1.6.jar ./framework/libs + # compile cordova.js and cordova.jar cd ./framework && ant jar && cd ../ diff --git a/bin/create.bat b/bin/create.bat index b9c9392b..7182d21e 100644 --- a/bin/create.bat +++ b/bin/create.bat @@ -1 +1 @@ -cscript create.js \ No newline at end of file +cscript bin\create.js %* \ No newline at end of file diff --git a/bin/create.js b/bin/create.js index 24e70325..a15db7a2 100644 --- a/bin/create.js +++ b/bin/create.js @@ -25,8 +25,9 @@ */ function read(filename) { + WScript.Echo('Reading in ' + filename); var fso=WScript.CreateObject("Scripting.FileSystemObject"); - var f=fso.OpenTextFile(filename, 1, true); + var f=fso.OpenTextFile(filename, 1); var s=f.ReadAll(); f.Close(); return s; @@ -40,13 +41,32 @@ function write(filename, contents) { function replaceInFile(filename, regexp, replacement) { write(filename, read(filename).replace(regexp, replacement)); } -function exec(s) { +function exec(s, output) { + WScript.Echo('Executing ' + s); var o=shell.Exec(s); + while (o.Status == 0) { + WScript.Sleep(100); + } + WScript.Echo("Command exited with code " + o.Status); +} + +function fork(s) { + WScript.Echo('Executing ' + s); + var o=shell.Exec(s); + while (o.Status != 1) { + WScript.Sleep(100); + } + WScript.Echo(o.StdOut.ReadAll()); + WScript.Echo(o.StdErr.ReadAll()); + WScript.Echo("Command exited with code " + o.Status); } var args = WScript.Arguments, PROJECT_PATH="example", PACKAGE="org.apache.cordova.example", ACTIVITY="cordovaExample", shell=WScript.CreateObject("WScript.Shell"); + +// working dir +var ROOT = WScript.ScriptFullName.split('\\bin\\create.js').join(''); if (args.Count() == 3) { WScript.Echo('Found expected arguments'); @@ -61,7 +81,15 @@ var MANIFEST_PATH=PROJECT_PATH+'\\AndroidManifest.xml'; var TARGET=shell.Exec('android.bat list targets').StdOut.ReadAll().match(/id:\s([0-9]).*/)[1]; var VERSION=read('VERSION').replace(/\r\n/,'').replace(/\n/,''); -// clobber any existing example +WScript.Echo("Project path: " + PROJECT_PATH); +WScript.Echo("Package: " + PACKAGE); +WScript.Echo("Activity: " + ACTIVITY); +WScript.Echo("Package as path: " + PACKAGE_AS_PATH); +WScript.Echo("Activity path: " + ACTIVITY_PATH); +WScript.Echo("Manifest path: " + MANIFEST_PATH); +WScript.Echo("Cordova version: " + VERSION); + +// TODO: clobber any existing example /* if [ $# -eq 0 ] @@ -76,32 +104,71 @@ exec('android.bat create project --target '+TARGET+' --path '+PROJECT_PATH+' --p // update the cordova framework project to a target that exists on this machine exec('android.bat update project --target '+TARGET+' --path framework'); +// pull down commons codec if necessary +var fso = WScript.CreateObject('Scripting.FileSystemObject'); +if (!fso.FileExists(ROOT + '\\framework\\libs\\commons-codec-1.6.jar')) { + // We need the .jar + var url = 'http://mirror.symnds.com/software/Apache//commons/codec/binaries/commons-codec-1.6-bin.zip'; + var savePath = ROOT + '\\framework\\libs\\commons-codec-1.6-bin.zip'; + if (!fso.FileExists(savePath)) { + // We need the zip to get the jar + var xhr = WScript.CreateObject('MSXML2.XMLHTTP'); + xhr.open('GET', url, false); + xhr.send(); + if (xhr.status == 200) { + var stream = WScript.CreateObject('ADODB.Stream'); + stream.Open(); + stream.Type = 1; + stream.Write(xhr.ResponseBody); + stream.Position = 0; + stream.SaveToFile(savePath); + stream.Close(); + } else { + WScript.Echo('Could not retrieve the commons-codec. Please download it yourself and put into the framework/libs directory. This process may fail now. Sorry.'); + } + } + var app = WScript.CreateObject('Shell.Application'); + var source = app.NameSpace(savePath).Items(); + var target = app.NameSpace(ROOT + '\\framework\\libs'); + target.CopyHere(source, 256); + + // Move the jar into libs + fso.MoveFile(ROOT + '\\framework\\libs\\commons-codec-1.6\\commons-codec-1.6.jar', ROOT + '\\framework\\libs\\commons-codec-1.6.jar'); + + // Clean up + fso.DeleteFile(ROOT + '\\framework\\libs\\commons-codec-1.6-bin.zip'); + fso.DeleteFolder(ROOT + '\\framework\\libs\\commons-codec-1.6', true); +} + + // compile cordova.js and cordova.jar // if you see an error about "Unable to resolve target" then you may need to // update your android tools or install an additional Android platform version exec('ant.bat -f framework\\build.xml jar'); // copy in the project template -exec('cmd /c xcopy bin\\templates\\project '+PROJECT_PATH+' /S /Y'); +exec('cmd /c xcopy bin\\templates\\project\\* '+PROJECT_PATH+' /S /Y'); + +// copy example www assets +exec('cmd /c xcopy ' + PROJECT_PATH + '\\cordova\\assets ' + PROJECT_PATH + ' /S /Y'); // copy in cordova.js -exec('cmd /c copy framework\\assets\\www\\cordova-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\cordova-'+VERSION+'.js /Y'); +exec('cmd /c copy framework\\assets\\js\\cordova.android.js '+PROJECT_PATH+'\\.cordova\\android\\cordova-'+VERSION+'.js /Y'); // copy in cordova.jar -exec('cmd /c copy framework\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar /Y'); +exec('cmd /c copy framework\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\.cordova\\android\\cordova-'+VERSION+'.jar /Y'); -// copy in default activity -exec('cmd /c copy bin\\templates\\Activity.java '+ACTIVITY_PATH+' /Y'); +// copy in xml +exec('cmd /c copy framework\\res\\xml\\cordova.xml ' + PROJECT_PATH + '\\.cordova\\android\\cordova.xml /Y'); +exec('cmd /c copy framework\\res\\xml\\plugins.xml ' + PROJECT_PATH + '\\.cordova\\android\\plugins.xml /Y'); -// interpolate the activity name and package -replaceInFile(ACTIVITY_PATH, /__ACTIVITY__/, ACTIVITY); -replaceInFile(ACTIVITY_PATH, /__ID__/, PACKAGE); +// write out config file +write(PROJECT_PATH + '\\.cordova\\config', + 'VERSION=' + VERSION + '\r\n' + + 'PROJECT_PATH=' + PROJECT_PATH + '\r\n' + + 'PACKAGE=' + PACKAGE + '\r\n' + + 'ACTIVITY=' + ACTIVITY + '\r\n' + + 'TARGET=' + TARGET); -replaceInFile(MANIFEST_PATH, /__ACTIVITY__/, ACTIVITY); -replaceInFile(MANIFEST_PATH, /__PACKAGE__/, PACKAGE); - -/* -# leave the id for launching -touch $PROJECT_PATH/package-activity -echo $PACKAGE/$PACKAGE.$ACTIVITY > $PROJECT_PATH/package-activity -*/ +// run project-specific create process +fork('cscript.exe ' + PROJECT_PATH + '\\cordova\\create.js'); \ No newline at end of file diff --git a/bin/templates/project/cordova/create.bat b/bin/templates/project/cordova/create.bat new file mode 100644 index 00000000..cfde65a5 --- /dev/null +++ b/bin/templates/project/cordova/create.bat @@ -0,0 +1,2 @@ +echo "BALLS" +cscript cordova\create.js \ No newline at end of file diff --git a/bin/templates/project/cordova/create.js b/bin/templates/project/cordova/create.js new file mode 100644 index 00000000..2bf56031 --- /dev/null +++ b/bin/templates/project/cordova/create.js @@ -0,0 +1,69 @@ +var shell=WScript.CreateObject("WScript.Shell"); + +function exec(s, output) { + WScript.Echo('Executing ' + s); + var o=shell.Exec(s); + while (o.Status == 0) { + WScript.Sleep(100); + } + WScript.Echo("Command exited with code " + o.Status); +} +function read(filename) { + var fso=WScript.CreateObject("Scripting.FileSystemObject"); + var f=fso.OpenTextFile(filename, 1); + var s=f.ReadAll(); + f.Close(); + return s; +} +function write(filename, contents) { + var fso=WScript.CreateObject("Scripting.FileSystemObject"); + var f=fso.OpenTextFile(filename, 2, true); + f.Write(contents); + f.Close(); +} +function replaceInFile(filename, regexp, replacement) { + write(filename, read(filename).replace(regexp, replacement)); +} + +// working dir +var PWD = WScript.ScriptFullName.split('\\cordova\\create.js').join(''); + +var fso=WScript.CreateObject("Scripting.FileSystemObject"); +var f=fso.OpenTextFile(PWD + '\\.cordova\\config', 1); +while (!f.AtEndOfStream) { + var prop = f.ReadLine().split('='); + var line = 'var ' + prop[0] + '=' + "'" + prop[1] + "';"; + eval(line); // hacky shit to load config but whatevs +} + +var PACKAGE_AS_PATH=PACKAGE.replace(/\./g, '\\'); +var ACTIVITY_PATH=PWD+'\\src\\'+PACKAGE_AS_PATH+'\\'+ACTIVITY+'.java'; +var MANIFEST_PATH=PWD+'\\AndroidManifest.xml'; + +exec('android.bat create project --target ' + TARGET + ' --path ' + PWD + ' --package ' + PACKAGE + ' --activity ' + ACTIVITY); + +// copy in activity and other android assets +exec('cmd /c xcopy ' + PWD + '\\cordova\\templates\\project\\* ' + PWD +' /Y /S'); + +// copy in cordova.js +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\cordova-' + VERSION + '.js ' + PWD + '\\assets\\www /Y'); + +// copy in cordova.jar +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\cordova-' + VERSION + '.jar ' + PWD + '\\libs /Y'); + +// copy in res/xml +exec('cmd /c md ' + PWD + '\\res\\xml'); +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\cordova.xml ' + PWD + '\\res\\xml /Y'); +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\plugins.xml ' + PWD + '\\res\\xml /Y'); + +// copy in default activity +exec('cmd /c copy ' + PWD + '\\cordova\\templates\\Activity.java ' + ACTIVITY_PATH + ' /Y'); + +// interpolate the activity name and package +replaceInFile(ACTIVITY_PATH, /__ACTIVITY__/, ACTIVITY); +replaceInFile(ACTIVITY_PATH, /__ID__/, PACKAGE); + +replaceInFile(MANIFEST_PATH, /__ACTIVITY__/, ACTIVITY); +replaceInFile(MANIFEST_PATH, /__PACKAGE__/, PACKAGE); + +WScript.Echo('Create completed successfully.'); \ No newline at end of file diff --git a/bin/templates/project/cordova/debug.bat b/bin/templates/project/cordova/debug.bat new file mode 100644 index 00000000..e69de29b diff --git a/bin/templates/project/cordova/emulate.bat b/bin/templates/project/cordova/emulate.bat new file mode 100644 index 00000000..e69de29b diff --git a/bin/templates/project/cordova/log.bat b/bin/templates/project/cordova/log.bat new file mode 100644 index 00000000..e69de29b diff --git a/bin/templates/project/cordova/templates/project/assets/www/main.js b/bin/templates/project/cordova/templates/project/assets/www/main.js index 739a02e3..3a8b04a1 100644 --- a/bin/templates/project/cordova/templates/project/assets/www/main.js +++ b/bin/templates/project/cordova/templates/project/assets/www/main.js @@ -88,7 +88,7 @@ function dump_pic(data) { viewport.style.position = "absolute"; viewport.style.top = "10px"; viewport.style.left = "10px"; - document.getElementById("test_img").src = "data:image/jpeg;base64," + data; + document.getElementById("test_img").src = data; } function fail(msg) { diff --git a/bin/templates/project/cordova/templates/project/res/drawable-hdpi/icon.png b/bin/templates/project/cordova/templates/project/res/drawable-hdpi/icon.png new file mode 100644 index 00000000..4d276344 Binary files /dev/null and b/bin/templates/project/cordova/templates/project/res/drawable-hdpi/icon.png differ diff --git a/bin/templates/project/cordova/templates/project/res/drawable-ldpi/icon.png b/bin/templates/project/cordova/templates/project/res/drawable-ldpi/icon.png new file mode 100644 index 00000000..cd5032a4 Binary files /dev/null and b/bin/templates/project/cordova/templates/project/res/drawable-ldpi/icon.png differ diff --git a/bin/templates/project/cordova/templates/project/res/drawable-mdpi/icon.png b/bin/templates/project/cordova/templates/project/res/drawable-mdpi/icon.png new file mode 100644 index 00000000..e79c6062 Binary files /dev/null and b/bin/templates/project/cordova/templates/project/res/drawable-mdpi/icon.png differ diff --git a/bin/templates/project/cordova/templates/project/res/drawable-xhdpi/icon.png b/bin/templates/project/cordova/templates/project/res/drawable-xhdpi/icon.png new file mode 100644 index 00000000..ec7ffbfb Binary files /dev/null and b/bin/templates/project/cordova/templates/project/res/drawable-xhdpi/icon.png differ diff --git a/bin/templates/project/cordova/templates/project/res/drawable/icon.png b/bin/templates/project/cordova/templates/project/res/drawable/icon.png old mode 100755 new mode 100644 index 697df7f3..ec7ffbfb Binary files a/bin/templates/project/cordova/templates/project/res/drawable/icon.png and b/bin/templates/project/cordova/templates/project/res/drawable/icon.png differ diff --git a/framework/.settings/org.eclipse.jdt.core.prefs b/framework/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index f77b31c2..00000000 --- a/framework/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 -org.eclipse.jdt.core.compiler.source=1.5 diff --git a/framework/assets/js/cordova.android.js b/framework/assets/js/cordova.android.js index 402de0c0..cdf619df 100644 --- a/framework/assets/js/cordova.android.js +++ b/framework/assets/js/cordova.android.js @@ -1,6 +1,6 @@ -// commit a5f0a62f9a9dc5fd7af95ec3d09b6b17c0b89b44 +// commit 4a4ba9985c920850fe3f229abc60de984e196ab5 -// File generated at :: Mon May 07 2012 16:20:11 GMT-0700 (PDT) +// File generated at :: Fri May 18 2012 13:43:11 GMT-0700 (PDT) /* Licensed to the Apache Software Foundation (ASF) under one @@ -98,17 +98,7 @@ var documentEventHandlers = {}, document.addEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); - if (e == 'deviceready') { - channel.onDeviceReady.subscribeOnce(handler); - } else if (e == 'resume') { - channel.onResume.subscribe(handler); - // if subscribing listener after event has already fired, invoke the handler - if (channel.onResume.fired && typeof handler == 'function') { - handler(); - } - } else if (e == 'pause') { - channel.onPause.subscribe(handler); - } else if (typeof documentEventHandlers[e] != 'undefined') { + if (typeof documentEventHandlers[e] != 'undefined') { documentEventHandlers[e].subscribe(handler); } else { m_document_addEventListener.call(document, evt, handler, capture); @@ -126,13 +116,8 @@ window.addEventListener = function(evt, handler, capture) { document.removeEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); - // Check for pause/resume events first. - if (e == 'resume') { - channel.onResume.unsubscribe(handler); - } else if (e == 'pause') { - channel.onPause.unsubscribe(handler); // If unsubcribing from an event that is handled by a plugin - } else if (typeof documentEventHandlers[e] != "undefined") { + if (typeof documentEventHandlers[e] != "undefined") { documentEventHandlers[e].unsubscribe(handler); } else { m_document_removeEventListener.call(document, evt, handler, capture); @@ -318,6 +303,11 @@ var cordova = { } }; +// Register pause, resume and deviceready channels as events on document. +channel.onPause = cordova.addDocumentEventHandler('pause'); +channel.onResume = cordova.addDocumentEventHandler('resume'); +channel.onDeviceReady = cordova.addDocumentEventHandler('deviceready'); + // Adds deprecation warnings to functions of an object (but only logs a message once) function deprecateFunctions(obj, objLabel) { var newObj = {}; @@ -645,10 +635,13 @@ Channel.prototype.unsubscribe = function(g) { if (g === null || g === undefined) { throw "You must pass _something_ into Channel.unsubscribe"; } if (typeof g == 'function') { g = g.observer_guid; } - this.handlers[g] = null; - delete this.handlers[g]; - this.numHandlers--; - if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this); + var handler = this.handlers[g]; + if (handler) { + this.handlers[g] = null; + delete this.handlers[g]; + this.numHandlers--; + if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this); + } }; /** @@ -1145,18 +1138,20 @@ module.exports = { } } }; + }); // file: lib/common/plugin/Acceleration.js define("cordova/plugin/Acceleration", function(require, exports, module) { var Acceleration = function(x, y, z, timestamp) { - this.x = x; - this.y = y; - this.z = z; - this.timestamp = timestamp || (new Date()).getTime(); + this.x = x; + this.y = y; + this.z = z; + this.timestamp = timestamp || (new Date()).getTime(); }; module.exports = Acceleration; + }); // file: lib/common/plugin/Camera.js @@ -1986,6 +1981,21 @@ Entry.prototype.getMetadata = function(successCallback, errorCallback) { exec(success, fail, "File", "getMetadata", [this.fullPath]); }; +/** + * Set the metadata of the entry. + * + * @param successCallback + * {Function} is called with a Metadata object + * @param errorCallback + * {Function} is called with a FileError + * @param metadataObject + * {Object} keys and values to set + */ +Entry.prototype.setMetadata = function(successCallback, errorCallback, metadataObject) { + + exec(successCallback, errorCallback, "File", "setMetadata", [this.fullPath, metadataObject]); +}; + /** * Move a file or directory to a new location. * @@ -3360,11 +3370,60 @@ define("cordova/plugin/accelerometer", function(require, exports, module) { * @constructor */ var utils = require("cordova/utils"), - exec = require("cordova/exec"); + exec = require("cordova/exec"), + Acceleration = require('cordova/plugin/Acceleration'); -// Local singleton variables. +// Is the accel sensor running? +var running = false; + +// Keeps reference to watchAcceleration calls. var timers = {}; +// Array of listeners; used to keep track of when we should call start and stop. +var listeners = []; + +// Last returned acceleration object from native +var accel = null; + +// Tells native to start. +function start() { + exec(function(a) { + var tempListeners = listeners.slice(0); + accel = new Acceleration(a.x, a.y, a.z, a.timestamp); + for (var i = 0, l = tempListeners.length; i < l; i++) { + tempListeners[i].win(accel); + } + }, function(e) { + var tempListeners = listeners.slice(0); + for (var i = 0, l = tempListeners.length; i < l; i++) { + tempListeners[i].fail(e); + } + }, "Accelerometer", "start", []); + running = true; +} + +// Tells native to stop. +function stop() { + exec(null, null, "Accelerometer", "stop", []); + running = false; +} + +// Adds a callback pair to the listeners array +function createCallbackPair(win, fail) { + return {win:win, fail:fail}; +} + +// Removes a win/fail listener pair from the listeners array +function removeListeners(l) { + var idx = listeners.indexOf(l); + if (idx > -1) { + listeners.splice(idx, 1); + if (listeners.length === 0) { + stop(); + } + } +} + var accelerometer = { /** * Asynchronously aquires the current acceleration. @@ -3374,21 +3433,27 @@ var accelerometer = { * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL) */ getCurrentAcceleration: function(successCallback, errorCallback, options) { - // successCallback required if (typeof successCallback !== "function") { - console.log("Accelerometer Error: successCallback is not a function"); - return; + throw "getCurrentAcceleration must be called with at least a success callback function as first parameter."; } - // errorCallback optional - if (errorCallback && (typeof errorCallback !== "function")) { - console.log("Accelerometer Error: errorCallback is not a function"); - return; - } + var p; + var win = function(a) { + successCallback(a); + removeListeners(p); + }; + var fail = function(e) { + errorCallback(e); + removeListeners(p); + }; - // Get acceleration - exec(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); + p = createCallbackPair(win, fail); + listeners.push(p); + + if (!running) { + start(); + } }, /** @@ -3400,36 +3465,38 @@ var accelerometer = { * @return String The watch id that must be passed to #clearWatch to stop watching. */ watchAcceleration: function(successCallback, errorCallback, options) { - // Default interval (10 sec) - var frequency = (options !== undefined && options.frequency !== undefined)? options.frequency : 10000; + var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000; // successCallback required if (typeof successCallback !== "function") { - console.log("Accelerometer Error: successCallback is not a function"); - return; + throw "watchAcceleration must be called with at least a success callback function as first parameter."; } - // errorCallback optional - if (errorCallback && (typeof errorCallback !== "function")) { - console.log("Accelerometer Error: errorCallback is not a function"); - return; - } - - // Make sure accelerometer timeout > frequency + 10 sec - exec( - function(timeout) { - if (timeout < (frequency + 10000)) { - exec(null, null, "Accelerometer", "setTimeout", [frequency + 10000]); - } - }, - function(e) { }, "Accelerometer", "getTimeout", []); - - // Start watch timer + // Keep reference to watch id, and report accel readings as often as defined in frequency var id = utils.createUUID(); - timers[id] = window.setInterval(function() { - exec(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); - }, (frequency ? frequency : 1)); + + var p = createCallbackPair(function(){}, function(e) { + errorCallback(e); + removeListeners(p); + }); + listeners.push(p); + + timers[id] = { + timer:window.setInterval(function() { + if (accel) { + successCallback(accel); + } + }, frequency), + listeners:p + }; + + if (running) { + // If we're already running then immediately invoke the success callback + successCallback(accel); + } else { + start(); + } return id; }, @@ -3440,16 +3507,17 @@ var accelerometer = { * @param {String} id The id of the watch returned from #watchAcceleration. */ clearWatch: function(id) { - // Stop javascript timer & remove from timer list - if (id && timers[id] !== undefined) { - window.clearInterval(timers[id]); + if (id && timers[id]) { + window.clearInterval(timers[id].timer); + removeListeners(timers[id].listeners); delete timers[id]; } } }; module.exports = accelerometer; + }); // file: lib/android/plugin/android/app.js @@ -5163,7 +5231,7 @@ window.cordova = require('cordova'); // Fire onDeviceReady event once all constructors have run and // cordova info has been received from native side. channel.join(function() { - channel.onDeviceReady.fire(); + require('cordova').fireDocumentEvent('deviceready'); }, channel.deviceReadyChannelsArray); }, [ channel.onDOMContentLoaded, channel.onNativeReady ]); @@ -5182,4 +5250,5 @@ window.cordova = require('cordova'); }(window)); + })(); \ No newline at end of file diff --git a/framework/project.properties b/framework/project.properties index 247ca66e..276f1305 100644 --- a/framework/project.properties +++ b/framework/project.properties @@ -10,5 +10,5 @@ # Indicates whether an apk should be generated for each density. split.density=false # Project target. -target=android-14 +target=Google Inc.:Google APIs:15 apk-configurations= diff --git a/framework/src/org/apache/cordova/AccelListener.java b/framework/src/org/apache/cordova/AccelListener.java index f751e4e2..9e19d753 100755 --- a/framework/src/org/apache/cordova/AccelListener.java +++ b/framework/src/org/apache/cordova/AccelListener.java @@ -18,7 +18,11 @@ */ package org.apache.cordova; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.cordova.api.CordovaInterface; import org.apache.cordova.api.Plugin; @@ -32,6 +36,8 @@ import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.location.Location; +import android.util.Log; import android.content.Context; /** @@ -40,20 +46,20 @@ import android.content.Context; */ public class AccelListener extends Plugin implements SensorEventListener { - public static int STOPPED = 0; - public static int STARTING = 1; + public static int STOPPED = 0; + public static int STARTING = 1; public static int RUNNING = 2; public static int ERROR_FAILED_TO_START = 3; - public float TIMEOUT = 30000; // Timeout in msec to shut off listener - - float x,y,z; // most recent acceleration values - long timestamp; // time of most recent value - int status; // status of listener - long lastAccessTime; // time the value was last retrieved + private float x,y,z; // most recent acceleration values + private long timestamp; // time of most recent value + private int status; // status of listener + private int accuracy = SensorManager.SENSOR_STATUS_UNRELIABLE; - private SensorManager sensorManager;// Sensor manager - Sensor mSensor; // Acceleration sensor returned by sensor manager + private SensorManager sensorManager; // Sensor manager + private Sensor mSensor; // Acceleration sensor returned by sensor manager + + private String callbackId; // Keeps track of the single "start" callback ID passed in from JS /** * Create an accelerometer listener. @@ -66,171 +72,115 @@ public class AccelListener extends Plugin implements SensorEventListener { this.setStatus(AccelListener.STOPPED); } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - public void setContext(CordovaInterface ctx) { - super.setContext(ctx); + /** + * Sets the context of the Command. This can then be used to do things like + * get file paths associated with the Activity. + * + * @param ctx The context of the main Activity. + */ + public void setContext(CordovaInterface ctx) { + super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); - } + } - /** - * Executes the request and returns PluginResult. - * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @param callbackId The callback id used when calling back into JavaScript. - * @return A PluginResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args, String callbackId) { - PluginResult.Status status = PluginResult.Status.OK; - String result = ""; - - try { - if (action.equals("getStatus")) { - int i = this.getStatus(); - return new PluginResult(status, i); - } - else if (action.equals("start")) { - int i = this.start(); - return new PluginResult(status, i); - } - else if (action.equals("stop")) { - this.stop(); - return new PluginResult(status, 0); - } - else if (action.equals("getAcceleration")) { - // If not running, then this is an async call, so don't worry about waiting - if (this.status != AccelListener.RUNNING) { - int r = this.start(); - if (r == AccelListener.ERROR_FAILED_TO_START) { - return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START); - } - // Wait until running - long timeout = 2000; - while ((this.status == STARTING) && (timeout > 0)) { - timeout = timeout - 100; - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - if (timeout == 0) { - return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START); - } - } - this.lastAccessTime = System.currentTimeMillis(); - JSONObject r = new JSONObject(); - r.put("x", this.x); - r.put("y", this.y); - r.put("z", this.z); - // TODO: Should timestamp be sent? - r.put("timestamp", this.timestamp); - return new PluginResult(status, r); - } - else if (action.equals("setTimeout")) { - try { - float timeout = Float.parseFloat(args.getString(0)); - this.setTimeout(timeout); - return new PluginResult(status, 0); - } catch (NumberFormatException e) { - status = PluginResult.Status.INVALID_ACTION; - e.printStackTrace(); - } catch (JSONException e) { - status = PluginResult.Status.JSON_EXCEPTION; - e.printStackTrace(); - } - } - else if (action.equals("getTimeout")) { - float f = this.getTimeout(); - return new PluginResult(status, f); - } else { - // Unsupported action - return new PluginResult(PluginResult.Status.INVALID_ACTION); - } - return new PluginResult(status, result); - } catch (JSONException e) { - return new PluginResult(PluginResult.Status.JSON_EXCEPTION); - } - } + /** + * Executes the request and returns PluginResult. + * + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. + */ + public PluginResult execute(String action, JSONArray args, String callbackId) { + PluginResult.Status status = PluginResult.Status.NO_RESULT; + String message = ""; + PluginResult result = new PluginResult(status, message); + result.setKeepCallback(true); - /** - * Identifies if action to be executed returns a value and should be run synchronously. - * - * @param action The action to execute - * @return T=returns value - */ - public boolean isSynch(String action) { - if (action.equals("getStatus")) { - return true; - } - else if (action.equals("getAcceleration")) { - // Can only return value if RUNNING - if (this.status == AccelListener.RUNNING) { - return true; - } - } - else if (action.equals("getTimeout")) { - return true; - } - return false; - } + if (action.equals("start")) { + this.callbackId = callbackId; + if (this.status != AccelListener.RUNNING) { + // If not running, then this is an async call, so don't worry about waiting + // We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road + this.start(); + } + } + else if (action.equals("stop")) { + if (this.status == AccelListener.RUNNING) { + this.stop(); + } + } else { + // Unsupported action + return new PluginResult(PluginResult.Status.INVALID_ACTION); + } + return result; + } /** * Called by AccelBroker when listener is to be shut down. * Stop listener. */ public void onDestroy() { - this.stop(); + this.stop(); } //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- - + // /** * Start listening for acceleration sensor. * * @return status of listener */ - public int start() { + private int start() { + // If already starting or running, then just return + if ((this.status == AccelListener.RUNNING) || (this.status == AccelListener.STARTING)) { + return this.status; + } + + this.setStatus(AccelListener.STARTING); + + // Get accelerometer from sensor manager + List list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); - // If already starting or running, then just return - if ((this.status == AccelListener.RUNNING) || (this.status == AccelListener.STARTING)) { - return this.status; - } - - // Get accelerometer from sensor manager - List list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); - - // If found, then register as listener - if ((list != null) && (list.size() > 0)) { - this.mSensor = list.get(0); - this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_FASTEST); - this.setStatus(AccelListener.STARTING); - this.lastAccessTime = System.currentTimeMillis(); - } - - // If error, then set status to error - else { - this.setStatus(AccelListener.ERROR_FAILED_TO_START); - } - - return this.status; + // If found, then register as listener + if ((list != null) && (list.size() > 0)) { + this.mSensor = list.get(0); + this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI); + this.setStatus(AccelListener.STARTING); + } else { + this.setStatus(AccelListener.ERROR_FAILED_TO_START); + this.fail(AccelListener.ERROR_FAILED_TO_START, "No sensors found to register accelerometer listening to."); + return this.status; + } + + // Wait until running + long timeout = 2000; + while ((this.status == STARTING) && (timeout > 0)) { + timeout = timeout - 100; + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + if (timeout == 0) { + this.setStatus(AccelListener.ERROR_FAILED_TO_START); + this.fail(AccelListener.ERROR_FAILED_TO_START, "Accelerometer could not be started."); + } + return this.status; } /** * Stop listening to acceleration sensor. */ - public void stop() { + private void stop() { if (this.status != AccelListener.STOPPED) { - this.sensorManager.unregisterListener(this); + this.sensorManager.unregisterListener(this); } this.setStatus(AccelListener.STOPPED); + this.accuracy = SensorManager.SENSOR_STATUS_UNRELIABLE; } /** @@ -240,6 +190,16 @@ public class AccelListener extends Plugin implements SensorEventListener { * @param accuracy */ public void onAccuracyChanged(Sensor sensor, int accuracy) { + // Only look at accelerometer events + if (sensor.getType() != Sensor.TYPE_ACCELEROMETER) { + return; + } + + // If not running, then just return + if (this.status == AccelListener.STOPPED) { + return; + } + this.accuracy = accuracy; } /** @@ -248,64 +208,68 @@ public class AccelListener extends Plugin implements SensorEventListener { * @param SensorEvent event */ public void onSensorChanged(SensorEvent event) { - - // Only look at accelerometer events + // Only look at accelerometer events if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) { return; } // If not running, then just return if (this.status == AccelListener.STOPPED) { - return; + return; } - // Save time that event was received - this.timestamp = System.currentTimeMillis(); - this.x = event.values[0]; - this.y = event.values[1]; - this.z = event.values[2]; - this.setStatus(AccelListener.RUNNING); + + if (this.accuracy >= SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) { - // If values haven't been read for TIMEOUT time, then turn off accelerometer sensor to save power - if ((this.timestamp - this.lastAccessTime) > this.TIMEOUT) { - this.stop(); - } + // Save time that event was received + this.timestamp = System.currentTimeMillis(); + this.x = event.values[0]; + this.y = event.values[1]; + this.z = event.values[2]; + + this.win(); + } } - /** - * Get status of accelerometer sensor. - * - * @return status - */ - public int getStatus() { - return this.status; - } - - /** - * Set the timeout to turn off accelerometer sensor if getX() hasn't been called. - * - * @param timeout Timeout in msec. - */ - public void setTimeout(float timeout) { - this.TIMEOUT = timeout; - } - - /** - * Get the timeout to turn off accelerometer sensor if getX() hasn't been called. - * - * @return timeout in msec - */ - public float getTimeout() { - return this.TIMEOUT; - } - - /** - * Set the status and send it to JavaScript. - * @param status - */ - private void setStatus(int status) { - this.status = status; - } + // Sends an error back to JS + private void fail(int code, String message) { + // Error object + JSONObject errorObj = new JSONObject(); + try { + errorObj.put("code", code); + errorObj.put("message", message); + } catch (JSONException e) { + e.printStackTrace(); + } + PluginResult err = new PluginResult(PluginResult.Status.ERROR, errorObj); + err.setKeepCallback(true); + + this.error(err, this.callbackId); + } + private void win() { + // Success return object + PluginResult result = new PluginResult(PluginResult.Status.OK, this.getAccelerationJSON()); + result.setKeepCallback(true); + + this.success(result, this.callbackId); + } + + private void setStatus(int status) { + this.status = status; + } + + private JSONObject getAccelerationJSON() { + JSONObject r = new JSONObject(); + try { + r.put("x", this.x); + r.put("y", this.y); + r.put("z", this.z); + r.put("timestamp", this.timestamp); + } catch (JSONException e) { + e.printStackTrace(); + } + return r; + } } diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index 61689db6..e94a44e2 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -813,7 +813,7 @@ public class DroidGap extends Activity implements CordovaInterface { } // Send pause event to JavaScript - this.appView.loadUrl("javascript:try{cordova.require('cordova/channel').onPause.fire();}catch(e){console.log('exception firing pause event from native');};"); + this.appView.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};"); // Forward to plugins if (this.pluginManager != null) { @@ -858,7 +858,7 @@ public class DroidGap extends Activity implements CordovaInterface { } // Send resume event to JavaScript - this.appView.loadUrl("javascript:try{cordova.require('cordova/channel').onResume.fire();}catch(e){console.log('exception firing resume event from native');};"); + this.appView.loadUrl("javascript:try{cordova.fireDocumentEvent('resume');}catch(e){console.log('exception firing resume event from native');};"); // Forward to plugins if (this.pluginManager != null) { diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java index c8a45cb4..c7d02804 100755 --- a/framework/src/org/apache/cordova/FileUtils.java +++ b/framework/src/org/apache/cordova/FileUtils.java @@ -220,7 +220,7 @@ public class FileUtils extends Plugin { * @param filePath the path to check */ private void notifyDelete(String filePath) { - String newFilePath = filePath.substring(7); + String newFilePath = stripFileProtocol(filePath); int result = this.ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + " = ?", new String[] {newFilePath});