forked from ShuriZma/suyu
1
0
Fork 0

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.model.Game
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.NfcReader
import org.yuzu.yuzu_emu.utils.SerializableHelper.parcelable
import org.yuzu.yuzu_emu.utils.ThemeHelper
import 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 = false
private var menuVisible = false
private var emulationFragment: EmulationFragment? = null
@ -57,8 +55,7 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener {
private lateinit var game: Game
override 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 = 0x1000
fun 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 {

View File

@ -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: State
private var surface: Surface? = null
private 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.STOPPED
NativeLibrary.stopEmulation()
state = State.STOPPED
} else {
Log.warning("[EmulationFragment] Stop called while already stopped.")
}
@ -402,12 +402,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
@Synchronized
fun pause() {
if (state != State.PAUSED) {
state = State.PAUSED
Log.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 -> {

View File

@ -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()
}

View File

@ -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 clicked
val 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
}