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

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.splashscreen/src/wp/SplashScreen.cs	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,167 @@
     1.4 +/*  
     1.5 +    Licensed under the Apache License, Version 2.0 (the "License");
     1.6 +    you may not use this file except in compliance with the License.
     1.7 +    You may obtain a copy of the License at
     1.8 +    
     1.9 +    http://www.apache.org/licenses/LICENSE-2.0
    1.10 +    
    1.11 +    Unless required by applicable law or agreed to in writing, software
    1.12 +    distributed under the License is distributed on an "AS IS" BASIS,
    1.13 +    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.14 +    See the License for the specific language governing permissions and
    1.15 +    limitations under the License.
    1.16 +*/
    1.17 +
    1.18 +using System;
    1.19 +using System.Net;
    1.20 +using System.Windows;
    1.21 +using System.Windows.Controls;
    1.22 +using System.Windows.Documents;
    1.23 +using System.Windows.Ink;
    1.24 +using System.Windows.Input;
    1.25 +using System.Windows.Media;
    1.26 +using System.Windows.Media.Animation;
    1.27 +using System.Windows.Shapes;
    1.28 +using Microsoft.Phone.Info;
    1.29 +using System.Windows.Controls.Primitives;
    1.30 +using System.Diagnostics;
    1.31 +using System.Windows.Media.Imaging;
    1.32 +using System.Windows.Resources;
    1.33 +using System.IO;
    1.34 +using System.Xml.Linq;
    1.35 +using System.Linq;
    1.36 +using System.Windows.Threading;
    1.37 +
    1.38 +namespace WPCordovaClassLib.Cordova.Commands
    1.39 +{
    1.40 +    /// <summary>
    1.41 +    /// Listens for changes to the state of the battery on the device.
    1.42 +    /// Currently only the "isPlugged" parameter available via native APIs.
    1.43 +    /// </summary>
    1.44 +    public class SplashScreen : BaseCommand
    1.45 +    {
    1.46 +        private Popup popup;
    1.47 +        private bool autohide = true;
    1.48 +
    1.49 +        private static bool WasShown = false;
    1.50 +
    1.51 +        public SplashScreen()
    1.52 +        {
    1.53 +            Image SplashScreen = new Image();
    1.54 +            BitmapImage splash_image = new BitmapImage();
    1.55 +            splash_image.SetSource(Application.GetResourceStream(new Uri(@"SplashScreenImage.jpg", UriKind.Relative)).Stream);
    1.56 +            SplashScreen.Source = splash_image;
    1.57 +
    1.58 +            // Instansiate the popup and set the Child property of Popup to SplashScreen
    1.59 +            popup = new Popup() {IsOpen = false, Child = SplashScreen };
    1.60 +            // Orient the popup accordingly
    1.61 +            popup.HorizontalAlignment = HorizontalAlignment.Stretch;
    1.62 +            popup.VerticalAlignment = VerticalAlignment.Center;
    1.63 +            
    1.64 +
    1.65 +            LoadConfigValues();
    1.66 +        }
    1.67 +
    1.68 +        public override void OnInit()
    1.69 +        {
    1.70 +            // we only want to autoload the first time a page is loaded.
    1.71 +            if (!WasShown)
    1.72 +            {
    1.73 +                WasShown = true;
    1.74 +                show();
    1.75 +            }
    1.76 +        }
    1.77 +
    1.78 +        void LoadConfigValues()
    1.79 +        {
    1.80 +            StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
    1.81 +
    1.82 +            if (streamInfo != null)
    1.83 +            {
    1.84 +                StreamReader sr = new StreamReader(streamInfo.Stream);
    1.85 +                //This will Read Keys Collection for the xml file
    1.86 +                XDocument document = XDocument.Parse(sr.ReadToEnd());
    1.87 +
    1.88 +                var preferences = from results in document.Descendants()
    1.89 +                                  where (string)results.Attribute("name") == "AutoHideSplashScreen"
    1.90 +                                  select (string)results.Attribute("value") == "true";
    1.91 +
    1.92 +                if (preferences.Count() > 0 &&  preferences.First() == false)
    1.93 +                {
    1.94 +                    autohide = false;
    1.95 +                }
    1.96 +            }
    1.97 +        }
    1.98 +
    1.99 +        public void show(string options = null)
   1.100 +        {
   1.101 +            Deployment.Current.Dispatcher.BeginInvoke(() =>
   1.102 +            {
   1.103 +                if (popup.IsOpen)
   1.104 +                {
   1.105 +                    return;
   1.106 +                }
   1.107 +
   1.108 +                popup.Child.Opacity = 0;
   1.109 +
   1.110 +                Storyboard story = new Storyboard();
   1.111 +                DoubleAnimation animation;
   1.112 +                animation = new DoubleAnimation();
   1.113 +                animation.From = 0.0;
   1.114 +                animation.To = 1.0;
   1.115 +                animation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
   1.116 +
   1.117 +                Storyboard.SetTarget(animation, popup.Child);
   1.118 +                Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
   1.119 +                story.Children.Add(animation);
   1.120 +
   1.121 +                Debug.WriteLine("Fading the splash screen in");
   1.122 +
   1.123 +                story.Begin();
   1.124 +
   1.125 +                popup.IsOpen = true;
   1.126 +
   1.127 +                if (autohide)
   1.128 +                {
   1.129 +                    DispatcherTimer timer = new DispatcherTimer();
   1.130 +                    timer.Tick += (object sender, EventArgs e) =>
   1.131 +                    {
   1.132 +                        hide();
   1.133 +                    };
   1.134 +                    timer.Interval = TimeSpan.FromSeconds(1.2);
   1.135 +                    timer.Start();
   1.136 +                }
   1.137 +            }); 
   1.138 +        }
   1.139 +
   1.140 +
   1.141 +        public void hide(string options = null)
   1.142 +        {
   1.143 +            Deployment.Current.Dispatcher.BeginInvoke(() =>
   1.144 +            {
   1.145 +                if (!popup.IsOpen)
   1.146 +                {
   1.147 +                    return;
   1.148 +                }
   1.149 +
   1.150 +                popup.Child.Opacity = 1.0;
   1.151 +
   1.152 +                Storyboard story = new Storyboard();
   1.153 +                DoubleAnimation animation;
   1.154 +                animation = new DoubleAnimation();
   1.155 +                animation.From = 1.0;
   1.156 +                animation.To = 0.0;
   1.157 +                animation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
   1.158 +
   1.159 +                Storyboard.SetTarget(animation, popup.Child);
   1.160 +                Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
   1.161 +                story.Children.Add(animation);
   1.162 +                story.Completed += (object sender, EventArgs e) =>
   1.163 +                {
   1.164 +                    popup.IsOpen = false;
   1.165 +                };
   1.166 +                story.Begin();
   1.167 +            });
   1.168 +        }
   1.169 +    }
   1.170 +}

mercurial