android: Re-enable service notification

This commit is contained in:
Charles Lombardo 2023-05-29 03:16:04 -04:00 committed by bunnei
parent 09747ca2d3
commit 0f9c5b8d6a
4 changed files with 29 additions and 24 deletions

View file

@ -31,18 +31,16 @@ import org.yuzu.yuzu_emu.features.settings.model.Settings
import org.yuzu.yuzu_emu.fragments.EmulationFragment import org.yuzu.yuzu_emu.fragments.EmulationFragment
import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.Game
import org.yuzu.yuzu_emu.utils.ControllerMappingHelper import org.yuzu.yuzu_emu.utils.ControllerMappingHelper
import org.yuzu.yuzu_emu.utils.ForegroundService
import org.yuzu.yuzu_emu.utils.InputHandler import org.yuzu.yuzu_emu.utils.InputHandler
import org.yuzu.yuzu_emu.utils.NfcReader import org.yuzu.yuzu_emu.utils.NfcReader
import org.yuzu.yuzu_emu.utils.SerializableHelper.parcelable import org.yuzu.yuzu_emu.utils.SerializableHelper.parcelable
import org.yuzu.yuzu_emu.utils.ThemeHelper import org.yuzu.yuzu_emu.utils.ThemeHelper
import kotlin.math.roundToInt import kotlin.math.roundToInt
open class EmulationActivity : AppCompatActivity(), SensorEventListener { class EmulationActivity : AppCompatActivity(), SensorEventListener {
private var controllerMappingHelper: ControllerMappingHelper? = null private var controllerMappingHelper: ControllerMappingHelper? = null
// TODO(bunnei): Disable notifications until we support app suspension.
//private Intent foregroundService;
var isActivityRecreated = false var isActivityRecreated = false
private var menuVisible = false private var menuVisible = false
private var emulationFragment: EmulationFragment? = null private var emulationFragment: EmulationFragment? = null
@ -57,8 +55,7 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {
private lateinit var game: Game private lateinit var game: Game
override fun onDestroy() { override fun onDestroy() {
// TODO(bunnei): Disable notifications until we support app suspension. stopForegroundService(this)
//stopService(foregroundService);
super.onDestroy() super.onDestroy()
} }
@ -100,9 +97,8 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {
inputHandler.initialize() inputHandler.initialize()
// Start a foreground service to prevent the app from getting killed in the background // Start a foreground service to prevent the app from getting killed in the background
// TODO(bunnei): Disable notifications until we support app suspension. val startIntent = Intent(this, ForegroundService::class.java)
//foregroundService = new Intent(EmulationActivity.this, ForegroundService.class); startForegroundService(startIntent)
//startForegroundService(foregroundService);
} }
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
@ -324,7 +320,6 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {
companion object { companion object {
const val EXTRA_SELECTED_GAME = "SelectedGame" const val EXTRA_SELECTED_GAME = "SelectedGame"
private const val EMULATION_RUNNING_NOTIFICATION = 0x1000
fun launch(activity: AppCompatActivity, game: Game) { fun launch(activity: AppCompatActivity, game: Game) {
val launcher = Intent(activity, EmulationActivity::class.java) val launcher = Intent(activity, EmulationActivity::class.java)
@ -332,9 +327,10 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {
activity.startActivity(launcher) activity.startActivity(launcher)
} }
fun tryDismissRunningNotification(activity: Activity?) { fun stopForegroundService(activity: Activity) {
// TODO(bunnei): Disable notifications until we support app suspension. val startIntent = Intent(activity, ForegroundService::class.java)
//NotificationManagerCompat.from(activity).cancel(EMULATION_RUNNING_NOTIFICATION); startIntent.action = ForegroundService.ACTION_STOP
activity.startForegroundService(startIntent)
} }
private fun areCoordinatesOutside(view: View?, x: Float, y: Float): Boolean { private fun areCoordinatesOutside(view: View?, x: Float, y: Float): Boolean {

View file

@ -123,8 +123,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
} }
R.id.menu_exit -> { R.id.menu_exit -> {
requireActivity().finish()
emulationState.stop() emulationState.stop()
requireActivity().finish()
true 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: State private var state: State
private var surface: Surface? = null private var surface: Surface? = null
private var runWhenSurfaceIsValid = false private var runWhenSurfaceIsValid = false
@ -391,8 +391,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
fun stop() { fun stop() {
if (state != State.STOPPED) { if (state != State.STOPPED) {
Log.debug("[EmulationFragment] Stopping emulation.") Log.debug("[EmulationFragment] Stopping emulation.")
state = State.STOPPED
NativeLibrary.stopEmulation() NativeLibrary.stopEmulation()
state = State.STOPPED
} else { } else {
Log.warning("[EmulationFragment] Stop called while already stopped.") Log.warning("[EmulationFragment] Stop called while already stopped.")
} }
@ -402,12 +402,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
@Synchronized @Synchronized
fun pause() { fun pause() {
if (state != State.PAUSED) { if (state != State.PAUSED) {
state = State.PAUSED
Log.debug("[EmulationFragment] Pausing emulation.") Log.debug("[EmulationFragment] Pausing emulation.")
// Release the surface before pausing, since emulation has to be running for that. // Release the surface before pausing, since emulation has to be running for that.
NativeLibrary.surfaceDestroyed() NativeLibrary.surfaceDestroyed()
NativeLibrary.pauseEmulation() NativeLibrary.pauseEmulation()
state = State.PAUSED
} else { } else {
Log.warning("[EmulationFragment] Pause called while already paused.") Log.warning("[EmulationFragment] Pause called while already paused.")
} }
@ -464,11 +465,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
when (state) { when (state) {
State.STOPPED -> { State.STOPPED -> {
NativeLibrary.surfaceChanged(surface) NativeLibrary.surfaceChanged(surface)
val mEmulationThread = Thread({ val emulationThread = Thread({
Log.debug("[EmulationFragment] Starting emulation thread.") Log.debug("[EmulationFragment] Starting emulation thread.")
NativeLibrary.run(mGamePath) NativeLibrary.run(gamePath)
}, "NativeEmulation") }, "NativeEmulation")
mEmulationThread.start() emulationThread.start()
} }
State.PAUSED -> { State.PAUSED -> {

View file

@ -119,7 +119,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
} }
// Dismiss previous notifications (should not happen unless a crash occurred) // Dismiss previous notifications (should not happen unless a crash occurred)
EmulationActivity.tryDismissRunningNotification(this) EmulationActivity.stopForegroundService(this)
setInsets() setInsets()
} }
@ -221,7 +221,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
} }
override fun onDestroy() { override fun onDestroy() {
EmulationActivity.tryDismissRunningNotification(this) EmulationActivity.stopForegroundService(this)
super.onDestroy() super.onDestroy()
} }

View file

@ -18,13 +18,16 @@ import org.yuzu.yuzu_emu.activities.EmulationActivity
*/ */
class ForegroundService : Service() { class ForegroundService : Service() {
companion object { companion object {
private const val EMULATION_RUNNING_NOTIFICATION = 0x1000 const val EMULATION_RUNNING_NOTIFICATION = 0x1000
const val ACTION_STOP = "stop"
} }
private fun showRunningNotification() { private fun showRunningNotification() {
// Intent is used to resume emulation if the notification is clicked // Intent is used to resume emulation if the notification is clicked
val contentIntent = PendingIntent.getActivity( val contentIntent = PendingIntent.getActivity(
this, 0, this,
0,
Intent(this, EmulationActivity::class.java), Intent(this, EmulationActivity::class.java),
PendingIntent.FLAG_IMMUTABLE PendingIntent.FLAG_IMMUTABLE
) )
@ -50,6 +53,11 @@ class ForegroundService : Service() {
} }
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 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 return START_STICKY
} }