Skip to content

Commit

Permalink
feat: Add onKeyDown handling for menu key press.
Browse files Browse the repository at this point in the history
Also moved overrides above functions
  • Loading branch information
HeCodes2Much committed May 1, 2024
1 parent 39170a8 commit 7d7cc3f
Showing 1 changed file with 81 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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())
}
}
}
}
}
}
}

0 comments on commit 7d7cc3f

Please sign in to comment.