Example #1
0
        public FolderData GetLocalAppFolder()
        {
            var folder = new FolderData()
            {
                FolderPath = Android.App.Application.Context.FilesDir.AbsolutePath,
                FolderName = Path.GetDirectoryName(Android.App.Application.Context.FilesDir.AbsolutePath)
            };

            return(folder);
        }
Example #2
0
        public FolderData GetLocalAppFolder()
        {
            var folder = new FolderData()
            {
                FolderName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),
                FolderPath = Path.GetDirectoryName(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData))
            };

            return(folder);
        }
        public FolderData GetLocalAppFolder()
        {
            var folder = new FolderData()
            {
                FolderPath = ApplicationData.Current.LocalFolder.Path + Path.DirectorySeparatorChar,
                FolderName = Path.GetDirectoryName(ApplicationData.Current.LocalFolder.Path)
            };

            return(folder);
        }
        public FolderData GetLocalAppFolder()
        {
            var libraryPath = GetPath(NSSearchPathDirectory.LibraryDirectory);
            var localPath   = libraryPath;

            var folder = new FolderData()
            {
                FolderPath = localPath,
                FolderName = Path.GetDirectoryName(localPath)
            };

            return(folder);
        }
        public async static Task <bool> SaveFileInFolder(FileData newFile, FolderData folder, bool shouldOverWrite)
        {
            try
            {
                var currPickedFile = await XFileManager.Current.SaveFileInFolder(newFile, folder, shouldOverWrite).ConfigureAwait(true);

                return(currPickedFile);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(false);
            }
        }
        public Task <FolderData> PickFolder()
        {
            var id = this.GetRequestId();

            var ntcs = new TaskCompletionSource <FolderData>(id);

            if (Interlocked.CompareExchange(ref this.tcs_folderData, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }

            var allowedUtis = new string[]
            {
                //UTType.Content,
                //UTType.Item,
                "public.folder"
            };

            // NOTE: Importing (UIDocumentPickerMode.Import) makes a local copy of the document,
            // while opening (UIDocumentPickerMode.Open) opens the document directly. We do the
            // second one here.
            var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Open);

            documentPicker.DidPickDocument       += this.DocumentPicker_DidPickDocument;
            documentPicker.WasCancelled          += this.DocumentPicker_WasCancelled;
            documentPicker.DidPickDocumentAtUrls += this.DocumentPicker_DidPickDocumentAtUrls;

            UIViewController viewController = GetActiveViewController();

            viewController.PresentViewController(documentPicker, true, null);

            this.Handler = (sender, args) =>
            {
                var tcs = Interlocked.Exchange(ref this.tcs_folderData, null);
                //re-using file picker, the path is here
                var folder = new FolderData()
                {
                    FolderPath = args.FilePath,
                    FolderName = args.FileName
                };
                tcs?.SetResult(folder);
            };

            return(this.tcs_folderData.Task);
        }
Example #7
0
        public Task <FolderData> PickFolder()
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyDocuments;
            var result = folderBrowserDialog.ShowDialog();



            if (result == DialogResult.Cancel)
            {
                return(Task.FromResult <FolderData>(null));
            }

            var folder = new FolderData()
            {
                FolderPath = folderBrowserDialog.SelectedPath,
                FolderName = Path.GetDirectoryName(folderBrowserDialog.SelectedPath)
            };

            return(Task.FromResult(folder));
        }
 public Task <bool> SaveFileInFolder(FileData fileToSave, FolderData folder, bool shouldOverWrite)
 {
     throw new NotImplementedException();
 }
