This repository was archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathSettingStore.kt
More file actions
219 lines (187 loc) · 9.27 KB
/
Copy pathSettingStore.kt
File metadata and controls
219 lines (187 loc) · 9.27 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
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
216
217
218
219
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package mozilla.lockbox.store
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.preference.PreferenceManager
import android.view.autofill.AutofillManager
import androidx.annotation.RequiresApi
import com.f2prateek.rx.preferences2.RxSharedPreferences
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.addTo
import io.reactivex.subjects.ReplaySubject
import io.reactivex.subjects.Subject
import mozilla.lockbox.action.FingerprintAuthAction
import mozilla.lockbox.action.LifecycleAction
import mozilla.lockbox.action.Setting
import mozilla.lockbox.action.SettingAction
import mozilla.lockbox.extensions.filterByType
import mozilla.lockbox.flux.Dispatcher
import mozilla.lockbox.support.Constant
open class SettingStore(
val dispatcher: Dispatcher = Dispatcher.shared,
val fingerprintStore: FingerprintStore = FingerprintStore.shared
) : ContextStore {
companion object {
val shared by lazy { SettingStore() }
}
object Keys {
const val SEND_USAGE_DATA = "send_usage_data"
const val ITEM_LIST_SORT_ORDER = "sort_order"
const val UNLOCK_WITH_FINGERPRINT = "unlock_with_fingerprint"
const val AUTO_LOCK_TIME = "auto_lock_time"
const val DEVICE_SECURITY_PRESENT = "device_security_present"
const val UNLOCK_WITH_FINGERPRINT_PENDING_AUTH = "unlock_with_fingerprint_pending_auth"
const val USE_LOCAL_SERVICE = "use_local_service"
}
private lateinit var preferences: SharedPreferences
@RequiresApi(Build.VERSION_CODES.O)
private lateinit var autofillManager: AutofillManager
private val compositeDisposable = CompositeDisposable()
private val _deviceSecurityWasPresent = ReplaySubject.createWithSize<Boolean>(1)
private val _autoLockTime = ReplaySubject.createWithSize<Setting.AutoLockTime>(1)
open val sendUsageData: Observable<Boolean> = ReplaySubject.createWithSize(1)
open val useLocalService: Observable<Boolean> = ReplaySubject.createWithSize(1)
open val itemListSortOrder: Observable<Setting.ItemListSort> = ReplaySubject.createWithSize(1)
open val unlockWithFingerprint: Observable<Boolean> = ReplaySubject.createWithSize(1)
open val autoLockTime: Observable<Setting.AutoLockTime>
get() = autoLockSetting()
open lateinit var unlockWithFingerprintPendingAuth: Observable<Boolean>
open val onEnablingFingerprint: Observable<FingerprintAuthAction> =
dispatcher.register
.filterByType(FingerprintAuthAction::class.java)
// Accessing these properties in an environment with an Android SDK lower than V26 will result in app crashes!
open val autofillAvailable: Boolean
@RequiresApi(Build.VERSION_CODES.O)
get() = autofillManager.isAutofillSupported
open val isCurrentAutofillProvider: Boolean
@RequiresApi(Build.VERSION_CODES.O)
get() = autofillManager.hasEnabledAutofillServices()
init {
val resetObservable = dispatcher.register
.filter { it == LifecycleAction.UserReset }
.map { SettingAction.Reset }
dispatcher.register
.filterByType(SettingAction::class.java)
.mergeWith(resetObservable)
.subscribe {
val edit = preferences.edit()
when (it) {
is SettingAction.SendUsageData ->
edit.putBoolean(Keys.SEND_USAGE_DATA, it.sendUsageData)
is SettingAction.UseLocalService ->
edit.putBoolean(Keys.USE_LOCAL_SERVICE, it.useLocalService)
is SettingAction.ItemListSortOrder ->
edit.putString(Keys.ITEM_LIST_SORT_ORDER, it.sortOrder.name)
is SettingAction.UnlockWithFingerprint ->
edit.putBoolean(Keys.UNLOCK_WITH_FINGERPRINT, it.unlockWithFingerprint)
is SettingAction.AutoLockTime ->
edit.putString(Keys.AUTO_LOCK_TIME, it.time.name)
is SettingAction.UnlockWithFingerprintPendingAuth ->
edit.putBoolean(Keys.UNLOCK_WITH_FINGERPRINT_PENDING_AUTH, it.unlockWithFingerprintPendingAuth)
is SettingAction.Autofill ->
handleAutofill(it.enable)
is SettingAction.Reset -> {
edit.putBoolean(Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
edit.putBoolean(Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
edit.putString(Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
edit.putBoolean(Keys.UNLOCK_WITH_FINGERPRINT, Constant.SettingDefault.unlockWithFingerprint)
edit.putString(Keys.AUTO_LOCK_TIME, Constant.SettingDefault.autoLockTime.name)
handleAutofill(false)
}
}
edit.apply()
}
.addTo(compositeDisposable)
}
override fun injectContext(context: Context) {
val chinaBuild = context.resources.configuration.locales.get(0).toString().equals("zh_CN_#Hans") &&
context.resources.configuration.locales.get(0).displayLanguage.equals("中文") &&
(!"com.android.vending".equals(context.packageManager.getInstallerPackageName(context.packageName)))
preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (chinaBuild) {
preferences.edit().putBoolean(Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceTrue).apply()
} else {
preferences.edit().putBoolean(Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse).apply()
}
val rxPrefs = RxSharedPreferences.create(preferences)
rxPrefs
.getBoolean(Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
.asObservable()
.subscribe(sendUsageData as Subject)
if (chinaBuild) {
rxPrefs
.getBoolean(Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceTrue)
.asObservable()
.subscribe(useLocalService as Subject)
} else {
rxPrefs
.getBoolean(Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
.asObservable()
.subscribe(useLocalService as Subject)
}
rxPrefs
.getString(Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
.asObservable()
.map {
Setting.ItemListSort.valueOf(it)
}
.subscribe(itemListSortOrder as Subject)
rxPrefs
.getBoolean(Keys.UNLOCK_WITH_FINGERPRINT, Constant.SettingDefault.unlockWithFingerprint)
.asObservable()
.subscribe(unlockWithFingerprint as Subject)
unlockWithFingerprintPendingAuth = rxPrefs.getBoolean(Keys.UNLOCK_WITH_FINGERPRINT_PENDING_AUTH).asObservable()
val defaultAutoLockTime =
if (fingerprintStore.isDeviceSecure) Constant.SettingDefault.autoLockTime else Constant.SettingDefault.noSecurityAutoLockTime
rxPrefs
.getString(Keys.AUTO_LOCK_TIME, defaultAutoLockTime.name)
.asObservable()
.map {
Setting.AutoLockTime.valueOf(it)
}
.subscribe(_autoLockTime)
rxPrefs
.getBoolean(Keys.DEVICE_SECURITY_PRESENT, fingerprintStore.isDeviceSecure)
.asObservable()
.subscribe(_deviceSecurityWasPresent)
if (!preferences.contains(Keys.DEVICE_SECURITY_PRESENT)) {
preferences.edit()
.putBoolean(Keys.DEVICE_SECURITY_PRESENT, fingerprintStore.isDeviceSecure)
.apply()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
autofillManager = context.getSystemService(AutofillManager::class.java)
}
}
private fun autoLockSetting() = Observables.combineLatest(_autoLockTime, _deviceSecurityWasPresent)
.doOnNext {
if (it.second != fingerprintStore.isDeviceSecure) {
updateFromDeviceSecurityChange()
}
}
.filter { it.second == fingerprintStore.isDeviceSecure }
.map { it.first }
private fun updateFromDeviceSecurityChange() {
val newAutoLockTime =
if (fingerprintStore.isDeviceSecure) Constant.SettingDefault.autoLockTime else Constant.SettingDefault.noSecurityAutoLockTime
val editor = preferences.edit()
editor.putString(Keys.AUTO_LOCK_TIME, newAutoLockTime.name)
editor.putBoolean(Keys.DEVICE_SECURITY_PRESENT, fingerprintStore.isDeviceSecure)
editor.apply()
}
private fun handleAutofill(enable: Boolean) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
if (!enable && isCurrentAutofillProvider) {
autofillManager.disableAutofillServices()
}
}
}