|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.camerax; |
| 6 | + |
| 7 | +import android.Manifest; |
| 8 | +import android.Manifest.permission; |
| 9 | +import android.app.Activity; |
| 10 | +import android.content.pm.PackageManager; |
| 11 | +import androidx.annotation.VisibleForTesting; |
| 12 | +import androidx.core.app.ActivityCompat; |
| 13 | +import androidx.core.content.ContextCompat; |
| 14 | + |
| 15 | +final class CameraPermissionsManager { |
| 16 | + interface PermissionsRegistry { |
| 17 | + @SuppressWarnings("deprecation") |
| 18 | + void addListener( |
| 19 | + io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener handler); |
| 20 | + } |
| 21 | + |
| 22 | + interface ResultCallback { |
| 23 | + void onResult(String errorCode, String errorDescription); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Camera access permission errors handled when camera is created. See {@code MethodChannelCamera} |
| 28 | + * in {@code camera/camera_platform_interface} for details. |
| 29 | + */ |
| 30 | + private static final String CAMERA_PERMISSIONS_REQUEST_ONGOING = |
| 31 | + "CameraPermissionsRequestOngoing"; |
| 32 | + |
| 33 | + private static final String CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE = |
| 34 | + "Another request is ongoing and multiple requests cannot be handled at once."; |
| 35 | + private static final String CAMERA_ACCESS_DENIED = "CameraAccessDenied"; |
| 36 | + private static final String CAMERA_ACCESS_DENIED_MESSAGE = "Camera access permission was denied."; |
| 37 | + private static final String AUDIO_ACCESS_DENIED = "AudioAccessDenied"; |
| 38 | + private static final String AUDIO_ACCESS_DENIED_MESSAGE = "Audio access permission was denied."; |
| 39 | + |
| 40 | + private static final int CAMERA_REQUEST_ID = 9796; |
| 41 | + @VisibleForTesting boolean ongoing = false; |
| 42 | + |
| 43 | + void requestPermissions( |
| 44 | + Activity activity, |
| 45 | + PermissionsRegistry permissionsRegistry, |
| 46 | + boolean enableAudio, |
| 47 | + ResultCallback callback) { |
| 48 | + if (ongoing) { |
| 49 | + callback.onResult( |
| 50 | + CAMERA_PERMISSIONS_REQUEST_ONGOING, CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE); |
| 51 | + return; |
| 52 | + } |
| 53 | + if (!hasCameraPermission(activity) || (enableAudio && !hasAudioPermission(activity))) { |
| 54 | + permissionsRegistry.addListener( |
| 55 | + new CameraRequestPermissionsListener( |
| 56 | + (String errorCode, String errorDescription) -> { |
| 57 | + ongoing = false; |
| 58 | + callback.onResult(errorCode, errorDescription); |
| 59 | + })); |
| 60 | + ongoing = true; |
| 61 | + ActivityCompat.requestPermissions( |
| 62 | + activity, |
| 63 | + enableAudio |
| 64 | + ? new String[] {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO} |
| 65 | + : new String[] {Manifest.permission.CAMERA}, |
| 66 | + CAMERA_REQUEST_ID); |
| 67 | + } else { |
| 68 | + // Permissions already exist. Call the callback with success. |
| 69 | + callback.onResult(null, null); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private boolean hasCameraPermission(Activity activity) { |
| 74 | + return ContextCompat.checkSelfPermission(activity, permission.CAMERA) |
| 75 | + == PackageManager.PERMISSION_GRANTED; |
| 76 | + } |
| 77 | + |
| 78 | + private boolean hasAudioPermission(Activity activity) { |
| 79 | + return ContextCompat.checkSelfPermission(activity, permission.RECORD_AUDIO) |
| 80 | + == PackageManager.PERMISSION_GRANTED; |
| 81 | + } |
| 82 | + |
| 83 | + @VisibleForTesting |
| 84 | + @SuppressWarnings("deprecation") |
| 85 | + static final class CameraRequestPermissionsListener |
| 86 | + implements io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener { |
| 87 | + |
| 88 | + // There's no way to unregister permission listeners in the v1 embedding, so we'll be called |
| 89 | + // duplicate times in cases where the user denies and then grants a permission. Keep track of if |
| 90 | + // we've responded before and bail out of handling the callback manually if this is a repeat |
| 91 | + // call. |
| 92 | + boolean alreadyCalled = false; |
| 93 | + |
| 94 | + final ResultCallback callback; |
| 95 | + |
| 96 | + @VisibleForTesting |
| 97 | + CameraRequestPermissionsListener(ResultCallback callback) { |
| 98 | + this.callback = callback; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean onRequestPermissionsResult(int id, String[] permissions, int[] grantResults) { |
| 103 | + if (alreadyCalled || id != CAMERA_REQUEST_ID) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + alreadyCalled = true; |
| 108 | + // grantResults could be empty if the permissions request with the user is interrupted |
| 109 | + // https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[]) |
| 110 | + if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) { |
| 111 | + callback.onResult(CAMERA_ACCESS_DENIED, CAMERA_ACCESS_DENIED_MESSAGE); |
| 112 | + } else if (grantResults.length > 1 && grantResults[1] != PackageManager.PERMISSION_GRANTED) { |
| 113 | + callback.onResult(AUDIO_ACCESS_DENIED, AUDIO_ACCESS_DENIED_MESSAGE); |
| 114 | + } else { |
| 115 | + callback.onResult(null, null); |
| 116 | + } |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments