From d71634d84c3c26b6ef3c286407ee3c1e64481d5f Mon Sep 17 00:00:00 2001 From: Chris Basinger Date: Tue, 6 Oct 2020 20:21:07 -0500 Subject: [PATCH] refactor_wait_for_permission_in_subscribe (#4) * wait for permission inside of subscribe * upgrade to BootLaces 3.1 in demo and refactor --- app/build.gradle | 2 +- .../evilthreads/drawersniffer/MainActivity.kt | 15 ++++++++++++--- .../com/evilthreads/drawersniffer/MyService.kt | 18 ------------------ .../drawersnifferlib/DrawerSniffer.kt | 6 +++++- 4 files changed, 18 insertions(+), 23 deletions(-) delete mode 100644 app/src/main/java/com/evilthreads/drawersniffer/MyService.kt diff --git a/app/build.gradle b/app/build.gradle index e2ae468..71c9234 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,7 +30,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' - implementation 'com.github.evilthreads669966:bootlaces:2.1' + implementation 'com.github.evilthreads669966:bootlaces:3.1' implementation "androidx.lifecycle:lifecycle-service:2.2.0" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0" implementation project(":drawersnifferlib") diff --git a/app/src/main/java/com/evilthreads/drawersniffer/MainActivity.kt b/app/src/main/java/com/evilthreads/drawersniffer/MainActivity.kt index c19669b..f80f58a 100644 --- a/app/src/main/java/com/evilthreads/drawersniffer/MainActivity.kt +++ b/app/src/main/java/com/evilthreads/drawersniffer/MainActivity.kt @@ -1,7 +1,9 @@ package com.evilthreads.drawersniffer -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.util.Log +import androidx.appcompat.app.AppCompatActivity +import com.candroid.bootlaces.LifecycleBootService import com.candroid.bootlaces.bootService import com.evilthreads.drawersnifferlib.DrawerSniffer import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -13,8 +15,15 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) if(!DrawerSniffer.hasPermission(this)) DrawerSniffer.requestPermission(this) - bootService(this){ + val payload = suspend{ + DrawerSniffer.subscribe(this){ notif -> + Log.d("DRAWER SNIFFER", notif.toString()) + } + } + bootService(this, payload){ service = MyService::class } } -} \ No newline at end of file +} + +class MyService: LifecycleBootService() \ No newline at end of file diff --git a/app/src/main/java/com/evilthreads/drawersniffer/MyService.kt b/app/src/main/java/com/evilthreads/drawersniffer/MyService.kt deleted file mode 100644 index 36612fe..0000000 --- a/app/src/main/java/com/evilthreads/drawersniffer/MyService.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.evilthreads.drawersniffer - -import android.util.Log -import androidx.lifecycle.lifecycleScope -import com.candroid.bootlaces.LifecycleBootService -import com.evilthreads.drawersnifferlib.DrawerSniffer -import kotlinx.coroutines.ExperimentalCoroutinesApi - -@ExperimentalCoroutinesApi -class MyService: LifecycleBootService(){ - init { - lifecycleScope.launchWhenCreated { - DrawerSniffer.subscribe{ notif -> - Log.d("DRAWER SNIFFER", notif.toString()) - } - } - } -} \ No newline at end of file diff --git a/drawersnifferlib/src/main/java/com/evilthreads/drawersnifferlib/DrawerSniffer.kt b/drawersnifferlib/src/main/java/com/evilthreads/drawersnifferlib/DrawerSniffer.kt index aebff8c..01f04a0 100644 --- a/drawersnifferlib/src/main/java/com/evilthreads/drawersnifferlib/DrawerSniffer.kt +++ b/drawersnifferlib/src/main/java/com/evilthreads/drawersnifferlib/DrawerSniffer.kt @@ -53,7 +53,11 @@ class DrawerSniffer{ /*subscribe to notification events*/ /*use this method to receive notifications as they are posted. Write the code in the consume block to handle each notification as it is intercepted*/ @ExperimentalCoroutinesApi - suspend fun subscribe(consume: (InterceptedNotification) -> Unit) = channel.consumeEach { notification -> consume(notification) } + suspend fun subscribe(ctx: Context, consume: (InterceptedNotification) -> Unit){ + while(!hasPermission(ctx)) + delay(1000) + channel.consumeEach { notification -> consume(notification) } + } /*checks whether the user has enabled notification listener services for you app in the notification services settings screen*/ fun hasPermission(ctx: Context): Boolean {