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

import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
Javokhir's avatar
2.2.2    
Javokhir committed
6
7
8
9
10
11
12
13
14
15
16
17
18
import uz.myid.android.sdk.capture.MyIdClient
import uz.myid.android.sdk.capture.MyIdConfig
import uz.myid.android.sdk.capture.MyIdException
import uz.myid.android.sdk.capture.MyIdResult
import uz.myid.android.sdk.capture.MyIdResultListener
import uz.myid.android.sdk.capture.model.MyIdBuildMode
import uz.myid.android.sdk.capture.model.MyIdCameraShape
import uz.myid.android.sdk.capture.model.MyIdEntryType
import uz.myid.android.sdk.capture.model.MyIdImageFormat
import uz.myid.android.sdk.capture.model.MyIdOrganizationDetails
import uz.myid.android.sdk.capture.model.MyIdResidentType
import uz.myid.android.sdk.capture.model.MyIdResolution
import uz.myid.android.sdk.capture.takeUserResult
Javokhir Savriev's avatar
Javokhir Savriev committed
19
import uz.myid.sdk.sample.databinding.ActivityMainBinding
Javokhir's avatar
2.2.2    
Javokhir committed
20
import java.util.Locale
Javokhir Savriev's avatar
Javokhir Savriev committed
21
22
23
24
25

class MainActivity : AppCompatActivity(), MyIdResultListener {

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

Javokhir's avatar
2.2.2    
Javokhir committed
26
    private val client = MyIdClient()
Javokhir Savriev's avatar
Javokhir Savriev committed
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

    private var buildMode = MyIdBuildMode.PRODUCTION
    private var entryType = MyIdEntryType.AUTH
    private var residency = MyIdResidentType.RESIDENT
    private var locale = Locale("en")
    private var shape = MyIdCameraShape.CIRCLE

    private var code = ""

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

        with(binding) {
            radioGroupLang.setOnCheckedChangeListener { _, checkedId ->
                locale = when (checkedId) {
                    R.id.radioUz -> Locale("uz")
                    R.id.radioEn -> Locale("en")
                    else -> Locale("ru")
                }
            }
            radioGroupEntryType.setOnCheckedChangeListener { _, checkedId ->
                entryType = if (checkedId == R.id.radioFace) {
                    MyIdEntryType.FACE
                } else {
                    MyIdEntryType.AUTH
                }
            }
            radioGroupResidentType.setOnCheckedChangeListener { _, checkedId ->
                residency = when (checkedId) {
                    R.id.radioManual -> MyIdResidentType.USER_DEFINED
                    R.id.radioNonResident -> MyIdResidentType.NON_RESIDENT
                    else -> MyIdResidentType.RESIDENT
                }
            }
            radioGroupBuildMode.setOnCheckedChangeListener { _, checkedId ->
                buildMode = if (checkedId == R.id.radioProd) {
                    MyIdBuildMode.PRODUCTION
                } else {
                    MyIdBuildMode.DEBUG
                }
            }
            radioGroupShape.setOnCheckedChangeListener { _, checkedId ->
                shape = if (checkedId == R.id.radioCircle) {
                    MyIdCameraShape.CIRCLE
                } else {
                    MyIdCameraShape.ELLIPSE
                }
            }

            buttonStart.setOnClickListener { startMyId() }
        }
    }

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

        with(binding) {
            imageResult.setImageBitmap(result.bitmap)
            """
                Result code: ${result.code}
                Comparison value: ${result.comparison}
            """.trimIndent().also { textResult.text = it }
        }
    }

    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)
            "User exited sdk".also { textResult.text = it }
        }
    }

    private fun startMyId() {
Javohir Savriy's avatar
2.1.6    
Javohir Savriy committed
115
        val organizationDetails = MyIdOrganizationDetails(
Javokhir Savriev's avatar
Javokhir Savriev committed
116
117
118
            phoneNumber = binding.inputPhoneNumber.value
        )

Javokhir's avatar
2.2.2    
Javokhir committed
119
120
121
122
123
        val config = MyIdConfig.builder(binding.inputClientId.value)
            .withClientHash(
                clientHash = binding.inputClientHash.value,
                clientHashId = binding.inputClientHashId.value,
            )
Javokhir Savriev's avatar
Javokhir Savriev committed
124
125
126
127
128
129
130
131
132
133
134
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
            .withSdkHash(binding.inputSdkHash.value)
            .withExternalId(binding.inputExternalId.value)
            .withThreshold(binding.thresholdSlider.value)
            .withBuildMode(buildMode)
            .withEntryType(entryType)
            .withResidency(residency)
            .withLocale(locale)
            .withCameraShape(shape)
            .withOrganizationDetails(organizationDetails)
Javokhir Savriev's avatar
Javokhir Savriev committed
135
136
            .withResolution(MyIdResolution.RESOLUTION_480)
            .withImageFormat(MyIdImageFormat.PNG)
Javokhir Savriev's avatar
Javokhir Savriev committed
137
138
139
            .withPhoto(binding.checkboxWithPhoto.isChecked)
            .build()

Javokhir's avatar
2.2.2    
Javokhir committed
140
        val intent = client.createIntent(this, config)
Javokhir Savriev's avatar
Javokhir Savriev committed
141
142
143
144
145
146
147
        result.launch(intent)
    }

    private val result = takeUserResult(this)

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