@@ -3,7 +3,10 @@ import * as utils from "tns-core-modules/utils/utils";
33import { MLKitCameraView as MLKitCameraViewBase } from "./mlkit-cameraview-common" ;
44
55declare const android , global : any ;
6+
67const 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+
710const CAMERA_PERMISSION_REQUEST_CODE = 502 ;
811
912interface 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