You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2013/03/14 23:30:43 UTC

[3/5] wp7 commit: Remove CordovaMediaonStatus, and CordovaCommandResult

Remove CordovaMediaonStatus, and CordovaCommandResult


Project: http://git-wip-us.apache.org/repos/asf/cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp7/commit/acdef7af
Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp7/tree/acdef7af
Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp7/diff/acdef7af

Branch: refs/heads/master
Commit: acdef7aff2f4a0c53002dddcac71511b819d02e7
Parents: 65443b1
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Thu Mar 14 15:11:21 2013 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Thu Mar 14 15:11:21 2013 -0700

----------------------------------------------------------------------
 .../standalone/cordovalib/Commands/AudioPlayer.cs  |  106 ++++++++++++---
 .../standalone/cordovalib/Commands/BaseCommand.cs  |    2 +-
 .../standalone/cordovalib/CordovaView.xaml.cs      |    4 +-
 templates/standalone/cordovalib/NativeExecution.cs |    2 +-
 4 files changed, 89 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-wp7/blob/acdef7af/templates/standalone/cordovalib/Commands/AudioPlayer.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/AudioPlayer.cs b/templates/standalone/cordovalib/Commands/AudioPlayer.cs
index ffc6a94..a83be5b 100644
--- a/templates/standalone/cordovalib/Commands/AudioPlayer.cs
+++ b/templates/standalone/cordovalib/Commands/AudioPlayer.cs
@@ -23,6 +23,7 @@ using Microsoft.Xna.Framework.Audio;
 using Microsoft.Xna.Framework.Media;
 using Microsoft.Phone.Controls;
 using System.Diagnostics;
+using System.Windows.Resources;
 
 namespace WPCordovaClassLib.Cordova.Commands
 {
@@ -57,7 +58,7 @@ namespace WPCordovaClassLib.Cordova.Commands
         private const int MediaErrorStopState = 8;
 
         //TODO: get rid of this callback, it should be universal
-        private const string CallbackFunction = "CordovaMediaonStatus";
+        //private const string CallbackFunction = "CordovaMediaonStatus";
 
         #endregion
 
@@ -132,7 +133,6 @@ namespace WPCordovaClassLib.Cordova.Commands
         /// </summary>
         public void Dispose()
         {
-            Debug.WriteLine("Dispose :: " + this.audioFile);
             if (this.player != null)
             {
                 this.stopPlaying();
@@ -147,6 +147,33 @@ namespace WPCordovaClassLib.Cordova.Commands
             this.FinalizeXnaGameLoop();
         }
 
+        private void InvokeCallback(int message, string value, bool removeHandler)
+        {
+            string args = string.Format("('{0}',{1},{2});", this.id, message, value);
+            string callback = @"(function(id,msg,value){
+                try {
+                    if (msg == Media.MEDIA_ERROR) {
+                        value = {'code':value};
+                    }
+                    Media.onStatus(id,msg,value);
+                }
+                catch(e) {
+                    console.log('Error calling Media.onStatus :: ' + e);
+                }
+            })" + args;
+            this.handler.InvokeCustomScript(new ScriptCallback("eval", new string[] { callback }), false);
+        }
+
+        private void InvokeCallback(int message, int value, bool removeHandler)
+        {
+            InvokeCallback(message, value.ToString(), removeHandler);
+        }
+
+        private void InvokeCallback(int message, double value, bool removeHandler)
+        {
+            InvokeCallback(message, value.ToString(), removeHandler);
+        }
+
         /// <summary>
         /// Starts recording, data is stored in memory
         /// </summary>
@@ -155,7 +182,7 @@ namespace WPCordovaClassLib.Cordova.Commands
         {
             if (this.player != null)
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPlayModeSet),false);
+                InvokeCallback(MediaError, MediaErrorPlayModeSet, false);
             }
             else if (this.recorder == null)
             {
@@ -175,13 +202,14 @@ namespace WPCordovaClassLib.Cordova.Commands
                 }
                 catch (Exception)
                 {
-                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingRecording),false);
+                    InvokeCallback(MediaError, MediaErrorStartingRecording, false);
+                    //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingRecording),false);
                 }
             }
             else
             {
-
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorAlreadyRecording),false);
+                InvokeCallback(MediaError, MediaErrorAlreadyRecording, false);
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorAlreadyRecording),false);
             }
         }
 
