2019-03-29 18:57:49 +01:00
|
|
|
/* 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.fenix.settings
|
|
|
|
|
2019-05-01 03:05:50 +02:00
|
|
|
import android.widget.RadioButton
|
2020-06-15 20:03:33 +02:00
|
|
|
import androidx.annotation.StringRes
|
2019-12-17 05:35:36 +01:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
2019-08-21 17:38:14 +02:00
|
|
|
import androidx.preference.Preference
|
2020-06-15 20:03:33 +02:00
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2019-04-02 05:40:40 +02:00
|
|
|
import mozilla.components.feature.sitepermissions.SitePermissions
|
2020-06-26 20:04:46 +02:00
|
|
|
import mozilla.components.support.ktx.android.content.res.resolveAttribute
|
2019-07-30 18:50:50 +02:00
|
|
|
import mozilla.components.support.ktx.android.view.putCompoundDrawablesRelative
|
2020-06-15 20:03:33 +02:00
|
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
2019-03-29 18:57:49 +01:00
|
|
|
|
2019-04-08 02:24:22 +02:00
|
|
|
fun SitePermissions.toggle(featurePhone: PhoneFeature): SitePermissions {
|
2020-06-08 22:02:42 +02:00
|
|
|
return update(featurePhone, get(featurePhone).toggle())
|
|
|
|
}
|
|
|
|
|
|
|
|
fun SitePermissions.get(field: PhoneFeature) = when (field) {
|
|
|
|
PhoneFeature.CAMERA -> camera
|
|
|
|
PhoneFeature.LOCATION -> location
|
|
|
|
PhoneFeature.MICROPHONE -> microphone
|
|
|
|
PhoneFeature.NOTIFICATION -> notification
|
|
|
|
PhoneFeature.AUTOPLAY_AUDIBLE -> autoplayAudible
|
|
|
|
PhoneFeature.AUTOPLAY_INAUDIBLE -> autoplayInaudible
|
|
|
|
}
|
|
|
|
|
|
|
|
fun SitePermissions.update(field: PhoneFeature, value: SitePermissions.Status) = when (field) {
|
|
|
|
PhoneFeature.CAMERA -> copy(camera = value)
|
|
|
|
PhoneFeature.LOCATION -> copy(location = value)
|
|
|
|
PhoneFeature.MICROPHONE -> copy(microphone = value)
|
|
|
|
PhoneFeature.NOTIFICATION -> copy(notification = value)
|
|
|
|
PhoneFeature.AUTOPLAY_AUDIBLE -> copy(autoplayAudible = value)
|
|
|
|
PhoneFeature.AUTOPLAY_INAUDIBLE -> copy(autoplayInaudible = value)
|
2019-04-11 22:04:51 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 15:22:05 +02:00
|
|
|
/**
|
|
|
|
* In devices with Android 6, when we use android:button="@null" android:drawableStart doesn't work via xml
|
|
|
|
* as a result we have to apply it programmatically. More info about this issue https://github.com/mozilla-mobile/fenix/issues/1414
|
|
|
|
*/
|
2019-05-01 03:05:50 +02:00
|
|
|
fun RadioButton.setStartCheckedIndicator() {
|
2020-06-26 20:04:46 +02:00
|
|
|
val attr = context.theme.resolveAttribute(android.R.attr.listChoiceIndicatorSingle)
|
2019-12-17 05:35:36 +01:00
|
|
|
val buttonDrawable = AppCompatResources.getDrawable(context, attr)
|
2019-06-25 15:22:05 +02:00
|
|
|
buttonDrawable?.apply {
|
2019-07-30 18:50:50 +02:00
|
|
|
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
|
2019-05-01 03:05:50 +02:00
|
|
|
}
|
2019-07-30 18:50:50 +02:00
|
|
|
putCompoundDrawablesRelative(start = buttonDrawable)
|
2019-05-01 03:05:50 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 17:38:14 +02:00
|
|
|
/**
|
|
|
|
* Sets the callback to be invoked when this preference is changed by the user (but before
|
|
|
|
* the internal state has been updated). Allows the type of the preference to be specified.
|
|
|
|
* If the new value doesn't match the preference type the listener isn't called.
|
|
|
|
*
|
|
|
|
* @param onPreferenceChangeListener The callback to be invoked
|
|
|
|
*/
|
|
|
|
inline fun <reified T> Preference.setOnPreferenceChangeListener(
|
|
|
|
crossinline onPreferenceChangeListener: (Preference, T) -> Boolean
|
|
|
|
) {
|
|
|
|
setOnPreferenceChangeListener { preference: Preference, newValue: Any ->
|
|
|
|
(newValue as? T)?.let { onPreferenceChangeListener(preference, it) } ?: false
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 20:03:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a preference with the corresponding key and throw if it does not exist.
|
|
|
|
* @param preferenceId Resource ID from preference_keys
|
|
|
|
*/
|
|
|
|
fun <T : Preference> PreferenceFragmentCompat.requirePreference(@StringRes preferenceId: Int) =
|
|
|
|
requireNotNull(findPreference<T>(getPreferenceKey(preferenceId)))
|