Skip to main content

Getting Ready for Photos

Lesson 27 from: Building a Twitter App

Tony Hillerson

Getting Ready for Photos

Lesson 27 from: Building a Twitter App

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

27. Getting Ready for Photos

Lesson Info

Getting Ready for Photos

before we get to the point of actually uploading or or ah, or finding the media on the device. We need to get ready and do a little bit of, ah of preparation to get some of those screens ready to show. Um, the information that we need Teoh gather from the user. So first of all, we're gonna add a big fat button to the right of the tweet text area to the edit text. That will be, ah, the button that the user will use to attach a photo to the tweet. Um, we're going to kind of give a double duty. So if we don't, we don't have the twit pic credentials, which are basically the user name and password of the users Twitter account. We don't have those yet. We need to ask them to sign in. So if we don't have the credentials, we're going to make that button say, sign into twit pic, and then it will just go to a setting screen where they can put in their credentials. And those credentials will be ready the next time they want to go attach a photo to the tweet. And if we do have credentials, then we...

'll actually say Attach a photo and, um, once they press that button will give them an option to either. Okay, so this is This is the This is the flow. If they don't, if they don't have Thea, we don't have the credentials will go to this setting screen that we're going to add and and have them put in the credentials for Twitpic, um, and then will also add a settings button to the menu so that you could get to that setting screen for more ever and then that will allow a little bit of extension later once they once we want to add new any new settings beyond just the Twitpic ones. And then once the once the credentials there in there on the post screen, it'll say, attach photo instead of sign into twit pic. And then, once you attached that photo will be dealing with that in or when she pressed attach photo, we're gonna be dealing with that in some some steps down the line. So let's get these screens into place and start dealing with the storing the twit pic credentials as well. So I'm gonna switch over to the code. Let's look at eclipse. I'm looking at Ah, in the code. Step one photos getting ready for a week five. So let's ah, do a quick tour of some simple stuff. First of all, in the resource is directory Inside the draw bowl directory. I've added a, um, an asset. A drop ball resource called Equalizer, which you saw back in the slides, is the icon that it will be putting onto the menu item for setting. So let's go have a look at that really quick. This will be sort of a little bit of review from last week, so Ah, inside the resource is directory again. But inside the menu directory, that is where the the menu items air all composed in XML and then loaded in later when we construct the menu. So if I open that up, you can see that now we've added one more item here online, five menu items, settings is the I D. The title is just settings, which is a in the strings, and I've also added that icon as the as the icon for that menu item. So if you remember how the menu works, um, in each one of the activities that we want the menu to appear. We have to override certain methods that certain FBI methods that Andrey gives us. So let's go look at say, for instance, the status list activity, the main activity where we show the tweets. Um, if you remember down here on the bottom of the file, there's a couple methods that we had override. One is on Create Options menu and the other is on options items selected. So notice that we haven't actually made any changes here to add that new menu item because we've tried Teoh deal with either constructing the menu. Um, well, that's done with the in the sort of normal, normal way you would an android. So he's a menu inflator instead of the instead of a view inflater to G O, which we can grab from the activity. But we ask that to inflate the menu from the XML that we just looked at and then say that yes, we got that inflated and it's ready to be displayed so nothing changes there. That's the standard and road wave of displaying the menu for the first time, when the menu was first created and then it will be dealt with by the framework. I'm from then on. Um, but one of the when one of the options is selected from the menu, this method is called and on options item selected, and it will pass the menu item. We don't deal with this in the activities we try. Teoh, we delegate this onto something we call the menu helper so that we don't have toe Teoh have a bunch of duplicate code all over the place. So, luckily, luckily, we can see the benefit of that right now because now we had this new ah menu item. That's Ah because to deal with that isn't just in one place. So if I hover over this online 1 54 status list activity the open activity from menu item method on the menu helper and hit F three I can jump into that that method static method on the class and you could see that all we need to do is lie. Ad had Edward, uh, when you see her in line 33 to 39 which is a new case in the switch statement that deals with when the user clicks on menu item settings. And then, um, you can see that I'm checking the class of the activity that comes in to see if it's if it's the settings activity. And then if that would mean that, um, they're requesting this. They clicked on the menu option on that menu item from the setting of activity, and they're requesting to go to the settings activity, so I'm just going to ignore it. But if it's not the settings activity, then I'm going to move on to the settings activity, which, as you may or may not remember, from from what we've gone through so far, that class we haven't run into. Somebody hovered this hover over the class name and hit F three, and you can see that's a pretty straightforward activity. We've just got a couple of different ah components and to edit text fields for password and user name. And then we have a reference to the application as well. And remember, let's let's look at the view for that really quickly. One more time. That's that's the view in the middle here that says settings. All we all we need to do here from the views point of view is to you grab the user name and password, so there's not a lot that needs to go on. And here but on create we set up the view to be the Are that layout that settings you And then, instead of views, jumped onto that hit of three down on line 58 of settings activity. You see, we just grab a reference to the user name text on the password text, and then, uh, would go back to on create. You could see the load settings is called necks, which is ahead of three defined down here online. 46 And I'm going to do in this case is this is, um, in the case that we've already got the Twitter pic twitpic credential stored. I'm going to put the user name into the user name text. I'm not going to display the password that's gonna show that I've already got something there. So it's kind of an indication of user that they've already filled this out pretty simple. Um, just so there's not a lot of code. We could probably do something more interesting there. Um, like, not show the fields or something, you know, and then and then make them visible only if we wanted to forget those credentials or delete that delete those credentials or something like that. So when the first thing the settings activity will do when it's created will get a reference to these these components and then fell out the user name with the the twit pic user name if we have it. So that begs the question of of understanding how we how we know whether we not her, not we have those credentials. Um, you can see that this is a method that we're calling on the application, which is our are overridden application class Theo tweet application, which we get a reference to appear online. 24 of the settings activity. So let's go look at how we get, um, that saved setting if we have it. So I'm gonna hover over here, get twit pic user name online hit F three, and it will open that code in a tweet application. Um, it's all this does is passed back. Whatever we have in the local variable are the instance variable user name and the same thing for the password. In the instance, variable passwords were keeping a reference to the user name and password on the application just for simplicity sake. Um, so where those actually set? Let's go up to the top and see, um, first of all the definitions on line 17 in line 18 of application, but in on create, I go through and actually grab those. So when the first time the application is loaded, I go and grab those just in case we're starting from scratch. The application was forced. Kuwait or something like that. We could go load those up when the application starts, and they're all ready to go. Um, you can see that I grabbed those from something called the Settings helper. Um, and if you remember the code that we set up in the first week of this class, this looks familiar to what I had called at that point the OAS helper. And in this case, I just made that same class a little more generic, generically named, and we'll go look at that in just a second. But it now it's now responsible for not only grabbing all the information that we need to tell Twitter what the idea of our application is. for a lot of purposes and storing the off the access token for the user. Once they've authenticated with Twitter, it's now gonna also deal with storing the twitpic credentials so you can see what we're getting him from from this setting sulphur on create. And also just to kind of close the loop here if I want to set those I also have a method, um, down here on the application online 84 to 88 called safe twit pic credentials, which just takes two strings and then we'll save those through the setting supper. So the application really just delegates onto the settings helper. So let's go take a look at that and see what this thing is. Um, I'm gonna go appear to, um, command shift are on the Mac, and I believe Ault shift on windows. That might be wrong. It might be control shift, and that is to open a resource. And I can type settings helper and then go open that from step one. Look at that class. That's a quick way to get to another class. Um, and see that there's a lot of stuff that guard have seen in here for one thing there's the the static, the constants for different hash keys that were storing, um, things like the off key for are the off tear that off Secret key, which were storing in the properties file, Um, that we pass on to Twitter, and then we're also starring twit, Big user name and password. Now, these new to to hash keys are constants. What uses hash keys? Um, and if you'll remember just a second ago to the methods that we called were get twit pic credentials online 73 and save twit pic credentials online 77. So saving them, um, the way that we deal with local settings. So Okay, so let me scroll back up to the to the constructor. The constructor. The first thing off the bat once it sets a reference to the context that started it. Um, we also said an instance variable preps, which points to you, Um, something that we call off the context, um, in return called a shared preferences, which is an instance of shared preferences. You can see online 21 premises is a of type shared preferences. And what we need to do is type in a key because you can. You can hold more than one shared preferences for your application, and then we want to open this up in mode private. That means that when we write these preferences, it'll be private. Um, no other application will have access to these preferences. We could also have public preferences that you may share between applications that you know about or let other applications read. But in this case, we want to keep these private. So he's gonna be some private shared preferences that Android will manage. Manage for us through this shared preferences. Um, this instance of shared preferences. So once we have a reference to that online 29 then, ah, once we set or get the twit pic credentials, you can see that the way that you set them is to on the preferences object you call edit, and then you get something called an editor, and it editor is just a way to do something like it's kind of acts like a hash you can put put things into it. So we're gonna put two strings which use these constants is the keys and then used the user name and password that we fasten, and then you have to commit those changes through the editor. And then they're they're gonna be saved locally for next time the app can The app could be killed. Napkin. The phone could be shut off. Any of those things could be can happen And then the preferences will still be saved. And then to get those twit pic credentials, we're just gonna We're going to construct a little, um, anonymous array here in place and then get strings out of the preferences. Object for the twitpic user name and password. That's pretty much it. We've got everything that we need to store those those bits of information that we need to log into twit pic locally. And we've got a menu item gives to the setting screen, and we have the settings activity to show the the controls needed. Teoh edit that information. So let's give it a run and see how everything looks. So go over here to the emulator. Um, looks like the activity is not starting. You can see down here in the council because it was already the exact same application I had already deployed. So here we go. Look at the menu Hey, Tony. Yeah, go ahead. Ah, there's a question in the forums from our G o asking if ah, the preference is stored in shared preferences is encrypted. It's not encrypted to my knowledge. So, um, so Ah, yeah, that's that's as far as I know. It's not encrypted. So it's not really too tough to get that information out of their, um, for you. You don't have to jump through any, like encryption and decryption things like that, but it also makes it a little slightly less secure, I suppose, for if someone was to get access to your phone, but in the normal coast of operation, um, there's no way to read shared preferences through other applications. So it's It's fairly secure, but not completely secure. Okay, so, um, once we hit men, you can see that there's the new settings item here. Once I click that, you can see the the twitpic credentials. You can see that already put in my credentials for this. Ah example. Application. Um, and I'm not gonna put the the password in here, but I do show the user name to show that we've at least got something in there, and that's what we've done so far. So now we can gather up those credentials and use them during the next coming steps. So kind of, in summary shared preferences is, ah, classes you can use to easily save simple, not really complex structured data. Locally, it's a lot easier to deal with in a sequel. I database. So, um, let's go on the questions for what? We've gone over here in this step. Okay, Tony, I have a question from th and go, Huebner. Ah, Where do the twit pic user new user name Constance come from? Are they provided by M an android library or the OS or from somewhere else? Ah, let's see. Do you mean twitpic user name and twitpic password? I think that I think that they mean those. And if they do, they're defined online. 19 and 20 of the settings. Helper. Do you have any questions there, Brian? Yeah, I have. Ah, the chat rooms kind of starting to overload with questions here. The first question is ah, why use Twitpic for Jay if you can't use the off tokens if that we already have from Twitter. And there was some discussion that there the whole office, actually inversion one inversion to of the Twitpic library. Oh, interesting. I just I didn't I guess I didn't look deep enough into the code. I just looked at one example. Okay, well, I'll be looking into that later than or if anybody else is interested, they can look into it too. That's why we have ah, code on. Get up. I actually have one more question from Goto Webinar. How did Tony get past the activity? Not started warning. When deploying the page, we're going deploying the APP. Okay, so let me explain that a little bit better. So what they're referring to is what I kind of pointed out here. It just said when I tried to deploy this project, um, on the emulator said there's a warning. Activity was not started. Its current task has been brought to the front. So basically, it was already running and there had been no changes to the project on the So the version that was already running on the emulator was just brought to the front as if it had just started. So it wasn't and it wasn't an error. It was just saying, Hey, you're already running so you don't need to deploy. Okay, Um so I have another question from Matthew. Um, does mere use of off a P. I assure that a rogue program cannot steal them and we don't even use. Oh, off yet for twit, twit pic was the rest of the comment. Right? Um, I didn't quite understand the first question, but yeah, we're not using it a lot for twit pic. I didn't I just looked at a quick example to get things running because I was more interested in showing the the fact that you could upload something from the camera or the library. So, um, apparently, you can use a lot. That would be much better. A much better option. In fact, the basic that's not gonna work in the future where the shared press physically stored on the device. So I'm not sure I think it may actually be stored in something like Sequel eight. I don't know what the implementation details are, though. Um, but it's stored in a directory underneath the your applications directory. So each application, when it's installed on the device, gets its own directory, and it's so it's Lennox running on the device. and the permissions air set on that directory to be only that application. So no one no one? No. That's what this mode private means. It pretty much maps to something like a linen. Um, permission. That means that no one else can see it. Do those files get encryption of any kind? No, they don't know. OK, do you need to have a user name and password for tweet pic to take? You actually use your Twitter user name and password. Although, like I said, oh, off is the better option. And now they realize that's available. That would be how we write it in the future. Okay, so I tried to end up updating the examples.

RELATED ARTICLES

RELATED ARTICLES