@@ -223,12 +251,13 @@ namespace WPCordovaClassLib.Cordova.Commands
         {
             if (this.recorder != null)
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorRecordModeSet),false);
+                InvokeCallback(MediaError, MediaErrorRecordModeSet, false);
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorRecordModeSet),false);
                 return;
             }
 
 
-            if (this.player == null || this.player.Source == null || this.player.Source.AbsolutePath.LastIndexOf(filePath) < 0)
+            if (this.player == null || this.player.Source.AbsolutePath.LastIndexOf(filePath) < 0)
             {
                 try
                 {
@@ -243,7 +272,6 @@ namespace WPCordovaClassLib.Cordova.Commands
                             if (grid != null)
                             {
 
-                                //Microsoft.Xna.Framework.Media.MediaPlayer.Play(
                                 this.player = grid.FindName("playerMediaElement") as MediaElement;
                                 if (this.player == null) // still null ?
                                 {
@@ -276,6 +304,37 @@ namespace WPCordovaClassLib.Cordova.Commands
                     {
                         using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                         {
+                            if (!isoFile.FileExists(filePath))
+                            {
+                                // try to unpack it from the dll into isolated storage
+                                StreamResourceInfo fileResourceStreamInfo = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));
+                                if (fileResourceStreamInfo != null)
+                                {
+                                    using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream))
+                                    {
+                                        byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length);          
+
+                                        string[] dirParts = filePath.Split('/');
+                                        string dirName = "";
+                                        for (int n = 0; n < dirParts.Length - 1; n++)
+                                        {
+                                            dirName += dirParts[n] + "/";
+                                        }
+                                        if (!isoFile.DirectoryExists(dirName))
+                                        {
+                                            isoFile.CreateDirectory(dirName);
+                                        }
+
+                                        using (IsolatedStorageFileStream outFile = isoFile.OpenFile(filePath, FileMode.Create))
+                                        {
+                                            using (BinaryWriter writer = new BinaryWriter(outFile))
+                                            {
+                                                writer.Write(data);
+                                            }
+                                        }
+                                    }
+                                }
+                            }
                             if (isoFile.FileExists(filePath))
                             {
                                 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, isoFile))
@@ -285,10 +344,9 @@ namespace WPCordovaClassLib.Cordova.Commands
                             }
                             else
                             {
-                                Debug.WriteLine("Error: source doesn't exist :: " + filePath);
-                                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, 1),false);
+                                InvokeCallback(MediaError, MediaErrorPlayModeSet, false);
+                                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, 1), false);
                                 return;
-                                //throw new ArgumentException("Source doesn't exist");
                             }
                         }
                     }
@@ -296,8 +354,9 @@ namespace WPCordovaClassLib.Cordova.Commands
                 }
                 catch (Exception e)
                 {
-                    Debug.WriteLine("Error: " + e.Message);
-                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingPlayback),false);
+                    Debug.WriteLine("Error in AudioPlayer::startPlaying : " + e.Message);
+                    InvokeCallback(MediaError, MediaErrorStartingPlayback, false);
+                    //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingPlayback),false);
                 }
             }
             else
@@ -309,7 +368,8 @@ namespace WPCordovaClassLib.Cordova.Commands
                 }
                 else
                 {
-                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorResumeState),false);
+                    InvokeCallback(MediaError, MediaErrorResumeState, false);
+                    //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorResumeState),false);
                 }
             }
         }
