-
Notifications
You must be signed in to change notification settings - Fork 80
/
Config.kt
215 lines (181 loc) · 7.96 KB
/
Config.kt
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2016 Marien Raat <marienraat@riseup.net>
* Copyright (c) 2017 Stephen Michel <s@smichel.me>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (c) 2015 Chris Nguyen
* Copyright (c) 2016 Zoraver <https://github.com/Zoraver>
* - App widget update broadcast
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.jmstudios.redmoon.model
import com.jmstudios.redmoon.R
import com.jmstudios.redmoon.schedule.ScheduleReceiver
import com.jmstudios.redmoon.widget.SwitchAppWidgetProvider
import com.jmstudios.redmoon.util.*
import com.luckycatlabs.sunrisesunset.SunriseSunsetCalculator
import java.util.Calendar
import java.util.TimeZone
import me.smichel.android.KPreferences.Preferences
private const val BROADCAST_ACTION = "com.jmstudios.redmoon.RED_MOON_TOGGLED"
private const val BROADCAST_FIELD = "jmstudios.bundle.key.FILTER_IS_ON"
/**
* This singleton provides allows easy access to the shared preferences
*/
object Config : Preferences(appContext) {
private val Log = KLogging.logger("Config")
//region state
var filterIsOn by BooleanPreference(R.string.pref_key_filter_is_on, false) {
Log.i("Sending update broadcasts: filter is on: $it")
//Broadcast to keep appwidgets in sync
context.sendBroadcast(intent(SwitchAppWidgetProvider::class).apply {
action = SwitchAppWidgetProvider.ACTION_UPDATE
putExtra(SwitchAppWidgetProvider.EXTRA_POWER, it)
})
// If an app like Tasker wants to do something each time
// Red Moon is toggled, it can listen for this event
context.sendBroadcast(intent().apply {
action = BROADCAST_ACTION
putExtra(BROADCAST_FIELD, it)
})
EventBus.post(filterIsOnChanged())
}
var brightnessLowered by BooleanPreference(R.string.pref_key_brightness_lowered, false)
var brightness by IntPreference(R.string.pref_key_brightness, 0)
var automaticBrightness by BooleanPreference(R.string.pref_key_automatic_brightness, true)
//endregion
//region filter
var color by IntPreference(R.string.pref_key_color, 10) {
activeProfile.run { if (it != color) activateProfile(copy(color = it)) }
}
var intensity by IntPreference(R.string.pref_key_intensity, 30) {
activeProfile.run { if (it != intensity) activateProfile(copy(intensity = it)) }
}
var dimLevel by IntPreference(R.string.pref_key_dim, 40) {
activeProfile.run { if (it != dimLevel) activateProfile(copy(dimLevel = it)) }
}
var lowerBrightness by BooleanPreference(R.string.pref_key_lower_brightness, false) {
activeProfile.run { if (it != lowerBrightness) activateProfile(copy(lowerBrightness = it)) }
}
private fun activateProfile(profile: Profile) {
Log.i("Activating profile: $profile")
custom = profile
activeProfile = profile
}
private var _custom by StringOrNullPreference(R.string.pref_key_custom_profile)
var custom: Profile
get() = _custom?.let { Profile.parse(it) } ?: activeProfile
set(value) {
Log.i("custom set to $value")
_custom = value.toString()
}
//endregion
//region settings
var scheduleOn by BooleanPreference(R.string.pref_key_schedule, true) {
if (it) {
Log.i("Schedule enabled")
ScheduleReceiver.rescheduleOnCommand()
ScheduleReceiver.rescheduleOffCommand()
} else {
Log.i("Schedule disabled")
ScheduleReceiver.cancelAlarms()
}
EventBus.post(scheduleChanged())
}
val customStartTime by StringPreference(R.string.pref_key_start_time, "22:00") {
ScheduleReceiver.rescheduleOnCommand()
EventBus.post(scheduleChanged())
}
val customStopTime by StringPreference(R.string.pref_key_stop_time, "06:00") {
ScheduleReceiver.rescheduleOffCommand()
EventBus.post(scheduleChanged())
}
var startAtSunset by BooleanPreference(R.string.pref_key_use_location_start, false) {
EventBus.post(useLocationChanged())
}
var stopAtSunrise by BooleanPreference(R.string.pref_key_use_location_stop, false) {
EventBus.post(useLocationChanged())
}
val useLocation: Boolean get() = startAtSunset || stopAtSunrise
val scheduledStartTime: String
get() = if (startAtSunset) sunsetTime else customStartTime
val scheduledStopTime: String
get() = if (stopAtSunrise) sunriseTime else customStopTime
private var _location by StringPreference(R.string.pref_key_location, "0,0") {
ScheduleReceiver.rescheduleOffCommand()
ScheduleReceiver.rescheduleOnCommand()
EventBus.post(locationChanged())
}
const val NOT_SET: Long = -1
private var _locationTimestamp by LongPreference(R.string.pref_key_location_timestamp, NOT_SET)
var location: Triple<String, String, Long?>
get() = with (_location) {
val latitude = substringBefore(',')
val longitude = substringAfter(',')
val timestamp = _locationTimestamp.let { if (it == NOT_SET) null else it }
return Triple(latitude, longitude, timestamp)
}
set(l) {
_locationTimestamp = l.third ?: NOT_SET
_location = l.first + "," + l.second
}
const val DEFAULT_SUNSET = "19:30"
val sunsetTime: String
get() {
val (latitude, longitude, time) = location
return if (time == null) {
DEFAULT_SUNSET
} else {
val sunLocation = com.luckycatlabs.sunrisesunset.dto.Location(latitude, longitude)
val calculator = SunriseSunsetCalculator(sunLocation, TimeZone.getDefault())
calculator.getOfficialSunsetForDate(Calendar.getInstance())
}
}
const val DEFAULT_SUNRISE = "06:30"
val sunriseTime: String
get() {
val (latitude, longitude, time) = location
return if (time == null) {
DEFAULT_SUNRISE
} else {
val sunLocation = com.luckycatlabs.sunrisesunset.dto.Location(latitude, longitude)
val calculator = SunriseSunsetCalculator(sunLocation, TimeZone.getDefault())
calculator.getOfficialSunriseForDate(Calendar.getInstance())
}
}
val secureSuspend by BooleanPreference(R.string.pref_key_secure_suspend, false) {
EventBus.post(secureSuspendChanged())
}
var darkThemeFlag by BooleanPreference(R.string.pref_key_dark_theme, false)
val buttonBacklightFlag by StringPreference(R.string.pref_key_button_backlight, "off") {
EventBus.post(buttonBacklightChanged())
}
val buttonBacklightLevel: Float
get() = when (buttonBacklightFlag) {
"system" -> (-1).toFloat()
"dim" -> 1 - (dimLevel.toFloat() / 100)
else -> 0.toFloat()
}
//endregion
//region application
var introShown by BooleanPreference(R.string.pref_key_intro_shown, false)
var fromVersionCode by IntPreference(R.string.pref_key_from_version_code, -1)
var lastChangelogShown by IntPreference(R.string.pref_key_last_changelog_shown, 0)
//endregion
}