示例#1
0
        /// <summary>
        /// Intercept request.
        /// </summary>
        /// <returns>The intercept request.</returns>
        /// <param name="view">View.</param>
        /// <param name="request">Request.</param>
        public override WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, IWebResourceRequest request)
        {
            string url  = request.Url.ToString();
            string ext  = MimeTypeMap.GetFileExtensionFromUrl(url);
            string mime = MimeTypeMap.Singleton.GetMimeTypeFromExtension(ext);

            if (url.ToLower().Contains("logoff"))
            {
                Xamarin.Forms.Device.BeginInvokeOnMainThread(App.Instance.Logout);
            }
            else if (request.Url.ToString().Contains("geofenceAutoCreate"))
            {
            }
            else if (mime != null)
            {
                HttpURLConnection conn = (HttpURLConnection) new URL(url).OpenConnection();
                foreach (var i in request.RequestHeaders)
                {
                    conn.SetRequestProperty(i.Key, i.Value);
                }
                var webResourceResponse = new WebResourceResponse(mime, "UTF-8", conn.InputStream);
                return(webResourceResponse);
            }

            BrowserPage.CheckWebSession();
            return(base.ShouldInterceptRequest(view, request));
        }
        public static bool canInitWithRequest(NSUrlRequest request)
        {
            BrowserPage.CheckWebSession();

            if (request.Body != null)
            {
                if (request.Body.ToString().Contains("Logoff"))
                {
                    Device.BeginInvokeOnMainThread(App.Instance.Logout);
                }

                else if (request.Url.ToString().Contains("localisation"))
                {
                    return(!request.Body.ToString().Contains("Longitude") && !request.Body.ToString().Contains("Latitude") && request.Body.ToString().Contains("pheidiparams"));
                }
                else if (request.Url.ToString().Contains("geofenceAutoCreate"))
                {
                    return(!CurrentlyProcessingRequestLocaly);
                }
            }
            if (request.Url.ToString().Contains("ImportImage"))
            {
                try
                {
                    var          param = request.Body.ToString().Split('&');
                    PheidiParams pp    = new PheidiParams();
                    int          index = 0;
                    for (int i = 0; i < param.Length; i++)
                    {
                        if (param[i].Contains("pheidiparams"))
                        {
                            pp.Load(WebUtility.UrlDecode(param[i]));
                            index = i;
                        }
                    }
                    Task.Run(async() =>
                    {
                        string answer = "";
                        if (pp.ContainsKey("FIELDVALUE"))
                        {
                            if (!string.IsNullOrEmpty(pp["FIELDVALUE"]))
                            {
                                var dic = new Dictionary <string, string>();
                                dic.Add("OTHERFIELD", pp["FIELD"]);
                                dic.Add("NOSEQ", pp["NOSEQ"]);
                                dic.Add("FIELD", pp["FIELD"]);

                                string p = "";
                                foreach (var d in dic)
                                {
                                    p += d.Key + "**:**" + d.Value + "**,**";
                                }
                                var parameters = new Dictionary <string, string> {
                                    { "pheidiaction", "GetFieldValueFromOtherField" }, { "pheidiparams", p }
                                };
                                HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30));
                                if (response != null)
                                {
                                    if (response.StatusCode == HttpStatusCode.OK)
                                    {
                                        string responseContent = response.Content.ReadAsStringAsync().Result;
                                        Debug.WriteLine("Reponse:" + responseContent);
                                        try
                                        {
                                            answer = PheidiNetworkManager.GetFields(responseContent)[0][dic["FIELD"]] as string;
                                        }
                                        catch (Exception e)
                                        {
                                            Debug.WriteLine(e.Message);
                                        }
                                    }
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(answer))
                        {
                            var p   = "";
                            var dic = new Dictionary <string, string>();
                            dic.Add("FIELD", pp["FIELD"]);
                            dic.Add("NOSEQ", pp["NOSEQ"]);
                            dic.Add("VALUE", "NULL");

                            foreach (var d in dic)
                            {
                                p += d.Key + "**:**" + d.Value + "**,**";
                            }
                            var parameters = new Dictionary <string, string> {
                                { "pheidiaction", "UpdateFieldValue" }, { "pheidiparams", p }
                            };

                            var response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30));
                            if (response != null)
                            {
                                if (response.StatusCode == HttpStatusCode.OK)
                                {
                                    var responseContent = response.Content.ReadAsStringAsync().Result;
                                    Debug.WriteLine("Reponse:" + responseContent);
                                }
                            }
                        }
                        else
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                UIAlertController actionSheetAlert = UIAlertController.Create(null, null, UIAlertControllerStyle.ActionSheet);
                                // Add Actions
                                var a1 = UIAlertAction.Create(AppResources.CameraFileChooser, UIAlertActionStyle.Default, (action) => ImageHelper.ImageImport(UIImagePickerControllerSourceType.Camera, request));
                                a1.SetValueForKey(new UIImage("camera.png"), new NSString("image"));
                                actionSheetAlert.AddAction(a1);

                                var a2 = UIAlertAction.Create(AppResources.GaleriePhotoFileChooser, UIAlertActionStyle.Default, (action) => ImageHelper.ImageImport(UIImagePickerControllerSourceType.PhotoLibrary, request));
                                a2.SetValueForKey(new UIImage("sort.png"), new NSString("image"));
                                actionSheetAlert.AddAction(a2);

                                actionSheetAlert.AddAction(UIAlertAction.Create(AppResources.AnnulerBouton, UIAlertActionStyle.Cancel, null));


                                UIViewController rootView = UIApplication.SharedApplication.KeyWindow.RootViewController;

                                //Setting du popoverPresentationController, obligatoire pour iPad
                                if (Device.Idiom == TargetIdiom.Tablet)
                                {
                                    actionSheetAlert.PopoverPresentationController.SourceView = rootView.View;

                                    var width  = rootView.View.Bounds.Width;
                                    var height = rootView.View.Bounds.Height;
                                    Debug.WriteLine("Width: " + width + ", Height: " + height);
                                    actionSheetAlert.PopoverPresentationController.SourceRect = new CGRect(width / 4, 3 * height / 4, width / 2, height / 4);

                                    actionSheetAlert.PopoverPresentationController.WillReposition += (sender, e) =>
                                    {
                                        //Changement d'orientation
                                        if (width > height != rootView.View.Bounds.Width > rootView.View.Bounds.Height)
                                        {
                                            actionSheetAlert.DismissViewController(false, null);
                                        }
                                    };
                                    actionSheetAlert.PopoverPresentationController.PermittedArrowDirections = 0;
                                }
                                rootView.PresentViewController(actionSheetAlert, true, null);
                            });
                        }
                    });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return(false);
        }