2019-04-02 05:40:40 +02: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.quicksettings
|
|
|
|
|
2019-04-08 02:24:22 +02:00
|
|
|
import android.content.Context
|
2019-04-02 05:40:40 +02:00
|
|
|
import android.view.ViewGroup
|
2019-04-08 02:24:22 +02:00
|
|
|
import mozilla.components.feature.sitepermissions.SitePermissions
|
|
|
|
import mozilla.components.support.ktx.kotlin.toUri
|
|
|
|
import org.mozilla.fenix.ext.components
|
2019-04-02 05:40:40 +02:00
|
|
|
import org.mozilla.fenix.mvi.Action
|
|
|
|
import org.mozilla.fenix.mvi.ActionBusFactory
|
|
|
|
import org.mozilla.fenix.mvi.Change
|
|
|
|
import org.mozilla.fenix.mvi.UIComponent
|
|
|
|
import org.mozilla.fenix.mvi.UIView
|
|
|
|
import org.mozilla.fenix.mvi.ViewState
|
|
|
|
import org.mozilla.fenix.settings.PhoneFeature
|
2019-04-08 02:24:22 +02:00
|
|
|
import org.mozilla.fenix.settings.toStatus
|
|
|
|
import org.mozilla.fenix.settings.toggle
|
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-04-02 05:40:40 +02:00
|
|
|
|
|
|
|
class QuickSettingsComponent(
|
|
|
|
private val container: ViewGroup,
|
|
|
|
bus: ActionBusFactory,
|
|
|
|
override var initialState: QuickSettingsState
|
|
|
|
) : UIComponent<QuickSettingsState, QuickSettingsAction, QuickSettingsChange>(
|
|
|
|
bus.getManagedEmitter(QuickSettingsAction::class.java),
|
|
|
|
bus.getSafeManagedObservable(QuickSettingsChange::class.java)
|
|
|
|
) {
|
|
|
|
override val reducer: (QuickSettingsState, QuickSettingsChange) -> QuickSettingsState = { state, change ->
|
|
|
|
when (change) {
|
|
|
|
is QuickSettingsChange.Change -> {
|
|
|
|
state.copy(
|
|
|
|
mode = QuickSettingsState.Mode.Normal(
|
|
|
|
change.url,
|
|
|
|
change.isSecured,
|
2019-04-08 02:24:22 +02:00
|
|
|
change.sitePermissions
|
2019-04-02 05:40:40 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
is QuickSettingsChange.PermissionGranted -> {
|
|
|
|
state.copy(
|
2019-04-08 02:24:22 +02:00
|
|
|
mode = QuickSettingsState.Mode.ActionLabelUpdated(change.phoneFeature, change.sitePermissions)
|
2019-04-02 05:40:40 +02:00
|
|
|
)
|
|
|
|
}
|
2019-04-08 02:24:22 +02:00
|
|
|
is QuickSettingsChange.PromptRestarted -> {
|
2019-04-02 05:40:40 +02:00
|
|
|
state.copy(
|
2019-04-08 02:24:22 +02:00
|
|
|
mode = QuickSettingsState.Mode.CheckPendingFeatureBlockedByAndroid(change.sitePermissions)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
is QuickSettingsChange.Stored -> {
|
|
|
|
state.copy(
|
|
|
|
mode = QuickSettingsState.Mode.ActionLabelUpdated(change.phoneFeature, change.sitePermissions)
|
2019-04-02 05:40:40 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun initView(): UIView<QuickSettingsState, QuickSettingsAction, QuickSettingsChange> {
|
|
|
|
return QuickSettingsUIView(container, actionEmitter, changesObservable, container)
|
|
|
|
}
|
|
|
|
|
|
|
|
init {
|
|
|
|
render(reducer)
|
|
|
|
}
|
2019-04-08 02:24:22 +02:00
|
|
|
|
|
|
|
fun toggleSitePermission(
|
|
|
|
context: Context,
|
|
|
|
featurePhone: PhoneFeature,
|
|
|
|
url: String,
|
|
|
|
sitePermissions: SitePermissions?
|
|
|
|
): SitePermissions {
|
|
|
|
|
|
|
|
return if (sitePermissions == null) {
|
|
|
|
val settings = Settings.getInstance(context)
|
|
|
|
val origin = requireNotNull(url.toUri().host)
|
|
|
|
var location = settings.getSitePermissionsPhoneFeatureLocation().toStatus()
|
|
|
|
var camera = settings.getSitePermissionsPhoneFeatureCameraAction().toStatus()
|
|
|
|
var microphone = settings.getSitePermissionsPhoneFeatureMicrophoneAction().toStatus()
|
|
|
|
var notification = settings.getSitePermissionsPhoneFeatureNotificationAction().toStatus()
|
|
|
|
|
|
|
|
when (featurePhone) {
|
|
|
|
PhoneFeature.CAMERA -> camera = camera.toggle()
|
|
|
|
PhoneFeature.LOCATION -> location = location.toggle()
|
|
|
|
PhoneFeature.MICROPHONE -> microphone = microphone.toggle()
|
|
|
|
PhoneFeature.NOTIFICATION -> notification = notification.toggle()
|
|
|
|
}
|
2019-04-11 22:04:51 +02:00
|
|
|
context.components.storage.addSitePermissionException(origin, location, notification, microphone, camera)
|
2019-04-08 02:24:22 +02:00
|
|
|
} else {
|
|
|
|
val updatedSitePermissions = sitePermissions.toggle(featurePhone)
|
|
|
|
context.components.storage.updateSitePermissions(updatedSitePermissions)
|
|
|
|
updatedSitePermissions
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data class QuickSettingsState(val mode: Mode) : ViewState {
|
|
|
|
sealed class Mode {
|
2019-04-08 02:24:22 +02:00
|
|
|
data class Normal(val url: String, val isSecured: Boolean, val sitePermissions: SitePermissions?) : Mode()
|
|
|
|
data class ActionLabelUpdated(val phoneFeature: PhoneFeature, val sitePermissions: SitePermissions?) :
|
|
|
|
Mode()
|
|
|
|
|
|
|
|
data class CheckPendingFeatureBlockedByAndroid(val sitePermissions: SitePermissions?) : Mode()
|
2019-04-02 05:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class QuickSettingsAction : Action {
|
|
|
|
data class SelectBlockedByAndroid(val permissions: Array<String>) : QuickSettingsAction()
|
2019-04-08 02:24:22 +02:00
|
|
|
data class TogglePermission(val featurePhone: PhoneFeature) : QuickSettingsAction()
|
2019-04-02 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sealed class QuickSettingsChange : Change {
|
|
|
|
data class Change(
|
|
|
|
val url: String,
|
|
|
|
val isSecured: Boolean,
|
2019-04-08 02:24:22 +02:00
|
|
|
val sitePermissions: SitePermissions?
|
2019-04-02 05:40:40 +02:00
|
|
|
) : QuickSettingsChange()
|
|
|
|
|
2019-04-08 02:24:22 +02:00
|
|
|
data class PermissionGranted(val phoneFeature: PhoneFeature, val sitePermissions: SitePermissions?) :
|
|
|
|
QuickSettingsChange()
|
|
|
|
|
|
|
|
data class PromptRestarted(val sitePermissions: SitePermissions?) : QuickSettingsChange()
|
|
|
|
data class Stored(val phoneFeature: PhoneFeature, val sitePermissions: SitePermissions?) : QuickSettingsChange()
|
2019-04-02 05:40:40 +02:00
|
|
|
}
|