Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] virtual-device-app: Add android service for managing app server #28399

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}