 Hi everybody in this video. I want to do a quick walkthrough of the app that you will use in your lab to test out getting app permissions and also getting the location Sorry took so long to get this up. I ran into every technical difficulty including someone running over our power pole That you can imagine. So that's why it's a little late anyway So if you go to your The module for today you will see a lab for Where am I and that is the name of our app that we're going to play around with so in this lab? I give you all the code. I just want you to be able to kind of walk through it Maybe understand it and then later if you so choose apply it in your project I'm going to first grab a copy of the project because I at least want you to play around with this apps and appreciate how it works Okay, so there's a github project It's public access. You can go download a copy of it. So I'm going to grab it and walk through the process of setting it up Whoops, so I'm going to check out a project from version control and paste in the github address that is in the lab right so Android studio has imported it for me. Thank you mr. Studio make this a little bit bigger All right So Gradle is building this right now. So it's going to take a second before this all lines up In the meantime, let's kind of take a look here Go and find the Android Manifest because you're going to need it in a second And is Gradle almost done looks like it Okay Starting to look finished anyway. It's doing its indexing thing. All right in this app There's only there's a couple of activities And we'll we'll go through each of them in a minute Let me just try and run the app though because I want to show you something that you will probably encounter or have encountered already So I tried to run the app and it gave me a build failure with exception And that exception is Google services dot JSON is missing. Hmm. Well, what does that tell you? Well, it tells you Google services dot JSON is missing But it also tells you that this app is using some Google service and probably fire base, right? So in fact this app does use fire base because we are going to be recording location in it and Staving it up into the cloud fire store. So let's go set up our fire base project to deal with this Here I am in the fire base console You can use an existing project if you would like I don't recommend using your like semester project But if you have like the fire base authentication example project still there feel free to use it or you can make a new project It's up to you. I'm going to use this existing project. I have that I use for a bunch of demos And so what you need to do is add Authorize this application to access this fire base project. So we're going to click on add app And then we'll go over here to the Android icon and click on it Okay, register the app Android package name. So this has to be precise So let's go back to our code and the package name it is looking for I'm here in the Android manifest and This is the package name. So this thing whatever this value is. That's what you got to put in that box Okay, so I'm going to grab it. It's edu.uncw. Where am I? Now the rest of this you leave blank Register the app. It's going to think for a second All right, and now it has produced for us the Google services dot json file So even if you are using this with an existing project You still need to do the step and you definitely need to redownload this Google services dot json file If you have an old version of the file on your disk somewhere, you can't just copy it in you got to get this new version Alright, so let me go grab this file. I got to get it off my other screen here Pick it up and I'm going to plop it paste it into my Where am I slash app folder? All right, so now if we go to Run this thing we should have a little bit more luck Okay, I've already got my emulator up and it looks like I've gotten past the Error that I had before so that's a good thing Take just a second to build on my very modern computer In the meantime, let's take just a quick look in the Java here Oh That wasn't so bad So there's only three activities in this app that I want you to kind of poke through and get familiar with The first is the main activity and it's just this screen here. It doesn't actually do much beyond that It just launches new activities Now let's check out the on-demand activity So if you click the button in the emulator that says or on your phone and you can run this on your phone, too No worries click the on-demand button And you'll see an activity that looks like this So I already have some data here your data will be blank and that's okay. That's expected this This activity is going to demonstrate Getting a location like on-demand when the user say presses a button or submits a post or Anything like that, right? It responds to an event. It's a one-time recording like just tell me where the last known location is Okay, so in the code the code is actually there's there's a fair amount to it a Lot of it is setting up this recycler view down here to kind of listen and get the Records as they're saved to the fire store and some of this code is saving to the fire store Which you already know how to do interestingly the fire store has a built-in data type for Latitude and longitude it's called a geo point right and it just wraps up the latitude and longitude inside a little class for you The code to actually get the location You've got to go through a process of requesting permission checking for permission To get the location If the user hasn't granted location permission you have to ask for it Once you have it then you can finally make a call to say hey Android operating system Google libraries tell me the last known location and then that's an asynchronous call and it comes back with a Success listener or a fail listener and then you do something updating your UI or recording to the database I've got it in both the lab page here and A lot of detailed comments in here to kind of help walk you through it I'm not asking you to do anything more than to try and poke at this and understand it because Later in your project you will probably or possibly be you know Adapting this for your code and you'll learn a lot through that process. Anyway, let's record the location Okay, so when I click record location in my app, what do you see? This is the standard kind of Android baked-in dialogue for requesting permissions This dialogue box is Not specified in my code My code makes a call to Android that launches this dialogue box, but I'm not specifying the layout of this thing or anything It's a separate entity, right? It's kind of happening happening at the operating system level If you do something here like click deny Okay, so you get a little toast that says your app won't work until you grant permission But then if you click it again it doesn't prompt you again like for the Settings right to to allow permission and that's kind of by design of Android Android makes it really hard that Once someone denies permission for you to keep pestering them for it. There are ways around that But it's not good practice. It's not pleasant to your users What you should do or what I should do here is give a little bit more information on Hey, you've already denied location access. Here's how you can turn it on it for me if you want to let me show you how to do that Whether you're on your phone or in your emulator you want to pull up the settings application If you pull up settings, let's see here Somewhere you'll have an option that looks like apps Okay, and you click on apps and then there's permissions in the permission manager Okay, so your goal is to get to this permission manager in this thing everything is organized by The type of permission so body sensors calendar call logs apps that can get to your camera apps that can access your location That's what we're interested in So if I click on it You can kind of scroll down and here's all the people who are allowed to access it And then here's everybody who's been denied access and here's my app So I can click on this and now change it here. Okay This is the way to do it through the system settings once the user has denied it Of course if they accept the permission whenever it comes up great, you're good to go Let me click record location now. I'm back in my app and here it pulled the location off the phone What's happening behind the scenes is it's pulling this information. It's asking the phone and the Google services What's the last known location of this device? It gets the data back then it uploads a record to the fire store Okay, and then the fire store this recycler view is listening to changes in the fire store and it updates So there's actually a lot of transmission going on here now You'll notice of course as I'm clicking record location here a lot of these locations are the same And that's of course because the emulator isn't moving If you were to do this on your device, you would see different locations as the location fix Just makes a little random noise movements, but you can change the location of the emulator So click the three little dots in the emulator menu right here And you'll get a menu that pops up for location, right? This is the emulator menu one of the options over here is location They have like a Google map integrated here, so let's change this University of North Carolina Wilmington Or none of us are allowed to be right now Let's save this point Okay, and now I'm going to click set location Okay. All right, so the location has been set to here. Let me click record location. Let's see if these things change Nope, there's still still thinking it's there Not what I expected Because as you can see here the the point is three four dot two two dot three nine dot seven seven eight six Look this thing, right? So it's in a different location. Let me go back. Let's go back here Trying to record location. No, it is still convinces there So I'm thinking that maybe something's wrong with my emulator here But if you were to run this on your phone You actually would see maybe let me change this try again. Nope still not updating something going wrong here with the emulator. I Think Yeah, I'm not going to play around with it anymore. I'll try and figure out what's going on later So anyway, this is the the code logic in here will work the problem. I'm having is with the emulator I'm not sure what the problem with the emulator is you've undoubtedly run into problems with the emulator yourself Anyway, maybe you won't even have this bug. Let me go back over Let me go back in my app The other option is to record data continuously and basically what happened. Oh here This is the new location So it was something about the way that the location was being kept in that other activity This is the new location here in the emulator So here you can see There's no record button whenever I launched this activity. It kind of fired off a background process Which again is all managed by Google That's gonna You specify the criteria for when you want the location to update. Whoa. Oh, what happened? Well, when you get a crash like that, what do you do? You got to go to the log cat. Let me get this out of this mode for a second I got to see what happened. All right No debuggable processes. That's cuz it's dead. Let's rerun this and see what happens Investigations, right? Let's just wait and we'll let this run and see what happens Maybe it maybe it was a bug me was a fluke. I doubt it. It should happen again. That's my guess Anyway, there we go. Oh, it's dead, but it's not telling you why it's dead That is very interesting Great, so this is a wonderful demo I have no idea what's going on because this is not throwing an exception. It's just dying inexplicably Let's see if I stop Is it maybe an error with firebase? I think this is probably okay Binding to service failed Uploading is not possible My guess is what's going on here is that firebase Okay, so this is oh here we go. Here's finally an exception user recoverable bad authentication Okay, so I do not have authentication baked into this. Where am I at right now? Long live credential is not available Okay, so there's some little problem going on here with firebase and authentication. I'll get this fixed up for you Actually, I'm not I'm not entirely sure why this is happening So we'll get this fixed up But what we'll see here is that the stuff is updating in the background albeit It's working for a little while and then it's crashing out Sometimes these errors can be due to the fact that you're running firebase on the emulator and firebase doesn't always like that All right, so anyway, it's updating. It's in the background. You can take a look at the code in Continuous look activity kind of see how this works and hopefully you won't run into these issues like I did on the emulator But anyway, have a look at the code try it out Let me know what you run into also try it on your phone see how that works You should get some more exciting data out of that. All right, take care and we'll see you next time