Skip to content
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 @@ -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
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions status-bar-alert/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
// }
// })
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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<StatusBarAlert?> = WeakReference(null)
Expand Down