Welcome back.
And this year we are going to implement and use Dexter, which is an Android library that simplifies
the process of requesting permissions at runtime.
And we've seen before that it can be quite some work to implement that.
But that's exactly what X comes, and it really helps us to make this a lot quicker.
So can you, of course, check out this Web site, get help that consulate Kurumi slash Dexter if you
want to know all about it, because here are a bunch of examples of how to use single permissions,
multiple permissions and how to handle Århus and so forth.
And, um, yeah, I would recommend to check that out.
We're going to use some of that functionality.
So let's dive right into it.
And what we're gonna do is, by the way, we are going to have this screen here, which allows us then
to ask what permissions I already gave the permission for this app.
So let's see how we can implement that.
So the first thing we need to do is to add an on click listener for our button, which is our TV ad
image button.
And it's going to be this.
So.
This glass, which is our ad happy place activity, will be the click listener because while it implements
the functionality, here are extensive.
OK.
So then of course, we need to add the idea here as well.
So our dot ideas dot.
And here it was, Ivy Place image.
So if that is the view that was clicked on, then what do we want to do?
Well, first of all, I want to have this little.
Higher alert dialogue here and with two options.
All right, so let's add that alert dialogue.
So Volle picture dialogue, as I'm gonna call it, is going to be an alert dialogue and I'm going to
use the builder to build it.
So a builder with this as the context.
OK.
And then I can set the title of that dialogue with the subtitle method here and I'm gonna say Select
Action.
And then I need to have two different picture dialogue items.
So that's how I'm gonna call them picture dialogue items, which is going to be an array of two values.
So we have select photo from gallery.
And then the other option is capture photo from camera.
All right.
So these are the two options.
And now I can go ahead and use that picture dialogue in order to set the items that it should display.
And it's going to be their picture dialogue items.
And then what do I want to do with it?
Well, depending on the dialogue which was selected.
I want to run some code.
So depending on which.
I want to run coat.
OK.
So we have two options.
Number zero will be select four from gallery.
And no one will be capture photo from camera.
So what I want to do here is four zero.
I want to run some code.
And for one, I also want to run some code here.
I'm just gonna use a toast.
So toast.
Make text which will be displayed for a certain amount of time.
And then, of course, I want to show this dialogue.
So picture dialogue, dot show.
Now, what is it that I want to run?
If we choose to functionality a photo from gallery while I want to run a method here or Rathod method
call choose photo from gallery, and I want will of course need to be created.
So let's create another function here.
Private fun.
Choose photo from gallery.
And this one will of course, then later on allow us the permission or ask for the permission, whether
we may even use a photo from the gallery and so forth.
So we, of course, need to ask for permissions, so let's go to the permission file, which is our
manifest.
And here had a bunch of permissions that we're gonna need and I'm gonna use multiple ones, so I'm just
gonna paste them in here.
So what I will use is the permission to read external storage.
Right.
External storage and camera.
All right.
And then I told you that we were to use Dexter.
OK.
So in order to use Dexter, we need to go to rebuild Gradle.
And then had this library here.
So include a library in the built out cradle and limitation column.
Kotomi, Dexter, six zero one.
So I'm just going to add that here and then click sync.
Now here, the top right hand corner.
Once that is done, we can now use Dexter.
So now we can go back to our happy places.
And use extra in order to ask for permission.
So here, Dexter.
And then I need to import this glossier import, Dexter.
All right.
Now it's available.
Let's look at the imports real quick.
You can see now I'm using the combat Kotomi that dextra that Dexter.
And now I can go ahead and use Dexters functionality.
So here, for example, the method with activity.
So I need to pass on the activity.
Then I go ahead and ask for permissions.
So which permissions do I want to use?
Well, you can see a permission string is what I'm asked to provide.
So many first thought.
Permission thought.
Read on the.
External.
On escort storage.
And here, of course, it's easier if you enter manifest correctly.
And then import manifest.
And I'm going to import the Android manifest, then separated by Chrome on my other permission that
I want to ask for.
So many first thought.
Permission thought.
Right.
External storage.
And then.
I can go ahead and have the listener.
So with a listener and hear, by the way, we can just follow his instructions.
And for us, what's interesting is there's multiple permissions because we are already asking for multiple
permissions.
Right.
So what we need is a new multiple permission listener.
All right.
Even though it's done like this here, what we can do is we can use a different one.
So I'm just gonna copy this.
And what we going to do is we are going to pass pastie object of the multiple permission listener in
here.
OK.
So it's instead of using the nuclear word, um, using an object of type, multiple permission.
Listen to here.
And by the way, it's complaining here, because I forgot the s here, because there is another cold
weather with cold, warm method with permissions.
So that's why it was complaining about this, right?
External storage.
Now it's going to work.
And here.
Because I'm passing in the object, I don't need to Brexiteer here.
And this additional bracket here at the end as well.
Because the court has, of course, Android, because the code is Java code.
So we need to make some more changes.
So here, overwrite.
And then.
I'm using the fund here.
So overwrite fun.
On permission checked.
Then we pass in the multiple permission report.
So what I'm doing here is really just bringing it in the right format and we need to import that as
well.
So multiple permissions as well as the permission request.
We need to import that as well.
And we are going to use the one from Dexter.
So here we have a list of permissions, so permissions kolon, and that will be immutable list so we
can change the list of permissions later on.
So mutable list of permission requests.
And then we need to token, which is going to be a permission token.
So token Collen, permission token.
Then we need to import permission.
Tolkan as well.
All right.
And that's already it.
Now, we, of course, need to implement the code that we want to run.
Wunder permission is checked and Wenda permission Rusciano should be shown.
And by the way, you also saw how easy it is to have just a code from being Java code to becoming codling
code.
So here, instead of the Newquay word, we use the object that we person of type, multiple permissions
listener.
And then, well, you can see what it does want and you can see that it's called the check method.
So now we can, of course, implement those, too.
And what do we want to do there?
Well, let's start with the first one.
So with this report and I'm going to do it like this.
Now it can read everything here.
So I'm gonna say if report exists and all permissions are granted.
So report.
All permissions granted.
Then.
I want to run a in there.
OK.
So here.
This will be toast.
Don't make text.
And so forth.
Okay, so I want to inform the user, so here I'm just gonna say storage, read, write, permissions
are granted.
Now you can select an image from gallery and the text will be.
Displayed very quickly or only shortly.
So this will be called as the permissions are checked and then we see if all the missions are granted.
Then we execute this code.
Now, what shall happen if the rationale should be shown?
So if he needed to tell the user why we needed it, because he maybe pressed Noah or something like
that.
Well, then.
I will show another method.
So I'm going to create in this specific method, which is just going to be there in order to let the
user know.
And I'm going to call this method show rational dialogue for permissions.
OK.
Now, let's implement this method here.
So private fun.
And what I want to display there is an alert dialog, so alert dialogue builder with this as the context
set the message to be heard.
Looks like you have turned off permissions required for this feature.
It can be and tabled under application settings or the the application settings.
All right.
So this is just an example of what you could display.
Of course, you can write a very different text if you want, but generally that is.
What we will do so show rationale for permission.
Then we need to set a positive button, so set positive button.
How should the text be?
Well, the text to be go to settings so that the user presses.
Yes.
Or the positive buttons.
So this go to settings button, then I want to send the user to the settings.
OK.
And how I do that is the following culture.
I'm going to paste it in here.
So we have two variables that we are past, but we are not using them.
So we can just use underscore here.
By the way, we could have done that somewhere else as well.
So here, for example, we're not using dialogue.
So we could have just used underscore because we never using it.
OK.
This is just an example.
Then.
We try to run a certain intent.
Why do we try?
Well, because it could go wrong.
So we run an intent called setting's application details settings.
And what we're using here is we're passing the you or I as well, which is the you arrive our package
name.
So what we want this to do is send the user to the application settings.
We can directly change the user rights or the permissions.
All right.
And then we add this data to the Internet, to your eye, and we start the activity with this intent.
And of course, as this could go wrong, we have a try and a catch surrounding it.
And for example, the error that could arise is to activity not found exception.
All right.
So this is what should happen if the user presses to positive button.
And now what should happen when the user sets the negative button or clicks the negative button?
Well, then.
I want this.
Oh, by the way, I want this button to be saying cancel the text and hear why it's important, which
said negative.
Button you use, because in some cases it accepts certain, others it doesn't.
So you need to have the one where it accepts it and you can see dialogue and which.
And we're gonna use the dialogue that this miss here.
Okay.
So, as I said, which is never used.
So we can rename the two an underscore.
And of course, if we want to display our whole alert dialog, we need to add this show here.
And before we test that, there are two minor things that I still need to change.
And one of them is that when it comes to this Hohn.
Permission, Rusciano, should be shown.
And actually this with listener, when you want to run that, what you need to add is this on a same
thread.
So here we've before just had check.
So here like this.
The check.
And what we actually need to do is on same threat thought.
Check.
OK.
So this is the one line.
And then another thing.
One testing.
I realized that I'm not using the right idea here.
So I'm using Ivy Place image.
But it should be TV ad image.
OK.
So it's superimportant that you use the correct names here.
When entering this, I accidentally entered the wrong name.
So let's test is again.
We are.
Let's click on an image and you can see we can select action.
So select photo from gallery could see we are asked for permission.
I'm gonna deny it.
I'm gonna click once again.
And you can see it says it looks like you have turned off permissions required for this feature.
It can be enabled under the application settings.
OK.
And let's click on Go to Settings.
You can see it goes to the app info directly.
And here we can go to permissions.
And go to camera.
For example, allow camera or here, no emissions allowed.
And we asked for storage.
So let's allow storage here.
We can also allow camera here if we want to.
So now later on, it will not ask us again.
Okay, going back from here and from here, it us back to our application and now and clicking it.
We should get to the point where we can actually select the image.
And by the way, if you were wondering why it didn't show just toes to be created, if all permissions
are granted.
Because currently all permissions are granted.
That has to do with this multiple permissions report not being available.
So we need to make this a nullable and then we can run this again.
And that it will work.
So that is something that is tricky.
That is something that you probably wouldn't come up with yourself, because what we did in this part
of the code is we translated his Java code into Cocklin code.
And then, of course, it's not obvious to use notables here.
Okay.
But now, you know, that's what this causes for at least to a certain degree.
So do you see these things as well?
And you can use it in the future.
So let's click on it.
And you can see storage permissions are granted.
Okay, so let's have a quick look at what we did.
So what we needed to do is we needed to go to our manifest.
We needed to add the permissions in here.
Then we added something to our gradle.
So this line here come Cherubini, Dexter, six zero one.
We sinked this file and then we could go ahead and use his functionality.
So we needed to import it.
Of course, we had it on Click Listener for our buttons so that it does something.
And when we click on that button, we opened a picture dialog.
At least we call this dialogue like this.
And we have two options.
One of them being select the photo.
The other one was capture the photo.
And then if we click on select a camera or capture the photo, then we currently just show a text here.
And otherwise, we call this function called Choose Photo from gallery, which we create ourselves,
in which we then finally use Dexter, which is this library that we're using here.
And we use multiple permissions in this case to read and write permissions.
And we said there are multiple permissions listener.
And there are two on options or top two on functions which are called.
One is when the permissions are checked and one is have permission rational should be shown, which
means to user said no.
And in this case, what we do is we call our own alert dialog where we tell the user that it's really
important that Benito's settings be to be active.
So those permissions to be active.
Otherwise, we cannot run the functionality of the application.
And then what we do is we send him to the application settings, which is pretty useful.
So this is something that you really might need in multiple of your applications.
So that's really something that you should keep in mind.
All right.
And if the user says no, then we just as Mr. Dialogue.
All right, cool.
So that's a very important step, because now you've seen how to use a third party library, which we
have not used before.
OK.
So I'd say let's go to the next video where we're going to implement these further features.
Soucy, they're.