MobAppDev (Fall 2013): Content Providers

27
Mobile Application Development Content Providers Vladimir Kulyukin www.vkedco.blogspot.com www.vkedco.blogspot.com

Transcript of MobAppDev (Fall 2013): Content Providers

Page 1: MobAppDev (Fall 2013): Content Providers

Mobile Application Development

Content Providers

Vladimir Kulyukin

www.vkedco.blogspot.comwww.vkedco.blogspot.com

Page 2: MobAppDev (Fall 2013): Content Providers

Outline

● Representational State Transfers● Content Providers

Authority Registration Table Data Model ContentProvider Resolution Content URIs

Page 3: MobAppDev (Fall 2013): Content Providers

Representational State Transfers

Page 4: MobAppDev (Fall 2013): Content Providers

Representational State Transfer: REST

● REST is a style of software architecture for distributed hypermedia systems such as WWW

● REST architecture consists of clients and servers

● Requests from clients and responses from servers are built around transfers of representations of resources

Page 5: MobAppDev (Fall 2013): Content Providers

Representational State Transfer: REST

● A resource is any coherent and meaningful concept that may addressed

● A representation is a sequence of bits that captures the state of a resource

● Architectures that comply with REST standards are called RESTful

Page 6: MobAppDev (Fall 2013): Content Providers

Content Providers

Page 7: MobAppDev (Fall 2013): Content Providers

What is a Content Provider?

● A Content Provider is a wrapper around data source

● A SQLite database is an example of a data source that can be exposed as a content provider

● A Content Provider is a REST-like abstraction

Page 8: MobAppDev (Fall 2013): Content Providers

When to Use Content Providers

● Only if you need to share some data among different applications

● For internal data access, you can use: Preferences Files SQLite Networks

Page 9: MobAppDev (Fall 2013): Content Providers

Android's Built-in Content Providers

● Several built-in providers are Browser, CallLog, Contacts, MediaStore, Settings

● These top-level providers are SQLite databases encapsulated as content providers

● Some of these databases have multiple tables: for example, Contacts has People, Phones, Photos, and Groups tables

● List of providers varies for different versions of Android

Page 10: MobAppDev (Fall 2013): Content Providers

Content Providers as Web Sites

● Each content provider registers itself on a device as a web site

● The registration is done with a string called authority

● Authority is similar to a domain name● Authority string is the basis of a set of URIs

the content provider offers

Page 11: MobAppDev (Fall 2013): Content Providers

Authority Registration

Page 12: MobAppDev (Fall 2013): Content Providers

Authority Registration ● Authority must be registered in AndroidManifest.xml● This is how ContentProviders become known (registered)

with the Android ecosystem

<provider

android:name=".ScienceContentProvider"

android:authorities="org.vkedco.provider.science">

</provider>

Page 13: MobAppDev (Fall 2013): Content Providers

Authority Registration

<provider

android:name=".ScienceContentProvider"

android:authorities="org.vkedco.provider.science">

</provider>

● In the above registration, the provider serves the URIs with the prefix

content://org.vkedco.provider.science

Page 14: MobAppDev (Fall 2013): Content Providers

Table Data Model

● Content providers structure data like relational database tables

● Rows can be viewed as records or instances of some data type (e.g. a Mathematician or a Book)

● Columns represent member variables and their values in corresponding instances

● It is irrelevant to outside clients whether the actual data reside in a relation database

Page 15: MobAppDev (Fall 2013): Content Providers

Content Resolvers● When a component must access the data in a

ContentProvider it must use the ContentResolver object in the Context of its application

● The ContentResolver object allows the component to communicate with the ContentProvider as a client

● The ContentResolver is the intermediary between the client component and the ContentProvider

Page 16: MobAppDev (Fall 2013): Content Providers

ContentProvider Resolution

Page 17: MobAppDev (Fall 2013): Content Providers

Content Resolvers● ContentResolver objects offers clients the same

methods as the corresponding ContentProvider object

● ContentResolver objects provide the create, retrieve, update and delete (aka CRUD) methods for persistent data storage

● Once paired, ContentProvider and ContentResolver objects handle inter-process communication

Page 18: MobAppDev (Fall 2013): Content Providers

ContentProvider Resolution

● In the ContentProvider model, data are manipulated only through URIs

● URIs are parsed by ContentResolvers● Upon parsing the URI, a given

ContentResolver object compares the authority extracted from the URI to the table of ContentProviders registered in the Android ecosystem

Page 19: MobAppDev (Fall 2013): Content Providers

Wrapping SQLite DBs with ContentProviders

Page 20: MobAppDev (Fall 2013): Content Providers

Problem

Build a content provider that wraps around an SQLite database with images of poets (Rumi, Hafiz) and book covers (Essential Rumi, Illuminated Rumi, Year with Rumi, Year with Hafiz, & The Gift). Build two clients for the content provider: the first one retrieves images of poets and book covers; the second one retrieves images of book covers given ISBNs.

Page 21: MobAppDev (Fall 2013): Content Providers

Implementation Steps

● Design and populate SQLite databases (we have accomplished these steps in the previous projects/presentations)

● Implement a content provider● Register the content provider on the

Android ecosystem● Implement the clients● Test everything

Page 22: MobAppDev (Fall 2013): Content Providers

Content URIs● Root URI: content://org.vkedco.mobappdev.content_providers.books ● Retrieval of book title records will use the following URIs● Examples:

– content://org.vkedco.mobappdev.content_providers.books/id

– content://org.vkedco.mobappdev.content_provides.books/id/#

● Query URIs for clients:– content://org.vkedco.mobappdev.content_providers.books/book_title/query?title=essential_rumi

– content://org.vkedco.mobappdev.content_providers.books/book_title/query?title=essential_rumi&author=rumi

● Content Provider (BookContentProvider.java) implements and services these URIs after it is registered on Android

Page 23: MobAppDev (Fall 2013): Content Providers

Content Provider Registration<application>

<activity

android:name=".MainActivity"

android:label="@string/title_activity_main" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<provider

android:name=".BookContentProvider_00001"

android:authorities="org.vkedco.mobappdev.content_providers.books"

android:process=":org.vkedco.mobappdev.content_providers.books">

</provider>

</application>

source code is here

Page 24: MobAppDev (Fall 2013): Content Providers

Implementing ContentProvider

● The content provider is implemented in BookContentProvider.java

● It publishes all URIs and MIMEs and matching codes

● It also implements the query method that returns Cursor objects

Page 25: MobAppDev (Fall 2013): Content Providers

Implementing 1st Client

source code is here

Page 26: MobAppDev (Fall 2013): Content Providers

Implementing 2nd Client

Page 27: MobAppDev (Fall 2013): Content Providers

References ● http://developer.android.com/guide/topics/providers/content-providers.html● http://tools.ietf.org/html/rfc2046● http://www.iana.org/assignments/media-types● http://vkedco.blogspot.com/2012/11/mobappdev-contentproviders-as-web-sites.html