MyIdSdk.kt 8.14 KB
Newer Older
Javokhir's avatar
1.1.8    
Javokhir committed
1
package uz.uzinfocom.myid
Javohir Savriy's avatar
Javohir Savriy committed
2
3

import android.app.Activity
Javokhir's avatar
Javokhir committed
4
5
import android.content.Context
import android.content.res.Resources.NotFoundException
Javohir Savriy's avatar
Javohir Savriy committed
6
7
8
9
10
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
Javokhir's avatar
1.1.8    
Javokhir committed
11
import uz.myid.android.sdk.capture.model.MyIdCameraSelector
Javohir Savriy's avatar
Javohir Savriy committed
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
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
Javokhir's avatar
1.1.0    
Javokhir committed
57
58
            val clientHash: String
            val clientHashId: String
Javohir Savriy's avatar
Javohir Savriy committed
59
60
            val passportData: String
            val dateOfBirth: String
Javokhir's avatar
1.1.4    
Javokhir committed
61
            val minAge: Int
Javohir Savriy's avatar
Javohir Savriy committed
62
63
64
65
66
67
68
69
            val sdkHash: String
            val externalId: String
            val threshold: Float
            val buildMode: MyIdBuildMode
            val entryType: MyIdEntryType
            val residency: MyIdResidentType
            val locale: Locale
            val cameraShape: MyIdCameraShape
Javokhir's avatar
1.1.8    
Javokhir committed
70
            val cameraSelector: MyIdCameraSelector
Javohir Savriy's avatar
Javohir Savriy committed
71
72
73
            val resolution: MyIdResolution
            val imageFormat: MyIdImageFormat
            val organizationDetails: MyIdOrganizationDetails
Javokhir's avatar
Javokhir committed
74
            val distance: Float
Javohir Savriy's avatar
Javohir Savriy committed
75
76
77

            try {
                clientId = config.fetch("clientId")
Javokhir's avatar
1.1.0    
Javokhir committed
78
79
                clientHash = config.fetch("clientHash")
                clientHashId = config.fetch("clientHashId")
Javohir Savriy's avatar
Javohir Savriy committed
80
81
                passportData = config.fetch("passportData")
                dateOfBirth = config.fetch("dateOfBirth")
Javokhir's avatar
1.1.4    
Javokhir committed
82
                minAge = config.fetch("minAge").toIntOrNull() ?: 16
Javohir Savriy's avatar
Javohir Savriy committed
83
84
                sdkHash = config.fetch("sdkHash")
                externalId = config.fetch("externalId")
Javokhir's avatar
Javokhir committed
85
                threshold = config.fetch("threshold").toFloatOrNull() ?: 0.55f
Javokhir's avatar
Javokhir committed
86
                distance = config.fetch("distance").toFloatOrNull() ?: 0.65f
Javohir Savriy's avatar
Javohir Savriy committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

                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
102
103
                    "ENGLISH" -> Locale("en")
                    "RUSSIAN" -> Locale("ru")
Javohir Savriy's avatar
Javohir Savriy committed
104
105
106
107
108
109
                    else -> Locale("uz")
                }
                cameraShape = when (config.fetch("cameraShape").uppercase()) {
                    MyIdCameraShape.ELLIPSE.name -> MyIdCameraShape.ELLIPSE
                    else -> MyIdCameraShape.CIRCLE
                }
Javokhir's avatar
1.1.8    
Javokhir committed
110
111
112
113
                cameraSelector = when (config.fetch("cameraSelector").uppercase()) {
                    MyIdCameraSelector.BACK.name -> MyIdCameraSelector.BACK
                    else -> MyIdCameraSelector.FRONT
                }
Javohir Savriy's avatar
Javohir Savriy committed
114
115
116
117
118
119
120
121
122
123
                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?>
Javokhir's avatar
Javokhir committed
124
125

                val drawableName = orgMap?.fetch("logo")
Javokhir's avatar
Javokhir committed
126
127
128
129
130
                val drawableId = try {
                    currentActivity?.resIdByName(drawableName, "drawable")
                } catch (e: NotFoundException) {
                    null
                }
Javokhir's avatar
Javokhir committed
131

Javohir Savriy's avatar
Javohir Savriy committed
132
                organizationDetails = MyIdOrganizationDetails(
Javokhir's avatar
Javokhir committed
133
134
                    phoneNumber = orgMap?.fetch("phone"),
                    logo = drawableId,
Javohir Savriy's avatar
Javohir Savriy committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                )
            } 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)
Javokhir's avatar
1.1.0    
Javokhir committed
158
                    .withClientHash(clientHash, clientHashId)
Javohir Savriy's avatar
Javohir Savriy committed
159
160
                    .withPassportData(passportData)
                    .withBirthDate(dateOfBirth)
Javokhir's avatar
1.1.4    
Javokhir committed
161
                    .withMinAge(minAge)
Javohir Savriy's avatar
Javohir Savriy committed
162
163
164
165
166
167
168
169
                    .withSdkHash(sdkHash)
                    .withExternalId(externalId)
                    .withThreshold(threshold)
                    .withBuildMode(buildMode)
                    .withEntryType(entryType)
                    .withResidency(residency)
                    .withLocale(locale)
                    .withCameraShape(cameraShape)
Javokhir's avatar
1.1.8    
Javokhir committed
170
                    .withCameraSelector(cameraSelector)
Javohir Savriy's avatar
Javohir Savriy committed
171
172
173
                    .withResolution(resolution)
                    .withImageFormat(imageFormat)
                    .withOrganizationDetails(organizationDetails)
Javokhir's avatar
Javokhir committed
174
                    .withDistance(distance)
Javohir Savriy's avatar
Javohir Savriy committed
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
                    .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()
    }
Javokhir's avatar
Javokhir committed
212
213
214
215
216
217
218

    private fun Context.resIdByName(resIdName: String?, resType: String): Int {
        resIdName?.let {
            return resources.getIdentifier(it, resType, packageName)
        }
        throw NotFoundException()
    }
Javohir Savriy's avatar
Javohir Savriy committed
219
}