private void SaveUrl(string url) { // First save the current settings -> We need them if the connection to the new server fails -> then we have to reset to these settings string CurrentUrl = DataAccessLayer.Utilities.ServerIP; DataAccessLayer.NetworkState CurrentNetworkState = UI.MainActivity.User.NetworkStatus; // First we set the user online to check if we have any connection to the server UI.MainActivity.User.NetworkStatus = UI.MainActivity._networkstate; DataAccessLayer.Utilities.ServerIP = url; // The user has to authenticate itself to the new server var dialog = new AuthenticationDialog(this); dialog.Show(SupportFragmentManager, "dialog"); // Event handler after the dialog has been dismissed dialog.Dismissed += (s, e) => { if (e.Result == true) { // Everything has been OK and the user is connected to the server and DB Toast.MakeText (_mainActivity, Resources.GetString(Resource.String.LoggedSuccessfully),ToastLength.Short); _OfflineSwitch.Checked = true; UI.MainActivity.User.NetworkStatus = UI.MainActivity._networkstate; // We have to show the two notifications NotificationManager notificationManager = (NotificationManager) _mainActivity.GetSystemService ( Android.Content.Context.NotificationService); Notification.Builder builder = new Notification.Builder (_mainActivity) .SetSmallIcon (UI.Resource.Drawable.ic_network_connected) .SetAutoCancel (false) .SetSound (Android.Media.RingtoneManager.GetDefaultUri ( Android.Media.RingtoneType.Notification)) .SetLights (54,3000,5) .SetContentText ( Resources.GetString(Resource.String.NetworkConnected)) .SetContentTitle (Resources.GetString(Resource.String.AppName) + " - " + Resources.GetString(Resource.String.Connected)) .SetContentIntent ( PendingIntent.GetActivity ( _mainActivity, (int)PendingIntentFlags.UpdateCurrent, new Intent (this, typeof(SettingsActivity)), 0)) .SetTicker (Resources.GetString(Resource.String.Connected)); //Notification notification = builder.Notification; Notification notification = builder.Build(); notificationManager.Notify (1, notification); builder = new Notification.Builder (_mainActivity) .SetSmallIcon (UI.Resource.Drawable.ic_action_online) .SetAutoCancel (false) .SetLights (54,3000,5) .SetContentText ( Resources.GetString(Resource.String.UserOnline)) .SetContentTitle (Resources.GetString(Resource.String.AppName) + " - " + "Online") .SetTicker (Resources.GetString(Resource.String.UserOnline)); //notification = builder.Notification; notification = builder.Build(); notificationManager.Notify (2, notification); // Set the new URL in the Offline Database BusinessLayer.User.SetUrl(url); Android.Widget.Toast.MakeText(_mainActivity,_mainActivity.Resources.GetString(Resource.String.LoggedSuccessfully), Android.Widget.ToastLength.Short).Show(); } else { // There is an Error and the user is disconnected _OfflineSwitch.Checked = false; // There has been a connection failure // The app has to stay offline new AlertDialog.Builder(this) .SetPositiveButton("OK", (senders, _args) => { // User pressed yes }) .SetMessage(Resource.String.ConnectionFailedTitle) .SetTitle(Resource.String.ConnectionFailedTitle) .Show() ; // Reset the global URL DataAccessLayer.Utilities.ServerIP = CurrentUrl; UI.MainActivity.User.NetworkStatus = CurrentNetworkState; _edUrl.Text = DataAccessLayer.Utilities.ServerIP; } }; }
private void OfflineSwitchChanged() { if (_OfflineSwitch.Checked == false) { UI.MainActivity.User.NetworkStatus = DataAccessLayer.NetworkState.Disconnected; Notification.Builder builder = new Notification.Builder (this) .SetSmallIcon (UI.Resource.Drawable.ic_action_offline) .SetAutoCancel (false) .SetSound (Android.Media.RingtoneManager.GetDefaultUri ( Android.Media.RingtoneType.Notification)) .SetLights (54,3000,5) .SetContentText ( Resources.GetString(Resource.String.UserOffline)) .SetContentTitle (Resources.GetString(Resource.String.AppName) + " - " + "Offline") .SetTicker (Resources.GetString(Resource.String.UserOffline)); Notification notification = builder.Build(); NotificationManager notificationManager = (NotificationManager) _mainActivity.GetSystemService ( Android.Content.Context.NotificationService); notificationManager.Notify (2, notification); } else { // If the user wants to go online for the first time // he or she must be authenticated // Run the Authentication Dialog // First save the current settings -> We need them if the connection to the new server fails -> then we have to reset to these settings string CurrentUrl = DataAccessLayer.Utilities.ServerIP; DataAccessLayer.NetworkState CurrentNetworkState = UI.MainActivity.User.NetworkStatus; // Just run the Authentication Dialog, if the user has never logged online before if (String.IsNullOrEmpty ( UI.MainActivity.User.IdSession )) { // First we set the user online to check if we have any connection to the server UI.MainActivity.User.NetworkStatus = UI.MainActivity._networkstate; DataAccessLayer.Utilities.ServerIP = _edUrl.Text; var dialog = new AuthenticationDialog(this); dialog.Dismissed += (s, e) => { if (e.Result == true) { // Everything has been OK and the user is connected to the server and DB Toast.MakeText (_mainActivity, Resources.GetString(Resource.String.LoggedSuccessfully),ToastLength.Short); _OfflineSwitch.Checked = true; UI.MainActivity.User.NetworkStatus = UI.MainActivity._networkstate; NotificationManager notificationManager = (NotificationManager) _mainActivity.GetSystemService ( Android.Content.Context.NotificationService); Notification.Builder builder = new Notification.Builder (_mainActivity) .SetSmallIcon (UI.Resource.Drawable.ic_action_online) .SetAutoCancel (false) .SetSound (Android.Media.RingtoneManager.GetDefaultUri ( Android.Media.RingtoneType.Notification)) .SetLights (54,3000,5) .SetContentText ( Resources.GetString(Resource.String.UserOnline)) .SetContentTitle (Resources.GetString(Resource.String.AppName) + " - " + "Online") .SetTicker (Resources.GetString(Resource.String.UserOnline)); //Notification notification = builder.Notification; Notification notification = builder.Build(); notificationManager.Notify (2, notification); } else { // There is an Error and the user is disconnected _OfflineSwitch.Checked = false; // There has been a connection failure // The app has to stay offline new AlertDialog.Builder(this) .SetPositiveButton("OK", (senders, _args) => { // User pressed yes }) .SetMessage(Resource.String.ConnectionFailedTitle) .SetTitle(Resource.String.ConnectionFailedTitle) .Show() ; // Reset the global status UI.MainActivity.User.NetworkStatus = CurrentNetworkState; } }; dialog.Show(SupportFragmentManager, "dialog"); } else { if (UI.MainActivity.User.TestConnection() == true) { // There is connection _OfflineSwitch.Checked = true; UI.MainActivity.User.NetworkStatus = UI.MainActivity._networkstate; // Sent a notification NotificationManager notificationManager = (NotificationManager) _mainActivity.GetSystemService ( Android.Content.Context.NotificationService); Notification.Builder builder = new Notification.Builder (_mainActivity) .SetSmallIcon (UI.Resource.Drawable.ic_action_online) .SetAutoCancel (false) .SetSound (Android.Media.RingtoneManager.GetDefaultUri ( Android.Media.RingtoneType.Notification)) .SetLights (54,3000,5) .SetContentText (Resources.GetString(Resource.String.UserOnline)) .SetContentTitle (Resources.GetString(Resource.String.AppName) + " - " + "Online") .SetTicker (Resources.GetString(Resource.String.UserOnline)); Notification notification = builder.Build(); notificationManager.Notify (2, notification); } else { // There is no connection _OfflineSwitch.Checked = false; UI.MainActivity.User.NetworkStatus = DataAccessLayer.NetworkState.Disconnected; // There has been a connection failure // The app has to stay offline new AlertDialog.Builder(this) .SetPositiveButton("OK", (senders, _args) => { // User pressed yes }) .SetMessage(Resource.String.ConnectionFailedTitle) .SetTitle(Resource.String.ConnectionFailedTitle) .Show() ; } } // Ask to upload any unsync data if the user is online if (offlineTasks != 0 &&MainActivity.User.NetworkStatus != DataAccessLayer.NetworkState.Disconnected) { UploadData(); } } }