Subversion Repository Public Repository

Nextrek

Diff Revisions 798 vs 799 for /Android/SmartCharging/SmartCharging_WP/SmartCharging/App.xaml.cs

Diff revisions: vs.
  @@ -1,4 +1,7 @@
1 - using SmartCharging.Common;
1 + using Facebook.Client;
2 + using SmartCharging.Common;
3 + using SmartCharging.DataModel;
4 + using SmartCharging.Net;
2 5 using System;
3 6 using System.Collections.Generic;
4 7 using System.IO;
  @@ -6,8 +9,11 @@
6 9 using System.Runtime.InteropServices.WindowsRuntime;
7 10 using Windows.ApplicationModel;
8 11 using Windows.ApplicationModel.Activation;
12 + using Windows.ApplicationModel.Background;
9 13 using Windows.Foundation;
10 14 using Windows.Foundation.Collections;
15 + using Windows.Security.Credentials;
16 + using Windows.UI.Core;
11 17 using Windows.UI.Xaml;
12 18 using Windows.UI.Xaml.Controls;
13 19 using Windows.UI.Xaml.Controls.Primitives;
  @@ -27,6 +33,8 @@
27 33 public sealed partial class App : Application
28 34 {
29 35 private TransitionCollection transitions;
36 + private SmartChargeAPI SMA;
37 + const string backgroundName = "SCBatteryBackgroundTaskTimer";
30 38
31 39 /// <summary>
32 40 /// Initializes the singleton application object. This is the first line of authored code
  @@ -36,6 +44,8 @@
36 44 {
37 45 this.InitializeComponent();
38 46 this.Suspending += this.OnSuspending;
47 + this.SMA = SmartChargeAPI.Instance;
48 + RegisterbackgroundTask();
39 49
40 50
41 51 }
  @@ -106,16 +116,62 @@
106 116 // When the navigation stack isn't restored navigate to the first page,
107 117 // configuring the new page by passing required information as a navigation
108 118 // parameter.
109 - if (!rootFrame.Navigate(typeof(IntroPage), e.Arguments))
119 +
120 + if (Session.ActiveSession != null && Session.ActiveSession.CurrentAccessTokenData != null && !String.IsNullOrEmpty(Session.ActiveSession.CurrentAccessTokenData.AccessToken))
110 121 {
111 - throw new Exception("Failed to create initial page");
122 + /*check if fb session is still valid. if it is valid, login with fb.
123 + * else check if username and password are stored; if they are stored, strandard login
124 + */
125 + GraphUser gu = await Utils.RetriveUserInfo(Session.ActiveSession.CurrentAccessTokenData.AccessToken);
126 + if (gu != null && !String.IsNullOrEmpty(gu.Id))
127 + {
128 + User user = await this.SMA.FacebookLogin(gu.Id, Session.ActiveSession.CurrentAccessTokenData.AccessToken);
129 + if (user != null)
130 + {
131 + rootFrame.Navigate(typeof(StandardUserLoggedInPage), e.Arguments);
132 + }
133 + else
134 + {
135 + rootFrame.Navigate(typeof(IntroPage), e.Arguments);
136 + }
137 + }
138 +
112 139 }
140 + else
141 + {
142 + CredentialStorage cs = new CredentialStorage();
143 + PasswordCredential credentials = cs.GetCredential();
144 + // autologin
145 + if (credentials != null)
146 + {
147 + credentials.RetrievePassword();
148 + User user = await this.SMA.Login(credentials.UserName, credentials.Password);
149 +
150 + if (user != null)
151 + {
152 + rootFrame.Navigate(typeof(StandardUserLoggedInPage), e.Arguments);
153 + }
154 + else
155 + {
156 + cs.RemoveCredentials();
157 + rootFrame.Navigate(typeof(IntroPage), e.Arguments);
158 + }
159 + }
160 + else
161 + {
162 + rootFrame.Navigate(typeof(IntroPage), e.Arguments);
163 + }
164 +
165 + }
166 +
167 +
113 168 }
114 169
115 170 // Ensure the current window is active.
116 171 Window.Current.Activate();
117 172 }
118 173
174 +
119 175 /// <summary>
120 176 /// Restores the content transitions after the app has launched.
121 177 /// </summary>
  @@ -140,8 +196,45 @@
140 196 deferral.Complete();
141 197 }
142 198
143 -
144 199
200 + protected override void OnActivated(IActivatedEventArgs args)
201 + {
202 + base.OnActivated(args);
203 + //Session.OnFacebookAuthenticationFinished += OnFacebookAuthenticationFinished;
204 + try
205 + {
206 + var protocolArgs = args as ProtocolActivatedEventArgs;
207 + LifecycleHelper.FacebookAuthenticationReceived(protocolArgs);
208 + }
209 + catch { }
210 +
211 + }
212 +
213 + private async void RegisterbackgroundTask()
214 + {
215 + if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == backgroundName))
216 + {
217 + // One register it once
218 + return;
219 + }
220 + await BackgroundExecutionManager.RequestAccessAsync();
221 + var builder = new BackgroundTaskBuilder();
222 + var trigger = new TimeTrigger(30, false);
223 +
224 + builder.Name = backgroundName;
225 + builder.TaskEntryPoint = typeof(WindowsRuntimeComponent1.BatteryBackgroundTask).FullName;
226 + builder.SetTrigger(trigger);
227 +
228 + var registration = builder.Register();
229 + }
230 +
231 + /*private void OnFacebookAuthenticationFinished(AccessTokenData session)
232 + {
233 + if (String.IsNullOrEmpty(session.AccessToken))
234 + {
235 + bool result = true;//await SMA.FacebookLogin(session.FacebookId, session.)
236 + }
237 + }*/
145 238
146 239 }
147 240 }