Ejemplo n.º 1
0
		protected override void OnResume ()
		{
			base.OnResume ();
			Task startupWork = new Task (() => {
				//INSTANCE DBREPOSITORY
				DBRepository dbr = new DBRepository ();
				//CREATION DE LA BDD
				dbr.CreateDB ();
				//CREATION DES TABLES
				dbr.CreateTable ();
				//TEST DE CONNEXION
				var connectivityManager = (ConnectivityManager)GetSystemService (ConnectivityService);
				var t = DateTime.Now.ToString ("dd_MM_yy");
				string dir_log = (Android.OS.Environment.GetExternalStoragePublicDirectory (Android.OS.Environment.DirectoryDownloads)).ToString ();
				//Shared Preference
				ISharedPreferences pref = Application.Context.GetSharedPreferences ("AppInfo", FileCreationMode.Private);
				string log = pref.GetString ("Log", String.Empty);
				//GetTelId
				TelephonyManager tel = (TelephonyManager)this.GetSystemService (Context.TelephonyService);
				var telId = tel.DeviceId;
				//Si il n'y a pas de shared pref
				if (log == String.Empty) {
					Data.log_file = Path.Combine (dir_log, t + "_" + telId + "_log.txt");
					ISharedPreferencesEditor edit = pref.Edit ();
					edit.PutString ("Log", Data.log_file);
					edit.Apply ();
				} else {
					//il y a des shared pref
					Data.log_file = pref.GetString ("Log", String.Empty);
					if (((File.GetCreationTime (Data.log_file)).CompareTo (DateTime.Now)) > 3) {
						File.Delete (Data.log_file);
						Data.log_file = Path.Combine (dir_log, t + "_" + telId + "_log.txt");
						ISharedPreferencesEditor edit = pref.Edit ();
						edit.PutString ("Log", Data.log_file);
						edit.Apply ();
						Data.log_file = pref.GetString ("Log", String.Empty);
					}
				}
				File.AppendAllText (Data.log_file, "[LAUNCH] DMS lancée le " + DateTime.Now.ToString ("F") + "\n");
				bool App_Connec = false;
				while (!App_Connec) {
					var activeConnection = connectivityManager.ActiveNetworkInfo;
					if ((activeConnection != null) && activeConnection.IsConnected) {
						try {
							string _url = "http://dms.jeantettransport.com/api/authen?chaufmdp=";
							var webClient = new WebClient ();
							webClient.Headers [HttpRequestHeader.ContentType] = "application/json";
							string userData = "";
							userData = webClient.DownloadString (_url);
							System.Console.WriteLine ("\n Webclient User Terminé ...");
							//GESTION DU XML
							JsonArray jsonVal = JsonArray.Parse (userData) as JsonArray;
							var jsonArr = jsonVal;
							foreach (var row in jsonArr) {
								var checkUser = dbr.user_AlreadyExist (row ["userandsoft"], row ["usertransics"], row ["mdpandsoft"], "true");
								Console.WriteLine ("\n" + checkUser + " " + row ["userandsoft"]);
								if (!checkUser) {
									var IntegUser = dbr.InsertDataUser (row ["userandsoft"], row ["usertransics"], row ["mdpandsoft"], "true");
									Console.WriteLine ("\n" + IntegUser);
								}
							}
							App_Connec = true;
						} catch (System.Exception ex) {
							System.Console.WriteLine (ex);
							Insights.Report (ex);
							App_Connec = false;
							AndHUD.Shared.ShowError (this, "Une erreur c'est produite lors du lancement, réessaie dans 5 secondes", MaskType.Black, TimeSpan.FromSeconds (5));
						}
					} else {
						App_Connec = false;
						//AndHUD.Shared.ShowError(this, "Pas de connexion, réessaie dans 5 secondes", MaskType.Black, TimeSpan.FromSeconds(5));
						Toast.MakeText (this, "Pas de connexion", ToastLength.Long).Show ();
						Thread.Sleep (5000);
					}
				}
			});
			startupWork.ContinueWith (t => {
				//Is a user login ?
				DBRepository dbr = new DBRepository ();
				var user_Login = dbr.is_user_Log_In ();
				if (!(user_Login == string.Empty)) {
					//Data.userAndsoft = user_Login;
					dbr.setUserdata (user_Login);
					File.AppendAllText (Data.log_file, "Connexion de " + Data.userAndsoft + " à " + DateTime.Now.ToString ("t") + "\n");
					StartActivity (new Intent (Application.Context, typeof(HomeActivity)));
				} else {
					StartActivity (new Intent (Application.Context, typeof(MainActivity)));
				}
			}, TaskScheduler.FromCurrentSynchronizationContext ());
			startupWork.Start ();
		}