incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [16/18] android commit: Port AudioHandler and AudioPlayer to CordovaPlugin.
Date Fri, 12 Oct 2012 21:06:18 GMT
Port AudioHandler and AudioPlayer to CordovaPlugin.

Had to adapt AudioPlayer because CordovaPlugin doesn't define
sendJavascript.


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/10510484
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/10510484
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/10510484

Branch: refs/heads/master
Commit: 10510484b5a9f9c7fb7f11a9a4c3f4992dd95a94
Parents: e1dea5b
Author: Braden Shepherdson <braden@chromium.org>
Authored: Thu Oct 11 11:09:12 2012 -0400
Committer: Braden Shepherdson <braden@chromium.org>
Committed: Thu Oct 11 11:09:12 2012 -0400

----------------------------------------------------------------------
 framework/src/org/apache/cordova/AudioHandler.java |   42 +++++++--------
 framework/src/org/apache/cordova/AudioPlayer.java  |   34 ++++++------
 2 files changed, 35 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/10510484/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 9ee226a..5872ddf 100644
--- a/framework/src/org/apache/cordova/AudioHandler.java
+++ b/framework/src/org/apache/cordova/AudioHandler.java
@@ -18,12 +18,14 @@
 */
 package org.apache.cordova;
 
+import org.apache.cordova.api.CallbackContext;
+import org.apache.cordova.api.CordovaPlugin;
+
 import android.content.Context;
 import android.media.AudioManager;
 
 import java.util.ArrayList;
 
-import org.apache.cordova.api.Plugin;
 import org.apache.cordova.api.PluginResult;
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -40,7 +42,7 @@ import java.util.HashMap;
  * 		android_asset: 		file name must start with /android_asset/sound.mp3
  * 		sdcard:				file name is just sound.mp3
  */
-public class AudioHandler extends Plugin {
+public class AudioHandler extends CordovaPlugin {
 
     public static String TAG = "AudioHandler";
     HashMap<String, AudioPlayer> players;	// Audio player object
@@ -58,10 +60,10 @@ public class AudioHandler extends Plugin {
      * 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.
+     * @param callbackContext		The callback context used when calling back into JavaScript.
      * @return 				A PluginResult object with a status and message.
      */
-    public PluginResult execute(String action, JSONArray args, String callbackId) {
+    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
{
         PluginResult.Status status = PluginResult.Status.OK;
         String result = "";
 
@@ -91,11 +93,13 @@ public class AudioHandler extends Plugin {
                }
             } else if (action.equals("getCurrentPositionAudio")) {
                 float f = this.getCurrentPositionAudio(args.getString(0));
-                return new PluginResult(status, f);
+                callbackContext.sendPluginResult(new PluginResult(status, f));
+                return true;
             }
             else if (action.equals("getDurationAudio")) {
                 float f = this.getDurationAudio(args.getString(0), args.getString(1));
-                return new PluginResult(status, f);
+                callbackContext.sendPluginResult(new PluginResult(status, f));
+                return true;
             }
             else if (action.equals("create")) {
                 String id = args.getString(0);
@@ -105,28 +109,20 @@ public class AudioHandler extends Plugin {
             }
             else if (action.equals("release")) {
                 boolean b = this.release(args.getString(0));
-                return new PluginResult(status, b);
+                callbackContext.sendPluginResult(new PluginResult(status, b));
+                return true;
+            }
+            else { // Unrecognized action.
+                return false;
             }
-            return new PluginResult(status, result);
+
+            callbackContext.sendPluginResult(new PluginResult(status, result));
         } catch (JSONException e) {
             e.printStackTrace();
-            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
+            callbackContext.sendPluginResult(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;
+        return true;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/10510484/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 c0d6b93..99dbb3d 100644
--- a/framework/src/org/apache/cordova/AudioPlayer.java
+++ b/framework/src/org/apache/cordova/AudioPlayer.java
@@ -31,8 +31,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 
-import org.apache.cordova.AudioPlayer.MODE;
-
 /**
  * This class implements the audio playback and recording capabilities used by Cordova.
  * It is called by the AudioHandler Cordova class.
@@ -135,7 +133,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         switch (this.mode) {
         case PLAY:
             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+"});");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
             break;
         case NONE:
             this.audioFile = file;
@@ -153,11 +151,11 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
             } catch (IOException e) {
                 e.printStackTrace();
             }
-            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
             break;
         case RECORD:
             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+"});");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
         }
     }
 
@@ -223,7 +221,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         if (this.readyPlayer(this.audioFile)) {
             this.player.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 + ");");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_POSITION + ", " + milliseconds / 1000.0f + ");");
         }
         else {
             this.seekOnPrepared = milliseconds;
@@ -242,7 +240,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         }
         else {
             Log.d(LOG_TAG, "AudioPlayer Error: pausePlaying() called during invalid state:
" + this.state.ordinal());
-            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
         }
     }
 
@@ -258,7 +256,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         }
         else {
             Log.d(LOG_TAG, "AudioPlayer Error: stopPlaying() called during invalid state:
" + this.state.ordinal());
-            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
         }
     }
 
@@ -280,7 +278,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
     public long getCurrentPosition() {
         if ((this.state == STATE.MEDIA_RUNNING) || (this.state == STATE.MEDIA_PAUSED)) {
             int curPos = this.player.getCurrentPosition();
-            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_POSITION + ", " + curPos / 1000.0f + ");");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_POSITION + ", " + curPos / 1000.0f + ");");
             return curPos;
         }
         else {
@@ -359,7 +357,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         this.prepareOnly = true;
 
         // Send status notification to JavaScript
-        this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_DURATION + "," + this.duration + ");");
+        this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_DURATION + "," + this.duration + ");");
     }
 
     /**
@@ -387,7 +385,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         this.player.release();
 
         // Send error notification to JavaScript
-        this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', { \"code\":" + arg1 + "});");
+        this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', { \"code\":" + arg1 + "});");
         return false;
     }
 
@@ -398,7 +396,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
      */
     private void setState(STATE state) {
         if (this.state != state) {
-            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_STATE + ", " + state.ordinal() + ");");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_STATE + ", " + state.ordinal() + ");");
         }
         this.state = state;
     }
@@ -411,7 +409,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
     private void setMode(MODE mode) {
         if (this.mode != mode) {
             //mode is not part of the expected behavior, so no notification
-            //this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_STATE + ", " + mode + ");");
+            //this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_STATE + ", " + mode + ");");
         }
         this.mode = mode;
     }
@@ -447,7 +445,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
             break;
         case RECORD:
             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 + "});");
+            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
             return false; //player is not ready
         }
         return true;
@@ -468,7 +466,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
                     try {
                         this.loadAudioFile(file);
                     } catch (Exception e) {
-                        this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
+                        this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
                     }
                     return false;
                 case MEDIA_LOADING:
@@ -493,14 +491,14 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
                         try {
                             this.loadAudioFile(file);
                         } catch (Exception e) {
-                            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
+                            this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
                         }
                         //if we had to prepare= the file, we won't be in the correct state
for playback
                         return false;
                     }
                 default:
                     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 + "});");
+                    this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('"
+ this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
             }
         }
         return false;
@@ -526,7 +524,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener,
On
         else {
             if (file.startsWith("/android_asset/")) {
                 String f = file.substring(15);
-                android.content.res.AssetFileDescriptor fd = this.handler.ctx.getActivity().getAssets().openFd(f);
+                android.content.res.AssetFileDescriptor fd = this.handler.cordova.getActivity().getAssets().openFd(f);
                 this.player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
             }
             else {


Mime
View raw message