Skip to content

Commit

Permalink
App/Service: Add nnstreamer to ml_inference_offloading service
Browse files Browse the repository at this point in the history
This patch adds nnstreamer to android service.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Apr 23, 2024
1 parent a76e44b commit 37df9ae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions ml_inference_offloading/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(project(":nnstreamer-api"))
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.os.Process
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import org.nnsuite.nnstreamer.NNStreamer


class MainService : Service() {
private inner class MainHandler(looper: Looper) : Handler(looper) {
Expand All @@ -36,6 +39,7 @@ class MainService : Service() {
private lateinit var serviceHandler : MainHandler
private lateinit var serviceLooper : Looper
private lateinit var handlerThread: HandlerThread
private var initialized = false

private fun startForeground() {
// Get NotificationManager
Expand Down Expand Up @@ -73,6 +77,7 @@ class MainService : Service() {
}

override fun onCreate() {
initNNStreamer()
handlerThread = HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
}
Expand Down Expand Up @@ -102,4 +107,22 @@ class MainService : Service() {
override fun onDestroy() {
Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show()
}
}

private fun initNNStreamer() {
if (this.initialized) {
return
}
try {
initialized = NNStreamer.initialize(this)
} catch (e: Exception) {
e.printStackTrace()
Log.e(TAG, e.message!!)
} finally {
if (initialized) {
Log.i(TAG, "Version: " + NNStreamer.getVersion())
} else {
Log.e(TAG, "Failed to initialize NNStreamer")
}
}
}
}
22 changes: 22 additions & 0 deletions nnstreamer-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,25 @@ android {
}
}
}

afterEvaluate {
val compileJavaDebug = project.getTasksByName("compileDebugJavaWithJavac", false)
val nativeBuildDebug = project.getTasksByName("externalNativeBuildDebug", false)

if (compileJavaDebug.size >= 1 && nativeBuildDebug.size >= 1) {
val compileJavaTask = compileJavaDebug.first()
val nativeBuildTask = nativeBuildDebug.first()

compileJavaTask.dependsOn(nativeBuildTask)
}

val compileJavaRelease = project.getTasksByName("compileReleaseJavaWithJavac", false)
val nativeBuildRelease = project.getTasksByName("externalNativeBuildRelease", false)

if (compileJavaRelease.size >= 1 && nativeBuildRelease.size >= 1) {
val compileJavaTask = compileJavaDebug.first()
val nativeBuildTask = nativeBuildDebug.first()

compileJavaTask.dependsOn(nativeBuildTask)
}
}

0 comments on commit 37df9ae

Please sign in to comment.