Skip to content

Commit d960de1

Browse files
committed
Extract function to top level
1 parent 5249308 commit d960de1

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

app/src/main/java/dev/zwander/installwithoptions/util/IncomingPackageUtils.kt

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,19 @@ import dev.zwander.installwithoptions.data.DataModel
1111
import net.lingala.zip4j.ZipFile
1212
import net.lingala.zip4j.exception.ZipException
1313
import java.io.File
14-
import java.io.FileNotFoundException
1514

1615
fun Context.handleIncomingUris(uris: List<Uri>) {
1716
DataModel.isImporting.value = true
1817
val currentSelection = DataModel.selectedFiles.value.toMutableMap()
1918

20-
fun addApkFile(file: DocumentFile) {
21-
try {
22-
val realFile = if (file.uri.scheme == "file") file else run {
23-
contentResolver.openInputStream(file.uri).use { input ->
24-
val dest = File(cacheDir, "${file.name ?: file.uri}")
25-
dest.outputStream().use { output ->
26-
input.copyTo(output)
27-
}
28-
DocumentFile.fromFile(dest)
29-
}
30-
}
31-
val apkFile = PackageParser.parseApkLite(File(realFile.uri.path), 0)
32-
val packageList = currentSelection[apkFile.packageName] ?: listOf()
33-
34-
currentSelection[apkFile.packageName] = (packageList + file).distinctBy { "${apkFile.packageName}:${it.name}" }
35-
} catch (_: PackageParser.PackageParserException) {
36-
} catch (_: IllegalStateException) {
37-
} catch (_: FileNotFoundException) {
38-
}
39-
}
40-
4119
uris.forEach { uri ->
4220
val file = DocumentFile.fromSingleUri(this, uri) ?: return@forEach
4321

4422
if (file.isApk) {
45-
addApkFile(file)
23+
addApkFile(file, currentSelection)
4624
} else if (file.isSplitBundle) {
4725
copyZipToCacheAndExtract(file).forEach { innerFile ->
48-
addApkFile(innerFile)
26+
addApkFile(innerFile, currentSelection)
4927
}
5028
}
5129
}
@@ -54,6 +32,24 @@ fun Context.handleIncomingUris(uris: List<Uri>) {
5432
DataModel.isImporting.value = false
5533
}
5634

35+
private fun Context.addApkFile(file: DocumentFile, currentSelection: MutableMap<String, List<DocumentFile>>) {
36+
try {
37+
val realFile = if (file.uri.scheme == "file") file else run {
38+
contentResolver.openInputStream(file.uri).use { input ->
39+
val dest = File(cacheDir, "${file.name ?: file.uri}")
40+
dest.outputStream().use { output ->
41+
input.copyTo(output)
42+
}
43+
DocumentFile.fromFile(dest)
44+
}
45+
}
46+
val apkFile = PackageParser.parseApkLite(File(realFile.uri.path), 0)
47+
val packageList = currentSelection[apkFile.packageName] ?: listOf()
48+
49+
currentSelection[apkFile.packageName] = (packageList + file).distinctBy { "${apkFile.packageName}:${it.name}" }
50+
} catch (_: Throwable) {}
51+
}
52+
5753
private fun Context.copyZipToCacheAndExtract(zip: DocumentFile): List<DocumentFile> {
5854
val destFile = File(cacheDir, zip.name ?: zip.uri.toString())
5955
val destDir = File(cacheDir, "${destFile.name}_extracted").apply {

0 commit comments

Comments
 (0)