@@ -322,7 +382,8 @@ namespace WPCordovaClassLib.Cordova.Commands
             if (this.player != null)
             {
                 this.duration = this.player.NaturalDuration.TimeSpan.TotalSeconds;
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaDuration, this.duration),false);
+                InvokeCallback(MediaDuration, this.duration, false);
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaDuration, this.duration),false);
                 if (!this.prepareOnly)
                 {
                     this.player.Play();
@@ -350,7 +411,8 @@ namespace WPCordovaClassLib.Cordova.Commands
         private void MediaFailed(object sender, RoutedEventArgs arg)
         {
             player.Stop();
-            this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), "Media failed"),false);
+            InvokeCallback(MediaError, MediaErrorStartingPlayback, false);
+            //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), "Media failed"),false);
         }
 
         /// <summary>
@@ -363,7 +425,8 @@ namespace WPCordovaClassLib.Cordova.Commands
             {
                 TimeSpan tsPos = new TimeSpan(0, 0, 0, 0, milliseconds);
                 this.player.Position = tsPos;
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, milliseconds / 1000.0f),false);
+                InvokeCallback(MediaPosition, milliseconds / 1000.0f, false);
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, milliseconds / 1000.0f),false);
             }
         }
 
@@ -391,7 +454,8 @@ namespace WPCordovaClassLib.Cordova.Commands
             }
             else
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPauseState),false);
+                InvokeCallback(MediaError, MediaErrorPauseState, false);
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPauseState),false);
             }
         }
 
@@ -465,7 +529,8 @@ namespace WPCordovaClassLib.Cordova.Commands
         {
             if (this.state != state)
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaState, state),false);
+                InvokeCallback(MediaState, state, false);
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaState, state),false);
             }
 
             this.state = state;
@@ -523,7 +588,6 @@ namespace WPCordovaClassLib.Cordova.Commands
             }
         }    
 
-
         #region Xna loop
         /// <summary>
         /// Special initialization required for the microphone: XNA game loop

http://git-wip-us.apache.org/repos/asf/cordova-wp7/blob/acdef7af/templates/standalone/cordovalib/Commands/BaseCommand.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/BaseCommand.cs b/templates/standalone/cordovalib/Commands/BaseCommand.cs
index f9942eb..237b72a 100644
--- a/templates/standalone/cordovalib/Commands/BaseCommand.cs
+++ b/templates/standalone/cordovalib/Commands/BaseCommand.cs
@@ -71,7 +71,7 @@ namespace WPCordovaClassLib.Cordova.Commands
 
         }
 
-
+        [Obsolete]
         public void InvokeCustomScript(ScriptCallback script, bool removeHandler)
         {
             if (this.OnCustomScript != null)

http://git-wip-us.apache.org/repos/asf/cordova-wp7/blob/acdef7af/templates/standalone/cordovalib/CordovaView.xaml.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/CordovaView.xaml.cs b/templates/standalone/cordovalib/CordovaView.xaml.cs
index e584a1a..f6584a8 100644
--- a/templates/standalone/cordovalib/CordovaView.xaml.cs
+++ b/templates/standalone/cordovalib/CordovaView.xaml.cs
@@ -367,9 +367,9 @@ namespace WPCordovaClassLib
 
             try
             {
-                CordovaBrowser.InvokeScript("execScript", new string[] { nativeReady });
+                CordovaBrowser.InvokeScript("eval", new string[] { nativeReady });
             }
-            catch (Exception /*ex*/)
+            catch (Exception ex)
             {
                 Debug.WriteLine("Error calling js to fire nativeReady event. Did you include cordova-x.x.x.js in your html script tag?");
             }

http://git-wip-us.apache.org/repos/asf/cordova-wp7/blob/acdef7af/templates/standalone/cordovalib/NativeExecution.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/NativeExecution.cs b/templates/standalone/cordovalib/NativeExecution.cs
index 28dc528..8d56225 100644
--- a/templates/standalone/cordovalib/NativeExecution.cs
+++ b/templates/standalone/cordovalib/NativeExecution.cs
@@ -129,7 +129,7 @@ namespace WPCordovaClassLib.Cordova
                     }
                 };
 
-                if (true || (bc is File) || (bc is Accelerometer))
+                if ((bc is File) || (bc is Accelerometer))
                 {
                     // Due to some issues with the IsolatedStorage in current version of WP8 SDK we have to run all File Api commands synchronously.
                     // TODO: test this in WP8 RTM