diff options
| author | Charles Lombardo <clombardo169@gmail.com> | 2023-03-11 00:37:22 -0500 | 
|---|---|---|
| committer | bunnei <bunneidev@gmail.com> | 2023-06-03 00:05:41 -0700 | 
| commit | 5d5233ec324aa75d20b7657dfc8aeb9930a0997e (patch) | |
| tree | 6e09b2645f01983906a799bd1bed7ac16e866359 | |
| parent | 7b54c2b2e25adb81c95d54941f55bae76770cdb9 (diff) | |
android: Convert ForegroundService to Kotlin
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.java | 63 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt | 56 | 
2 files changed, 56 insertions, 63 deletions
| diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.java deleted file mode 100644 index 8834c7bc5..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.java +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright 2014 Dolphin Emulator Project - * Licensed under GPLv2+ - * Refer to the license.txt file included. - */ - -package org.yuzu.yuzu_emu.utils; - -import android.app.PendingIntent; -import android.app.Service; -import android.content.Intent; -import android.os.IBinder; - -import androidx.core.app.NotificationCompat; -import androidx.core.app.NotificationManagerCompat; - -import org.yuzu.yuzu_emu.R; -import org.yuzu.yuzu_emu.activities.EmulationActivity; - -/** - * A service that shows a permanent notification in the background to avoid the app getting - * cleared from memory by the system. - */ -public class ForegroundService extends Service { -    private static final int EMULATION_RUNNING_NOTIFICATION = 0x1000; - -    private void showRunningNotification() { -        // Intent is used to resume emulation if the notification is clicked -        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, -                new Intent(this, EmulationActivity.class), PendingIntent.FLAG_IMMUTABLE); - -        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.app_notification_channel_id)) -                .setSmallIcon(R.drawable.ic_stat_notification_logo) -                .setContentTitle(getString(R.string.app_name)) -                .setContentText(getString(R.string.app_notification_running)) -                .setPriority(NotificationCompat.PRIORITY_LOW) -                .setOngoing(true) -                .setVibrate(null) -                .setSound(null) -                .setContentIntent(contentIntent); -        startForeground(EMULATION_RUNNING_NOTIFICATION, builder.build()); -    } - -    @Override -    public IBinder onBind(Intent intent) { -        return null; -    } - -    @Override -    public void onCreate() { -        showRunningNotification(); -    } - -    @Override -    public int onStartCommand(Intent intent, int flags, int startId) { -        return START_STICKY; -    } - -    @Override -    public void onDestroy() { -        NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION); -    } -} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt new file mode 100644 index 000000000..238ed54d3 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt @@ -0,0 +1,56 @@ +package org.yuzu.yuzu_emu.utils + +import android.app.PendingIntent +import android.app.Service +import android.content.Intent +import android.os.IBinder +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.activities.EmulationActivity + +/** + * A service that shows a permanent notification in the background to avoid the app getting + * cleared from memory by the system. + */ +class ForegroundService : Service() { +    companion object { +        private const val EMULATION_RUNNING_NOTIFICATION = 0x1000 +    } + +    private fun showRunningNotification() { +        // Intent is used to resume emulation if the notification is clicked +        val contentIntent = PendingIntent.getActivity( +            this, 0, +            Intent(this, EmulationActivity::class.java), +            PendingIntent.FLAG_IMMUTABLE +        ) +        val builder = +            NotificationCompat.Builder(this, getString(R.string.app_notification_channel_id)) +                .setSmallIcon(R.drawable.ic_stat_notification_logo) +                .setContentTitle(getString(R.string.app_name)) +                .setContentText(getString(R.string.app_notification_running)) +                .setPriority(NotificationCompat.PRIORITY_LOW) +                .setOngoing(true) +                .setVibrate(null) +                .setSound(null) +                .setContentIntent(contentIntent) +        startForeground(EMULATION_RUNNING_NOTIFICATION, builder.build()) +    } + +    override fun onBind(intent: Intent): IBinder? { +        return null +    } + +    override fun onCreate() { +        showRunningNotification() +    } + +    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { +        return START_STICKY +    } + +    override fun onDestroy() { +        NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION) +    } +} | 
