From 90348b7514186a7ec8f6a000438abd6dcbe1251d Mon Sep 17 00:00:00 2001 From: Kamil Chmiel Date: Mon, 24 Feb 2020 11:39:00 +0100 Subject: [PATCH] Fix issuses with Android plugin after Flutter update to 1.12 --- .../FlutterStatusbarcolorPlugin.kt | 68 ++++++++++++++----- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/android/src/main/kotlin/com/fuyumi/flutterstatusbarcolor/flutterstatusbarcolor/FlutterStatusbarcolorPlugin.kt b/android/src/main/kotlin/com/fuyumi/flutterstatusbarcolor/flutterstatusbarcolor/FlutterStatusbarcolorPlugin.kt index c7dd7db..e07ca7d 100644 --- a/android/src/main/kotlin/com/fuyumi/flutterstatusbarcolor/flutterstatusbarcolor/FlutterStatusbarcolorPlugin.kt +++ b/android/src/main/kotlin/com/fuyumi/flutterstatusbarcolor/flutterstatusbarcolor/FlutterStatusbarcolorPlugin.kt @@ -1,21 +1,57 @@ package com.fuyumi.flutterstatusbarcolor.flutterstatusbarcolor import android.os.Build -import android.app.Activity import android.view.View import android.animation.ValueAnimator +import io.flutter.embedding.engine.plugins.FlutterPlugin 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.MethodCall import io.flutter.plugin.common.PluginRegistry.Registrar +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding +import io.flutter.embedding.engine.plugins.activity.ActivityAware +import androidx.annotation.NonNull + +class FlutterStatusbarcolorPlugin: MethodCallHandler, FlutterPlugin, ActivityAware { + + private var activity: android.app.Activity? = null + lateinit var myplugin: FlutterStatusbarcolorPlugin + + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + myplugin = FlutterStatusbarcolorPlugin() + val channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "plugins.fuyumi.com/statusbar") + channel.setMethodCallHandler(myplugin) + } + + override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { + } + + override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) { + myplugin.activity = activityPluginBinding.getActivity() + } + + override fun onDetachedFromActivityForConfigChanges() { + } + + override fun onReattachedToActivityForConfigChanges(activityPluginBinding: ActivityPluginBinding) { + } + + override fun onDetachedFromActivity() { + } -class FlutterStatusbarcolorPlugin private constructor(private val activity: Activity?) : MethodCallHandler { companion object { @JvmStatic fun registerWith(registrar: Registrar): Unit { val channel = MethodChannel(registrar.messenger(), "plugins.fuyumi.com/statusbar") - channel.setMethodCallHandler(FlutterStatusbarcolorPlugin(registrar.activity())) + val plugin = FlutterStatusbarcolorPlugin() + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + { + plugin.activity = registrar.activity() + } + + channel.setMethodCallHandler(FlutterStatusbarcolorPlugin()) } } @@ -26,7 +62,7 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti "getstatusbarcolor" -> { var statusBarColor: Int = 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - statusBarColor = activity.window.statusBarColor + statusBarColor = activity!!.window.statusBarColor } result.success(statusBarColor) } @@ -35,12 +71,12 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti val animate: Boolean = call.argument("animate")!! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (animate) { - val colorAnim = ValueAnimator.ofArgb(activity.window.statusBarColor, statusBarColor) - colorAnim.addUpdateListener { anim -> activity.window.statusBarColor = anim.animatedValue as Int } + val colorAnim = ValueAnimator.ofArgb(activity!!.window.statusBarColor, statusBarColor) + colorAnim.addUpdateListener { anim -> activity!!.window.statusBarColor = anim.animatedValue as Int } colorAnim.setDuration(300) colorAnim.start() } else { - activity.window.statusBarColor = statusBarColor + activity!!.window.statusBarColor = statusBarColor } } result.success(null) @@ -49,9 +85,9 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti val usewhiteforeground: Boolean = call.argument("whiteForeground")!! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (usewhiteforeground) { - activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() + activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() } else { - activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR + activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR } } result.success(null) @@ -59,7 +95,7 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti "getnavigationbarcolor" -> { var navigationBarColor: Int = 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - navigationBarColor = activity.window.navigationBarColor + navigationBarColor = activity!!.window.navigationBarColor } result.success(navigationBarColor) } @@ -68,12 +104,12 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti val animate: Boolean = call.argument("animate")!! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (animate) { - val colorAnim = ValueAnimator.ofArgb(activity.window.navigationBarColor, navigationBarColor) - colorAnim.addUpdateListener { anim -> activity.window.navigationBarColor = anim.animatedValue as Int } + val colorAnim = ValueAnimator.ofArgb(activity!!.window.navigationBarColor, navigationBarColor) + colorAnim.addUpdateListener { anim -> activity!!.window.navigationBarColor = anim.animatedValue as Int } colorAnim.setDuration(300) colorAnim.start() } else { - activity.window.navigationBarColor = navigationBarColor + activity!!.window.navigationBarColor = navigationBarColor } } result.success(null) @@ -82,9 +118,9 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti val usewhiteforeground: Boolean = call.argument("whiteForeground")!! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (usewhiteforeground) { - activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv() + activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv() } else { - activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR + activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR } } result.success(null) @@ -92,4 +128,4 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti else -> result.notImplemented() } } -} +} \ No newline at end of file