BaseActivity.kt 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package io.r_a_d.radio2
  2. import android.content.Intent
  3. import android.os.Bundle
  4. import android.util.Log
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.view.ViewTreeObserver
  8. import androidx.appcompat.app.AppCompatActivity
  9. import androidx.localbroadcastmanager.content.LocalBroadcastManager
  10. import androidx.preference.PreferenceManager
  11. import com.google.android.material.bottomnavigation.BottomNavigationView
  12. abstract class BaseActivity : AppCompatActivity() {
  13. private val keyboardLayoutListener : ViewTreeObserver.OnGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
  14. val viewHeight = (rootLayout?.rootView?.height ?: 0)
  15. val viewWidth = (rootLayout?.rootView?.width ?: 0)
  16. val height = ((rootLayout?.height ?: 0))
  17. val width = ((rootLayout?.width ?: 0))
  18. Log.d(tag, "$viewWidth, $viewHeight, $width, $height, ${viewHeight.toDouble()/viewWidth.toDouble()}, ${height.toDouble()/width.toDouble()}")
  19. val broadcastManager = LocalBroadcastManager.getInstance(this@BaseActivity)
  20. if(height <= viewHeight * 2 / 3 /*height.toDouble()/width.toDouble() < 1.20 */){
  21. val keyboardHeight = viewHeight - height
  22. onShowKeyboard(keyboardHeight)
  23. val intent = Intent("KeyboardWillShow")
  24. intent.putExtra("KeyboardHeight", keyboardHeight)
  25. broadcastManager.sendBroadcast(intent)
  26. } else {
  27. onHideKeyboard()
  28. val intent = Intent("KeyboardWillHide")
  29. broadcastManager.sendBroadcast(intent)
  30. }
  31. }
  32. private var keyboardListenersAttached = false
  33. private var rootLayout: ViewGroup? = null
  34. // keyboard stuff
  35. private fun onShowKeyboard(keyboardHeight: Int) {
  36. // do things when keyboard is shown
  37. val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
  38. bottomNavigationView.visibility = View.GONE
  39. Log.d(tag, "bottomNav visibility set to GONE (height $keyboardHeight)")
  40. }
  41. private fun onHideKeyboard() {
  42. // do things when keyboard is hidden
  43. val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
  44. bottomNavigationView.visibility = View.VISIBLE
  45. Log.d(tag, "bottomNav visibility set to VISIBLE")
  46. }
  47. protected fun attachKeyboardListeners() {
  48. if (keyboardListenersAttached) {
  49. return
  50. }
  51. rootLayout = findViewById(R.id.rootLayout)
  52. rootLayout!!.viewTreeObserver.addOnGlobalLayoutListener(keyboardLayoutListener)
  53. keyboardListenersAttached = true
  54. }
  55. override fun onCreate(savedInstanceState: Bundle?) {
  56. super.onCreate(savedInstanceState)
  57. preferenceStore = PreferenceManager.getDefaultSharedPreferences(this)
  58. }
  59. override fun onDestroy() {
  60. super.onDestroy()
  61. if (keyboardListenersAttached) {
  62. rootLayout?.viewTreeObserver?.removeOnGlobalLayoutListener(keyboardLayoutListener)
  63. }
  64. }
  65. }