michael@0: /* michael@0: Licensed under the Apache License, Version 2.0 (the "License"); michael@0: you may not use this file except in compliance with the License. michael@0: You may obtain a copy of the License at michael@0: michael@0: http://www.apache.org/licenses/LICENSE-2.0 michael@0: michael@0: Unless required by applicable law or agreed to in writing, software michael@0: distributed under the License is distributed on an "AS IS" BASIS, michael@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: See the License for the specific language governing permissions and michael@0: limitations under the License. michael@0: */ michael@0: michael@0: using System; michael@0: using System.Net; michael@0: using System.Windows; michael@0: using System.Windows.Controls; michael@0: using System.Windows.Documents; michael@0: using System.Windows.Ink; michael@0: using System.Windows.Input; michael@0: using System.Windows.Media; michael@0: using System.Windows.Media.Animation; michael@0: using System.Windows.Shapes; michael@0: using Microsoft.Phone.Info; michael@0: using System.Windows.Controls.Primitives; michael@0: using System.Diagnostics; michael@0: using System.Windows.Media.Imaging; michael@0: using System.Windows.Resources; michael@0: using System.IO; michael@0: using System.Xml.Linq; michael@0: using System.Linq; michael@0: using System.Windows.Threading; michael@0: michael@0: namespace WPCordovaClassLib.Cordova.Commands michael@0: { michael@0: /// michael@0: /// Listens for changes to the state of the battery on the device. michael@0: /// Currently only the "isPlugged" parameter available via native APIs. michael@0: /// michael@0: public class SplashScreen : BaseCommand michael@0: { michael@0: private Popup popup; michael@0: private bool autohide = true; michael@0: michael@0: private static bool WasShown = false; michael@0: michael@0: public SplashScreen() michael@0: { michael@0: Image SplashScreen = new Image(); michael@0: BitmapImage splash_image = new BitmapImage(); michael@0: splash_image.SetSource(Application.GetResourceStream(new Uri(@"SplashScreenImage.jpg", UriKind.Relative)).Stream); michael@0: SplashScreen.Source = splash_image; michael@0: michael@0: // Instansiate the popup and set the Child property of Popup to SplashScreen michael@0: popup = new Popup() {IsOpen = false, Child = SplashScreen }; michael@0: // Orient the popup accordingly michael@0: popup.HorizontalAlignment = HorizontalAlignment.Stretch; michael@0: popup.VerticalAlignment = VerticalAlignment.Center; michael@0: michael@0: michael@0: LoadConfigValues(); michael@0: } michael@0: michael@0: public override void OnInit() michael@0: { michael@0: // we only want to autoload the first time a page is loaded. michael@0: if (!WasShown) michael@0: { michael@0: WasShown = true; michael@0: show(); michael@0: } michael@0: } michael@0: michael@0: void LoadConfigValues() michael@0: { michael@0: StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative)); michael@0: michael@0: if (streamInfo != null) michael@0: { michael@0: StreamReader sr = new StreamReader(streamInfo.Stream); michael@0: //This will Read Keys Collection for the xml file michael@0: XDocument document = XDocument.Parse(sr.ReadToEnd()); michael@0: michael@0: var preferences = from results in document.Descendants() michael@0: where (string)results.Attribute("name") == "AutoHideSplashScreen" michael@0: select (string)results.Attribute("value") == "true"; michael@0: michael@0: if (preferences.Count() > 0 && preferences.First() == false) michael@0: { michael@0: autohide = false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void show(string options = null) michael@0: { michael@0: Deployment.Current.Dispatcher.BeginInvoke(() => michael@0: { michael@0: if (popup.IsOpen) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: popup.Child.Opacity = 0; michael@0: michael@0: Storyboard story = new Storyboard(); michael@0: DoubleAnimation animation; michael@0: animation = new DoubleAnimation(); michael@0: animation.From = 0.0; michael@0: animation.To = 1.0; michael@0: animation.Duration = new Duration(TimeSpan.FromSeconds(0.2)); michael@0: michael@0: Storyboard.SetTarget(animation, popup.Child); michael@0: Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity")); michael@0: story.Children.Add(animation); michael@0: michael@0: Debug.WriteLine("Fading the splash screen in"); michael@0: michael@0: story.Begin(); michael@0: michael@0: popup.IsOpen = true; michael@0: michael@0: if (autohide) michael@0: { michael@0: DispatcherTimer timer = new DispatcherTimer(); michael@0: timer.Tick += (object sender, EventArgs e) => michael@0: { michael@0: hide(); michael@0: }; michael@0: timer.Interval = TimeSpan.FromSeconds(1.2); michael@0: timer.Start(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: michael@0: public void hide(string options = null) michael@0: { michael@0: Deployment.Current.Dispatcher.BeginInvoke(() => michael@0: { michael@0: if (!popup.IsOpen) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: popup.Child.Opacity = 1.0; michael@0: michael@0: Storyboard story = new Storyboard(); michael@0: DoubleAnimation animation; michael@0: animation = new DoubleAnimation(); michael@0: animation.From = 1.0; michael@0: animation.To = 0.0; michael@0: animation.Duration = new Duration(TimeSpan.FromSeconds(0.4)); michael@0: michael@0: Storyboard.SetTarget(animation, popup.Child); michael@0: Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity")); michael@0: story.Children.Add(animation); michael@0: story.Completed += (object sender, EventArgs e) => michael@0: { michael@0: popup.IsOpen = false; michael@0: }; michael@0: story.Begin(); michael@0: }); michael@0: } michael@0: } michael@0: }