summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt
blob: c5259a13d87fb0a3ca4b32f7d23060413a31a16f (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.ui.main

import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.yuzu.yuzu_emu.NativeLibrary
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.activities.EmulationActivity
import org.yuzu.yuzu_emu.databinding.ActivityMainBinding
import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivity
import org.yuzu.yuzu_emu.model.GameProvider
import org.yuzu.yuzu_emu.ui.platform.PlatformGamesFragment
import org.yuzu.yuzu_emu.utils.*

class MainActivity : AppCompatActivity(), MainView {
    private var platformGamesFragment: PlatformGamesFragment? = null
    private val presenter = MainPresenter(this)

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        val splashScreen = installSplashScreen()
        splashScreen.setKeepOnScreenCondition { !DirectoryInitialization.areDirectoriesReady() }

        ThemeHelper.setTheme(this)

        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        setSupportActionBar(binding.toolbarMain)
        presenter.onCreate()
        if (savedInstanceState == null) {
            StartupHandler.handleInit(this)
            platformGamesFragment = PlatformGamesFragment()
            supportFragmentManager.beginTransaction()
                .add(R.id.games_platform_frame, platformGamesFragment!!)
                .commit()
        } else {
            platformGamesFragment = supportFragmentManager.getFragment(
                savedInstanceState,
                PlatformGamesFragment.TAG
            ) as PlatformGamesFragment?
        }

        // Dismiss previous notifications (should not happen unless a crash occurred)
        EmulationActivity.tryDismissRunningNotification(this)

        setInsets()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        supportFragmentManager.putFragment(
            outState,
            PlatformGamesFragment.TAG,
            platformGamesFragment!!
        )
    }

    override fun onResume() {
        super.onResume()
        presenter.addDirIfNeeded(AddDirectoryHelper(this))
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_game_grid, menu)
        return true
    }

    /**
     * MainView
     */
    override fun setVersionString(version: String) {
        binding.toolbarMain.subtitle = version
    }

    override fun refresh() {
        contentResolver.insert(GameProvider.URI_REFRESH, null)
        refreshFragment()
    }

    override fun launchSettingsActivity(menuTag: String) {
        SettingsActivity.launch(this, menuTag, "")
    }

    override fun launchFileListActivity(request: Int) {
        when (request) {
            MainPresenter.REQUEST_ADD_DIRECTORY -> FileBrowserHelper.openDirectoryPicker(
                this,
                MainPresenter.REQUEST_ADD_DIRECTORY,
                R.string.select_game_folder
            )
            MainPresenter.REQUEST_INSTALL_KEYS -> FileBrowserHelper.openFilePicker(
                this,
                MainPresenter.REQUEST_INSTALL_KEYS,
                R.string.install_keys
            )
            MainPresenter.REQUEST_SELECT_GPU_DRIVER -> {
                // Get the driver name for the dialog message.
                var driverName = GpuDriverHelper.customDriverName
                if (driverName == null) {
                    driverName = getString(R.string.system_gpu_driver)
                }

                MaterialAlertDialogBuilder(this)
                    .setTitle(getString(R.string.select_gpu_driver_title))
                    .setMessage(driverName)
                    .setNegativeButton(android.R.string.cancel, null)
                    .setPositiveButton(R.string.select_gpu_driver_default) { _: DialogInterface?, _: Int ->
                        GpuDriverHelper.installDefaultDriver(this)
                        Toast.makeText(
                            this,
                            R.string.select_gpu_driver_use_default,
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                    .setNeutralButton(R.string.select_gpu_driver_install) { _: DialogInterface?, _: Int ->
                        FileBrowserHelper.openFilePicker(
                            this,
                            MainPresenter.REQUEST_SELECT_GPU_DRIVER,
                            R.string.select_gpu_driver
                        )
                    }
                    .show()
            }
        }
    }

    /**
     * @param requestCode An int describing whether the Activity that is returning did so successfully.
     * @param resultCode  An int describing what Activity is giving us this callback.
     * @param result      The information the returning Activity is providing us.
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, result: Intent?) {
        super.onActivityResult(requestCode, resultCode, result)
        when (requestCode) {
            MainPresenter.REQUEST_ADD_DIRECTORY -> if (resultCode == RESULT_OK) {
                val takeFlags =
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
                contentResolver.takePersistableUriPermission(
                    Uri.parse(result!!.dataString),
                    takeFlags
                )
                // When a new directory is picked, we currently will reset the existing games
                // database. This effectively means that only one game directory is supported.
                // TODO(bunnei): Consider fixing this in the future, or removing code for this.
                contentResolver.insert(GameProvider.URI_RESET, null)
                // Add the new directory
                presenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result))
            }
            MainPresenter.REQUEST_INSTALL_KEYS -> if (resultCode == RESULT_OK) {
                val takeFlags =
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
                contentResolver.takePersistableUriPermission(
                    Uri.parse(result!!.dataString),
                    takeFlags
                )
                val dstPath = DirectoryInitialization.userDirectory + "/keys/"
                if (FileUtil.copyUriToInternalStorage(this, result.data, dstPath, "prod.keys")) {
                    if (NativeLibrary.ReloadKeys()) {
                        Toast.makeText(
                            this,
                            R.string.install_keys_success,
                            Toast.LENGTH_SHORT
                        ).show()
                        refreshFragment()
                    } else {
                        Toast.makeText(
                            this,
                            R.string.install_keys_failure,
                            Toast.LENGTH_LONG
                        ).show()
                        launchFileListActivity(MainPresenter.REQUEST_INSTALL_KEYS)
                    }
                }
            }
            MainPresenter.REQUEST_SELECT_GPU_DRIVER -> if (resultCode == RESULT_OK) {
                val takeFlags =
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
                contentResolver.takePersistableUriPermission(
                    Uri.parse(result!!.dataString),
                    takeFlags
                )
                GpuDriverHelper.installCustomDriver(this, result.data)
                val driverName = GpuDriverHelper.customDriverName
                if (driverName != null) {
                    Toast.makeText(
                        this,
                        getString(R.string.select_gpu_driver_install_success, driverName),
                        Toast.LENGTH_SHORT
                    ).show()
                } else {
                    Toast.makeText(
                        this,
                        R.string.select_gpu_driver_error,
                        Toast.LENGTH_LONG
                    ).show()
                }
            }
        }
    }

    /**
     * Called by the framework whenever any actionbar/toolbar icon is clicked.
     *
     * @param item The icon that was clicked on.
     * @return True if the event was handled, false to bubble it up to the OS.
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return presenter.handleOptionSelection(item.itemId)
    }

    private fun refreshFragment() {
        if (platformGamesFragment != null) {
            NativeLibrary.ResetRomMetadata()
            platformGamesFragment!!.refresh()
        }
    }

    override fun onDestroy() {
        EmulationActivity.tryDismissRunningNotification(this)
        super.onDestroy()
    }

    private fun setInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.gamesPlatformFrame) { view: View, windowInsets: WindowInsetsCompat ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
            view.updatePadding(left = insets.left, right = insets.right)
            InsetsHelper.insetAppBar(insets, binding.appbarMain)
            windowInsets
        }
    }
}