|
1 | 1 | package com.klippa.identity_verification.klippa_identity_verification_sdk
|
2 | 2 |
|
3 |
| -import android.util.Log |
4 |
| -import com.klippa.identity_verification.model.KlippaError |
5 |
| -import com.klippa.identity_verification.modules.base.IdentityBuilder |
6 |
| -import com.klippa.identity_verification.modules.base.IdentityBuilder.IdentityBuilderListener |
| 3 | +import android.content.Intent |
| 4 | +import com.klippa.identity_verification.modules.base.IdentitySession |
| 5 | +import com.klippa.identity_verification.modules.base.IdentitySessionResultCode |
7 | 6 | import io.flutter.embedding.engine.plugins.FlutterPlugin
|
8 | 7 | import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
9 | 8 | import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
10 | 9 | import io.flutter.plugin.common.MethodCall
|
11 | 10 | import io.flutter.plugin.common.MethodChannel
|
12 | 11 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
13 | 12 | import io.flutter.plugin.common.MethodChannel.Result
|
| 13 | +import io.flutter.plugin.common.PluginRegistry |
14 | 14 |
|
15 | 15 | /** KlippaIdentityVerificationSdkPlugin */
|
16 |
| -class KlippaIdentityVerificationSdkPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { |
17 |
| - /// The MethodChannel that will the communication between Flutter and native Android |
18 |
| - /// |
19 |
| - /// This local reference serves to register the plugin with the Flutter Engine and unregister it |
20 |
| - /// when the Flutter Engine is detached from the Activity |
21 |
| - private lateinit var channel : MethodChannel |
22 |
| - |
23 |
| - private var activityPluginBinding : ActivityPluginBinding? = null |
24 |
| - |
25 |
| - companion object { |
26 |
| - private const val E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST" |
27 |
| - private const val E_MISSING_SESSION_TOKEN = "E_MISSING_SESSION_TOKEN" |
28 |
| - private const val E_FAILED_TO_SHOW_SESSION = "E_FAILED_TO_SHOW_SESSION" |
29 |
| - private const val E_CANCELED = "E_CANCELED" |
30 |
| - private const val E_SUPPORT_PRESSED = "E_CONTACT_SUPPORT_PRESSED" |
31 |
| - } |
32 |
| - |
33 |
| - |
34 |
| - private val listener = object: IdentityBuilderListener { |
35 |
| - override fun identityVerificationFinished() { |
36 |
| - val resultMap = HashMap<String, Any>() |
37 |
| - resultHandler?.success(resultMap) |
| 16 | +class KlippaIdentityVerificationSdkPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, |
| 17 | + PluginRegistry.ActivityResultListener { |
| 18 | + /// The MethodChannel that will the communication between Flutter and native Android |
| 19 | + /// |
| 20 | + /// This local reference serves to register the plugin with the Flutter Engine and unregister it |
| 21 | + /// when the Flutter Engine is detached from the Activity |
| 22 | + private lateinit var channel: MethodChannel |
| 23 | + |
| 24 | + private var activityPluginBinding: ActivityPluginBinding? = null |
| 25 | + |
| 26 | + companion object { |
| 27 | + private const val E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST" |
| 28 | + private const val E_MISSING_SESSION_TOKEN = "E_MISSING_SESSION_TOKEN" |
| 29 | + private const val E_FAILED_TO_SHOW_SESSION = "E_FAILED_TO_SHOW_SESSION" |
| 30 | + private const val E_CANCELED = "E_CANCELED" |
| 31 | + private const val E_SUPPORT_PRESSED = "E_CONTACT_SUPPORT_PRESSED" |
| 32 | + |
| 33 | + private const val REQUEST_CODE = 99991801 |
38 | 34 | }
|
39 | 35 |
|
40 |
| - override fun identityVerificationCanceled(error: KlippaError) { |
41 |
| - val err = when (error) { |
42 |
| - KlippaError.InsufficientPermissions -> "Insufficient permissions" |
43 |
| - KlippaError.InvalidSessionToken -> "Invalid session token" |
44 |
| - KlippaError.UserCanceled -> "User canceled session" |
45 |
| - KlippaError.NoInternetConnection -> "No internet connection" |
46 |
| - KlippaError.DeviceDoesNotSupportNFC -> "Device doesn't support NFC" |
47 |
| - KlippaError.DeviceNFCDisabled -> "Device NFC is disabled" |
48 |
| - } |
49 |
| - |
50 |
| - resultHandler?.error(E_CANCELED, err, null) |
| 36 | + private var resultHandler: Result? = null |
| 37 | + |
| 38 | + override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
| 39 | + channel = |
| 40 | + MethodChannel(flutterPluginBinding.binaryMessenger, "klippa_identity_verification_sdk") |
| 41 | + channel.setMethodCallHandler(this) |
51 | 42 | }
|
52 | 43 |
|
53 |
| - override fun identityVerificationContactSupportPressed() { |
54 |
| - resultHandler?.error(E_SUPPORT_PRESSED, "Contact support pressed", null) |
| 44 | + override fun onAttachedToActivity(binding: ActivityPluginBinding) { |
| 45 | + this.activityPluginBinding = binding |
| 46 | + binding.addActivityResultListener(this) |
55 | 47 | }
|
56 |
| - } |
57 | 48 |
|
| 49 | + override fun onDetachedFromActivityForConfigChanges() { |
| 50 | + this.activityPluginBinding = null |
| 51 | + } |
58 | 52 |
|
59 |
| - private var resultHandler : Result? = null |
| 53 | + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { |
| 54 | + this.activityPluginBinding = binding |
| 55 | + binding.addActivityResultListener(this) |
| 56 | + } |
60 | 57 |
|
61 |
| - override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
62 |
| - channel = MethodChannel(flutterPluginBinding.binaryMessenger, "klippa_identity_verification_sdk") |
63 |
| - channel.setMethodCallHandler(this) |
64 |
| - } |
| 58 | + override fun onDetachedFromActivity() { |
| 59 | + this.activityPluginBinding = null |
| 60 | + } |
65 | 61 |
|
66 |
| - override fun onAttachedToActivity(binding: ActivityPluginBinding) { |
67 |
| - this.activityPluginBinding = binding |
68 |
| - } |
| 62 | + override fun onMethodCall(call: MethodCall, result: Result) { |
| 63 | + if (call.method == "startSession") { |
| 64 | + startSession(call, result) |
| 65 | + } else { |
| 66 | + result.notImplemented() |
| 67 | + } |
| 68 | + } |
69 | 69 |
|
70 |
| - override fun onDetachedFromActivityForConfigChanges() { |
71 |
| - this.activityPluginBinding = null |
72 |
| - } |
| 70 | + private fun startSession(call: MethodCall, result: Result) { |
| 71 | + val activity = activityPluginBinding?.activity ?: kotlin.run { |
| 72 | + result.error(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist", null) |
| 73 | + return |
| 74 | + } |
73 | 75 |
|
74 |
| - override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { |
75 |
| - this.activityPluginBinding = binding |
76 |
| - } |
| 76 | + try { |
| 77 | + if (!call.hasArgument("SessionToken")) { |
| 78 | + result.error(E_MISSING_SESSION_TOKEN, "Missing session token", null) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + val token = call.argument<String>("SessionToken")!! |
| 83 | + |
| 84 | + val identitySession = IdentitySession( |
| 85 | + token |
| 86 | + ) |
| 87 | + |
| 88 | + call.argument<String>("Language")?.let { language -> |
| 89 | + when (language) { |
| 90 | + "KIVLanguage.English" -> { |
| 91 | + identitySession.language = IdentitySession.KIVLanguage.English |
| 92 | + } |
| 93 | + "KIVLanguage.Dutch" -> { |
| 94 | + identitySession.language = IdentitySession.KIVLanguage.Dutch |
| 95 | + } |
| 96 | + "KIVLanguage.Spanish" -> { |
| 97 | + identitySession.language = IdentitySession.KIVLanguage.Spanish |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + call.argument<Boolean>("HasIntroScreen")?.let { hasIntroScreen -> |
| 103 | + identitySession.hasIntroScreen = hasIntroScreen |
| 104 | + } |
| 105 | + |
| 106 | + call.argument<Boolean>("HasSuccessScreen")?.let { hasSuccessScreen -> |
| 107 | + identitySession.hasSuccessScreen = hasSuccessScreen |
| 108 | + } |
| 109 | + |
| 110 | + call.argument<Boolean>("IsDebug")?.let { isDebug -> |
| 111 | + identitySession.isDebug = isDebug |
| 112 | + } |
| 113 | + |
| 114 | + call.argument<List<String>>("VerifyIncludeList")?.let { include -> |
| 115 | + identitySession.kivIncludeList = include |
| 116 | + } |
| 117 | + |
| 118 | + call.argument<List<String>>("VerifyExcludeList")?.let { exclude -> |
| 119 | + identitySession.kivExcludeList = exclude |
| 120 | + } |
| 121 | + |
| 122 | + call.argument<Int>("RetryThreshold")?.let { retryThreshold -> |
| 123 | + identitySession.retryThreshold = retryThreshold |
| 124 | + } |
| 125 | + |
| 126 | + val intent = identitySession.getIntent(activity) |
| 127 | + resultHandler = result |
| 128 | + activity.startActivityForResult(intent, REQUEST_CODE) |
| 129 | + } catch (e: Exception) { |
| 130 | + result.error( |
| 131 | + E_FAILED_TO_SHOW_SESSION, |
| 132 | + "Could not launch identity verification session", |
| 133 | + e.message + "\n" + e.stackTrace |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
| 139 | + channel.setMethodCallHandler(null) |
| 140 | + } |
| 141 | + |
| 142 | + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean { |
| 143 | + val mappedResultCode = IdentitySessionResultCode.mapResultCode(resultCode) |
| 144 | + when (mappedResultCode) { |
| 145 | + IdentitySessionResultCode.FINISHED -> identityVerificationFinished() |
| 146 | + IdentitySessionResultCode.CONTACT_SUPPORT_PRESSED -> identityVerificationContactSupportPressed( |
| 147 | + mappedResultCode.message() |
| 148 | + ) |
| 149 | + |
| 150 | + IdentitySessionResultCode.INSUFFICIENT_PERMISSIONS, |
| 151 | + IdentitySessionResultCode.INVALID_SESSION_TOKEN, |
| 152 | + IdentitySessionResultCode.USER_CANCELED, |
| 153 | + IdentitySessionResultCode.NO_INTERNET_CONNECTION, |
| 154 | + IdentitySessionResultCode.DEVICE_DOES_NOT_SUPPORT_NFC, |
| 155 | + IdentitySessionResultCode.DEVICE_NFC_DISABLED, |
| 156 | + IdentitySessionResultCode.TAKING_PHOTO_FAILED, |
| 157 | + IdentitySessionResultCode.UNKNOWN_ERROR -> identityVerificationCanceled(mappedResultCode.message()) |
| 158 | + } |
| 159 | + |
| 160 | + return true |
| 161 | + } |
77 | 162 |
|
78 |
| - override fun onDetachedFromActivity() { |
79 |
| - this.activityPluginBinding = null |
80 |
| - } |
81 | 163 |
|
82 |
| - override fun onMethodCall(call: MethodCall, result: Result) { |
83 |
| - if (call.method == "startSession") { |
84 |
| - startSession(call, result) |
85 |
| - } else { |
86 |
| - result.notImplemented() |
| 164 | + private fun identityVerificationFinished() { |
| 165 | + val resultMap = HashMap<String, Any>() |
| 166 | + resultHandler?.success(resultMap) |
87 | 167 | }
|
88 |
| - } |
89 | 168 |
|
90 |
| - private fun startSession(call: MethodCall, result: Result) { |
91 |
| - if (activityPluginBinding == null) { |
92 |
| - result.error(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist", null) |
93 |
| - return |
| 169 | + |
| 170 | + private fun identityVerificationCanceled(message: String) { |
| 171 | + resultHandler?.error(E_CANCELED, message, null) |
94 | 172 | }
|
95 | 173 |
|
96 |
| - try { |
97 |
| - if (!call.hasArgument("SessionToken")) { |
98 |
| - result.error(E_MISSING_SESSION_TOKEN, "Missing session token", null) |
99 |
| - return |
100 |
| - } |
101 |
| - |
102 |
| - val builder = IdentityBuilder(listener, call.argument<String>("SessionToken")!!) |
103 |
| - |
104 |
| - if (call.hasArgument("Language")) { |
105 |
| - val language = call.argument<String>("Language")!! |
106 |
| - if (language == "KIVLanguage.English") { |
107 |
| - builder.language = IdentityBuilder.KIVLanguage.English |
108 |
| - } else if (language == "KIVLanguage.Dutch") { |
109 |
| - builder.language = IdentityBuilder.KIVLanguage.Dutch |
110 |
| - } else if (language == "KIVLanguage.Spanish") { |
111 |
| - builder.language = IdentityBuilder.KIVLanguage.Spanish |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - if (call.hasArgument("HasIntroScreen")) { |
116 |
| - val hasIntroScreen = call.argument<Boolean>("HasIntroScreen")!! |
117 |
| - builder.hasIntroScreen = hasIntroScreen |
118 |
| - } |
119 |
| - |
120 |
| - if (call.hasArgument("HasSuccessScreen")) { |
121 |
| - val hasSuccessScreen = call.argument<Boolean>("HasSuccessScreen")!! |
122 |
| - builder.hasSuccessScreen = hasSuccessScreen |
123 |
| - } |
124 |
| - |
125 |
| - if (call.hasArgument("IsDebug")) { |
126 |
| - val isDebug = call.argument<Boolean>("IsDebug")!! |
127 |
| - builder.isDebug = isDebug |
128 |
| - } |
129 |
| - |
130 |
| - if (call.hasArgument("VerifyIncludeList")) { |
131 |
| - val include = call.argument<List<String>>("VerifyIncludeList")!! |
132 |
| - builder.kivIncludeList = include |
133 |
| - } |
134 |
| - |
135 |
| - if (call.hasArgument("VerifyExcludeList")) { |
136 |
| - val exclude = call.argument<List<String>>("VerifyExcludeList")!! |
137 |
| - builder.kivExcludeList = exclude |
138 |
| - } |
139 |
| - |
140 |
| - val intent = builder.getIntent(activityPluginBinding!!.activity) |
141 |
| - resultHandler = result |
142 |
| - activityPluginBinding?.activity?.startActivity(intent) |
143 |
| - } catch (e: Exception) { |
144 |
| - result.error(E_FAILED_TO_SHOW_SESSION, "Could not launch identity verification session", e.message + "\n" + Log.getStackTraceString(e)) |
| 174 | + private fun identityVerificationContactSupportPressed(message: String) { |
| 175 | + resultHandler?.error(E_SUPPORT_PRESSED, message, null) |
145 | 176 | }
|
146 |
| - } |
147 | 177 |
|
148 |
| - override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
149 |
| - channel.setMethodCallHandler(null) |
150 |
| - } |
151 | 178 |
|
152 | 179 | }
|
0 commit comments