forked from ReVanced/revanced-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
431 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/src/main/java/app/revanced/manager/data/platform/FileSystem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package app.revanced.manager.data.platform | ||
|
||
import android.app.Application | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.Manifest | ||
import android.content.pm.PackageManager | ||
import androidx.activity.result.contract.ActivityResultContract | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import app.revanced.manager.util.RequestManageStorageContract | ||
|
||
class FileSystem(private val app: Application) { | ||
val contentResolver = app.contentResolver // TODO: move Content Resolver operations to here. | ||
|
||
fun externalFilesDir() = Environment.getExternalStorageDirectory().toPath() | ||
|
||
private fun usesManagePermission() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R | ||
|
||
private val storagePermissionName = if (usesManagePermission()) Manifest.permission.MANAGE_EXTERNAL_STORAGE else Manifest.permission.READ_EXTERNAL_STORAGE | ||
|
||
fun permissionContract(): Pair<ActivityResultContract<String, Boolean>, String> { | ||
val contract = if (usesManagePermission()) RequestManageStorageContract() else ActivityResultContracts.RequestPermission() | ||
return contract to storagePermissionName | ||
} | ||
|
||
fun hasStoragePermission() = if (usesManagePermission()) Environment.isExternalStorageManager() else app.checkSelfPermission(storagePermissionName) == PackageManager.PERMISSION_GRANTED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/main/java/app/revanced/manager/ui/component/patches/OptionFields.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package app.revanced.manager.ui.component.patches | ||
|
||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.FileOpen | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import app.revanced.manager.data.platform.FileSystem | ||
import app.revanced.manager.patcher.patch.Option | ||
import app.revanced.patcher.patch.PatchOption | ||
import org.koin.compose.rememberKoinInject | ||
|
||
/** | ||
* [Composable] functions do not support function references, so we have to use composable lambdas instead. | ||
*/ | ||
private typealias OptionField = @Composable (Any?, (Any?) -> Unit) -> Unit | ||
|
||
private val StringField: OptionField = { value, setValue -> | ||
val fs: FileSystem = rememberKoinInject() | ||
var showFileDialog by rememberSaveable { mutableStateOf(false) } | ||
val (contract, permissionName) = fs.permissionContract() | ||
val permissionLauncher = rememberLauncherForActivityResult(contract = contract) { | ||
showFileDialog = it | ||
} | ||
val current = value as? String | ||
|
||
if (showFileDialog) { | ||
PathSelectorDialog( | ||
root = fs.externalFilesDir() | ||
) { | ||
showFileDialog = false | ||
it?.let { path -> | ||
setValue(path.toString()) | ||
} | ||
} | ||
} | ||
|
||
Column { | ||
TextField(value = current ?: "", onValueChange = setValue) | ||
Button(onClick = { | ||
if (fs.hasStoragePermission()) { | ||
showFileDialog = true | ||
} else { | ||
permissionLauncher.launch(permissionName) | ||
} | ||
}) { | ||
Icon(Icons.Filled.FileOpen, null) | ||
Text("Select file or folder") | ||
} | ||
} | ||
} | ||
|
||
private val BooleanField: OptionField = { value, setValue -> | ||
val current = value as? Boolean | ||
Switch(checked = current ?: false, onCheckedChange = setValue) | ||
} | ||
|
||
private val UnknownField: OptionField = { _, _ -> | ||
Text("This type has not been implemented") | ||
} | ||
|
||
@Composable | ||
fun OptionField(option: Option, value: Any?, setValue: (Any?) -> Unit) { | ||
val implementation = remember(option.type) { | ||
when (option.type) { | ||
// These are the only two types that are currently used by the official patches. | ||
PatchOption.StringOption::class.java -> StringField | ||
PatchOption.BooleanOption::class.java -> BooleanField | ||
else -> UnknownField | ||
} | ||
} | ||
|
||
implementation(value, setValue) | ||
} |
110 changes: 110 additions & 0 deletions
110
app/src/main/java/app/revanced/manager/ui/component/patches/PathSelectorDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package app.revanced.manager.ui.component.patches | ||
|
||
import androidx.activity.compose.BackHandler | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.FileOpen | ||
import androidx.compose.material.icons.filled.Folder | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.compose.ui.window.DialogProperties | ||
import app.revanced.manager.R | ||
import app.revanced.manager.ui.component.AppTopBar | ||
import app.revanced.manager.util.PathSaver | ||
import java.nio.file.Path | ||
import kotlin.io.path.isDirectory | ||
import kotlin.io.path.isRegularFile | ||
import kotlin.io.path.listDirectoryEntries | ||
import kotlin.io.path.name | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun PathSelectorDialog(root: Path, onSelect: (Path?) -> Unit) { | ||
var currentDirectory by rememberSaveable(root, stateSaver = PathSaver) { mutableStateOf(root) } | ||
val notAtRootDir = remember(currentDirectory) { | ||
currentDirectory != root | ||
} | ||
val everything = remember(currentDirectory) { | ||
currentDirectory.listDirectoryEntries() | ||
} | ||
val directories = remember(everything) { | ||
everything.filter { it.isDirectory() } | ||
} | ||
val files = remember(everything) { | ||
everything.filter { it.isRegularFile() } | ||
} | ||
|
||
Dialog( | ||
onDismissRequest = { onSelect(null) }, | ||
properties = DialogProperties( | ||
usePlatformDefaultWidth = false, | ||
dismissOnBackPress = true | ||
) | ||
) { | ||
Scaffold( | ||
topBar = { | ||
AppTopBar( | ||
title = stringResource(R.string.select_file), | ||
onBackClick = { onSelect(null) } | ||
) | ||
} | ||
) { paddingValues -> | ||
BackHandler(enabled = notAtRootDir) { | ||
currentDirectory = currentDirectory.parent | ||
} | ||
|
||
Column( | ||
modifier = Modifier | ||
.padding(paddingValues) | ||
.verticalScroll(rememberScrollState()) | ||
) { | ||
Text(text = currentDirectory.toString()) | ||
Row( | ||
modifier = Modifier.clickable { onSelect(currentDirectory) } | ||
) { | ||
Text("(Use this directory)") | ||
} | ||
if (notAtRootDir) { | ||
Row( | ||
modifier = Modifier.clickable { currentDirectory = currentDirectory.parent } | ||
) { | ||
Text("Previous directory") | ||
} | ||
} | ||
|
||
directories.forEach { | ||
Row( | ||
modifier = Modifier.clickable { currentDirectory = it } | ||
) { | ||
Icon(Icons.Filled.Folder, null) | ||
Text(text = it.name) | ||
} | ||
} | ||
files.forEach { | ||
Row( | ||
modifier = Modifier.clickable { onSelect(it) } | ||
) { | ||
Icon(Icons.Filled.FileOpen, null) | ||
Text(text = it.name) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.