Exposing Data and Information Outside Your Android App By Cybersecurity Team PK


Introduction


Welcome to the Exposing Data course, Exposing Data and Information Outside Your App. This is our first module, Creating a Content company. My call is Jim Wilson. Throughout this direction, we're going to discover ways to create opportunities with applications and users to interact with our app's content material and approaches to fall outside of the traditional app enjoy. Now the traditional app experiences customers directly interacting with our app. A user can see consumer app activities, and thru the consumer enjoy furnished with the aid of the app's sports, the user can view and interact with our app's records. So conceptually, I need to think of this direct app interplay as a user working inside our app's bubble. Now normally speaking, this direct interplay is the primary manner that customers revel in an app's content material, however, there are numerous situations wherein it is used to make some of a part of the app's content available even if a person is not at once interacting with the app. In different phrases, it's on occasion beneficial to make our app content available outside of that bubble. Well at some point of this course, we're going to see a number of the methods that Android permits us to do this. The first answer we will look at on this direction is something referred to as a content company. Now we now and again use content carriers within our apps to centralize facts management and facts garage details. The content provider interacts directly with the statistics save and then different components of our app can get right of entry to information thru the content issuer, which then shields those components of the app from the information of facts storage and control. But similarly, a content provider can be exported. Now as soon as exported, our app's statistics can then be accessed from different applications. So with a content material company, other packages can read and regulate our app's information in a way it truly is controlled and secure. And then once we finish up with content providers, we're going to study every other way our app can reveal content outside of the conventional app revel in and this is one you're in all likelihood familiar with, person notifications. User notifications are the manner the application show messages or signals. They're an amazing manner to provide the person with records, even if the consumer isn't always at once interacting with your app. For example, Gmail makes use of notifications to indicate whilst a brand new message has arrived. And then we will finish up the direction with something known as pending intents. Pending intents allow our applications to delegate actions. For example, we are able to create a pending motive that incorporates records showing our app's primary interest. We can then bypass that pending cause outside of our app, for example, we will ship it out to the notification machine, and then in response to a user motion, a notification gadget may want to then use that pending rationale to start our app's essential activity. Alright, in the order you can see, we've got lots of awesome stuff ahead. So to get us begun, in this module, we're going to get content material providers. So we'll first look a bit nearer at simply what a content provider is, we're going to then see the way to put in force a content material issuer, we're going to see how to request records from a content material company, and then we're going to end up with a study how we are able to use our content material issuer to share facts between apps.

Content Provider Overview


To get us begun, we'll first observe a definition of a content material company and then we will damage down what that definition means. So a content provider is an Android aspect that encapsulates records, exposes that information to a fashionable interface, and optionally, makes that statistics to be had to multiple applications. So let's observe what that really means. Well fundamentally, a content provider is a way to expose statistics. The key is, even though, it's a standardized manner to show records, and so it gives us a general manner to show data, but gives us quite a few managers over the visibility. So we create a content material company, we will restrict its visibility to handiest the application that implements that content material provider, or you could sincerely make the content material provider visible to different applications, so it is a manner for one software to proportion information with different packages. Now something it really is very crucial to recollect is that the concept of content material companies is unbiased of data garage. So content material companies don't have anything to do with facts storage. So a content material company may serve up statistics this is saved domestically, some other content issuer may serve up facts it truly is saved remotely like off in net provider, but another content company might take a few neighborhood data and a few remote records, merge them collectively, and then serve that up. Now many content material carriers will serve up facts this is statically saved, however, a few content carriers will clearly dynamically produce that facts. So while a request for facts is available, it'd really programmatically put together things and then serve up the consequences of that programmatic action. The secret's that content companies encapsulate the information, so we simply serve them up as a content material provider, and the client of that content company is not at all involved with how that data is turned into stored or produced. Now a question that in all likelihood involves thoughts is properly how do content material vendors and SQLite examine each other because we spent that complete ultimate course speaking about SQLite and that become a records solution. And now we're selecting content providers and that's a statistics solution. And so, it is critical to understand that they clearly do kind of exceptionally separate matters. Alright, if we take a look at SQLite, SQLite is a records garage and control solution. Alright, so it surely identified how we store our records and we want it to collect the information in an SQLite database, we virtually use the SQLite library or a library unique to SQLite to get admission to that facts, and that data became available by our own application. So our utility created that database for our own functions. Now a content issuer, once more, has nothing to do with storage. Content companies are information get entry to answer, they're a way to reveal information and a way to expose that records in a popular manner, and using content carriers, our application can clearly disclose that information, now not most effective to our personal application, however also can make it to be had to different programs. Alright, so now in our next clip, let's test away content providers are maximum normally applied.

