Skip to content

Commit

Permalink
[feat]: disabling separate notification channels
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Jul 28, 2024
1 parent 056a598 commit c613b8e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class AppPreferences @Inject constructor(
get() = get("pref_include_device_info_in_archives", true)
set(value) { put("pref_include_device_info_in_archives", value) }

var useSeparateNotificationsChannelsForCrashes
get() = get("pref_notifications_use_separate_channels", true)
set(value) { put("pref_notifications_use_separate_channels", value) }

var askedNotificationsPermission
get() = get("pref_asked_notifications_permission", false)
set(value) { put("pref_asked_notifications_permission", value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.f0x1d.logfox.intents.COPY_CRASH_INTENT_ID
import com.f0x1d.logfox.intents.makeBroadcastPendingIntent
import com.f0x1d.logfox.navigation.Directions
import com.f0x1d.logfox.navigation.NavGraphs
import com.f0x1d.logfox.preferences.shared.AppPreferences
import com.f0x1d.logfox.strings.Strings
import com.f0x1d.logfox.ui.Icons
import dagger.hilt.android.qualifiers.ApplicationContext
Expand All @@ -33,6 +34,7 @@ internal interface CrashesNotificationsController {
@SuppressLint("MissingPermission")
internal class CrashesNotificationsControllerImpl @Inject constructor(
@ApplicationContext private val context: Context,
private val appPreferences: AppPreferences,
) : CrashesNotificationsController {

override fun sendErrorNotification(appCrash: AppCrash, crashLog: String?) {
Expand All @@ -42,7 +44,14 @@ internal class CrashesNotificationsControllerImpl @Inject constructor(
notify(
appCrash.packageName,
appCrash.notificationId,
NotificationCompat.Builder(context, appCrash.notificationChannelId)
NotificationCompat.Builder(
context,
if (appPreferences.useSeparateNotificationsChannelsForCrashes) {
appCrash.notificationChannelId
} else {
CRASHES_CHANNEL_ID
},
)
.setContentTitle(
context.getString(
Strings.app_crashed,
Expand Down Expand Up @@ -80,26 +89,41 @@ internal class CrashesNotificationsControllerImpl @Inject constructor(
}

private fun createNotificationChannelFor(appCrash: AppCrash) {
val crashTypeName = appCrash.crashType.readableName
val groupId = "${CRASHES_CHANNEL_GROUP_ID}_$crashTypeName"
if (appPreferences.useSeparateNotificationsChannelsForCrashes) {
val crashTypeName = appCrash.crashType.readableName
val groupId = "${CRASHES_CHANNEL_GROUP_ID}_$crashTypeName"

val crashesGroup = NotificationChannelGroupCompat.Builder(groupId)
.setName(context.getString(Strings.type_crashes, crashTypeName))
.build()
val crashesGroup = NotificationChannelGroupCompat.Builder(groupId)
.setName(context.getString(Strings.type_crashes, crashTypeName))
.build()

val appCrashesChannel = NotificationChannelCompat.Builder(
appCrash.notificationChannelId,
NotificationManagerCompat.IMPORTANCE_HIGH,
)
.setName(context.getString(Strings.crashes_of, crashTypeName, appCrash.packageName))
.setLightsEnabled(true)
.setVibrationEnabled(true)
.setGroup(groupId)
.build()
val appCrashesChannel = NotificationChannelCompat.Builder(
appCrash.notificationChannelId,
NotificationManagerCompat.IMPORTANCE_HIGH,
)
.setName(context.getString(Strings.crashes_of, crashTypeName, appCrash.packageName))
.setLightsEnabled(true)
.setVibrationEnabled(true)
.setGroup(groupId)
.build()

context.notificationManagerCompat.apply {
createNotificationChannelGroupsCompat(listOf(crashesGroup))
createNotificationChannelsCompat(listOf(appCrashesChannel))
}
} else {
val crashesChannel = NotificationChannelCompat.Builder(
CRASHES_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_HIGH,
)
.setName(context.getString(Strings.crashes))
.setLightsEnabled(true)
.setVibrationEnabled(true)
.build()

context.notificationManagerCompat.apply {
createNotificationChannelGroupsCompat(listOf(crashesGroup))
createNotificationChannelsCompat(listOf(appCrashesChannel))
context.notificationManagerCompat.apply {
createNotificationChannelsCompat(listOf(crashesChannel))
}
}
}

Expand All @@ -115,6 +139,10 @@ internal class CrashesNotificationsControllerImpl @Inject constructor(
if (it.tag != null && it.tag.contains(".")) cancel(it.tag, it.id)
}
}

companion object {
private const val CRASHES_CHANNEL_ID = "crashes"
}
}

val AppCrash.notificationChannelId get() = "${CRASHES_CHANNEL_GROUP_ID}_${crashType.readableName}_$packageName"
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class CrashDetailsFragment: BaseViewModelFragment<CrashDetailsViewModel, Fragmen
}.let(::startActivity)
}

findItem(R.id.notifications_item).setVisible(notificationsChannelsAvailable)
findItem(R.id.notifications_item).setVisible(
notificationsChannelsAvailable
&& viewModel.appPreferences.useSeparateNotificationsChannelsForCrashes
)
setClickListenerOn(R.id.notifications_item) {
Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CrashDetailsViewModel @Inject constructor(
@CrashId val crashId: Long,
val dateTimeFormatter: DateTimeFormatter,
private val crashesRepository: CrashesRepository,
private val appPreferences: AppPreferences,
val appPreferences: AppPreferences,
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
application: Application,
): BaseViewModel(application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
android:selectable="false"
app:iconSpaceReserved="false" />

<SwitchPreferenceCompat
android:title="@string/use_separate_channels_for_crashes"
android:key="pref_notifications_use_separate_channels"
android:defaultValue="true"
app:singleLineTitle="false"
app:widgetLayout="@layout/preference_material_switch"
app:iconSpaceReserved="false" />

<SwitchPreferenceCompat
android:title="@string/show_java_crashes_notifications"
android:key="pref_notifications_java"
Expand Down
1 change: 1 addition & 0 deletions strings/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,5 @@
<string name="sort_by_name">По имени</string>
<string name="sort_by_new">По новизне</string>
<string name="sort_by_count">По количеству сбоев</string>
<string name="use_separate_channels_for_crashes">Использовать разные каналы уведомлений для уведомлений о сбоях</string>
</resources>
1 change: 1 addition & 0 deletions strings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@
<string name="sort_by_name">By name</string>
<string name="sort_by_new">By novelty</string>
<string name="sort_by_count">By crashes count</string>
<string name="use_separate_channels_for_crashes">Use separate notifications channels for crashes notifications</string>
</resources>

0 comments on commit c613b8e

Please sign in to comment.