From 6ee034078241b76dbb0ab2627d7983551b26a34e Mon Sep 17 00:00:00 2001 From: GibsonRuitiari Date: Thu, 29 Dec 2022 11:49:23 +0300 Subject: [PATCH] added backward compatibility by using WindowInsetsControllerCompat api + refactored deprecated code --- .../statusbaralert/app/MainActivity.kt | 3 +- status-bar-alert/build.gradle | 6 +- .../fede987/statusbaralert/StatusBarAlert.kt | 64 ++++++++++++------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/fede987/statusbaralert/app/MainActivity.kt b/app/src/main/java/com/fede987/statusbaralert/app/MainActivity.kt index 8e1ce23..b351b29 100644 --- a/app/src/main/java/com/fede987/statusbaralert/app/MainActivity.kt +++ b/app/src/main/java/com/fede987/statusbaralert/app/MainActivity.kt @@ -5,6 +5,7 @@ import android.graphics.Typeface import android.os.Build import android.os.Bundle import android.os.Handler +import android.os.Looper import android.view.View import androidx.appcompat.app.AppCompatActivity import com.fede987.statusbaralert.StatusBarAlert @@ -19,7 +20,7 @@ import java.util.concurrent.TimeUnit class MainActivity : AppCompatActivity() { var typeface: Typeface? = null - val handler: Handler = Handler() + val handler: Handler = Handler(Looper.getMainLooper()) var alert1: StatusBarAlert? = null var alert2: StatusBarAlert? = null diff --git a/status-bar-alert/build.gradle b/status-bar-alert/build.gradle index 4bac5cd..a668b34 100644 --- a/status-bar-alert/build.gradle +++ b/status-bar-alert/build.gradle @@ -6,11 +6,11 @@ apply plugin: 'com.github.dcendents.android-maven' group = 'com.github.fede987' android { - compileSdkVersion 29 + compileSdkVersion 30 defaultConfig { minSdkVersion 14 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 200 versionName "2.0.0" @@ -30,7 +30,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.appcompat:appcompat:1.5.1' testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' diff --git a/status-bar-alert/src/main/java/com/fede987/statusbaralert/StatusBarAlert.kt b/status-bar-alert/src/main/java/com/fede987/statusbaralert/StatusBarAlert.kt index 996a7fc..87ac366 100644 --- a/status-bar-alert/src/main/java/com/fede987/statusbaralert/StatusBarAlert.kt +++ b/status-bar-alert/src/main/java/com/fede987/statusbaralert/StatusBarAlert.kt @@ -22,11 +22,12 @@ import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.LinearLayoutCompat import androidx.core.graphics.BlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat +import androidx.core.view.WindowCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.WindowInsetsControllerCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleObserver -import androidx.lifecycle.OnLifecycleEvent +import androidx.lifecycle.* import com.fede987.statusbaralert.utils.convertDpToPixel import com.fede987.statusbaralert.utils.getStatusBarHeight import com.fede987.statusbaralert.utils.getColorSafe @@ -69,14 +70,21 @@ class StatusBarAlert @JvmOverloads internal constructor( private fun observeLifecycle() { if (activity is AppCompatActivity) { - activity.lifecycle.addObserver(object : LifecycleObserver { - - @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) - fun destroy() { + activity.lifecycle.addObserver(object: DefaultLifecycleObserver{ + override fun onDestroy(owner: LifecycleOwner) { hide() activity.lifecycle.removeObserver(this) } }) + // using the annotation leads to reflection/code gen which is undesirable +// activity.lifecycle.addObserver(object : LifecycleObserver { +// +// @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) +// fun destroy() { +// hide() +// activity.lifecycle.removeObserver(this) +// } +// }) } } @@ -172,14 +180,22 @@ class StatusBarAlert @JvmOverloads internal constructor( private fun showInternal() { currentInstance = WeakReference(this) - val lowProfileSystemUIVisibility = decorView?.systemUiVisibility - ?.or(View.SYSTEM_UI_FLAG_LOW_PROFILE) - ?: View.SYSTEM_UI_FLAG_LOW_PROFILE - decorView?.systemUiVisibility = lowProfileSystemUIVisibility - decorView?.setOnSystemUiVisibilityChangeListener { _ -> - this.systemUiVisibility = lowProfileSystemUIVisibility +// val lowProfileSystemUIVisibility = decorView?.systemUiVisibility +// ?.or(View.SYSTEM_UI_FLAG_LOW_PROFILE) +// ?: View.SYSTEM_UI_FLAG_LOW_PROFILE +// decorView?.systemUiVisibility = lowProfileSystemUIVisibility +// decorView?.setOnSystemUiVisibilityChangeListener { _ -> +// this.systemUiVisibility = lowProfileSystemUIVisibility +// } + //backward and forward compatibility is ensured using [WindowInsetsControllerCompat] api + decorView?.let {decor-> + WindowCompat.setDecorFitsSystemWindows(activity.window,false) // signal to the system that we will handle + // the insets ourselves + WindowInsetsControllerCompat(activity.window,decor).apply { + hide(WindowInsetsCompat.Type.statusBars()) + systemBarsBehavior =WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + } } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { statusBarColorOriginal = activity.window.statusBarColor } @@ -205,10 +221,17 @@ class StatusBarAlert @JvmOverloads internal constructor( } slideUp { + decorView?.post { - decorView?.systemUiVisibility = decorView?.systemUiVisibility - ?.and(View.SYSTEM_UI_FLAG_LOW_PROFILE.inv()) - ?: View.SYSTEM_UI_FLAG_LOW_PROFILE.inv() + //backward and forward compatibility is ensured using [WindowInsetsControllerCompat] api + WindowCompat.setDecorFitsSystemWindows(activity.window,true) // signal to the system that it should + // handle insets for us + WindowInsetsControllerCompat(activity.window, decorView!!).apply { + show(WindowInsetsCompat.Type.statusBars()) + } +// decorView?.systemUiVisibility = decorView?.systemUiVisibility +// ?.and(View.SYSTEM_UI_FLAG_LOW_PROFILE.inv()) +// ?: View.SYSTEM_UI_FLAG_LOW_PROFILE.inv() (decorView as? ViewGroup?)?.removeView(this) } currentInstance.clear() @@ -321,7 +344,7 @@ class StatusBarAlert @JvmOverloads internal constructor( /** * Enable or disable autoHide after StatusBarAlert has been shown. * - * @param hide whether or not to hide it automatically + * @param hide whether to hide it automatically * @return Builder */ fun setAutoHide(autoHide: Boolean) { @@ -338,10 +361,7 @@ class StatusBarAlert @JvmOverloads internal constructor( textView?.typeface = typeface } - override fun onDetachedFromWindow() { - activity.window?.decorView?.setOnSystemUiVisibilityChangeListener(null) - super.onDetachedFromWindow() - } + companion object { private var currentInstance: WeakReference = WeakReference(null)