-
Notifications
You must be signed in to change notification settings - Fork 98
/
WalletPasswordFragment.kt
419 lines (380 loc) 路 13.7 KB
/
WalletPasswordFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package one.mixin.android.ui.setting
import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.WindowManager
import android.view.animation.AnimationUtils
import android.widget.FrameLayout
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import one.mixin.android.R
import one.mixin.android.api.ResponseError
import one.mixin.android.api.handleMixinResponse
import one.mixin.android.databinding.FragmentWalletPasswordBinding
import one.mixin.android.extension.clickVibrate
import one.mixin.android.extension.colorFromAttribute
import one.mixin.android.extension.indeterminateProgressDialog
import one.mixin.android.extension.replaceFragment
import one.mixin.android.extension.tickVibrate
import one.mixin.android.extension.toast
import one.mixin.android.extension.updatePinCheck
import one.mixin.android.extension.withArgs
import one.mixin.android.tip.exception.TipNetworkException
import one.mixin.android.ui.common.BaseFragment
import one.mixin.android.ui.tip.Processing
import one.mixin.android.ui.tip.TipBundle
import one.mixin.android.ui.tip.TipFragment
import one.mixin.android.ui.tip.TipFragment.Companion.ARGS_TIP_BUNDLE
import one.mixin.android.ui.tip.getTipBundle
import one.mixin.android.ui.wallet.WalletViewModel
import one.mixin.android.util.ErrorHandler
import one.mixin.android.util.viewBinding
import one.mixin.android.widget.Keyboard
import one.mixin.android.widget.PinView
@AndroidEntryPoint
class WalletPasswordFragment :
BaseFragment(R.layout.fragment_wallet_password),
PinView.OnPinListener,
PinView.OnPinFinishListener {
companion object {
const val TAG = "WalletPasswordFragment"
private const val STEP0 = 0
private const val STEP1 = 1
private const val STEP2 = 2
private const val STEP3 = 3
private const val STEP4 = 4
fun newInstance(tipBundle: TipBundle): WalletPasswordFragment {
return WalletPasswordFragment().withArgs {
putParcelable(ARGS_TIP_BUNDLE, tipBundle)
}
}
}
private val binding by viewBinding(FragmentWalletPasswordBinding::bind)
private var step = STEP0
private var max = 5
private var lastPassword: String? = null
private val tipBundle: TipBundle by lazy { requireArguments().getTipBundle() }
private val walletViewModel by viewModels<WalletViewModel>()
private val rightInAnim by lazy {
AnimationUtils.loadAnimation(
requireContext(),
R.anim.slide_in_right,
).apply {
duration = 200
}
}
private val leftOutAnim by lazy {
AnimationUtils.loadAnimation(
requireContext(),
R.anim.slide_out_left,
).apply {
duration = 200
}
}
private val leftInAnim by lazy {
AnimationUtils.loadAnimation(
requireContext(),
R.anim.slide_in_left,
).apply {
duration = 200
}
}
private val rightOutAnim by lazy {
AnimationUtils.loadAnimation(
requireContext(),
R.anim.slide_out_right,
).apply {
duration = 200
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
binding.apply {
tipTv.inAnimation = rightInAnim
tipTv.outAnimation = leftOutAnim
tipTv.setFactory {
TextView(requireContext()).apply {
setTextColor(requireContext().colorFromAttribute(R.attr.text_primary))
gravity = Gravity.CENTER_HORIZONTAL
layoutParams = FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
gravity = Gravity.CENTER_HORIZONTAL
}
}
}
if (tipBundle.forChange()) {
step = STEP0
titleView.setSubTitle(getString(R.string.Old_PIN), "1/5")
tipTv.setText(getString(R.string.wallet_password_change_tip))
max = 5
titleView.initProgress(max, 0)
} else {
step = STEP1
titleView.setSubTitle(getString(R.string.Set_PIN), "1/4")
tipTv.setText(getString(R.string.tip_create_pin_title))
max = 4
titleView.initProgress(max, 0)
}
titleView.leftIb.setOnClickListener {
when (step) {
STEP0, STEP1 -> activity?.onBackPressedDispatcher?.onBackPressed()
STEP2 -> toStep1()
STEP3 -> toStep2()
STEP4 -> toStep3()
}
}
disableTitleRight()
titleView.rightAnimator.setOnClickListener { createPin() }
pin.setListener(this@WalletPasswordFragment)
pin.setOnPinFinishListener(this@WalletPasswordFragment)
keyboard.initPinKeys(requireContext())
keyboard.setOnClickKeyboardListener(keyboardListener)
keyboard.animate().translationY(0f).start()
}
}
override fun onDestroyView() {
super.onDestroyView()
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
override fun onUpdate(index: Int) {
if (index == binding.pin.getCount()) {
binding.titleView.rightTv.setTextColor(resources.getColor(R.color.colorBlue, null))
binding.titleView.rightAnimator.isEnabled = true
} else {
disableTitleRight()
}
}
override fun onPinFinish() {
createPin()
}
override fun onBackPressed(): Boolean {
when (step) {
STEP0 -> return false
STEP1 -> {
return if (tipBundle.forChange()) {
toStep0()
true
} else {
false
}
}
STEP2 -> {
toStep1()
return true
}
STEP3 -> {
toStep2()
return true
}
STEP4 -> {
toStep3()
return true
}
else -> return false
}
}
private fun disableTitleRight() {
binding.titleView.apply {
rightTv.setTextColor(resources.getColor(R.color.text_gray, null))
rightAnimator.isEnabled = false
}
}
private fun toStep0() {
lastPassword = null
binding.pin.clear()
binding.titleView.setProgress(max - 5)
changeContent(getString(R.string.wallet_password_change_tip), STEP0)
step = STEP0
binding.titleView.setSubTitle(
getString(R.string.Old_PIN),
getSubTitle(),
)
}
private fun toStep1() {
lastPassword = null
binding.pin.clear()
binding.titleView.setProgress(max - 4)
if (tipBundle.forChange()) {
changeContent(getString(R.string.wallet_password_set_new_pin_desc), STEP1)
} else {
changeContent(getString(R.string.tip_create_pin_title), STEP1)
}
step = STEP1
binding.titleView.setSubTitle(
getString(if (tipBundle.forChange()) R.string.Set_new_PIN else R.string.Set_PIN),
getSubTitle(),
)
}
@SuppressLint("SetTextI18n")
private fun toStep2(check: Boolean = false) {
if (check && !validatePin()) {
binding.pin.clear()
return
}
binding.titleView.setProgress(max - 3)
lastPassword = binding.pin.code()
binding.apply {
pin.clear()
changeContent("${getString(R.string.pin_confirm_hint)}\n${getString(R.string.pin_lost_hint)}", STEP2)
step = STEP2
titleView.setSubTitle(getString(R.string.Confirm_PIN), getSubTitle())
}
}
@SuppressLint("SetTextI18n")
private fun toStep3(check: Boolean = false) {
if (check && checkEqual()) return
binding.titleView.setProgress(max - 2)
binding.apply {
pin.clear()
changeContent("${getString(R.string.pin_confirm_again_hint)}\n${getString(R.string.third_pin_confirm_hint)}", STEP3)
step = STEP3
titleView.setSubTitle(getString(R.string.Confirm_PIN), getSubTitle())
}
}
private fun toStep4(check: Boolean = false) {
if (check && checkEqual()) return
binding.titleView.setProgress(max - 1)
binding.apply {
pin.clear()
changeContent(getString(R.string.fourth_pin_confirm_hint), STEP4)
step = STEP4
titleView.setSubTitle(getString(R.string.Confirm_PIN), getSubTitle())
}
}
private fun changeContent(content: String, target: Int) {
if (target > step) {
binding.tipTv.inAnimation = rightInAnim
binding.tipTv.outAnimation = leftOutAnim
} else {
binding.tipTv.inAnimation = leftInAnim
binding.tipTv.outAnimation = rightOutAnim
}
binding.tipTv.setText(content)
}
private fun getSubTitle(): String {
val change = tipBundle.forChange()
return when (step) {
STEP0 -> if (change) "1/5" else throw IllegalArgumentException("")
STEP1 -> if (change) "2/5" else "1/4"
STEP2 -> if (change) "3/5" else "2/4"
STEP3 -> if (change) "4/5" else "3/4"
STEP4 -> if (change) "5/5" else "4/4"
else -> throw IllegalArgumentException("")
}
}
private fun validatePin(): Boolean {
val pin = binding.pin.code()
if (pin == "123456") {
toast(R.string.wallet_password_unsafe)
return false
}
val numKind = arrayListOf<Char>()
pin.forEach {
if (!numKind.contains(it)) {
numKind.add(it)
}
}
if (numKind.size <= 2) {
toast(R.string.wallet_password_unsafe)
return false
}
return true
}
private fun createPin() {
when (step) {
STEP0 -> verify(binding.pin.code())
STEP1 -> toStep2(true)
STEP2 -> toStep3(true)
STEP3 -> toStep4(true)
STEP4 -> {
if (checkEqual()) return
val pin = binding.pin.code()
tipBundle.pin = pin
tipBundle.tipStep = Processing.Creating
val tipFragment = TipFragment.newInstance(tipBundle)
activity?.replaceFragment(tipFragment, R.id.container, TipFragment.TAG)
}
}
}
private fun checkEqual(): Boolean {
if (lastPassword != binding.pin.code()) {
toast(R.string.wallet_password_not_equal)
toStep1()
return true
}
return false
}
private fun verify(pinCode: String) = lifecycleScope.launch {
val dialog = indeterminateProgressDialog(
message = getString(R.string.Please_wait_a_bit),
title = getString(R.string.Verifying),
)
dialog.setCancelable(false)
dialog.show()
handleMixinResponse(
invokeNetwork = { walletViewModel.verifyPin(pinCode) },
successBlock = { response ->
dialog.dismiss()
context?.updatePinCheck()
response.data?.let {
val pin = binding.pin.code()
val tipBundle = requireArguments().getTipBundle()
tipBundle.oldPin = pin
toStep1()
binding.pin.clear()
}
},
exceptionBlock = {
if (it is TipNetworkException) {
return@handleMixinResponse handleFailure(it.error, dialog)
} else {
dialog.dismiss()
binding.pin.clear()
return@handleMixinResponse false
}
},
failureBlock = {
return@handleMixinResponse handleFailure(requireNotNull(it.error), dialog)
},
)
}
private suspend fun handleFailure(error: ResponseError, dialog: Dialog): Boolean {
binding.pin.clear()
if (error.code == ErrorHandler.TOO_MANY_REQUEST) {
dialog.dismiss()
toast(R.string.error_pin_check_too_many_request)
return true
} else if (error.code == ErrorHandler.PIN_INCORRECT) {
val errorCount = walletViewModel.errorCount()
toast(requireContext().resources.getQuantityString(R.plurals.error_pin_incorrect_with_times, errorCount, errorCount))
dialog.dismiss()
return true
}
dialog.dismiss()
return false
}
private val keyboardListener: Keyboard.OnClickKeyboardListener = object : Keyboard.OnClickKeyboardListener {
override fun onKeyClick(position: Int, value: String) {
context?.tickVibrate()
if (position == 11) {
binding.pin.delete()
} else {
binding.pin.append(value)
}
}
override fun onLongClick(position: Int, value: String) {
context?.clickVibrate()
if (position == 11) {
binding.pin.clear()
} else {
binding.pin.append(value)
}
}
}
}