summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt
blob: 0da7562a6104bc175c26f2595a6dd89ae3491843 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.activities

import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.slider.Slider.OnChangeListener
import org.yuzu.yuzu_emu.NativeLibrary
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.databinding.DialogSliderBinding
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.SerializableHelper.parcelable
import org.yuzu.yuzu_emu.utils.ThemeHelper
import kotlin.math.roundToInt

open class EmulationActivity : AppCompatActivity() {
    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

    private lateinit var game: Game

    override fun onDestroy() {
        // TODO(bunnei): Disable notifications until we support app suspension.
        //stopService(foregroundService);
        super.onDestroy()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ThemeHelper.setTheme(this)

        super.onCreate(savedInstanceState)
        if (savedInstanceState == null) {
            // Get params we were passed
            game = intent.parcelable(EXTRA_SELECTED_GAME)!!
            isActivityRecreated = false
        } else {
            isActivityRecreated = true
            restoreState(savedInstanceState)
        }
        controllerMappingHelper = ControllerMappingHelper()

        // Set these options now so that the SurfaceView the game renders into is the right size.
        enableFullscreenImmersive()

        setContentView(R.layout.activity_emulation)

        // Find or create the EmulationFragment
        emulationFragment =
            supportFragmentManager.findFragmentById(R.id.frame_emulation_fragment) as EmulationFragment?
        if (emulationFragment == null) {
            emulationFragment = EmulationFragment.newInstance(game)
            supportFragmentManager.beginTransaction()
                .add(R.id.frame_emulation_fragment, emulationFragment!!)
                .commit()
        }
        title = game.title

        // 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);
    }

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putParcelable(EXTRA_SELECTED_GAME, game)
        super.onSaveInstanceState(outState)
    }

    private fun restoreState(savedInstanceState: Bundle) {
        game = savedInstanceState.parcelable(EXTRA_SELECTED_GAME)!!

        // If an alert prompt was in progress when state was restored, retry displaying it
        NativeLibrary.retryDisplayAlertPrompt()
    }

    private fun enableFullscreenImmersive() {
        window.attributes.layoutInDisplayCutoutMode =
            WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

        // It would be nice to use IMMERSIVE_STICKY, but that doesn't show the toolbar.
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_IMMERSIVE
    }

    private fun editControlsPlacement() {
        if (emulationFragment!!.isConfiguringControls) {
            emulationFragment!!.stopConfiguringControls()
        } else {
            emulationFragment!!.startConfiguringControls()
        }
    }

    private fun adjustScale() {
        val sliderBinding = DialogSliderBinding.inflate(layoutInflater)
        sliderBinding.slider.valueTo = 150F
        sliderBinding.slider.value = PreferenceManager.getDefaultSharedPreferences(applicationContext)
            .getInt(Settings.PREF_CONTROL_SCALE, 50).toFloat()
        sliderBinding.slider.addOnChangeListener(OnChangeListener { _, value, _ ->
            sliderBinding.textValue.text = value.toString()
            setControlScale(value.toInt())
        })
        sliderBinding.textValue.text = sliderBinding.slider.value.toString()
        sliderBinding.textUnits.text = "%"
        MaterialAlertDialogBuilder(this)
            .setTitle(R.string.emulation_control_scale)
            .setView(sliderBinding.root)
            .setNegativeButton(android.R.string.cancel, null)
            .setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int ->
                setControlScale(sliderBinding.slider.value.toInt())
            }
            .setNeutralButton(R.string.slider_default) { _: DialogInterface?, _: Int ->
                setControlScale(50)
            }
            .show()
    }

    private fun setControlScale(scale: Int) {
        PreferenceManager.getDefaultSharedPreferences(applicationContext).edit()
            .putInt(Settings.PREF_CONTROL_SCALE, scale)
            .apply()
        emulationFragment!!.refreshInputOverlay()
    }

    private fun resetOverlay() {
        MaterialAlertDialogBuilder(this)
            .setTitle(getString(R.string.emulation_touch_overlay_reset))
            .setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int -> emulationFragment!!.resetInputOverlay() }
            .setNegativeButton(android.R.string.cancel, null)
            .create()
            .show()
    }

    companion object {
        const val EXTRA_SELECTED_GAME = "SelectedGame"
        private const val EMULATION_RUNNING_NOTIFICATION = 0x1000

        @JvmStatic
        fun launch(activity: FragmentActivity, game: Game) {
            val launcher = Intent(activity, EmulationActivity::class.java)
            launcher.putExtra(EXTRA_SELECTED_GAME, game)
            activity.startActivity(launcher)
        }

        @JvmStatic
        fun tryDismissRunningNotification(activity: Activity?) {
            // TODO(bunnei): Disable notifications until we support app suspension.
            //NotificationManagerCompat.from(activity).cancel(EMULATION_RUNNING_NOTIFICATION);
        }

        private fun areCoordinatesOutside(view: View?, x: Float, y: Float): Boolean {
            if (view == null) {
                return true
            }
            val viewBounds = Rect()
            view.getGlobalVisibleRect(viewBounds)
            return !viewBounds.contains(x.roundToInt(), y.roundToInt())
        }
    }
}