Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added LiveTile plugin for Windows Phone

  • Loading branch information...
commit e34fabf1a3d98010a41e042ec13469c57f7fcba3 1 parent eb02b16
@purplecabbage purplecabbage authored
View
285 WindowsPhone/LiveTiles/LiveTiles.cs
@@ -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;
+ }
+
+ }
+}
View
25 WindowsPhone/LiveTiles/README.md
@@ -0,0 +1,25 @@
+Live Tiles plugin usage:
+===============
+Source files
+---
+liveTiles.js - plugin definition and js implementation
+LiveTiles.cs - native side implementation
+liveTilesExample.html- usage example
+
+In your head
+---
+
+[script type="text/javascript" charset="utf-8" src="liveTiles.js"][/script]
+
+
+Somewhere in your code
+---
+
+
+ navigator.plugins.liveTiles.updateAppTile(success, fail,{title: 'title', image:'Images/appbar.next.rest.png', count: 5, backTitle: 'Back title', backContent:'Back side', backImage : 'Images/appbar.close.rest.png'});
+
+ navigator.plugins.liveTiles.createSecondaryTile(success, fail, { title: 'title', image: 'Images/appbar.save.rest.png', count: 5, secondaryTileUri: 'www/myPage.html',backTitle:'back'});
+
+ navigator.plugins.liveTiles.updateSecondaryTile(success, fail, { title: 'title', count: 5, secondaryTileUri: 'www/myPage.html' });
+
+ navigator.plugins.liveTiles.deleteSecondaryTile(success, fail, { secondaryTileUri: 'www/myPage.html' });
View
55 WindowsPhone/LiveTiles/liveTiles.js
@@ -0,0 +1,55 @@
+PhoneGap.addConstructor(function () {
+
+ navigator.plugins.liveTiles =
+ {
+ updateAppTile: function (successCallback, errorCallback, options) {
+ if (successCallback && (typeof successCallback !== "function")) {
+ console.log("LiveTiles Error: successCallback is not a function");
+ return;
+ }
+
+ if (errorCallback && (typeof errorCallback !== "function")) {
+ console.log("LiveTiles Error: errorCallback is not a function");
+ return;
+ }
+ PhoneGap.exec(successCallback, errorCallback, "LiveTiles", "updateAppTile", options);
+ },
+
+ createSecondaryTile: function (successCallback, errorCallback, options) {
+ if (successCallback && (typeof successCallback !== "function")) {
+ console.log("LiveTiles Error: successCallback is not a function");
+ return;
+ }
+
+ if (errorCallback && (typeof errorCallback !== "function")) {
+ console.log("LiveTiles Error: errorCallback is not a function");
+ return;
+ }
+ PhoneGap.exec(successCallback, errorCallback, "LiveTiles", "createSecondaryTile", options);
+ },
+ updateSecondaryTile: function (successCallback, errorCallback, options) {
+ if (successCallback && (typeof successCallback !== "function")) {
+ console.log("LiveTiles Error: successCallback is not a function");
+ return;
+ }
+
+ if (errorCallback && (typeof errorCallback !== "function")) {
+ console.log("LiveTiles Error: errorCallback is not a function");
+ return;
+ }
+ PhoneGap.exec(successCallback, errorCallback, "LiveTiles", "updateSecondaryTile", options);
+ },
+ deleteSecondaryTile: function (successCallback, errorCallback, options) {
+ if (successCallback && (typeof successCallback !== "function")) {
+ console.log("LiveTile Error: successCallback is not a function");
+ return;
+ }
+
+ if (errorCallback && (typeof errorCallback !== "function")) {
+ console.log("LiveTiles Error: errorCallback is not a function");
+ return;
+ }
+ PhoneGap.exec(successCallback, errorCallback, "LiveTiles", "deleteSecondaryTile", options);
+ }
+ }
+});
View
131 WindowsPhone/LiveTiles/liveTilesExample.html
@@ -0,0 +1,131 @@
+<!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 -->
+ <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.2.0.js"></script>
+ <script type="text/javascript" charset="utf-8" src="liveTiles.js"></script>
+
+<script type="text/javascript" charset="utf-8">
+
+ var deviceReady = false;
+
+ var secondaryTile = 'www/liveTiles.html';
+
+ /**
+ * Function called when page has finished loading.
+ */
+ function init() {
+ document.addEventListener("deviceready", function () {
+ deviceReady = true;
+ console.log("Device=" + device.platform + " " + device.version);
+ }, false);
+ window.setTimeout(function () {
+ if (!deviceReady) {
+ alert("Error: PhoneGap did not initialize. Demo will not run correctly.");
+ }
+ }, 1000);
+ }
+
+ function updateAppTile() {
+ console.log('Update app tile');
+
+ var success = function (res) {
+ console.log('tile was updated');
+ document.getElementById('res').innerHTML = res;
+ };
+
+ var fail = function (e) {
+ console.log("Error occurred: " + e);
+ document.getElementById('res').innerHTML = "Error occurred: " + e;
+ };
+ navigator.plugins.liveTiles.updateAppTile(success,fail,{title:document.getElementById('title').value, image:'Images/appbar.next.rest.png', count: document.getElementById('count').value, backTitle: 'Back title', backContent:'Back side', backImage : 'Images/appbar.close.rest.png'});
+ };
+
+ function createSecondaryTile() {
+ console.log('Create secondary tile');
+
+ var success = function (res) {
+ console.log('secondary tile was created');
+ document.getElementById('res').innerHTML = res;
+ };
+
+ var fail = function (e) {
+ console.log("Error occurred: " + e);
+ document.getElementById('res').innerHTML = "Error occurred: " + e;
+ };
+ navigator.plugins.liveTiles.createSecondaryTile(success, fail, { title: document.getElementById('title').value, image: 'Images/appbar.save.rest.png', count: document.getElementById('count').value, secondaryTileUri: secondaryTile,backTitle:'back'});
+ };
+
+ function updateSecondaryTile() {
+ console.log('Update secondary tile');
+
+ var success = function (res) {
+ console.log('tile was updated');
+ document.getElementById('res').innerHTML = res;
+ };
+
+ var fail = function (e) {
+ console.log("Error occurred: " + e);
+ document.getElementById('res').innerHTML = "Error occurred: " + e;
+ };
+ navigator.plugins.liveTiles.updateSecondaryTile(success, fail, { title: document.getElementById('title').value, count: document.getElementById('count').value, secondaryTileUri: secondaryTile });
+ };
+
+ function deleteSecondaryTile() {
+ console.log('Delete secondary tile');
+
+ var success = function (res) {
+ console.log('tile was deleted');
+ document.getElementById('res').innerHTML = res;
+ };
+
+ var fail = function (e) {
+ console.log("Error occurred: " + e);
+ document.getElementById('res').innerHTML = "Error occurred: " + e;
+ };
+
+ navigator.plugins.liveTiles.deleteSecondaryTile(success, fail, { secondaryTileUri: secondaryTile });
+ };
+
+</script>
+
+ </head>
+ <body onLoad="init();" id="stage" class="theme">
+
+ <h1>Live tile</h1>
+ <div id="info">
+ <b>New title:</b> <span id="Span1"></span><br/>
+ <input type="text" id="title" value="new title" style="width:250px;height:20px;"/>
+ <br/>
+ <b>New count:</b> <span id="Span2"></span><br/>
+ <input type="text" id="count" value="10" style="width:250px;height:20px;"/>
+ <br/>
+ <b>Result:</b> <span id="res"></span>
+ </div>
+ <h2>Action</h2>
+ <a href="#" class="btn large" onclick="updateAppTile();">Update application tile</a>
+ <a href="#" class="btn large" onclick="createSecondaryTile();">Create secondary tile </br>(will open this page)</a>
+ <a href="#" class="btn large" onclick="updateSecondaryTile();">Update secondary tile</a>
+ <a href="#" class="btn large" onclick="deleteSecondaryTile();">Delete secondary tile</a>
+ <h2>&nbsp;</h2><a href="index.html" class="backBtn">Back</a>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.