Skip to content

Commit

Permalink
virtual-device-app: Add android service for managing app server (#28399)
Browse files Browse the repository at this point in the history
Signed-off-by: Jaehoon You <[email protected]>
Signed-off-by: Charles Kim <[email protected]>
  • Loading branch information
Jaehoon-You authored and pull[bot] committed Feb 1, 2024
1 parent d6de7e6 commit 1179154
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:name=".App"
Expand All @@ -26,6 +27,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.matter.virtual.device.app.core.matter.MatterAppService" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.matter.virtual.device.app.core.matter

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

@AndroidEntryPoint
class MatterAppService : Service() {

override fun onCreate() {
super.onCreate()
Timber.d("onCreate()")
}

override fun onDestroy() {
super.onDestroy()
Timber.d("onDestroy()")
}

override fun onBind(intent: Intent?): IBinder? {
Timber.d("onBind()")
return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.d("Hit")

intent?.let {
when (it.action) {
MatterAppServiceConstants.ACTION_START_MATTER_APP_SERVICE -> {
Timber.i("Start matter app service")
startService()
}
MatterAppServiceConstants.ACTION_STOP_MATTER_APP_SERVICE -> {
Timber.i("Stop matter app service")
stopService()
}
else -> {}
}
}

return START_REDELIVER_INTENT
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel =
NotificationChannel(
MatterAppServiceConstants.NOTIFICATION_CHANNEL_ID,
MatterAppServiceConstants.NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)

val manager = getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(notificationChannel)
}
}

private fun cancelNotificationChannel() {
val manager = getSystemService(NotificationManager::class.java)
manager?.cancel(MatterAppServiceConstants.NOTIFICATION_FOREGROUND_ID)
}

private fun startService() {
Timber.d("Hit")

createNotificationChannel()

val notification: Notification =
NotificationCompat.Builder(this, MatterAppServiceConstants.NOTIFICATION_CHANNEL_ID)
.setContentTitle("MatterApp Service")
.setContentText("MatterApp is running")
.setSmallIcon(R.mipmap.ic_launcher)
.build()

startForeground(MatterAppServiceConstants.NOTIFICATION_FOREGROUND_ID, notification)
}

private fun stopService() {
Timber.d("Hit")
cancelNotificationChannel()
stopForeground(true)
stopSelf()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.matter.virtual.device.app.core.matter

object MatterAppServiceConstants {
const val ACTION_START_MATTER_APP_SERVICE = "ACTION_START_MATTER_APP_SERVICE"
const val ACTION_STOP_MATTER_APP_SERVICE = "ACTION_STOP_MATTER_APP_SERVICE"

const val NOTIFICATION_CHANNEL_ID = "VIRTUAL_DEVICE"
const val NOTIFICATION_FOREGROUND_ID = 1
const val NOTIFICATION_CHANNEL_NAME = "Matter App"
}

0 comments on commit 1179154

Please sign in to comment.