diff --git a/app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt b/app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt index 378382f2b..cd365e23a 100644 --- a/app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt +++ b/app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt @@ -11,6 +11,7 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import android.provider.Settings +import android.view.KeyEvent import android.view.View import android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS import androidx.appcompat.app.AppCompatActivity @@ -45,6 +46,27 @@ class MainActivity : AppCompatActivity() { super.onBackPressed() } + override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { + return when (keyCode) { + KeyEvent.KEYCODE_MENU -> { + when (navController.currentDestination?.id) { + R.id.mainFragment -> { + Navigation.findNavController(this, R.id.nav_host_fragment) + .navigate(R.id.action_mainFragment_to_appListFragment) + true + } + + else -> { + false + } + } + } + else -> { + super.onKeyDown(keyCode, event) + } + } + } + override fun onCreate(savedInstanceState: Bundle?) { prefs = Prefs(this) val themeMode = when (prefs.appTheme) { @@ -95,12 +117,57 @@ class MainActivity : AppCompatActivity() { } } - @Suppress("DEPRECATION") - private fun setLanguage() { - val locale = prefs.language.locale() - val config = resources.configuration - config.locale = locale - resources.updateConfiguration(config, resources.displayMetrics) + @Deprecated("Deprecated in Java") + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + @Suppress("DEPRECATION") + super.onActivityResult(requestCode, resultCode, data) + + if (resultCode != Activity.RESULT_OK) { + // showToastLong(applicationContext, "Intent Error") + return + } + + when (requestCode) { + Constants.REQUEST_CODE_ENABLE_ADMIN -> { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) + showMessage(getString(R.string.double_tap_lock_is_enabled_message)) + else + showMessage(getString(R.string.double_tap_lock_uninstall_message)) + } + + Constants.BACKUP_READ -> { + data?.data?.also { uri -> + applicationContext.contentResolver.openInputStream(uri).use { inputStream -> + val stringBuilder = StringBuilder() + BufferedReader(InputStreamReader(inputStream)).use { reader -> + var line: String? = reader.readLine() + while (line != null) { + stringBuilder.append(line) + line = reader.readLine() + } + } + + val string = stringBuilder.toString() + val prefs = Prefs(applicationContext) + prefs.clear() + prefs.loadFromString(string) + } + } + startActivity(Intent.makeRestartActivityTask(this.intent?.component)) + } + + Constants.BACKUP_WRITE -> { + data?.data?.also { uri -> + applicationContext.contentResolver.openFileDescriptor(uri, "w")?.use { file -> + FileOutputStream(file.fileDescriptor).use { stream -> + val text = Prefs(applicationContext).saveToString() + stream.channel.truncate(0) + stream.write(text.toByteArray()) + } + } + } + } + } } override fun onStop() { @@ -123,6 +190,14 @@ class MainActivity : AppCompatActivity() { recreate() } + @Suppress("DEPRECATION") + private fun setLanguage() { + val locale = prefs.language.locale() + val config = resources.configuration + config.locale = locale + resources.updateConfiguration(config, resources.displayMetrics) + } + private fun initClickListeners() { binding.okay.setOnClickListener { binding.messageLayout.visibility = View.GONE @@ -173,57 +248,4 @@ class MainActivity : AppCompatActivity() { binding.messageTextView.text = message binding.messageLayout.visibility = View.VISIBLE } - - @Deprecated("Deprecated in Java") - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - @Suppress("DEPRECATION") - super.onActivityResult(requestCode, resultCode, data) - - if (resultCode != Activity.RESULT_OK) { - // showToastLong(applicationContext, "Intent Error") - return - } - - when (requestCode) { - Constants.REQUEST_CODE_ENABLE_ADMIN -> { - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) - showMessage(getString(R.string.double_tap_lock_is_enabled_message)) - else - showMessage(getString(R.string.double_tap_lock_uninstall_message)) - } - - Constants.BACKUP_READ -> { - data?.data?.also { uri -> - applicationContext.contentResolver.openInputStream(uri).use { inputStream -> - val stringBuilder = StringBuilder() - BufferedReader(InputStreamReader(inputStream)).use { reader -> - var line: String? = reader.readLine() - while (line != null) { - stringBuilder.append(line) - line = reader.readLine() - } - } - - val string = stringBuilder.toString() - val prefs = Prefs(applicationContext) - prefs.clear() - prefs.loadFromString(string) - } - } - startActivity(Intent.makeRestartActivityTask(this.intent?.component)) - } - - Constants.BACKUP_WRITE -> { - data?.data?.also { uri -> - applicationContext.contentResolver.openFileDescriptor(uri, "w")?.use { file -> - FileOutputStream(file.fileDescriptor).use { stream -> - val text = Prefs(applicationContext).saveToString() - stream.channel.truncate(0) - stream.write(text.toByteArray()) - } - } - } - } - } - } }