Skip to content

Commit f05eda9

Browse files
authored
Merge pull request #10 from m4gr3d/dev
Code cleanup
2 parents 3181984 + 5067754 commit f05eda9

File tree

3 files changed

+34
-65
lines changed

3 files changed

+34
-65
lines changed

app/src/main/java/org/godotengine/godot_gradle_build_environment/BuildEnvironment.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class BuildEnvironment(
1818
) {
1919

2020
companion object {
21-
private const val TAG = "BuildEnvironment"
22-
private const val STDOUT_TAG = "BuildEnvironment-Stdout"
23-
private const val STDERR_TAG = "BuildEnvironment-Stderr"
21+
private val TAG = BuildEnvironment::class.java.simpleName
22+
private val STDOUT_TAG = "$TAG-Stdout"
23+
private val STDERR_TAG = "$TAG-Stderr"
2424

2525
const val OUTPUT_INFO = 0;
2626
const val OUTPUT_STDOUT = 1;
@@ -41,7 +41,7 @@ class BuildEnvironment(
4141
File(rootfs, "env").readLines()
4242
} catch (e: IOException) {
4343
Log.i(TAG, "Unable to read default environment from $rootfs/env: $e")
44-
emptyList<String>()
44+
emptyList()
4545
}
4646
}
4747

@@ -147,7 +147,7 @@ class BuildEnvironment(
147147
stderrThread.join()
148148

149149
val exitCode = currentProcess?.waitFor() ?: 255
150-
Log.i(TAG, "ExitCode: " + exitCode.toString())
150+
Log.i(TAG, "ExitCode: $exitCode")
151151

152152
currentProcess = null
153153
return exitCode

app/src/main/java/org/godotengine/godot_gradle_build_environment/BuildEnvironmentService.kt

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import android.os.Message
1111
import android.os.Messenger
1212
import android.os.RemoteException
1313
import android.util.Log
14-
import java.io.File
15-
import java.util.LinkedList
14+
import java.util.concurrent.LinkedBlockingQueue
1615

1716
class BuildEnvironmentService : Service() {
1817

1918
companion object {
20-
private const val TAG = "BuildEnvironmentService"
19+
private val TAG = BuildEnvironmentService::class.java.simpleName
2120

2221
const val MSG_EXECUTE_GRADLE = 1
2322
const val MSG_COMMAND_RESULT = 2
@@ -30,28 +29,25 @@ class BuildEnvironmentService : Service() {
3029
}
3130

3231
private lateinit var mMessenger: Messenger
33-
private lateinit var mBuildEnvironment: BuildEnvironment
34-
private lateinit var mSettingsManager: SettingsManager
35-
private lateinit var mWorkThread: HandlerThread
36-
private lateinit var mWorkHandler: Handler
32+
private val mBuildEnvironment: BuildEnvironment by lazy {
33+
BuildEnvironment(
34+
applicationContext,
35+
AppPaths.getRootfs(applicationContext).absolutePath,
36+
AppPaths.getProjectDir(applicationContext).absolutePath
37+
)
38+
}
39+
private val mSettingsManager: SettingsManager by lazy { SettingsManager(applicationContext)}
40+
private val mWorkThread = HandlerThread("BuildEnvironmentServiceWorker")
41+
private val mWorkHandler: Handler by lazy { Handler(mWorkThread.looper) }
3742

38-
internal class WorkItem(public val msg: Message, public val id: Int)
39-
private val queue = LinkedList<WorkItem>()
43+
internal data class WorkItem(val msg: Message, val id: Int)
44+
private val queue = LinkedBlockingQueue<WorkItem>()
4045
private var currentItem: WorkItem? = null
4146

42-
private val lock = Object()
43-
4447
override fun onCreate() {
4548
super.onCreate()
4649

47-
val rootfs = AppPaths.getRootfs(this).absolutePath
48-
val projectDir = AppPaths.getProjectDir(this).absolutePath
49-
mBuildEnvironment = BuildEnvironment(this, rootfs, projectDir)
50-
mSettingsManager = SettingsManager(this)
51-
52-
mWorkThread = HandlerThread("BuildEnvironmentServiceWorker")
5350
mWorkThread.start()
54-
mWorkHandler = Handler(mWorkThread.looper)
5551

5652
val incomingHandler = object : Handler(Looper.getMainLooper()) {
5753
override fun handleMessage(msg: Message) {
@@ -83,10 +79,7 @@ class BuildEnvironmentService : Service() {
8379
override fun onBind(intent: Intent?): IBinder? = mMessenger.binder
8480

8581
private fun queueWork(item: WorkItem) {
86-
synchronized(lock) {
87-
queue.add(item)
88-
lock.notifyAll()
89-
}
82+
queue.put(item)
9083
}
9184

9285
private fun cancelWork(id: Int) {
@@ -97,20 +90,15 @@ class BuildEnvironmentService : Service() {
9790

9891
Log.i(TAG, "Canceling command: ${id}")
9992

100-
synchronized(lock) {
101-
if (currentItem?.id == id && currentItem?.msg?.what == MSG_EXECUTE_GRADLE) {
102-
mBuildEnvironment.killCurrentProcess()
103-
}
104-
queue.removeAll { it.id == id && it.msg.what == MSG_EXECUTE_GRADLE }
93+
if (currentItem?.id == id && currentItem?.msg?.what == MSG_EXECUTE_GRADLE) {
94+
mBuildEnvironment.killCurrentProcess()
10595
}
96+
queue.removeAll { it.id == id && it.msg.what == MSG_EXECUTE_GRADLE }
10697
}
10798

10899
private fun workerLoop() {
109100
while (true) {
110-
val work: WorkItem = synchronized(lock) {
111-
while (queue.isEmpty()) lock.wait()
112-
queue.removeFirst()
113-
}
101+
val work: WorkItem = queue.take()
114102

115103
currentItem = work
116104
handleMessage(work.msg)

app/src/main/java/org/godotengine/godot_gradle_build_environment/FileUtils.kt

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ import kotlin.math.pow
1313

1414
object FileUtils {
1515

16-
private const val TAG = "FileUtils"
16+
private val TAG = FileUtils::class.java.simpleName
1717

1818
fun tryCopyFile(source: File, dest: File): Boolean {
1919
try {
20-
FileInputStream(source).use { input ->
21-
FileOutputStream(dest).use { output ->
22-
input.copyTo(output)
23-
}
24-
}
25-
} catch (e: IOException) {
26-
Log.e(TAG, "Failed to copy ${source.absolutePath} to ${dest.absolutePath}: ${e.message}")
20+
source.copyTo(dest)
21+
} catch (e: Exception) {
22+
Log.e(TAG, "Failed to copy ${source.absolutePath} to ${dest.absolutePath}: ${e.message}", e)
2723
return false
2824
}
2925
return true
@@ -35,28 +31,13 @@ object FileUtils {
3531
return false
3632
}
3733

38-
var success = true
39-
sourceDir.walkTopDown().forEach { source ->
40-
val relativePath = source.relativeTo(sourceDir)
41-
val target = File(destDir, relativePath.path)
42-
43-
try {
44-
if (source.isDirectory) {
45-
if (!target.exists()) {
46-
target.mkdirs()
47-
}
48-
} else {
49-
if (!tryCopyFile(source, target)) {
50-
success = false
51-
}
52-
}
53-
} catch (e: IOException) {
54-
Log.e(TAG, "Failed to copy ${source.absolutePath} -> ${target.absolutePath}: ${e.message}")
55-
success = false
56-
}
34+
try {
35+
sourceDir.copyRecursively(destDir)
36+
} catch (e: Exception) {
37+
Log.e(TAG, "Failed to copy ${sourceDir.absolutePath} -> ${destDir.absolutePath}: ${e.message}", e)
38+
return false
5739
}
58-
59-
return success
40+
return true
6041
}
6142

6243
/**

0 commit comments

Comments
 (0)