request bluetooth connect permission

fixes #4338
This commit is contained in:
Daniel Gultsch 2022-08-08 21:08:28 +02:00
parent 52ff6f446c
commit 5aeed63844
3 changed files with 1302 additions and 732 deletions

View file

@ -272,14 +272,16 @@ public class RtpSessionActivity extends XmppActivity
} }
private void requestPermissionsAndAcceptCall() { private void requestPermissionsAndAcceptCall() {
final List<String> permissions; final ImmutableList.Builder<String> permissions = ImmutableList.builder();
if (getMedia().contains(Media.VIDEO)) { if (getMedia().contains(Media.VIDEO)) {
permissions = permissions.add(Manifest.permission.CAMERA).add(Manifest.permission.RECORD_AUDIO);
ImmutableList.of(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO);
} else { } else {
permissions = ImmutableList.of(Manifest.permission.RECORD_AUDIO); permissions.add(Manifest.permission.RECORD_AUDIO);
} }
if (PermissionUtils.hasPermission(this, permissions, REQUEST_ACCEPT_CALL)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
}
if (PermissionUtils.hasPermission(this, permissions.build(), REQUEST_ACCEPT_CALL)) {
putScreenInCallMode(); putScreenInCallMode();
checkRecorderAndAcceptCall(); checkRecorderAndAcceptCall();
} }
@ -491,13 +493,16 @@ public class RtpSessionActivity extends XmppActivity
public void onRequestPermissionsResult( public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (PermissionUtils.allGranted(grantResults)) { final PermissionUtils.PermissionResult permissionResult =
PermissionUtils.removeBluetoothConnect(permissions, grantResults);
if (PermissionUtils.allGranted(permissionResult.grantResults)) {
if (requestCode == REQUEST_ACCEPT_CALL) { if (requestCode == REQUEST_ACCEPT_CALL) {
checkRecorderAndAcceptCall(); checkRecorderAndAcceptCall();
} }
} else { } else {
@StringRes int res; @StringRes int res;
final String firstDenied = getFirstDenied(grantResults, permissions); final String firstDenied =
getFirstDenied(permissionResult.grantResults, permissionResult.permissions);
if (Manifest.permission.RECORD_AUDIO.equals(firstDenied)) { if (Manifest.permission.RECORD_AUDIO.equals(firstDenied)) {
res = R.string.no_microphone_permission; res = R.string.no_microphone_permission;
} else if (Manifest.permission.CAMERA.equals(firstDenied)) { } else if (Manifest.permission.CAMERA.equals(firstDenied)) {

View file

@ -8,7 +8,9 @@ import android.os.Build;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PermissionUtils { public class PermissionUtils {
@ -40,11 +42,41 @@ public class PermissionUtils {
return null; return null;
} }
public static boolean hasPermission(final Activity activity, final List<String> permissions, final int requestCode) { public static class PermissionResult {
public final String[] permissions;
public final int[] grantResults;
public PermissionResult(String[] permissions, int[] grantResults) {
this.permissions = permissions;
this.grantResults = grantResults;
}
}
public static PermissionResult removeBluetoothConnect(
final String[] inPermissions, final int[] inGrantResults) {
final List<String> outPermissions = new ArrayList<>();
final List<Integer> outGrantResults = new ArrayList<>();
for (int i = 0; i < Math.min(inPermissions.length, inGrantResults.length); ++i) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (inPermissions[i].equals(Manifest.permission.BLUETOOTH_CONNECT)) {
continue;
}
}
outPermissions.add(inPermissions[i]);
outGrantResults.add(inGrantResults[i]);
}
return new PermissionResult(
outPermissions.toArray(new String[0]), Ints.toArray(outGrantResults));
}
public static boolean hasPermission(
final Activity activity, final List<String> permissions, final int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final ImmutableList.Builder<String> missingPermissions = new ImmutableList.Builder<>(); final ImmutableList.Builder<String> missingPermissions = new ImmutableList.Builder<>();
for (final String permission : permissions) { for (final String permission : permissions) {
if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
missingPermissions.add(permission); missingPermissions.add(permission);
} }
} }
@ -52,7 +84,8 @@ public class PermissionUtils {
if (missing.size() == 0) { if (missing.size() == 0) {
return true; return true;
} }
ActivityCompat.requestPermissions(activity, missing.toArray(new String[0]), requestCode); ActivityCompat.requestPermissions(
activity, missing.toArray(new String[0]), requestCode);
return false; return false;
} else { } else {
return true; return true;