// This software is Copyright (c) 2015 Embarcadero Technologies, Inc. // You may only use this software if you are an authorized licensee // of an Embarcadero developer tools product. // This software is considered a Redistributable as defined under // the software license agreement that comes with the Embarcadero Products // and is subject to that software license agreement.
procedure TFlashLightForm.SetFlashlightState(Active: Boolean); begin if Active then Camera.TorchMode := TTorchMode.ModeOn else Camera.TorchMode := TTorchMode.ModeOff; end;
procedure TFlashLightForm.AccessCameraPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray); begin // 1 permission involved: CAMERA if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then ImageOff.Enabled := Camera.HasFlash else TDialogService.ShowMessage('Cannot access the camera flashlight because the required permission has not been granted'); end;
procedure TFlashLightForm.ActivateCameraPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray); begin // 1 permission involved: CAMERA if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then begin Camera.Active := True; ImageOff.Visible := False; ImageOn.Visible := True; SetFlashlightState(True); Light.Visible := True; end else TDialogService.ShowMessage('Cannot access the camera flashlight because the required permission has not been granted'); end;
// Optional rationale display routine to display permission requirement rationale to the user procedure TFlashLightForm.DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc); begin // Show an explanation to the user *asynchronously* - don't block this thread waiting for the user's response! // After the user sees the explanation, invoke the post-rationale routine to request the permissions TDialogService.ShowMessage('The app needs to access the camera in order to work', procedure(const AResult: TModalResult) begin APostRationaleProc; end) end;
0) Project -> Options... Permission (Release/Debug) check all that you needs 0.1) if necessary, you can ADD directly in Android.Manifest.Template.xml in your project folder, just use any text editor... if the does not exists, dont worry, just BUILD your project and it appears there
1) in code, same that the permissions does not needs user approval, you needs request it, using default way.
.... for var i:integer := 0 to high(APermissions) do ......... xxxxxxxx any logic here xxxxxxxx
if AllOK then ..... do your action here else .... ERROR ERROR
procedure DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc); // this procedure is not necessary at all, you can ignore it if does not needs show any msg to user.
2) you should run action ONLY all permissions was gived by user, else Dont.
3) you CAN NOT CHANGE any CAM property if a) you dont have any permission b) if CAM doing something - then, always stop firstly.
see my posts in WEDELPHI.COM http://wedelphi.com/t/422486/
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
@emailx45 Thank you. I have manually granted permissions to the app on my phone. The code does not report errors, but the flashlight cannot be turned on. What's going on? You must edit Android.Manifest.Template.xml or ask the user for these two methods. Choose one, are you indispensable without the other?
----------------------------------------------
-
you ALWAYS works in "AndroidManifest.TEMPLATE.xml" in project folder ( except if you use another folder to store it, of course)!!!
when building your project, others files will be created and "deployed" with your app.
you need to do manually changes if necessary, else the RAD do it when you choice the options on menus.
if does not works, maybe this properties is not really used... then, needs investigate if is necessary use JAVA code directly, in this case.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
@emailx45 I tried and it's no use modifying 'AndroidManifest. TEMPLATE. xml'. You must use PermissionsService.RequestPermissions to get the user to agree
----------------------------------------------
-
There are permissions that are strictly given by the system and do not require user authorization; These permissions must be defined in AndroidManifestTemplate.xml (normally they do not require any code in your APP internally)
Ex.: BOOT_COMPLETED - need be in AndroidManifestTemplate.xml only ---- Why not in app code? --> because when bootting smartphone there is not user-UI!!!
There are permissions that require strict authorization from the user; for these permissions, you will need to require permissions in AndroidManifestTemplate.xml and in the code;
Ex.: INTERNET - no need require on codigo, but needs be in AndroidManifestTemplate.xml file
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
0) check the permission in Project-Options... Permissions (CAMERA) ---- in "AndroidManifesTemplate.xml" it does not appears at all, because the IDE dont need!!!
---- this it will add in your "AndroidManifest.xml" when buiding your project -- see it after building your app in DEBUG or RELEASE folder!!!
1) in code, you need use: PermissionsService.RequestPermissions( { } [ LMyCAMPermission ], { } prcPermissionsResulted, { } prcDisplayRationale { = nil, if you DONT WANT show any message! } );
procedure TfrmFormMain.prcPermissionsResulted(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray); begin // verifying if the permissions was granted! - Here, testing only 1 permission = CAM if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
prcCAMStartCapture { execute your procedure here if all it's ok }
else TDialogService.ShowMessage('The permission <<CAMERA access>> not allowed by user'); end;
4 ) any changes (actions) in CAM needs permission, for that you can:
if PermissionsService.IsPermissionGranted(lMyCAMPermission) then .... CAM property changes ...
procedure TfrmFormMain.prcCAMStartCapture; begin if not(lMyCAMDevice = nil) then begin // to Mobile (Android), change properties from CAMERA, needs permission! {$IF DEFINED(ANDROID)} if PermissionsService.IsPermissionGranted(lMyCAMPermission) then {$ENDIF} begin try lMyCAMDevice.StopCapture; // to avoid any error below // lMyCAMDevice.Quality := TVideoCaptureQuality.PhotoQuality; // lMyCAMDevice.StartCapture; // starting video capture! // prcMyLog('CAM device = Capture stated!'); prcMyLog('CAM ' + fncMyIIF(lMyCAMDevice.IsDefault, 'is', 'is not') + ' Default'); prcMyLog('CAM ' + fncMyIIF(lMyCAMDevice.HasFlash, 'has', 'has not') + ' Flash');
except on E: Exception do prcMyLog('Error Start CAM' + #13#10 + E.Message); end; end {$IF DEFINED(ANDROID)} else TDialogService.ShowMessage('Then CAM device needs your permission to access it!'); {$ENDIF} end else TDialogService.ShowMessage('None CAM device defined!'); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
@emailx45 Thank you, I checked it out,Before Delphi 10.3, Android's authorization mode was one, which was determined during compilation. There was no need for users to dynamically authorize when the program was running. From 10.3 onwards, the authorization modes are divided into the following three types:
1 Normal Uses Permissions (no dynamic confirmation required)
2 Dangerous Uses Permissions (Dynamic confirmation required: confirmation when user program is running)
3 Signature Uses Permissions (No dynamic confirmation required)
The camera belongs to the second type.
----------------------------------------------
-