Example #9
0
        public Task <bool> SaveFileInFolder(FileData fileToSave, FolderData folder, bool shouldOverWrite)
        {
            var uniqueId = Guid.NewGuid();
            var next     = new TaskCompletionSource <bool>(uniqueId);

            // Interlocked.CompareExchange(ref object location1, object value, object comparand)
            // Compare location1 with comparand.
            // If equal replace location1 by value.
            // Returns the original value of location1.
            // ---
            // In this context, tcs is compared to null, if equal tcs is replaced by next,
            // and original tcs is returned.
            // We then compare original tcs with null, if not null it means that a task was
            // already started.
            if (Interlocked.CompareExchange(ref tcs_bool_as_int, next, null) != null)
            {
                return(Task.FromResult <bool>(false));
            }

            EventHandler <PermissionRequestEventArgs> handler = null;

            weakContext.TryGetTarget(out Context newContext);

            var requestPermissionIntent = new Intent(newContext, typeof(RequestPermissionActivity));

            requestPermissionIntent.SetFlags(ActivityFlags.NewTask);
            requestPermissionIntent.PutExtra(RequestPermissionActivity.RequestedPermission, Manifest.Permission.WriteExternalStorage);



            handler = (sender, e) =>
            {
                // Interlocaked.Exchange(ref object location1, object value)
                // Sets an object to a specified value and returns a reference to the original object.
                // ---
                // In this context, sets tcs to null and returns it.
                var task = Interlocked.Exchange(ref tcs_bool_as_int, null);

                RequestPermissionActivity.OnPermissionGranted -= handler;

                if (e.success)
                {
                    try
                    {
                        var test = Android.Net.Uri.Parse(folder.FolderPath);// + "/test.pdf");



                        var documentFile = DocumentFile.FromTreeUri(newContext, test);

                        DocumentFile newFile = documentFile.FindFile(fileToSave.FileName);



                        if (newFile != null)
                        {
                            if (shouldOverWrite)
                            {
                                newFile.Delete();
                            }
                            else
                            {
                                //supposed to create uniuqe name in this case
                                var purefilename  = Path.GetFileNameWithoutExtension(fileToSave.FileName);
                                var fileextention = Path.GetExtension(fileToSave.FileName);
                                fileToSave.FileName = purefilename + "_" + Path.GetRandomFileName() + fileextention;
                            }
                        }

                        documentFile.CreateFile("*/*", fileToSave.FileName);
                        //var documentFile = DocumentFile.FromTreeUri(activity, test);


                        var outputstream = newContext.ContentResolver.OpenOutputStream(newFile.Uri);

                        fileToSave.GetStream().CopyTo(outputstream);

                        outputstream.Flush();
                        outputstream.Close();

                        task.SetResult(e.success);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        task.SetCanceled();
                    }
                }
            };
            RequestPermissionActivity.OnPermissionGranted += handler;

            //proably need a try statement here
            newContext.StartActivity(requestPermissionIntent);



            return(tcs_bool_as_int.Task);
        }
Example #10
0
        public Task <FolderData> PickFolderAsync(string action)
        {
            {
                var id = this.GetRequestId();

                var next = new TaskCompletionSource <FolderData>(id);

                // Interlocked.CompareExchange(ref object location1, object value, object comparand)
                // Compare location1 with comparand.
                // If equal replace location1 by value.
                // Returns the original value of location1.
                // ---
                // In this context, tcs is compared to null, if equal tcs is replaced by next,
                // and original tcs is returned.
                // We then compare original tcs with null, if not null it means that a task was
                // already started.
                if (Interlocked.CompareExchange(ref tcs_folderData, next, null) != null)
                {
                    return(Task.FromResult <FolderData>(null));
                }
                try
                {
                    var pickIntent = new Intent(this.context, typeof(FolderPickActivity));
                    //weakContext.TryGetTarget(out Context newContext);
                    //var pickIntent = new Intent(newContext, typeof(FolderPickActivity));
                    pickIntent.SetFlags(ActivityFlags.NewTask);
                    //newContext.StartActivity(pickIntent);
                    this.context.StartActivity(pickIntent);


                    EventHandler <FolderPickerEventArgs>          handler          = null;
                    EventHandler <FolderPickerCancelledEventArgs> cancelledHandler = null;

                    handler = (sender, e) =>
                    {
                        // Interlocaked.Exchange(ref object location1, object value)
                        // Sets an object to a specified value and returns a reference to the original object.
                        // ---
                        // In this context, sets tcs to null and returns it.
                        var task = Interlocked.Exchange(ref tcs_folderData, null);

                        FolderPickActivity.FolderPickCancelled -= cancelledHandler;
                        FolderPickActivity.FolderPicked        -= handler;

                        var folder = new FolderData()
                        {
                            FolderPath = e.FolderPath,
                            FolderName = e.FolderName
                        };
                        task?.SetResult(folder);
                        //if (e.FolderPath != null)
                        //{
                        //    task.SetResult(e.FolderPath);

                        //}
                        //else
                        //{
                        //    task.SetCanceled();
                        //}
                    };

                    cancelledHandler = (s, e) =>
                    {
                        var tcs = Interlocked.Exchange(ref this.completionSource, null);

                        FolderPickActivity.FolderPickCancelled -= cancelledHandler;
                        FolderPickActivity.FolderPicked        -= handler;

                        if (e?.Exception != null)
                        {
                            tcs?.SetException(e.Exception);
                        }
                        else
                        {
                            tcs?.SetResult(null);
                        }
                    };

                    FolderPickActivity.FolderPickCancelled += cancelledHandler;
                    FolderPickActivity.FolderPicked        += handler;
                }
                catch (Exception ex)
                {
                    Debug.Write(ex);
                    this.completionSource.SetException(ex);
                }

                return(tcs_folderData.Task);
            }
        }
Example #11
0
 public Task <bool> SaveFileInFolder(FileData fileToSave, FolderData folder, bool shouldOverWrite)
 {
     return(SaveFile(fileToSave, folder, shouldOverWrite));
 }