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.

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

mercurial