forked from VREMSoftwareDevelopment/WiFiAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.kt
More file actions
163 lines (130 loc) · 6.58 KB
/
Settings.kt
File metadata and controls
163 lines (130 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* WiFiAnalyzer
* Copyright (C) 2015 - 2026 VREM Software Development <VREMSoftwareDevelopment@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.vrem.wifianalyzer.settings
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import com.vrem.util.buildMinVersionQ
import com.vrem.util.defaultCountryCode
import com.vrem.util.defaultLanguageTag
import com.vrem.util.findByLanguageTag
import com.vrem.util.findOne
import com.vrem.util.findSet
import com.vrem.util.ordinals
import com.vrem.wifianalyzer.R
import com.vrem.wifianalyzer.navigation.MAIN_NAVIGATION
import com.vrem.wifianalyzer.navigation.NavigationMenu
import com.vrem.wifianalyzer.wifi.accesspoint.AccessPointViewType
import com.vrem.wifianalyzer.wifi.accesspoint.ConnectionViewType
import com.vrem.wifianalyzer.wifi.band.WiFiBand
import com.vrem.wifianalyzer.wifi.graphutils.GraphLegend
import com.vrem.wifianalyzer.wifi.model.GroupBy
import com.vrem.wifianalyzer.wifi.model.Security
import com.vrem.wifianalyzer.wifi.model.SortBy
import com.vrem.wifianalyzer.wifi.model.Strength
import java.util.Locale
import kotlin.enums.EnumEntries
class Settings(
private val repository: Repository,
) {
fun initializeDefaultValues(): Unit = repository.initializeDefaultValues()
fun registerOnSharedPreferenceChangeListener(
onSharedPreferenceChangeListener: OnSharedPreferenceChangeListener,
): Unit = repository.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener)
fun scanSpeed(): Int =
repository.stringAsInteger(
R.string.scan_speed_key,
repository.stringAsInteger(R.string.scan_speed_default, SCAN_SPEED_DEFAULT),
)
fun cacheOff(): Boolean =
repository.boolean(R.string.cache_off_key, repository.resourceBoolean(R.bool.cache_off_default))
fun graphMaximumY(): Int {
val defaultValue = repository.stringAsInteger(R.string.graph_maximum_y_default, GRAPH_Y_DEFAULT)
val result = repository.stringAsInteger(R.string.graph_maximum_y_key, defaultValue)
return result * GRAPH_Y_MULTIPLIER
}
fun wiFiBand(wiFiBand: WiFiBand): Unit = repository.save(R.string.wifi_band_key, wiFiBand.ordinal)
fun countryCode(): String = repository.string(R.string.country_code_key, defaultCountryCode())
fun languageLocale(): Locale {
val defaultLanguageTag = defaultLanguageTag()
val languageTag = repository.string(R.string.language_key, defaultLanguageTag)
return findByLanguageTag(languageTag)
}
fun sortBy(): SortBy = settingsFind(SortBy.entries, R.string.sort_by_key, SortBy.STRENGTH)
fun groupBy(): GroupBy = settingsFind(GroupBy.entries, R.string.group_by_key, GroupBy.NONE)
fun accessPointView(): AccessPointViewType =
settingsFind(AccessPointViewType.entries, R.string.ap_view_key, AccessPointViewType.COMPLETE)
fun connectionViewType(): ConnectionViewType =
settingsFind(ConnectionViewType.entries, R.string.connection_view_key, ConnectionViewType.COMPACT)
fun channelGraphLegend(): GraphLegend =
settingsFind(GraphLegend.entries, R.string.channel_graph_legend_key, GraphLegend.HIDE)
fun timeGraphLegend(): GraphLegend =
settingsFind(GraphLegend.entries, R.string.time_graph_legend_key, GraphLegend.LEFT)
fun wiFiBand(): WiFiBand = settingsFind(WiFiBand.entries, R.string.wifi_band_key, WiFiBand.GHZ2)
fun wiFiOffOnExit(): Boolean =
if (buildMinVersionQ()) {
false
} else {
repository.boolean(
R.string.wifi_off_on_exit_key,
repository.resourceBoolean(R.bool.wifi_off_on_exit_default),
)
}
fun keepScreenOn(): Boolean =
repository.boolean(R.string.keep_screen_on_key, repository.resourceBoolean(R.bool.keep_screen_on_default))
fun themeStyle(): ThemeStyle = settingsFind(ThemeStyle.entries, R.string.theme_key, ThemeStyle.DARK)
fun selectedMenu(): NavigationMenu =
settingsFind(NavigationMenu.entries, R.string.selected_menu_key, NavigationMenu.ACCESS_POINTS)
fun saveSelectedMenu(navigationMenu: NavigationMenu) {
if (MAIN_NAVIGATION.contains(navigationMenu)) {
repository.save(R.string.selected_menu_key, navigationMenu.ordinal)
}
}
fun findSSIDs(): Set<String> = repository.stringSet(R.string.filter_ssid_key, setOf())
fun saveSSIDs(values: Set<String>): Unit = repository.saveStringSet(R.string.filter_ssid_key, values)
fun findWiFiBands(): Set<WiFiBand> = settingsFindSet(WiFiBand.entries, R.string.filter_wifi_band_key, WiFiBand.GHZ2)
fun saveWiFiBands(values: Set<WiFiBand>): Unit = settingsSaveSet(R.string.filter_wifi_band_key, values)
fun findStrengths(): Set<Strength> = settingsFindSet(Strength.entries, R.string.filter_strength_key, Strength.FOUR)
fun saveStrengths(values: Set<Strength>): Unit = settingsSaveSet(R.string.filter_strength_key, values)
fun findSecurities(): Set<Security> = settingsFindSet(Security.entries, R.string.filter_security_key, Security.NONE)
fun saveSecurities(values: Set<Security>): Unit = settingsSaveSet(R.string.filter_security_key, values)
private fun <T : Enum<T>> settingsFind(
values: EnumEntries<T>,
key: Int,
defaultValue: T,
): T {
val value = repository.stringAsInteger(key, defaultValue.ordinal)
return findOne(values, value, defaultValue)
}
private fun <T : Enum<T>> settingsFindSet(
values: EnumEntries<T>,
key: Int,
defaultValue: T,
): Set<T> {
val ordinalDefault = ordinals(values)
val ordinalSaved = repository.stringSet(key, ordinalDefault)
return findSet(values, ordinalSaved, defaultValue)
}
private fun <T : Enum<T>> settingsSaveSet(
key: Int,
values: Set<T>,
): Unit = repository.saveStringSet(key, ordinals(values))
companion object {
private const val SCAN_SPEED_DEFAULT = 5
private const val GRAPH_Y_MULTIPLIER = -10
private const val GRAPH_Y_DEFAULT = 2
}
}