Touchgui/plugins/org.apache.cordova.splashscreen/src/wp/SplashScreen.cs

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

michael@0 1 /*
michael@0 2 Licensed under the Apache License, Version 2.0 (the "License");
michael@0 3 you may not use this file except in compliance with the License.
michael@0 4 You may obtain a copy of the License at
michael@0 5
michael@0 6 http://www.apache.org/licenses/LICENSE-2.0
michael@0 7
michael@0 8 Unless required by applicable law or agreed to in writing, software
michael@0 9 distributed under the License is distributed on an "AS IS" BASIS,
michael@0 10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 11 See the License for the specific language governing permissions and
michael@0 12 limitations under the License.
michael@0 13 */
michael@0 14
michael@0 15 using System;
michael@0 16 using System.Net;
michael@0 17 using System.Windows;
michael@0 18 using System.Windows.Controls;
michael@0 19 using System.Windows.Documents;
michael@0 20 using System.Windows.Ink;
michael@0 21 using System.Windows.Input;
michael@0 22 using System.Windows.Media;
michael@0 23 using System.Windows.Media.Animation;
michael@0 24 using System.Windows.Shapes;
michael@0 25 using Microsoft.Phone.Info;
michael@0 26 using System.Windows.Controls.Primitives;
michael@0 27 using System.Diagnostics;
michael@0 28 using System.Windows.Media.Imaging;
michael@0 29 using System.Windows.Resources;
michael@0 30 using System.IO;
michael@0 31 using System.Xml.Linq;
michael@0 32 using System.Linq;
michael@0 33 using System.Windows.Threading;
michael@0 34
michael@0 35 namespace WPCordovaClassLib.Cordova.Commands
michael@0 36 {
michael@0 37 /// <summary>
michael@0 38 /// Listens for changes to the state of the battery on the device.
michael@0 39 /// Currently only the "isPlugged" parameter available via native APIs.
michael@0 40 /// </summary>
michael@0 41 public class SplashScreen : BaseCommand
michael@0 42 {
michael@0 43 private Popup popup;
michael@0 44 private bool autohide = true;
michael@0 45
michael@0 46 private static bool WasShown = false;
michael@0 47
michael@0 48 public SplashScreen()
michael@0 49 {
michael@0 50 Image SplashScreen = new Image();
michael@0 51 BitmapImage splash_image = new BitmapImage();
michael@0 52 splash_image.SetSource(Application.GetResourceStream(new Uri(@"SplashScreenImage.jpg", UriKind.Relative)).Stream);
michael@0 53 SplashScreen.Source = splash_image;
michael@0 54
michael@0 55 // Instansiate the popup and set the Child property of Popup to SplashScreen
michael@0 56 popup = new Popup() {IsOpen = false, Child = SplashScreen };
michael@0 57 // Orient the popup accordingly
michael@0 58 popup.HorizontalAlignment = HorizontalAlignment.Stretch;
michael@0 59 popup.VerticalAlignment = VerticalAlignment.Center;
michael@0 60
michael@0 61
michael@0 62 LoadConfigValues();
michael@0 63 }
michael@0 64
michael@0 65 public override void OnInit()
michael@0 66 {
michael@0 67 // we only want to autoload the first time a page is loaded.
michael@0 68 if (!WasShown)
michael@0 69 {
michael@0 70 WasShown = true;
michael@0 71 show();
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 void LoadConfigValues()
michael@0 76 {
michael@0 77 StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
michael@0 78
michael@0 79 if (streamInfo != null)
michael@0 80 {
michael@0 81 StreamReader sr = new StreamReader(streamInfo.Stream);
michael@0 82 //This will Read Keys Collection for the xml file
michael@0 83 XDocument document = XDocument.Parse(sr.ReadToEnd());
michael@0 84
michael@0 85 var preferences = from results in document.Descendants()
michael@0 86 where (string)results.Attribute("name") == "AutoHideSplashScreen"
michael@0 87 select (string)results.Attribute("value") == "true";
michael@0 88
michael@0 89 if (preferences.Count() > 0 && preferences.First() == false)
michael@0 90 {
michael@0 91 autohide = false;
michael@0 92 }
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 public void show(string options = null)
michael@0 97 {
michael@0 98 Deployment.Current.Dispatcher.BeginInvoke(() =>
michael@0 99 {
michael@0 100 if (popup.IsOpen)
michael@0 101 {
michael@0 102 return;
michael@0 103 }
michael@0 104
michael@0 105 popup.Child.Opacity = 0;
michael@0 106
michael@0 107 Storyboard story = new Storyboard();
michael@0 108 DoubleAnimation animation;
michael@0 109 animation = new DoubleAnimation();
michael@0 110 animation.From = 0.0;
michael@0 111 animation.To = 1.0;
michael@0 112 animation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
michael@0 113
michael@0 114 Storyboard.SetTarget(animation, popup.Child);
michael@0 115 Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
michael@0 116 story.Children.Add(animation);
michael@0 117
michael@0 118 Debug.WriteLine("Fading the splash screen in");
michael@0 119
michael@0 120 story.Begin();
michael@0 121
michael@0 122 popup.IsOpen = true;
michael@0 123
michael@0 124 if (autohide)
michael@0 125 {
michael@0 126 DispatcherTimer timer = new DispatcherTimer();
michael@0 127 timer.Tick += (object sender, EventArgs e) =>
michael@0 128 {
michael@0 129 hide();
michael@0 130 };
michael@0 131 timer.Interval = TimeSpan.FromSeconds(1.2);
michael@0 132 timer.Start();
michael@0 133 }
michael@0 134 });
michael@0 135 }
michael@0 136
michael@0 137
michael@0 138 public void hide(string options = null)
michael@0 139 {
michael@0 140 Deployment.Current.Dispatcher.BeginInvoke(() =>
michael@0 141 {
michael@0 142 if (!popup.IsOpen)
michael@0 143 {
michael@0 144 return;
michael@0 145 }
michael@0 146
michael@0 147 popup.Child.Opacity = 1.0;
michael@0 148
michael@0 149 Storyboard story = new Storyboard();
michael@0 150 DoubleAnimation animation;
michael@0 151 animation = new DoubleAnimation();
michael@0 152 animation.From = 1.0;
michael@0 153 animation.To = 0.0;
michael@0 154 animation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
michael@0 155
michael@0 156 Storyboard.SetTarget(animation, popup.Child);
michael@0 157 Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
michael@0 158 story.Children.Add(animation);
michael@0 159 story.Completed += (object sender, EventArgs e) =>
michael@0 160 {
michael@0 161 popup.IsOpen = false;
michael@0 162 };
michael@0 163 story.Begin();
michael@0 164 });
michael@0 165 }
michael@0 166 }
michael@0 167 }

mercurial