package br.com.elwnet.manuia import android.app.* import android.content.Intent import android.os.Build import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat /** * MikeAI Foreground Service * Mantém o app vivo em background para o wake word funcionar. * Mostra notificação persistente "MikeAI está ouvindo..." */ class MikeForegroundService : Service() { companion object { const val CHANNEL_ID = "mikeai_listening" const val NOTIF_ID = 1001 const val ACTION_START = "START" const val ACTION_STOP = "STOP" } override fun onBind(intent: Intent?): IBinder? = null override fun onCreate() { super.onCreate() createNotificationChannel() } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { when (intent?.action) { ACTION_STOP -> { stopForeground(STOP_FOREGROUND_REMOVE) stopSelf() return START_NOT_STICKY } else -> { val notif = buildNotification() startForeground(NOTIF_ID, notif) } } return START_STICKY } private fun buildNotification(): Notification { val openIntent = PendingIntent.getActivity( this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) return NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(android.R.drawable.ic_btn_speak_now) .setContentTitle("MikeAI") .setContentText("Diga \"Mike\" para ativar...") .setOngoing(true) .setSilent(true) .setCategory(Notification.CATEGORY_SERVICE) .setContentIntent(openIntent) .build() } private fun createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( CHANNEL_ID, "MikeAI Ouvindo", NotificationManager.IMPORTANCE_MIN ).apply { description = "Mantém o MikeAI ativo para reconhecer seu nome" setShowBadge(false) } getSystemService(NotificationManager::class.java) .createNotificationChannel(channel) } } }