|
1 | 1 | package com.linecorp.flutter_line_sdk |
2 | 2 |
|
| 3 | +import android.app.Activity |
3 | 4 | import android.content.Intent |
4 | 5 | import io.flutter.plugin.common.MethodCall |
5 | 6 | import io.flutter.plugin.common.MethodChannel |
6 | 7 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler |
7 | 8 | import io.flutter.plugin.common.MethodChannel.Result |
8 | 9 | import io.flutter.plugin.common.PluginRegistry |
| 10 | +import io.flutter.embedding.engine.plugins.FlutterPlugin |
| 11 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware |
| 12 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding |
| 13 | +import io.flutter.plugin.common.BinaryMessenger |
9 | 14 |
|
10 | | -class FlutterLineSdkPlugin( |
11 | | - private val channel: MethodChannel, |
12 | | - private val lineSdkWrapper: LineSdkWrapper |
13 | | -) : MethodCallHandler, PluginRegistry.ActivityResultListener { |
14 | | - override fun onMethodCall(call: MethodCall, result: Result) = when (call.method) { |
15 | | - "toBeta" -> run { |
16 | | - val channelId: String = call.argument("channelId") ?: "" |
17 | | - val openDiscoveryIdDocumentUrl: String = call.argument("openDiscoveryIdDocumentUrl") ?: "" |
18 | | - val apiServerBaseUrl: String = call.argument("apiServerBaseUrl") ?: "" |
19 | | - val webLoginPageUrl: String = call.argument("webLoginPageUrl") ?: "" |
20 | | - lineSdkWrapper.setupBetaConfig( |
21 | | - channelId, |
22 | | - openDiscoveryIdDocumentUrl, |
23 | | - apiServerBaseUrl, |
24 | | - webLoginPageUrl |
25 | | - ) |
26 | | - result.success(null) |
27 | | - } |
28 | | - "setup" -> { |
29 | | - val channelId: String = call.argument<String?>("channelId").orEmpty() |
30 | | - lineSdkWrapper.setupSdk(channelId) |
31 | | - result.success(null) |
32 | | - } |
33 | | - "login" -> { |
34 | | - val scopes = call.argument("scopes") ?: emptyList<String>() |
35 | | - val isWebLogin = call.argument("onlyWebLogin") ?: false |
36 | | - val botPrompt = call.argument("botPrompt") ?: "normal" |
37 | | - val loginRequestCode = call.argument<Int?>("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE |
38 | | - lineSdkWrapper.login( |
39 | | - loginRequestCode, |
40 | | - scopes = scopes, |
41 | | - onlyWebLogin = isWebLogin, |
42 | | - botPromptString = botPrompt, |
43 | | - result = result |
44 | | - ) |
| 15 | +class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware { |
| 16 | + |
| 17 | + private var methodChannel: MethodChannel? = null |
| 18 | + private val lineSdkWrapper = LineSdkWrapper() |
| 19 | + |
| 20 | + private var activity: Activity? = null |
| 21 | + private var activityBinding: ActivityPluginBinding? = null |
| 22 | + |
| 23 | + override fun onMethodCall(call: MethodCall, result: Result) { |
| 24 | + when (call.method) { |
| 25 | + "toBeta" -> run { |
| 26 | + val channelId: String = call.argument("channelId") ?: "" |
| 27 | + val openDiscoveryIdDocumentUrl: String = call.argument("openDiscoveryIdDocumentUrl") ?: "" |
| 28 | + val apiServerBaseUrl: String = call.argument("apiServerBaseUrl") ?: "" |
| 29 | + val webLoginPageUrl: String = call.argument("webLoginPageUrl") ?: "" |
| 30 | + lineSdkWrapper.setupBetaConfig( |
| 31 | + channelId, |
| 32 | + openDiscoveryIdDocumentUrl, |
| 33 | + apiServerBaseUrl, |
| 34 | + webLoginPageUrl |
| 35 | + ) |
| 36 | + result.success(null) |
| 37 | + } |
| 38 | + "setup" -> { |
| 39 | + val channelId: String = call.argument<String?>("channelId").orEmpty() |
| 40 | + val activity = activity |
| 41 | + if (activity == null) { |
| 42 | + result.error( |
| 43 | + "no_activity_found", |
| 44 | + "There is no valid Activity found to present LINE SDK Login screen.", |
| 45 | + null |
| 46 | + ) |
| 47 | + return |
| 48 | + } |
| 49 | + lineSdkWrapper.setupSdk(activity, channelId) |
| 50 | + result.success(null) |
| 51 | + } |
| 52 | + "login" -> { |
| 53 | + val activity = this.activity |
| 54 | + if (activity == null) { |
| 55 | + result.error( |
| 56 | + "no_activity_found", |
| 57 | + "There is no valid Activity found to present LINE SDK Login screen.", |
| 58 | + null |
| 59 | + ) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + val scopes = call.argument("scopes") ?: emptyList<String>() |
| 64 | + val isWebLogin = call.argument("onlyWebLogin") ?: false |
| 65 | + val botPrompt = call.argument("botPrompt") ?: "normal" |
| 66 | + val loginRequestCode = call.argument<Int?>("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE |
| 67 | + lineSdkWrapper.login( |
| 68 | + loginRequestCode, |
| 69 | + activity, |
| 70 | + scopes = scopes, |
| 71 | + onlyWebLogin = isWebLogin, |
| 72 | + botPromptString = botPrompt, |
| 73 | + result = result |
| 74 | + ) |
| 75 | + } |
| 76 | + "getProfile" -> lineSdkWrapper.getProfile(result) |
| 77 | + "currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result) |
| 78 | + "refreshToken" -> lineSdkWrapper.refreshToken(result) |
| 79 | + "verifyAccessToken" -> lineSdkWrapper.verifyAccessToken(result) |
| 80 | + "getBotFriendshipStatus" -> lineSdkWrapper.getBotFriendshipStatus(result) |
| 81 | + "logout" -> lineSdkWrapper.logout(result) |
| 82 | + else -> result.notImplemented() |
45 | 83 | } |
46 | | - "getProfile" -> lineSdkWrapper.getProfile(result) |
47 | | - "currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result) |
48 | | - "refreshToken" -> lineSdkWrapper.refreshToken(result) |
49 | | - "verifyAccessToken" -> lineSdkWrapper.verifyAccessToken(result) |
50 | | - "getBotFriendshipStatus" -> lineSdkWrapper.getBotFriendshipStatus(result) |
51 | | - "logout" -> lineSdkWrapper.logout(result) |
52 | | - else -> result.notImplemented() |
| 84 | + } |
| 85 | + |
| 86 | + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
| 87 | + onAttachedToEngine(binding.binaryMessenger) |
| 88 | + } |
| 89 | + |
| 90 | + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
| 91 | + methodChannel = null; |
| 92 | + } |
| 93 | + |
| 94 | + override fun onAttachedToActivity(binding: ActivityPluginBinding) { |
| 95 | + bindActivityBinding(binding) |
| 96 | + } |
| 97 | + |
| 98 | + override fun onDetachedFromActivity() { |
| 99 | + unbindActivityBinding() |
| 100 | + } |
| 101 | + |
| 102 | + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { |
| 103 | + bindActivityBinding(binding) |
| 104 | + } |
| 105 | + |
| 106 | + override fun onDetachedFromActivityForConfigChanges() { |
| 107 | + unbindActivityBinding() |
53 | 108 | } |
54 | 109 |
|
55 | 110 | override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean = |
56 | 111 | lineSdkWrapper.handleActivityResult(requestCode, resultCode, intent) |
57 | 112 |
|
| 113 | + private fun bindActivityBinding(binding: ActivityPluginBinding) { |
| 114 | + this.activity = binding.activity |
| 115 | + this.activityBinding = binding |
| 116 | + addActivityResultListener(binding) |
| 117 | + } |
| 118 | + |
| 119 | + private fun unbindActivityBinding() { |
| 120 | + activityBinding?.removeActivityResultListener(this) |
| 121 | + this.activity = null; |
| 122 | + this.activityBinding = null |
| 123 | + } |
| 124 | + |
| 125 | + private fun onAttachedToEngine(messenger: BinaryMessenger) { |
| 126 | + methodChannel = MethodChannel(messenger, CHANNEL_NAME) |
| 127 | + methodChannel!!.setMethodCallHandler(this) |
| 128 | + } |
| 129 | + |
| 130 | + private fun addActivityResultListener(activityBinding: ActivityPluginBinding) { |
| 131 | + activityBinding.addActivityResultListener(this) |
| 132 | + } |
| 133 | + |
| 134 | + private fun addActivityResultListener(registrar: PluginRegistry.Registrar) { |
| 135 | + registrar.addActivityResultListener(this) |
| 136 | + } |
| 137 | + |
58 | 138 | companion object { |
59 | 139 | private const val CHANNEL_NAME = "com.linecorp/flutter_line_sdk" |
60 | 140 | private const val DEFAULT_ACTIVITY_RESULT_REQUEST_CODE = 8192 |
61 | 141 |
|
62 | 142 | @JvmStatic |
63 | 143 | fun registerWith(registrar: PluginRegistry.Registrar) { |
64 | | - val methodChannel = MethodChannel(registrar.messenger(), CHANNEL_NAME) |
65 | | - val lineSdkPlugin = |
66 | | - FlutterLineSdkPlugin(methodChannel, LineSdkWrapper(registrar.activity())) |
67 | | - methodChannel.setMethodCallHandler(lineSdkPlugin) |
68 | | - registrar.addActivityResultListener(lineSdkPlugin) |
| 144 | + FlutterLineSdkPlugin().apply { |
| 145 | + onAttachedToEngine(registrar.messenger()) |
| 146 | + activity = registrar.activity() |
| 147 | + addActivityResultListener(registrar) |
| 148 | + } |
69 | 149 | } |
70 | 150 | } |
71 | 151 | } |
0 commit comments