Update preferences

This commit is contained in:
Koitharu
2021-01-24 09:58:21 +02:00
parent 42f0fa9bbf
commit 3df8b8d170
8 changed files with 139 additions and 78 deletions

View File

@@ -174,5 +174,6 @@ class AppSettings private constructor(private val prefs: SharedPreferences) :
const val KEY_BACKUP = "backup"
const val KEY_RESTORE = "restore"
const val KEY_HISTORY_GROUPING = "history_grouping"
const val KEY_DOZE_WHITELIST = "doze_whitelist"
}
}

View File

@@ -1,11 +1,8 @@
package org.koitharu.kotatsu.settings
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.text.InputType
import android.view.View
import androidx.appcompat.app.AppCompatDelegate
@@ -18,11 +15,8 @@ import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
import org.koitharu.kotatsu.base.ui.dialog.StorageSelectDialog
import org.koitharu.kotatsu.base.ui.dialog.TextInputDialog
import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.model.ZoomMode
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.prefs.ListMode
import org.koitharu.kotatsu.settings.utils.MultiSummaryProvider
import org.koitharu.kotatsu.tracker.work.TrackWorker
import org.koitharu.kotatsu.utils.ext.*
import java.io.File
@@ -40,18 +34,10 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
true
}
}
preferenceScreen?.findPreference<ListPreference>(AppSettings.KEY_ZOOM_MODE)?.run {
entryValues = ZoomMode.values().names()
setDefaultValueCompat(ZoomMode.FIT_CENTER.name)
}
preferenceScreen?.findPreference<ListPreference>(AppSettings.KEY_LIST_MODE)?.run {
entryValues = ListMode.values().names()
setDefaultValueCompat(ListMode.GRID.name)
}
findPreference<MultiSelectListPreference>(AppSettings.KEY_READER_SWITCHERS)?.summaryProvider =
MultiSummaryProvider(R.string.gestures_only)
findPreference<MultiSelectListPreference>(AppSettings.KEY_TRACK_SOURCES)?.summaryProvider =
MultiSummaryProvider(R.string.dont_check)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -106,17 +92,6 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
AppSettings.KEY_NOTIFICATIONS_SETTINGS -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName)
.putExtra(Settings.EXTRA_CHANNEL_ID, TrackWorker.CHANNEL_ID)
startActivity(intent)
} else {
(activity as? SettingsActivity)?.openNotificationSettingsLegacy()
}
true
}
AppSettings.KEY_LOCAL_STORAGE -> {
val ctx = context ?: return false
StorageSelectDialog.Builder(ctx, settings.getStorageDir(ctx), this)

View File

@@ -0,0 +1,86 @@
package org.koitharu.kotatsu.settings
import android.annotation.SuppressLint
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import androidx.preference.MultiSelectListPreference
import androidx.preference.Preference
import com.google.android.material.snackbar.Snackbar
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.settings.utils.MultiSummaryProvider
import org.koitharu.kotatsu.tracker.work.TrackWorker
class TrackerSettingsFragment : BasePreferenceFragment(R.string.new_chapters_checking) {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_tracker)
findPreference<MultiSelectListPreference>(AppSettings.KEY_TRACK_SOURCES)
?.summaryProvider = MultiSummaryProvider(R.string.dont_check)
findPreference<Preference>(AppSettings.KEY_DOZE_WHITELIST)
?.isVisible = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
AppSettings.KEY_NOTIFICATIONS_SETTINGS -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName)
.putExtra(Settings.EXTRA_CHANNEL_ID, TrackWorker.CHANNEL_ID)
startActivity(intent)
} else {
(activity as? SettingsActivity)?.openNotificationSettingsLegacy()
}
true
}
AppSettings.KEY_DOZE_WHITELIST -> {
disablePowerOptimization()
true
}
else -> super.onPreferenceTreeClick(preference)
}
}
@SuppressLint("BatteryLife")
private fun disablePowerOptimization() {
val context = context ?: return
val powerManager = context.getSystemService(Context.POWER_SERVICE) as? PowerManager
if (powerManager == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Snackbar.make(
listView ?: return,
R.string.operation_not_supported,
Snackbar.LENGTH_LONG
).show()
return
}
val packageName = context.packageName
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
.setData(Uri.parse("package:$packageName"))
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Snackbar.make(
listView ?: return,
R.string.operation_not_supported,
Snackbar.LENGTH_LONG
).show()
}
} else {
Snackbar.make(
listView ?: return,
R.string.power_optimization_already_disabled,
Snackbar.LENGTH_LONG
).show()
}
}
}