diff --git a/CatFlipScreen.apk b/CatFlipScreen.apk index 39689f8..d9d18f8 100644 Binary files a/CatFlipScreen.apk and b/CatFlipScreen.apk differ diff --git a/app/release/app-release.apk b/app/release/app-release.apk index 39689f8..d9d18f8 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 17aef3a..ef7e6b9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,7 +3,12 @@ xmlns:tools="http://schemas.android.com/tools"> + + + + + + android:foregroundServiceType="systemExempted" + android:stopWithTask="false" + android:exported="false" /> diff --git a/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt b/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt index de056fa..877e2a9 100644 --- a/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt +++ b/app/src/main/java/net/mezimmah/catflipscreen/FlipScreenService.kt @@ -1,5 +1,6 @@ package net.mezimmah.catflipscreen +import android.app.AlarmManager import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager @@ -9,8 +10,8 @@ import android.content.Context import android.content.Intent import android.media.MediaRouter import android.os.IBinder +import android.os.SystemClock import android.util.Log -import androidx.core.app.NotificationCompat class FlipScreenService : Service() { @@ -20,34 +21,21 @@ class FlipScreenService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - showNotification(intent) + showNotification() showPresentationScreen() return super.onStartCommand(intent, flags, startId) } - private fun showNotification(intent: Intent?) { - val input: String? = intent?.getStringExtra("inputExtra") - val notificationIntent = Intent(this, MainActivity::class.java) - val pendingIntent = PendingIntent.getActivity( - this, - 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE - ) - val notification: Notification = NotificationCompat.Builder(this, CatFlipScreenApplication.CHANNEL_ID) - .setContentTitle("Auto Start Service") - .setContentText(input) - .setSmallIcon(android.R.drawable.ic_dialog_info) - .setContentIntent(pendingIntent) - .build() - val mNotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - val channel = NotificationChannel( - CatFlipScreenApplication.CHANNEL_ID, - CatFlipScreenApplication.CHANNEL_NAME, - NotificationManager.IMPORTANCE_DEFAULT - ) - mNotificationManager.createNotificationChannel(channel) - NotificationCompat.Builder(this, CatFlipScreenApplication.CHANNEL_ID) - startForeground(1, notification) + override fun onTaskRemoved(rootIntent: Intent?) { + val restartServiceIntent = Intent(applicationContext, FlipScreenService::class.java).also { + it.setPackage(packageName) + }; + val restartServicePendingIntent: PendingIntent = PendingIntent.getService(this, 1, restartServiceIntent, + PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE); + applicationContext.getSystemService(Context.ALARM_SERVICE); + val alarmService: AlarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager; + alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent); } private fun showPresentationScreen() { @@ -69,4 +57,32 @@ class FlipScreenService : Service() { ) } } + + private fun showNotification() { + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val channel = NotificationChannel( + CatFlipScreenApplication.CHANNEL_ID, + CatFlipScreenApplication.CHANNEL_NAME, + NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Cat Flip Screen Service" + channel.enableLights(true) + channel.enableVibration(true) + notificationManager.createNotificationChannel(channel) + + val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent -> + PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE) + } + val builder: Notification.Builder = Notification.Builder( + this, + CatFlipScreenApplication.CHANNEL_ID + ) + val notification = builder + .setContentTitle("Cat Flip Screen") + .setContentText("For making the external display work.") + .setContentIntent(pendingIntent) + .setSmallIcon(R.mipmap.ic_launcher) + .build() + startForeground(1, notification) + } } \ No newline at end of file diff --git a/app/src/main/java/net/mezimmah/catflipscreen/MyReceiver.kt b/app/src/main/java/net/mezimmah/catflipscreen/MyReceiver.kt index 7b2786a..bd85868 100644 --- a/app/src/main/java/net/mezimmah/catflipscreen/MyReceiver.kt +++ b/app/src/main/java/net/mezimmah/catflipscreen/MyReceiver.kt @@ -9,7 +9,7 @@ import android.util.Log class MyReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { - context.startForegroundService(Intent(context, FlipScreenService::class.java)) + context.startService(Intent(context, FlipScreenService::class.java)) Log.i("Autostart", "started") } } \ No newline at end of file