Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

Commit

Permalink
work in progress submit (command bridge refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrebnov committed Sep 27, 2011
1 parent 001427b commit eb2a15a
Show file tree
Hide file tree
Showing 16 changed files with 378 additions and 437 deletions.
137 changes: 11 additions & 126 deletions framework/PGView.xaml.cs
Expand Up @@ -25,6 +25,7 @@
using System.Threading;
using Microsoft.Phone.Shell;


namespace WP7GapClassLib
{
public partial class PGView : UserControl
Expand All @@ -37,6 +38,11 @@ public partial class PGView : UserControl
private bool IsBrowserInitialized = false;
private bool OverrideBackButton = false;

/// <summary>
/// Handles native api calls
/// </summary>
private NativeExecution nativeExecution;

public PGView()
{

Expand All @@ -62,6 +68,9 @@ public PGView()
{

}

// initializes native execution logic
this.nativeExecution = new NativeExecution(ref this.GapBrowser);
}


Expand Down Expand Up @@ -288,39 +297,7 @@ void GapBrowser_ScriptNotify(object sender, NotifyEventArgs e)
return;
}

BaseCommand bc = CommandFactory.CreateUsingServiceName(commandCallParams.Service);

if (bc == null)
{
this.InvokeJSSCallback(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION));
return;
}

bc.OnCommandResult += new EventHandler<PluginResult>(
delegate(object o, PluginResult res) {
this.OnCommandResult(o, res, commandCallParams.CallbackId);
});


bc.OnCustomScript += new EventHandler<ScriptCallback>(
delegate(object o, ScriptCallback script)
{
this.InvokeCustomScript(script);
});

try
{
bc.InvokeMethodNamed(commandCallParams.Action, commandCallParams.Args);
}
catch(Exception)
{
bc.OnCommandResult -= delegate(object o, PluginResult res) {
OnCommandResult(o, res, null);
};
Debug.WriteLine("failed to InvokeMethodNamed :: " + commandCallParams.Action + " on Object :: " + commandCallParams.Service);
this.InvokeJSSCallback(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.INVALID_ACTION));
return;
}
this.nativeExecution.ProcessCommand(commandCallParams);
}

private void GapBrowser_Unloaded(object sender, RoutedEventArgs e)
Expand All @@ -338,98 +315,6 @@ private void GapBrowser_Navigated(object sender, System.Windows.Navigation.Navig
Debug.WriteLine("GapBrowser_Navigated");
}

private void OnCommandResult(object sender, PluginResult result, string callbackId)
{
BaseCommand command = sender as BaseCommand;

if (command == null)
{
Debug.WriteLine("OnCommandResult missing argument");
}
else if (result == null)
{
Debug.WriteLine("OnCommandResult missing argument");
}
else if (!String.IsNullOrEmpty(callbackId))
{
this.InvokeJSSCallback(callbackId, result);
}

// else // no callback required

// remove listener
command.OnCommandResult -= delegate(object o, PluginResult res) {
OnCommandResult(sender, result, callbackId);
};

}

private void InvokeJSSCallback(String callbackId, PluginResult result)
{
this.Dispatcher.BeginInvoke((ThreadStart)delegate()
{

if (String.IsNullOrEmpty(callbackId))
{
throw new ArgumentNullException("callbackId");
}

if (result == null)
{
throw new ArgumentNullException("result");
}

//string callBackScript = result.ToCallbackString(callbackId, "commandResult", "commandError");

// TODO: this is correct invokation method
//this.GapBrowser.InvokeScript("eval", new string[] {callBackScript });

/// But we temporary use this version because C#<->JS bridge is on fully ready
///
try
{
string status = ((int)result.Result).ToString();
string jsonResult = result.ToJSONString();
if (String.IsNullOrEmpty(result.Cast))
{
this.GapBrowser.InvokeScript("PhoneGapCommandResult", new string[] { status, callbackId, jsonResult });
}
else
{
this.GapBrowser.InvokeScript("PhoneGapCommandResult", new string[] { status, callbackId, jsonResult, result.Cast });
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception in InvokeJSSCallback :: " + ex.Message);
}
});
}

private void InvokeCustomScript(ScriptCallback script)
{
if (script == null)
{
throw new ArgumentNullException("script");
}

if (String.IsNullOrEmpty(script.ScriptName))
{
throw new ArgumentNullException("ScriptName");
}

this.Dispatcher.BeginInvoke((ThreadStart)delegate()
{
try
{
this.GapBrowser.InvokeScript(script.ScriptName, script.Args);
}
catch (Exception ex)
{
Debug.WriteLine("Exception in InvokeCustomScript :: " + ex.Message);
}
});
}


}
}
5 changes: 2 additions & 3 deletions framework/PhoneGap/CommandFactory.cs
Expand Up @@ -14,8 +14,7 @@
namespace WP7GapClassLib.PhoneGap
{
/// <summary>
/// Provides functionality to create phone gap command by name,
/// Each command is created only once and stored in commands pool.
/// Provides functionality to create phone gap command by name.
/// </summary>
public static class CommandFactory
{
Expand All @@ -29,7 +28,7 @@ public static class CommandFactory
/// </summary>
/// <param name="service">Command class name, for example Device or Notification</param>
/// <returns>Command class instance or null</returns>
public static BaseCommand CreateUsingServiceName(string service)
public static BaseCommand CreateByServiceName(string service)
{

if (string.IsNullOrEmpty(service))
Expand Down
44 changes: 23 additions & 21 deletions framework/PhoneGap/Commands/AudioPlayer.cs
Expand Up @@ -144,7 +144,7 @@ public void startRecording(string filePath)
{
if (this.player != null)
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorPlayModeSet.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPlayModeSet));
}
else if (this.recorder == null)
{
Expand All @@ -164,12 +164,12 @@ public void startRecording(string filePath)
}
catch (Exception e)
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorStartingRecording.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingRecording));
}
} else
{

this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorAlreadyRecording.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorAlreadyRecording));
}
}

