[fenix] Closes https://github.com/mozilla-mobile/fenix/issues/1078: Added UI for managing phone feature permissions.
parent
c2ee06d7d9
commit
3000707381
@ -0,0 +1,164 @@
|
||||
/* 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
|
||||
|
||||
import android.Manifest.permission.ACCESS_COARSE_LOCATION
|
||||
import android.Manifest.permission.CAMERA
|
||||
import android.Manifest.permission.RECORD_AUDIO
|
||||
import android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.provider.Settings
|
||||
import android.text.SpannableString
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.Spanned.SPAN_EXCLUSIVE_INCLUSIVE
|
||||
import android.text.style.AbsoluteSizeSpan
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.View.VISIBLE
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.RadioButton
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.text.HtmlCompat
|
||||
import androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT
|
||||
import androidx.fragment.app.Fragment
|
||||
import mozilla.components.support.ktx.android.content.isPermissionGranted
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
class SitePermissionsManagePhoneFeature : Fragment() {
|
||||
|
||||
private lateinit var phoneFeature: PhoneFeature
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
phoneFeature = SitePermissionsManagePhoneFeatureArgs
|
||||
.fromBundle(requireArguments())
|
||||
.permission.toPhoneFeature()
|
||||
|
||||
(activity as AppCompatActivity).title = phoneFeature.label
|
||||
(activity as AppCompatActivity).supportActionBar?.show()
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
val rootView = inflater.inflate(R.layout.fragment_manage_site_permissions_feature_phone, container, false)
|
||||
|
||||
initAskToAllowRadio(rootView)
|
||||
|
||||
initBockedByAndroidContainer(rootView)
|
||||
|
||||
return rootView
|
||||
}
|
||||
|
||||
private fun initAskToAllowRadio(rootView: View) {
|
||||
val radio = rootView.findViewById<RadioButton>(R.id.ask_to_allow_switch)
|
||||
val askToAllowText = getString(R.string.preference_option_phone_feature_ask_to_allow)
|
||||
val recommendedText = getString(R.string.phone_feature_recommended)
|
||||
val recommendedTextSize = resources.getDimensionPixelSize(R.dimen.phone_feature_label_recommended_text_size)
|
||||
val recommendedSpannable = SpannableString(recommendedText)
|
||||
|
||||
recommendedSpannable.setSpan(
|
||||
ForegroundColorSpan(Color.GRAY),
|
||||
0,
|
||||
recommendedSpannable.length,
|
||||
SPAN_EXCLUSIVE_INCLUSIVE
|
||||
)
|
||||
|
||||
recommendedSpannable.setSpan(
|
||||
AbsoluteSizeSpan(recommendedTextSize), 0,
|
||||
recommendedSpannable.length,
|
||||
SPAN_EXCLUSIVE_INCLUSIVE
|
||||
)
|
||||
|
||||
radio.text = with(SpannableStringBuilder()) {
|
||||
append(askToAllowText)
|
||||
append("\n")
|
||||
append(recommendedSpannable)
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
private fun initBockedByAndroidContainer(rootView: View) {
|
||||
if (!phoneFeature.isAndroidPermissionGranted) {
|
||||
val containerView = rootView.findViewById<View>(R.id.permissions_blocked_container)
|
||||
containerView.visibility = VISIBLE
|
||||
|
||||
val descriptionLabel = rootView.findViewById<TextView>(R.id.blocked_by_android_explanation_label)
|
||||
val text = getString(R.string.phone_feature_blocked_by_android_explanation, phoneFeature.label)
|
||||
descriptionLabel.text = HtmlCompat.fromHtml(text, FROM_HTML_MODE_COMPACT)
|
||||
|
||||
initSettingsButton(rootView)
|
||||
}
|
||||
}
|
||||
|
||||
enum class PhoneFeature(val id: Int) {
|
||||
CAMERA(CAMERA_PERMISSION),
|
||||
LOCATION(LOCATION_PERMISSION),
|
||||
MICROPHONE(MICROPHONE_PERMISSION),
|
||||
NOTIFICATION(NOTIFICATION_PERMISSION)
|
||||
}
|
||||
|
||||
private val PhoneFeature.label: String
|
||||
get() {
|
||||
return when (this) {
|
||||
PhoneFeature.CAMERA -> getString(R.string.preference_phone_feature_camera)
|
||||
PhoneFeature.LOCATION -> getString(R.string.preference_phone_feature_location)
|
||||
PhoneFeature.MICROPHONE -> getString(R.string.preference_phone_feature_microphone)
|
||||
PhoneFeature.NOTIFICATION -> getString(R.string.preference_phone_feature_notification)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("SpreadOperator")
|
||||
private val PhoneFeature.isAndroidPermissionGranted: Boolean
|
||||
get() {
|
||||
val permissions = when (this) {
|
||||
PhoneFeature.CAMERA -> arrayOf(CAMERA)
|
||||
PhoneFeature.LOCATION -> arrayOf(ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION)
|
||||
PhoneFeature.MICROPHONE -> arrayOf(RECORD_AUDIO)
|
||||
PhoneFeature.NOTIFICATION -> {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return requireContext().isPermissionGranted(*permissions)
|
||||
}
|
||||
|
||||
private fun Int.toPhoneFeature(): PhoneFeature {
|
||||
return requireNotNull(PhoneFeature.values().find { feature ->
|
||||
this == feature.id
|
||||
}) {
|
||||
"$this is a invalid PhoneFeature"
|
||||
}
|
||||
}
|
||||
|
||||
private fun initSettingsButton(rootView: View) {
|
||||
val button = rootView.findViewById<Button>(R.id.settings_button)
|
||||
button.setOnClickListener {
|
||||
openSettings()
|
||||
}
|
||||
}
|
||||
|
||||
private fun openSettings() {
|
||||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||
val uri = Uri.fromParts("package", requireContext().packageName, null)
|
||||
intent.data = uri
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val CAMERA_PERMISSION = 0
|
||||
const val LOCATION_PERMISSION = 1
|
||||
const val MICROPHONE_PERMISSION = 2
|
||||
const val NOTIFICATION_PERMISSION = 3
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<corners android:radius="4dp"/>
|
||||
<solid android:color="@color/photonBlue80"/>
|
||||
</shape>
|
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginTop="@dimen/radio_button_preference_vertical">
|
||||
|
||||
<RadioGroup
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/ask_to_allow_switch"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/preference_option_phone_feature_ask_to_allow"
|
||||
android:textAppearance="?android:attr/textAppearanceListItem"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:button="@null"
|
||||
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
|
||||
android:drawablePadding="@dimen/radio_button_preference_drawable_padding"
|
||||
android:paddingTop="@dimen/radio_button_preference_vertical"
|
||||
android:paddingStart="@dimen/radio_button_preference_horizontal"
|
||||
android:paddingEnd="@dimen/radio_button_preference_horizontal"
|
||||
android:paddingBottom="@dimen/radio_button_preference_vertical"/>
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/block_switch"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/radio_button_preference_height"
|
||||
android:text="@string/preference_option_phone_feature_block"
|
||||
android:textAppearance="?android:attr/textAppearanceListItem"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:button="@null"
|
||||
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
|
||||
android:drawablePadding="@dimen/radio_button_preference_drawable_padding"
|
||||
android:paddingTop="@dimen/radio_button_preference_vertical"
|
||||
android:paddingStart="@dimen/radio_button_preference_horizontal"
|
||||
android:paddingEnd="@dimen/radio_button_preference_horizontal"
|
||||
android:paddingBottom="@dimen/radio_button_preference_vertical"/>
|
||||
</RadioGroup>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/permissions_blocked_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginTop="32dp"
|
||||
android:paddingStart="@dimen/radio_button_preference_horizontal"
|
||||
android:paddingEnd="@dimen/radio_button_preference_horizontal"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?android:attr/textAppearanceListItem"
|
||||
android:text="@string/phone_feature_blocked_by_android"
|
||||
android:layout_marginBottom="16dp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/blocked_by_android_explanation_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?android:attr/textAppearanceListItemSmall"
|
||||
android:text="@string/phone_feature_blocked_by_android_explanation"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/settings_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/SitePermissionButton"
|
||||
android:text="@string/phone_feature_go_to_settings"
|
||||
android:layout_marginTop="24dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
Loading…
Reference in New Issue