Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 5ebe79b

Browse files
Add checkPermission for camera before requestPermission #752
1 parent 3796752 commit 5ebe79b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import * as utils from "tns-core-modules/utils/utils";
33
import { MLKitCameraView as MLKitCameraViewBase } from "./mlkit-cameraview-common";
44

55
declare const android, global: any;
6+
67
const ActivityCompatClass = useAndroidX() ? global.androidx.core.app.ActivityCompat : android.support.v4.app.ActivityCompat;
8+
const ContentPackageName = useAndroidX() ? global.androidx.core.content : android.support.v4.content;
9+
710
const CAMERA_PERMISSION_REQUEST_CODE = 502;
811

912
interface SizeWH {
@@ -61,8 +64,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
6164
let nativeView = super.createNativeView();
6265

6366
if (this.hasCamera()) {
64-
// no permission required for older Android versions
65-
if (android.os.Build.VERSION.SDK_INT < 23) {
67+
if (this.wasCameraPermissionGranted()) {
6668
this.initView(nativeView);
6769
} else {
6870
const permissionCb = (args: application.AndroidActivityRequestPermissionsEventData) => {
@@ -107,6 +109,15 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
107109
.hasSystemFeature("android.hardware.camera");
108110
}
109111

112+
private wasCameraPermissionGranted() {
113+
let hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0)
114+
if (!hasPermission) {
115+
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ===
116+
ContentPackageName.ContextCompat.checkSelfPermission(utils.ad.getApplicationContext(), android.Manifest.permission.CAMERA);
117+
}
118+
return hasPermission;
119+
}
120+
110121
private initView(nativeView): void {
111122
this.surfaceView = new android.view.SurfaceView(utils.ad.getApplicationContext());
112123
nativeView.addView(this.surfaceView);

0 commit comments

Comments
 (0)