forked from angelsl/Wabbitemu-Android
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWabbitemuActivity.kt
More file actions
427 lines (396 loc) · 16.5 KB
/
Copy pathWabbitemuActivity.kt
File metadata and controls
427 lines (396 loc) · 16.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package io.github.angelsl.wabbitemu.activity
import android.annotation.TargetApi
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.View.OnSystemUiVisibilityChangeListener
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.drawerlayout.widget.DrawerLayout
import androidx.drawerlayout.widget.DrawerLayout.DrawerListener
import androidx.preference.PreferenceManager
import io.github.angelsl.wabbitemu.R
import io.github.angelsl.wabbitemu.SkinBitmapLoader
import io.github.angelsl.wabbitemu.calc.CalcModel
import io.github.angelsl.wabbitemu.calc.CalculatorManager
import io.github.angelsl.wabbitemu.fragment.EmulatorButtonsFragment
import io.github.angelsl.wabbitemu.fragment.EmulatorFragment
import io.github.angelsl.wabbitemu.utils.*
import java.io.File
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.text.SimpleDateFormat
import java.util.*
class WabbitemuActivity : AppCompatActivity() {
private enum class MainMenuItem(private val mPosition: Int) {
LOAD_FILE_MENU_ITEM(0),
WIZARD_MENU_ITEM(1),
RESET_MENU_ITEM(2),
SCREENSHOT_MENU_ITEM(3),
SETTINGS_MENU_ITEM(
4
),
ABOUT_MENU_ITEM(5);
companion object {
fun fromPosition(position: Int): MainMenuItem? {
return values().find { it.mPosition == position } //TODO just use index?
}
}
}
private val mCalcManager = CalculatorManager.getInstance()
private val mSkinLoader = SkinBitmapLoader.getInstance()
private val mVisibilityListener = VisibilityChangeListener()
private val mSharedPrefs by lazy { PreferenceManager.getDefaultSharedPreferences(this) }
private var mEmulatorFragment: EmulatorFragment? = null
private lateinit var mDrawerLayout: DrawerLayout
private lateinit var mDrawerList: ListView
private var mWasUserLaunched = false
private fun handleFile(f: File, runnable: Runnable) {
// mEmulatorFragment!!.handleFile(f, runnable)
}
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
HttpURLConnection.setFollowRedirects(true)
workaroundAsyncTaskIssue()
if (!testNativeLibraryLoad()) {
ErrorUtils.showErrorDialog(
this,
R.string.error_failed_load_native_lib,
FinishActivityClickListener()
)
return
}
sBestCacheDir = findBestCacheDir()
mCalcManager.initialize(this, sBestCacheDir)
mSkinLoader.initialize(this)
val fileName = lastRomSetting
if (fileName != null) {
val file = File(fileName)
Log.d("Wabbitemu", "onCreate: Loading rom file.")
mCalcManager.loadRomFile(file) { errorCode ->
if (errorCode != 0) {
Log.e(
"Wabbitemu",
String.format(
"Loading last ROM '%s' failed with error code %d",
fileName,
errorCode
)
)
// launchRunnable.run()
// Toast.makeText(this, "Unable to load ROM; error code $errorCode", Toast.LENGTH_LONG).show()
}
}
}
setTheme(R.style.Wabbitemu)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
setContentView(R.layout.main)
mEmulatorFragment = supportFragmentManager.findFragmentById(R.id.content_frame) as EmulatorFragment
attachMenu()
Log.d("Wabbitemu", "onCreate: isFirstRun: $isFirstRun, lastRomModel=$lastRomModel")
if (isFirstRun) {
mWasUserLaunched = false
val wizardIntent = Intent(this, WizardActivity::class.java)
startActivityForResult(wizardIntent, SETUP_WIZARD)
return
}
// we expect an absolute filename
val lastRomModel = CalcModel.fromModel(lastRomModel)
if (lastRomModel != CalcModel.NO_CALC) {
mSkinLoader.loadSkinAndKeymap(lastRomModel)
} else if (fileName == null || fileName == "") {
// runnable.run()
}
}
private fun findBestCacheDir(): String? {
val cacheDir = applicationContext.cacheDir
if (cacheDir != null) {
return cacheDir.absolutePath
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
for (file in applicationContext.externalCacheDirs) {
if (file != null) {
return file.absolutePath
}
}
}
return null
}
private fun workaroundAsyncTaskIssue() {
try {
Class.forName("android.os.AsyncTask")
} catch (ignore: Throwable) {
// ignored
}
}
private fun testNativeLibraryLoad(): Boolean {
try {
mCalcManager.testLoadLib()
} catch (ex: UnsatisfiedLinkError) {
return false
}
return true
}
public override fun onResume() {
super.onResume()
if (mSharedPrefs.getBoolean(PreferenceConstants.IMMERSIVE_MODE.toString(), true)) {
window.decorView.setOnSystemUiVisibilityChangeListener(mVisibilityListener)
setImmersiveMode(true)
}
}
override fun onPause() {
super.onPause()
if (mSharedPrefs.getBoolean(PreferenceConstants.IMMERSIVE_MODE.toString(), true)) {
setImmersiveMode(false)
window.decorView.setOnSystemUiVisibilityChangeListener(null)
}
}
private fun attachMenu() {
mDrawerLayout = findViewById(R.id.drawer_layout)
mDrawerList = findViewById(R.id.left_drawer)
val menuItems = resources.getStringArray(R.array.menu_array)
mDrawerList.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, menuItems)
mDrawerLayout.setScrimColor(Color.parseColor("#DD000000"))
mDrawerList.setOnItemClickListener { parent, view, position, id ->
handleMenuItem(
MainMenuItem.fromPosition(position)
)
}
mDrawerLayout.addDrawerListener(object : DrawerListener {
override fun onDrawerStateChanged(arg0: Int) {
// no-op
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
mDrawerLayout.bringChildToFront(drawerView)
mDrawerLayout.requestLayout()
}
override fun onDrawerOpened(arg0: View) {}
override fun onDrawerClosed(arg0: View) {}
})
}
private val launchRunnable: Runnable
get() = Runnable {
val handler = Handler(Looper.getMainLooper())
handler.post { ErrorUtils.showErrorDialog(this@WabbitemuActivity, R.string.errorLink) }
val wizardIntent = Intent(this@WabbitemuActivity, WizardActivity::class.java)
startActivityForResult(wizardIntent, SETUP_WIZARD)
}
private val lastRomSetting: String?
get() = mSharedPrefs.getString(PreferenceConstants.ROM_PATH.toString(), null)
private val lastRomModel: Int
get() = mSharedPrefs.getInt(PreferenceConstants.ROM_MODEL.toString(), -1)
private val isFirstRun: Boolean
get() = mSharedPrefs.getBoolean(PreferenceConstants.FIRST_RUN.toString(), true)
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
LOAD_FILE_CODE -> if (resultCode == RESULT_OK) {
val fileName = data?.getStringExtra(IntentConstants.FILENAME_EXTRA_STRING)
handleFile(File(fileName)) {
ErrorUtils.showErrorDialog(
this@WabbitemuActivity,
R.string.errorLink
)
}
}
SETUP_WIZARD -> if (resultCode == RESULT_OK) {
val fileName = data?.getStringExtra(IntentConstants.FILENAME_EXTRA_STRING)
handleFile(File(fileName), launchRunnable)
if (isFirstRun) {
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
val editor = sharedPrefs.edit()
editor.putBoolean(PreferenceConstants.FIRST_RUN.toString(), false)
editor.apply()
mDrawerLayout.openDrawer(mDrawerList)
}
} else if (!mWasUserLaunched) {
finish()
}
}
}
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList)
} else {
mDrawerLayout.openDrawer(mDrawerList)
}
return false
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val position = when (item.itemId) {
R.id.aboutMenuItem -> MainMenuItem.ABOUT_MENU_ITEM
R.id.settingsMenuItem -> MainMenuItem.SETTINGS_MENU_ITEM
R.id.resetMenuItem -> MainMenuItem.RESET_MENU_ITEM
R.id.rerunWizardMenuItem -> MainMenuItem.WIZARD_MENU_ITEM
R.id.loadFileMenuItem -> MainMenuItem.LOAD_FILE_MENU_ITEM
else -> return super.onOptionsItemSelected(item)
}
return handleMenuItem(position)
}
private fun handleMenuItem(position: MainMenuItem?): Boolean {
mDrawerLayout.closeDrawer(mDrawerList)
return when (position) {
MainMenuItem.SETTINGS_MENU_ITEM -> {
launchSettings()
true
}
MainMenuItem.RESET_MENU_ITEM -> {
resetCalc()
true
}
MainMenuItem.SCREENSHOT_MENU_ITEM -> {
screenshotCalc()
true
}
MainMenuItem.WIZARD_MENU_ITEM -> {
launchWizard()
true
}
MainMenuItem.LOAD_FILE_MENU_ITEM -> {
launchBrowse()
true
}
MainMenuItem.ABOUT_MENU_ITEM -> {
launchAbout()
true
}
else -> throw IllegalStateException("Invalid menu item")
}
}
private fun screenshotCalc() {
val screenshot: Bitmap? = null//mEmulatorFragment!!.screenshot
if (screenshot == null) {
ErrorUtils.showErrorDialog(this, R.string.errorScreenshot)
return
}
val scaledScreenshot = Bitmap.createScaledBitmap(
screenshot, screenshot.width * 2,
screenshot.height * 2, true
)
val outputDir: File
val outputFile: File
if (StorageUtils.hasExternalStorage()) {
outputDir = File(File(StorageUtils.getPrimaryStoragePath(), "Wabbitemu"), "Screenshots")
if (!outputDir.exists()) {
outputDir.mkdirs()
}
val sdf = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
val now = sdf.format(Date())
val fileName = "screenshot$now.png"
outputFile = File(outputDir, fileName)
} else {
ErrorUtils.showErrorDialog(this, R.string.errorMissingSdCard)
return
}
try {
val out = FileOutputStream(outputFile)
scaledScreenshot.compress(Bitmap.CompressFormat.PNG, 100, out)
out.close()
} catch (e: Exception) {
ErrorUtils.showErrorDialog(this, R.string.errorScreenshot)
return
}
val formatString = resources.getString(R.string.screenshotSuccess)
val successString = String.format(formatString, outputFile)
val toast = Toast.makeText(this, successString, Toast.LENGTH_LONG)
toast.show()
}
private fun launchAbout() {
val aboutIntent = Intent(this, AboutActivity::class.java)
startActivity(aboutIntent)
}
private fun launchBrowse() {
startActivity(Intent(this, ChooseFileActivity::class.java))
/*
val setupIntent = Intent(this, BrowseActivity::class.java)
// not perfect but it will work well enough
val extensions = when (mCalcManager.model) {
CalcModel.TI_73 -> "\\.(rom|sav|73[b|c|d|g|i|k|l|m|n|p|q|s|t|u|v|w|y|z])$"
CalcModel.TI_82 -> "\\.(rom|sav|82[b|c|d|g|i|l|m|n|p|q|s|t|u|v|w|y|z])$"
CalcModel.TI_83 -> "\\.(rom|sav|83[b|c|d|g|i|l|m|n|p|q|s|t|u|v|w|y|z])$"
CalcModel.TI_83P,
CalcModel.TI_83PSE,
CalcModel.TI_84P,
CalcModel.TI_84PSE -> "\\.(rom|sav|8x[b|c|d|g|i|k|l|m|n|p|q|s|t|u|v|w|y|z])$"
CalcModel.TI_84PCSE -> "\\.(rom|sav|8[x|c][b|c|d|g|i|k|l|m|n|p|q|s|t|u|v|w|y|z])$"
CalcModel.TI_85 -> "\\.(rom|sav|85[b|c|d|g|i|l|m|n|p|q|s|t|u|v|w|y|z])$"
CalcModel.TI_86 -> "\\.(rom|sav|86[b|c|d|g|i|l|m|n|p|q|s|t|u|v|w|y|z])$"
else -> DEFAULT_FILE_REGEX
}
val description = resources.getString(R.string.browseFileDescription)
setupIntent.putExtra(IntentConstants.EXTENSION_EXTRA_REGEX, extensions)
setupIntent.putExtra(IntentConstants.BROWSE_DESCRIPTION_EXTRA_STRING, description)
startActivityForResult(setupIntent, LOAD_FILE_CODE)*/
}
private fun launchWizard() {
mWasUserLaunched = true
val wizardIntent = Intent(this, WizardActivity::class.java)
startActivityForResult(wizardIntent, SETUP_WIZARD)
}
private fun resetCalc() {
// mEmulatorFragment!!.resetCalc()
}
private fun launchSettings() {
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
@TargetApi(Build.VERSION_CODES.KITKAT)
fun setImmersiveMode(isImmersive: Boolean) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return
}
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
val decorView = window.decorView
val uiOptions = decorView.systemUiVisibility
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments
// the behavior of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes
// of this sample all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is
// referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and
// status bars semi-transparent, and the UI flag does not get cleared
// when the user interacts with the screen.
decorView.systemUiVisibility = if (isImmersive) {
uiOptions or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
} else {
uiOptions and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.inv() and View.SYSTEM_UI_FLAG_FULLSCREEN.inv() and View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY.inv()
}
}
private inner class VisibilityChangeListener : OnSystemUiVisibilityChangeListener {
@Deprecated("Deprecated in Java", ReplaceWith("setImmersiveMode(true)"))
override fun onSystemUiVisibilityChange(visibility: Int) {
// If someone tries to to change the visibility after have set it,
// we basically want to ignore it. Usually this is caused by
// something like the loading dialog.
setImmersiveMode(true)
}
}
private inner class FinishActivityClickListener : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface, which: Int) {
dialog.dismiss()
finish()
}
}
companion object {
private const val LOAD_FILE_CODE = 1
private const val SETUP_WIZARD = 2
private const val DEFAULT_FILE_REGEX =
"\\.(rom|sav|[7|8][2|3|x|c|5|6][b|c|d|g|i|k|l|m|n|p|q|s|t|u|v|w|y|z])$"
@JvmField
var sBestCacheDir: String? = null
}
}