Skip to content

Commit

Permalink
refactor_wait_for_permission_in_subscribe (#4)
Browse files Browse the repository at this point in the history
* wait for permission inside of subscribe

* upgrade to BootLaces 3.1 in demo and refactor
  • Loading branch information
evilthreads669966 authored Oct 7, 2020
1 parent e11b051 commit d71634d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/com/evilthreads/drawersniffer/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
}
}

class MyService: LifecycleBootService()
18 changes: 0 additions & 18 deletions app/src/main/java/com/evilthreads/drawersniffer/MyService.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d71634d

Please sign in to comment.