Skip to content

Commit

Permalink
Merge pull request #24 from purplecabbage/AccelExampleTesting
Browse files Browse the repository at this point in the history
Added more interesting Accel example.
  • Loading branch information
Jesse MacFadyen committed Oct 12, 2011
2 parents e5579b0 + 3560cd5 commit 4d992d0
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 18 deletions.
3 changes: 2 additions & 1 deletion framework/PGView.xaml
Expand Up @@ -23,7 +23,8 @@
ScriptNotify="GapBrowser_ScriptNotify"
LoadCompleted="GapBrowser_LoadCompleted"
Navigating="GapBrowser_Navigating"
NavigationFailed="GapBrowser_NavigationFailed" IsGeolocationEnabled="True" />
NavigationFailed="GapBrowser_NavigationFailed"
IsGeolocationEnabled="True" />

</Grid>
</UserControl>
Expand Down
15 changes: 14 additions & 1 deletion framework/PGView.xaml.cs
Expand Up @@ -36,11 +36,12 @@
using Microsoft.Phone.Shell;



namespace WP7GapClassLib
{
public partial class PGView : UserControl
{

/// <summary>
/// Indicates whether web control has been loaded and no additional initialization is needed.
/// Prevents data clearing during page transitions.
Expand All @@ -55,6 +56,14 @@ public partial class PGView : UserControl

protected DOMStorageHelper domStorageHelper;

public System.Windows.Controls.Grid _LayoutRoot
{
get
{
return ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
}
}

public PGView()
{

Expand All @@ -65,6 +74,7 @@ public PGView()
return;
}


StartupMode mode = PhoneApplicationService.Current.StartupMode;
Debug.WriteLine("StartupMode mode =" + mode.ToString());

Expand Down Expand Up @@ -309,6 +319,9 @@ void GapBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
string commandStr = e.Value;

Debug.WriteLine("Command::" + commandStr);


// DOMStorage/Local OR DOMStorage/Session
if (commandStr.IndexOf("DOMStorage") == 0)
{
Expand Down
7 changes: 6 additions & 1 deletion framework/PhoneGap/CommandFactory.cs
Expand Up @@ -29,6 +29,7 @@ public static class CommandFactory
/// <summary>
/// Performance optimization allowing more faster create already known commands.
/// </summary>

private static Dictionary<string, Type> commandMap = new Dictionary<string, Type>();

/// <summary>
Expand All @@ -46,12 +47,16 @@ public static BaseCommand CreateByServiceName(string service)

if (!commandMap.ContainsKey(service))
{

Type t = Type.GetType("WP7GapClassLib.PhoneGap.Commands." + service);
if (t != null)
{
commandMap[service] = t;

}
else
{

}
}

Expand Down
7 changes: 5 additions & 2 deletions framework/PhoneGap/Commands/BaseCommand.cs
Expand Up @@ -9,12 +9,12 @@
*/

using System;
using System.Reflection;
using System.Reflection;
using Microsoft.Phone.Shell;

namespace WP7GapClassLib.PhoneGap.Commands
{
public abstract class BaseCommand : IDisposable
public abstract class BaseCommand : IDisposable
{
/*
* All commands + plugins must extend BaseCommand, because they are dealt with as BaseCommands in PGView.xaml.cs
Expand Down Expand Up @@ -117,4 +117,7 @@ public void Dispose()
this.OnCommandResult = null;
}
}



}
12 changes: 8 additions & 4 deletions framework/PhoneGap/Commands/Notification.cs
Expand Up @@ -118,8 +118,7 @@ public void beep(string count)
});

}



// TODO: may need a listener to trigger DispatchCommandResult after the alarm has finished executing...
DispatchCommandResult();
}
Expand All @@ -129,14 +128,19 @@ public void activityStart(string unused)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
var t1 = Application.Current.RootVisual;
PhoneApplicationFrame frame = t1 as PhoneApplicationFrame;
var temp1 = frame.FindName("LayoutRoot");
if (frame != null)
{
PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
if (page != null)
{
Grid grid = page.FindName("LayoutRoot") as Grid;
var temp = page.FindName("LayoutRoot");// as Grid;
Grid grid = temp as Grid;
if (grid != null)
{
if (progressBar != null)
Expand Down
1 change: 1 addition & 0 deletions tests/MobileSpecUnitTests/GapSourceDictionary.xml
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<GapSourceDictionary>
<FilePath Value="www\accelerometer.html"/>
<FilePath Value="www\accelerometer2.html"/>
<FilePath Value="www\audio.html"/>
<FilePath Value="www\camera.html"/>
<FilePath Value="www\capture.html"/>
Expand Down
5 changes: 4 additions & 1 deletion tests/MobileSpecUnitTests/MobileSpecUnitTests.csproj
Expand Up @@ -101,10 +101,13 @@
<Content Include="www\accelerometer.html">
<SubType>Designer</SubType>
</Content>
<Content Include="www\accelerometer2.html" />
<Content Include="www\audio.html">
<SubType>Designer</SubType>
</Content>
<Content Include="www\autotest\index.html" />
<Content Include="www\autotest\index.html">
<SubType>Designer</SubType>
</Content>
<Content Include="www\autotest\qunit.css" />
<Content Include="www\autotest\qunit.js" />
<Content Include="www\autotest\test-runner.js" />
Expand Down
16 changes: 10 additions & 6 deletions tests/MobileSpecUnitTests/www/accelerometer.html
@@ -1,11 +1,11 @@
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
<!-- meta name="viewport" content="width=device-width, height=device-height, user-scalable=yes, initial-scale=2.0, maximum-scale=4.0, minimum-scale=1.0" / -->
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <!-- ISO-8859-1 -->

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen">
<link rel="stylesheet" href="master.css" type="text/css" media="screen"/>

<script type="text/javascript">
// provide our own console if it does not exist, huge dev aid!
Expand Down Expand Up @@ -65,7 +65,7 @@

// Update acceleration every 1 sec
var opt = {};
opt.frequency = 5000;
opt.frequency = 50;
watchAccelId = navigator.accelerometer.watchAcceleration(success, fail, opt);

setAccelStatus("Running");
Expand Down Expand Up @@ -136,6 +136,8 @@

</script>



</head>
<body onLoad="init();" id="stage" class="theme">

Expand All @@ -148,6 +150,8 @@ <h1>Acceleration</h1>
<tr><td width="20%">Z:</td><td id="z">&nbsp;</td></tr>
</table></div>
</div>



<h2>Action</h2>
<a href="#" class="btn large" onclick="getAccel();">Get Acceleration</a>
Expand Down
171 changes: 171 additions & 0 deletions tests/MobileSpecUnitTests/www/accelerometer2.html
@@ -0,0 +1,171 @@
<!DOCTYPE html>
<html>
<head>
<!-- meta name="viewport" content="width=device-width, height=device-height, user-scalable=yes, initial-scale=2.0, maximum-scale=4.0, minimum-scale=1.0" / -->
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen"/>

<script type="text/javascript">
// provide our own console if it does not exist, huge dev aid!
if(typeof window.console == "undefined")
{
window.console = {log:function(str){window.external.Notify(str);}};
}

// output any errors to console log, created above.
window.onerror=function(e)
{
console.log("window.onerror ::" + JSON.stringify(e));
};

console.log("Installed console ! ");
</script>


<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>


<script type="text/javascript" charset="utf-8">


//-------------------------------------------------------------------------
// Acceleration
//-------------------------------------------------------------------------
var watchAccelId = null;

/**
* Start watching acceleration
*/
var watchAccel = function() {
console.log("watchAccel()");

if(watchAccelId)
{
return;
}

// Success callback
var success = function (a) {
document.getElementById("ball").style.left = parseInt(a.x * 100 + 100) + "px" ;
document.getElementById("ball").style.top = parseInt(a.z * 100 + 100) + "px" ;

};

// Fail callback
var fail = function(e){
console.log("watchAccel fail callback with error code "+e);
stopAccel();
};

// Update acceleration every 1 sec
var opt = {};
opt.frequency = 50;
watchAccelId = navigator.accelerometer.watchAcceleration(onAccelData, fail, opt);

setAccelStatus("Running");
};


var ballX = 0;
var ballY = 0;
var ballPixelsPerUpdate = 10;

function onAccelData(a)
{
ballX += a.x * ballPixelsPerUpdate;
ballY += a.z * ballPixelsPerUpdate;

ballX = Math.max(ballX,0);
ballX = Math.min(ballX,200);

ballY = Math.max(ballY,0);
ballY = Math.min(ballY,200);


document.getElementById("ball").style.left = ballX + "px" ;
document.getElementById("ball").style.top = ballY + "px" ;
}

/**
* Stop watching the acceleration
*/
var stopAccel = function() {
console.log("stopAccel()");
if (watchAccelId) {
navigator.accelerometer.clearWatch(watchAccelId);
watchAccelId = null;
}
};


/**
* Function called when page has finished loading.
*/
function init() {

document.addEventListener("deviceready", function() {
console.log("Device="+device.platform+" "+device.version);
}, false);
}

</script>

<style>
#ball
{
position:relative;
width:20px;
height:20px;
border-radius:10px;
background-color:#FFF;
}

#playfield
{
position:absolute;
left:50px;
top:220px;
width:220px;
height:220px;
background-color:#ff8800;
}

.dButton
{
width:80px;
height:40px;
display:inline;
float:left;
clear:none;
border-style:solid;
border-width:1px;
border-radius:2px;
line-height:40px;
}

.backBtnz
{
position:absolute;
bottom:-20px;
}
</style>

</head>
<body onLoad="init();" id="stage" class="theme">

<div id="playfield">
<div id="ball">

</div>
</div>

<h2>Actions</h2>
<div class="dButton" onclick="watchAccel();">Start</div>
<div class="dButton" onclick="stopAccel();">Stop</div>

<div/>
<a href="index.html" class="backBtn backBtnz">Back</a>
</body>
</html>
5 changes: 3 additions & 2 deletions tests/MobileSpecUnitTests/www/index.html
Expand Up @@ -2,9 +2,9 @@
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8"/>

<script type="text/javascript">
// provide our own console if it does not exist, huge dev aid!
Expand Down Expand Up @@ -88,6 +88,7 @@ <h4>Width: <span id="width"> &nbsp;</span>, Height: <span id="height">&nbsp;

<a href="autotest/index.html" class="btn large">Automatic Test</a>
<a href="accelerometer.html" class="btn large">Accelerometer</a>
<a href="accelerometer2.html" class="btn large">Accel Game</a>
<a href="audio.html" class="btn large">Audio Play/Record</a>
<a href="camera.html" class="btn large">Camera</a>
<a href="capture.html" class="btn large">Capture</a>
Expand Down

0 comments on commit 4d992d0

Please sign in to comment.