VERY experimental Nintendo Switch 2 emulator
C++
android: Re-enable service notification
Charles Lombardo70b013ab2f46
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.ktindex ea4071ba7b..37caa1b83e 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt@@ -31,18 +31,16 @@ import org.yuzu.yuzu_emu.features.settings.model.Settingsimport org.yuzu.yuzu_emu.fragments.EmulationFragmentimport org.yuzu.yuzu_emu.model.Gameimport org.yuzu.yuzu_emu.utils.ControllerMappingHelper+import org.yuzu.yuzu_emu.utils.ForegroundServiceimport org.yuzu.yuzu_emu.utils.InputHandlerimport org.yuzu.yuzu_emu.utils.NfcReaderimport org.yuzu.yuzu_emu.utils.SerializableHelper.parcelableimport org.yuzu.yuzu_emu.utils.ThemeHelperimport kotlin.math.roundToInt-open class EmulationActivity : AppCompatActivity(), SensorEventListener {+class EmulationActivity : AppCompatActivity(), SensorEventListener {private var controllerMappingHelper: ControllerMappingHelper? = null- // TODO(bunnei): Disable notifications until we support app suspension.- //private Intent foregroundService;-var isActivityRecreated = falseprivate var menuVisible = falseprivate var emulationFragment: EmulationFragment? = null@@ -57,8 +55,7 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {private lateinit var game: Gameoverride fun onDestroy() {- // TODO(bunnei): Disable notifications until we support app suspension.- //stopService(foregroundService);+ stopForegroundService(this)super.onDestroy()}@@ -100,9 +97,8 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {inputHandler.initialize()// Start a foreground service to prevent the app from getting killed in the background- // TODO(bunnei): Disable notifications until we support app suspension.- //foregroundService = new Intent(EmulationActivity.this, ForegroundService.class);- //startForegroundService(foregroundService);+ val startIntent = Intent(this, ForegroundService::class.java)+ startForegroundService(startIntent)}override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {@@ -324,7 +320,6 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {companion object {const val EXTRA_SELECTED_GAME = "SelectedGame"- private const val EMULATION_RUNNING_NOTIFICATION = 0x1000fun launch(activity: AppCompatActivity, game: Game) {val launcher = Intent(activity, EmulationActivity::class.java)@@ -332,9 +327,10 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {activity.startActivity(launcher)}- fun tryDismissRunningNotification(activity: Activity?) {- // TODO(bunnei): Disable notifications until we support app suspension.- //NotificationManagerCompat.from(activity).cancel(EMULATION_RUNNING_NOTIFICATION);+ fun stopForegroundService(activity: Activity) {+ val startIntent = Intent(activity, ForegroundService::class.java)+ startIntent.action = ForegroundService.ACTION_STOP+ activity.startForegroundService(startIntent)}private fun areCoordinatesOutside(view: View?, x: Float, y: Float): Boolean {diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.ktindex a31414a0e6..ce3f2639ab 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt@@ -123,8 +123,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {}R.id.menu_exit -> {- requireActivity().finish()emulationState.stop()+ requireActivity().finish()true}@@ -364,7 +364,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {}}- private class EmulationState(private val mGamePath: String?) {+ private class EmulationState(private val gamePath: String) {private var state: Stateprivate var surface: Surface? = nullprivate var runWhenSurfaceIsValid = false@@ -391,8 +391,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {fun stop() {if (state != State.STOPPED) {Log.debug("[EmulationFragment] Stopping emulation.")- state = State.STOPPEDNativeLibrary.stopEmulation()+ state = State.STOPPED} else {Log.warning("[EmulationFragment] Stop called while already stopped.")}@@ -402,12 +402,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {@Synchronizedfun pause() {if (state != State.PAUSED) {- state = State.PAUSEDLog.debug("[EmulationFragment] Pausing emulation.")// Release the surface before pausing, since emulation has to be running for that.NativeLibrary.surfaceDestroyed()NativeLibrary.pauseEmulation()++ state = State.PAUSED} else {Log.warning("[EmulationFragment] Pause called while already paused.")}@@ -464,11 +465,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {when (state) {State.STOPPED -> {NativeLibrary.surfaceChanged(surface)- val mEmulationThread = Thread({+ val emulationThread = Thread({Log.debug("[EmulationFragment] Starting emulation thread.")- NativeLibrary.run(mGamePath)+ NativeLibrary.run(gamePath)}, "NativeEmulation")- mEmulationThread.start()+ emulationThread.start()}State.PAUSED -> {diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.ktindex 961b5b8222..b1329db74c 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt@@ -119,7 +119,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {}// Dismiss previous notifications (should not happen unless a crash occurred)- EmulationActivity.tryDismissRunningNotification(this)+ EmulationActivity.stopForegroundService(this)setInsets()}@@ -221,7 +221,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {}override fun onDestroy() {- EmulationActivity.tryDismissRunningNotification(this)+ EmulationActivity.stopForegroundService(this)super.onDestroy()}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.ktindex 7e33ff0447..6261239660 100644--- 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@@ -18,13 +18,16 @@ import org.yuzu.yuzu_emu.activities.EmulationActivity*/class ForegroundService : Service() {companion object {- private const val EMULATION_RUNNING_NOTIFICATION = 0x1000+ const val EMULATION_RUNNING_NOTIFICATION = 0x1000++ const val ACTION_STOP = "stop"}private fun showRunningNotification() {// Intent is used to resume emulation if the notification is clickedval contentIntent = PendingIntent.getActivity(- this, 0,+ this,+ 0,Intent(this, EmulationActivity::class.java),PendingIntent.FLAG_IMMUTABLE)@@ -50,6 +53,11 @@ class ForegroundService : Service() {}override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {+ if (intent.action == ACTION_STOP) {+ NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION)+ stopForeground(STOP_FOREGROUND_REMOVE)+ stopSelfResult(startId)+ }return START_STICKY}