-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from line/fix/plugin-integration
Migrate to Android v2 plugin spec
- Loading branch information
Showing
13 changed files
with
162 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 127 additions & 47 deletions
174
android/src/main/kotlin/com/linecorp/flutter_line_sdk/FlutterLineSdkPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,151 @@ | ||
package com.linecorp.flutter_line_sdk | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
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( | ||
private val channel: MethodChannel, | ||
private val lineSdkWrapper: LineSdkWrapper | ||
) : MethodCallHandler, PluginRegistry.ActivityResultListener { | ||
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") ?: "" | ||
lineSdkWrapper.setupBetaConfig( | ||
channelId, | ||
openDiscoveryIdDocumentUrl, | ||
apiServerBaseUrl, | ||
webLoginPageUrl | ||
) | ||
result.success(null) | ||
} | ||
"setup" -> { | ||
val channelId: String = call.argument<String?>("channelId").orEmpty() | ||
lineSdkWrapper.setupSdk(channelId) | ||
result.success(null) | ||
} | ||
"login" -> { | ||
val scopes = call.argument("scopes") ?: emptyList<String>() | ||
val isWebLogin = call.argument("onlyWebLogin") ?: false | ||
val botPrompt = call.argument("botPrompt") ?: "normal" | ||
val loginRequestCode = call.argument<Int?>("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE | ||
lineSdkWrapper.login( | ||
loginRequestCode, | ||
scopes = scopes, | ||
onlyWebLogin = isWebLogin, | ||
botPromptString = botPrompt, | ||
result = result | ||
) | ||
class FlutterLineSdkPlugin : MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware { | ||
|
||
private var methodChannel: MethodChannel? = null | ||
private val lineSdkWrapper = LineSdkWrapper() | ||
|
||
private var activity: Activity? = null | ||
private var activityBinding: ActivityPluginBinding? = null | ||
|
||
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") ?: "" | ||
lineSdkWrapper.setupBetaConfig( | ||
channelId, | ||
openDiscoveryIdDocumentUrl, | ||
apiServerBaseUrl, | ||
webLoginPageUrl | ||
) | ||
result.success(null) | ||
} | ||
"setup" -> { | ||
val channelId: String = call.argument<String?>("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 | ||
} | ||
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<String>() | ||
val isWebLogin = call.argument("onlyWebLogin") ?: false | ||
val botPrompt = call.argument("botPrompt") ?: "normal" | ||
val loginRequestCode = call.argument<Int?>("loginRequestCode") ?: DEFAULT_ACTIVITY_RESULT_REQUEST_CODE | ||
lineSdkWrapper.login( | ||
loginRequestCode, | ||
activity, | ||
scopes = scopes, | ||
onlyWebLogin = isWebLogin, | ||
botPromptString = botPrompt, | ||
result = result | ||
) | ||
} | ||
"getProfile" -> lineSdkWrapper.getProfile(result) | ||
"currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result) | ||
"refreshToken" -> lineSdkWrapper.refreshToken(result) | ||
"verifyAccessToken" -> lineSdkWrapper.verifyAccessToken(result) | ||
"getBotFriendshipStatus" -> lineSdkWrapper.getBotFriendshipStatus(result) | ||
"logout" -> lineSdkWrapper.logout(result) | ||
else -> result.notImplemented() | ||
} | ||
"getProfile" -> lineSdkWrapper.getProfile(result) | ||
"currentAccessToken" -> lineSdkWrapper.getCurrentAccessToken(result) | ||
"refreshToken" -> lineSdkWrapper.refreshToken(result) | ||
"verifyAccessToken" -> lineSdkWrapper.verifyAccessToken(result) | ||
"getBotFriendshipStatus" -> lineSdkWrapper.getBotFriendshipStatus(result) | ||
"logout" -> lineSdkWrapper.logout(result) | ||
else -> result.notImplemented() | ||
} | ||
|
||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
onAttachedToEngine(binding.binaryMessenger) | ||
} | ||
|
||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
methodChannel = null; | ||
} | ||
|
||
override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
bindActivityBinding(binding) | ||
} | ||
|
||
override fun onDetachedFromActivity() { | ||
unbindActivityBinding() | ||
} | ||
|
||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
bindActivityBinding(binding) | ||
} | ||
|
||
override fun onDetachedFromActivityForConfigChanges() { | ||
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) | ||
} | ||
|
||
private fun unbindActivityBinding() { | ||
activityBinding?.removeActivityResultListener(this) | ||
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) | ||
} | ||
|
||
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) { | ||
val methodChannel = MethodChannel(registrar.messenger(), CHANNEL_NAME) | ||
val lineSdkPlugin = | ||
FlutterLineSdkPlugin(methodChannel, LineSdkWrapper(registrar.activity())) | ||
methodChannel.setMethodCallHandler(lineSdkPlugin) | ||
registrar.addActivityResultListener(lineSdkPlugin) | ||
FlutterLineSdkPlugin().apply { | ||
onAttachedToEngine(registrar.messenger()) | ||
activity = registrar.activity() | ||
addActivityResultListener(registrar) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 1 addition & 8 deletions
9
example/android/app/src/main/kotlin/com/linecorp/linesdk/sample/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
package com.linecorp.linesdk.sample | ||
|
||
import android.os.Bundle | ||
|
||
import io.flutter.app.FlutterActivity | ||
import io.flutter.plugins.GeneratedPluginRegistrant | ||
import io.flutter.embedding.android.FlutterActivity | ||
|
||
class MainActivity: FlutterActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
GeneratedPluginRegistrant.registerWith(this) | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
example/android/app/src/main/kotlin/com/linecorp/linesdk/sample/MyApplication.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,3 +158,4 @@ packages: | |
version: "2.0.8" | ||
sdks: | ||
dart: ">=2.9.0-14.0.dev <3.0.0" | ||
flutter: ">=1.12.0 <2.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,3 +144,4 @@ packages: | |
version: "2.0.8" | ||
sdks: | ||
dart: ">=2.9.0-14.0.dev <3.0.0" | ||
flutter: ">=1.12.0 <2.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters