public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Android.Resource.Id.Home: { mDrawerToggle.OnOptionsItemSelected(item); return(true); } case Resource.Id.action_exit: { this.RunOnUiThread(() => { Android.App.AlertDialog.Builder alertDiag = new Android.App.AlertDialog.Builder(this); alertDiag.SetTitle(Resource.String.DialogHeaderGeneric); alertDiag.SetMessage(Resource.String.appLogoutMessage); alertDiag.SetIcon(Resource.Drawable.alert); alertDiag.SetPositiveButton(Resource.String.DialogButtonYes, (senderAlert, args) => { try { objdb = new DBaseOperations(); var lstu = objdb.selectTable(); if (lstu != null && lstu.Count > default(int)) { var uobj = lstu.FirstOrDefault(); objdb.updateTable(new UserLoginInfo() { Id = uobj.Id, EmailId = uobj.EmailId, GoodName = uobj.GoodName, Password = "******", IsAdmin = uobj.IsAdmin, AuthToken = uobj.AuthToken, ProfilePicture = uobj.ProfilePicture }); } } catch { } Intent intent = new Intent(this, typeof(MainActivity)); StartActivity(intent); this.Finish(); }); alertDiag.SetNegativeButton(Resource.String.DialogButtonNo, (senderAlert, args) => { }); Dialog diag = alertDiag.Create(); diag.Show(); diag.SetCanceledOnTouchOutside(false); }); return(true); } default: return(base.OnOptionsItemSelected(item)); } }
private void LoginCall(ProgressDialog dialog, Activity curActivity, string username, string password) { try { var client = new RestClient(Common.UrlBase); var request = new RestRequest("Account/SignIn", Method.GET); request.AddHeader("Content-Type", "application/json"); request.AddQueryParameter("username", username); request.AddQueryParameter("password", password); request.AddQueryParameter("userDeviceId", Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId)); IRestResponse response = client.Execute(request); var content = response.Content; var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject <AccountResponse>(content); if (responseObj.Status == ResponseStatus.Successful && !string.IsNullOrEmpty(responseObj.AuthToken)) { string strGoodName = string.Empty; try { objdb = new DBaseOperations(); var lst = objdb.selectTable(); if (lst == null || lst.Count <= default(int)) { var usr = this.FetchUserDetails(responseObj.AuthToken); if (usr != null) { strGoodName = string.Format("{0} {1}", usr.FirstName, usr.LastName); } else { strGoodName = this.GetUserFullName(responseObj.AuthToken); } objdb.createDatabase(); objdb.insertIntoTable(new UserLoginInfo() { EmailId = username, GoodName = strGoodName, Password = password, IsAdmin = (usr != null && usr.IsAdmin ? 1 : default(int)), AuthToken = responseObj.AuthToken }); } else if (lst != null && lst.Count > default(int)) { /*objdb.removeTable(lst.FirstOrDefault());*/ var usr = this.FetchUserDetails(responseObj.AuthToken); var uobj = lst.FirstOrDefault(); if (!string.IsNullOrEmpty(uobj.GoodName)) { if (usr != null) { strGoodName = string.Format("{0} {1}", usr.FirstName, usr.LastName); } else { strGoodName = this.GetUserFullName(responseObj.AuthToken); } } else { strGoodName = uobj.GoodName; } objdb.updateTable(new UserLoginInfo() { Id = uobj.Id, EmailId = username, GoodName = strGoodName, Password = password, IsAdmin = (usr != null && usr.IsAdmin ? 1 : default(int)), AuthToken = responseObj.AuthToken, ProfilePicture = uobj.ProfilePicture }); } } catch { } Intent intent = new Intent(curActivity, typeof(MainContentActivity)); StartActivity(intent); curActivity.Finish(); } else { if (string.IsNullOrEmpty(responseObj.Error)) { throw new Exception(responseObj.Error); } else { throw new Exception("Invalid credential."); } } } catch (Exception ex) { curActivity.RunOnUiThread(() => { Android.App.AlertDialog.Builder alertDiag = new Android.App.AlertDialog.Builder(curActivity); alertDiag.SetTitle(Resource.String.DialogHeaderError); alertDiag.SetMessage(ex.Message); alertDiag.SetIcon(Resource.Drawable.alert); alertDiag.SetPositiveButton(Resource.String.DialogButtonOk, (senderAlert, args) => { }); Dialog diag = alertDiag.Create(); diag.Show(); diag.SetCanceledOnTouchOutside(false); }); } finally { if (dialog != null && dialog.IsShowing) { dialog.Hide(); dialog.Dismiss(); } } }
public override void OnActivityResult(int requestCode, int resultCode, Intent data) { try { if (Build.VERSION.SdkInt >= Build.VERSION_CODES.M) { if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this.Activity, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted) { Android.Support.V4.App.ActivityCompat.RequestPermissions(this.Activity, new String[] { Manifest.Permission.WriteExternalStorage, Manifest.Permission.ReadExternalStorage }, 53); } else { if (requestCode == 102) { Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); Uri contentUri = Uri.FromFile(_file); mediaScanIntent.SetData(contentUri); this.Activity.SendBroadcast(mediaScanIntent); int height = profile_image0.Height; int width = Resources.DisplayMetrics.WidthPixels; using (Android.Graphics.Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height)) { profile_image0.RecycleBitmap(); profile_image0.SetImageBitmap(bitmap); try { this.SetProfileImageToNavMenuHeader(this.Activity, bitmap); } catch { } _profilepicbase64 = BitmapHelpers.BitmapToBase64(bitmap); try { objdb = new DBaseOperations(); var lstu = objdb.selectTable(); if (lstu != null && lstu.Count > default(int)) { var uobj = lstu.FirstOrDefault(); objdb.updateTable(new UserLoginInfo() { Id = uobj.Id, EmailId = uobj.EmailId, GoodName = uobj.GoodName, Password = uobj.Password, IsAdmin = uobj.IsAdmin, AuthToken = uobj.AuthToken, ProfilePicture = _profilepicbase64 }); } } catch { } } } } } else { if (requestCode == 102) { Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); Uri contentUri = Uri.FromFile(_file); mediaScanIntent.SetData(contentUri); this.Activity.SendBroadcast(mediaScanIntent); int height = profile_image0.Height; int width = Resources.DisplayMetrics.WidthPixels; using (Android.Graphics.Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height)) { profile_image0.RecycleBitmap(); profile_image0.SetImageBitmap(bitmap); try { this.SetProfileImageToNavMenuHeader(this.Activity, bitmap); } catch { } _profilepicbase64 = BitmapHelpers.BitmapToBase64(bitmap); try { objdb = new DBaseOperations(); var lstu = objdb.selectTable(); if (lstu != null && lstu.Count > default(int)) { var uobj = lstu.FirstOrDefault(); objdb.updateTable(new UserLoginInfo() { Id = uobj.Id, EmailId = uobj.EmailId, GoodName = uobj.GoodName, Password = uobj.Password, IsAdmin = uobj.IsAdmin, AuthToken = uobj.AuthToken, ProfilePicture = _profilepicbase64 }); } } catch { } } } } } catch (Exception ex) { this.Activity.RunOnUiThread(() => { Android.App.AlertDialog.Builder alertDiag = new Android.App.AlertDialog.Builder(this.Activity); alertDiag.SetTitle(Resource.String.DialogHeaderError); alertDiag.SetMessage(ex.Message); alertDiag.SetIcon(Resource.Drawable.alert); alertDiag.SetPositiveButton(Resource.String.DialogButtonOk, (senderAlert, args) => { }); Dialog diag = alertDiag.Create(); diag.Show(); diag.SetCanceledOnTouchOutside(false); }); } }