Content Provider Common Implementation


As we stated, content vendors are independent of any specific form of information storage. They can serve up something type of statistics you need to serve up. But whilst we implement content material carriers in our own programs, most commonly we put in force them together with SQLite. Because remember that content material providers in SQLite do exceedingly separate jobs. SQLite is ready information garage, so we use SQLite to provide our backing keep. Remember that content providers are approximately information get right of entry. They permit us to encapsulate the garage details of that information and they allow us to percentage facts with different packages if that's what we'd like to do. So permit's test the application we have been constructing during this path series. Remember, inside the preceding path, we've updated our application, and now I'll work with an SQLite database. It has tables in it, a note_info desk that contains we be aware information, and our course_info table that contains our route records. If we take a look at our observed pastime, which we used to display a single note, nicely I got that note from the note_info table. Remember, that pastime additionally has a spinner that shows a listing of guides and it receives that list of courses from our course_info table. So then we have our main pastime. Remember our foremost hobby got the list of nodes from the note_info desk. Remember that our note_info table didn't contain the title of the route, so we surely needed to be a part of it in opposition to the course_info desk as a way to display the identity of the route it truly is associated with a be aware. So what that means now could be that each of our observed activities and our important interests at once depends on the current structure of this database. If we have been to start making tremendous adjustments to the structure of that database it would have an effect on the being an aware pastime and it might also affect the principle activity due to the fact they're querying at once against the presently saved layout of that database. Now in addition, understand that our database record is personal to our utility, so activities belonging to other applications can't get admission to that database. Now that's exact from a protection viewpoint, however, it does create some challenges of when to proportion statistics from our database with other packages. So let's look at how content material providers affect this. So what we'll do now's rather than have our activities pass directly to the database, I want you to use a content material company. The content material provider will sincerely be the one that at once accesses the database and then we'll make statistics from that database available to our sports. So our be aware interest will get its facts from the content company and the principle interest gets its records from the content material issuer. So now with this version, the content material issuer is protecting the activities and the storage info of the database. So now if we make adjustments to our database, or even if we considerably restructure those tables, as long as we replace the content company to keep in mind that new shape and it continues providing that records in a consistent style, the activities are absolutely shielded from that info, so we don't have to make any modifications to the sports. In addition, content vendors can be exported, because of this, they are able to turn out to be seen to different applications. So facts from our database can now be shared with activities in different programs. Now all of their access will nevertheless bypass through a content material issuer, so they're not immediately gaining access to our database, so we nonetheless have a lot of control over what they can do, however permits the shared records from our application with different applications. Alright, so now in our next clip, permit's check what is worried in creating a content company.

Creating a Content Provider


When we want to put in force a content company, what we are going to do is amplify the Android magnificence named ContentProvider. Now it is critical to take into account that content material vendors are additives similar to activities are components, so content material provider virtually has a lifecycle to it. So while we put into effect our class, we are going to need to implement our lifecycle methods like onCreate. Now content material companies guide statistics lookup, so we'll need to put into effect the records lookup technique. And content companies permit modification, so we will need to put into effect our statistics modification strategies. When the without a doubt crucial component of creating a content issuer is the content material issuer's identity. Remember the content material provider can be seen at some point of the device and you may never know what different packages might be on a user's tool. So what that means is that the identification for our content provider should be globally precise, so it must be identified in a manner with a view to now not collide with all people else's content material companies. So the way we do this is by way of associating an expert with our content material company. Now authority uniquely identifies our content issuer and the way we specify an authority is with the aid of the usage of reverse domain call format. And as you may don't forget, reverse area call format is the identical manner we call Java programs. So normally what we're going to do is we create a content company, the authority for our content material provider will be some thing our package deal call is followed by way of. Issuer. So if we have a look at the application I've been operating on all through this route series, the packaging for my software is com. Jwhh. Jim. Notekeeper. So the authority for my content issuer might be com. Jwhh. Jim. Notekeeper. Issuer, so it truly is the authority we'll associate with my content material issuer. Now a key selection we want to make approximately our content material provider is the visibility to other packages and you want to be express about this choice due to the fact the visibility defaults to distinct values relying on the version of Android you're on. On older versions of Android, content material providers default to being seen to different apps. On more moderen versions of Android, they default to being now not visible to different apps. So what we need to do is set the exported value for our content company. We say is exported is true, then our content material issuer is seen to other packages. If exported is fake, our content issuer is not visible to other programs. So now in our next clip, we'll create a content issuer for our software.

