2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] Closes https://github.com/mozilla-mobile/fenix/issues/1078: Added UI for managing phone feature permissions.

This commit is contained in:
Arturo Mejia 2019-03-27 12:18:37 -04:00 committed by Jeff Boek
parent c2ee06d7d9
commit 3000707381
9 changed files with 379 additions and 46 deletions

View File

@ -6,11 +6,19 @@ package org.mozilla.fenix.settings
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.Navigation
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceClickListener
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import org.jetbrains.anko.support.v4.defaultSharedPreferences import org.jetbrains.anko.support.v4.defaultSharedPreferences
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.settings.SitePermissionsManagePhoneFeature.PhoneFeature
import org.mozilla.fenix.settings.SitePermissionsManagePhoneFeature.PhoneFeature.NOTIFICATION
import org.mozilla.fenix.settings.SitePermissionsManagePhoneFeature.PhoneFeature.LOCATION
import org.mozilla.fenix.settings.SitePermissionsManagePhoneFeature.PhoneFeature.CAMERA
import org.mozilla.fenix.settings.SitePermissionsManagePhoneFeature.PhoneFeature.MICROPHONE
@SuppressWarnings("TooManyFunctions")
class SitePermissionsFragment : PreferenceFragmentCompat() { class SitePermissionsFragment : PreferenceFragmentCompat() {
private lateinit var categoryPhoneFeatures: Preference private lateinit var categoryPhoneFeatures: Preference
@ -19,7 +27,6 @@ class SitePermissionsFragment : PreferenceFragmentCompat() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
(activity as AppCompatActivity).title = getString(R.string.preferences_site_permissions)
(activity as AppCompatActivity).supportActionBar?.show() (activity as AppCompatActivity).supportActionBar?.show()
} }
@ -53,7 +60,7 @@ class SitePermissionsFragment : PreferenceFragmentCompat() {
radioCustomSettings = requireNotNull(findPreference(keyCustomSettings)) radioCustomSettings = requireNotNull(findPreference(keyCustomSettings))
radioCustomSettings.onClickListener { radioCustomSettings.onClickListener {
toggleCategoryPhoneFeatureVisibility() categoryPhoneFeatures.isVisible = true
} }
} }
@ -62,7 +69,7 @@ class SitePermissionsFragment : PreferenceFragmentCompat() {
radioRecommendSettings = requireNotNull(findPreference(keyRecommendSettings)) radioRecommendSettings = requireNotNull(findPreference(keyRecommendSettings))
radioRecommendSettings.onClickListener { radioRecommendSettings.onClickListener {
toggleCategoryPhoneFeatureVisibility() categoryPhoneFeatures.isVisible = false
} }
} }
@ -75,9 +82,33 @@ class SitePermissionsFragment : PreferenceFragmentCompat() {
if (isCategoryActivate) { if (isCategoryActivate) {
categoryPhoneFeatures.isVisible = true categoryPhoneFeatures.isVisible = true
} }
initPhoneFeature(CAMERA)
initPhoneFeature(LOCATION)
initPhoneFeature(MICROPHONE)
initPhoneFeature(NOTIFICATION)
} }
private fun toggleCategoryPhoneFeatureVisibility() { private fun initPhoneFeature(phoneFeature: PhoneFeature) {
categoryPhoneFeatures.isVisible = !categoryPhoneFeatures.isVisible val keyPreference = getPreferenceKeyBy(phoneFeature)
val cameraPhoneFeatures: Preference = requireNotNull(findPreference(keyPreference))
cameraPhoneFeatures.onPreferenceClickListener = OnPreferenceClickListener {
navigateToPhoneFeature(phoneFeature)
true
}
}
private fun getPreferenceKeyBy(phoneFeature: PhoneFeature): String {
return when (phoneFeature) {
CAMERA -> getString(R.string.pref_key_phone_feature_camera)
LOCATION -> getString(R.string.pref_key_phone_feature_location)
MICROPHONE -> getString(R.string.pref_key_phone_feature_microphone)
NOTIFICATION -> getString(R.string.pref_key_phone_feature_notification)
}
}
private fun navigateToPhoneFeature(phoneFeature: PhoneFeature) {
val directions = SitePermissionsFragmentDirections.actionSitePermissionsToManagePhoneFeatures(phoneFeature.id)
Navigation.findNavController(view!!).navigate(directions)
} }
} }

View File

@ -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
}
}

View File

@ -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>

View File

@ -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>

View File

