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

@@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:name="org.koitharu.kotatsu.KotatsuApp"

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()
}
}
}

View File

@@ -195,4 +195,8 @@
<string name="chapers_checking_progress">Проверка новых глав: %1$d из %2$d</string>
<string name="clear_feed">Очистить ленту</string>
<string name="text_clear_updates_feed_prompt">Вся история обновлений будет очищена и её нельзя будет вернуть. Вы уверены?</string>
<string name="power_optimization_simmary">Может помочь с фоновыми операциями. Используйте только если фоновая проверка новых глав не работает</string>
<string name="disable_power_optimization">Отключить оптимизацию батареи</string>
<string name="power_optimization_already_disabled">Отпимизация батареи уже отключена</string>
<string name="new_chapters_checking">Проверка новых глав</string>
</resources>

View File

@@ -197,4 +197,8 @@
<string name="chapers_checking_progress">Checking for new chapters: %1$d of %2$d</string>
<string name="clear_feed">Clear feed</string>
<string name="text_clear_updates_feed_prompt">All updates history will be cleared and this action cannot be undone. Are you sure?</string>
<string name="power_optimization_already_disabled">Power optimization is already disabled</string>
<string name="power_optimization_simmary">Helps with background operations such as checking for new chapters. Use only if you have a troubles with it</string>
<string name="disable_power_optimization">Disable power optimization</string>
<string name="new_chapters_checking">New chapters checking</string>
</resources>

View File

@@ -63,63 +63,16 @@
android:title="@string/protect_application"
app:iconSpaceReserved="false" />
<MultiSelectListPreference
android:defaultValue="@array/values_reader_switchers_default"
android:entries="@array/reader_switchers"
android:entryValues="@array/values_reader_switchers"
android:key="reader_switchers"
android:title="@string/switch_pages"
app:allowDividerAbove="true"
<PreferenceScreen
android:fragment="org.koitharu.kotatsu.settings.ReaderSettingsFragment"
android:title="@string/reader_settings"
app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="false"
android:key="reader_animation"
android:title="@string/pages_animation"
<PreferenceScreen
android:fragment="org.koitharu.kotatsu.settings.TrackerSettingsFragment"
android:title="@string/new_chapters_checking"
app:iconSpaceReserved="false" />
<ListPreference
android:entries="@array/zoom_modes"
android:key="zoom_mode"
android:title="@string/scale_mode"
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:defaultValue="false"
android:key="reader_prefer_rtl"
android:summary="@string/prefer_rtl_reader_summary"
android:title="@string/prefer_rtl_reader"
app:iconSpaceReserved="false" />
<PreferenceCategory
android:title="@string/new_chapters"
app:allowDividerAbove="true"
app:iconSpaceReserved="false">
<MultiSelectListPreference
android:defaultValue="@array/values_track_sources_default"
android:entries="@array/track_sources"
android:entryValues="@array/values_track_sources"
android:key="track_sources"
android:title="@string/track_sources"
app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="true"
android:key="tracker_notifications"
android:summary="@string/show_notification_new_chapters"
android:title="@string/notifications"
app:iconSpaceReserved="false" />
<Preference
android:dependency="tracker_notifications"
android:key="notifications_settings"
android:title="@string/notifications_settings"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/about"
app:iconSpaceReserved="false">

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<MultiSelectListPreference
android:defaultValue="@array/values_track_sources_default"
android:entries="@array/track_sources"
android:entryValues="@array/values_track_sources"
android:key="track_sources"
android:title="@string/track_sources"
app:iconSpaceReserved="false" />
<Preference
android:key="doze_whitelist"
android:persistent="false"
android:summary="@string/power_optimization_simmary"
android:title="@string/disable_power_optimization"
app:iconSpaceReserved="false"
app:isPreferenceVisible="false"
tools:isPreferenceVisible="true" />
<SwitchPreference
android:defaultValue="true"
android:key="tracker_notifications"
android:summary="@string/show_notification_new_chapters"
android:title="@string/notifications"
app:iconSpaceReserved="false" />
<Preference
android:dependency="tracker_notifications"
android:key="notifications_settings"
android:title="@string/notifications_settings"
app:iconSpaceReserved="false" />
</PreferenceScreen>