@@ -2,14 +2,18 @@ package com.descope.flutter
22
33import android.content.Context
44import android.net.Uri
5+ import android.os.Handler
6+ import android.os.Looper
57import androidx.browser.customtabs.CustomTabsIntent
68import androidx.security.crypto.EncryptedSharedPreferences
79import androidx.security.crypto.MasterKeys
10+ import com.descope.Descope
811import com.descope.android.DescopeSystemInfo
912import com.descope.internal.routes.getPackageOrigin
1013import com.descope.internal.routes.performAssertion
1114import com.descope.internal.routes.performNativeAuthorization
1215import com.descope.internal.routes.performRegister
16+ import com.descope.sdk.DescopeLogger
1317import io.flutter.embedding.engine.plugins.FlutterPlugin
1418import io.flutter.embedding.engine.plugins.activity.ActivityAware
1519import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
@@ -25,6 +29,7 @@ import kotlinx.coroutines.launch
2529/* * DescopePlugin */
2630class DescopePlugin : FlutterPlugin , MethodCallHandler , ActivityAware {
2731 private var channel : MethodChannel ? = null
32+ private var logChannel: MethodChannel ? = null
2833 private var context: Context ? = null
2934 private lateinit var storage: Store
3035
@@ -180,6 +185,38 @@ class DescopePlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
180185 channel = MethodChannel (flutterPluginBinding.binaryMessenger, " descope_flutter/methods" )
181186 channel?.setMethodCallHandler(this )
182187
188+ // Set up the log channel with a handler for logger configuration from Flutter
189+ val logsChannel = MethodChannel (flutterPluginBinding.binaryMessenger, " descope_flutter/logs" )
190+ logChannel = logsChannel
191+ val applicationContext = flutterPluginBinding.applicationContext
192+
193+ logsChannel.setMethodCallHandler { call, result ->
194+ if (call.method == " configure" ) {
195+ val levelString = call.argument<String >(" level" )
196+ val unsafe = call.argument<Boolean >(" unsafe" )
197+
198+ if (levelString == null || unsafe == null ) {
199+ result.error(" INVALID_ARGS" , " Missing level or unsafe arguments" , null )
200+ return @setMethodCallHandler
201+ }
202+
203+ val level = when (levelString) {
204+ " error" -> DescopeLogger .Level .Error
205+ " info" -> DescopeLogger .Level .Info
206+ else -> DescopeLogger .Level .Debug
207+ }
208+
209+ // Initialize SDK with the logger configured from Flutter
210+ Descope .setup(applicationContext, projectId = " " ) {
211+ logger = FlutterDescopeLogger (logsChannel, level, unsafe)
212+ }
213+
214+ result.success(null )
215+ } else {
216+ result.notImplemented()
217+ }
218+ }
219+
183220 flutterPluginBinding.platformViewRegistry.registerViewFactory(
184221 " descope_flutter/descope_flow_view" ,
185222 DescopeFlowViewFactory (flutterPluginBinding.binaryMessenger)
@@ -189,6 +226,8 @@ class DescopePlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
189226 override fun onDetachedFromEngine (binding : FlutterPlugin .FlutterPluginBinding ) {
190227 channel?.setMethodCallHandler(null )
191228 channel = null
229+ logChannel?.setMethodCallHandler(null )
230+ logChannel = null
192231 }
193232
194233 // ActivityAware
@@ -272,3 +311,31 @@ private fun createEncryptedStore(context: Context, projectId: String): Store {
272311 }
273312 }
274313}
314+
315+ // Logger
316+
317+ /* *
318+ * A DescopeLogger subclass that forwards all logs to Flutter via a MethodChannel.
319+ * This logger mirrors the level and unsafe settings from the Flutter layer.
320+ */
321+ private class FlutterDescopeLogger (private val channel : MethodChannel , level : Level , unsafe : Boolean ) : DescopeLogger(level, unsafe) {
322+ private val handler = Handler (Looper .getMainLooper())
323+
324+ override fun output (level : Level , message : String , values : List <Any >) {
325+ val levelString = when (level) {
326+ Level .Error -> " error"
327+ Level .Info -> " info"
328+ Level .Debug -> " debug"
329+ }
330+
331+ val valuesArray = values.map { it.toString() }
332+
333+ handler.post {
334+ channel.invokeMethod(" log" , mapOf (
335+ " level" to levelString,
336+ " message" to message,
337+ " values" to valuesArray,
338+ ))
339+ }
340+ }
341+ }
0 commit comments