Welcome back.
We finished everything.
Now let's call the camera screen in our main activity.
So here main activity file.
Remove the greeting composables.
And here remove the scaffold and the gadget app screen okay.
Inside the set content let's start building our application.
Start with surface.
Set the modifier fill maximum size material theme, color scheme, dot background and var has camera
permission.
We need to check for camera permissions by.
Remember.
Let me add this in the manifest.
So here inside the manifest we need to add the permissions uses permission internet.
You can access the internet the camera the external storage.
You can read the read write and so on okay we need to add the That uses features so Alt plus enter to
solve this add uses feature.
You see guys that whenever you need to use a hardware from the phone, we need to add uses feature and
set the hardware in this type.
In this application we need to use the camera.
You can use low latency Bluetooth camera or fingerprint, geolocation, microphone, NFS, NFC and others.
We need to use the camera and set it like this.
Okay, you can also remove the internet permission okay, so we need just the camera.
And the camera uses features okay.
So those are the camera permissions that we need to add in the manifest back to the main activity here.
Let me continue with has permission Alt plus enter to import the Remember and Alt Plus enter to add
by.
Okay, start with mutable state of context compat dot check self permission.
We need to check if there is a permission for the camera manifest, dot permission dot camera and if
this equals to the package manager dot permission granted.
Here we are checking if we have a camera permission.
Import the operator mutable state, dot set and alt plus.
Enter again just to import the operator state dot get value.
And here we go.
So this variable is used to check if there is granted.
If the permissions of the camera are granted or not then val launcher equals to remember launcher for
activity result and set the contract as activity contracts result, contracts dot request permission
and if the result is granted, go and set the has permission to is granted.
I want from you to focus with me.
We used this new method.
This is the first time we used in our application.
Remember launcher for activity result.
This creates a launcher that survives Recompositions.
Why needed permission requests are asynchronous and need stable reference without remember would recreate
launcher on recomposition losing the permission request callback.
The first parameter is the contract.
The first parameter.
The contract defines what type of activity result we are requesting on the request permission function,
specifically for requesting a single permission.
So other common contracts.
We can use your request multiple permissions for multiple permissions.
If we have like a camera read and write external storage and so on.
But since we are using a single permission, we used request permission.
Also, we can set the on contract to request multiple permissions, create document, get content capture
video, open document, pick contact, pick picture for taking photos, for contact selection, pick
contact and so on.
Okay, so there are a lot of contents we can use on a result executed when the user responds to permission.
Dialog granted Boolean indicating if permission was granted and update state has camera permission set
it to granted is granted.
Updates the UI state since this the the has a camera permission is a variable that we created before
and being updated here in the on results callback.
Then after this we need to use a launcher effect.
As I told you before, launch effect set it to true key number one and if not has camera permission.
What we need to do, we need to request from the user a launcher for manifest camera.
Here we need to check if the user if there's not a.
Granted that camera permissions go and set and launch the camera manifest request from the user.
If the if has camera permission, we need to load the camera screen that we created before okay.
This is our application.
This is how we handle the camera permissions for our application.