Alright so we've established that the app's pretty useless without permission to access the Contacts.
That's all it does,
so the core functionality is now disabled.
We can stop the app from crashing though. An app that does nothing is pretty bad, but we don't want to get
a reputation for writing apps that crash.
So what I'm going to do is disable the floating action button
if we don't have permission to access the contacts. Basically I'm going to write the kind of code that
you'd use if the permission was for some peripheral function that wasn't essential for your app
to be useful.
Now one thing that may not have been obvious when you use a synthetic import, is that the widget reference
is valid throughout the activity's methods.
And that means we can refer to fab in the onRequestPermissionsResult method, and disable it
if the permission isn't granted.
So basically we can access it within the onRequestPermissionsResult function here on the screen now.
Now note that I was careful to say the reference is valid throughout the activity's methods.
It's not necessarily available everywhere in the activity.
And you'll see later that we can't always use these synthetic imports in an inner class which includes
listeners that we implement as an inner class. When onRequestPermissionsResult's called, we can enable
or disable the floating action button depending on the result.
That's quite interesting. We can just come down here outside of the test there for setting the read
Granted variable to True or False.
Now on this next line we can put fab.isEnabled equals readGranted. And basically rather than just
disabling the fab in the else clause, I've used the Boolean field readGranted and put the code at the end
of the case, so that the fab's enabled as well as disabled depending on whether we've got the required
permission or not.
Now that can be a useful technique
when we just want to disable some functionality. Let's just confirm that. We're going to run the app again.
Coming over here now, I'm clicking on the floating action button - the fab button - and nothing's happening.
So the floating action button's now disabled as a result of us not having the required permission.
Now we might do something like that to disable a button or a menu item in a more complex app, but here
it's not very user friendly.
We've got an app that doesn't do anything and there's nothing to let the user know why.
So what I'm going to do instead is comment that line out,
and now it's time for a challenge. Alright, so your challenge is to modify the onClickListener of the floating
action button, so that its onClick method displays a Snackbar if permission to view the Contacts
isn't granted. Now if permission is granted the contact details should be displayed, just as they were when I ran
the app and accepted the permission request.
We haven't covered Snackbars yet and I'm going to go into more detail about them shortly,
but if you create a new project using the basic activity template, you'll find the code you need in the
onClick method that's generated for you.
So you can copy that and paste that code into this project, and note that you can create a new project
without closing this one.
Once you've copied the code and closed a new project you'll be back in this one.
So that's your challenge.
Pause the video now and see how you go with that challenge and I'll see you when you get back.
Pause the video now.
Alright so welcome back.
Hopefully you managed to get through that.
Let's start by creating the new project,
so I'm going to come over here and do File, New project. Now I'm just going to accept all the defaults,
then Next, Next. I'm going to make sure I choose basic activity so we get the Snackbar code in the floating
action button that we need,
Next,
Finish.
OK, once that's loaded I want to go to MainActivity.kt,
and we're going to grab this code, this Snackbar code that's in the onClickListener, the setOnClickListener function
and copy that.
And that's literally all we need from here,
so I'm going to close the project.
Now the full path is up here at the top in the title bar,
so you could make a note of that and delete the directory later if you want, because we're not going
to be re-using that app for anything else.
So I'm going to close this one down,
File, Close Project,
and that brings us back into our other project which was left open.
So what we need to do now is wrap the existing onClickListener method in an if statement. So I'm going to come up here,
and by the way, occasionally I've found in Android Studio you will get these errors
when you open and close files, for some reason. So we click on build and rebuild - that will nearly always fix
the problem.
Alright you can see that the rebuild fixed that problem.
Alright so we need to wrap the code as I mentioned, or wrap an if condition in our setOnClickListener function.
So what we're going to do is, just after the fab onClick starts, a log call. We're going to put an if, parentheses
readGranted, and we're going to open up a code block. Then we're going to add an else, and we're going to open a code block for
the else.
Now what I'm going to do firstly for the else, is I'm going to paste in the code that I've just copied
from the other project - the Snackbar
code - I'm going to paste that in,
noting that it's asking for an import. I'm going to click OK to that, and we're going to change the text
message there, which will be Please grant access to your Contacts.
Alright so that's the else code, and then we need to grab a copy of the code starting from this
val projection line, down to and including the contact names dot adapter equals adapter line.
So I'm going to cut that, just clear up those lines, that line there.
I'm going to paste that in the first code block, if the if condition was true,
and just click on OK there to accept those imports.
So basically that's it. We've now got a test there that's checking to see whether we've got permission and only
trying to execute the code in the fab button,
in other words, to retrieve the contacts and display them if we have got root access, and if we haven't we've
got an else clause down here starting on line 61 that shows a message on the screen.
So what I'm going to do is go back to our emulator, and just to make sure that we're starting with a
clear slate, I'm going to uninstall.
So I'm going to close that down, and I'm going to uninstall as I would normally uninstall -
uninstall Content Provider Example, which is our app of course - and I'm going to run the app again and put
our logcat on screen.
OK and we can tab over to the emulator. You can see we're getting the standard dialog to Deny
or Allow access.
And now when I click on Deny, we got permission refused in logcat, but if I click on the fab button now,
you can see we get this groovy little Snackbar appearing on the bottom of the screen with a message for the
user.
So that's a lot friendlier.
Basically it's telling the user now that they need to grant access to their contacts in order to get
our app to display them.
So your solution to the challenge may have been slightly different to mine.
But if it works, then congratulations.
So with that said, we can do even better than that.
So Snackbars, by the way, were introduced with material design as a replacement for toast messages,
and they're a lot more interactive than the old toast messages were.
So I'm going to stop the video here, and in the next one we'll have a look at Snackbars in more detail,
and once we've done that we can then improve our app even further.
So I'll see you in the next video.