Last active
August 19, 2020 15:02
-
-
Save hackerwgf/8a5054cd25d3f0e2f77b7384d81f30a9 to your computer and use it in GitHub Desktop.
Help for check canDrawOverlays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.graphics.PixelFormat | |
import android.os.Build | |
import android.os.Handler | |
import android.os.Looper | |
import android.provider.Settings | |
import android.view.View | |
import android.view.WindowManager | |
fun Context.canDrawOverlays(callback: (Boolean) -> Unit) { | |
when { | |
Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(this) -> callback( | |
true | |
) | |
Build.VERSION.SDK_INT == Build.VERSION_CODES.O -> { | |
val handler = Handler(Looper.getMainLooper()) | |
val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager | |
val v = View(applicationContext).apply { | |
addOnAttachStateChangeListener( | |
object : View.OnAttachStateChangeListener { | |
override fun onViewAttachedToWindow(v: View) { | |
handler.removeCallbacksAndMessages(null) | |
callback(true) | |
wm.removeView(v) | |
} | |
override fun onViewDetachedFromWindow(v: View) { | |
} | |
} | |
) | |
} | |
val p = WindowManager.LayoutParams( | |
10, | |
10, | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY else WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, | |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, | |
PixelFormat.TRANSLUCENT | |
) | |
try { | |
wm.addView(v, p) | |
handler.postDelayed({ callback(false) }, 500) | |
} catch (ignore: Exception) { | |
callback(false) | |
} | |
} | |
else -> callback(false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment