MyIdSdk.kt 6.78 KB
Newer Older
Javohir Savriy's avatar
Javohir Savriy committed
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
package uz.aigroup.myid

import android.app.Activity
import android.os.Build
import io.flutter.plugin.common.MethodChannel
import uz.myid.android.sdk.capture.MyIdClient
import uz.myid.android.sdk.capture.MyIdConfig
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 java.util.Locale

class MyIdSdk(
    private val client: MyIdClient,
    private val activityListener: MyIdSdkActivityListener,
) {

    private var currentFlutterResult: MethodChannel.Result? = null
    private var currentActivity: Activity? = null

    private fun setFlutterResult(result: MethodChannel.Result?) {
        currentFlutterResult = result
        activityListener.setCurrentFlutterResult(result)
    }

    fun setActivity(activity: Activity?) {
        if (activity != null) {
            currentActivity = activity
        }
    }

    fun start(
        config: HashMap<String, Any?>?,
        result: MethodChannel.Result?
    ) {
        setFlutterResult(result)

        try {
            if (config == null) {
                currentFlutterResult?.error(
                    "error",
                    "MyID config is null",
                    null
                )
                setFlutterResult(null)
                return
            }

            val clientId: String
            val passportData: String
            val dateOfBirth: String
            val sdkHash: String
            val externalId: String
            val threshold: Float
            val buildMode: MyIdBuildMode
            val entryType: MyIdEntryType
            val residency: MyIdResidentType
            val locale: Locale
            val cameraShape: MyIdCameraShape
            val resolution: MyIdResolution
            val imageFormat: MyIdImageFormat
            val organizationDetails: MyIdOrganizationDetails
            val withPhoto: Boolean

            try {
                clientId = config.fetch("clientId")
                passportData = config.fetch("passportData")
                dateOfBirth = config.fetch("dateOfBirth")
                sdkHash = config.fetch("sdkHash")
                externalId = config.fetch("externalId")
                threshold = config.fetch("threshold").toFloatOrNull() ?: 0.6f

                buildMode = when (config.fetch("buildMode").uppercase()) {
                    MyIdBuildMode.DEBUG.name -> MyIdBuildMode.DEBUG
                    else -> MyIdBuildMode.PRODUCTION
                }
                entryType = when (config.fetch("entryType").uppercase()) {
                    MyIdEntryType.FACE.name -> MyIdEntryType.FACE
                    else -> MyIdEntryType.AUTH
                }
                residency = when (config.fetch("residency").uppercase()) {
                    MyIdResidentType.USER_DEFINED.name -> MyIdResidentType.USER_DEFINED
                    MyIdResidentType.NON_RESIDENT.name -> MyIdResidentType.NON_RESIDENT
                    else -> MyIdResidentType.RESIDENT
                }
                locale = when (config.fetch("locale").uppercase()) {
Javokhir's avatar
Javokhir committed
91
92
                    "ENGLISH" -> Locale("en")
                    "RUSSIAN" -> Locale("ru")
Javohir Savriy's avatar
Javohir Savriy committed
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
                    else -> Locale("uz")
                }
                cameraShape = when (config.fetch("cameraShape").uppercase()) {
                    MyIdCameraShape.ELLIPSE.name -> MyIdCameraShape.ELLIPSE
                    else -> MyIdCameraShape.CIRCLE
                }
                resolution = when (config.fetch("resolution").uppercase()) {
                    MyIdResolution.RESOLUTION_720.name -> MyIdResolution.RESOLUTION_720
                    else -> MyIdResolution.RESOLUTION_480
                }
                imageFormat = when (config.fetch("imageFormat").uppercase()) {
                    MyIdImageFormat.JPG.name -> MyIdImageFormat.JPG
                    else -> MyIdImageFormat.PNG
                }

                val orgMap = config["organizationDetails"] as? HashMap<String, Any?>
                organizationDetails = MyIdOrganizationDetails(
                    phoneNumber = orgMap?.fetch("phone")
                )

                withPhoto = config.fetch("withPhoto").toBooleanStrictOrNull() ?: false
            } catch (e: Exception) {
                currentFlutterResult?.error(
                    "error",
                    "MyID config error: ${e.message}",
                    null
                )
                setFlutterResult(null)
                return
            }

            if (currentActivity == null) {
                currentFlutterResult?.error(
                    "error",
                    "Android activity does not exist",
                    null
                )
                setFlutterResult(null)
                return
            }

            try {
                val myIdConfig = MyIdConfig.builder(clientId)
                    .withPassportData(passportData)
                    .withBirthDate(dateOfBirth)
                    .withSdkHash(sdkHash)
                    .withExternalId(externalId)
                    .withThreshold(threshold)
                    .withBuildMode(buildMode)
                    .withEntryType(entryType)
                    .withResidency(residency)
                    .withLocale(locale)
                    .withCameraShape(cameraShape)
                    .withResolution(resolution)
                    .withImageFormat(imageFormat)
                    .withOrganizationDetails(organizationDetails)
                    .withPhoto(withPhoto)
                    .build()

                currentActivity?.let {
                    client.startActivityForResult(it, 1, myIdConfig)
                }
            } catch (e: Exception) {
                currentFlutterResult?.error(
                    "error",
                    "Failed to show MyID page: ${e.message}",
                    null
                )
                setFlutterResult(null)
                return
            }
        } catch (e: Exception) {
            currentFlutterResult?.error(
                "error",
                "Unexpected error starting MyID page: ${e.message}",
                null
            )
            setFlutterResult(null)
            return
        }
    }

    private fun <T> HashMap<String, T>.fetch(key: String): String {
        val result = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            getOrDefault(key, "")
        } else {
            if (containsKey(key)) {
                this[key] ?: ""
            } else {
                ""
            }
        }
        return result?.toString().orEmpty()
    }
}