Welcome back and this video, we're going to look at permission, so I'm going to create a new project
here, I'm going to use an empty activity and I'm going to call this one per mission's example.
All right, because we're going to use permissions quite a bit in the future.
So in the other projects that we're going to build on, of course, in the project that we are working
on right now, because I want to have access to images, because we want to have an image background
and we also want to be able to export images.
And so we will need to have access.
And in order to get that access, well, we need to ask for permission because that's how it works in
Android.
OK, so how we start is, first of all, we go to the Android manifest and in the Android manifest,
we, first of all have to use a permission.
So you can see here just underneath the package name.
So just on top of application, that's where you would go ahead and add this user's permission and then
you can ask for many different permissions.
So Android thought permission dot.
And then you could see there are so many different permissions that you can ask for.
All right, let me do that again.
Here you can see.
Access a lot of access, then, for example, the camera, then the location and so forth, so anything
that you can you can think of.
All right.
So we're going to start with a camera, because that's the most obvious one, I'd say.
And we need to close that bracket like we could have done that like this as well, of course, to keep
it simple.
All right.
So now you could go ahead and add a bunch of permissions here.
All right.
You could have multiple different permissions.
And then instead of camera, you could use something like location.
And the actual location is X's final location.
So that's if you want to use GPS.
OK, but we're going to see the different permission names once we get there.
And that is also something that you can just look up when you know what you should do, then you can
look up which permissions you were required for.
All right.
So this is just the beginning.
This is just to tell our app or tell Android when it installs the app, that this application will at
one point need to use the permission that we are defining it, for example, will want to access the
camera.
But that by itself is not enough.
We will need to add a lot of code or quite some code.
It's not super much, but it's still quite something to the activity in which we need to permission.
OK, so what I'm going to do is, first of all, I'm going to create a companion object.
Companion object is basically a data class which just contains constant variables.
So I'm going to create this variable called camera.
Underscore permission, underscore code.
And all in capital letters here, and I'm going to assign a value of one here so I could assign any
value here, and even if I write it incorrectly, you can see it.
What's the work?
But the idea behind this is that this is just a variable which I use in order to identify which permission
I am looking at because I now assigned the code one to the camera permission.
This is something that I have done with this variable.
OK, I could have used another value here.
I could have used five if I wanted to, but it makes sense to start with one and then the next permission
that you would add would be two, for example.
So private const Val, this one could be fine on this location code or find location on the score permission
code.
That could be two for example.
OK, but we're not going to use that.
It's just for you to see the general structure so it doesn't have to be to again it's a number that
I have decided to use, but now we will see why it is important that we do that because we need to have
those constants, because we're going to require them once we actually ask for permissions.
So what I'm going to do in this example is I'm just going to add a button to my activity.
So here instead of Hello World, I would like to have a button.
So I got rid of this text there.
And instead of.
Showing you the whole thing, I'm just going to paste it in there because you know how to create buttons,
right?
So just going to copy and paste the code in here.
Inside of my constrained lay out.
I would like to.
I have my button, OK, so I'm going to call this one button camera permission, it's going to rep the
contents, the text that it says is going to be a request camera permission, and then it's just inside
of my view.
All right.
And then that's pretty much it.
So let's look at it.
The design.
You can see we have this button here, request camera permission.
All right, so once I click on this button, I want something to happen.
All right, so let's do that.
Let's at this in the uncreate method, we know how to do that.
You can already post video and tried yourself.
So please go ahead and add an on click listener here.
OK, I hope you tried it, so button camera permission not set on Click Listener.
All right, and what I'm going to do in here is I'm going to now check if I have permission.
So if Drew, I'm just going to keep it simple now, because we are we need to change this condition
later on.
But in here, I'm just going to use a toast.
So I'm just going to say make text.
And this will be the context of all this activity.
And you already have access or you have already have the permission.
So actually, let me just paste the code in here.
So you already have the permission for the camera and it's just going to show for a long time at the
bottom and then otherwise.
So if we don't have access, that's where we want to request permission.
OK, so this is the code that should run if we have the permission and the code that should run if we
don't have the permission.
OK, so let's check it, let's actually adjust the condition here, and it's a rather long condition.
So basically what we do is we use the context compact class and in there we check self permission.
So there's this method called check self permission.
It needs a context.
So I'm going to pass this, which is the context, main activity.
So the permission should be asked in this activity.
And then I need to say which permission that is.
So I'm going to say manifest dot camera.
OK, so I'm asking for the camera permission.
And as you can see, I need to import manifest here.
And we need to use the Android one, OK?
Once you have many first import it, you will see is that you can now.
Use all of the different permissions here, but we are looking at the camera permission, OK?
It's the same as we have already defined in our Android manifest.
OK, this Android permission camera only that now it's called manifest permission camera.
So we're getting the permission from the manifest here.
And then we need to add another value here.
All right, so that's already check self permission.
If you want to know more about yourself, permission you can always control and click on it and then
you will see what it does.
So you can see it needs a context and a permission.
But as you can see, it will return an integer so it will not return a boolean.
And that's why it's complaining.
So our if statement, it needs to have a condition in here which returns either true or false.
So what we can do is we can compare that.
To package manager.
That permission granted, so you can see you can either check if you had the permission denied or you
got it granted.
So I'm just going to use permission granted here.
OK, so overall, it's going to look like this.
So if context checks are permission, then.
We look at the camera, so that's the permission that we want to know if we have that permission and
then we say equal, equal package manager, permission granted.
So we check if the permission was granted before, but that's just checking if we have the permission.
That's not even requesting the permission.
So how do we request a permission?
Well, in order to request a permission, we go to this block here where we say activity, combat dot
request permissions.
And you can see there is an S here, which means it's a plural.
So you can ask for multiple permissions at once, but we're going to keep it at one for now, which
is just going to be the camera.
So you need to pass the activity and then an array of the different permissions that you want to look
at.
And in my case, I just want to request permission for the camera.
So permission that camera.
Now, this request permissions method, if you look at it, it requires an activity.
Then it requires the permissions, which is an array of strings, and then as the final parameter,
it has to request code so it will require a request code.
And here we look at what the request code should be, Kansi application specific request to match with
a result reported to you on request permission, result, callback and string, OK, but should be greater
than zero.
All right.
And that is what our permission code is for.
That's why I said we should start with one.
So it should be greater than zero, which means we start with one.
And that is what I need to pass as the final argument here.
OK, so what it looks in the end is, as you can see here, activity, combat request permissions.
This array of then the array of permissions, in our case, just a camera permission and then the permission
code and in our case, the camera permission code, which we defined there at the bottom.
OK, so this is the second last step we need to have one more step, and that is what should happen
once we get the permission result.
So once we did that, request permission and we get a result.
So either the user gave us the permission or he denied the permission.
So in order to implement that, we will need to add an override here.
So that should be outside of the uncreate method.
That's important.
So here on this level of the companion object, which is still inside of our main activity.
All right.
So in here I will override on request permission result.
So this method here on request permission result, it has a request code, which is of type integer,
then it has a permission array and great results, which is an array of integers.
All right, so here we can check if the request code.
Was our camera a permission code?
Then we want to execute something.
OK, so we have two options, either when the request was actually our camera permission code, we got
access denied or we got access granted.
So let's look at access granted.
So if grant results, which is the grant results here.
From our method, the grant results, which is an array.
Which is automatically passed to on request permission to be sold once this is called.
OK, so we will see in a second when this method will be called on.
So look here is not empty.
And the grant results at the position zero, so at the first entry, so to speak, is equal to package
manager, that permission granted.
And you need to double equal because we're not setting it, we're comparing it.
All right, if all of that is true.
Then I want to make a little toast, just letting the user know permission granted for a camera.
OK, so I pasted this code in here and this will be executed once we have access or we have just received
access.
And then what should happen if we don't get access?
So if we didn't receive any access to our camera?
Then we want to write another Tostes message here, which would be, oops, you just denied the permission
for camera.
You can also allow it from the settings, OK?
So that is it.
This looks a little complicated, especially as a beginner, I can totally understand, because it's
a long process, right?
We need to do so many things, but it's something that you just have to copy.
It's not something that you have to come up with or that you have to create out of your own imagination.
It's really something that just is the structure.
And if you don't like this structure, if you want to have it in another way, then you can, of course,
always use third party libraries, which can make this process a little easier.
But then you need to understand how to import third party libraries.
And that is something that we're going to do later on in the course.
So no worries there.
OK, let's repeat what we need to do and we are going to do that while the cradle will be built.
So first of all, we need to go to our Android manifesto, which is this manifest here, Android manifest
XML file.
And then we need to add users permission and permission, name whatever that is, if it's the camera,
if it's the location, if it's access to the user's contacts, to his library or his gallery and so
forth.
So you can see it could be anything.
Right.
So we say we want to have this permission at one point.
If you don't have this line and you asking for permission in code, as we do here, it will not work.
All right.
So then the next thing is you need to have a trigger which triggers this event.
And in our case, it's just this button, this button click.
Once we click on this button, it will trigger this request.
So how do we trigger this request?
Well, with trigger it with our button camera permission set on Click Listener.
And we actually trigger the request if we don't have access yet or if we don't have the permission yet.
So we ask first, do we have the permission?
If we have the permission and somebody clicks on the button, it's just going to say you already have
permission.
And if we don't have permission, then we will request the permission.
And in order to do so, we use activity compared to request permission.
Then we use an array of the different permissions that we want to request and then we need to pass in
the camera permission code, which is a static constant that we created ourselves here.
Camera permission code, which are set to one.
OK, the next thing is.
What shall happen once you have access to the permission?
Well, that is something that you will probably not even need.
So this code here, you will not even need it, except you want to do something specific once the user
gives you access.
But usually you wouldn't even need that.
So in this case, I added it so that we can see what happens once we give permission.
So we check if the on request permission result.
Is giving the request code of our camera permission code, so the code that we created ourselves, this
constant down here.
So if it's the same code and where does it come from?
Well, it comes from this request permission.
So once we called this thing here, this request permission automatically on request permission result
will be called once we click on the button.
So which button do I mean?
Well, not this button.
So let's click on this button.
You can see this comes up so this little dialogue comes up and I can either allow or deny.
So let's deny first.
You can see, oops, you just denied the permission for camera.
You can also allowed from settings.
Now, let's click on it again now you can see I have three options allow, deny and deny.
Don't ask again.
The third option is there for people who are annoyed by constantly getting requests, for example,
GPS, even though their application is just making images or it's just editing images.
So why would anyone need your GPS or you're using a game and for some reason it asks you for your GPS
data?
Well, it usually asks for that because it wants to give you advertisement, which fits you or your
country or whatever.
OK, so that is something that you can, of course, deny.
So let's deny that.
Now let's click on it again.
And you see it's not popping up again.
So we cannot request again.
We cannot get it.
Well, in order to still get it, you can go to your settings.
And then go to permissions, so here you can just search for permissions, so here, let's search for
permission, not permissions.
And here the permission manager.
OK, so inside of the permission manager, you can see which apps recently were opened.
And I'm just going to open up this permission example and here I can change its permissions.
So here on the permissions, you can see no permissions were granted, but the night was camera.
So now I'm going to allow this and let's go back to the application.
And now let's click on the button and it says you already have the permission for camera.
So now we can use the camera in our application because we got access to it.
But we always need to have access first.
All right, so in order to get access, we needed to request permission and here we are just checking
if we have permission.
So if we want to run any code that is depending on the permission, that is something that we would
always need to check first with need to check.
Do we have permission to use the camera?
And only if we have permission to use the camera.
Let's execute our code.
That should then run, for example.
Taking a picture and then editing the picture with a filter or drawing on top of it or using it somewhere
in the application.
OK, so that is the structure of the permission management and as I said, you will not need this if
you don't want to specifically execute something once someone has access.
But what you can do is on request permission results.
You could store it in a variable.
So the result, if you have access or not any variable so that you don't need to do this check all the
time, but then you could run into a situation where the user actually changes the permission inside
of his settings and then suddenly your app doesn't have permission for a certain feature anymore.
All right.
So now I'm going to show you how you can actually access multiple permissions.
OK, so I'm going to add this variable here, this fine location permission, and I'm going to add another
one const volle.
And this one will be.
Camer and find the location.
Promotion code.
And I'm going to call it one, two, so this one, I will set the value to one, two, and then in the
end, what many of us would need to use this line.
So we need to access the fine location.
So here I'm adding the additional permission here and then we can go back to our main activity because
that's where we actually want to add to request permission code.
So here, it's not just going to be the manifest permission camera, but actually also and this is separated
by a comma here manifest.
Permission access, fine location or fine location?
This one here.
And then, of course, the permission code will not be the camera permission code, but the.
Camera and final location permission code.
And then, of course, here in the check, where we check, if we have access to the camera, we would
also need to check if we have access to.
The final location, so I'm just going to copy this line, not the whole line, but just as his statement
and I'm going to add an add or end and I'm going to now check both.
So here, camera.
Replaced with access, fine location.
So I'm checking both, I'm checking the camera and the final location and check if I have permission
granted, and then I'm just going to say you already have access or permission for camera and GPS.
All right, so now let's see.
What happens because we already know that we have access to the camera, but we don't have access to
that GPS yet.
So it should still request for permissions.
And of course, we could have also changed her name here, so it's a request camera permission, but
it's more than just a camera position now.
So let's click on it.
And we can see now we are asking for the device location.
So I'm just going to allow it and I'm going to click on it again.
And you can see I already have the permission for camera and GPS.
All right.
So now you know how you can get permissions to your application and how you can use it, and we are
going to use that more.
So no worries if it was a little tough to understand and a little overwhelming because it's quite a
long process or quite some code that we need to use here.
And we're going to use that quite a bit.
So it's going to be a natural later on.
So no worries there.
And yes, you in the next video.