private static void Upload(SmartPrintScreen service, string screenshotFile)
        {
            var permissionCheck = ContextCompat.CheckSelfPermission(service, Android.Manifest.Permission.ReadExternalStorage);

            if (permissionCheck != Android.Content.PM.Permission.Granted)
            {
                SmartPrintScreen.MakeToast(service, Strings.RequestRead);
                return;
            }
            using (var opt = new BitmapFactory.Options()) {
                opt.InDither          = true;
                opt.InPreferredConfig = Bitmap.Config.Argb8888;
                var bitmap = BitmapFactory.DecodeFile(screenshotFile, opt);
                if (bitmap != null)
                {
                    bool disposed = false;
                    Task.Run(async() => {
                        string url = await SmartPrintScreen.UploadToImgurTask(bitmap);
                        bitmap.Dispose();
                        disposed = true;
                        SmartPrintScreen.PostUpload(service, url, screenshotFile);
                    }).ContinueWith((t) => {
                        if (!disposed)
                        {
                            bitmap.Dispose();
                        }
                        service.handler.Post(() => {
                            ExceptionHandler.SaveManagedException(Java.Lang.Throwable.FromException(t.Exception), Java.Lang.Thread.CurrentThread(), null);
                        });
                    }, TaskContinuationOptions.OnlyOnFaulted);
                }
            }
        }
示例#2
0
 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
 {
     switch (requestCode)
     {
     case 1337:
         if (grantResults[0] != Permission.Granted || grantResults[1] != Permission.Granted)
         {
             SmartPrintScreen.MakeToast(this, Strings.RequestRead);
         }
         break;
     }
 }
        private static void PostUpload(SmartPrintScreen service, string url, string screenshotFile)
        {
            Log.Debug(Tag, "SmartPrintScreen: PostUpload " + url);
            if (url != null)
            {
                Log.Debug(Tag, "SmartPrintScreen: PostUpload prepared showing toast");
//				Device.BeginInvokeOnMainThread(() => {
                service.handler.Post(() => {
                    Log.Debug(Tag, "SmartPrintScreen: PostUpload got from the clipboard");
                    Plugin.Clipboard.CrossClipboard.Current.SetText(url);
                    SmartPrintScreen.MakeToast(service, Strings.CopiedToClipboardToast + ":\n" + url);
                    MessagingCenter.Send(url, MainPage.AddedURL);
                });
//				});
                //we reached here, so it got uploaded fine
                if (Settings.RemoveShots)
                {
                    Log.Debug(Tag, "SmartPrintScreen: PostUpload removing the screenshot");
                    Java.IO.File f = new Java.IO.File(screenshotFile);
                    Log.Debug(Tag, "SmartPrintScreen: PostUpload removing the screenshot from " + f.ToString());
                    if (f.Exists() && !f.IsDirectory)
                    {
                        if (
                            f.Delete()
                            )
                        {
                            Log.Debug(Tag, "SmartPrintScreen: PostUpload removed the screenshot");
                        }
                        else
                        {
                            Log.Debug(Tag, "SmartPrintScreen: PostUpload failed to removed the screenshot");
                        }
                    }
                }
                else
                {
                    Log.Debug(Tag, "SmartPrintScreen: PostUpload not removing the screenshot");
                }
                Log.Debug(Tag, "SmartPrintScreen: PostUpload updating the screenshots list");
                Settings.ScreenshotsList += url + "\n";
                Log.Debug(Tag, "SmartPrintScreen: PostUpload notifying the UI");
                //if wi-fi is enabled then we actually failed
            }
            else
            {
                service.handler.Post(() => {
                    SmartPrintScreen.MakeToast(service, Strings.AppName + ": " + Strings.ErrorCheckConnection);
                });
            }
            Log.Debug(Tag, "SmartPrintScreen: PostUpload finished");
        }
 public override void OnEvent(FileObserverEvents ev, string path)
 {
     Log.Debug(Tag, "UploadFileObserver: OnEvent " + ev + " " + path);
     if ((ev & FileObserverEvents.CloseWrite) != 0)
     {
         string screenshotFile = this.ssFolder + sep + path;
         Log.Debug(Tag, "UploadFileObserver: OnEvent " + ev + " at " + screenshotFile);
         if (!(new Java.IO.File(screenshotFile)).Exists())
         {
             return;
         }
         Log.Debug(Tag, "UploadFileObserver: OnEvent " + ev + " " + path + " started bitmapping");
         SmartPrintScreen.Upload(this.service, screenshotFile);
     }
 }
 public override void OnChange(bool selfChange, Android.Net.Uri uri)
 {
     lock (locker) {
         base.OnChange(selfChange, uri);
         if (uri.ToString().Contains(MediaStore.Images.Media.ExternalContentUri.ToString()))
         {
             ICursor cursor = null;
             try {
                 cursor = contentResolver.Query(uri, new string[] {
                     MediaStore.Images.Media.InterfaceConsts.DisplayName,
                     MediaStore.Images.Media.InterfaceConsts.Data,
                     MediaStore.Images.Media.InterfaceConsts.DateAdded,
                 }, null, null, null);
                 if (cursor != null && cursor.MoveToFirst())
                 {
                     long now = Java.Lang.JavaSystem.CurrentTimeMillis() / 1000;
                     do
                     {
                         string fileName  = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.DisplayName));
                         string path      = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
                         long   dateAdded = cursor.GetLong(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.DateAdded));
                         if ((now - dateAdded) < 3 &&
                             this.lastUploadingFileName != fileName &&
                             fileName.ToLowerInvariant().Contains("screenshot"))
                         {
                             this.lastUploadingFileName = fileName;
                             SmartPrintScreen.Upload(this.service, path);
                             break;
                         }
                     } while (cursor.MoveToNext());
                 }
             } finally {
                 if (cursor != null)
                 {
                     cursor.Close();
                     cursor.Dispose();
                 }
             }
         }
     }
 }
 public UploadContentObserver(Handler ha, SmartPrintScreen service, ContentResolver contentResolver) : base(ha)
 {
     this.service         = service;
     this.contentResolver = contentResolver;
 }
 public UploadFileObserver(string path, SmartPrintScreen service) : base(path)
 {
     this.ssFolder = path;
     this.service  = service;
 }