diff options
| author | 2023-03-07 21:13:50 -0500 | |
|---|---|---|
| committer | 2023-06-03 00:05:37 -0700 | |
| commit | b10e13c090419fe2236cf02494d4c015dad07d87 (patch) | |
| tree | 7161e1fd40f87a2fb963c2bdf844e8671ba7a2b5 /src/android | |
| parent | android: Convert SubmenuViewHolder to Kotlin (diff) | |
| download | yuzu-b10e13c090419fe2236cf02494d4c015dad07d87.tar.gz yuzu-b10e13c090419fe2236cf02494d4c015dad07d87.tar.xz yuzu-b10e13c090419fe2236cf02494d4c015dad07d87.zip | |
android: Convert SettingsActivity to Kotlin
Diffstat (limited to 'src/android')
2 files changed, 186 insertions, 209 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.java deleted file mode 100644 index 0a1323a1f..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.java +++ /dev/null | |||
| @@ -1,209 +0,0 @@ | |||
| 1 | package org.yuzu.yuzu_emu.features.settings.ui; | ||
| 2 | |||
| 3 | import android.app.ProgressDialog; | ||
| 4 | import android.content.Context; | ||
| 5 | import android.content.Intent; | ||
| 6 | import android.content.IntentFilter; | ||
| 7 | import android.os.Bundle; | ||
| 8 | import android.provider.Settings; | ||
| 9 | import android.view.Menu; | ||
| 10 | import android.view.MenuInflater; | ||
| 11 | import android.widget.Toast; | ||
| 12 | |||
| 13 | import androidx.annotation.NonNull; | ||
| 14 | import androidx.appcompat.app.AppCompatActivity; | ||
| 15 | import androidx.fragment.app.FragmentTransaction; | ||
| 16 | import androidx.localbroadcastmanager.content.LocalBroadcastManager; | ||
| 17 | |||
| 18 | import org.yuzu.yuzu_emu.NativeLibrary; | ||
| 19 | import org.yuzu.yuzu_emu.R; | ||
| 20 | import org.yuzu.yuzu_emu.utils.DirectoryInitialization; | ||
| 21 | import org.yuzu.yuzu_emu.utils.DirectoryStateReceiver; | ||
| 22 | import org.yuzu.yuzu_emu.utils.EmulationMenuSettings; | ||
| 23 | |||
| 24 | public final class SettingsActivity extends AppCompatActivity implements SettingsActivityView { | ||
| 25 | private static final String ARG_MENU_TAG = "menu_tag"; | ||
| 26 | private static final String ARG_GAME_ID = "game_id"; | ||
| 27 | private static final String FRAGMENT_TAG = "settings"; | ||
| 28 | private SettingsActivityPresenter mPresenter = new SettingsActivityPresenter(this); | ||
| 29 | |||
| 30 | private ProgressDialog dialog; | ||
| 31 | |||
| 32 | public static void launch(Context context, String menuTag, String gameId) { | ||
| 33 | Intent settings = new Intent(context, SettingsActivity.class); | ||
| 34 | settings.putExtra(ARG_MENU_TAG, menuTag); | ||
| 35 | settings.putExtra(ARG_GAME_ID, gameId); | ||
| 36 | context.startActivity(settings); | ||
| 37 | } | ||
| 38 | |||
| 39 | @Override | ||
| 40 | protected void onCreate(Bundle savedInstanceState) { | ||
| 41 | super.onCreate(savedInstanceState); | ||
| 42 | |||
| 43 | setContentView(R.layout.activity_settings); | ||
| 44 | |||
| 45 | Intent launcher = getIntent(); | ||
| 46 | String gameID = launcher.getStringExtra(ARG_GAME_ID); | ||
| 47 | String menuTag = launcher.getStringExtra(ARG_MENU_TAG); | ||
| 48 | |||
| 49 | mPresenter.onCreate(savedInstanceState, menuTag, gameID); | ||
| 50 | |||
| 51 | // Show "Back" button in the action bar for navigation | ||
| 52 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||
| 53 | } | ||
| 54 | |||
| 55 | @Override | ||
| 56 | public boolean onSupportNavigateUp() { | ||
| 57 | onBackPressed(); | ||
| 58 | |||
| 59 | return true; | ||
| 60 | } | ||
| 61 | |||
| 62 | @Override | ||
| 63 | public boolean onCreateOptionsMenu(Menu menu) { | ||
| 64 | MenuInflater inflater = getMenuInflater(); | ||
| 65 | inflater.inflate(R.menu.menu_settings, menu); | ||
| 66 | |||
| 67 | return true; | ||
| 68 | } | ||
| 69 | |||
| 70 | @Override | ||
| 71 | protected void onSaveInstanceState(@NonNull Bundle outState) { | ||
| 72 | // Critical: If super method is not called, rotations will be busted. | ||
| 73 | super.onSaveInstanceState(outState); | ||
| 74 | mPresenter.saveState(outState); | ||
| 75 | } | ||
| 76 | |||
| 77 | @Override | ||
| 78 | protected void onStart() { | ||
| 79 | super.onStart(); | ||
| 80 | mPresenter.onStart(); | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * If this is called, the user has left the settings screen (potentially through the | ||
| 85 | * home button) and will expect their changes to be persisted. So we kick off an | ||
| 86 | * IntentService which will do so on a background thread. | ||
| 87 | */ | ||
| 88 | @Override | ||
| 89 | protected void onStop() { | ||
| 90 | super.onStop(); | ||
| 91 | |||
| 92 | mPresenter.onStop(isFinishing()); | ||
| 93 | |||
| 94 | // Update framebuffer layout when closing the settings | ||
| 95 | NativeLibrary.NotifyOrientationChange(EmulationMenuSettings.getLandscapeScreenLayout(), | ||
| 96 | getWindowManager().getDefaultDisplay().getRotation()); | ||
| 97 | } | ||
| 98 | |||
| 99 | @Override | ||
| 100 | public void showSettingsFragment(String menuTag, boolean addToStack, String gameID) { | ||
| 101 | if (!addToStack && getFragment() != null) { | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); | ||
| 106 | |||
| 107 | if (addToStack) { | ||
| 108 | if (areSystemAnimationsEnabled()) { | ||
| 109 | transaction.setCustomAnimations( | ||
| 110 | R.animator.settings_enter, | ||
| 111 | R.animator.settings_exit, | ||
| 112 | R.animator.settings_pop_enter, | ||
| 113 | R.animator.setttings_pop_exit); | ||
| 114 | } | ||
| 115 | |||
| 116 | transaction.addToBackStack(null); | ||
| 117 | } | ||
| 118 | transaction.replace(R.id.frame_content, SettingsFragment.newInstance(menuTag, gameID), FRAGMENT_TAG); | ||
| 119 | |||
| 120 | transaction.commit(); | ||
| 121 | } | ||
| 122 | |||
| 123 | private boolean areSystemAnimationsEnabled() { | ||
| 124 | float duration = Settings.Global.getFloat( | ||
| 125 | getContentResolver(), | ||
| 126 | Settings.Global.ANIMATOR_DURATION_SCALE, 1); | ||
| 127 | float transition = Settings.Global.getFloat( | ||
| 128 | getContentResolver(), | ||
| 129 | Settings.Global.TRANSITION_ANIMATION_SCALE, 1); | ||
| 130 | return duration != 0 && transition != 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | @Override | ||
| 134 | public void startDirectoryInitializationService(DirectoryStateReceiver receiver, IntentFilter filter) { | ||
| 135 | LocalBroadcastManager.getInstance(this).registerReceiver( | ||
| 136 | receiver, | ||
| 137 | filter); | ||
| 138 | DirectoryInitialization.start(this); | ||
| 139 | } | ||
| 140 | |||
| 141 | @Override | ||
| 142 | public void stopListeningToDirectoryInitializationService(DirectoryStateReceiver receiver) { | ||
| 143 | LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver); | ||
| 144 | } | ||
| 145 | |||
| 146 | @Override | ||
| 147 | public void showLoading() { | ||
| 148 | if (dialog == null) { | ||
| 149 | dialog = new ProgressDialog(this); | ||
| 150 | dialog.setMessage(getString(R.string.load_settings)); | ||
| 151 | dialog.setIndeterminate(true); | ||
| 152 | } | ||
| 153 | |||
| 154 | dialog.show(); | ||
| 155 | } | ||
| 156 | |||
| 157 | @Override | ||
| 158 | public void hideLoading() { | ||
| 159 | dialog.dismiss(); | ||
| 160 | } | ||
| 161 | |||
| 162 | @Override | ||
| 163 | public void showExternalStorageNotMountedHint() { | ||
| 164 | Toast.makeText(this, R.string.external_storage_not_mounted, Toast.LENGTH_SHORT) | ||
| 165 | .show(); | ||
| 166 | } | ||
| 167 | |||
| 168 | @Override | ||
| 169 | public org.yuzu.yuzu_emu.features.settings.model.Settings getSettings() { | ||
| 170 | return mPresenter.getSettings(); | ||
| 171 | } | ||
| 172 | |||
| 173 | @Override | ||
| 174 | public void setSettings(org.yuzu.yuzu_emu.features.settings.model.Settings settings) { | ||
| 175 | mPresenter.setSettings(settings); | ||
| 176 | } | ||
| 177 | |||
| 178 | @Override | ||
| 179 | public void onSettingsFileLoaded(org.yuzu.yuzu_emu.features.settings.model.Settings settings) { | ||
| 180 | SettingsFragmentView fragment = getFragment(); | ||
| 181 | |||
| 182 | if (fragment != null) { | ||
| 183 | fragment.onSettingsFileLoaded(settings); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | @Override | ||
| 188 | public void onSettingsFileNotFound() { | ||
| 189 | SettingsFragmentView fragment = getFragment(); | ||
| 190 | |||
| 191 | if (fragment != null) { | ||
| 192 | fragment.loadDefaultSettings(); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | @Override | ||
| 197 | public void showToastMessage(String message, boolean is_long) { | ||
| 198 | Toast.makeText(this, message, is_long ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show(); | ||
| 199 | } | ||
| 200 | |||
| 201 | @Override | ||
| 202 | public void onSettingChanged() { | ||
| 203 | mPresenter.onSettingChanged(); | ||
| 204 | } | ||
| 205 | |||
| 206 | private SettingsFragment getFragment() { | ||
| 207 | return (SettingsFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG); | ||
| 208 | } | ||
| 209 | } | ||
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt new file mode 100644 index 000000000..ed26ec52f --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | package org.yuzu.yuzu_emu.features.settings.ui | ||
| 2 | |||
| 3 | import android.app.ProgressDialog | ||
| 4 | import android.content.Context | ||
| 5 | import android.content.Intent | ||
| 6 | import android.content.IntentFilter | ||
| 7 | import android.os.Bundle | ||
| 8 | import android.view.Menu | ||
| 9 | import android.widget.Toast | ||
| 10 | import androidx.appcompat.app.AppCompatActivity | ||
| 11 | import androidx.localbroadcastmanager.content.LocalBroadcastManager | ||
| 12 | import org.yuzu.yuzu_emu.NativeLibrary | ||
| 13 | import org.yuzu.yuzu_emu.R | ||
| 14 | import org.yuzu.yuzu_emu.features.settings.model.Settings | ||
| 15 | import org.yuzu.yuzu_emu.features.settings.ui.SettingsFragment.Companion.newInstance | ||
| 16 | import org.yuzu.yuzu_emu.utils.DirectoryInitialization | ||
| 17 | import org.yuzu.yuzu_emu.utils.DirectoryStateReceiver | ||
| 18 | import org.yuzu.yuzu_emu.utils.EmulationMenuSettings | ||
| 19 | |||
| 20 | class SettingsActivity : AppCompatActivity(), SettingsActivityView { | ||
| 21 | private val presenter = SettingsActivityPresenter(this) | ||
| 22 | private var dialog: ProgressDialog? = null | ||
| 23 | |||
| 24 | override fun onCreate(savedInstanceState: Bundle?) { | ||
| 25 | super.onCreate(savedInstanceState) | ||
| 26 | setContentView(R.layout.activity_settings) | ||
| 27 | val launcher = intent | ||
| 28 | val gameID = launcher.getStringExtra(ARG_GAME_ID) | ||
| 29 | val menuTag = launcher.getStringExtra(ARG_MENU_TAG) | ||
| 30 | presenter.onCreate(savedInstanceState, menuTag!!, gameID!!) | ||
| 31 | |||
| 32 | // Show "Back" button in the action bar for navigation | ||
| 33 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) | ||
| 34 | } | ||
| 35 | |||
| 36 | override fun onSupportNavigateUp(): Boolean { | ||
| 37 | onBackPressed() | ||
| 38 | return true | ||
| 39 | } | ||
| 40 | |||
| 41 | override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
| 42 | val inflater = menuInflater | ||
| 43 | inflater.inflate(R.menu.menu_settings, menu) | ||
| 44 | return true | ||
| 45 | } | ||
| 46 | |||
| 47 | override fun onSaveInstanceState(outState: Bundle) { | ||
| 48 | // Critical: If super method is not called, rotations will be busted. | ||
| 49 | super.onSaveInstanceState(outState) | ||
| 50 | presenter.saveState(outState) | ||
| 51 | } | ||
| 52 | |||
| 53 | override fun onStart() { | ||
| 54 | super.onStart() | ||
| 55 | presenter.onStart() | ||
| 56 | } | ||
| 57 | |||
| 58 | /** | ||
| 59 | * If this is called, the user has left the settings screen (potentially through the | ||
| 60 | * home button) and will expect their changes to be persisted. So we kick off an | ||
| 61 | * IntentService which will do so on a background thread. | ||
| 62 | */ | ||
| 63 | override fun onStop() { | ||
| 64 | super.onStop() | ||
| 65 | presenter.onStop(isFinishing) | ||
| 66 | |||
| 67 | // Update framebuffer layout when closing the settings | ||
| 68 | NativeLibrary.NotifyOrientationChange( | ||
| 69 | EmulationMenuSettings.landscapeScreenLayout, | ||
| 70 | windowManager.defaultDisplay.rotation | ||
| 71 | ) | ||
| 72 | } | ||
| 73 | |||
| 74 | override fun showSettingsFragment(menuTag: String, addToStack: Boolean, gameId: String) { | ||
| 75 | if (!addToStack && fragment != null) { | ||
| 76 | return | ||
| 77 | } | ||
| 78 | val transaction = supportFragmentManager.beginTransaction() | ||
| 79 | if (addToStack) { | ||
| 80 | if (areSystemAnimationsEnabled()) { | ||
| 81 | transaction.setCustomAnimations( | ||
| 82 | R.animator.settings_enter, | ||
| 83 | R.animator.settings_exit, | ||
| 84 | R.animator.settings_pop_enter, | ||
| 85 | R.animator.setttings_pop_exit | ||
| 86 | ) | ||
| 87 | } | ||
| 88 | transaction.addToBackStack(null) | ||
| 89 | } | ||
| 90 | transaction.replace(R.id.frame_content, newInstance(menuTag, gameId), FRAGMENT_TAG) | ||
| 91 | transaction.commit() | ||
| 92 | } | ||
| 93 | |||
| 94 | private fun areSystemAnimationsEnabled(): Boolean { | ||
| 95 | val duration = android.provider.Settings.Global.getFloat( | ||
| 96 | contentResolver, | ||
| 97 | android.provider.Settings.Global.ANIMATOR_DURATION_SCALE, 1f | ||
| 98 | ) | ||
| 99 | val transition = android.provider.Settings.Global.getFloat( | ||
| 100 | contentResolver, | ||
| 101 | android.provider.Settings.Global.TRANSITION_ANIMATION_SCALE, 1f | ||
| 102 | ) | ||
| 103 | return duration != 0f && transition != 0f | ||
| 104 | } | ||
| 105 | |||
| 106 | override fun startDirectoryInitializationService( | ||
| 107 | receiver: DirectoryStateReceiver?, | ||
| 108 | filter: IntentFilter | ||
| 109 | ) { | ||
| 110 | LocalBroadcastManager.getInstance(this).registerReceiver( | ||
| 111 | receiver!!, | ||
| 112 | filter | ||
| 113 | ) | ||
| 114 | DirectoryInitialization.start(this) | ||
| 115 | } | ||
| 116 | |||
| 117 | override fun stopListeningToDirectoryInitializationService(receiver: DirectoryStateReceiver) { | ||
| 118 | LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver) | ||
| 119 | } | ||
| 120 | |||
| 121 | override fun showLoading() { | ||
| 122 | if (dialog == null) { | ||
| 123 | dialog = ProgressDialog(this) | ||
| 124 | dialog!!.setMessage(getString(R.string.load_settings)) | ||
| 125 | dialog!!.isIndeterminate = true | ||
| 126 | } | ||
| 127 | dialog!!.show() | ||
| 128 | } | ||
| 129 | |||
| 130 | override fun hideLoading() { | ||
| 131 | dialog!!.dismiss() | ||
| 132 | } | ||
| 133 | |||
| 134 | override fun showExternalStorageNotMountedHint() { | ||
| 135 | Toast.makeText( | ||
| 136 | this, | ||
| 137 | R.string.external_storage_not_mounted, | ||
| 138 | Toast.LENGTH_SHORT | ||
| 139 | ).show() | ||
| 140 | } | ||
| 141 | |||
| 142 | override var settings: Settings? | ||
| 143 | get() = presenter.settings | ||
| 144 | set(settings) { | ||
| 145 | presenter.settings = settings | ||
| 146 | } | ||
| 147 | |||
| 148 | override fun onSettingsFileLoaded(settings: Settings?) { | ||
| 149 | val fragment: SettingsFragmentView? = fragment | ||
| 150 | fragment?.onSettingsFileLoaded(settings) | ||
| 151 | } | ||
| 152 | |||
| 153 | override fun onSettingsFileNotFound() { | ||
| 154 | val fragment: SettingsFragmentView? = fragment | ||
| 155 | fragment?.loadDefaultSettings() | ||
| 156 | } | ||
| 157 | |||
| 158 | override fun showToastMessage(message: String, is_long: Boolean) { | ||
| 159 | Toast.makeText( | ||
| 160 | this, | ||
| 161 | message, | ||
| 162 | if (is_long) Toast.LENGTH_LONG else Toast.LENGTH_SHORT | ||
| 163 | ).show() | ||
| 164 | } | ||
| 165 | |||
| 166 | override fun onSettingChanged() { | ||
| 167 | presenter.onSettingChanged() | ||
| 168 | } | ||
| 169 | |||
| 170 | private val fragment: SettingsFragment? | ||
| 171 | get() = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as SettingsFragment? | ||
| 172 | |||
| 173 | companion object { | ||
| 174 | private const val ARG_MENU_TAG = "menu_tag" | ||
| 175 | private const val ARG_GAME_ID = "game_id" | ||
| 176 | private const val FRAGMENT_TAG = "settings" | ||
| 177 | |||
| 178 | @JvmStatic | ||
| 179 | fun launch(context: Context, menuTag: String?, gameId: String?) { | ||
| 180 | val settings = Intent(context, SettingsActivity::class.java) | ||
| 181 | settings.putExtra(ARG_MENU_TAG, menuTag) | ||
| 182 | settings.putExtra(ARG_GAME_ID, gameId) | ||
| 183 | context.startActivity(settings) | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||