OK I've made a sample to show all ways to detect it, including what happens when you use the new API (setHideOverlayWindows) to hide apps that are shown on top:
- https://developer.android.com/reference/android/app/Activity#dispatchTouchEvent(android.view.MotionEvent)
- https://developer.android.com/reference/android/view/View#setOnTouchListener(android.view.View.OnTouchListener)
- https://developer.android.com/reference/android/view/View#onFilterTouchEventForSecurity(android.view.MotionEvent)
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lb.detect_floating_app">
<uses-permission android:name="android.permission.HIDE_OVERLAY_WINDOWS" />
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
android:theme="@style/Theme.DetectFloatingApp">
<activity
android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt
class MainActivity : AppCompatActivity() {
var isObscured: Boolean? = null
var isPartiallyObscured: Boolean? = null
var isHidingFloatingApp = false
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<View>(R.id.textView).setOnTouchListener(object : View.OnTouchListener {
var isObscured: Boolean? = null
var isPartiallyObscured: Boolean? = null
override fun onTouch(p0: View?, ev: MotionEvent): Boolean {
val checkIsObscured = ev.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED != 0
val checkIsPartiallyObscured = ev.flags and MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED != 0
if (checkIsObscured != isObscured || checkIsPartiallyObscured != isPartiallyObscured) {
isObscured = checkIsObscured
isPartiallyObscured = checkIsPartiallyObscured
Log.d("AppLog", "${System.currentTimeMillis()} setOnTouchListener isObscured:$isObscured isPartiallyObscured:$isPartiallyObscured")
}
return false
}
})
findViewById<View>(R.id.toggleHideFloatingAppButton).setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
isHidingFloatingApp = !isHidingFloatingApp
window.setHideOverlayWindows(isHidingFloatingApp)
} else
Toast.makeText(this, "need Android API 31", Toast.LENGTH_SHORT).show()
}
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
val checkIsObscured = ev.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED != 0
val checkIsPartiallyObscured = ev.flags and MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED != 0
if (checkIsObscured != isObscured || checkIsPartiallyObscured != isPartiallyObscured) {
isObscured = checkIsObscured
isPartiallyObscured = checkIsPartiallyObscured
Log.d("AppLog", "${System.currentTimeMillis()} dispatchTouchEvent isObscured:$isObscured isPartiallyObscured:$isPartiallyObscured")
}
return super.dispatchTouchEvent(ev)
}
}
FloatingDetectorFrameLayout.kt
class FloatingDetectorFrameLayout : FrameLayout {
var isObscured: Boolean? = null
var isPartiallyObscured: Boolean? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
override fun onFilterTouchEventForSecurity(ev: MotionEvent): Boolean {
val checkIsObscured = ev.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED != 0
val checkIsPartiallyObscured = ev.flags and MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED != 0
if (checkIsObscured != isObscured || checkIsPartiallyObscured != isPartiallyObscured) {
isObscured = checkIsObscured
isPartiallyObscured = checkIsPartiallyObscured
Log.d("AppLog", "${System.currentTimeMillis()} onFilterTouchEventForSecurity isObscured:$isObscured isPartiallyObscured:$isPartiallyObscured")
}
return super.onFilterTouchEventForSecurity(ev)
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"
android:orientation="vertical" tools:context=".MainActivity">
<com.lb.detect_floating_app.FloatingDetectorFrameLayout
android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#f00"
android:padding="100dp">
<Button
android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</com.lb.detect_floating_app.FloatingDetectorFrameLayout>
<Button android:id="@+id/toggleHideFloatingAppButton"
android:text="toggle hide floating app"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>