Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/customize notification led color #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 82 additions & 11 deletions app/src/main/java/com/orgzly/android/NotificationChannels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,43 @@ import androidx.annotation.RequiresApi
import com.orgzly.R
import com.orgzly.android.reminders.RemindersNotifications
import com.orgzly.android.ui.util.getNotificationManager
import com.orgzly.android.prefs.AppPreferences
import java.util.UUID


/**
* Creates all channels for notifications.
*/
object NotificationChannels {

const val ONGOING = "ongoing"
const val REMINDERS = "reminders"
private const val _ONGOING = "ongoing"
private var ongoingId = ""
private var prevOngoingId = _ONGOING

private const val _REMINDERS = "reminders"
private var remindersId = ""
private var prevRemindersId = _REMINDERS

const val SYNC_PROGRESS = "sync-progress"
const val SYNC_FAILED = "sync-failed"
const val SYNC_PROMPT = "sync-prompt"

@JvmStatic
fun channelIdForOngoing() : String {
return ongoingId;
}

@JvmStatic
fun channelIdForReminders() : String {
return remindersId;
}


@JvmStatic
fun createAll(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
updateChannelIds();

createForOngoing(context)
createForReminders(context)
createForSyncProgress(context)
Expand All @@ -33,9 +54,46 @@ object NotificationChannels {
}
}

@JvmStatic
fun updateAll(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
updateChannelIds();

updateForReminders(context)
updateForOngoing(context)
}
}

private fun updateChannelIds() {
prevRemindersId = remindersId;
remindersId = _REMINDERS + "_" + UUID.randomUUID()

prevOngoingId = ongoingId;
ongoingId = _ONGOING + "_" + UUID.randomUUID()
}


