files: open files in relevant apps
- Commit
714820b0996ad537e53ba40e7ef5bf3f33e1be6a- Author
- Vayun <[email protected]>
- When
- 2026-01-19 18:24 UTC (10 months ago)
- Parents
46a52a0
Diff
files/src/main/AndroidManifest.xml
diff --git a/files/src/main/AndroidManifest.xml b/files/src/main/AndroidManifest.xmlindex 89854654a4..013080a950 100644--- a/files/src/main/AndroidManifest.xml+++ b/files/src/main/AndroidManifest.xml@@ -1,12 +1,25 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> + <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />+ <application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.General">++ <provider+ android:name="androidx.core.content.FileProvider"+ android:authorities="${applicationId}.fileprovider"+ android:exported="false"+ android:grantUriPermissions="true">+ <meta-data+ android:name="android.support.FILE_PROVIDER_PATHS"+ android:resource="@xml/file_paths" />+ </provider>+ <activity android:name=".MainActivity" android:exported="true"
files/src/main/java/com/vayunmathur/files/MainActivity.kt
diff --git a/files/src/main/java/com/vayunmathur/files/MainActivity.kt b/files/src/main/java/com/vayunmathur/files/MainActivity.ktindex 2629a45e40..4f9ac12ea1 100644--- a/files/src/main/java/com/vayunmathur/files/MainActivity.kt+++ b/files/src/main/java/com/vayunmathur/files/MainActivity.kt@@ -1,10 +1,78 @@ package com.vayunmathur.files +import android.content.Intent+import android.net.Uri import android.os.Bundle+import android.os.Environment+import android.provider.Settings import androidx.activity.ComponentActivity+import androidx.activity.compose.BackHandler+import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge+import androidx.activity.result.contract.ActivityResultContracts+import androidx.compose.foundation.ExperimentalFoundationApi+import androidx.compose.foundation.background+import androidx.compose.foundation.clickable+import androidx.compose.foundation.combinedClickable+import androidx.compose.foundation.interaction.MutableInteractionSource+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.imePadding+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.width+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.items+import androidx.compose.foundation.text.BasicTextField+import androidx.compose.foundation.text.KeyboardActions+import androidx.compose.foundation.text.KeyboardOptions+import androidx.compose.material3.Button+import androidx.compose.material3.DropdownMenu+import androidx.compose.material3.DropdownMenuItem+import androidx.compose.material3.ExperimentalMaterial3Api+import androidx.compose.material3.Icon+import androidx.compose.material3.IconButton+import androidx.compose.material3.ListItem+import androidx.compose.material3.ListItemDefaults+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.OutlinedTextField+import androidx.compose.material3.Scaffold+import androidx.compose.material3.Text+import androidx.compose.material3.TextField+import androidx.compose.material3.TopAppBar+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.derivedStateOf+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.focus.FocusRequester+import androidx.compose.ui.focus.focusRequester+import androidx.compose.ui.focus.onFocusChanged+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.platform.LocalFocusManager+import androidx.compose.ui.platform.LocalSoftwareKeyboardController+import androidx.compose.ui.res.painterResource+import androidx.compose.ui.text.TextRange+import androidx.compose.ui.text.input.ImeAction+import androidx.compose.ui.text.input.TextFieldValue+import androidx.compose.ui.unit.dp+import androidx.core.content.FileProvider import com.vayunmathur.library.ui.DynamicTheme+import com.vayunmathur.library.ui.IconAdd+import com.vayunmathur.library.ui.IconDelete+import com.vayunmathur.library.ui.IconEdit+import kotlinx.coroutines.delay+import java.io.File+import kotlin.math.roundToLong+import kotlin.math.sin+import kotlin.time.Instant class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) {@@ -12,7 +80,207 @@ class MainActivity : ComponentActivity() { enableEdgeToEdge() setContent { DynamicTheme {+ HomeDirectoryPage() } } }-}\ No newline at end of file+}++@Composable+fun HomeDirectoryPage() {+ val context = LocalContext.current+ var isGranted by remember { mutableStateOf(Environment.isExternalStorageManager()) }+ val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {+ isGranted = Environment.isExternalStorageManager()+ }++ if (!isGranted) {+ Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {+ Button(onClick = {+ val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION).apply {+ data = Uri.fromParts("package", context.packageName, null)+ }+ launcher.launch(intent)+ }) {+ Text("Grant All Files Access")+ }+ }+ } else {+ DirectoryPage(Environment.getExternalStorageDirectory())+ }+}++@OptIn(ExperimentalMaterial3Api::class)+@Composable+fun DirectoryPage(rootFile: File) {+ val context = LocalContext.current+ var currentDirectory by remember { mutableStateOf(rootFile) }+ var selectedPaths by remember(currentDirectory) { mutableStateOf(setOf<String>()) }+ var pathBeingRenamed by remember { mutableStateOf<String?>(null) }++ // Data state+ var filesList by remember(currentDirectory) {+ mutableStateOf(currentDirectory.listFiles()?.partition { it.isDirectory }+ ?: Pair(emptyList<File>(), emptyList<File>()))+ }++ fun forceRefresh() {+ filesList = currentDirectory.listFiles()?.partition { it.isDirectory }+ ?: Pair(emptyList<File>(), emptyList<File>())+ }+ val (directories, files) = filesList++ val focusManager = LocalFocusManager.current++ BackHandler(currentDirectory.absolutePath != Environment.getExternalStorageDirectory().absolutePath) {+ if (selectedPaths.isNotEmpty()) {+ selectedPaths = emptySet()+ } else {+ currentDirectory = currentDirectory.parentFile ?: currentDirectory+ }+ }++ Scaffold(+ modifier = Modifier+ .imePadding()+ .clickable(interactionSource = remember { MutableInteractionSource() }, indication = null) {+ focusManager.clearFocus()+ pathBeingRenamed = null+ },+ topBar = {+ TopAppBar(+ title = { Text(currentDirectory.name.ifEmpty { "Internal Storage" }) },+ actions = {+ // Show Rename Button if exactly 1 is selected+ if (selectedPaths.size == 1) {+ IconButton(onClick = { pathBeingRenamed = selectedPaths.first() }) {+ IconEdit()+ }+ }+ // Delete Button+ if (selectedPaths.isNotEmpty()) {+ IconButton(onClick = {+ selectedPaths.forEach { File(it).deleteRecursively() }+ selectedPaths = emptySet()+ forceRefresh()+ }) {+ IconDelete()+ }+ }+ }+ )+ }+ ) { padding ->+ LazyColumn(Modifier.padding(padding)) {+ val allItems = directories.sortedBy { it.name.lowercase() } + files.sortedBy { it.name.lowercase() }++ items(allItems, key = { it.absolutePath }) { child ->+ val isSelected = selectedPaths.contains(child.absolutePath)+ val isEditing = pathBeingRenamed == child.absolutePath++ DirectoryItem(+ file = child,+ isEditing = isEditing,+ isSelected = isSelected,+ onRename = { newName ->+ val newFile = File(child.parentFile, newName)+ if (child.renameTo(newFile)) {+ forceRefresh()+ }+ pathBeingRenamed = null+ selectedPaths = emptySet()+ },+ onToggleSelection = {+ if(pathBeingRenamed != null) pathBeingRenamed = null+ selectedPaths = if (isSelected) selectedPaths - child.absolutePath+ else selectedPaths + child.absolutePath+ },+ onClick = {+ if (selectedPaths.isNotEmpty()) {+ selectedPaths = if (isSelected) selectedPaths - child.absolutePath+ else selectedPaths + child.absolutePath+ } else if (child.isDirectory) {+ currentDirectory = child+ } else {+ val intent = Intent(Intent.ACTION_VIEW).apply {+ data = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", child)+ flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION+ }+ context.startActivity(intent)+ }+ }+ )+ }+ }+ }+}++@OptIn(ExperimentalFoundationApi::class)+@Composable+fun DirectoryItem(+ file: File,+ isEditing: Boolean,+ isSelected: Boolean,+ onRename: (String) -> Unit,+ onToggleSelection: () -> Unit,+ onClick: () -> Unit+) {+ var editedName by remember(isEditing) { mutableStateOf(file.name) }+ val focusRequester = remember { FocusRequester() }+ val keyboard = LocalSoftwareKeyboardController.current++ ListItem(+ modifier = Modifier+ .combinedClickable(+ onClick = onClick,+ onLongClick = onToggleSelection+ ),+ headlineContent = {+ if (isEditing) {+ TextField(+ value = editedName,+ onValueChange = { editedName = it },+ modifier = Modifier.focusRequester(focusRequester).fillMaxWidth(),+ singleLine = true,+ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),+ keyboardActions = KeyboardActions(onDone = { onRename(editedName) })+ )+ LaunchedEffect(Unit) {+ focusRequester.requestFocus()+ keyboard?.show()+ }+ } else {+ Text(file.name)+ }+ },+ leadingContent = {+ Icon(+ if (file.isDirectory) painterResource(R.drawable.folder_24px) else painterResource(R.drawable.docs_24px),+ contentDescription = null,+ tint = if (isSelected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.outline+ )+ },+ supportingContent = {+ if (!file.isDirectory) {+ Text("${byteSizeString(file.length())}")+ }+ },+ colors = ListItemDefaults.colors(+ containerColor = if (isSelected) MaterialTheme.colorScheme.surfaceVariant else Color.Transparent+ )+ )+}++fun byteSizeString(bytes: Long): String {+ val units = arrayOf("B", "KB", "MB", "GB", "TB")+ var unitIdx = 0;+ var bytesS = bytes.toDouble();+ while(bytesS >= 1024) {+ bytesS /= 1024+ unitIdx+++ }+ bytesS = (bytesS*100).roundToLong()/100.0+ return "$bytesS ${units[unitIdx]}"+}++fun Boolean.int(): Int = if(this) 1 else 0\ No newline at end of file
files/src/main/res/drawable/docs_24px.xml
diff --git a/files/src/main/res/drawable/docs_24px.xml b/files/src/main/res/drawable/docs_24px.xmlnew file mode 100644index 0000000000..086537c656--- /dev/null+++ b/files/src/main/res/drawable/docs_24px.xml@@ -0,0 +1,5 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">+ + <path android:fillColor="@android:color/white" android:pathData="M320,520L640,520L640,440L320,440L320,520ZM320,640L640,640L640,560L320,560L320,640ZM320,760L520,760L520,680L320,680L320,760ZM240,880Q207,880 183.5,856.5Q160,833 160,800L160,160Q160,127 183.5,103.5Q207,80 240,80L560,80L800,320L800,800Q800,833 776.5,856.5Q753,880 720,880L240,880ZM520,360L520,160L240,160Q240,160 240,160Q240,160 240,160L240,800Q240,800 240,800Q240,800 240,800L720,800Q720,800 720,800Q720,800 720,800L720,360L520,360ZM240,160L240,160L240,360L240,360L240,160L240,360L240,360L240,800Q240,800 240,800Q240,800 240,800L240,800Q240,800 240,800Q240,800 240,800L240,160Q240,160 240,160Q240,160 240,160Z"/>+ +</vector>
files/src/main/res/drawable/folder_24px.xml
diff --git a/files/src/main/res/drawable/folder_24px.xml b/files/src/main/res/drawable/folder_24px.xmlnew file mode 100644index 0000000000..7fdb9428f0--- /dev/null+++ b/files/src/main/res/drawable/folder_24px.xml@@ -0,0 +1,5 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">+ + <path android:fillColor="@android:color/white" android:pathData="M160,800Q127,800 103.5,776.5Q80,753 80,720L80,240Q80,207 103.5,183.5Q127,160 160,160L400,160L480,240L800,240Q833,240 856.5,263.5Q880,287 880,320L880,720Q880,753 856.5,776.5Q833,800 800,800L160,800ZM160,720L800,720Q800,720 800,720Q800,720 800,720L800,320Q800,320 800,320Q800,320 800,320L447,320L367,240L160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720ZM160,720Q160,720 160,720Q160,720 160,720L160,240Q160,240 160,240Q160,240 160,240L160,240L160,320L160,320Q160,320 160,320Q160,320 160,320L160,720Q160,720 160,720Q160,720 160,720Z"/>+ +</vector>
files/src/main/res/xml/file_paths.xml
diff --git a/files/src/main/res/xml/file_paths.xml b/files/src/main/res/xml/file_paths.xmlnew file mode 100644index 0000000000..8248868e52--- /dev/null+++ b/files/src/main/res/xml/file_paths.xml@@ -0,0 +1,4 @@+<?xml version="1.0" encoding="utf-8"?>+<paths>+ <external-path name="external_files" path="." />+</paths>\ No newline at end of file