DialogPasswordActivity.kt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.kdanmobile.reader.password
  2. import android.annotation .SuppressLint
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.os.Bundle
  6. import android.os.Handler
  7. import android.os.Message
  8. import android.text.Editable
  9. import android.view.View
  10. import android.view.inputmethod.InputMethodManager
  11. import com.kdanmobile.reader.R
  12. import com.kdanmobile.reader.screen.model.SimpleTextWatcher
  13. import com.kdanmobile.base.KdanBaseActivity
  14. import kotlinx.android.synthetic.main.activity_reader_password_dialog.*
  15. class DialogPasswordActivity : KdanBaseActivity(), View.OnClickListener {
  16. companion object {
  17. private const val KEY_FILENAME = "filename"
  18. private const val KEY_IS_INCORRECT_PASSWORD = "isIncorrectPassword"
  19. const val KEY_USER_INPUT_PASSWORD = "password"
  20. fun createLaunchIntent(context: Context, filename: String, isIncorrectPassword: Boolean): Intent {
  21. return Intent(context, DialogPasswordActivity::class.java).apply {
  22. putExtras(Bundle().also {
  23. it.putBoolean(KEY_IS_INCORRECT_PASSWORD, isIncorrectPassword)
  24. it.putString(KEY_FILENAME, filename)
  25. })
  26. }
  27. }
  28. }
  29. private var filename = ""
  30. private var isIncorrectPassword = false
  31. @SuppressLint("HandlerLeak")
  32. private var handler = object : Handler() {
  33. override fun handleMessage(msg: Message) {
  34. super.handleMessage(msg)
  35. when (msg.what) {
  36. 10 -> (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(et_dialogPassword_name, InputMethodManager.RESULT_UNCHANGED_SHOWN)
  37. 20 -> (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(et_dialogPassword_name.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
  38. }
  39. }
  40. }
  41. override fun onSaveInstanceState(outState: Bundle?) {
  42. super.onSaveInstanceState(outState)
  43. outState?.putBoolean(KEY_IS_INCORRECT_PASSWORD, isIncorrectPassword)
  44. outState?.putString(KEY_FILENAME, filename)
  45. }
  46. override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
  47. super.onRestoreInstanceState(savedInstanceState)
  48. updateErrorMessage(savedInstanceState)
  49. }
  50. override fun onCreate(savedInstanceState: Bundle?) {
  51. super.onCreate(savedInstanceState)
  52. filename = intent.getStringExtra(KEY_FILENAME)
  53. setFinishOnTouchOutside(false)
  54. setContentView(R.layout.activity_reader_password_dialog)
  55. initView()
  56. setListener()
  57. }
  58. private fun initView() {
  59. btn_dialogPassword_ok.isEnabled = false
  60. tv_dialogPassword_title.text = filename
  61. handler.sendEmptyMessageDelayed(10, 100)
  62. updateErrorMessage(intent.extras)
  63. }
  64. private fun updateErrorMessage(bundle: Bundle?) {
  65. if (null != bundle) {
  66. val value = bundle.getBoolean(KEY_IS_INCORRECT_PASSWORD, false)
  67. if (value) {
  68. layout_dialogPassword_name.error = resources.getString(R.string.reader_dialog_password_error)
  69. isIncorrectPassword = value
  70. }
  71. }
  72. }
  73. private fun setListener() {
  74. iv_dialogPassword_back.setOnClickListener(this)
  75. btn_dialogPassword_ok.setOnClickListener(this)
  76. et_dialogPassword_name.addTextChangedListener(object : SimpleTextWatcher() {
  77. override fun afterTextChanged(editable: Editable) {
  78. super.afterTextChanged(editable)
  79. val password = editable.toString()
  80. btn_dialogPassword_ok.isEnabled = password.isNotEmpty()
  81. if (isIncorrectPassword) {
  82. isIncorrectPassword = false
  83. layout_dialogPassword_name.error = ""
  84. }
  85. }
  86. })
  87. }
  88. override fun onClick(v: View) {
  89. when (v.id) {
  90. R.id.iv_dialogPassword_back -> onBackPressed()
  91. R.id.btn_dialogPassword_ok -> {
  92. val password = et_dialogPassword_name.text.toString()
  93. val data = Intent().apply {
  94. putExtra(KEY_USER_INPUT_PASSWORD, password)
  95. }
  96. setResult(RESULT_OK, data)
  97. finish()
  98. }
  99. }
  100. }
  101. override fun onResume() {
  102. super.onResume()
  103. handler.sendEmptyMessageDelayed(10, 100)
  104. }
  105. override fun onBackPressed() {
  106. val data = Intent().apply {
  107. putExtra(KEY_USER_INPUT_PASSWORD, "")
  108. }
  109. setResult(RESULT_OK, data)
  110. super.onBackPressed()
  111. }
  112. override fun onPause() {
  113. handler.sendEmptyMessage(20)
  114. super.onPause()
  115. }
  116. }