Skip to content

Migrate from registrar #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,61 @@ package com.linusu.flutter_web_auth
import android.content.Context
import android.content.Intent
import android.net.Uri

import androidx.browser.customtabs.CustomTabsIntent

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
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.Registrar

class FlutterWebAuthPlugin(private var context: Context? = null, private var channel: MethodChannel? = null): MethodCallHandler, FlutterPlugin {
companion object {
val callbacks = mutableMapOf<String, Result>()
class FlutterWebAuthPlugin : MethodCallHandler, FlutterPlugin {
private var context: Context? = null
private lateinit var channel: MethodChannel

@JvmStatic
fun registerWith(registrar: Registrar) {
val plugin = FlutterWebAuthPlugin()
plugin.initInstance(registrar.messenger(), registrar.context())
companion object {
val callbacks = mutableMapOf<String, Result>()
}

}

fun initInstance(messenger: BinaryMessenger, context: Context) {
this.context = context
channel = MethodChannel(messenger, "flutter_web_auth")
channel?.setMethodCallHandler(this)
}

override public fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
initInstance(binding.getBinaryMessenger(), binding.getApplicationContext())
}

override public fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
context = null
channel = null
}

override fun onMethodCall(call: MethodCall, resultCallback: Result) {
when (call.method) {
"authenticate" -> {
val url = Uri.parse(call.argument("url"))
val callbackUrlScheme = call.argument<String>("callbackUrlScheme")!!
val preferEphemeral = call.argument<Boolean>("preferEphemeral")!!

callbacks[callbackUrlScheme] = resultCallback

val intent = CustomTabsIntent.Builder().build()
val keepAliveIntent = Intent(context, KeepAliveService::class.java)
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
context = binding.applicationContext
channel = MethodChannel(binding.binaryMessenger, "flutter_web_auth")
channel.setMethodCallHandler(this)
}

intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
if (preferEphemeral) {
intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
}
intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent)
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
channel = null as MethodChannel

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assigning null to channel causes a NPE

I was able to address the NPE with this change: greensopinion@f28e5a2

context = null
}

intent.launchUrl(context!!, url)
}
"cleanUpDanglingCalls" -> {
callbacks.forEach{ (_, danglingResultCallback) ->
danglingResultCallback.error("CANCELED", "User canceled login", null)
}
callbacks.clear()
resultCallback.success(null)
override fun onMethodCall(call: MethodCall, resultCallback: Result) {
when (call.method) {
"authenticate" -> {
val url = Uri.parse(call.argument<String>("url"))
val callbackUrlScheme = call.argument<String>("callbackUrlScheme")!!
val preferEphemeral = call.argument<Boolean>("preferEphemeral") ?: false

callbacks[callbackUrlScheme] = resultCallback

val intent = CustomTabsIntent.Builder().build()
val keepAliveIntent = Intent(context, KeepAliveService::class.java)

intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
if (preferEphemeral) {
intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
}
intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent)

intent.launchUrl(context!!, url)
}
"cleanUpDanglingCalls" -> {
callbacks.forEach { (_, danglingResultCallback) ->
danglingResultCallback.error("CANCELED", "User canceled login", null)
}
callbacks.clear()
resultCallback.success(null)
}
else -> resultCallback.notImplemented()
}
else -> resultCallback.notImplemented()
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_web_auth
description: Flutter plugin for authenticating a user with a web service.
version: 0.6.0
version: 0.7.0
homepage: https://github.com/LinusU/flutter_web_auth

environment:
Expand Down