Skip to content

Commit

Permalink
Implement vmiklos' suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
masonhfisher authored and vmiklos committed Feb 23, 2024
1 parent e1d4ea9 commit fd9c2d3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 102 deletions.
17 changes: 11 additions & 6 deletions app/src/main/java/hu/vmiklos/plees_tracker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,12 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {

if (DataModel.start != null && DataModel.stop != null) {
// When user stops tracking sleep
if (dndManager.isNotificationPolicyAccessGranted and dndEnabled) {
// Restore Do Not Disturb status when user started tracking
val filterAll = NotificationManager.INTERRUPTION_FILTER_ALL
dndManager.setInterruptionFilter(preferences.getInt("current_dnd", filterAll))
if (dndManager.isNotificationPolicyAccessGranted) {
if (dndEnabled) {
// Restore Do Not Disturb status when user started tracking
val filterAll = NotificationManager.INTERRUPTION_FILTER_ALL
dndManager.setInterruptionFilter(preferences.getInt("current_dnd", filterAll))
}
} else {
Log.w(TAG, "Failed to disable DND, permissions not enabled")
}
Expand All @@ -346,8 +348,11 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
preferences.edit()
.putInt("current_dnd", dndManager.currentInterruptionFilter)
.apply() // Saves current Do Not Disturb status
if (dndManager.isNotificationPolicyAccessGranted and dndEnabled) {
dndManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
if (dndManager.isNotificationPolicyAccessGranted) {
if (dndEnabled) {
val filterPri = NotificationManager.INTERRUPTION_FILTER_PRIORITY
dndManager.setInterruptionFilter(filterPri)
}
} else {
Log.w(TAG, "Failed to enable DND, permissions not enabled")
}
Expand Down
80 changes: 0 additions & 80 deletions app/src/main/java/hu/vmiklos/plees_tracker/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@

package hu.vmiklos.plees_tracker

import android.app.AlertDialog
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager

class Preferences : PreferenceFragmentCompat() {
companion object {
private const val TAG = "PreferencesActivity"
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
val autoBackupPath = findPreference<Preference>("auto_backup_path")
Expand All @@ -30,76 +20,6 @@ class Preferences : PreferenceFragmentCompat() {
it.summary = path
}
}

override fun onPreferenceTreeClick(preference: Preference): Boolean {
val sharedPreferences = context?.let { PreferenceManager.getDefaultSharedPreferences(it) }

if (preference.key == "enable_dnd") { // If someone toggled the enable_dnd preference
if (sharedPreferences != null) {
if (sharedPreferences.getBoolean("enable_dnd", false)) {
val activity = activity ?: return super.onPreferenceTreeClick(preference)
val noteServ = Context.NOTIFICATION_SERVICE // 100 char limit
val dndManager = activity.getSystemService(noteServ) as NotificationManager
val hasPermission = dndManager.isNotificationPolicyAccessGranted

if (!hasPermission) {
// If we don't have permissions for DND
AlertDialog.Builder(context)
.setTitle(R.string.settings_enable_dnd_q_title)
.setMessage(R.string.settings_enable_dnd_q_message)
.setPositiveButton(R.string.settings_enable_dnd_q_ok) { _, _ ->
val setting_id = Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
val intent = Intent(setting_id)
startActivity(intent)
}
.setNegativeButton(R.string.settings_enable_dnd_q_cancel) { _, _ ->
val editor = sharedPreferences.edit()
if (editor != null) {
editor.putBoolean("enable_dnd", false)
editor.apply()
} else {
Log.wtf(TAG, "editor is null")
}
checkDnd()
}
.create()
.show()
}
}
} else {
Log.wtf(TAG, "sharedPreferences is null")
}
}
return super.onPreferenceTreeClick(preference)
}

override fun onResume() {
super.onResume()
checkDnd()
}

fun checkDnd() {
val activity = activity ?: return
val noteServ = Context.NOTIFICATION_SERVICE // 100 char limit
val dndManager = activity.getSystemService(noteServ) as NotificationManager
val hasPermission = dndManager.isNotificationPolicyAccessGranted

val sharedPreferences = context?.let { PreferenceManager.getDefaultSharedPreferences(it) }
val editor = sharedPreferences?.edit()

if (!hasPermission) {
if (editor != null) {
editor.putBoolean("enable_dnd", false)
editor.apply()
Log.d(TAG, "Set enable_dnd to false!")
// Refresh screen, not the "cleanest way" but it works
preferenceScreen.removeAll()
addPreferencesFromResource(R.xml.preferences)
} else {
Log.wtf(TAG, "editor is null")
}
}
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class PreferencesActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// To my knowledge you have to use getSystemService in activity code
supportFragmentManager
.beginTransaction()
.replace(R.id.settings_container, Preferences())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

package hu.vmiklos.plees_tracker

import android.app.AlertDialog
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate

Expand All @@ -16,25 +21,51 @@ class SharedPreferencesChangeListener : SharedPreferences.OnSharedPreferenceChan
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
if (key == "auto_backup") {
val autoBackup = sharedPreferences.getBoolean("auto_backup", false)
val autoBackupPath = sharedPreferences.getString("auto_backup_path", "")
if (autoBackup) {
if (autoBackupPath.isNullOrEmpty()) {
when (key) {
"auto_backup" -> {
val autoBackup = sharedPreferences.getBoolean("auto_backup", false)
val autoBackupPath = sharedPreferences.getString("auto_backup_path", "")
if (autoBackup) {
if (autoBackupPath.isNullOrEmpty()) {
val preferencesActivity = DataModel.preferencesActivity
if (preferencesActivity != null) {
Log.i(TAG, "onSharedPreferenceChanged: setting new backup path")
preferencesActivity.openFolderChooser()
}
}
} else {
// Forget old path, so it's possible to set a different one later.
Log.i(TAG, "onSharedPreferenceChanged: clearing old backup path")
val editor = DataModel.preferences.edit()
editor.remove("auto_backup_path")
editor.apply()
}
return
}
"enable_dnd" -> {
if (sharedPreferences.getBoolean("enable_dnd", false)) {
val preferencesActivity = DataModel.preferencesActivity
if (preferencesActivity != null) {
Log.i(TAG, "onSharedPreferenceChanged: setting new backup path")
preferencesActivity.openFolderChooser()
val noteServ = Context.NOTIFICATION_SERVICE // 100 char limit
val dndManager = preferencesActivity?.getSystemService(noteServ)
as NotificationManager
val hasPermission = dndManager.isNotificationPolicyAccessGranted

if (!hasPermission) {
// If we don't have permissions for DND
AlertDialog.Builder(preferencesActivity)
.setTitle(R.string.settings_enable_dnd_q_title)
.setMessage(R.string.settings_enable_dnd_q_message)
.setPositiveButton(R.string.settings_enable_dnd_q_ok) { _, _ ->
val settingId = Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
val intent = Intent(settingId)
preferencesActivity.startActivity(intent)
}
.setNegativeButton(R.string.settings_enable_dnd_q_cancel) { _, _ -> }
.create()
.show()
}
}
} else {
// Forget old path, so it's possible to set a different one later.
Log.i(TAG, "onSharedPreferenceChanged: clearing old backup path")
val editor = DataModel.preferences.edit()
editor.remove("auto_backup_path")
editor.apply()
}
return
}

applyTheme(sharedPreferences)
Expand Down

0 comments on commit fd9c2d3

Please sign in to comment.