Skip to content

Commit

Permalink
Add option to remove all custom notification channels (#3549)
Browse files Browse the repository at this point in the history
Closes #3531

Signed-off-by: mueller-ma <mueller-ma@users.noreply.github.com>
  • Loading branch information
mueller-ma committed Jan 9, 2024
1 parent 0a6451c commit 24b5894
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class PreferencesActivity : AbstractBaseActivity() {
const val ITEM_UPDATE_WIDGET_WIDGET_LABEL = "widgetLabel"
const val ITEM_UPDATE_WIDGET_MAPPED_STATE = "mappedState"
const val ITEM_UPDATE_WIDGET_ICON = "icon"
const val ITEM_UPDATE_WIDGET_THEME = "theme"
const val ITEM_UPDATE_WIDGET_SHOW_STATE = "show_state"
private const val STATE_KEY_RESULT = "result"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class MainSettingsFragment : AbstractSettingsFragment(), ConnectionFactory.Updat
val iconFormatPref = getPreference(PrefKeys.ICON_FORMAT)
val ringtonePref = getPreference(PrefKeys.NOTIFICATION_TONE)
val vibrationPref = getPreference(PrefKeys.NOTIFICATION_VIBRATION)
val ringtoneVibrationPref = getPreference(PrefKeys.NOTIFICATION_TONE_VIBRATION)
val viewLogPref = getPreference(PrefKeys.LOG)
val screenLockPref = getPreference(PrefKeys.SCREEN_LOCK)
val tilePref = getPreference(PrefKeys.SUBSCREEN_TILE)
Expand Down Expand Up @@ -240,7 +239,7 @@ class MainSettingsFragment : AbstractSettingsFragment(), ConnectionFactory.Updat
preferenceScreen.removePreferenceRecursively(PrefKeys.NOTIFICATION_TONE)
preferenceScreen.removePreferenceRecursively(PrefKeys.NOTIFICATION_VIBRATION)

ringtoneVibrationPref.setOnPreferenceClickListener { pref ->
getPreference(PrefKeys.NOTIFICATION_TONE_VIBRATION).setOnPreferenceClickListener { pref ->
val i = Intent(Settings.ACTION_SETTINGS).apply {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra(Settings.EXTRA_APP_PACKAGE, pref.context.packageName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.openhab.habdroid.ui.preference.widgets

import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.widget.ImageView
import androidx.appcompat.widget.TooltipCompat
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.openhab.habdroid.R

class NotificationChannelPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
private var helpIcon: ImageView? = null

init {
widgetLayoutResource = R.layout.help_icon_pref
}

override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}

helpIcon = holder.itemView.findViewById(R.id.help_icon)
helpIcon?.apply {
val contentDescription = context.getString(R.string.click_here_for_more_information)
this.contentDescription = contentDescription
TooltipCompat.setTooltipText(this, contentDescription)

setOnClickListener {
MaterialAlertDialogBuilder(context)
.setMessage(R.string.settings_notification_hint)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(R.string.settings_notification_reset_settings) { _, _ ->
MaterialAlertDialogBuilder(context)
.setMessage(R.string.settings_notification_reset_settings_confirm)
.setPositiveButton(android.R.string.ok) { _, _ ->
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nm.notificationChannels
.filter { it.id.startsWith("severity-") }
.forEach {
nm.deleteNotificationChannel(it.id)
}
}
.setNegativeButton(android.R.string.cancel, null)
.show()
}
.show()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import androidx.preference.PreferenceViewHolder
import org.openhab.habdroid.R
import org.openhab.habdroid.ui.setupHelpIcon

class PrimaryServerPreference constructor(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
class PrimaryServerPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
private var helpIcon: ImageView? = null

init {
Expand Down
3 changes: 3 additions & 0 deletions mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<string name="settings_notification_vibration_value_long" translatable="false">long</string>
<string name="settings_notification_vibration_value_twice" translatable="false">twice</string>
<string name="settings_notification_ringtone_vibration">Ringtone &amp; Vibration</string>
<string name="settings_notification_reset_settings">Reset channels</string>
<string name="settings_notification_reset_settings_confirm">Do you want to remove all notification categories? They will be re-created when a new notification arrives for this category.</string>
<string name="settings_notification_hint">The severity of notifications via myopenHAB.org is set as notification category. You can configure different notification settings for each category.</string>
<string name="settings_connection_summary">Connected to %s</string>
<!-- %1$s: URL, %2$s: Reason why connection is insecure -->
<string name="settings_insecure_connection_summary">Insecurely connected to %1$s: %2$s</string>
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
android:entries="@array/notificationVibration"
android:entryValues="@array/notificationVibrationValues"
android:icon="@drawable/ic_vibration_grey_24dp" />
<Preference
<org.openhab.habdroid.ui.preference.widgets.NotificationChannelPreference
android:clickable="true"
android:key="default_openhab_alertringtone_vibration"
android:title="@string/settings_notification_ringtone_vibration"
Expand Down

0 comments on commit 24b5894

Please sign in to comment.