Skip to content

Commit

Permalink
Updated Kotlin and Java versions, removed sequences, started migratio…
Browse files Browse the repository at this point in the history
…n to coroutines
  • Loading branch information
Szaki committed Jan 22, 2020
1 parent fb3bd9b commit 0a8b3a2
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 116 deletions.
17 changes: 8 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.60'
id 'application'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'com.eu.szaki'

repositories {
mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "org.openjfx:javafx-graphics:11.0.2:linux"
compile "org.openjfx:javafx-graphics:11.0.2:win"
compile "org.openjfx:javafx-graphics:11.0.2:mac"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-javafx:1.3.3"
implementation "org.openjfx:javafx-graphics:11.0.2:linux"
implementation "org.openjfx:javafx-graphics:11.0.2:win"
implementation "org.openjfx:javafx-graphics:11.0.2:mac"
}

jar {
Expand All @@ -28,11 +27,11 @@ jar {
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "11"
}

javafx {
modules = [ 'javafx.base', 'javafx.graphics', 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = 'Main'
mainClassName = "Main"
3 changes: 1 addition & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = 'XiaomiADBFastbootTools'

rootProject.name = "XiaomiADBFastbootTools"
48 changes: 24 additions & 24 deletions src/main/kotlin/AppManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import javafx.scene.control.TableView
import java.io.File
import java.net.URL
import java.util.*
import kotlin.concurrent.thread
import kotlinx.coroutines.*


object AppManager : Command() {
Expand All @@ -18,16 +18,16 @@ object AppManager : Command() {
lateinit var progress: ProgressBar
lateinit var progressInd: ProgressIndicator
var user = 0
private val apps: Sequence<String>?
private val apps: List<String>?
val customApps = File(MainController.dir, "apps.yml")
private var potentialApps = mutableMapOf<String, String>()

init {
apps = try {
URL("https://raw.githubusercontent.com/Szaki/XiaomiADBFastbootTools/master/src/main/resources/apps.yml").readText()
.trim().lineSequence()
.trim().lines()
} catch (ex: Exception) {
this::class.java.classLoader.getResource("apps.yml")?.readText()?.trim()?.lineSequence()
this::class.java.classLoader.getResource("apps.yml")?.readText()?.trim()?.lines()
}
}

Expand Down Expand Up @@ -66,16 +66,16 @@ object AppManager : Command() {
val disableApps = mutableMapOf<String, MutableList<String>>()
val enableApps = mutableMapOf<String, MutableList<String>>()
val deviceApps = mutableMapOf<String, String>()
exec("adb shell pm list packages -u --user $user").trim().lineSequence().forEach {
exec("adb shell pm list packages -u --user $user").trim().lines().forEach {
deviceApps[it.substringAfter(':').trim()] = "uninstalled"
}
exec("adb shell pm list packages -d --user $user").trim().lineSequence().forEach {
exec("adb shell pm list packages -d --user $user").trim().lines().forEach {
deviceApps[it.substringAfter(':').trim()] = "disabled"
}
exec("adb shell pm list packages -e --user $user").trim().lineSequence().forEach {
exec("adb shell pm list packages -e --user $user").trim().lines().forEach {
deviceApps[it.substringAfter(':').trim()] = "enabled"
}
potentialApps.asSequence().forEach { (pkg, name) ->
potentialApps.forEach { (pkg, name) ->
when (deviceApps[pkg]) {
"disabled" -> {
uninstallApps.add(name, pkg)
Expand Down Expand Up @@ -103,8 +103,8 @@ object AppManager : Command() {
outputTextArea.text = ""
progress.progress = 0.0
progressInd.isVisible = true
thread(true, true) {
selected.asSequence().forEach {
GlobalScope.launch {
selected.forEach {
it.packagenameProperty().get().trim().lines().forEach { pkg ->
proc =
pb.command("${prefix}adb", "shell", "pm", "uninstall", "--user", "$user", pkg.trim())
Expand All @@ -114,7 +114,7 @@ object AppManager : Command() {
while (scanner.hasNextLine())
sb.append(scanner.nextLine() + '\n')
}
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.apply {
appendText("App: ${it.appnameProperty().get()}\n")
appendText("Package: $pkg\n")
Expand All @@ -124,7 +124,7 @@ object AppManager : Command() {
}
}
}
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.appendText("Done!")
progress.progress = 0.0
progressInd.isVisible = false
Expand All @@ -139,8 +139,8 @@ object AppManager : Command() {
outputTextArea.text = ""
progress.progress = 0.0
progressInd.isVisible = true
thread(true, true) {
selected.asSequence().forEach {
GlobalScope.launch {
selected.forEach {
it.packagenameProperty().get().trim().lines().forEach { pkg ->
proc =
pb.command(
Expand All @@ -163,7 +163,7 @@ object AppManager : Command() {
output = if ("installed for user" in output)
"Success\n"
else "Failure [${output.substringAfter(pkg).trim()}]\n"
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.apply {
appendText("App: ${it.appnameProperty().get()}\n")
appendText("Package: $pkg\n")
Expand All @@ -173,7 +173,7 @@ object AppManager : Command() {
}
}
}
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.appendText("Done!")
progress.progress = 0.0
progressInd.isVisible = false
Expand All @@ -188,8 +188,8 @@ object AppManager : Command() {
outputTextArea.text = ""
progress.progress = 0.0
progressInd.isVisible = true
thread(true, true) {
selected.asSequence().forEach {
GlobalScope.launch {
selected.forEach {
it.packagenameProperty().get().trim().lines().forEach { pkg ->
proc = pb.command(
"${prefix}adb",
Expand All @@ -208,7 +208,7 @@ object AppManager : Command() {
val output = if ("disabled-user" in sb.toString())
"Success\n"
else "Failure\n"
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.apply {
appendText("App: ${it.appnameProperty().get()}\n")
appendText("Package: $pkg\n")
Expand All @@ -218,7 +218,7 @@ object AppManager : Command() {
}
}
}
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.appendText("Done!")
progress.progress = 0.0
progressInd.isVisible = false
Expand All @@ -233,8 +233,8 @@ object AppManager : Command() {
outputTextArea.text = ""
progress.progress = 0.0
progressInd.isVisible = true
thread(true, true) {
selected.asSequence().forEach {
GlobalScope.launch {
selected.forEach {
it.packagenameProperty().get().trim().lines().forEach { pkg ->
proc =
pb.command("${prefix}adb", "shell", "pm", "enable", "--user", "$user", pkg.trim())
Expand All @@ -247,7 +247,7 @@ object AppManager : Command() {
val output = if ("enabled" in sb.toString())
"Success\n"
else "Failure\n"
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.apply {
appendText("App: ${it.appnameProperty().get()}\n")
appendText("Package: $pkg\n")
Expand All @@ -257,7 +257,7 @@ object AppManager : Command() {
}
}
}
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.appendText("Done!")
progress.progress = 0.0
progressInd.isVisible = false
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import javafx.scene.control.ProgressIndicator
import javafx.scene.control.TextInputControl
import java.io.File
import java.util.*
import kotlin.concurrent.thread
import kotlinx.coroutines.*

open class Command {

Expand Down Expand Up @@ -55,22 +55,22 @@ open class Command {
pb.redirectErrorStream(true)
progressIndicator.isVisible = true
outputTextArea.text = ""
thread(true, true) {
GlobalScope.launch {
args.forEach {
val bits = it.split(' ').toMutableList()
bits[0] = prefix + bits[0]
proc = pb.command(bits + image?.absolutePath).start()
Scanner(proc.inputStream, "UTF-8").useDelimiter("").use { scanner ->
while (scanner.hasNext()) {
val next = scanner.next()
Platform.runLater {
withContext(Dispatchers.Main) {
outputTextArea.appendText(next)
}
}
}
proc.waitFor()
}
Platform.runLater {
withContext(Dispatchers.Main) {
progressIndicator.isVisible = false
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object Device {
if (mode == Mode.ADB && serial in propstring && dpi != -1 && width != -1 && height != -1)
return true
props.clear()
propstring.trim().lineSequence().forEach {
propstring.trim().lines().forEach {
val parts = it.split("]: [")
if (parts.size == 2)
props[parts[0].trimStart('[')] = parts[1].trimEnd(']')
Expand Down Expand Up @@ -78,7 +78,7 @@ object Device {
mode == Mode.FASTBOOT && serial in status -> return true
}
props.clear()
command.exec("fastboot getvar all").trim().lineSequence().forEach {
command.exec("fastboot getvar all").trim().lines().forEach {
if (it[0] == '(')
props[it.substringAfter(')').substringBeforeLast(':').trim()] = it.substringAfterLast(':').trim()
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/kotlin/FileExplorer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import javafx.scene.control.ProgressBar
import javafx.scene.control.TextField
import java.io.File
import java.util.*
import kotlin.concurrent.thread
import kotlinx.coroutines.*

class FileExplorer(val statusTextField: TextField, val statusProgressBar: ProgressBar) : Command() {

Expand Down Expand Up @@ -65,14 +65,14 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre

fun String.fmt(): String = "'$this'"

fun init(command: String = "adb") {
fun init(command: String = "adb") = runBlocking(Dispatchers.IO) {
pb.redirectErrorStream(false)
statusTextField.text = ""
proc = pb.start()
Scanner(proc.inputStream, "UTF-8").useDelimiter("").use { scanner ->
while (scanner.hasNextLine()) {
val output = scanner.nextLine()
Platform.runLater {
withContext(Dispatchers.Main) {
if ('%' in output)
statusProgressBar.progress = output.substringBefore('%').trim('[', ' ').toInt() / 100.0
else if (command in output)
Expand All @@ -84,7 +84,7 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre
}

inline fun pull(selected: List<AndroidFile>, to: File, crossinline func: () -> Unit) {
thread(true, true) {
GlobalScope.launch {
if (selected.isEmpty()) {
pb.command("${prefix}adb", "pull", path, to.absolutePath)
init()
Expand All @@ -94,7 +94,7 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre
init()
}
}
Platform.runLater {
withContext(Dispatchers.Main) {
if (statusTextField.text.isEmpty())
statusTextField.text = "Done!"
statusProgressBar.progress = 0.0
Expand All @@ -104,12 +104,12 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre
}

inline fun push(selected: List<File>, crossinline func: () -> Unit) {
thread(true, true) {
GlobalScope.launch {
selected.forEach {
pb.command("${prefix}adb", "push", it.absolutePath, path)
init()
}
Platform.runLater {
withContext(Dispatchers.Main) {
if (statusTextField.text.isEmpty())
statusTextField.text = "Done!"
statusProgressBar.progress = 0.0
Expand All @@ -119,14 +119,14 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre
}

inline fun delete(selected: List<AndroidFile>, crossinline func: () -> Unit) {
thread(true, true) {
GlobalScope.launch {
selected.forEach {
if (it.dir)
pb.command("${prefix}adb", "shell", "rm", "-rf", (path + it.name).fmt())
else pb.command("${prefix}adb", "shell", "rm", "-f", (path + it.name).fmt())
init("rm")
}
Platform.runLater {
withContext(Dispatchers.Main) {
if (statusTextField.text.isEmpty())
statusTextField.text = "Done!"
statusProgressBar.progress = 0.0
Expand All @@ -136,10 +136,10 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre
}

inline fun mkdir(name: String, crossinline func: () -> Unit) {
thread(true, true) {
GlobalScope.launch {
pb.command("${prefix}adb", "shell", "mkdir", (path + name).fmt())
init("mkdir")
Platform.runLater {
withContext(Dispatchers.Main) {
if (statusTextField.text.isEmpty())
statusTextField.text = "Done!"
statusProgressBar.progress = 0.0
Expand All @@ -149,10 +149,10 @@ class FileExplorer(val statusTextField: TextField, val statusProgressBar: Progre
}

inline fun rename(selected: AndroidFile, to: String, crossinline func: () -> Unit) {
thread(true, true) {
GlobalScope.launch {
pb.command("${prefix}adb", "shell", "mv", (path + selected.name).fmt(), (path + to).fmt())
init("mv")
Platform.runLater {
withContext(Dispatchers.Main) {
if (statusTextField.text.isEmpty())
statusTextField.text = "Done!"
statusProgressBar.progress = 0.0
Expand Down
Loading

0 comments on commit 0a8b3a2

Please sign in to comment.