Adding a Content Provider to Your App


Here we're in Android Studio and we have our NoteKeeper mission open. Now this is the equal undertaking we have been working on all through this course series and the challenge is precisely the way we left it on the quit of the previous course. So what we want to do now is add a content material issuer to our project. So to do this, we'll visit our File menu, we will pick New, and then alternative we need is manner down there in the direction of the bottom, it's Other. So right here in Other, now we want Content Provider, so we're going to pick that. So now the first aspect we want to do is offer the name for our ContentProvider class and permit's call ours NoteKeeperprovider. Now we need to companion an expert with our company. Since our app's bundle name is com. Jwhh. Jim. Notekeeper, we will use that because the begin of the authority, after which we will add. Issuer to the quit of it. We need different apps with a view to use our company, so go away each exported and enabled checked. Exported method that our content material company is seen to different apps. Enabled sincerely means that the machine can begin our content provider if it desires to. Alright, so it really is all of the statistics we want to provide. So permit's go in advance and click Finish, and as you could see, it created our NoteKeeperProvider elegance. And due to the fact that our elegance implements a content material provider, it extends the entry framework magnificence, ContentProvider. Alright, now with that, we've got our ContentProvider elegance in region. In our subsequent clip, we're going to begin doing the implementation.

Implementing a Content Provider Over SQLite


As we mentioned, we create a content issuer, we need to typically put into effect that over top of SQLite and that's exactly what we are doing with our content material issuer. Now whilst we're developing our content material company, there may be some things we need to hold in thoughts. Remember that the content material provider need to encapsulate all database associated information. So the content provider is accountable to address the information of connecting as much as the database and it'll perform all database associated interactions. Now whilst we are imposing our content material issuer, the techniques that we disclose from our content issuer are very just like the techniques that are exposed by the SQLite API. For instance, while you want records from SQLite, you name the SQLite question technique. When we need to provide records from our content company, we enforce the content issuer's query approach and the content company question approach and the SQLite question method are very, very comparable. So in fashionable, what we'll do inner of our query method implementation for our content issuer is delegate the majority of the work all the way down to SQLite. To give us a higher understanding of the way all this works, in our next clip, we're going to begin implementing our content company's question technique.

Adding Simple Query Support to Your Provider


