MainActivity.kt 4.04 KB
Newer Older
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
1
2
3
4
5
6
package uz.myid.sdk.sample

import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import uz.myid.android.sdk.capture.*
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
7
import uz.myid.android.sdk.capture.model.OrganizationDetails
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
8
import uz.myid.sdk.sample.databinding.ActivityMainBinding
Javokhir's avatar
Javokhir committed
9
import java.util.*
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
10
11
12
13
14

class MainActivity : AppCompatActivity(), MyIdResultListener {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

Javokhir's avatar
Javokhir committed
15
    private val myIdClient = MyIdClient()
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
16
17
18

    private var clientId = ""
    private var myIdBuildMode = MyIdBuildMode.PRODUCTION
Javokhir's avatar
Javokhir committed
19
20
21
    private var myIdEntryType = MyIdEntryType.AUTH
    private var myIdLocale = Locale("en")
    private var myIdCameraShape = MyIdCameraShape.CIRCLE
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
22
23
24
25
26
27
28
29
30
31
32
33

    private var code = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        with(binding) {
            inputClientId.setText(clientId)

            radioGroupLang.setOnCheckedChangeListener { _, checkedId ->
                myIdLocale = when (checkedId) {
Javokhir's avatar
Javokhir committed
34
35
36
                    R.id.radioUz -> Locale("uz")
                    R.id.radioEn -> Locale("en")
                    else -> Locale("ru")
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
37
38
                }
            }
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
39
            radioGroupEntryType.setOnCheckedChangeListener { _, checkedId ->
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
40
41
42
43
44
45
                myIdEntryType = if (checkedId == R.id.radioFace) {
                    MyIdEntryType.FACE
                } else {
                    MyIdEntryType.AUTH
                }
            }
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
46
            radioGroupBuildMode.setOnCheckedChangeListener { _, checkedId ->
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
47
48
49
50
51
52
                myIdBuildMode = if (checkedId == R.id.radioProd) {
                    MyIdBuildMode.PRODUCTION
                } else {
                    MyIdBuildMode.DEBUG
                }
            }
Javokhir's avatar
Javokhir committed
53
54
55
56
57
58
59
            radioGroupShape.setOnCheckedChangeListener { _, checkedId ->
                myIdCameraShape = if (checkedId == R.id.radioCircle) {
                    MyIdCameraShape.CIRCLE
                } else {
                    MyIdCameraShape.ELLIPSE
                }
            }
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
60
61
62
63
64
65
66
67
68
69

            buttonStart.setOnClickListener { startMyId() }
        }
    }

    override fun onSuccess(result: MyIdResult) {
        code = result.code.orEmpty()

        with(binding) {
            imageResult.setImageBitmap(result.bitmap)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
70
71
72
73
            """
                Result code: ${result.code}
                Comparison value: ${result.comparison}
            """.trimIndent().also { textResult.text = it }
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        }
    }

    override fun onError(e: MyIdException) {
        code = ""

        with(binding) {
            imageResult.setImageBitmap(null)
            """
                Result error: ${e.message}
                Result error code: ${e.code}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onUserExited() {
        code = ""

        with(binding) {
            imageResult.setImageBitmap(null)
Javohir Savriy's avatar
2.1.0    
Javohir Savriy committed
94
            "User exited sdk".also { textResult.text = it }
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
95
96
97
98
        }
    }

    private fun startMyId() {
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
99
100
101
102
103
        val organizationDetails = OrganizationDetails(
            phoneNumber = binding.inputPhoneNumber.value,
            logo = R.drawable.image_logo
        )

Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
104
105
106
        val myIdConfig = MyIdConfig.builder(binding.inputClientId.value)
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
107
            .withSdkHash(binding.inputSdkHash.value)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
108
            .withExternalId(binding.inputExternalId.value)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
109
            .withThreshold(binding.thresholdSlider.value)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
110
            .withBuildMode(myIdBuildMode)
Javokhir's avatar
Javokhir committed
111
            .withEntryType(myIdEntryType)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
112
            .withLocale(myIdLocale)
Javokhir's avatar
Javokhir committed
113
114
            .withCameraShape(myIdCameraShape)
            .withOrganizationDetails(organizationDetails)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
115
            .withPhoto(binding.checkboxWithPhoto.isChecked)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
116
117
            .build()

Javokhir's avatar
Javokhir committed
118
        val intent = myIdClient.createIntent(this, myIdConfig)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
119
120
121
122
123
124
125
        result.launch(intent)
    }

    private val result = takeUserResult(this)

    private inline val EditText.value: String get() = text.toString().trim()
}