@RequiresApi(Build.VERSION_CODES.O)
private fun createForReminders(context: Context) {
val id = REMINDERS
private fun updateForReminders(context: Context) {
val channel = createChannelForReminders(context);
context.getNotificationManager().createNotificationChannel(channel)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun updateForOngoing(context: Context) {
// Note: Effect on Notifications: Deleting a notification channel will remove all
// notifications associated with that channel. If you recreate the channel later, it will be
// treated as a new channel, and any previous notifications will not be restored. Using the
// same ID will restore the previous channel including the LED color.

val channel = createChannelForOngoing(context);
context.getNotificationManager().createNotificationChannel(channel)
}


@RequiresApi(Build.VERSION_CODES.O)
private fun createChannelForReminders(context: Context) : NotificationChannel {
val id = remindersId;
val name = context.getString(R.string.reminders_channel_name)
val description = context.getString(R.string.reminders_channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
Expand All @@ -45,22 +103,32 @@ object NotificationChannels {
channel.description = description

channel.enableLights(true)
channel.lightColor = Color.BLUE

val colorString = AppPreferences.remindersLedColor(context);
channel.lightColor = Color.parseColor(colorString);

channel.vibrationPattern = RemindersNotifications.VIBRATION_PATTERN

channel.setShowBadge(false)

return channel;
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createForReminders(context: Context) {
val channel = createChannelForReminders(context)
context.getNotificationManager().createNotificationChannel(channel)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createForOngoing(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
private fun createForOngoing(context: Context) {
val channel = createChannelForOngoing(context)
context.getNotificationManager().createNotificationChannel(channel)
}

val id = ONGOING
@RequiresApi(Build.VERSION_CODES.O)
private fun createChannelForOngoing(context: Context) : NotificationChannel {
val id = ongoingId
val name = context.getString(R.string.ongoing_channel_name)
val description = context.getString(R.string.ongoing_channel_description)
val importance = NotificationManager.IMPORTANCE_MIN
Expand All @@ -71,7 +139,10 @@ object NotificationChannels {

channel.setShowBadge(false)

context.getNotificationManager().createNotificationChannel(channel)
val colorString = AppPreferences.remindersLedColor(context);
channel.lightColor = Color.parseColor(colorString);

return channel;
}

@RequiresApi(Build.VERSION_CODES.O)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ public static boolean remindersLed(Context context) {
context.getResources().getBoolean(R.bool.pref_default_reminders_led));
}

public static String remindersLedColor(Context context) {
return getDefaultSharedPreferences(context).getString(
context.getResources().getString(R.string.pref_key_reminders_led_color),
context.getResources().getString(R.string.pref_default_reminders_led_color));
}

public static boolean remindersVibrate(Context context) {
return getDefaultSharedPreferences(context).getBoolean(
context.getResources().getString(R.string.pref_key_reminders_vibrate),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import com.orgzly.android.util.UserTimeFormatter
object RemindersNotifications {
val VIBRATION_PATTERN = longArrayOf(500, 50, 50, 300)

private val LIGHTS = Triple(Color.BLUE, 1000, 5000)
private val LIGHTS_ON_MS = 1000
private val LIGHTS_OFF_MS = 5000

fun showNotifications(context: Context, notes: List<NoteReminder>, logs: AppLogsRepository) {
val notificationManager = context.getNotificationManager()
Expand All @@ -38,7 +39,8 @@ object RemindersNotifications {

val content = getContent(context, noteReminder)

val builder = NotificationCompat.Builder(context, NotificationChannels.REMINDERS)
val builder = NotificationCompat.Builder(context,
NotificationChannels.channelIdForReminders())
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setPriority(NotificationCompat.PRIORITY_MAX)
Expand All @@ -61,7 +63,9 @@ object RemindersNotifications {

// Set LED
if (AppPreferences.remindersLed(context)) {
builder.setLights(LIGHTS.first, LIGHTS.second, LIGHTS.third)
val colorString = AppPreferences.remindersLedColor(context);
val color = Color.parseColor(colorString);
builder.setLights(color, LIGHTS_ON_MS, LIGHTS_OFF_MS)
}

builder.setContentTitle(OrgFormatter.parse(
Expand Down Expand Up @@ -127,7 +131,8 @@ object RemindersNotifications {
// Create a group summary notification, but only if notifications can be grouped
if (canGroupReminders()) {
if (notes.isNotEmpty()) {
val builder = NotificationCompat.Builder(context, NotificationChannels.REMINDERS)
val builder = NotificationCompat.Builder(context,
NotificationChannels.channelIdForReminders())
.setAutoCancel(true)
.setSmallIcon(R.drawable.cic_logo_for_notification)
.setGroup(Notifications.REMINDERS_GROUP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void showOngoingNotification(Context context) {
PendingIntent newNotePendingIntent =
ShareActivity.createNewNotePendingIntent(context, "ongoing notification", null);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.ONGOING)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.channelIdForOngoing())
.setOngoing(true)
.setSmallIcon(R.drawable.cic_logo_for_notification)
.setContentTitle(context.getString(R.string.new_note))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.preference.*
import com.orgzly.BuildConfig
Expand All @@ -26,6 +27,7 @@ import com.orgzly.android.usecase.UseCase
import com.orgzly.android.util.AppPermissions
import com.orgzly.android.util.LogUtils
import com.orgzly.android.widgets.ListWidgetProvider
import com.orgzly.android.NotificationChannels

/**
* Displays settings.
Expand Down Expand Up @@ -124,6 +126,18 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
}
}

preference(R.string.pref_key_reminders_led_color)?.let {
it.setOnPreferenceChangeListener {
_, newValue ->
val pattern = Regex("^#[0-9A-Fa-f]{6}$")
val stringCorrect = pattern.matches(newValue.toString())
if (!stringCorrect) {
Toast.makeText(activity, "Please enter a hexadecimal value for color.", Toast.LENGTH_SHORT).show()
}
stringCorrect
}
}

/* Update preferences which depend on multiple others. */
updateRemindersScreen()
updateWidgetScreen()
Expand Down Expand Up @@ -369,6 +383,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP

updateRemindersScreen()
updateWidgetScreen()
updateNotificationChannels()

/* Always notify about possibly changed data, if settings are modified.
*
Expand All @@ -381,6 +396,11 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
SharingShortcutsManager().replaceDynamicShortcuts(requireContext())
}

private fun updateNotificationChannels() {
var context = requireContext();
NotificationChannels.updateAll(context);
}

private fun updateRemindersScreen() {
val scheduled = preference(R.string.pref_key_use_reminders_for_scheduled_times)
val deadline = preference(R.string.pref_key_use_reminders_for_deadline_times)
Expand All @@ -403,6 +423,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
preference(R.string.pref_key_snooze_time)?.isEnabled = remindersEnabled
preference(R.string.pref_key_snooze_type)?.isEnabled = remindersEnabled
preference(R.string.pref_key_daily_reminder_time)?.isEnabled = remindersEnabled
preference(R.string.pref_key_reminders_led_color)?.isEnabled = remindersEnabled
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/prefs_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@

<string name="pref_key_reminders_led" translatable="false">pref_key_reminders_led</string>
<bool name="pref_default_reminders_led" translatable="false">true</bool>

<string name="pref_key_reminders_led_color" translatable="false">pref_key_reminders_led_color</string>
<string name="pref_default_reminders_led_color" translatable="false">#0000FF</string>

<string name="pref_key_daily_reminder_time" translatable="false">pref_key_daily_reminder_time</string>
<!-- Time in minutes after midnight. 540 minutes == 9 o'clock -->
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@
<string name="pref_title_reminders_sound">Sound</string>
<string name="pref_title_reminders_vibrate">Vibration</string>
<string name="pref_title_reminders_led">Light</string>
<string name="pref_title_reminders_led_color">LED color</string>
<string name="pref_summary_reminders_led_color">Set the color of the LED. Has no effect on existing notifications.</string>

<string name="pref_title_snooze_time">Snooze time (minutes)</string>
<string name="pref_title_snooze_type">Snooze type</string>
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/xml/prefs_screen_reminders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
android:key="@string/pref_key_reminders_sound"
android:title="@string/pref_title_reminders_sound"
android:defaultValue="@bool/pref_default_reminders_sound" />

<SwitchPreference
android:key="@string/pref_key_reminders_led"
android:title="@string/pref_title_reminders_led"
Expand All @@ -55,6 +55,12 @@
</PreferenceCategory>

<!-- v26 and after -->
<EditTextPreference
android:defaultValue="@string/pref_default_reminders_led_color"
android:key="pref_key_reminders_led_color"
android:title="@string/pref_title_reminders_led_color"
android:summary="@string/pref_summary_reminders_led_color"/>

<!-- Open system settings to configure reminders' notification channel -->
<androidx.preference.PreferenceScreen
android:key="@string/pref_key_reminders_notification_settings_V26"
Expand Down
Loading