magnusja / libaums

Open source library to access USB Mass Storage devices on Android without rooting your device
Apache License 2.0
1.27k stars 272 forks source link

Control FileSystemCreator evaluation order #332

Closed clifforous closed 2 years ago

clifforous commented 2 years ago

Problem

I am registering a FileSystemCreator via an initializer block in the Main Activity companion object.

class MainActivity : AppCompatActivity() {
    companion object {
        ...
        init {
           ...
            FileSystemFactory.registerFileSystem(UsbFatFileSystemCreator())
        }
   }
}

The FileSystemFactory object initializer block registers the default Fat32FileSystemCreator before that FileSystemCreator. The createFileSystem function then iterates in the order the creators were registered so that Fat32FileSystemCreator is always evaluated first.

I would like my FileSystemCreator to be evaluated before the default creator so that I consistently use the custom creator for FAT12, FAT16, and FAT32.

Proposed Solution

Add a mechanism of assigning an order. This could just be adding an additional function signature such as:

fun registerFileSystem(creator: FileSystemCreator, priority: Int)

A constant value for the priority default file system creator could be exposed so that registered creators can be ordered before or after the default. The existing function can continue to behave the same way so as to make the change non-breaking.

Alternatively, could just remove the initializer block from FileSystemFactory and lazily add the Fat32FileSystemCreator at the time of the call to createFileSystem if there are no registered creators.

Code where problem occurs

https://github.com/magnusja/libaums/blob/develop/libaums/src/main/java/me/jahnen/libaums/core/fs/FileSystemFactory.kt#L48

magnusja commented 2 years ago

Oh yeah that makes total sense, feel free to open a MR :)

I guess we can have DEFAULT = 0 and then have the

registerFileSystem(creator: FileSystemCreator) {
  registerFileSystem(creator, 1)
}

Then this would not happen by accident anymore