commonroutesServer/www/plugins/cordova-plugin-splashscreen/src/wp/SplashScreen.cs
www/plugins/cordova-plugin-splashscreen/src/wp/SplashScreen.csRaw
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
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
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*/
14
15using System;
16using System.Net;
17using System.Windows;
18using System.Windows.Controls;
19using System.Windows.Documents;
20using System.Windows.Ink;
21using System.Windows.Input;
22using System.Windows.Media;
23using System.Windows.Media.Animation;
24using System.Windows.Shapes;
25using Microsoft.Phone.Info;
26using System.Windows.Controls.Primitives;
27using System.Diagnostics;
28using System.Windows.Media.Imaging;
29using System.Windows.Resources;
30using System.IO;
31using System.Xml.Linq;
32using System.Linq;
33using System.Windows.Threading;
34
35namespace 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
45 // Time until we dismiss the splashscreen
46 private int prefDelay = 3000;
47
48 // Whether we hide it by default
49 private bool prefAutoHide = true;
50
51 // Path to image to use
52 private string prefImagePath = "SplashScreenImage.jpg";
53
54 // static because autodismiss is only ever applied once, at app launch
55 // subsequent page loads should not cause the SplashScreen to be shown.
56 private static bool WasShown = false;
57
58 public SplashScreen()
59 {
60 LoadConfigPrefs();
61
62 Image SplashScreen = new Image()
63 {
64 Height = Application.Current.Host.Content.ActualHeight,
65 Width = Application.Current.Host.Content.ActualWidth,
66 Stretch = Stretch.Fill
67 };
68
69 var imageResource = GetSplashScreenImageResource();
70 if (imageResource != null)
71 {
72 BitmapImage splash_image = new BitmapImage();
73 splash_image.SetSource(imageResource.Stream);
74 SplashScreen.Source = splash_image;
75 }
76
77 // Instansiate the popup and set the Child property of Popup to SplashScreen
78 popup = new Popup() { IsOpen = false,
79 Child = SplashScreen,
80 HorizontalAlignment = HorizontalAlignment.Stretch,
81 VerticalAlignment = VerticalAlignment.Center
82
83 };
84 }
85
86 public override void OnInit()
87 {
88 // we only want to autoload on the first page load.
89 // but OnInit is called for every page load.
90 if (!SplashScreen.WasShown)
91 {
92 SplashScreen.WasShown = true;
93 show();
94 }
95 }
96
97 private void LoadConfigPrefs()
98 {
99 StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
100 if (streamInfo != null)
101 {
102 using (StreamReader sr = new StreamReader(streamInfo.Stream))
103 {
104 //This will Read Keys Collection for the xml file
105 XDocument configFile = XDocument.Parse(sr.ReadToEnd());
106
107 string configAutoHide = configFile.Descendants()
108 .Where(x => x.Name.LocalName == "preference")
109 .Where(x => (string)x.Attribute("name") == "AutoHideSplashScreen")
110 .Select(x => (string)x.Attribute("value"))
111 .FirstOrDefault();
112
113 bool bVal;
114 prefAutoHide = bool.TryParse(configAutoHide, out bVal) ? bVal : prefAutoHide;
115
116 string configDelay = configFile.Descendants()
117 .Where(x => x.Name.LocalName == "preference")
118 .Where(x => (string)x.Attribute("name") == "SplashScreenDelay")
119 .Select(x => (string)x.Attribute("value"))
120 .FirstOrDefault();
121 int nVal;
122 prefDelay = int.TryParse(configDelay, out nVal) ? nVal : prefDelay;
123
124 string configImage = configFile.Descendants()
125 .Where(x => x.Name.LocalName == "preference")
126 .Where(x => (string)x.Attribute("name") == "SplashScreen")
127 .Select(x => (string)x.Attribute("value"))
128 .FirstOrDefault();
129
130 if (!String.IsNullOrEmpty(configImage))
131 {
132 prefImagePath = configImage;
133 }
134 }
135 }
136 }
137
138 private StreamResourceInfo GetSplashScreenImageResource()
139 {
140 // Get the base filename for the splash screen images
141 string imageName = System.IO.Path.GetFileNameWithoutExtension(prefImagePath);
142 Uri imageUri = null;
143 StreamResourceInfo imageResource = null;
144
145 // First, try to get a resolution-specific splashscreen
146 try
147 {
148 // Determine the device's resolution
149 switch (ResolutionHelper.CurrentResolution)
150 {
151 case Resolutions.HD:
152 imageUri = new Uri(imageName + ".screen-720p.jpg", UriKind.Relative);
153 break;
154
155 case Resolutions.WVGA:
156 imageUri = new Uri(imageName + ".screen-WVGA.jpg", UriKind.Relative);
157 break;
158
159 case Resolutions.WXGA:
160 default:
161 imageUri = new Uri(imageName + ".screen-WXGA.jpg", UriKind.Relative);
162 break;
163 }
164
165 imageResource = Application.GetResourceStream(imageUri);
166 }
167 catch (Exception)
168 {
169 // It's OK if we didn't get a resolution-specific image
170 }
171
172 // Fallback to the default image name without decoration
173 if (imageResource == null)
174 {
175 imageUri = new Uri(prefImagePath, UriKind.Relative);
176 imageResource = Application.GetResourceStream(imageUri);
177 }
178
179 if (imageUri != null) Debug.WriteLine("INFO :: SplashScreen: using image {0}", imageUri.OriginalString);
180
181 return imageResource;
182 }
183
184 public void show(string options = null)
185 {
186 Deployment.Current.Dispatcher.BeginInvoke(() =>
187 {
188 if (!popup.IsOpen)
189 {
190 popup.Child.Opacity = 0;
191
192 Storyboard story = new Storyboard();
193 DoubleAnimation animation = new DoubleAnimation()
194 {
195 From = 0.0,
196 To = 1.0,
197 Duration = new Duration(TimeSpan.FromSeconds(0.2))
198 };
199
200 Storyboard.SetTarget(animation, popup.Child);
201 Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
202 story.Children.Add(animation);
203
204 story.Begin();
205
206 popup.IsOpen = true;
207
208 if (prefAutoHide)
209 {
210 StartAutoHideTimer();
211 }
212 }
213 });
214 }
215
216 public void hide(string options = null)
217 {
218 Deployment.Current.Dispatcher.BeginInvoke(() =>
219 {
220 if (popup.IsOpen)
221 {
222 popup.Child.Opacity = 1.0;
223
224 Storyboard story = new Storyboard();
225 DoubleAnimation animation = new DoubleAnimation()
226 {
227 From = 1.0,
228 To = 0.0,
229 Duration = new Duration(TimeSpan.FromSeconds(0.4))
230 };
231
232 Storyboard.SetTarget(animation, popup.Child);
233 Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
234 story.Children.Add(animation);
235 story.Completed += (object sender, EventArgs e) =>
236 {
237 popup.IsOpen = false;
238 };
239 story.Begin();
240 }
241 });
242 }
243
244 private void StartAutoHideTimer()
245 {
246 var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(prefDelay) };
247 timer.Tick += (object sender, EventArgs e) =>
248 {
249 hide();
250 timer.Stop();
251 };
252 timer.Start();
253 }
254 }
255}