diff --git a/android/src/main/kotlin/com/linecorp/flutter_line_sdk/FlutterLineSdkPlugin.kt b/android/src/main/kotlin/com/linecorp/flutter_line_sdk/FlutterLineSdkPlugin.kt index 95c1b99..3cfc46f 100644 --- a/android/src/main/kotlin/com/linecorp/flutter_line_sdk/FlutterLineSdkPlugin.kt +++ b/android/src/main/kotlin/com/linecorp/flutter_line_sdk/FlutterLineSdkPlugin.kt @@ -6,13 +6,12 @@ import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result -import io.flutter.plugin.common.PluginRegistry import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.activity.ActivityAware import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding import io.flutter.plugin.common.BinaryMessenger -class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware { +class FlutterLineSdkPlugin : MethodCallHandler, FlutterPlugin, ActivityAware { private var methodChannel: MethodChannel? = null private val lineSdkWrapper = LineSdkWrapper() @@ -23,49 +22,34 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis override fun onMethodCall(call: MethodCall, result: Result) { when (call.method) { "toBeta" -> run { - val channelId: String = call.argument("channelId") ?: "" - val openDiscoveryIdDocumentUrl: String = call.argument("openDiscoveryIdDocumentUrl") ?: "" - val apiServerBaseUrl: String = call.argument("apiServerBaseUrl") ?: "" - val webLoginPageUrl: String = call.argument("webLoginPageUrl") ?: "" + val channelId = call.argument("channelId").orEmpty() + val openDiscoveryIdDocumentUrl = call.argument("openDiscoveryIdDocumentUrl").orEmpty() + val apiServerBaseUrl = call.argument("apiServerBaseUrl").orEmpty() + val webLoginPageUrl = call.argument("webLoginPageUrl").orEmpty() lineSdkWrapper.setupBetaConfig( - channelId, - openDiscoveryIdDocumentUrl, - apiServerBaseUrl, - webLoginPageUrl + channelId, + openDiscoveryIdDocumentUrl, + apiServerBaseUrl, + webLoginPageUrl ) result.success(null) } "setup" -> { - val channelId: String = call.argument("channelId").orEmpty() - val activity = activity - if (activity == null) { - result.error( - "no_activity_found", - "There is no valid Activity found to present LINE SDK Login screen.", - null - ) - return + withActivity(result) { activity -> + val channelId = call.argument("channelId").orEmpty() + lineSdkWrapper.setupSdk(activity, channelId) + result.success(null) } - lineSdkWrapper.setupSdk(activity, channelId) - result.success(null) } "login" -> { - val activity = this.activity - if (activity == null) { - result.error( - "no_activity_found", - "There is no valid Activity found to present LINE SDK Login screen.", - null - ) - return - } - - val scopes = call.argument("scopes") ?: emptyList() - val isWebLogin = call.argument("onlyWebLogin") ?: false - val botPrompt = call.argument("botPrompt") ?: "normal" - val idTokenNonce: String? = call.argument("idTokenNonce") - val loginRequestCode = call.argument("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE - lineSdkWrapper.login( + withActivity(result) { activity -> + val scopes = call.argument>("scopes").orEmpty() + val isWebLogin = call.argument("onlyWebLogin") ?: false + val botPrompt = call.argument("botPrompt") ?: "normal" + val idTokenNonce = call.argument("idTokenNonce") + val loginRequestCode = call.argument("loginRequestCode") + ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE + lineSdkWrapper.login( loginRequestCode, activity, scopes = scopes, @@ -73,7 +57,8 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis botPromptString = botPrompt, idTokenNonce = idTokenNonce, result = result - ) + ) + } } "getProfile" -> lineSdkWrapper.getProfile(result) "currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result) @@ -85,12 +70,26 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis } } + private fun withActivity(result: Result, block: (Activity) -> Unit) { + val activity = this.activity + if (activity == null) { + result.error( + "no_activity_found", + "There is no valid Activity found to present LINE SDK Login screen.", + null + ) + return + } + block(activity) + } + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { onAttachedToEngine(binding.binaryMessenger) } override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { - methodChannel = null; + methodChannel?.setMethodCallHandler(null) + methodChannel = null } override fun onAttachedToActivity(binding: ActivityPluginBinding) { @@ -109,45 +108,25 @@ class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultLis unbindActivityBinding() } - override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean = - lineSdkWrapper.handleActivityResult(requestCode, resultCode, intent) - private fun bindActivityBinding(binding: ActivityPluginBinding) { this.activity = binding.activity this.activityBinding = binding - addActivityResultListener(binding) + binding.addActivityResultListener(lineSdkWrapper::handleActivityResult) } private fun unbindActivityBinding() { - activityBinding?.removeActivityResultListener(this) - this.activity = null; + activityBinding?.removeActivityResultListener(lineSdkWrapper::handleActivityResult) + this.activity = null this.activityBinding = null } private fun onAttachedToEngine(messenger: BinaryMessenger) { methodChannel = MethodChannel(messenger, CHANNEL_NAME) - methodChannel!!.setMethodCallHandler(this) - } - - private fun addActivityResultListener(activityBinding: ActivityPluginBinding) { - activityBinding.addActivityResultListener(this) - } - - private fun addActivityResultListener(registrar: PluginRegistry.Registrar) { - registrar.addActivityResultListener(this) + methodChannel?.setMethodCallHandler(this) } companion object { private const val CHANNEL_NAME = "com.linecorp/flutter_line_sdk" private const val DEFAULT_ACTIVITY_RESULT_REQUEST_CODE = 8192 - - @JvmStatic - fun registerWith(registrar: PluginRegistry.Registrar) { - FlutterLineSdkPlugin().apply { - onAttachedToEngine(registrar.messenger()) - activity = registrar.activity() - addActivityResultListener(registrar) - } - } } }