/
PreferencesFragment.kt
431 lines (389 loc) 路 20.7 KB
/
PreferencesFragment.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Copyright 2016-2020 Open Food Facts
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package openfoodfacts.github.scrachx.openfood.features
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.Intent.ACTION_VIEW
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.SearchRecentSuggestions
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.edit
import androidx.core.content.pm.PackageInfoCompat
import androidx.core.net.toUri
import androidx.preference.*
import androidx.preference.Preference.OnPreferenceClickListener
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.afollestad.materialdialogs.MaterialDialog
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import openfoodfacts.github.scrachx.openfood.AppFlavors
import openfoodfacts.github.scrachx.openfood.AppFlavors.OBF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OPFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.isFlavors
import openfoodfacts.github.scrachx.openfood.BuildConfig
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.analytics.AnalyticsEvent
import openfoodfacts.github.scrachx.openfood.analytics.MatomoAnalytics
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabActivityHelper
import openfoodfacts.github.scrachx.openfood.customtabs.WebViewFallback
import openfoodfacts.github.scrachx.openfood.jobs.LoadTaxonomiesWorker
import openfoodfacts.github.scrachx.openfood.jobs.OfflineProductWorker.Companion.scheduleSync
import openfoodfacts.github.scrachx.openfood.models.DaoSession
import openfoodfacts.github.scrachx.openfood.models.entities.analysistag.AnalysisTagNameDao
import openfoodfacts.github.scrachx.openfood.models.entities.analysistagconfig.AnalysisTagConfig
import openfoodfacts.github.scrachx.openfood.models.entities.analysistagconfig.AnalysisTagConfigDao
import openfoodfacts.github.scrachx.openfood.models.entities.country.CountryName
import openfoodfacts.github.scrachx.openfood.models.entities.country.CountryNameDao
import openfoodfacts.github.scrachx.openfood.utils.*
import openfoodfacts.github.scrachx.openfood.utils.LocaleHelper.getLanguage
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.NavigationDrawerType
import org.greenrobot.greendao.async.AsyncOperation
import org.greenrobot.greendao.async.AsyncOperationListener
import org.greenrobot.greendao.query.WhereCondition.StringCondition
import java.util.*
import javax.inject.Inject
@AndroidEntryPoint
class PreferencesFragment : PreferenceFragmentCompat(), INavigationItem, OnSharedPreferenceChangeListener {
private val disp = CompositeDisposable()
@Inject
lateinit var daoSession: DaoSession
@Inject
lateinit var matomoAnalytics: MatomoAnalytics
override val navigationDrawerListener: NavigationDrawerListener? by lazy {
if (activity is NavigationDrawerListener) activity as NavigationDrawerListener
else null
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
menu.findItem(R.id.action_search).isVisible = false
}
override fun onCreatePreferences(bundle: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
setHasOptionsMenu(true)
val settings = requireActivity().getSharedPreferences("prefs", 0)
initLanguageCell()
requirePreference<ListPreference>(getString(R.string.pref_app_theme_key)).let {
it.setEntries(R.array.application_theme_entries)
it.setEntryValues(R.array.application_theme_entries)
it.setOnPreferenceChangeListener { _, value ->
when (value) {
getString(R.string.day) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
getString(R.string.night) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
getString(R.string.follow_system) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
true
}
}
requirePreference<Preference>(getString(R.string.pref_delete_history_key)).setOnPreferenceChangeListener { _, _ ->
MaterialDialog.Builder(requireActivity()).run {
content(R.string.pref_delete_history_dialog_content)
positiveText(R.string.delete_txt)
onPositive { _, _ ->
Toast.makeText(requireContext(), getString(R.string.pref_delete_history), Toast.LENGTH_SHORT).show()
SearchRecentSuggestions(requireContext(), SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE).clearHistory()
}
neutralText(R.string.dialog_cancel)
onNeutral { dialog, _ -> dialog.dismiss() }
show()
}
true
}
requirePreference<SwitchPreference>(getString(R.string.pref_scanner_type_key)).let {
it.isVisible = BuildConfig.USE_MLKIT
it.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue == true) {
MaterialDialog.Builder(requireActivity()).run {
title(R.string.preference_choose_scanner_dialog_title)
content(R.string.preference_choose_scanner_dialog_body)
positiveText(R.string.proceed)
onPositive { _, _ ->
it.isChecked = true
settings.edit { putBoolean(getString(R.string.pref_scanner_type_key), newValue as Boolean) }
Toast.makeText(requireActivity(), getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
}
negativeText(R.string.dialog_cancel)
onNegative { dialog, _ ->
dialog.dismiss()
it.isChecked = false
settings.edit { putBoolean(getString(R.string.pref_scanner_type_key), false) }
}
show()
}
} else {
it.isChecked = false
settings.edit { putBoolean(getString(R.string.pref_scanner_type_key), newValue as Boolean) }
Toast.makeText(requireActivity(), getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
}
true
}
}
val countryPreference = requirePreference<ListPreference>(getString(R.string.pref_country_key))
val asyncSessionCountries = daoSession.startAsyncSession()
val countryNameDao = daoSession.countryNameDao
// Set query finish listener
asyncSessionCountries.listenerMainThread = AsyncOperationListener { operation: AsyncOperation ->
val countryNames = operation.result as List<CountryName>
countryPreference.entries = countryNames.map { it.name }.toTypedArray()
countryPreference.entryValues = countryNames.map { it.countyTag }.toTypedArray()
}
// Execute query
asyncSessionCountries.queryList(countryNameDao.queryBuilder()
.where(CountryNameDao.Properties.LanguageCode.eq(getLanguage(requireActivity())))
.orderAsc(CountryNameDao.Properties.Name).build())
countryPreference.setOnPreferenceChangeListener { preference, newValue ->
val country = newValue as String?
settings.edit { putString(preference.key, country) }
Toast.makeText(context, getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
true
}
requirePreference<Preference>(getString(R.string.pref_contact_us_key)).setOnPreferenceChangeListener { _, _ ->
try {
startActivity(Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse(getString(R.string.off_mail))
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
} catch (e: ActivityNotFoundException) {
Toast.makeText(requireActivity(), R.string.email_not_found, Toast.LENGTH_SHORT).show()
}
true
}
requirePreference<Preference>(getString(R.string.pref_rate_us_key)).setOnPreferenceChangeListener { _, _ ->
try {
startActivity(Intent(ACTION_VIEW,
Uri.parse("market://details?id=${requireActivity().packageName}")))
} catch (e: ActivityNotFoundException) {
startActivity(Intent(ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=${requireActivity().packageName}")))
}
true
}
requirePreference<Preference>(getString(R.string.pref_faq_key))
.setOnPreferenceClickListener { openWebCustomTab(R.string.faq_url) }
requirePreference<Preference>(getString(R.string.pref_terms_key))
.setOnPreferenceClickListener { openWebCustomTab(R.string.terms_url) }
requirePreference<Preference>(getString(R.string.pref_help_translate_key))
.setOnPreferenceClickListener { openWebCustomTab(R.string.translate_url) }
requirePreference<ListPreference>(getString(R.string.pref_energy_unit_key)).let {
it.setEntries(R.array.energy_units)
it.setEntryValues(R.array.energy_units)
it.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
settings.edit { putString(getString(R.string.pref_energy_unit_key), newValue as String?) }
Toast.makeText(requireActivity(), getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
true
}
}
requirePreference<ListPreference>(getString(R.string.pref_volume_unit_key)).let {
it.setEntries(R.array.volume_units)
it.setEntryValues(R.array.volume_units)
it.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
settings.edit { putString(getString(R.string.pref_volume_unit_key), newValue as String?) }
Toast.makeText(requireActivity(), getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
true
}
}
requirePreference<ListPreference>(getString(R.string.pref_resolution_key)).let {
it.setEntries(R.array.upload_image)
it.setEntryValues(R.array.upload_image)
it.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
settings.edit { putString(getString(R.string.pref_resolution_key), newValue as String?) }
Toast.makeText(requireActivity(), getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
true
}
}
// Disable photo mode for OpenProductFacts
if (isFlavors(AppFlavors.OPF)) {
requirePreference<Preference>(getString(R.string.pref_show_product_photos_key)).isVisible = false
}
// Preference to show version name
requirePreference<Preference>(getString(R.string.pref_version_key)).let {
try {
val pInfo = requireActivity().packageManager.getPackageInfo(requireActivity().packageName, 0)
val version = pInfo.versionName
val versionCode = PackageInfoCompat.getLongVersionCode(pInfo)
it.summary = "${getString(R.string.version_string)} $version ($versionCode)"
} catch (e: PackageManager.NameNotFoundException) {
Log.e(PreferencesFragment::class.simpleName, "onCreatePreferences", e)
}
if (isFlavors(OFF, OBF, OPFF)) {
getAnalysisTagConfigs(daoSession)
} else {
preferenceScreen.removePreference(preferenceScreen.requirePreference(getString(R.string.pref_key_display)))
}
}
requirePreference<SwitchPreference>(getString(R.string.pref_analytics_reporting_key)).let {
it.setOnPreferenceChangeListener { _, newValue ->
matomoAnalytics.onAnalyticsEnabledToggled(newValue == true)
true
}
}
}
private fun buildDisplayCategory(configs: List<AnalysisTagConfig>) {
if (!isAdded) return
val displayCategory = preferenceScreen.requirePreference<PreferenceCategory>(getString(R.string.pref_key_display))
displayCategory.removeAll()
preferenceScreen.addPreference(displayCategory)
// If analysis tag is empty show "Load ingredient detection data" option in order to manually reload taxonomies
if (configs.isNotEmpty()) {
configs.forEach { config ->
displayCategory.addPreference(CheckBoxPreference(this.context).apply {
key = config.type
setDefaultValue(true)
summary = null
summaryOn = null
summaryOff = null
title = getString(R.string.display_analysis_tag_status, config.typeName.toLowerCase(Locale.getDefault()))
setOnPreferenceChangeListener { _, newValue ->
val event = if (newValue == true) {
AnalyticsEvent.IngredientAnalysisEnabled(config.type)
} else {
AnalyticsEvent.IngredientAnalysisDisabled(config.type)
}
matomoAnalytics.trackEvent(event)
true
}
})
}
} else {
val preference = Preference(preferenceScreen.context).apply {
setTitle(R.string.load_ingredient_detection_data)
setSummary(R.string.load_ingredient_detection_data_summary)
onPreferenceClickListener = OnPreferenceClickListener { pref ->
pref.onPreferenceClickListener = null
val request = OneTimeWorkRequest.from(LoadTaxonomiesWorker::class.java)
// The service will load server resources only if newer than already downloaded...
WorkManager.getInstance(requireContext()).let { manager ->
manager.enqueue(request)
manager.getWorkInfoByIdLiveData(request.id).observe(this@PreferencesFragment, { workInfo: WorkInfo? ->
if (workInfo != null) {
if (workInfo.state == WorkInfo.State.RUNNING) {
pref.setTitle(R.string.please_wait)
pref.setIcon(R.drawable.ic_cloud_download_black_24dp)
pref.summary = null
pref.widgetLayoutResource = R.layout.loading
} else if (workInfo.state == WorkInfo.State.SUCCEEDED) {
getAnalysisTagConfigs(daoSession)
}
}
})
}
true
}
}
displayCategory.addPreference(preference)
}
displayCategory.isVisible = true
}
private fun openWebCustomTab(@StringRes resId: Int): Boolean {
val customTabsIntent = CustomTabsIntent.Builder().build().apply {
intent.putExtra("android.intent.extra.REFERRER", "android-app://${requireContext().packageName}".toUri())
}
CustomTabActivityHelper.openCustomTab(requireActivity(), customTabsIntent, getString(resId).toUri(), WebViewFallback())
return true
}
@NavigationDrawerType
override fun getNavigationDrawerType() = NavigationDrawerListener.ITEM_PREFERENCES
override fun onResume() {
super.onResume()
try {
(this.activity as? AppCompatActivity)?.supportActionBar!!.title = getString(R.string.action_preferences)
} catch (e: NullPointerException) {
throw IllegalStateException("Preference fragment not attached to AppCompatActivity.")
}
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
super.onPause()
}
override fun onDestroy() {
disp.dispose()
super.onDestroy()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when (key) {
getString(R.string.pref_enable_mobile_data_key) -> scheduleSync(requireContext())
}
}
private fun getAnalysisTagConfigs(daoSession: DaoSession) {
val language = getLanguage(requireContext())
Single.fromCallable {
val analysisTagConfigDao = daoSession.analysisTagConfigDao
val analysisTagConfigs = analysisTagConfigDao.queryBuilder()
.where(StringCondition("1 GROUP BY type"))
.orderAsc(AnalysisTagConfigDao.Properties.Type).build().list()
val analysisTagNameDao = daoSession.analysisTagNameDao
analysisTagConfigs.forEach { config ->
val type = "en:${config.type}"
var analysisTagTypeName = analysisTagNameDao.queryBuilder().where(
AnalysisTagNameDao.Properties.AnalysisTag.eq(type),
AnalysisTagNameDao.Properties.LanguageCode.eq(language),
).unique()
if (analysisTagTypeName == null) {
analysisTagTypeName = analysisTagNameDao.queryBuilder().where(
AnalysisTagNameDao.Properties.AnalysisTag.eq(type),
AnalysisTagNameDao.Properties.LanguageCode.eq("en")
).unique()
}
config.typeName = if (analysisTagTypeName != null) analysisTagTypeName.name else config.type
}
analysisTagConfigs
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { configs: List<AnalysisTagConfig> -> buildDisplayCategory(configs) }
.addTo(disp)
}
private fun initLanguageCell() {
val localesWithNames = SupportedLanguages.codes()
.map {
val locale = LocaleHelper.getLocale(it)
it to locale.getDisplayName(locale).capitalize(locale)
}
requirePreference<ListPreference>(getString(R.string.pref_language_key)).let { preference ->
preference.entries = localesWithNames.map { it.second }.toTypedArray()
preference.entryValues = localesWithNames.map { it.first }.toTypedArray()
preference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, locale: Any? ->
val configuration = requireActivity().resources.configuration
Toast.makeText(context, getString(R.string.changes_saved), Toast.LENGTH_SHORT).show()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(LocaleHelper.getLocale(locale as String?))
requireActivity().recreate()
}
true
}
}
}
companion object {
const val LOGIN_PREF = "login"
fun newInstance() = PreferencesFragment().apply { arguments = Bundle() }
}
}