Skip to content

Commit

Permalink
fix: crash on old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
agronick committed Dec 27, 2023
1 parent b9cb63a commit 8b1eaf2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
19 changes: 16 additions & 3 deletions app/src/main/java/com/aatorque/prefs/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.aatorque.prefs

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.text.InputType
import androidx.lifecycle.lifecycleScope
import androidx.preference.CheckBoxPreference
Expand Down Expand Up @@ -123,8 +125,17 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
mediaBgPref.setOnPreferenceChangeListener { preference, newValue ->
updateDatastorePref {
if (newValue as Boolean && !NotiService.isNotificationAccessEnabled) {
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
if (newValue as Boolean && !NotiService.isNotificationAccessEnabled(requireContext())) {
startActivity(
Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS").apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
putExtra(
Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME,
NotiService::class.java.canonicalName
)
}
}
)
}
it.setAlbumArt(newValue)
}
Expand All @@ -148,6 +159,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
return@setOnPreferenceChangeListener true
}
blurArtPref.isVisible = Build.VERSION.SDK_INT >= 31

numScreensPref.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
Expand All @@ -161,7 +173,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
centerGaugeLargePref.isChecked = it.centerGaugeLarge
rotaryInputPref.isChecked = it.rotaryInput
minMaxBelowPref.isChecked = it.minMaxBelow
mediaBgPref.isChecked = it.albumArt
mediaBgPref.isChecked =
it.albumArt && NotiService.isNotificationAccessEnabled(requireContext())
opacityPref.value = if (it.opacity == 0) 100 else it.opacity
blurArtPref.value = it.blurArt
darkenArtPref.value = it.darkenArt
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/com/aatorque/stats/DashboardFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@ open class DashboardFragment : AlbumArt() {
albumArtReady.countDown()

albumBlurEffect = if (
it.blurArt != 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
it.blurArt != 0 && Build.VERSION.SDK_INT >= 31
) {
val blurFloat = it.blurArt.toFloat()
RenderEffect.createBlurEffect(
blurFloat, blurFloat,
Shader.TileMode.MIRROR
)
try {
RenderEffect.createBlurEffect(
blurFloat, blurFloat,
Shader.TileMode.MIRROR
)
} catch (e: NoClassDefFoundError) {
Timber.i("Version check failed to prevent error ${Build.VERSION.SDK_INT} >= ${Build.VERSION_CODES.S}")
null
}
} else null
albumColorFilter = if (it.darkenArt != 0) {
PorterDuffColorFilter(
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/com/aatorque/stats/NotiService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.aatorque.stats

import android.content.Context
import android.service.notification.NotificationListenerService
import androidx.core.app.NotificationManagerCompat

class NotiService : NotificationListenerService() {
companion object {
var isNotificationAccessEnabled = false
}

override fun onListenerConnected() {
isNotificationAccessEnabled = true
}

override fun onListenerDisconnected() {
isNotificationAccessEnabled = false
fun isNotificationAccessEnabled(context: Context): Boolean {
return NotificationManagerCompat.getEnabledListenerPackages(context)
.contains(BuildConfig.APPLICATION_ID)
}
}
}

0 comments on commit 8b1eaf2

Please sign in to comment.