Add action to see permission status in the DebugActivity

This commit is contained in:
Benoit Marty 2021-07-12 21:33:15 +02:00 committed by Benoit Marty
parent cca9a8007c
commit fabbd6da9e
7 changed files with 185 additions and 25 deletions

View File

@ -4,6 +4,7 @@
<application>
<activity android:name=".features.debug.TestLinkifyActivity" />
<activity android:name=".features.debug.DebugPermissionActivity" />
<activity android:name=".features.debug.sas.DebugSasEmojiActivity" />
</application>

View File

@ -20,15 +20,20 @@ import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.Person
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.ScreenComponent
import im.vector.app.core.extensions.registerStartForActivityResult
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.core.utils.PERMISSIONS_ALL
import im.vector.app.core.utils.PERMISSIONS_FOR_TAKING_PHOTO
import im.vector.app.core.utils.checkPermissions
import im.vector.app.core.utils.registerForPermissionsResult
@ -113,6 +118,9 @@ class DebugMenuActivity : VectorBaseActivity<ActivityDebugMenuBinding>() {
}
views.debugTestCrash.setOnClickListener { testCrash() }
views.debugScanQrCode.setOnClickListener { scanQRCode() }
views.debugPermission.setOnClickListener {
startActivity(Intent(this, DebugPermissionActivity::class.java))
}
}
private fun renderQrCode(text: String) {

View File

@ -0,0 +1,91 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.debug
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import im.vector.app.R
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.core.utils.PERMISSIONS_ALL
import im.vector.app.core.utils.checkPermissions
import im.vector.app.core.utils.registerForPermissionsResult
import im.vector.app.databinding.ActivityDebugPermissionBinding
import timber.log.Timber
class DebugPermissionActivity : VectorBaseActivity<ActivityDebugPermissionBinding>() {
override fun getBinding() = ActivityDebugPermissionBinding.inflate(layoutInflater)
override fun initUiAndData() {
views.status.setOnClickListener { refresh() }
listOf(
views.audio,
views.camera,
views.write,
views.read,
views.contact
).forEach { button ->
button.setOnClickListener {
checkPermissions(listOf(button.text.toString()), this, launcher, R.string.debug_rationale)
}
}
}
private val launcher = registerForPermissionsResult { allGranted ->
if (allGranted) {
Toast.makeText(this, "All granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Denied", Toast.LENGTH_SHORT).show()
}
}
override fun onResume() {
super.onResume()
refresh()
}
private fun refresh() {
views.status.text = getStatus()
}
private fun getStatus(): String {
return buildString {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Timber.v("## debugPermission() : log the permissions status used by the app")
PERMISSIONS_ALL.forEach { permission ->
append("[$permission] : ")
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this@DebugPermissionActivity, permission)) {
append("PERMISSION_GRANTED")
} else {
append("PERMISSION_DENIED")
}
append(" show rational: ")
append(ActivityCompat.shouldShowRequestPermissionRationale(this@DebugPermissionActivity, permission))
append("\n")
}
} else {
append("Before M!")
}
append("\n")
append("(Click to refresh)")
}
}
}

View File

@ -152,6 +152,12 @@
android:layout_height="200dp"
tools:src="@drawable/ic_qr_code_add" />
<Button
android:id="@+id/debug_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Permission status" />
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".features.debug.DebugPermissionActivity"
tools:ignore="HardcodedText">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/linear_divider"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="@dimen/layout_horizontal_margin"
android:showDividers="middle">
<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Status" />
<Button
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manifest.permission.CAMERA"
android:textAllCaps="false" />
<Button
android:id="@+id/audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manifest.permission.RECORD_AUDIO"
android:textAllCaps="false" />
<Button
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manifest.permission.WRITE_EXTERNAL_STORAGE"
android:textAllCaps="false" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manifest.permission.READ_EXTERNAL_STORAGE"
android:textAllCaps="false" />
<Button
android:id="@+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manifest.permission.READ_CONTACTS"
android:textAllCaps="false" />
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="debug_rationale">Rationale!</string>
</resources>

View File

@ -18,7 +18,6 @@ package im.vector.app.core.utils
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
@ -45,30 +44,13 @@ val PERMISSIONS_FOR_PICKING_CONTACT = listOf(Manifest.permission.READ_CONTACTS)
val PERMISSIONS_EMPTY = emptyList<String>()
/**
* Log the used permissions statuses.
*/
fun logPermissionStatuses(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val permissions = listOf(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS)
Timber.v("## logPermissionStatuses() : log the permissions status used by the app")
for (permission in permissions) {
Timber.v(("Status of [$permission] : " +
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, permission)) {
"PERMISSION_GRANTED"
} else {
"PERMISSION_DENIED"
}))
}
}
}
// For debug
val PERMISSIONS_ALL = listOf(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS)
fun ComponentActivity.registerForPermissionsResult(allGranted: (Boolean) -> Unit): ActivityResultLauncher<Array<String>> {
return registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->