Here we're back in Android Studio. We're presently searching at our NoteKeeperprovider elegance. Remember that this class serves as our utility's content provider. What we need to do now's upload the code necessary to permit our content material provider to deal with a easy query. As we talked about, the content material provider is going to handle all the direct interplay with the database. So to engage with the database we'll, of path, need to connect with the database. So just as we did in our sports, we'll use our OpenHelper elegance to handle the details of connecting to the database. So the first component we're going to do is add a NoteKeeper open helper field to our content provider and we will call it mDbOpenHelper. That gives us our open helper field. Let's scroll down now to our onCreate method. So right here we are at the onCreate method. So the first issue we're going to do is simply take away this remark right here. Now right here within the onCreate is in which we need to move ahead and create that instance of our open helper. Now as you do not forget, whilst we're using our open helper with sports, we created the instance of the OpenHelper and the sports onCreate method. We're going to do the equal element for our content material issuer. So we will say mDbOpenHelper = new NoteKeeperOpenHelper. Now remember the fact that our open helper constructor accepts a context reference. Now in contrast to with an pastime, we can not pass a content company's disk reference as a context. Instead, we get entry to the context for a content material issuer via calling its getContext technique. With that, now our open helper is prepared, but we do have one extra trouble to cope with earlier than we leave the onCreate approach. Now this is the onCreate technique presently returns fake. The onCreate go back cost shows whether or not the content provider became efficiently created. We need to indicate that our content provider has been effectively created. So rather than returning false, we're going to return authentic. And now that takes of making our content company. Next thing we want to look at is the way to enforce a simple query. So when facts is asked from our content issuer, that request is going to be treated with the aid of our question approach. You can see our query method here just beneath onCreate, and if you check the parameters, observe that most of them are very much like parameters we handed at the SQLite query approach. Let's bypass over the first parameter for now. We'll come lower back to that one a touch bit later inside the direction. But now word the second one parameter, the only named projection. That one is a String array. Now as you recollect, a String array is how we skip the listing of columns to the SQLite question method. Well that is exactly what the projection parameter is for, it receives the array of column names that we want to return. Now the subsequent  parameters, selection and selectionArgs, those include our selection standards. And then in the end, the closing parameter consists of the sortOrder. Notice additionally that the content issuer query technique includes a Cursor and that's the same form of go back by using a SQLite question approach. So in the end, what all this means is that the majority of the paintings of our content issuer's question approach can be delegated down to the SQLite question technique. So the primary aspect we're going to do is take away the code this is presently interior of the query approach. Since our question method returns a cursor, permit's claim a nearby variable call Cursor of type cursor and we're going to initialize it to null, after which we're going to simply cross beforehand and return lower back that cursor variable from our question technique. Now with the intention to question our database, we're going to need a SQLite database reference. So permit's call our OpenHelper's getReadableDatabase approach and we're going to assign it to a local variable. So that offers us a connection with a SQLiteDatabase, our local variable db. So now let's call our db. Question technique and let's assign its result lower back to our cursor variable. And from this point, we're simply querying the database as we generally could. Since we are starting out via having our content material provider queries all query our route event desk in our database, the first aspect we will bypass to the database question approach is the desk named steady from our CourseInfoEntry elegance. The rest of the parameters that we exceeded on the database query approach will all come from parameters handed to our content material company's query approach. So for the listing of columns, we're going to bypass inside the projection parameter, then for the selection standards, we're going to skip in selection and selectionargs, we're going to bypass null for subsequent two parameters, and then we'll pass in sortOrder because the final parameter. And with that, our content material provider can now manage queries. Return back a cursor from our CourseInfo table containing the list of request to columns. The cursor will contain simplest those rows that meet the beyond selection criteria and the rows can be ordered within the asked order. So we're off to a first rate start, but now the question is, how do you problem a query to this content material company and that is what we're going to study in out next clip.

Requesting Data from a Content Provider


Now we are equipped to start soliciting for facts from our content provider. Requesting records from the content provider may be very just like querying SQLite without delay, like we are going to comply with all of the equal fashionable suggestions. So one of the things we need to genuinely recall, so we in no way need to issue those queries directly from our utility's predominant thread. Instead, we are going to hold to use the CursorLoader elegance because remember that the CursorLoader elegance knows a way to run queries on a background thread for us and it is aware of the way to cooperate with a lifecycle of our sports. And we're going to continue to initiate the query procedure using a LoaderManager due to the fact understand that the LoaderManager knows a way to coordinate what is going on within the loader with the lifecycle of our activity. Now whilst using the CursorLoader elegance with a content company, our activity honestly receives a bit bit easier because the CursorLoader magnificence sincerely knows the way to question content providers. So what that means is that we'll not need to overload the loading background technique. Instead, all we ought to do is construct the CursorLoader and pass inside the identifier for our content material provider. And then the CursorLoader will cope with the information of finding the content issuer and issuing the query. The manner we perceive our content material companies is with the aid of using something known as a common resource identifier, and more usually what is known as a URI. And URIs look very similar to URLs. URIs begin with a scheme and in the case of content vendors, that scheme is content. So our URI will begin with content material:// and then it desires to encompass the authority of the content provider. So in our case, our URIs will be content material:// then com. Jwhh. Jim. Notekeeper. Provider, and then building a CursorLoader with that identifier allows it to find our content issuer. So now in our next clip, allow's alter our application, begin the usage of facts returned from the content issuer.

Post a Comment

0 Comments