Welcome back.
And this video, we are going to check out the permissions and we're going to add that to our application.
So we need to have the location, permissions and order to then, of course, get the location and then
display the weather for that location.
So here we had this geolocation provider has already turned on instead of that.
What I would like to do is I would like to check the four permissions.
I would like to get the final location and course location permissions.
All right.
And in order to do so, we are going to use Dexter once again.
OK, so let's just use the whole Dexter code, which we have done in the past.
So I have used Dexter here with activity.
And then you have to pass an activity in which you want to ask for the permission, then the different
permissions that you need with permissions, and then you have the different permission.
So in my case, it's the manifest of permission, access file, location and access courts location
and then with a listener.
So here we have multiple permission listeners and we have to implement those methods.
So on permission checked and then on permission, Rusciano should be shown.
So those two, as usual.
So in permission checked, what we will do is we will get the report, which is all permissions granted.
So here, once we have all the permissions, that's where we want to request the location data.
So we will need to add this function, which will then, of course, request the location data specifically.
And then if any of the permissions is denied, so permanently denied, then for now we're going to write
a little toast, which is going to say you have denied location permission, please enable them as it
is mandatory for the app to work.
OK, so this is just going to be the toast that we're going to display and then in on permission, Rusciano
should be shown.
That's where we are going to implement the functionality for show rational dialog for permissions.
So this is going to be another function that we need to create.
And as you can see, we have this on same thread.
So this whole thing, like this whole part up to here is actually writable in one line of code.
So this could be one line of code, basically just one connection.
So we are asking for with permissions done with the listener, which are those two listeners then with
on same fret and then we check them.
OK, so this is generally the structure that we need to follow here and that's what we do.
OK, and now of course, we need to implement those methods.
Right.
So we need to get.
So rational dialogue for permissions and what this will be is an alert dialogue, so I'm just going
to display an alert dialogue with two buttons, so positive and negative button.
So let's look at those.
This is, again, nothing new.
We have implemented this multiple times already.
That's why I'm really just going over it.
If you want to really dig deeper into that, you should check out the video earlier on in the course,
which goes over this feature.
So when the rational dialogue for permission should be shown, which means once the user has denied
it and we don't have access and we really need to let the user know that we need it, then I want to
display a little dialogue.
So we used the boulder in order to set a specific message, which is it looks like you have turned off
permissions required for this feature.
It can be enabled on the application settings and then the positive button will be go to settings and
once the user clicks on them, we will try to start an intent which will go to the application detail
settings.
Settings.
And then we need to add to your eye, which is the package name.
Which pretty much means that we want to open up the settings for our particular app, so that's why
we need to add those two lines.
Otherwise, we would just add the settings or open the settings.
But here we also add the your eyes of the link to our application itself so that it knows which settings
should be opened.
And that's why we need those two lines.
And then we start that activity with the intent that we prepared.
So we have to urai with the intent data.
And if something goes wrong, we will just ride it on the stack trace and then we have the negative
button, which will simply say Cancel has a text and the dialogue will be dismissed.
So here, by the way, we use double on the score because we don't use those parameters that are given
to us and here and said negative button.
We use one of those two.
So we use the dialogue, but we don't use the second one, OK?
That's why we can just use underscore here.
All right, now, the next thing that we need to do is we need to get the location and therefore I'm
going to create an object of use location, client so private, late in it, variable is going to be
infused.
Location, client.
All right.
So this is going to be the name of the variable and it will be of type fused location provider, client.
This one will be required in order to get the location of the latitude and longitude, so to speak,
so once you get the latitude and longitude of the user in order to then use it for our API call so that
we can get the weather for that location.
OK, so what we need to do, of course, is to initialize this variable because we said it will be a
light in it.
So we need to let in it it, which means we are going to initialize it in our own create method director
here.
So let's say that our infused location client will be location services.
Thoughts get fused, location, provider, client.
All right, and here I'm going to person this, which is going to be this activity that we are in.
All right, now, at this point, we can ask for the location of the user, and we've done that already
before.
And by the way, this is where we need to.
I had to do, which we've just prepared, so let's do that real quick, by the way, before we ask for
the location.
So here, show rational dialogue for permissions.
That is what we need to display here.
So on permission rational should be shown.
We want to show our rational dialogue.
OK, so that's why we prepared this method all together.
Right.
And now we need to request the location data.
All right.
And what we will need is the request location data function, which we have prepared before already
or we have used before already.
What that does is it requests the location, it sets the priority to be location, request, priority,
high accuracy, and then we use our confused location client, which is displayed in a variable which
we just prepared with location services, get views, location, provider, client.
And we then used that in order to request location updates.
So here we need to pass in three variables.
One of them is the location request, then a location callback, and then finally to Luper.
So we don't have the location callback yet.
So that is something that we still need to prepare and we have done that as well in the past.
So let's just use the code that we have used before in order to get this location callback, which is
basically a new object of location callback that we're going to assign to this variable.
Then we override the on location result.
So that will be called as soon as we get the location.
And you can see we get the last location of the user.
By using location result, last location, and then we can get the latitude from it by using every last
location or latitude because it is a location, and if we look at the location, it's a Pozible, which
has the parameter latitude so we can get the latitude and we can set it, as you can see here, but
we can also get the longitude, OK?
And once we have those two, we will log them on the screen for now.
All right, and at that point, of course, we have to request the location data on one point, OK,
and where would that be?
Well, we still had it to do here where we said, OK, once we have the multiple permission listener
permissions.
So once we have all the permissions that we need, we can request the location data and that is in here,
OK?
So at this point, we can test our application and see if this is going to work.
And by the way, I added this at the president, which will suppress the missing permission error,
so to speak.
So if you get rid of that, then will complain because the confused location client is not specifically
requesting for permission.
So usually you would have to surround that with a permission request.
So it's around this court.
But the thing is, before we called this request, location data is exactly where we ask for the permission
check.
So you could, of course, use all of that code and pasted just in here instead of creating a function
for it.
But if you want to request the location data somewhere else, then it really makes sense to have this
function here.
But as I said, you need to have the permissions for this to work.
Otherwise this will create an error.
OK, now let's test his application.
And we are so it requires us to allow the application, let's deny for now and then let's run this application
again, because we are not requesting the location yet by doing anything specifically.
You see, we have no buttons or anything.
It's just directly when we start the application because this application is not going to have any buttons
or anything.
It will have an action button here at the top, which allows us to refresh the location.
But generally the app should directly, when we open it up, display the weather.
OK, so here it looks like you have turned off permissions required for this feature.
It can be enabled on application settings and that is what we have prepared here and show rational dialogue
for permissions because we have denied permissions.
Now this dialogue, this alert dialog pops up and this is the alert dialog that we've prepared.
So it says cancel, which is to set negative button and it says go to settings, which is positive button.
So now we can go to the settings and we can go to permissions and we can allow this for this application.
OK, and now let's go back to the application.
Let's run it again, because as I said, we don't have any other way of testing it.
You can see.
That doesn't require any permissions anymore because it has the permissions that it needs, OK, at
this point we can start using our API and we're going to use retrofit in order to establish a connection
to the Internet and download the file.
All right.
So let's do that in the next video.
So you there?