Skip to content

Commit

Permalink
implemented live tiles plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrebnov committed Nov 8, 2011
1 parent b9a87ad commit 4ba9289
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 10 deletions.
16 changes: 15 additions & 1 deletion framework/PGView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using WP7GapClassLib.PhoneGap;
using System.Threading;
using Microsoft.Phone.Shell;
using System.Windows.Navigation;



Expand Down Expand Up @@ -240,9 +241,22 @@ void GapBrowser_Loaded(object sender, RoutedEventArgs e)
// todo: this should be a start page param passed in via a getter/setter
// aka StartPage

Uri indexUri = new Uri("www/index.html", UriKind.Relative);
PhoneApplicationPage currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage;
string appUri = string.Empty;
Uri indexUri;
if (currentPage.NavigationContext.QueryString.TryGetValue("Uri", out appUri))
{
indexUri = new Uri(appUri, UriKind.Relative);
}
else
{
indexUri = new Uri("www/index.html", UriKind.Relative);
}
this.GapBrowser.Navigate(indexUri);

//Uri indexUri = new Uri("www/index.html", UriKind.Relative);
//this.GapBrowser.Navigate(indexUri);

this.IsBrowserInitialized = true;

AttachHardwareButtonHandlers();
Expand Down
2 changes: 2 additions & 0 deletions tests/MobileSpecUnitTests/GapSourceDictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<FilePath Value="www\facebook_js_sdk.js"/>
<FilePath Value="www\FBConnect.js"/>
<FilePath Value="www\index.html"/>
<FilePath Value="www\liveTiles.html"/>
<FilePath Value="www\liveTiles.js"/>
<FilePath Value="www\master.css"/>
<FilePath Value="www\pg-plugin-fb-connect.js"/>
<FilePath Value="www\phonegap-1.1.0.js"/>
Expand Down
285 changes: 285 additions & 0 deletions tests/MobileSpecUnitTests/LiveTiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2011, Nitobi Software Inc.
* Copyright (c) 2011, Microsoft Corporation
*/

using System.Runtime.Serialization;
using WP7GapClassLib.PhoneGap;
using WP7GapClassLib.PhoneGap.Commands;
using WP7GapClassLib.PhoneGap.JSON;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Phone.Controls;
using System.Windows;

namespace PhoneGap.Extension.Commands
{
/// <summary>
/// Implementes access to application live tiles
/// http://msdn.microsoft.com/en-us/library/hh202948(v=VS.92).aspx
/// </summary>
public class LiveTiles : BaseCommand
{

#region Live tiles options

/// <summary>
/// Represents LiveTile options
/// </summary>
[DataContract]
public class LiveTilesOptions
{
/// <summary>
/// Tile title
/// </summary>
[DataMember(IsRequired=false, Name="title")]
public string Title { get; set; }

/// <summary>
/// Tile count
/// </summary>
[DataMember(IsRequired = false, Name = "count")]
public int Count { get; set; }

/// <summary>
/// Tile image
/// </summary>
[DataMember(IsRequired = false, Name = "image")]
public string Image { get; set; }

/// <summary>
/// Back tile title
/// </summary>
[DataMember(IsRequired = false, Name = "backTitle")]
public string BackTitle { get; set; }

/// <summary>
/// Back tile content
/// </summary>
[DataMember(IsRequired = false, Name = "backContent")]
public string BackContent { get; set; }

/// <summary>
/// Back tile image
/// </summary>
[DataMember(IsRequired = false, Name = "backImage")]
public string BackImage { get; set; }

/// <summary>
/// Identifier for second tile
/// </summary>
[DataMember(IsRequired = false, Name = "secondaryTileUri")]
public string SecondaryTileUri { get; set; }

}
#endregion

/// <summary>
/// Updates application live tile
/// </summary>
public void updateAppTile(string options)
{
LiveTilesOptions liveTileOptions;
try
{
liveTileOptions = WP7GapClassLib.PhoneGap.JSON.JsonHelper.Deserialize<LiveTilesOptions>(options);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}

try
{
ShellTile appTile = ShellTile.ActiveTiles.First();

if (appTile != null)
{
StandardTileData standardTile = CreateTileData(liveTileOptions);
appTile.Update(standardTile);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Can't get application tile"));
}
}
catch(Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error updating application tile"));
}
}

/// <summary>
/// Creates secondary tile
/// </summary>
public void createSecondaryTile(string options)
{
LiveTilesOptions liveTileOptions;
try
{
liveTileOptions = WP7GapClassLib.PhoneGap.JSON.JsonHelper.Deserialize<LiveTilesOptions>(options);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}

if (string.IsNullOrEmpty(liveTileOptions.Title) || string.IsNullOrEmpty(liveTileOptions.Image) || string.IsNullOrEmpty(liveTileOptions.SecondaryTileUri))
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}
try
{
ShellTile foundTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(liveTileOptions.SecondaryTileUri));
if (foundTile == null)
{
StandardTileData secondaryTile = CreateTileData(liveTileOptions);
PhoneApplicationPage currentPage;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage;
string currentUri = currentPage.NavigationService.Source.ToString().Split('?')[0];
ShellTile.Create(new Uri(currentUri + "?Uri=" + liveTileOptions.SecondaryTileUri, UriKind.Relative), secondaryTile);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
});
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,"Tile already exist"));
}
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,"Error creating secondary live tile"));
}
}

