Functionality Done

This commit is contained in:
zaki
2023-05-26 17:19:27 +05:30
parent e191594ae4
commit 899bed5d0c
7 changed files with 192 additions and 23 deletions
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
-1
View File
@@ -23,7 +23,6 @@ kotlin {
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation("com.lordcodes.turtle:turtle:0.5.0")
}
}
val jvmTest by getting
+8
View File
@@ -0,0 +1,8 @@
object Constant {
const val SUCCESS = 0
const val FAILURE = 1
fun getCommand(): String {
return "bundletool build-apks --mode=universal --bundle=INPUT_FILE_PATH --output=OUTPUT_FILE_NAME.apks"
}
}
+27
View File
@@ -0,0 +1,27 @@
import java.io.File
import java.io.IOException
object FileHelper {
fun performFileOperations(directory: String, fileName: String, fileStatus: (Int, String) -> Unit) {
val oldFile = File(directory, "${fileName.split(".")[0]}.apks")
val newFile = File(directory, "${fileName.split(".")[0]}.zip")
if (FileUtils.renameFile(oldFile, newFile)) {
Log.i("CHANGED NAME\n Unzipping...")
try {
FileUtils.unzip(newFile, directory)
fileStatus.invoke(Constant.SUCCESS, "Rename and Unzip Successful")
Log.i("TRYING DELETING FILE")
val value = FileUtils.deleteFile(newFile)
Log.i("DELETE STATUS : $value")
} catch (exception: IOException) {
Log.i("Unzipping Failed: ${exception.printStackTrace()}")
fileStatus.invoke(Constant.FAILURE, "Unzipping Failed: ${exception.printStackTrace()}")
}
} else {
Log.i("FAILED RENAMING THE FILE!!")
fileStatus.invoke(Constant.FAILURE, "FAILED RENAMING THE FILE!!")
}
}
}
+74
View File
@@ -0,0 +1,74 @@
import java.io.*
import java.util.zip.ZipFile
object FileUtils {
/**
* Size of the buffer to read/write data
*/
private const val BUFFER_SIZE = 4096
fun deleteFile(file: File) =
file.delete()
/**
* @param oldFile
* @param newFile
*/
fun renameFile(oldFile: File, newFile: File) =
oldFile.renameTo(newFile)
/**
* @param zipFilePath
* @param destDirectory
* @throws IOException
*/
@Throws(IOException::class)
fun unzip(zipFilePath: File, destDirectory: String, skipFile: String = "toc.pb") {
File(destDirectory).run {
if (!exists()) {
mkdirs()
}
}
ZipFile(zipFilePath).use { zip ->
zip.entries().asSequence().forEach { entry ->
if (entry.name == skipFile) return@forEach
zip.getInputStream(entry).use { input ->
val filePath = destDirectory + File.separator + entry.name
if (!entry.isDirectory) {
// if the entry is a file, extracts it
extractFile(input, filePath)
} else {
// if the entry is a directory, make the directory
val dir = File(filePath)
dir.mkdir()
}
}
}
}
}
/**
* Extracts a zip entry (file entry)
* @param inputStream
* @param destFilePath
* @throws IOException
*/
@Throws(IOException::class)
private fun extractFile(inputStream: InputStream, destFilePath: String) {
val bos = BufferedOutputStream(FileOutputStream(destFilePath))
val bytesIn = ByteArray(BUFFER_SIZE)
var read: Int
while (inputStream.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
bos.close()
}
}
+9
View File
@@ -0,0 +1,9 @@
object Log {
var showLogs: Boolean = true
fun i(message: String) {
if (showLogs) {
println(message)
}
}
}
+73 -22
View File
@@ -1,49 +1,99 @@
import androidx.compose.material.MaterialTheme
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.*
import java.awt.FileDialog
import java.awt.Frame
import java.io.File
@Composable
@Preview
fun App() {
var isOpen by remember { mutableStateOf(false) }
var logs by remember { mutableStateOf("Logs:") }
var isOpen by remember { mutableStateOf(false) }
var showLoading by remember { mutableStateOf(false) }
if (isOpen) {
FileDialog(
onCloseRequest = {
isOpen = false
val file = File(it)
println("Result ${file.absolutePath}")
FileDialog { fileName, directory ->
logs = "Logs: "
isOpen = false
val cmd = Constant.getCommand().replace("INPUT_FILE_PATH", "${directory}$fileName")
.replace("OUTPUT_FILE_NAME", "${directory}${fileName.split(".")[0]}")
Log.i("Command $cmd")
showLoading = true
val runtime = Runtime.getRuntime()
val startTime = System.currentTimeMillis()
val process = runtime.exec(cmd)
process.waitFor()
val endTime = System.currentTimeMillis()
logs += if (process.exitValue() == 0) {
Log.i("Command Executed in ${((endTime - startTime) / 1000)}s")
"Command Executed in ${((endTime - startTime) / 1000)}s\n"
}else{
"Failed while running command"
}
)
FileHelper.performFileOperations(directory, fileName) { status, message ->
Log.i("STATUS - $status\nMESSAGE - $message")
logs += "STATUS - $status\nMESSAGE - $message"
showLoading = false
}
}
}
MaterialTheme {
Button(
onClick = {
isOpen = true
},
modifier = Modifier.padding(start = 16.dp, top = 8.dp, end = 16.dp, bottom = 8.dp)
.fillMaxWidth(0.32f),
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.padding(12.dp))
Text(
text = "Choose File",
color = Color.White,
fontWeight = FontWeight.Medium,
text = "Aab To Apk",
style = TextStyle(
fontSize = 28.sp
)
)
Spacer(modifier = Modifier.padding(8.dp))
Button(
onClick = {
isOpen = true
},
modifier = Modifier.padding(start = 16.dp, top = 8.dp, end = 16.dp, bottom = 8.dp)
.fillMaxWidth(0.32f),
) {
Text(
text = "Choose File",
color = Color.White,
fontWeight = FontWeight.Medium,
)
}
Spacer(modifier = Modifier.padding(8.dp))
Text(
textAlign = TextAlign.Center,
text = logs,
style = TextStyle(
color = Color.Blue,
fontSize = 18.sp
)
)
if (showLoading) {
CircularProgressIndicator(
modifier = Modifier.size(size = 64.dp),
strokeWidth = 6.dp
)
}
}
}
}
@@ -51,14 +101,14 @@ fun App() {
@Composable
private fun FileDialog(
parent: Frame? = null,
onCloseRequest: (result: String?) -> Unit
onCloseRequest: (fileName: String, directory: String) -> Unit
) = AwtWindow(
create = {
object : FileDialog(parent, "Choose a file", LOAD) {
override fun setVisible(value: Boolean) {
super.setVisible(value)
if (value) {
onCloseRequest(file)
onCloseRequest(file, directory)
}
}
}
@@ -68,6 +118,7 @@ private fun FileDialog(
fun main() = application {
Log.showLogs = true
Window(
onCloseRequest = ::exitApplication,
state = rememberWindowState(