The goal of project is to create a Flutter application that provides access to public folders of Android devices. For example, this application will allow you to get a list of files in the common Documents or Downloads folders.
Basically there are two main problems that need to be solved:
- Allow the application to access shared folders.
- Define absolute paths to shared folders
- To solve the first problem, need to allow the application access to manage external storage: android.permission.MANAGE_EXTERNAL_STORAGE and possibility for reading/writing files: android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE. These permissions must be described in the application manifest AndroidManifest.xml. Next, need to use the standard Flutter plugin permission_handler https://pub.dev/packages/permission_handler from pub.dev and from the application request permission to use android.permission.MANAGE_EXTERNAL_STORAGE, and select permission. Request is implemented in the AsyncPermissionRequestFuture class in the process method. Can first find out whether access is allowed or not by using the AsyncPermissionStatusFuture class. This will save operations. Pay attention on method process.
- To determine absolute paths to shared folders, a plugin was written in Java, encapsulated in the MainActivity class and used in the method process in AsyncExternalPathFuture class.
- The AsyncPathExistFuture class is designed to check whether the requested folder path exists in shared memory, and the AsyncFilesFuture class allows you to get a list of files of the given folder, including subfolders.
As a matter of fact, the five mentioned classes are responsible for the technical solution. All that’s left to do is to glue these methods into a sequence of operations and ensure that the final result is obtained at the one click of a button.
I was able to chain the above operations into a sequence of calls to five asynchronous futures built into the state machine below. The latter is associated with a specially created class called AccessBloc. In this decision I used trans(ition)- functions, which are called when moving from one state to another in state machines under the influence of event and run callback functions passed as parameter to event. Can also notice that uses AsyncProcess objects that run callbacks don't use await operators. I.e. absolutely all operations are asynchronous.
The movie below illustrates this approach. Access to publc Downloads and Documents folders has been implemented.