You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bc...@apache.org on 2012/05/15 06:38:01 UTC
[9/15] android commit: Updates.
Updates.
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/c8fafa6b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/c8fafa6b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/c8fafa6b
Branch: refs/heads/CordovaWebView
Commit: c8fafa6bbd55ee06180d7326632ac681cdc669df
Parents: f3e70c7
Author: Bryce Curtis <cu...@gmail.com>
Authored: Mon May 14 21:49:30 2012 -0500
Committer: Bryce Curtis <cu...@gmail.com>
Committed: Mon May 14 21:49:30 2012 -0500
----------------------------------------------------------------------
framework/src/com/phonegap/api/PluginManager.java | 3 +-
.../src/org/apache/cordova/AccelListener.java | 332 +-
framework/src/org/apache/cordova/App.java | 217 +-
framework/src/org/apache/cordova/AudioHandler.java | 403 +-
framework/src/org/apache/cordova/AudioPlayer.java | 752 ++--
.../src/org/apache/cordova/BatteryListener.java | 49 +-
.../src/org/apache/cordova/CameraLauncher.java | 222 +-
framework/src/org/apache/cordova/Capture.java | 116 +-
.../src/org/apache/cordova/CompassListener.java | 43 +-
.../src/org/apache/cordova/ContactAccessor.java | 211 +-
.../org/apache/cordova/ContactAccessorSdk5.java | 3125 +++++++--------
.../org/apache/cordova/CordovaChromeClient.java | 188 +-
.../src/org/apache/cordova/CordovaException.java | 1 -
.../src/org/apache/cordova/CordovaWebView.java | 1062 +++---
.../org/apache/cordova/CordovaWebViewClient.java | 255 +-
framework/src/org/apache/cordova/Device.java | 57 +-
framework/src/org/apache/cordova/DroidGap.java | 1045 +++--
framework/src/org/apache/cordova/FileTransfer.java | 132 +-
framework/src/org/apache/cordova/FileUtils.java | 65 +-
framework/src/org/apache/cordova/GeoListener.java | 203 +-
framework/src/org/apache/cordova/GpsListener.java | 256 +-
framework/src/org/apache/cordova/HttpHandler.java | 105 +-
.../cordova/LinearLayoutSoftKeyboardDetect.java | 27 +-
.../src/org/apache/cordova/NetworkListener.java | 246 +-
.../src/org/apache/cordova/NetworkManager.java | 51 +-
framework/src/org/apache/cordova/Notification.java | 593 ++--
framework/src/org/apache/cordova/SplashScreen.java | 3 +-
framework/src/org/apache/cordova/StandAlone.java | 18 +-
framework/src/org/apache/cordova/Storage.java | 395 +-
framework/src/org/apache/cordova/TempListener.java | 130 +-
.../org/apache/cordova/api/CordovaInterface.java | 187 +-
framework/src/org/apache/cordova/api/IPlugin.java | 77 +-
framework/src/org/apache/cordova/api/Plugin.java | 119 +-
.../src/org/apache/cordova/api/PluginEntry.java | 12 +-
.../src/org/apache/cordova/api/PluginManager.java | 49 +-
.../src/org/apache/cordova/api/PluginResult.java | 186 +-
36 files changed, 5680 insertions(+), 5255 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/com/phonegap/api/PluginManager.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java
index b0c0f0b..f381359 100755
--- a/framework/src/com/phonegap/api/PluginManager.java
+++ b/framework/src/com/phonegap/api/PluginManager.java
@@ -18,6 +18,7 @@
*/
package com.phonegap.api;
+import org.apache.cordova.CordovaWebView;
import org.apache.cordova.api.CordovaInterface;
import android.webkit.WebView;
@@ -31,6 +32,6 @@ import android.webkit.WebView;
public class PluginManager extends org.apache.cordova.api.PluginManager {
public PluginManager(WebView app, CordovaInterface ctx) throws Exception {
- super(app, ctx);
+ super((CordovaWebView) app, ctx);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/AccelListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AccelListener.java b/framework/src/org/apache/cordova/AccelListener.java
index 9e62a5b..dbda3fe 100755
--- a/framework/src/org/apache/cordova/AccelListener.java
+++ b/framework/src/org/apache/cordova/AccelListener.java
@@ -27,7 +27,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
@@ -40,14 +39,14 @@ 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
+
+ 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
@@ -64,128 +63,129 @@ public class AccelListener extends Plugin implements SensorEventListener {
this.z = 0;
this.timestamp = 0;
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(Context 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);
- }
- }
-
- /**
- * 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;
- }
-
+ /**
+ * 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.getActivity().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);
+ }
+ }
+
+ /**
+ * 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;
+ }
+
/**
* Called by AccelBroker when listener is to be shut down.
* Stop listener.
*/
public void onDestroy() {
- this.stop();
+ this.stop();
}
//--------------------------------------------------------------------------
@@ -199,14 +199,14 @@ public class AccelListener extends Plugin implements SensorEventListener {
*/
public int start() {
- // If already starting or running, then just return
+ // If already starting or running, then just return
if ((this.status == AccelListener.RUNNING) || (this.status == AccelListener.STARTING)) {
- return this.status;
+ return this.status;
}
// Get accelerometer from sensor manager
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
-
+
// If found, then register as listener
if ((list != null) && (list.size() > 0)) {
this.mSensor = list.get(0);
@@ -214,12 +214,12 @@ public class AccelListener extends Plugin implements SensorEventListener {
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;
}
@@ -228,7 +228,7 @@ public class AccelListener extends Plugin implements SensorEventListener {
*/
public void stop() {
if (this.status != AccelListener.STOPPED) {
- this.sensorManager.unregisterListener(this);
+ this.sensorManager.unregisterListener(this);
}
this.setStatus(AccelListener.STOPPED);
}
@@ -248,29 +248,29 @@ 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.z = event.values[2];
this.setStatus(AccelListener.RUNNING);
// 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();
- }
+ if ((this.timestamp - this.lastAccessTime) > this.TIMEOUT) {
+ this.stop();
+ }
}
/**
@@ -278,34 +278,34 @@ public class AccelListener extends Plugin implements SensorEventListener {
*
* @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;
- }
-
+ 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;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/App.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/App.java b/framework/src/org/apache/cordova/App.java
index 146140d..75460d5 100755
--- a/framework/src/org/apache/cordova/App.java
+++ b/framework/src/org/apache/cordova/App.java
@@ -26,7 +26,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import android.webkit.WebView;
+//import android.webkit.WebView;
import android.app.Activity;
import android.view.View;
@@ -50,38 +50,43 @@ public class App extends Plugin {
String result = "";
try {
- if (action.equals("clearCache")) {
- this.clearCache();
- }
- else if (action.equals("show")) {
- final CordovaWebView wv = this.webView;
- ((Activity)this.ctx).runOnUiThread(new Runnable() {
- public void run() {
- wv.setVisibility(View.VISIBLE);
- }
- });
- }
- else if (action.equals("loadUrl")) {
- this.loadUrl(args.getString(0), args.optJSONObject(1));
+ if (action.equals("clearCache")) {
+ this.clearCache();
+ }
+ else if (action.equals("show")) { // TODO @bc - Not in master branch. When should this be called?
+ ctx.getActivity().runOnUiThread(new Runnable() {
+ public void run() {
+ webView.postMessage("spinner", "stop");
+ }
+ });
+// final CordovaWebView wv = this.webView;
+// ((Activity) this.ctx).runOnUiThread(new Runnable() {
+// public void run() {
+// wv.setVisibility(View.VISIBLE);
+// }
+// });
}
- else if (action.equals("cancelLoadUrl")) {
- this.cancelLoadUrl();
+ else if (action.equals("loadUrl")) {
+ this.loadUrl(args.getString(0), args.optJSONObject(1));
}
- else if (action.equals("clearHistory")) {
- this.clearHistory();
+ else if (action.equals("cancelLoadUrl")) {
+ this.cancelLoadUrl();
+ }
+ else if (action.equals("clearHistory")) {
+ this.clearHistory();
}
else if (action.equals("backHistory")) {
this.backHistory();
}
- else if (action.equals("overrideBackbutton")) {
- this.overrideBackbutton(args.getBoolean(0));
+ else if (action.equals("overrideBackbutton")) {
+ this.overrideBackbutton(args.getBoolean(0));
}
- else if (action.equals("isBackbuttonOverridden")) {
- boolean b = this.isBackbuttonOverridden();
- return new PluginResult(status, b);
+ else if (action.equals("isBackbuttonOverridden")) {
+ boolean b = this.isBackbuttonOverridden();
+ return new PluginResult(status, b);
}
- else if (action.equals("exitApp")) {
- this.exitApp();
+ else if (action.equals("exitApp")) {
+ this.exitApp();
}
return new PluginResult(status, result);
} catch (JSONException e) {
@@ -93,93 +98,93 @@ public class App extends Plugin {
// LOCAL METHODS
//--------------------------------------------------------------------------
- /**
- * Clear the resource cache.
- */
- public void clearCache() {
- webView.clearCache(true);
- }
-
- /**
- * Load the url into the webview.
- *
- * @param url
- * @param props Properties that can be passed in to the DroidGap activity (i.e. loadingDialog, wait, ...)
- * @throws JSONException
- */
- public void loadUrl(String url, JSONObject props) throws JSONException {
- LOG.d("App", "App.loadUrl("+url+","+props+")");
- int wait = 0;
- boolean openExternal = false;
- boolean clearHistory = false;
-
- // If there are properties, then set them on the Activity
- HashMap<String, Object> params = new HashMap<String, Object>();
- if (props != null) {
- JSONArray keys = props.names();
- for (int i=0; i<keys.length(); i++) {
- String key = keys.getString(i);
- if (key.equals("wait")) {
- wait = props.getInt(key);
- }
- else if (key.equalsIgnoreCase("openexternal")) {
- openExternal = props.getBoolean(key);
- }
- else if (key.equalsIgnoreCase("clearhistory")) {
- clearHistory = props.getBoolean(key);
- }
- else {
- Object value = props.get(key);
- if (value == null) {
-
- }
- else if (value.getClass().equals(String.class)) {
- params.put(key, (String)value);
- }
- else if (value.getClass().equals(Boolean.class)) {
- params.put(key, (Boolean)value);
- }
- else if (value.getClass().equals(Integer.class)) {
- params.put(key, (Integer)value);
- }
- }
- }
- }
-
- // If wait property, then delay loading
-
- if (wait > 0) {
- try {
- synchronized(this) {
- this.wait(wait);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- webView.showWebPage(url, openExternal, clearHistory, params);
- }
-
- /**
- * Cancel loadUrl before it has been loaded (Only works on a CordovaInterface class)
- */
- public void cancelLoadUrl() {
- ((DroidGap)this.ctx).cancelLoadUrl();
- }
-
+ /**
+ * Clear the resource cache.
+ */
+ public void clearCache() {
+ this.webView.clearCache(true);
+ }
+
+ /**
+ * Load the url into the webview.
+ *
+ * @param url
+ * @param props Properties that can be passed in to the DroidGap activity (i.e. loadingDialog, wait, ...)
+ * @throws JSONException
+ */
+ public void loadUrl(String url, JSONObject props) throws JSONException {
+ LOG.d("App", "App.loadUrl(" + url + "," + props + ")");
+ int wait = 0;
+ boolean openExternal = false;
+ boolean clearHistory = false;
+
+ // If there are properties, then set them on the Activity
+ HashMap<String, Object> params = new HashMap<String, Object>();
+ if (props != null) {
+ JSONArray keys = props.names();
+ for (int i = 0; i < keys.length(); i++) {
+ String key = keys.getString(i);
+ if (key.equals("wait")) {
+ wait = props.getInt(key);
+ }
+ else if (key.equalsIgnoreCase("openexternal")) {
+ openExternal = props.getBoolean(key);
+ }
+ else if (key.equalsIgnoreCase("clearhistory")) {
+ clearHistory = props.getBoolean(key);
+ }
+ else {
+ Object value = props.get(key);
+ if (value == null) {
+
+ }
+ else if (value.getClass().equals(String.class)) {
+ params.put(key, (String) value);
+ }
+ else if (value.getClass().equals(Boolean.class)) {
+ params.put(key, (Boolean) value);
+ }
+ else if (value.getClass().equals(Integer.class)) {
+ params.put(key, (Integer) value);
+ }
+ }
+ }
+ }
+
+ // If wait property, then delay loading
+
+ if (wait > 0) {
+ try {
+ synchronized (this) {
+ this.wait(wait);
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ this.webView.showWebPage(url, openExternal, clearHistory, params);
+ }
+
+ /**
+ * Cancel loadUrl before it has been loaded (Only works on a CordovaInterface class)
+ */
+ public void cancelLoadUrl() {
+ this.ctx.cancelLoadUrl();
+ }
+
/**
* Clear page history for the app.
*/
public void clearHistory() {
- webView.clearHistory();
+ this.webView.clearHistory();
}
-
+
/**
* Go to previous page displayed.
* This is the same as pressing the backbutton on Android device.
*/
public void backHistory() {
- webView.backHistory();
+ this.webView.backHistory();
}
/**
@@ -189,8 +194,8 @@ public class App extends Plugin {
* @param override T=override, F=cancel override
*/
public void overrideBackbutton(boolean override) {
- LOG.i("DroidGap", "WARNING: Back Button Default Behaviour will be overridden. The backbutton event will be fired!");
- ((DroidGap)this.ctx).bound = override;
+ LOG.i("App", "WARNING: Back Button Default Behaviour will be overridden. The backbutton event will be fired!");
+ this.ctx.bindBackButton(override);
}
/**
@@ -199,14 +204,14 @@ public class App extends Plugin {
* @return boolean
*/
public boolean isBackbuttonOverridden() {
- return ((DroidGap)this.ctx).bound;
+ return this.ctx.isBackButtonBound();
}
/**
* Exit the Android application.
*/
public void exitApp() {
- ((DroidGap)this.ctx).endActivity();
+ this.ctx.getActivity().finish();
}
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/AudioHandler.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AudioHandler.java b/framework/src/org/apache/cordova/AudioHandler.java
index d7df63e..644640c 100755
--- a/framework/src/org/apache/cordova/AudioHandler.java
+++ b/framework/src/org/apache/cordova/AudioHandler.java
@@ -29,7 +29,8 @@ import org.json.JSONArray;
import org.json.JSONException;
import java.util.HashMap;
-import java.util.Map.Entry;
+
+//import java.util.Map.Entry;
/**
* This class called by CordovaActivity to play and record audio.
@@ -45,98 +46,98 @@ import java.util.Map.Entry;
public class AudioHandler extends Plugin {
public static String TAG = "AudioHandler";
- HashMap<String,AudioPlayer> players; // Audio player object
- ArrayList<AudioPlayer> pausedForPhone; // Audio players that were paused when phone call came in
-
- /**
- * Constructor.
- */
- public AudioHandler() {
- this.players = new HashMap<String,AudioPlayer>();
- this.pausedForPhone = new ArrayList<AudioPlayer>();
- }
-
- /**
- * 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("startRecordingAudio")) {
- this.startRecordingAudio(args.getString(0), args.getString(1));
- }
- else if (action.equals("stopRecordingAudio")) {
- this.stopRecordingAudio(args.getString(0));
- }
- else if (action.equals("startPlayingAudio")) {
- this.startPlayingAudio(args.getString(0), args.getString(1));
- }
- else if (action.equals("seekToAudio")) {
- this.seekToAudio(args.getString(0), args.getInt(1));
- }
- else if (action.equals("pausePlayingAudio")) {
- this.pausePlayingAudio(args.getString(0));
- }
- else if (action.equals("stopPlayingAudio")) {
- this.stopPlayingAudio(args.getString(0));
- } else if (action.equals("setVolume")) {
- try {
- this.setVolume(args.getString(0), Float.parseFloat(args.getString(1)));
- } catch (NumberFormatException nfe) {
- //no-op
- }
- } else if (action.equals("getCurrentPositionAudio")) {
- float f = this.getCurrentPositionAudio(args.getString(0));
- return new PluginResult(status, f);
- }
- else if (action.equals("getDurationAudio")) {
- float f = this.getDurationAudio(args.getString(0), args.getString(1));
- return new PluginResult(status, f);
- }
- else if (action.equals("release")) {
- boolean b = this.release(args.getString(0));
- return new PluginResult(status, b);
- }
- return new PluginResult(status, result);
- } catch (JSONException e) {
- e.printStackTrace();
- return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
- }
- }
-
- /**
- * 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("getCurrentPositionAudio")) {
- return true;
- }
- else if (action.equals("getDurationAudio")) {
- return true;
- }
- return false;
- }
-
- /**
- * Stop all audio players and recorders.
- */
- public void onDestroy() {
+ HashMap<String, AudioPlayer> players; // Audio player object
+ ArrayList<AudioPlayer> pausedForPhone; // Audio players that were paused when phone call came in
+
+ /**
+ * Constructor.
+ */
+ public AudioHandler() {
+ this.players = new HashMap<String, AudioPlayer>();
+ this.pausedForPhone = new ArrayList<AudioPlayer>();
+ }
+
+ /**
+ * 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("startRecordingAudio")) {
+ this.startRecordingAudio(args.getString(0), args.getString(1));
+ }
+ else if (action.equals("stopRecordingAudio")) {
+ this.stopRecordingAudio(args.getString(0));
+ }
+ else if (action.equals("startPlayingAudio")) {
+ this.startPlayingAudio(args.getString(0), args.getString(1));
+ }
+ else if (action.equals("seekToAudio")) {
+ this.seekToAudio(args.getString(0), args.getInt(1));
+ }
+ else if (action.equals("pausePlayingAudio")) {
+ this.pausePlayingAudio(args.getString(0));
+ }
+ else if (action.equals("stopPlayingAudio")) {
+ this.stopPlayingAudio(args.getString(0));
+ } else if (action.equals("setVolume")) {
+ try {
+ this.setVolume(args.getString(0), Float.parseFloat(args.getString(1)));
+ } catch (NumberFormatException nfe) {
+ //no-op
+ }
+ } else if (action.equals("getCurrentPositionAudio")) {
+ float f = this.getCurrentPositionAudio(args.getString(0));
+ return new PluginResult(status, f);
+ }
+ else if (action.equals("getDurationAudio")) {
+ float f = this.getDurationAudio(args.getString(0), args.getString(1));
+ return new PluginResult(status, f);
+ }
+ else if (action.equals("release")) {
+ boolean b = this.release(args.getString(0));
+ return new PluginResult(status, b);
+ }
+ return new PluginResult(status, result);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
+ }
+ }
+
+ /**
+ * 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("getCurrentPositionAudio")) {
+ return true;
+ }
+ else if (action.equals("getDurationAudio")) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Stop all audio players and recorders.
+ */
+ public void onDestroy() {
for (AudioPlayer audio : this.players.values()) {
audio.destroy();
}
this.players.clear();
- }
-
+ }
+
/**
* Called when a message is sent to plugin.
*
@@ -144,13 +145,13 @@ public class AudioHandler extends Plugin {
* @param data The message data
*/
public void onMessage(String id, Object data) {
-
+
// If phone message
if (id.equals("telephone")) {
-
+
// If phone ringing, then pause playing
if ("ringing".equals(data) || "offhook".equals(data)) {
-
+
// Get all audio players and pause them
for (AudioPlayer audio : this.players.values()) {
if (audio.getState() == AudioPlayer.MEDIA_RUNNING) {
@@ -160,7 +161,7 @@ public class AudioHandler extends Plugin {
}
}
-
+
// If phone idle, then resume playing those players we paused
else if ("idle".equals(data)) {
for (AudioPlayer audio : this.pausedForPhone) {
@@ -174,177 +175,179 @@ public class AudioHandler extends Plugin {
//--------------------------------------------------------------------------
// LOCAL METHODS
//--------------------------------------------------------------------------
-
- /**
- * Release the audio player instance to save memory.
- *
- * @param id The id of the audio player
- */
- private boolean release(String id) {
- if (!this.players.containsKey(id)) {
- return false;
- }
- AudioPlayer audio = this.players.get(id);
- this.players.remove(id);
- audio.destroy();
- return true;
- }
-
- /**
- * Start recording and save the specified file.
- *
- * @param id The id of the audio player
- * @param file The name of the file
- */
+
+ /**
+ * Release the audio player instance to save memory.
+ *
+ * @param id The id of the audio player
+ */
+ private boolean release(String id) {
+ if (!this.players.containsKey(id)) {
+ return false;
+ }
+ AudioPlayer audio = this.players.get(id);
+ this.players.remove(id);
+ audio.destroy();
+ return true;
+ }
+
+ /**
+ * Start recording and save the specified file.
+ *
+ * @param id The id of the audio player
+ * @param file The name of the file
+ */
public void startRecordingAudio(String id, String file) {
- // If already recording, then just return;
- if (this.players.containsKey(id)) {
- return;
- }
- AudioPlayer audio = new AudioPlayer(this, id);
- this.players.put(id, audio);
- audio.startRecording(file);
+ // If already recording, then just return;
+ if (this.players.containsKey(id)) {
+ return;
+ }
+ AudioPlayer audio = new AudioPlayer(this, id);
+ this.players.put(id, audio);
+ audio.startRecording(file);
}
/**
* Stop recording and save to the file specified when recording started.
*
- * @param id The id of the audio player
+ * @param id The id of the audio player
*/
public void stopRecordingAudio(String id) {
- AudioPlayer audio = this.players.get(id);
- if (audio != null) {
- audio.stopRecording();
- this.players.remove(id);
- }
+ AudioPlayer audio = this.players.get(id);
+ if (audio != null) {
+ audio.stopRecording();
+ this.players.remove(id);
+ }
}
-
+
/**
* Start or resume playing audio file.
*
- * @param id The id of the audio player
+ * @param id The id of the audio player
* @param file The name of the audio file.
*/
public void startPlayingAudio(String id, String file) {
- AudioPlayer audio = this.players.get(id);
- if (audio == null) {
- audio = new AudioPlayer(this, id);
- this.players.put(id, audio);
- }
- audio.startPlaying(file);
+ AudioPlayer audio = this.players.get(id);
+ if (audio == null) {
+ audio = new AudioPlayer(this, id);
+ this.players.put(id, audio);
+ }
+ audio.startPlaying(file);
}
/**
* Seek to a location.
*
*
- * @param id The id of the audio player
- * @param miliseconds int: number of milliseconds to skip 1000 = 1 second
+ * @param id The id of the audio player
+ * @param miliseconds int: number of milliseconds to skip 1000 = 1 second
*/
public void seekToAudio(String id, int milliseconds) {
- AudioPlayer audio = this.players.get(id);
- if (audio != null) {
- audio.seekToPlaying(milliseconds);
- }
+ AudioPlayer audio = this.players.get(id);
+ if (audio != null) {
+ audio.seekToPlaying(milliseconds);
+ }
}
-
+
/**
* Pause playing.
*
- * @param id The id of the audio player
+ * @param id The id of the audio player
*/
public void pausePlayingAudio(String id) {
- AudioPlayer audio = this.players.get(id);
- if (audio != null) {
- audio.pausePlaying();
- }
+ AudioPlayer audio = this.players.get(id);
+ if (audio != null) {
+ audio.pausePlaying();
+ }
}
/**
* Stop playing the audio file.
*
- * @param id The id of the audio player
+ * @param id The id of the audio player
*/
public void stopPlayingAudio(String id) {
- AudioPlayer audio = this.players.get(id);
- if (audio != null) {
- audio.stopPlaying();
- //audio.destroy();
- //this.players.remove(id);
- }
+ AudioPlayer audio = this.players.get(id);
+ if (audio != null) {
+ audio.stopPlaying();
+ //audio.destroy();
+ //this.players.remove(id);
+ }
}
-
+
/**
* Get current position of playback.
*
- * @param id The id of the audio player
+ * @param id The id of the audio player
* @return position in msec
*/
public float getCurrentPositionAudio(String id) {
- AudioPlayer audio = this.players.get(id);
- if (audio != null) {
- return(audio.getCurrentPosition()/1000.0f);
- }
- return -1;
+ AudioPlayer audio = this.players.get(id);
+ if (audio != null) {
+ return (audio.getCurrentPosition() / 1000.0f);
+ }
+ return -1;
}
-
+
/**
* Get the duration of the audio file.
*
- * @param id The id of the audio player
+ * @param id The id of the audio player
* @param file The name of the audio file.
* @return The duration in msec.
*/
public float getDurationAudio(String id, String file) {
-
- // Get audio file
- AudioPlayer audio = this.players.get(id);
- if (audio != null) {
- return(audio.getDuration(file));
- }
-
- // If not already open, then open the file
- else {
- audio = new AudioPlayer(this, id);
- this.players.put(id, audio);
- return(audio.getDuration(file));
- }
- }
-
+
+ // Get audio file
+ AudioPlayer audio = this.players.get(id);
+ if (audio != null) {
+ return (audio.getDuration(file));
+ }
+
+ // If not already open, then open the file
+ else {
+ audio = new AudioPlayer(this, id);
+ this.players.put(id, audio);
+ return (audio.getDuration(file));
+ }
+ }
+
/**
* Set the audio device to be used for playback.
*
* @param output 1=earpiece, 2=speaker
*/
+ @SuppressWarnings("deprecation")
public void setAudioOutputDevice(int output) {
- AudioManager audiMgr = (AudioManager) this.ctx.getSystemService(Context.AUDIO_SERVICE);
- if (output == 2) {
- audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
- }
- else if (output == 1) {
- audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
- }
- else {
- System.out.println("AudioHandler.setAudioOutputDevice() Error: Unknown output device.");
- }
+ AudioManager audiMgr = (AudioManager) this.ctx.getActivity().getSystemService(Context.AUDIO_SERVICE);
+ if (output == 2) {
+ audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
+ }
+ else if (output == 1) {
+ audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
+ }
+ else {
+ System.out.println("AudioHandler.setAudioOutputDevice() Error: Unknown output device.");
+ }
}
-
+
/**
* Get the audio device to be used for playback.
*
* @return 1=earpiece, 2=speaker
*/
+ @SuppressWarnings("deprecation")
public int getAudioOutputDevice() {
- AudioManager audiMgr = (AudioManager) this.ctx.getSystemService(Context.AUDIO_SERVICE);
- if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) {
- return 1;
- }
- else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) {
- return 2;
- }
- else {
- return -1;
- }
+ AudioManager audiMgr = (AudioManager) this.ctx.getActivity().getSystemService(Context.AUDIO_SERVICE);
+ if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) {
+ return 1;
+ }
+ else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) {
+ return 2;
+ }
+ else {
+ return -1;
+ }
}
/**
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/AudioPlayer.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AudioPlayer.java b/framework/src/org/apache/cordova/AudioPlayer.java
index cb97453..72efcda 100755
--- a/framework/src/org/apache/cordova/AudioPlayer.java
+++ b/framework/src/org/apache/cordova/AudioPlayer.java
@@ -45,413 +45,411 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
private static final String LOG_TAG = "AudioPlayer";
// AudioPlayer states
- public static int MEDIA_NONE = 0;
- public static int MEDIA_STARTING = 1;
- public static int MEDIA_RUNNING = 2;
- public static int MEDIA_PAUSED = 3;
- public static int MEDIA_STOPPED = 4;
-
- // AudioPlayer message ids
- private static int MEDIA_STATE = 1;
- private static int MEDIA_DURATION = 2;
+ public static int MEDIA_NONE = 0;
+ public static int MEDIA_STARTING = 1;
+ public static int MEDIA_RUNNING = 2;
+ public static int MEDIA_PAUSED = 3;
+ public static int MEDIA_STOPPED = 4;
+
+ // AudioPlayer message ids
+ private static int MEDIA_STATE = 1;
+ private static int MEDIA_DURATION = 2;
private static int MEDIA_POSITION = 3;
- private static int MEDIA_ERROR = 9;
-
- // Media error codes
- private static int MEDIA_ERR_NONE_ACTIVE = 0;
- private static int MEDIA_ERR_ABORTED = 1;
- private static int MEDIA_ERR_NETWORK = 2;
- private static int MEDIA_ERR_DECODE = 3;
- private static int MEDIA_ERR_NONE_SUPPORTED = 4;
-
- private AudioHandler handler; // The AudioHandler object
- private String id; // The id of this player (used to identify Media object in JavaScript)
- private int state = MEDIA_NONE; // State of recording or playback
- private String audioFile = null; // File name to play or record to
- private float duration = -1; // Duration of audio
-
- private MediaRecorder recorder = null; // Audio recording object
- private String tempFile = null; // Temporary recording file name
-
- private MediaPlayer mPlayer = null; // Audio player object
- private boolean prepareOnly = false;
-
- /**
- * Constructor.
- *
- * @param handler The audio handler object
- * @param id The id of this audio player
- */
- public AudioPlayer(AudioHandler handler, String id) {
- this.handler = handler;
- this.id = id;
+ private static int MEDIA_ERROR = 9;
+
+ // Media error codes
+ private static int MEDIA_ERR_NONE_ACTIVE = 0;
+ private static int MEDIA_ERR_ABORTED = 1;
+// private static int MEDIA_ERR_NETWORK = 2;
+// private static int MEDIA_ERR_DECODE = 3;
+// private static int MEDIA_ERR_NONE_SUPPORTED = 4;
+
+ private AudioHandler handler; // The AudioHandler object
+ private String id; // The id of this player (used to identify Media object in JavaScript)
+ private int state = MEDIA_NONE; // State of recording or playback
+ private String audioFile = null; // File name to play or record to
+ private float duration = -1; // Duration of audio
+
+ private MediaRecorder recorder = null; // Audio recording object
+ private String tempFile = null; // Temporary recording file name
+
+ private MediaPlayer mPlayer = null; // Audio player object
+ private boolean prepareOnly = false;
+
+ /**
+ * Constructor.
+ *
+ * @param handler The audio handler object
+ * @param id The id of this audio player
+ */
+ public AudioPlayer(AudioHandler handler, String id) {
+ this.handler = handler;
+ this.id = id;
this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.mp3";
- }
-
- /**
- * Destroy player and stop audio playing or recording.
- */
- public void destroy() {
-
- // Stop any play or record
- if (this.mPlayer != null) {
- if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
- this.mPlayer.stop();
- this.setState(MEDIA_STOPPED);
- }
- this.mPlayer.release();
- this.mPlayer = null;
- }
- if (this.recorder != null) {
- this.stopRecording();
- this.recorder.release();
- this.recorder = null;
- }
- }
-
- /**
- * Start recording the specified file.
- *
- * @param file The name of the file
- */
- public void startRecording(String file) {
- if (this.mPlayer != null) {
- Log.d(LOG_TAG, "AudioPlayer Error: Can't record in play mode.");
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
- }
-
- // Make sure we're not already recording
- else if (this.recorder == null) {
- this.audioFile = file;
- this.recorder = new MediaRecorder();
- this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // THREE_GPP);
- this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //AMR_NB);
- this.recorder.setOutputFile(this.tempFile);
- try {
- this.recorder.prepare();
- this.recorder.start();
- this.setState(MEDIA_RUNNING);
- return;
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
- }
- else {
- Log.d(LOG_TAG, "AudioPlayer Error: Already recording.");
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
- }
- }
-
- /**
- * Save temporary recorded file to specified name
- *
- * @param file
- */
- public void moveFile(String file) {
-
- /* this is a hack to save the file as the specified name */
- File f = new File(this.tempFile);
- f.renameTo(new File("/sdcard/" + file));
- }
-
+ }
+
+ /**
+ * Destroy player and stop audio playing or recording.
+ */
+ public void destroy() {
+
+ // Stop any play or record
+ if (this.mPlayer != null) {
+ if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
+ this.mPlayer.stop();
+ this.setState(MEDIA_STOPPED);
+ }
+ this.mPlayer.release();
+ this.mPlayer = null;
+ }
+ if (this.recorder != null) {
+ this.stopRecording();
+ this.recorder.release();
+ this.recorder = null;
+ }
+ }
+
+ /**
+ * Start recording the specified file.
+ *
+ * @param file The name of the file
+ */
+ public void startRecording(String file) {
+ if (this.mPlayer != null) {
+ Log.d(LOG_TAG, "AudioPlayer Error: Can't record in play mode.");
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+ }
+
+ // Make sure we're not already recording
+ else if (this.recorder == null) {
+ this.audioFile = file;
+ this.recorder = new MediaRecorder();
+ this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
+ this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // THREE_GPP);
+ this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //AMR_NB);
+ this.recorder.setOutputFile(this.tempFile);
+ try {
+ this.recorder.prepare();
+ this.recorder.start();
+ this.setState(MEDIA_RUNNING);
+ return;
+ } catch (IllegalStateException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+ }
+ else {
+ Log.d(LOG_TAG, "AudioPlayer Error: Already recording.");
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+ }
+ }
+
+ /**
+ * Save temporary recorded file to specified name
+ *
+ * @param file
+ */
+ public void moveFile(String file) {
+
+ /* this is a hack to save the file as the specified name */
+ File f = new File(this.tempFile);
+ f.renameTo(new File("/sdcard/" + file));
+ }
+
/**
* Stop recording and save to the file specified when recording started.
*/
- public void stopRecording() {
- if (this.recorder != null) {
- try{
- if (this.state == MEDIA_RUNNING) {
- this.recorder.stop();
- this.setState(MEDIA_STOPPED);
- }
- this.moveFile(this.audioFile);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
+ public void stopRecording() {
+ if (this.recorder != null) {
+ try {
+ if (this.state == MEDIA_RUNNING) {
+ this.recorder.stop();
+ this.setState(MEDIA_STOPPED);
+ }
+ this.moveFile(this.audioFile);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
/**
* Start or resume playing audio file.
*
* @param file The name of the audio file.
*/
- public void startPlaying(String file) {
- if (this.recorder != null) {
- Log.d(LOG_TAG, "AudioPlayer Error: Can't play in record mode.");
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
- }
-
- // If this is a new request to play audio, or stopped
- else if ((this.mPlayer == null) || (this.state == MEDIA_STOPPED)) {
- try {
- // If stopped, then reset player
- if (this.mPlayer != null) {
- this.mPlayer.reset();
- }
- // Otherwise, create a new one
- else {
- this.mPlayer = new MediaPlayer();
- }
- this.audioFile = file;
-
- // If streaming file
- if (this.isStreaming(file)) {
- this.mPlayer.setDataSource(file);
- this.mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- this.setState(MEDIA_STARTING);
- this.mPlayer.setOnPreparedListener(this);
- this.mPlayer.prepareAsync();
- }
-
- // If local file
- else {
- if (file.startsWith("/android_asset/")) {
- String f = file.substring(15);
- android.content.res.AssetFileDescriptor fd = this.handler.ctx.getAssets().openFd(f);
- this.mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
- }
+ public void startPlaying(String file) {
+ if (this.recorder != null) {
+ Log.d(LOG_TAG, "AudioPlayer Error: Can't play in record mode.");
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+ }
+
+ // If this is a new request to play audio, or stopped
+ else if ((this.mPlayer == null) || (this.state == MEDIA_STOPPED)) {
+ try {
+ // If stopped, then reset player
+ if (this.mPlayer != null) {
+ this.mPlayer.reset();
+ }
+ // Otherwise, create a new one
+ else {
+ this.mPlayer = new MediaPlayer();
+ }
+ this.audioFile = file;
+
+ // If streaming file
+ if (this.isStreaming(file)) {
+ this.mPlayer.setDataSource(file);
+ this.mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
+ this.setState(MEDIA_STARTING);
+ this.mPlayer.setOnPreparedListener(this);
+ this.mPlayer.prepareAsync();
+ }
+
+ // If local file
+ else {
+ if (file.startsWith("/android_asset/")) {
+ String f = file.substring(15);
+ android.content.res.AssetFileDescriptor fd = this.handler.ctx.getActivity().getAssets().openFd(f);
+ this.mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
+ }
else {
File fp = new File(file);
if (fp.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
this.mPlayer.setDataSource(fileInputStream.getFD());
- }
+ }
else {
this.mPlayer.setDataSource("/sdcard/" + file);
}
}
- this.setState(MEDIA_STARTING);
- this.mPlayer.setOnPreparedListener(this);
- this.mPlayer.prepare();
-
- // Get duration
- this.duration = getDurationInSeconds();
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
- }
- }
-
- // If we have already have created an audio player
- else {
-
- // If player has been paused, then resume playback
- if ((this.state == MEDIA_PAUSED) || (this.state == MEDIA_STARTING)) {
- this.mPlayer.start();
- this.setState(MEDIA_RUNNING);
- }
- else {
- Log.d(LOG_TAG, "AudioPlayer Error: startPlaying() called during invalid state: "+this.state);
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
- }
- }
- }
-
- /**
- * Seek or jump to a new time in the track.
- */
- public void seekToPlaying(int milliseconds) {
- if (this.mPlayer != null) {
- this.mPlayer.seekTo(milliseconds);
- Log.d(LOG_TAG, "Send a onStatus update for the new seek");
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_POSITION+", "+milliseconds/1000.0f+");");
- }
- }
-
- /**
- * Pause playing.
- */
- public void pausePlaying() {
-
- // If playing, then pause
- if (this.state == MEDIA_RUNNING) {
- this.mPlayer.pause();
- this.setState(MEDIA_PAUSED);
- }
- else {
- Log.d(LOG_TAG, "AudioPlayer Error: pausePlaying() called during invalid state: "+this.state);
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_NONE_ACTIVE+"});");
- }
- }
+ this.setState(MEDIA_STARTING);
+ this.mPlayer.setOnPreparedListener(this);
+ this.mPlayer.prepare();
+
+ // Get duration
+ this.duration = getDurationInSeconds();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+ }
+ }
+
+ // If we have already have created an audio player
+ else {
+
+ // If player has been paused, then resume playback
+ if ((this.state == MEDIA_PAUSED) || (this.state == MEDIA_STARTING)) {
+ this.mPlayer.start();
+ this.setState(MEDIA_RUNNING);
+ }
+ else {
+ Log.d(LOG_TAG, "AudioPlayer Error: startPlaying() called during invalid state: " + this.state);
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+ }
+ }
+ }
+
+ /**
+ * Seek or jump to a new time in the track.
+ */
+ public void seekToPlaying(int milliseconds) {
+ if (this.mPlayer != null) {
+ this.mPlayer.seekTo(milliseconds);
+ Log.d(LOG_TAG, "Send a onStatus update for the new seek");
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + milliseconds / 1000.0f + ");");
+ }
+ }
+
+ /**
+ * Pause playing.
+ */
+ public void pausePlaying() {
+
+ // If playing, then pause
+ if (this.state == MEDIA_RUNNING) {
+ this.mPlayer.pause();
+ this.setState(MEDIA_PAUSED);
+ }
+ else {
+ Log.d(LOG_TAG, "AudioPlayer Error: pausePlaying() called during invalid state: " + this.state);
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
+ }
+ }
/**
* Stop playing the audio file.
*/
- public void stopPlaying() {
- if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
- this.mPlayer.stop();
- this.setState(MEDIA_STOPPED);
- }
- else {
- Log.d(LOG_TAG, "AudioPlayer Error: stopPlaying() called during invalid state: "+this.state);
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_NONE_ACTIVE+"});");
- }
- }
-
- /**
- * Callback to be invoked when playback of a media source has completed.
- *
- * @param mPlayer The MediaPlayer that reached the end of the file
- */
- public void onCompletion(MediaPlayer mPlayer) {
- this.setState(MEDIA_STOPPED);
- }
-
+ public void stopPlaying() {
+ if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
+ this.mPlayer.stop();
+ this.setState(MEDIA_STOPPED);
+ }
+ else {
+ Log.d(LOG_TAG, "AudioPlayer Error: stopPlaying() called during invalid state: " + this.state);
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
+ }
+ }
+
+ /**
+ * Callback to be invoked when playback of a media source has completed.
+ *
+ * @param mPlayer The MediaPlayer that reached the end of the file
+ */
+ public void onCompletion(MediaPlayer mPlayer) {
+ this.setState(MEDIA_STOPPED);
+ }
+
/**
* Get current position of playback.
*
* @return position in msec or -1 if not playing
*/
- public long getCurrentPosition() {
- if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
- int curPos = this.mPlayer.getCurrentPosition();
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_POSITION+", "+curPos/1000.0f+");");
- return curPos;
- }
- else {
- return -1;
- }
- }
-
- /**
- * Determine if playback file is streaming or local.
- * It is streaming if file name starts with "http://"
- *
- * @param file The file name
- * @return T=streaming, F=local
- */
- public boolean isStreaming(String file) {
- if (file.contains("http://") || file.contains("https://")) {
- return true;
- }
- else {
- return false;
- }
- }
-
- /**
- * Get the duration of the audio file.
+ public long getCurrentPosition() {
+ if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
+ int curPos = this.mPlayer.getCurrentPosition();
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + curPos / 1000.0f + ");");
+ return curPos;
+ }
+ else {
+ return -1;
+ }
+ }
+
+ /**
+ * Determine if playback file is streaming or local.
+ * It is streaming if file name starts with "http://"
*
- * @param file The name of the audio file.
- * @return The duration in msec.
- * -1=can't be determined
- * -2=not allowed
+ * @param file The file name
+ * @return T=streaming, F=local
+ */
+ public boolean isStreaming(String file) {
+ if (file.contains("http://") || file.contains("https://")) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ /**
+ * Get the duration of the audio file.
+ *
+ * @param file The name of the audio file.
+ * @return The duration in msec.
+ * -1=can't be determined
+ * -2=not allowed
+ */
+ public float getDuration(String file) {
+
+ // Can't get duration of recording
+ if (this.recorder != null) {
+ return (-2); // not allowed
+ }
+
+ // If audio file already loaded and started, then return duration
+ if (this.mPlayer != null) {
+ return this.duration;
+ }
+
+ // If no player yet, then create one
+ else {
+ this.prepareOnly = true;
+ this.startPlaying(file);
+
+ // This will only return value for local, since streaming
+ // file hasn't been read yet.
+ return this.duration;
+ }
+ }
+
+ /**
+ * Callback to be invoked when the media source is ready for playback.
+ *
+ * @param mPlayer The MediaPlayer that is ready for playback
+ */
+ public void onPrepared(MediaPlayer mPlayer) {
+ // Listen for playback completion
+ this.mPlayer.setOnCompletionListener(this);
+
+ // If start playing after prepared
+ if (!this.prepareOnly) {
+
+ // Start playing
+ this.mPlayer.start();
+
+ // Set player init flag
+ this.setState(MEDIA_RUNNING);
+ }
+
+ // Save off duration
+ this.duration = getDurationInSeconds();
+ this.prepareOnly = false;
+
+ // Send status notification to JavaScript
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_DURATION + "," + this.duration + ");");
+
+ }
+
+ /**
+ * By default Android returns the length of audio in mills but we want seconds
+ *
+ * @return length of clip in seconds
+ */
+ private float getDurationInSeconds() {
+ return (this.mPlayer.getDuration() / 1000.0f);
+ }
+
+ /**
+ * Callback to be invoked when there has been an error during an asynchronous operation
+ * (other errors will throw exceptions at method call time).
+ *
+ * @param mPlayer the MediaPlayer the error pertains to
+ * @param arg1 the type of error that has occurred: (MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_SERVER_DIED)
+ * @param arg2 an extra code, specific to the error.
+ */
+ public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) {
+ Log.d(LOG_TAG, "AudioPlayer.onError(" + arg1 + ", " + arg2 + ")");
+
+ // TODO: Not sure if this needs to be sent?
+ this.mPlayer.stop();
+ this.mPlayer.release();
+
+ // Send error notification to JavaScript
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', { \"code\":" + arg1 + "});");
+ return false;
+ }
+
+ /**
+ * Set the state and send it to JavaScript.
+ *
+ * @param state
+ */
+ private void setState(int state) {
+ if (this.state != state) {
+ this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_STATE + ", " + state + ");");
+ }
+
+ this.state = state;
+ }
+
+ /**
+ * Get the audio state.
+ *
+ * @return int
+ */
+ public int getState() {
+ return this.state;
+ }
+
+ /**
+ * Set the volume for audio player
+ *
+ * @param volume
*/
- public float getDuration(String file) {
-
- // Can't get duration of recording
- if (this.recorder != null) {
- return(-2); // not allowed
- }
-
- // If audio file already loaded and started, then return duration
- if (this.mPlayer != null) {
- return this.duration;
- }
-
- // If no player yet, then create one
- else {
- this.prepareOnly = true;
- this.startPlaying(file);
-
- // This will only return value for local, since streaming
- // file hasn't been read yet.
- return this.duration;
- }
- }
-
- /**
- * Callback to be invoked when the media source is ready for playback.
- *
- * @param mPlayer The MediaPlayer that is ready for playback
- */
- public void onPrepared(MediaPlayer mPlayer) {
- // Listen for playback completion
- this.mPlayer.setOnCompletionListener(this);
-
- // If start playing after prepared
- if (!this.prepareOnly) {
-
- // Start playing
- this.mPlayer.start();
-
- // Set player init flag
- this.setState(MEDIA_RUNNING);
- }
-
- // Save off duration
- this.duration = getDurationInSeconds();
- this.prepareOnly = false;
-
- // Send status notification to JavaScript
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_DURATION+","+this.duration+");");
-
- }
-
- /**
- * By default Android returns the length of audio in mills but we want seconds
- *
- * @return length of clip in seconds
- */
- private float getDurationInSeconds() {
- return (this.mPlayer.getDuration() / 1000.0f);
- }
-
- /**
- * Callback to be invoked when there has been an error during an asynchronous operation
- * (other errors will throw exceptions at method call time).
- *
- * @param mPlayer the MediaPlayer the error pertains to
- * @param arg1 the type of error that has occurred: (MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_SERVER_DIED)
- * @param arg2 an extra code, specific to the error.
- */
- public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) {
- Log.d(LOG_TAG, "AudioPlayer.onError(" + arg1 + ", " + arg2+")");
-
- // TODO: Not sure if this needs to be sent?
- this.mPlayer.stop();
- this.mPlayer.release();
-
- // Send error notification to JavaScript
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', { \"code\":"+arg1+"});");
- return false;
- }
-
- /**
- * Set the state and send it to JavaScript.
- *
- * @param state
- */
- private void setState(int state) {
- if (this.state != state) {
- this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_STATE+", "+state+");");
- }
-
- this.state = state;
- }
-
- /**
- * Get the audio state.
- *
- * @return int
- */
- public int getState() {
- return this.state;
- }
-
- /**
- * Set the volume for audio player
- *
- * @param volume
- */
public void setVolume(float volume) {
this.mPlayer.setVolume(volume, volume);
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/BatteryListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/BatteryListener.java b/framework/src/org/apache/cordova/BatteryListener.java
index 84024b2..43d820b 100755
--- a/framework/src/org/apache/cordova/BatteryListener.java
+++ b/framework/src/org/apache/cordova/BatteryListener.java
@@ -24,7 +24,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -32,20 +31,20 @@ import android.content.IntentFilter;
import android.util.Log;
public class BatteryListener extends Plugin {
-
+
private static final String LOG_TAG = "BatteryManager";
BroadcastReceiver receiver;
private String batteryCallbackId = null;
-
+
/**
* Constructor.
*/
public BatteryListener() {
this.receiver = null;
}
-
+
/**
* Executes the request and returns PluginResult.
*
@@ -56,43 +55,43 @@ public class BatteryListener extends Plugin {
*/
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.INVALID_ACTION;
- String result = "Unsupported Operation: " + action;
-
+ String result = "Unsupported Operation: " + action;
+
if (action.equals("start")) {
- if (this.batteryCallbackId != null) {
- return new PluginResult(PluginResult.Status.ERROR, "Battery listener already running.");
- }
+ if (this.batteryCallbackId != null) {
+ return new PluginResult(PluginResult.Status.ERROR, "Battery listener already running.");
+ }
this.batteryCallbackId = callbackId;
// We need to listen to power events to update battery status
- IntentFilter intentFilter = new IntentFilter() ;
+ IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
if (this.receiver == null) {
this.receiver = new BroadcastReceiver() {
@Override
- public void onReceive(Context context, Intent intent) {
- updateBatteryInfo(intent);
+ public void onReceive(Context context, Intent intent) {
+ updateBatteryInfo(intent);
}
};
- ctx.registerReceiver(this.receiver, intentFilter);
+ ctx.getActivity().registerReceiver(this.receiver, intentFilter);
}
// Don't return any result now, since status results will be sent when events come in from broadcast receiver
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
return pluginResult;
- }
-
+ }
+
else if (action.equals("stop")) {
removeBatteryListener();
this.sendUpdate(new JSONObject(), false); // release status callback in JS side
this.batteryCallbackId = null;
return new PluginResult(PluginResult.Status.OK);
}
-
+
return new PluginResult(status, result);
}
-
+
/**
* Stop battery receiver.
*/
@@ -106,7 +105,7 @@ public class BatteryListener extends Plugin {
private void removeBatteryListener() {
if (this.receiver != null) {
try {
- this.ctx.unregisterReceiver(this.receiver);
+ this.ctx.getActivity().unregisterReceiver(this.receiver);
this.receiver = null;
} catch (Exception e) {
Log.e(LOG_TAG, "Error unregistering battery receiver: " + e.getMessage(), e);
@@ -137,20 +136,20 @@ public class BatteryListener extends Plugin {
* @param batteryIntent the current battery information
* @return
*/
- private void updateBatteryInfo(Intent batteryIntent) {
+ private void updateBatteryInfo(Intent batteryIntent) {
sendUpdate(this.getBatteryInfo(batteryIntent), true);
}
-
+
/**
* Create a new plugin result and send it back to JavaScript
*
* @param connection the network info to set as navigator.connection
*/
private void sendUpdate(JSONObject info, boolean keepCallback) {
- if (this.batteryCallbackId != null) {
- PluginResult result = new PluginResult(PluginResult.Status.OK, info);
- result.setKeepCallback(keepCallback);
- this.success(result, this.batteryCallbackId);
- }
+ if (this.batteryCallbackId != null) {
+ PluginResult result = new PluginResult(PluginResult.Status.OK, info);
+ result.setKeepCallback(keepCallback);
+ this.success(result, this.batteryCallbackId);
+ }
}
}