Skip to content

Commit

Permalink
fix: ensure app is correctly working again at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jul 12, 2024
1 parent 21be54a commit 8e09050
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
24 changes: 2 additions & 22 deletions app/src/main/java/io/github/jd1378/otphelper/App.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.github.jd1378.otphelper

import android.app.Application
import android.content.ComponentName
import android.content.pm.PackageManager
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import dagger.hilt.android.HiltAndroidApp
import io.github.jd1378.otphelper.NotificationListener.Companion.isNotificationServiceEnabled
import io.github.jd1378.otphelper.NotificationListener.Companion.tryReEnableNotificationListener
import javax.inject.Inject

@HiltAndroidApp
Expand All @@ -19,24 +17,6 @@ class App : Application(), Configuration.Provider {
override fun onCreate() {
super.onCreate()

if (isNotificationServiceEnabled(applicationContext)) {
// Rebind the service if it's already enabled
val componentName =
ComponentName(
this,
NotificationListener::class.java,
)
val pm = packageManager
pm.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
)
pm.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP,
)
}
tryReEnableNotificationListener(applicationContext)
}
}
17 changes: 13 additions & 4 deletions app/src/main/java/io/github/jd1378/otphelper/BootReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationManagerCompat
import android.util.Log
import io.github.jd1378.otphelper.NotificationListener.Companion.isNotificationListenerServiceEnabled
import io.github.jd1378.otphelper.NotificationListener.Companion.tryReEnableNotificationListener
import io.github.jd1378.otphelper.utils.NotificationHelper

class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
if (NotificationManagerCompat.getEnabledListenerPackages(context)
.contains(context.packageName)) {
context.startService(Intent(context, NotificationListener::class.java))
if (isNotificationListenerServiceEnabled(context)) {
try {
context.startService(Intent(context, NotificationListener::class.java))
} catch (e: Throwable) {
Log.e(
"BootReceiver",
"Failed to start NotificationListener",
)
}
tryReEnableNotificationListener(context)
}
when (Build.VERSION.SDK_INT) {
Build.VERSION_CODES.Q,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package io.github.jd1378.otphelper
import android.app.Notification
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
Expand Down Expand Up @@ -35,25 +37,39 @@ class NotificationListener : NotificationListenerService() {
)
val notification_text_arrays_keys = listOf(Notification.EXTRA_TEXT_LINES)

fun isNotificationServiceEnabled(context: Context): Boolean {
val pkgName = context.packageName
val flat =
Settings.Secure.getString(context.contentResolver, "enabled_notification_listeners")
if (!flat.isNullOrEmpty()) {
val names = flat.split(":").toTypedArray()
for (name in names) {
val componentName = ComponentName.unflattenFromString(name)
if (componentName != null) {
if (pkgName == componentName.packageName) {
return true
}
}
}
fun isNotificationListenerServiceEnabled(context: Context): Boolean {
return NotificationManagerCompat.getEnabledListenerPackages(context)
.contains(context.packageName)
}

fun tryReEnableNotificationListener(context: Context) {
if (isNotificationListenerServiceEnabled(context)) {
// Rebind the service if it's already enabled
val componentName =
ComponentName(
context,
NotificationListener::class.java,
)
val pm = context.packageManager
pm.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
)
pm.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP,
)
}
return false
}
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
return START_STICKY
}

override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
autoUpdatingListenerUtils.awaitCodeExtractor()
Expand Down Expand Up @@ -142,7 +158,7 @@ class NotificationListener : NotificationListenerService() {
// Handle the listener disconnected event
Log.i(TAG, "Notification listener disconnected.")

if (isNotificationServiceEnabled(applicationContext)) {
if (isNotificationListenerServiceEnabled(applicationContext)) {
Log.d(TAG, "Rebinding to the service")
val componentName =
ComponentName(
Expand Down

0 comments on commit 8e09050

Please sign in to comment.