예제 #1
0
        public async Task <int> CheckCredentials(string datatoken, string userid, string password)
        {
            //Check username and password
            System.Diagnostics.Debug.WriteLine("We are inside the outer task");
            ProgressDialog pd = new ProgressDialog(Activity);

            pd.SetMessage("Checking username and password");
            pd.SetCancelable(false);
            pd.Show();
            AlertDialog.Builder builder = new AlertDialog.Builder((Activity));
            await Task.Run(() =>
            {
                Debug.WriteLine("We are checking username");
                HttpWebResponse resp;
                try
                {
                    DataSetLocationResolver dslr = new DataSetLocationResolver();
                    dslr.ResolveFromToken(datatoken, out baseurl, out dataset);

                    System.Diagnostics.Debug.WriteLine("Base url :" + baseurl);

                    AccountStorage.SetContext(Activity);
                    AccountStorage.Set(userid, password, baseurl, dataset);


                    var req =
                        WebRequest.CreateHttp(AccountStorage.BaseUrl + "api/v1/datasets/" +
                                              AccountStorage.DataSet + "/");
                    req.Method            = "GET";
                    req.AllowAutoRedirect = false;
                    string credential     = userid + ":" + password;
                    req.Headers.Add("Authorization",
                                    "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(credential)));
                    // req.Get

                    resp = (HttpWebResponse)req.GetResponse();

                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        if (resp.GetResponseStream().CanRead)
                        {
                            Stream data = resp.GetResponseStream();
                            var reader  = new StreamReader(data);

                            string responseStr = reader.ReadToEnd();
                            Debug.WriteLine(responseStr);

                            if (responseStr.Contains("auth-required"))
                            {
                                Debug.WriteLine("Wrong credentials 2");
                                AccountStorage.ClearStorage();
                                Activity.RunOnUiThread(() =>
                                {
                                    if (pd.IsShowing)
                                    {
                                        pd.Dismiss();
                                    }

                                    builder.SetTitle("Wrong Credentials")
                                    .SetMessage("Please check your username and password and try again.")
                                    .SetNeutralButton("Okay", (sender2, args2) => { builder.Dispose(); })
                                    .SetCancelable(false);
                                    AlertDialog alert = builder.Create();
                                    alert.Show();
                                    Debug.WriteLine("We should have shown the dialog now");
                                });
                            }
                            else if (responseStr.Contains("permission-denied"))
                            {
                                Debug.WriteLine("permission issue");
                                AccountStorage.ClearStorage();
                                Activity.RunOnUiThread(() =>
                                {
                                    if (pd.IsShowing)
                                    {
                                        pd.Dismiss();
                                    }

                                    builder.SetTitle("Access Denied")
                                    .SetMessage("You donot have access to this dataset")
                                    .SetNeutralButton("Okay", (sender2, args2) => { builder.Dispose(); })
                                    .SetCancelable(false);
                                    AlertDialog alert = builder.Create();

                                    alert.Show();
                                });
                            }
                            else if (responseStr.Contains("dataset"))
                            {
                                Debug.WriteLine("Username and password was correct");
                                Activity.RunOnUiThread(() =>
                                {
                                    pd.SetMessage("Getting Account Info");
                                    pd.SetCancelable(false);
                                    if (!pd.IsShowing)
                                    {
                                        pd.Show();
                                    }
                                });
                                Task.Run(() =>
                                {
                                    //LOAD METHOD TO GET ACCOUNT INFO


                                    Debug.WriteLine("We are going to store the values");

                                    Debug.WriteLine("We have stored the values");
                                    Debug.WriteLine(AccountStorage.BaseUrl);
                                    Debug.WriteLine(AccountStorage.DataSet);
                                    Debug.WriteLine(AccountStorage.Password);
                                    Debug.WriteLine(AccountStorage.UserId);

                                    // Switch to next screen

                                    //HIDE PROGRESS DIALOG
                                    Activity.RunOnUiThread(() =>
                                    {
                                        if (pd.IsShowing)
                                        {
                                            pd.Dismiss();
                                        }

                                        Toast.MakeText(Activity, "Logged in", ToastLength.Short).Show();
                                        ((MainActivity)Activity).FragmentManager.PopBackStack();
                                        ((MainActivity)Activity).SetDrawerState(true);
                                        ((MainActivity)Activity).SwitchToFragment(
                                            MainActivity.FragmentTypes.Home);
                                    });
                                });
                            }
                        }
                    }
                }
                catch (WebException e)
                {
                    Debug.WriteLine("We have a problem");
                    Activity.RunOnUiThread(() =>
                    {
                        if (pd.IsShowing)
                        {
                            pd.Dismiss();
                        }
                    });
                    using (WebResponse response = e.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        Console.WriteLine("Error code: {0}", httpResponse.StatusCode);

                        if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            Debug.WriteLine("Wrong credentials");
                            AccountStorage.ClearStorage();
                            Activity.RunOnUiThread(() =>
                            {
                                try
                                {
                                    builder.SetTitle("Unauthorized")
                                    .SetMessage(
                                        "Please check your username and password and data token and try again.")
                                    .SetNeutralButton("Okay", (sender2, args2) => { builder.Dispose(); })
                                    .SetCancelable(false);

                                    AlertDialog alert = builder.Create();

                                    alert.Show();
                                }
                                catch (Exception e2)
                                {
                                    Debug.WriteLine("We have hit an error while showing the dialog :" + e2.Message);
                                    AccountStorage.ClearStorage();
                                }
                            });
                        }
                    }
                }


                catch (Exception e)
                {
                    // Catching any generic exception
                    Debug.WriteLine("We have hit a generic exception :" + e.Message);
                    AccountStorage.ClearStorage();
                    Activity.RunOnUiThread(() =>
                    {
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(Activity);
                        builder2.SetTitle("Error occured")
                        .SetMessage(e.Message +
                                    ". Please report this error to the developers. We are sorry for the inconvenience.")
                        .SetNeutralButton("Okay", (sender2, args2) => { builder2.Dispose(); })
                        .SetCancelable(false);
                        AlertDialog alert2 = builder2.Create();
                        alert2.Show();
                    });
                }

                return(true);
            });

            //    pd.Dismiss();

            System.Diagnostics.Debug.WriteLine("We are done with the outer task");
            return(0);
        }
 public LoginPageViewController(IntPtr handle) : base(handle)
 {
     _resolver = new DataSetLocationResolver();
 }
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Debug.WriteLine("On Create");
            RetainInstance = true;

            var    dslr = new DataSetLocationResolver();
            string url, id;

            dslr.ResolveFromToken("go.yn-hk1", out url, out id);

            Debug.WriteLine("URL is :" + url);
            Debug.WriteLine("Id is :" + id);

            try
            {
                var ct = new ControllerTest(((MainActivity)Activity).Ctrl);

                //ct.TestListOfProjects();
                //ct.TestRecentTasks();
                //ct.TestListOfTasks();
                //ct.TestSingleTask();
                //                ct.TestListOfTimeLogs();
                //                ct.TestListOfTimeLogsWithMaxResults();
                //                ct.TestListOfTimeLogsWithprojectId();
                //                ct.TestListOfTimeLogsWithstartDateFrom();
                //                ct.TestListOfTimeLogsWithstartDateTo();
                ct.TestListOfTimeLogsWithtaskId();
                //ct.TestAddATimeLog();

                //ct.TestGetATimeLog("-71");
                //ct.TestDeleteATimeLog(-71);

                string tid = "-87";
                //ct.TestUpdateATimeLogUpdateComment(tid);
                //ct.TestUpdateATimeLogUpdateInterruptTimeNotOpen(tid);
                ct.TestUpdateATimeLogUpdateLoggedTime(tid);
                // ct.TestUpdateATimeLogUpdateStartDate(tid);
                //ct.TestUpdateATimeLogUpdateTaskId(tid);
                //ct.TestUpdateATimeLogUpdateInterruptTime(tid);
                //ct.TestUpdateATimeLogUpdateLoggedTimeNotOpen(tid);
                // ct.TestUpdateEstimatedTime("330", 125);
                //ct.TestMarkTaskComplete("330");
                // ct.TestMarkTaskIncomplete("330");
            }
            catch (CannotReachServerException)
            {
                Debug.WriteLine("Unable to connect to network");
            }
            catch (CancelTimeLoggingException)
            {
                // Add this when needed
                Debug.WriteLine("Cancel Time Logging Right now");
            }
            catch (StatusNotOkayException)
            {
                Debug.WriteLine("Status Not Okay!!");
            }
            catch (Exception)
            {
                // For any other weird exceptions
                Debug.WriteLine("Some other weird exception");
            }

            // Create your fragment here
        }