@ -46,6 +46,15 @@
<argument android:name="session_id" app:argType="string" app:nullable="true"/> <argument android:name="session_id" app:argType="string" app:nullable="true"/>
</fragment> </fragment>
<fragment
android:id="@+id/SitePermissionsManagePhoneFeature"
android:name="org.mozilla.fenix.settings.SitePermissionsManagePhoneFeature"
tools:layout="@layout/fragment_manage_site_permissions_feature_phone">
<argument
android:name="permission"
app:argType="integer"/>
</fragment>
<fragment <fragment
android:id="@+id/browserFragment" android:id="@+id/browserFragment"
android:name="org.mozilla.fenix.browser.BrowserFragment" android:name="org.mozilla.fenix.browser.BrowserFragment"
@ -112,9 +121,17 @@
</fragment> </fragment>
<fragment android:id="@+id/dataChoicesFragment" android:name="org.mozilla.fenix.settings.DataChoicesFragment" <fragment android:id="@+id/dataChoicesFragment" android:name="org.mozilla.fenix.settings.DataChoicesFragment"
android:label="DataChoicesFragment"/> android:label="DataChoicesFragment"/>
<fragment android:id="@+id/sitePermissionsFragment" <fragment
android:id="@+id/sitePermissionsFragment"
android:name="org.mozilla.fenix.settings.SitePermissionsFragment" android:name="org.mozilla.fenix.settings.SitePermissionsFragment"
android:label="SitePermissionsFragment"/> android:label="@string/preferences_site_permissions">
<action
android:id="@+id/action_site_permissions_to_manage_phone_features"
app:destination="@id/SitePermissionsManagePhoneFeature"
app:popUpTo="@id/sitePermissionsFragment"/>
</fragment>
<fragment android:id="@+id/accessibilityFragment" android:name="org.mozilla.fenix.settings.AccessibilityFragment" <fragment android:id="@+id/accessibilityFragment" android:name="org.mozilla.fenix.settings.AccessibilityFragment"
android:label="AccessibilityFragment"/> android:label="AccessibilityFragment"/>
<fragment android:id="@+id/accountSettingsFragment" android:name="org.mozilla.fenix.settings.AccountSettingsFragment" <fragment android:id="@+id/accountSettingsFragment" android:name="org.mozilla.fenix.settings.AccountSettingsFragment"

View File

@ -26,4 +26,5 @@
<dimen name="radio_button_preference_horizontal">16dp</dimen> <dimen name="radio_button_preference_horizontal">16dp</dimen>
<dimen name="radio_button_preference_drawable_padding">16dp</dimen> <dimen name="radio_button_preference_drawable_padding">16dp</dimen>
<dimen name="radio_button_preference_vertical">12dp</dimen> <dimen name="radio_button_preference_vertical">12dp</dimen>
<dimen name="phone_feature_label_recommended_text_size">14sp</dimen>
</resources> </resources>

View File

@ -170,8 +170,18 @@
<string name="preference_phone_feature_location">Location</string> <string name="preference_phone_feature_location">Location</string>
<!-- Preference for altering the notification access for all websites --> <!-- Preference for altering the notification access for all websites -->
<string name="preference_phone_feature_notification">Notification</string> <string name="preference_phone_feature_notification">Notification</string>
<!-- Preference option for altering the notification access for all websites --> <!-- Label that indicates that a permission must be asked always -->
<string name="preference_option_phone_feature_ask_to_allow">Ask to allow</string> <string name="preference_option_phone_feature_ask_to_allow">Ask to allow</string>
<!-- Label that indicates that a permission must be blocked -->
<string name="preference_option_phone_feature_block">Block</string>
<!--Label that indicates a permission is by the Android OS-->
<string name="phone_feature_blocked_by_android">Blocked by Android</string>
<!-- Alternative explanation label that is shown when a permissions like (camera,location and microphone) is required, this indicate to the user how to enable the permission via Android settings %1$s indicate the name of the permission (camera,location and microphone) -->
<string name="phone_feature_blocked_by_android_explanation">To allow it: &lt;br/&gt;&lt;br/&gt; 1. Go to Android Settings &lt;br/&gt;&lt;br/&gt;2. Tap &lt;b&gt;Permissions&lt;/b&gt; &lt;br/&gt;&lt;br/&gt; 3. Toggle &lt;b&gt;%1$s&lt;/b&gt; to ON</string>
<!--Label that indicates that this option it the recommended one-->
<string name="phone_feature_recommended">Recommended</string>
<!--Button label that take the user to the Android App setting -->
<string name="phone_feature_go_to_settings">Go to Settings</string>
<!-- Account Preferences --> <!-- Account Preferences -->
<!-- Preference for triggering sync --> <!-- Preference for triggering sync -->

View File

@ -149,6 +149,22 @@
<item name="android:background">@drawable/search_pill_background</item> <item name="android:background">@drawable/search_pill_background</item>
</style> </style>
<style name="SitePermissionButton" parent="Widget.AppCompat.Button.Borderless">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">@color/photonWhite</item>
<item name="android:gravity">center_vertical</item>
<item name="android:singleLine">true</item>
<item name="android:padding">8dp</item>
<item name="android:paddingStart">24dp</item>
<item name="android:paddingEnd">24dp</item>
<item name="android:foreground">?android:attr/selectableItemBackground</item>
<item name="android:background">@drawable/site_permissions_button_background</item>
</style>
<style name="CurrentSessionBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog"> <style name="CurrentSessionBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/CurrentSessionBottomSheetStyle</item> <item name="bottomSheetStyle">@style/CurrentSessionBottomSheetStyle</item>
<item name="android:textColor">@color/light_mode_text_color</item> <item name="android:textColor">@color/light_mode_text_color</item>

View File

@ -2,7 +2,8 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public <!-- 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 - 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/. --> - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <androidx.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.SwitchPreference <androidx.preference.SwitchPreference
@ -54,8 +55,8 @@
<androidx.preference.Preference <androidx.preference.Preference
android:icon="@drawable/ic_notification" android:icon="@drawable/ic_notification"
android:key="@string/pref_key_phone_feature_microphone" android:key="@string/pref_key_phone_feature_notification"
android:title="@string/preference_phone_feature_microphone" android:title="@string/preference_phone_feature_notification"
android:summary="@string/preference_option_phone_feature_ask_to_allow"/> android:summary="@string/preference_option_phone_feature_ask_to_allow"/>
</androidx.preference.PreferenceCategory> </androidx.preference.PreferenceCategory>