Expand Down Expand Up @@ -211,27 +211,29 @@ public void startPlaying(string filePath)
{
if (this.recorder != null)
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorRecordModeSet.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorRecordModeSet));
return;
}
else if ((this.player == null) || (this.state == MediaStopped))


if ((this.player == null) || (this.state == MediaStopped))
{
try
{
if (this.player == null)
{
bool isMediaExist = Application.Current.Resources.Contains("PhoneGapMediaPlayer");
if (isMediaExist)
{
this.player = Application.Current.Resources["PhoneGapMediaPlayer"] as MediaElement;
}
else

if (!Application.Current.Resources.Contains("PhoneGapMediaPlayer"))
{
throw new Exception("PhoneGapMediaPlayer wasn't found in application resources");
}
//this.mPlayer = new MediaElement();

this.player = Application.Current.Resources["PhoneGapMediaPlayer"] as MediaElement;

this.player.MediaOpened += MediaOpened;
this.player.MediaEnded += MediaEnded;
this.player.MediaFailed += MediaFailed;

}
this.audioFile = filePath;
this.player.AutoPlay = false;
Expand All @@ -253,7 +255,7 @@ public void startPlaying(string filePath)
}
else
{
throw new ArgumentException("Source doesn't exist");
throw new ArgumentException("Source doesn't exist");
}
}
}
Expand All @@ -262,7 +264,7 @@ public void startPlaying(string filePath)
catch (Exception e)
{
string s = e.Message;
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorStartingPlayback.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingPlayback));
}
}
else
Expand All @@ -274,7 +276,7 @@ public void startPlaying(string filePath)
}
else
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorResumeState.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorResumeState));
}
}
}
Expand All @@ -292,7 +294,7 @@ private void MediaOpened(object sender, RoutedEventArgs arg)

this.duration = this.player.NaturalDuration.TimeSpan.TotalSeconds;
this.prepareOnly = false;
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaDuration.ToString(), this.duration.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaDuration, this.duration));
}

/// <summary>
Expand Down Expand Up @@ -322,7 +324,7 @@ public void seekToPlaying(int milliseconds)
{
TimeSpan timeSpen = new TimeSpan(0, 0, 0, 0, milliseconds);
this.player.Position = timeSpen;
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition.ToString(), (milliseconds / 1000.0f).ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, milliseconds / 1000.0f));
}
}

Expand All @@ -337,7 +339,7 @@ public void pausePlaying()
this.SetState(MediaPaused);
} else
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorPauseState.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPauseState));
}
}

Expand All @@ -353,7 +355,7 @@ public void stopPlaying()
this.SetState(MediaStopped);
} else
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), MediaErrorStopState.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStopState));
}
}

Expand All @@ -366,7 +368,7 @@ public double getCurrentPosition()
if ((this.state == MediaRunning) || (this.state == MediaPaused))
{
double currentPosition = this.player.Position.TotalSeconds;
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition.ToString(), currentPosition.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, currentPosition));
return currentPosition;
}
else
Expand Down Expand Up @@ -407,7 +409,7 @@ private void SetState(int state)
{
if (this.state != state)
{
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaState.ToString(), state.ToString()));
this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaState, state));
}

this.state = state;
Expand Down
3 changes: 3 additions & 0 deletions framework/PhoneGap/Commands/BaseCommand.cs
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Shapes;
using System.Reflection;
using System.Collections.Generic;
using System.Threading;

namespace WP7GapClassLib.PhoneGap.Commands
{
Expand Down Expand Up @@ -81,6 +82,8 @@ public void DispatchCommandResult(PluginResult result)
{
if (this.OnCommandResult != null)
{
// TODO
//Thread.Sleep(5000);
this.OnCommandResult(this, result);
this.OnCommandResult = null;

Expand Down

0 comments on commit eb2a15a

Please sign in to comment.