85 lines
2.8 KiB
Kotlin
85 lines
2.8 KiB
Kotlin
package com.openless.app
|
|
|
|
import android.app.Activity
|
|
import android.app.Application
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.provider.Settings
|
|
import android.util.Log
|
|
|
|
/**
|
|
* Registers activity lifecycle hooks for overlay background trigger mode.
|
|
*/
|
|
class OpenLessApplication : Application() {
|
|
override fun onCreate() {
|
|
super.onCreate()
|
|
OpenLessAppContext.initialize(this)
|
|
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
|
|
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit
|
|
override fun onActivityStarted(activity: Activity) {
|
|
if (activity.javaClass.name.endsWith("MainActivity")) {
|
|
maybeHideOverlayOnForeground()
|
|
}
|
|
}
|
|
override fun onActivityResumed(activity: Activity) = Unit
|
|
override fun onActivityPaused(activity: Activity) = Unit
|
|
override fun onActivityStopped(activity: Activity) {
|
|
if (activity.javaClass.name.endsWith("MainActivity")) {
|
|
maybeShowOverlayOnBackground()
|
|
}
|
|
}
|
|
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit
|
|
override fun onActivityDestroyed(activity: Activity) = Unit
|
|
})
|
|
}
|
|
|
|
private fun maybeShowOverlayOnBackground() {
|
|
val configured = configuredOverlayTriggerMode()
|
|
val shouldShow = configured == "background" ||
|
|
configured == "always"
|
|
if (!shouldShow) {
|
|
return
|
|
}
|
|
if (!canDrawOverlays()) {
|
|
return
|
|
}
|
|
sendOverlayAction(OpenLessOverlayService.ACTION_SHOW)
|
|
}
|
|
|
|
private fun maybeHideOverlayOnForeground() {
|
|
if (configuredOverlayTriggerMode() == "always") {
|
|
if (canDrawOverlays()) {
|
|
sendOverlayAction(OpenLessOverlayService.ACTION_SHOW)
|
|
}
|
|
return
|
|
}
|
|
sendOverlayAction(OpenLessOverlayService.ACTION_HIDE)
|
|
}
|
|
|
|
private fun configuredOverlayTriggerMode(): String {
|
|
return OpenLessAndroidPreferences.overlayTriggerMode(this) ?: "background"
|
|
}
|
|
|
|
private fun canDrawOverlays(): Boolean {
|
|
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
Settings.canDrawOverlays(this)
|
|
} else {
|
|
true
|
|
}
|
|
}
|
|
|
|
private fun sendOverlayAction(action: String) {
|
|
try {
|
|
startService(Intent(this, OpenLessOverlayService::class.java).apply {
|
|
this.action = action
|
|
})
|
|
} catch (error: Throwable) {
|
|
Log.w(TAG, "overlay action failed: $action", error)
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
private const val TAG = "OpenLessApplication"
|
|
}
|
|
}
|