You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.7 KiB
Kotlin
124 lines
4.7 KiB
Kotlin
6 years ago
|
/* 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/. */
|
||
|
|
||
5 years ago
|
package org.mozilla.fenix.settings.sitepermissions
|
||
6 years ago
|
|
||
|
import android.os.Bundle
|
||
|
import androidx.appcompat.app.AppCompatActivity
|
||
5 years ago
|
import androidx.lifecycle.lifecycleScope
|
||
6 years ago
|
import androidx.navigation.Navigation
|
||
|
import androidx.preference.Preference
|
||
|
import androidx.preference.PreferenceFragmentCompat
|
||
|
import kotlinx.coroutines.Dispatchers.IO
|
||
|
import kotlinx.coroutines.Dispatchers.Main
|
||
|
import kotlinx.coroutines.launch
|
||
|
import mozilla.components.feature.sitepermissions.SitePermissions
|
||
|
import org.jetbrains.anko.alert
|
||
|
import org.jetbrains.anko.noButton
|
||
|
import org.jetbrains.anko.yesButton
|
||
|
import org.mozilla.fenix.R
|
||
|
import org.mozilla.fenix.ext.components
|
||
5 years ago
|
import org.mozilla.fenix.ext.getPreferenceKey
|
||
5 years ago
|
import org.mozilla.fenix.settings.PhoneFeature
|
||
6 years ago
|
import org.mozilla.fenix.settings.PhoneFeature.CAMERA
|
||
|
import org.mozilla.fenix.settings.PhoneFeature.LOCATION
|
||
5 years ago
|
import org.mozilla.fenix.settings.PhoneFeature.MICROPHONE
|
||
6 years ago
|
import org.mozilla.fenix.settings.PhoneFeature.NOTIFICATION
|
||
|
|
||
|
@SuppressWarnings("TooManyFunctions")
|
||
5 years ago
|
class SitePermissionsDetailsExceptionsFragment : PreferenceFragmentCompat() {
|
||
6 years ago
|
private lateinit var sitePermissions: SitePermissions
|
||
|
|
||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||
|
super.onCreate(savedInstanceState)
|
||
|
|
||
|
sitePermissions = SitePermissionsDetailsExceptionsFragmentArgs
|
||
|
.fromBundle(requireArguments())
|
||
|
.sitePermissions
|
||
|
}
|
||
|
|
||
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||
|
setPreferencesFromResource(R.xml.site_permissions_details_exceptions_preferences, rootKey)
|
||
|
}
|
||
|
|
||
|
override fun onResume() {
|
||
|
super.onResume()
|
||
5 years ago
|
(activity as AppCompatActivity).apply {
|
||
|
title = sitePermissions.origin
|
||
|
supportActionBar?.show()
|
||
|
}
|
||
5 years ago
|
lifecycleScope.launch(IO) {
|
||
6 years ago
|
val context = requireContext()
|
||
5 years ago
|
sitePermissions =
|
||
|
requireNotNull(context.components.core.permissionStorage.findSitePermissionsBy(sitePermissions.origin))
|
||
6 years ago
|
launch(Main) {
|
||
|
bindCategoryPhoneFeatures()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun bindCategoryPhoneFeatures() {
|
||
|
val context = requireContext()
|
||
|
|
||
|
val cameraAction = CAMERA.getActionLabel(context, sitePermissions)
|
||
|
val locationAction = LOCATION.getActionLabel(context, sitePermissions)
|
||
|
val microphoneAction = MICROPHONE.getActionLabel(context, sitePermissions)
|
||
|
val notificationAction = NOTIFICATION.getActionLabel(context, sitePermissions)
|
||
|
|
||
|
initPhoneFeature(CAMERA, cameraAction)
|
||
|
initPhoneFeature(LOCATION, locationAction)
|
||
|
initPhoneFeature(MICROPHONE, microphoneAction)
|
||
|
initPhoneFeature(NOTIFICATION, notificationAction)
|
||
|
bindClearPermissionsButton()
|
||
|
}
|
||
|
|
||
|
private fun initPhoneFeature(phoneFeature: PhoneFeature, summary: String) {
|
||
|
val keyPreference = phoneFeature.getPreferenceKey(requireContext())
|
||
|
val cameraPhoneFeatures: Preference = requireNotNull(findPreference(keyPreference))
|
||
|
cameraPhoneFeatures.summary = summary
|
||
|
|
||
|
cameraPhoneFeatures.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||
|
navigateToPhoneFeature(phoneFeature)
|
||
|
true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun bindClearPermissionsButton() {
|
||
5 years ago
|
val keyPreference = getPreferenceKey(R.string.pref_key_exceptions_clear_site_permissions)
|
||
6 years ago
|
val button: Preference = requireNotNull(findPreference(keyPreference))
|
||
|
|
||
|
button.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||
|
requireContext().alert(
|
||
|
R.string.confirm_clear_permissions_site,
|
||
|
R.string.clear_permissions
|
||
|
) {
|
||
|
yesButton {
|
||
|
clearSitePermissions()
|
||
|
}
|
||
|
noButton { }
|
||
|
}.show()
|
||
|
|
||
|
true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun clearSitePermissions() {
|
||
5 years ago
|
lifecycleScope.launch(IO) {
|
||
5 years ago
|
requireContext().components.core.permissionStorage.deleteSitePermissions(sitePermissions)
|
||
6 years ago
|
launch(Main) {
|
||
|
Navigation.findNavController(requireNotNull(view)).popBackStack()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun navigateToPhoneFeature(phoneFeature: PhoneFeature) {
|
||
|
val directions =
|
||
|
SitePermissionsDetailsExceptionsFragmentDirections.actionSitePermissionsToExceptionsToManagePhoneFeature(
|
||
|
phoneFeatureId = phoneFeature.id,
|
||
|
sitePermissions = sitePermissions
|
||
|
)
|
||
|
Navigation.findNavController(view!!).navigate(directions)
|
||
|
}
|
||
|
}
|