123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.kdanmobile.reader.password
- import android.annotation .SuppressLint
- import android.content.Context
- import android.content.Intent
- import android.os.Bundle
- import android.os.Handler
- import android.os.Message
- import android.text.Editable
- import android.view.View
- import android.view.inputmethod.InputMethodManager
- import com.kdanmobile.reader.R
- import com.kdanmobile.reader.screen.model.SimpleTextWatcher
- import com.kdanmobile.base.KdanBaseActivity
- import kotlinx.android.synthetic.main.activity_reader_password_dialog.*
- class DialogPasswordActivity : KdanBaseActivity(), View.OnClickListener {
- companion object {
- private const val KEY_FILENAME = "filename"
- private const val KEY_IS_INCORRECT_PASSWORD = "isIncorrectPassword"
- const val KEY_USER_INPUT_PASSWORD = "password"
- fun createLaunchIntent(context: Context, filename: String, isIncorrectPassword: Boolean): Intent {
- return Intent(context, DialogPasswordActivity::class.java).apply {
- putExtras(Bundle().also {
- it.putBoolean(KEY_IS_INCORRECT_PASSWORD, isIncorrectPassword)
- it.putString(KEY_FILENAME, filename)
- })
- }
- }
- }
- private var filename = ""
- private var isIncorrectPassword = false
- @SuppressLint("HandlerLeak")
- private var handler = object : Handler() {
- override fun handleMessage(msg: Message) {
- super.handleMessage(msg)
- when (msg.what) {
- 10 -> (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(et_dialogPassword_name, InputMethodManager.RESULT_UNCHANGED_SHOWN)
- 20 -> (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(et_dialogPassword_name.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
- }
- }
- }
- override fun onSaveInstanceState(outState: Bundle?) {
- super.onSaveInstanceState(outState)
- outState?.putBoolean(KEY_IS_INCORRECT_PASSWORD, isIncorrectPassword)
- outState?.putString(KEY_FILENAME, filename)
- }
- override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
- super.onRestoreInstanceState(savedInstanceState)
- updateErrorMessage(savedInstanceState)
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- filename = intent.getStringExtra(KEY_FILENAME)
- setFinishOnTouchOutside(false)
- setContentView(R.layout.activity_reader_password_dialog)
- initView()
- setListener()
- }
- private fun initView() {
- btn_dialogPassword_ok.isEnabled = false
- tv_dialogPassword_title.text = filename
- handler.sendEmptyMessageDelayed(10, 100)
- updateErrorMessage(intent.extras)
- }
- private fun updateErrorMessage(bundle: Bundle?) {
- if (null != bundle) {
- val value = bundle.getBoolean(KEY_IS_INCORRECT_PASSWORD, false)
- if (value) {
- layout_dialogPassword_name.error = resources.getString(R.string.reader_dialog_password_error)
- isIncorrectPassword = value
- }
- }
- }
- private fun setListener() {
- iv_dialogPassword_back.setOnClickListener(this)
- btn_dialogPassword_ok.setOnClickListener(this)
- et_dialogPassword_name.addTextChangedListener(object : SimpleTextWatcher() {
- override fun afterTextChanged(editable: Editable) {
- super.afterTextChanged(editable)
- val password = editable.toString()
- btn_dialogPassword_ok.isEnabled = password.isNotEmpty()
- if (isIncorrectPassword) {
- isIncorrectPassword = false
- layout_dialogPassword_name.error = ""
- }
- }
- })
- }
- override fun onClick(v: View) {
- when (v.id) {
- R.id.iv_dialogPassword_back -> onBackPressed()
- R.id.btn_dialogPassword_ok -> {
- val password = et_dialogPassword_name.text.toString()
- val data = Intent().apply {
- putExtra(KEY_USER_INPUT_PASSWORD, password)
- }
- setResult(RESULT_OK, data)
- finish()
- }
- }
- }
- override fun onResume() {
- super.onResume()
- handler.sendEmptyMessageDelayed(10, 100)
- }
- override fun onBackPressed() {
- val data = Intent().apply {
- putExtra(KEY_USER_INPUT_PASSWORD, "")
- }
- setResult(RESULT_OK, data)
- super.onBackPressed()
- }
- override fun onPause() {
- handler.sendEmptyMessage(20)
- super.onPause()
- }
- }
|