This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 713
/
ExperimentsSettingsFragment.kt
58 lines (50 loc) · 2.3 KB
/
ExperimentsSettingsFragment.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
/* 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 org.mozilla.focus.settings
import android.content.SharedPreferences
import android.os.Bundle
import android.support.v7.preference.SwitchPreferenceCompat
import android.support.v7.preference.PreferenceFragmentCompat
import com.jakewharton.processphoenix.ProcessPhoenix
import org.mozilla.focus.R
import org.mozilla.focus.utils.app
import org.mozilla.focus.utils.experimentDescriptor
import org.mozilla.focus.utils.isInExperiment
import org.mozilla.focus.web.Config
import org.mozilla.focus.web.ENGINE_PREF_STRING_KEY
class ExperimentsSettingsFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener {
companion object {
const val FRAGMENT_TAG = "ExperimentSettings"
}
private var rendererPreferenceChanged = false
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.experiments_settings)
val enginePref: SwitchPreferenceCompat? = preferenceManager
.findPreference(ENGINE_PREF_STRING_KEY) as SwitchPreferenceCompat?
enginePref?.isChecked = activity!!.isInExperiment(experimentDescriptor)
}
override fun onResume() {
super.onResume()
preferenceScreen?.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceScreen?.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
if (rendererPreferenceChanged) {
val launcherIntent = activity?.packageManager?.getLaunchIntentForPackage(activity!!.packageName)
ProcessPhoenix.triggerRebirth(context, launcherIntent)
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
ENGINE_PREF_STRING_KEY -> {
rendererPreferenceChanged = true
activity!!.app.fretboard.setOverride(
activity!!.app, experimentDescriptor,
sharedPreferences!!.getBoolean(key, Config.DEFAULT_NEW_RENDERER))
}
}
}
}