Added permissions fragment

This commit is contained in:
Sylvain Berfini 2023-10-03 11:53:03 +02:00
parent ea87c48586
commit adad98f3e2
6 changed files with 516 additions and 52 deletions

View file

@ -0,0 +1,176 @@
/*
* Copyright (c) 2010-2023 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.ui.assistant.fragment
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.UiThread
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import org.linphone.R
import org.linphone.core.tools.Log
import org.linphone.databinding.AssistantPermissionsFragmentBinding
import org.linphone.ui.assistant.viewmodel.PermissionsViewModel
@UiThread
class PermissionsFragment : Fragment() {
companion object {
private const val TAG = "[Permissions Fragment]"
}
private lateinit var binding: AssistantPermissionsFragmentBinding
private val viewModel: PermissionsViewModel by navGraphViewModels(
R.id.assistant_nav_graph
)
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
permissions.entries.forEach() {
val permissionName = it.key
val isGranted = it.value
when (permissionName) {
"READ_CONTACTS" -> {
viewModel.readContacts.value = isGranted
}
"POST_NOTIFICATIONS" -> {
viewModel.postNotifications.value = isGranted
}
"RECORD_AUDIO" -> {
viewModel.recordAudio.value = isGranted
}
"CAMERA" -> {
viewModel.accessCamera.value = isGranted
}
else -> {}
}
if (isGranted) {
Log.i("Permission [$permissionName] is now granted")
} else {
Log.i("Permission [$permissionName] has been denied")
}
}
checkIfAllPermissionsHaveBeenGranted()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = AssistantPermissionsFragmentBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
binding.viewModel = viewModel
binding.setBackClickListener {
findNavController().popBackStack()
}
binding.setGrantReadContactsClickListener {
Log.i("$TAG Requesting READ_CONTACTS permission")
requestPermissionLauncher.launch(arrayOf(Manifest.permission.READ_CONTACTS))
}
// TODO FIXME: use compat for older Androids
binding.setGrantPostNotificationsClickListener {
Log.i("$TAG Requesting POST_NOTIFICATIONS permission")
requestPermissionLauncher.launch(arrayOf(Manifest.permission.POST_NOTIFICATIONS))
}
binding.setGrantRecordAudioClickListener {
Log.i("$TAG Requesting RECORD_AUDIO permission")
requestPermissionLauncher.launch(arrayOf(Manifest.permission.RECORD_AUDIO))
}
binding.setGrantAccessCameraClickListener {
Log.i("$TAG Requesting CAMERA permission")
requestPermissionLauncher.launch(arrayOf(Manifest.permission.CAMERA))
}
binding.setSkipClickListener {
Log.i("$TAG User clicked skip...")
goToLoginFragment()
}
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.MANAGE_OWN_CALLS
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissionLauncher.launch(arrayOf(Manifest.permission.MANAGE_OWN_CALLS))
}
}
override fun onResume() {
super.onResume()
viewModel.readContacts.value = ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.READ_CONTACTS
) == PackageManager.PERMISSION_GRANTED
// TODO FIXME: use compat for older Androids
viewModel.postNotifications.value = ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
viewModel.recordAudio.value = ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.RECORD_AUDIO
) == PackageManager.PERMISSION_GRANTED
viewModel.accessCamera.value = ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
checkIfAllPermissionsHaveBeenGranted()
}
private fun goToLoginFragment() {
val action = PermissionsFragmentDirections.actionPermissionsFragmentToLoginFragment()
findNavController().navigate(action)
}
private fun checkIfAllPermissionsHaveBeenGranted() {
if (viewModel.readContacts.value == true && viewModel.postNotifications.value == true && viewModel.recordAudio.value == true && viewModel.accessCamera.value == true) {
Log.i("$TAG All permissions are granted, continuing to login fragment")
goToLoginFragment()
}
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2010-2023 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.ui.assistant.viewmodel
import androidx.annotation.UiThread
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class PermissionsViewModel @UiThread constructor() : ViewModel() {
val readContacts = MutableLiveData<Boolean>()
val postNotifications = MutableLiveData<Boolean>()
val recordAudio = MutableLiveData<Boolean>()
val accessCamera = MutableLiveData<Boolean>()
}

View file

@ -43,13 +43,6 @@ import org.linphone.utils.slideInToastFromTopForDuration
@UiThread
class MainActivity : AppCompatActivity() {
companion object {
private const val CONTACTS_PERMISSION_REQUEST = 0
private const val CAMERA_PERMISSION_REQUEST = 1
private const val RECORD_AUDIO_PERMISSION_REQUEST = 2
private const val POST_NOTIFICATIONS_PERMISSION_REQUEST = 3
private const val MANAGE_OWN_CALLS_PERMISSION_REQUEST = 4
}
private lateinit var binding: MainActivityBinding
@ -101,38 +94,6 @@ class MainActivity : AppCompatActivity() {
// TODO FIXME: uncomment
// startActivity(Intent(this, WelcomeActivity::class.java))
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.READ_CONTACTS),
CONTACTS_PERMISSION_REQUEST
)
}
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.CAMERA),
CAMERA_PERMISSION_REQUEST
)
}
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.RECORD_AUDIO),
RECORD_AUDIO_PERMISSION_REQUEST
)
}
if (checkSelfPermission(Manifest.permission.MANAGE_OWN_CALLS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.MANAGE_OWN_CALLS),
MANAGE_OWN_CALLS_PERMISSION_REQUEST
)
}
// TODO FIXME : use compatibility
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
POST_NOTIFICATIONS_PERMISSION_REQUEST
)
}
coreContext.greenToastToShowEvent.observe(this) {
it.consume { pair ->
val message = pair.first
@ -142,18 +103,6 @@ class MainActivity : AppCompatActivity() {
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == CONTACTS_PERMISSION_REQUEST && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadContacts()
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
@SuppressLint("RtlHardcoded")
fun toggleDrawerMenu() {
if (binding.drawerMenu.isDrawerOpen(Gravity.LEFT)) {

View file

@ -0,0 +1,279 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<import type="android.text.InputType" />
<variable
name="backClickListener"
type="View.OnClickListener" />
<variable
name="grantReadContactsClickListener"
type="View.OnClickListener" />
<variable
name="grantPostNotificationsClickListener"
type="View.OnClickListener" />
<variable
name="grantRecordAudioClickListener"
type="View.OnClickListener" />
<variable
name="grantAccessCameraClickListener"
type="View.OnClickListener" />
<variable
name="skipClickListener"
type="View.OnClickListener" />
<variable
name="viewModel"
type="org.linphone.ui.assistant.viewmodel.PermissionsViewModel" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@color/white">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:onClick="@{backClickListener}"
android:id="@+id/back"
android:layout_width="@dimen/top_bar_height"
android:layout_height="@dimen/top_bar_height"
android:padding="15dp"
android:src="@drawable/caret_left"
app:tint="@color/gray_main2_500"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="19dp"
android:src="@drawable/header"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/back"
app:layout_constraintBottom_toBottomOf="@id/title"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/assistant_page_title_style"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:paddingBottom="27dp"
android:text="@string/assistant_permissions_title"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:id="@+id/subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/assistant_permissions_subtitle"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
<com.google.android.material.materialswitch.MaterialSwitch
style="@style/material_switch_style"
android:id="@+id/read_contacts_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:checked="@{viewModel.readContacts}"
android:enabled="@{!viewModel.readContacts}"
android:onClick="@{grantReadContactsClickListener}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/subtitle" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_title_style"
android:id="@+id/read_contacts_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_read_contacts_title"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintTop_toTopOf="@id/read_contacts_switch"
app:layout_constraintBottom_toTopOf="@id/read_contacts_subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/read_contacts_switch"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_subtitle_style"
android:id="@+id/read_contacts_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_read_contacts_message"
app:layout_constraintTop_toBottomOf="@id/read_contacts_title"
app:layout_constraintBottom_toBottomOf="@id/read_contacts_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/read_contacts_switch"/>
<com.google.android.material.materialswitch.MaterialSwitch
style="@style/material_switch_style"
android:id="@+id/post_notifications_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
android:checked="@{viewModel.postNotifications}"
android:enabled="@{!viewModel.postNotifications}"
android:onClick="@{grantPostNotificationsClickListener}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/read_contacts_switch" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_title_style"
android:id="@+id/post_notifications_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_post_notifications_title"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintTop_toTopOf="@id/post_notifications_switch"
app:layout_constraintBottom_toTopOf="@id/post_notifications_subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/post_notifications_switch"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_subtitle_style"
android:id="@+id/post_notifications_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_post_notifications_message"
app:layout_constraintTop_toBottomOf="@id/post_notifications_title"
app:layout_constraintBottom_toBottomOf="@id/post_notifications_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/post_notifications_switch"/>
<com.google.android.material.materialswitch.MaterialSwitch
style="@style/material_switch_style"
android:id="@+id/record_audio_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
android:checked="@{viewModel.recordAudio}"
android:enabled="@{!viewModel.recordAudio}"
android:onClick="@{grantRecordAudioClickListener}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/post_notifications_switch" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_title_style"
android:id="@+id/record_audio_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_record_audio_title"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintTop_toTopOf="@id/record_audio_switch"
app:layout_constraintBottom_toTopOf="@id/record_audio_subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/record_audio_switch"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_subtitle_style"
android:id="@+id/record_audio_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_record_audio_message"
app:layout_constraintTop_toBottomOf="@id/record_audio_title"
app:layout_constraintBottom_toBottomOf="@id/record_audio_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/record_audio_switch"/>
<com.google.android.material.materialswitch.MaterialSwitch
style="@style/material_switch_style"
android:id="@+id/access_camera_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
android:checked="@{viewModel.accessCamera}"
android:enabled="@{!viewModel.accessCamera}"
android:onClick="@{grantAccessCameraClickListener}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/record_audio_switch" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_title_style"
android:id="@+id/access_camera_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_access_camera_title"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintTop_toTopOf="@id/access_camera_switch"
app:layout_constraintBottom_toTopOf="@id/access_camera_subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/access_camera_switch"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_subtitle_style"
android:id="@+id/access_camera_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/assistant_permissions_access_camera_message"
app:layout_constraintTop_toBottomOf="@id/access_camera_title"
app:layout_constraintBottom_toBottomOf="@id/access_camera_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/access_camera_switch"/>
<androidx.appcompat.widget.AppCompatTextView
android:onClick="@{skipClickListener}"
style="@style/default_text_style_600"
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/welcome_carousel_skip"
android:textSize="13sp"
android:textColor="@color/gray_main2_500"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/access_camera_switch"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>

View file

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/assistant_nav_graph"
app:startDestination="@id/loginFragment">
app:startDestination="@id/permissionsFragment">
<fragment
android:id="@+id/loginFragment"
@ -132,4 +132,19 @@
android:label="ProfileModeFragment"
tools:layout="@layout/assistant_secure_mode_fragment" />
<fragment
android:id="@+id/permissionsFragment"
android:name="org.linphone.ui.assistant.fragment.PermissionsFragment"
android:label="PermissionsFragment"
tools:layout="@layout/assistant_permissions_fragment">
<action
android:id="@+id/action_permissionsFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:launchSingleTop="true" />
</fragment>
</navigation>

View file

@ -151,6 +151,17 @@
<string name="assistant_account_register_unexpected_error">Unexpected error occurred, please try again later</string>
<string name="assistant_account_login_forbidden_error">Wrong username or password</string>
<string name="assistant_account_login_error">Failed to login: error code is %i</string>
<string name="assistant_permissions_title">Grant permissions</string>
<string name="assistant_permissions_subtitle">To be able to work properly, we need you to grand the application the following permissions.\n\nIf you don\'t, some features won\'t be available, but you can grant them later.</string>
<string name="assistant_permissions_read_contacts_title">Read contacts</string>
<string name="assistant_permissions_read_contacts_message">This allows us to import your address-book in &appName;</string>
<string name="assistant_permissions_post_notifications_title">Post notifications</string>
<string name="assistant_permissions_post_notifications_message">To notify incoming calls &amp; chat messages</string>
<string name="assistant_permissions_record_audio_title">Record audio</string>
<string name="assistant_permissions_record_audio_message">For audio calls, duh!</string>
<string name="assistant_permissions_access_camera_title">Access camera</string>
<string name="assistant_permissions_access_camera_message">For video calls &amp; scan QR codes</string>
<string name="drawer_menu_manage_account">Manage the profile</string>
<string name="drawer_menu_account_connection_status_connected">Connected</string>