WEBVTT

00:00.080 --> 00:00.960
Welcome back.

00:01.000 --> 00:02.440
We finished everything.

00:02.440 --> 00:05.760
Now let's call the camera screen in our main activity.

00:05.760 --> 00:08.240
So here main activity file.

00:08.280 --> 00:11.280
Remove the greeting composables.

00:11.280 --> 00:16.680
And here remove the scaffold and the gadget app screen okay.

00:16.920 --> 00:21.440
Inside the set content let's start building our application.

00:21.440 --> 00:23.560
Start with surface.

00:23.760 --> 00:33.400
Set the modifier fill maximum size material theme, color scheme, dot background and var has camera

00:33.400 --> 00:34.320
permission.

00:34.320 --> 00:37.080
We need to check for camera permissions by.

00:37.120 --> 00:37.960
Remember.

00:38.000 --> 00:41.280
Let me add this in the manifest.

00:41.280 --> 00:48.480
So here inside the manifest we need to add the permissions uses permission internet.

00:48.520 --> 00:53.240
You can access the internet the camera the external storage.

00:53.400 --> 01:03.640
You can read the read write and so on okay we need to add the That uses features so Alt plus enter to

01:03.680 --> 01:06.200
solve this add uses feature.

01:06.240 --> 01:14.040
You see guys that whenever you need to use a hardware from the phone, we need to add uses feature and

01:14.040 --> 01:16.720
set the hardware in this type.

01:16.720 --> 01:19.080
In this application we need to use the camera.

01:19.120 --> 01:29.640
You can use low latency Bluetooth camera or fingerprint, geolocation, microphone, NFS, NFC and others.

01:29.640 --> 01:32.440
We need to use the camera and set it like this.

01:32.480 --> 01:38.360
Okay, you can also remove the internet permission okay, so we need just the camera.

01:38.360 --> 01:41.680
And the camera uses features okay.

01:41.920 --> 01:49.520
So those are the camera permissions that we need to add in the manifest back to the main activity here.

01:49.680 --> 01:59.120
Let me continue with has permission Alt plus enter to import the Remember and Alt Plus enter to add

01:59.240 --> 01:59.840
by.

01:59.880 --> 02:10.040
Okay, start with mutable state of context compat dot check self permission.

02:10.080 --> 02:19.200
We need to check if there is a permission for the camera manifest, dot permission dot camera and if

02:19.320 --> 02:26.640
this equals to the package manager dot permission granted.

02:26.640 --> 02:31.320
Here we are checking if we have a camera permission.

02:31.320 --> 02:35.480
Import the operator mutable state, dot set and alt plus.

02:35.480 --> 02:39.360
Enter again just to import the operator state dot get value.

02:39.360 --> 02:40.320
And here we go.

02:40.520 --> 02:44.840
So this variable is used to check if there is granted.

02:44.840 --> 02:55.680
If the permissions of the camera are granted or not then val launcher equals to remember launcher for

02:55.680 --> 03:04.960
activity result and set the contract as activity contracts result, contracts dot request permission

03:04.960 --> 03:11.240
and if the result is granted, go and set the has permission to is granted.

03:11.240 --> 03:13.320
I want from you to focus with me.

03:13.360 --> 03:15.920
We used this new method.

03:15.920 --> 03:18.800
This is the first time we used in our application.

03:18.800 --> 03:21.520
Remember launcher for activity result.

03:21.520 --> 03:26.240
This creates a launcher that survives Recompositions.

03:26.240 --> 03:36.040
Why needed permission requests are asynchronous and need stable reference without remember would recreate

03:36.040 --> 03:41.160
launcher on recomposition losing the permission request callback.

03:41.680 --> 03:44.520
The first parameter is the contract.

03:44.560 --> 03:45.760
The first parameter.

03:45.800 --> 03:55.400
The contract defines what type of activity result we are requesting on the request permission function,

03:55.400 --> 03:58.200
specifically for requesting a single permission.

03:58.230 --> 04:00.310
So other common contracts.

04:00.310 --> 04:04.550
We can use your request multiple permissions for multiple permissions.

04:04.550 --> 04:10.150
If we have like a camera read and write external storage and so on.

04:10.350 --> 04:15.750
But since we are using a single permission, we used request permission.

04:15.750 --> 04:22.910
Also, we can set the on contract to request multiple permissions, create document, get content capture

04:22.910 --> 04:30.870
video, open document, pick contact, pick picture for taking photos, for contact selection, pick

04:30.870 --> 04:32.830
contact and so on.

04:32.830 --> 04:42.310
Okay, so there are a lot of contents we can use on a result executed when the user responds to permission.

04:42.310 --> 04:52.270
Dialog granted Boolean indicating if permission was granted and update state has camera permission set

04:52.270 --> 04:54.150
it to granted is granted.

04:54.390 --> 05:01.710
Updates the UI state since this the the has a camera permission is a variable that we created before

05:01.870 --> 05:05.790
and being updated here in the on results callback.

05:05.990 --> 05:10.870
Then after this we need to use a launcher effect.

05:10.870 --> 05:21.950
As I told you before, launch effect set it to true key number one and if not has camera permission.

05:21.950 --> 05:28.230
What we need to do, we need to request from the user a launcher for manifest camera.

05:28.270 --> 05:31.830
Here we need to check if the user if there's not a.

05:31.870 --> 05:40.510
Granted that camera permissions go and set and launch the camera manifest request from the user.

05:40.630 --> 05:49.630
If the if has camera permission, we need to load the camera screen that we created before okay.

05:50.110 --> 05:52.350
This is our application.

05:52.350 --> 05:57.830
This is how we handle the camera permissions for our application.