/// <summary>
/// Updates secondary tile
/// </summary>
public void updateSecondaryTile(string options)
{
LiveTilesOptions liveTileOptions;
try
{
liveTileOptions = WP7GapClassLib.PhoneGap.JSON.JsonHelper.Deserialize<LiveTilesOptions>(options);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}

if (string.IsNullOrEmpty(liveTileOptions.SecondaryTileUri))
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}

try
{
ShellTile foundTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(liveTileOptions.SecondaryTileUri));

if (foundTile != null)
{
StandardTileData liveTile = this.CreateTileData(liveTileOptions);
foundTile.Update(liveTile);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Can't get secondary live tile"));
}
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,"Error updating secondary live tile"));
}
}

/// <summary>
/// Deletes secondary tile
/// </summary>
public void deleteSecondaryTile(string options)
{
LiveTilesOptions liveTileOptions;
try
{
liveTileOptions = WP7GapClassLib.PhoneGap.JSON.JsonHelper.Deserialize<LiveTilesOptions>(options);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}

if (string.IsNullOrEmpty(liveTileOptions.SecondaryTileUri))
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}
try
{
ShellTile foundTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(liveTileOptions.SecondaryTileUri));
if (foundTile != null)
{
foundTile.Delete();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Can't get secondary live tile"));
}

}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error deleting secondary live tile"));
}
}


/// <summary>
/// Cerates tile data
/// </summary>
private StandardTileData CreateTileData(LiveTilesOptions liveTileOptions)
{
StandardTileData standardTile = new StandardTileData();
if (!string.IsNullOrEmpty(liveTileOptions.Title))
{
standardTile.Title = liveTileOptions.Title;
}
if (!string.IsNullOrEmpty(liveTileOptions.Image))
{
standardTile.BackgroundImage = new Uri(liveTileOptions.Image, UriKind.RelativeOrAbsolute);
}
if (liveTileOptions.Count > 0)
{
standardTile.Count = liveTileOptions.Count;
}
if (!string.IsNullOrEmpty(liveTileOptions.BackTitle))
{
standardTile.BackTitle = liveTileOptions.BackTitle;
}
if (!string.IsNullOrEmpty(liveTileOptions.BackContent))
{
standardTile.BackContent = liveTileOptions.BackContent;
}
if (!string.IsNullOrEmpty(liveTileOptions.BackImage))
{
standardTile.BackBackgroundImage = new Uri(liveTileOptions.BackImage, UriKind.RelativeOrAbsolute);
}
return standardTile;
}

}
}
43 changes: 34 additions & 9 deletions tests/MobileSpecUnitTests/MobileSpecUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
</Compile>
<Compile Include="Calculator.cs" />
<Compile Include="ChildBrowserCommand.cs" />
<Compile Include="LiveTiles.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -104,16 +105,40 @@
<Content Include="www\autotest\qunit.js" />
<Content Include="www\autotest\test-runner.js" />
<Content Include="www\autotest\tests\pg-plugin-fb-connect-tests.js" />
<Content Include="www\ChildBrowser.js" />
<Content Include="www\facebook1.html" />
<Content Include="www\facebook2.html" />
<Content Include="www\facebook_js_sdk.js" />
<Content Include="www\FBConnect.js" />
<Content Include="www\index.html" />
<Content Include="www\master.css" />
<Content Include="www\ChildBrowser.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\facebook1.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\facebook2.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\facebook_js_sdk.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\FBConnect.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\liveTiles.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\liveTiles.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\master.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\misc\index.html" />
<Content Include="www\pg-plugin-fb-connect.js" />
<Content Include="www\phonegap-1.1.0.js" />
<Content Include="www\pg-plugin-fb-connect.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="www\phonegap-1.1.0.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\framework\WP7GapClassLib.csproj">
Expand Down
1 change: 1 addition & 0 deletions tests/MobileSpecUnitTests/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ <h4>Width: <span id="width"> &nbsp;</span>, Height: <span id="height">&nbsp;
<a href="facebook1.html" class="btn large">Facebook v1</a>
<a href="facebook2.html" class="btn large">Facebook v2</a>
<a href="twitter.html" class="btn large">Twitter (not impl)</a>
<a href="liveTiles.html" class="btn large">Live Tiles</a>
<a href="autotest/index.html" class="btn large">Tests</a>
</body>
</html>
Loading

0 comments on commit 4ba9289

Please sign in to comment.