SlideShare a Scribd company logo
Content Providers
Content Providers
A Content Provider is a main component that manages access to
a centralized repository of data.
The main purpose is to share data with other applications (then
communicates data in interprocess communication). It is used to
create a database for internal research application.
A Content Provider manages a SQLite DB.
There are default Content Providers in the system to access
common data:
● Dictionary
● Contacts
● SMS
● Calls
● ...
Content Provider
Content Providers
A Content Provider is accessed through a client object called
ContentResolver that enables interprocess Communication.
For access to any Content Provider the application must specify
its permissions in the Manifest file. Typically permissions are
divided into
● Reading permissions
● Writing permissions
When you ask permission in writing is not necessary to require
reading ones.
Ex. To request permission to read the user dictionary, you must
add permission request in the Manifest file and you can not
request access at runtime:
<uses-permission android:name="android.permission.READ_USER_DICTIONARY">
Access
Content Providers
To perform a query on a ContentProvider you can use
ContentResolver.query() method to get the Cursor to read:
// Queries the user dictionary and returns results
mCursor = getContentResolver().query(
mUri, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Selection criteria
mSelectionArgs, // Selection criteria
mSortOrder); // The sort order for the returned rows
Parameters:
● Uri: Table name
● Projection: String array to specify what columns to invlude in
the query result
● Selection: string that specifies selection criteria
● SelectionArgs: argument strings for selection
● SortOrder: order of the query result
Query
Content Providers
The table identifying Uri is composed by:
● Prefix: "content://“
● Authority: it identifies the Provider
● Path: it identifies the table using its name
Es. content://user_dictionary/words
To create the Uri, use utility class called Uri, Uri.Builder and
ContentUris that simplify the management of Uri and provide
access to a single row of the table with the static method
ContentUris.withAppendedId()
Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4);
Query - Uri
Content Providers
To construct a SQL query you must follow its syntax
String[] mSelectionArgs = {""};
mSearchString = mSearchWord.getText().toString();
if (TextUtils.isEmpty(mSearchString)) {
mSelectionClause = null;
mSelectionArgs[0] = "";
} else {
mSelectionClause = UserDictionary.Words.WORD + " = ?";
mSelectionArgs[0] = mSearchString;
}
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI,
mProjection,
mSelectionClause
mSelectionArgs,
MsortOrder);
SELECT _ID, word, locale FROM words WHERE word = <userinput> ORDER BY word ASC;
Query - Statement
Content Providers
The "?" in selectionClause and selectionArgs are useful to protect
the DB from unknowing operations like dropthe user would insert.
Ex.
String mSelectionClause = "var = " + mUserInput;
The user would insert "nothing: DROP TABLE *;". Instead the
following is used to avoid problema:
String mSelectionClause = "var = ?";
String[] selectionArgs = {""};
selectionArgs[0] = mUserInput;
Query - Statement
Content Providers
The ContentResolver.query() method returns a Cursor that
contains all the rows resulting from the query with the specified
columns from the projection. For this reason, you must iterate
over the lines and you can retrieve the value for each column.
if(mCursor!=null){
while(mCursor.moveToNext()){
mCursor.getBlob(mCursor.getColumnIndex(BLOB_COLUMN_NAME));
mCursor.getDouble(mCursor.getColumnIndex(DOUBLE_COLUMN_NAME));
mCursor.getFloat(mCursor.getColumnIndex(FLOAT_COLUMN_NAME));
mCursor.getInt(mCursor.getColumnIndex(INT_COLUMN_NAME));
mCursor.getLong(mCursor.getColumnIndex(LONG_COLUMN_NAME));
mCursor.getShort(mCursor.getColumnIndex(SHORT_COLUMN_NAME));
mCursor.getString(mCursor.getColumnIndex(STRING_COLUMN_NAME));
}
}else{
//...
}
Query - Cursor
Content Providers
ContentResolver.insert() method inserts a new rowin the provider
table and returns the created Uri.
content://user_dictionary/words/<id_value>
The insertion takes place via a contentValues object that contains
the values of the new row. You do not need to manage _ID
because it is handled automatically. To insert a null value use
ContentValues.putNull().
Uri mNewUri;
ContentValues mNewValues = new ContentValues();
mNewValues.put(UserDictionary.Words.APP_ID, "example.user");
mNewValues.put(UserDictionary.Words.LOCALE, "en_US");
mNewValues.put(UserDictionary.Words.WORD, "insert");
mNewValues.put(UserDictionary.Words.FREQUENCY, "100");
mNewUri = getContentResolver().insert(
UserDictionary.Word.CONTENT_URI,// the user dictionary content URI
mNewValues // the values to insert
);
String mSelectionClause = "var = ?";
String[] selectionArgs = {""};
selectionArgs[0] = mUserInput;
Insert
Content Providers
The method ContentResolver.update() updates the rows resulting
from the query with the values specified by contentValues.
ContentValues mUpdateValues = new ContentValues();
String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?";
String[] mSelectionArgs = {"en_%"};
int mRowsUpdated = 0;
mUpdateValues.putNull(UserDictionary.Words.LOCALE);
mRowsUpdated = getContentResolver().update(
UserDictionary.Words.CONTENT_URI,// the user dictionary content URI
mUpdateValues // the columns to update
mSelectionClause // the column to select on
mSelectionArgs // the value to compare to
);
Update
Content Providers
The method ContentResolver.delete() delete query resulting
rows.
String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?";
String[] mSelectionArgs = {"user"};
int mRowsDeleted = 0;
mRowsDeleted = getContentResolver().delete(
UserDictionary.Words.CONTENT_URI,// the user dictionary content URI
mSelectionClause // the column to select on
mSelectionArgs // the value to compare to
);
Delete
Content Providers
A ContentProvider is necessary only if:
● You need to share data with other applications
● You want to develop a custom search system for
autocomplete text
In all other cases a Database SQLite is sufficient to achieve the
purpose.
Creation - Analysis
Content Providers
To configure a Content Provider you must establish its Authority
and the Path of its tables.
● Authority: to avoid conflicts extend the application package to
make it unique
<provider
android:name=".ExampleProvider"
android:authorities="com.example.<appname>.provider" >
</provider>
● Path: to define tables path extend Authority:
com.example.<appname>.provider/table1
com.example.<appname>.provider/table2
...
Creation - Design
Content Providers
When a ContentProvider receives a Uri it must understand what
action to take. Use a UriMatcher that maps Uri to integers.
public class ExampleProvider extends ContentProvider {
private static final UriMatcher sUriMatcher;
static {
sUriMatcher.addURI("com.example.app.provider", "table3", 1);
sUriMatcher.addURI("com.example.app.provider", "table3/#", 2);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String
sortOrder) {
switch (sUriMatcher.match(uri)) {
// If the incoming URI was for all of table3
case 1:
if (TextUtils.isEmpty(sortOrder)) sortOrder = "_ID ASC";
break;
// If the incoming URI was for a single row
case 2:
selection = selection + "_ID = " + uri.getLastPathSegment();
break;
default:
// If the URI is not recognized, you should do some error handling here.
}
// call the code to actually do the query
}
}
Creation - Design
Content Providers
The abstract class ContentProvider must manage accesses to
data by calls from a ContentResolver from other processes
and/or applications. The methods to implement are:
● ContentProvider.query()
● ContentProvider.insert()
● ContentProvider.update()
● ContentProvider.delete()
● ContentProvider.getType()
● ContentProvider.onCreate()
These methods have the same signature as the ContentResolver
ones to simplify their use.
Creation - Implementation
Content Providers
Using a SQLite Database simply return the Cursor returned by
SQLiteDatabase.query()
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case 1:
queryBuilder.appendWhere(Contract._ID + "="
+ uri.getLastPathSegment());
break;
case 2:
break;
default:
throw new IllegalArgumentException("Unknown URI");
}
Cursor cursor = queryBuilder.query(database, projection, selection,
selectionArgs, groupBy, having, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
Creation – Implementation -
query()
Content Providers
@Override
public Uri insert(Uri uri, ContentValues values) {
int uriType = sURIMatcher.match(uri);
long id;
switch (uriType) {
case 1:
id = database.insert(TABLE_NAME, null, values);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(Contract.CONTENT_URI, id);
}
Creation – Implementation -
insert()
Content Providers
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int rowsDeleted = database.delete(TABLE_NAME, selection,
selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
Creation – Implementation -
delete()
Content Providers
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int rowsUpdated = 0;
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case 1:
rowsUpdated = database.update(TABLE_NAME, values, selection,
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsUpdated;
}
Creation – Implementation -
update()
Content Providers
@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
database = dbHelper.getWritableDatabase();
return database != null;
}
protected static final class DatabaseHelper extends SQLiteOpenHelper {
private static final String DBNAME = "db_name";
private static final String SQL_CREATE = "CREATE TABLE "
+ "main ( _ID INTEGER PRIMARY KEY, WORD TEXT, FREQUENCY INTEGER, LOCALE TEXT )";
public WeatherDatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
Creation – Implementation -
onCreate()
Content Providers
The method returns a string in MIME format that describes the
type of data returned by the Uri argument. In the case of the
SQLite Database, string is composed in this way:
● Type: vnd
● Subtype:
● Single row: android.cursor.item/
● Multiple rows: android.cursor.dir/
● Provider: vnd.<name>.<type>
@Override
public String getType(Uri uri) {
switch (sURIMatcher.match(uri)){
case 1:
return "vnd.android.cursor.dir/vnd.com.example.provider";
case 2:
return "vnd.android.cursor.item/vnd.com.example.provider";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
Creation – Implementation -
getType()
Content Providers
To allow other applications access to the Content Provider you
must specify required permissions in the Manifest file.
<provider
android:name=".ExampleProvider"
android:authorities="com.example.android.provider"
android:permission="com.example.android.provider.PERMISSION"
android:readPermission="com.example.android.provider.READ_PERMISSION"
android:writePermission="com.example.android.provider.WRITE_PERMISSION" >
</provider>
Creation - Permission

More Related Content

What's hot

Android content provider explained
Android content provider explainedAndroid content provider explained
Android content provider explained
Shady Selim
 
Lecture14Slides.ppt
Lecture14Slides.pptLecture14Slides.ppt
Lecture14Slides.pptVideoguy
 
CSCI6505 Project:Construct search engine using ML approach
CSCI6505 Project:Construct search engine using ML approachCSCI6505 Project:Construct search engine using ML approach
CSCI6505 Project:Construct search engine using ML approachbutest
 
Android and firebase database
Android and firebase databaseAndroid and firebase database
Android and firebase database
NILESH SAWARDEKAR
 
Ado.net
Ado.netAdo.net
Ado.net
dina1985vlr
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
Randy Connolly
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.netTarun Jain
 

What's hot (9)

Android content provider explained
Android content provider explainedAndroid content provider explained
Android content provider explained
 
Lecture14Slides.ppt
Lecture14Slides.pptLecture14Slides.ppt
Lecture14Slides.ppt
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
CSCI6505 Project:Construct search engine using ML approach
CSCI6505 Project:Construct search engine using ML approachCSCI6505 Project:Construct search engine using ML approach
CSCI6505 Project:Construct search engine using ML approach
 
Simple Data Binding
Simple Data BindingSimple Data Binding
Simple Data Binding
 
Android and firebase database
Android and firebase databaseAndroid and firebase database
Android and firebase database
 
Ado.net
Ado.netAdo.net
Ado.net
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
 

Similar to Android App Development - 10 Content providers

Android App Development 05 : Saving Data
Android App Development 05 : Saving DataAndroid App Development 05 : Saving Data
Android App Development 05 : Saving DataAnuchit Chalothorn
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
SQLite Opening .pptx
SQLite Opening .pptxSQLite Opening .pptx
SQLite Opening .pptx
ImranS18
 
Android101 - Content Providers
Android101 - Content ProvidersAndroid101 - Content Providers
Android101 - Content Providers
jromero1214
 
springdatajpa-up.pdf
springdatajpa-up.pdfspringdatajpa-up.pdf
springdatajpa-up.pdf
ssuser0562f1
 
Android Database
Android DatabaseAndroid Database
Android Database
Dr Karthikeyan Periasamy
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
DicodingEvent
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using Jersey
Payal Jain
 
Local Storage
Local StorageLocal Storage
Local Storage
Ivano Malavolta
 
Rx 101 Codemotion Milan 2015 - Tamir Dresher
Rx 101   Codemotion Milan 2015 - Tamir DresherRx 101   Codemotion Milan 2015 - Tamir Dresher
Rx 101 Codemotion Milan 2015 - Tamir Dresher
Tamir Dresher
 
Tamir Dresher - Reactive Extensions (Rx) 101
Tamir Dresher - Reactive Extensions (Rx) 101Tamir Dresher - Reactive Extensions (Rx) 101
Tamir Dresher - Reactive Extensions (Rx) 101
Codemotion
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
Emprovise
 
Cursor & Content Value.pdf
Cursor & Content Value.pdfCursor & Content Value.pdf
Cursor & Content Value.pdf
uttamrao7
 
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenTutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Sony Suci
 
NodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdf
ArthyR3
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
Ben Abdallah Helmi
 
Apache Lucene Basics
Apache Lucene BasicsApache Lucene Basics
Apache Lucene Basics
Anirudh Sharma
 

Similar to Android App Development - 10 Content providers (20)

Android App Development 05 : Saving Data
Android App Development 05 : Saving DataAndroid App Development 05 : Saving Data
Android App Development 05 : Saving Data
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
SQLite Opening .pptx
SQLite Opening .pptxSQLite Opening .pptx
SQLite Opening .pptx
 
Android101 - Content Providers
Android101 - Content ProvidersAndroid101 - Content Providers
Android101 - Content Providers
 
springdatajpa-up.pdf
springdatajpa-up.pdfspringdatajpa-up.pdf
springdatajpa-up.pdf
 
Android Database
Android DatabaseAndroid Database
Android Database
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using Jersey
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
 
Local Storage
Local StorageLocal Storage
Local Storage
 
Rx 101 Codemotion Milan 2015 - Tamir Dresher
Rx 101   Codemotion Milan 2015 - Tamir DresherRx 101   Codemotion Milan 2015 - Tamir Dresher
Rx 101 Codemotion Milan 2015 - Tamir Dresher
 
Tamir Dresher - Reactive Extensions (Rx) 101
Tamir Dresher - Reactive Extensions (Rx) 101Tamir Dresher - Reactive Extensions (Rx) 101
Tamir Dresher - Reactive Extensions (Rx) 101
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Cursor & Content Value.pdf
Cursor & Content Value.pdfCursor & Content Value.pdf
Cursor & Content Value.pdf
 
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenTutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
 
NodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdf
 
Deployment
DeploymentDeployment
Deployment
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
 
Rails notification
Rails notificationRails notification
Rails notification
 
Apache Lucene Basics
Apache Lucene BasicsApache Lucene Basics
Apache Lucene Basics
 

More from Diego Grancini

Android App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsAndroid App Development - 14 location, media and notifications
Android App Development - 14 location, media and notifications
Diego Grancini
 
Android App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgetsAndroid App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgets
Diego Grancini
 
Android App Development - 12 animations
Android App Development - 12 animationsAndroid App Development - 12 animations
Android App Development - 12 animations
Diego Grancini
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsAndroid App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Diego Grancini
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 Storage
Diego Grancini
 
Android App Development - 08 Services
Android App Development - 08 ServicesAndroid App Development - 08 Services
Android App Development - 08 Services
Diego Grancini
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
Diego Grancini
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
Diego Grancini
 
Android App Development - 05 Action bar
Android App Development - 05 Action barAndroid App Development - 05 Action bar
Android App Development - 05 Action bar
Diego Grancini
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
Diego Grancini
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 Resources
Diego Grancini
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
Diego Grancini
 
Android App Development - 01 Introduction
Android App Development - 01 IntroductionAndroid App Development - 01 Introduction
Android App Development - 01 Introduction
Diego Grancini
 

More from Diego Grancini (13)

Android App Development - 14 location, media and notifications
Android App Development - 14 location, media and notificationsAndroid App Development - 14 location, media and notifications
Android App Development - 14 location, media and notifications
 
Android App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgetsAndroid App Development - 13 Broadcast receivers and app widgets
Android App Development - 13 Broadcast receivers and app widgets
 
Android App Development - 12 animations
Android App Development - 12 animationsAndroid App Development - 12 animations
Android App Development - 12 animations
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsAndroid App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 Storage
 
Android App Development - 08 Services
Android App Development - 08 ServicesAndroid App Development - 08 Services
Android App Development - 08 Services
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
 
Android App Development - 05 Action bar
Android App Development - 05 Action barAndroid App Development - 05 Action bar
Android App Development - 05 Action bar
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 Resources
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Android App Development - 01 Introduction
Android App Development - 01 IntroductionAndroid App Development - 01 Introduction
Android App Development - 01 Introduction
 

Android App Development - 10 Content providers

  • 2. Content Providers A Content Provider is a main component that manages access to a centralized repository of data. The main purpose is to share data with other applications (then communicates data in interprocess communication). It is used to create a database for internal research application. A Content Provider manages a SQLite DB. There are default Content Providers in the system to access common data: ● Dictionary ● Contacts ● SMS ● Calls ● ... Content Provider
  • 3. Content Providers A Content Provider is accessed through a client object called ContentResolver that enables interprocess Communication. For access to any Content Provider the application must specify its permissions in the Manifest file. Typically permissions are divided into ● Reading permissions ● Writing permissions When you ask permission in writing is not necessary to require reading ones. Ex. To request permission to read the user dictionary, you must add permission request in the Manifest file and you can not request access at runtime: <uses-permission android:name="android.permission.READ_USER_DICTIONARY"> Access
  • 4. Content Providers To perform a query on a ContentProvider you can use ContentResolver.query() method to get the Cursor to read: // Queries the user dictionary and returns results mCursor = getContentResolver().query( mUri, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause // Selection criteria mSelectionArgs, // Selection criteria mSortOrder); // The sort order for the returned rows Parameters: ● Uri: Table name ● Projection: String array to specify what columns to invlude in the query result ● Selection: string that specifies selection criteria ● SelectionArgs: argument strings for selection ● SortOrder: order of the query result Query
  • 5. Content Providers The table identifying Uri is composed by: ● Prefix: "content://“ ● Authority: it identifies the Provider ● Path: it identifies the table using its name Es. content://user_dictionary/words To create the Uri, use utility class called Uri, Uri.Builder and ContentUris that simplify the management of Uri and provide access to a single row of the table with the static method ContentUris.withAppendedId() Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4); Query - Uri
  • 6. Content Providers To construct a SQL query you must follow its syntax String[] mSelectionArgs = {""}; mSearchString = mSearchWord.getText().toString(); if (TextUtils.isEmpty(mSearchString)) { mSelectionClause = null; mSelectionArgs[0] = ""; } else { mSelectionClause = UserDictionary.Words.WORD + " = ?"; mSelectionArgs[0] = mSearchString; } mCursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, mProjection, mSelectionClause mSelectionArgs, MsortOrder); SELECT _ID, word, locale FROM words WHERE word = <userinput> ORDER BY word ASC; Query - Statement
  • 7. Content Providers The "?" in selectionClause and selectionArgs are useful to protect the DB from unknowing operations like dropthe user would insert. Ex. String mSelectionClause = "var = " + mUserInput; The user would insert "nothing: DROP TABLE *;". Instead the following is used to avoid problema: String mSelectionClause = "var = ?"; String[] selectionArgs = {""}; selectionArgs[0] = mUserInput; Query - Statement
  • 8. Content Providers The ContentResolver.query() method returns a Cursor that contains all the rows resulting from the query with the specified columns from the projection. For this reason, you must iterate over the lines and you can retrieve the value for each column. if(mCursor!=null){ while(mCursor.moveToNext()){ mCursor.getBlob(mCursor.getColumnIndex(BLOB_COLUMN_NAME)); mCursor.getDouble(mCursor.getColumnIndex(DOUBLE_COLUMN_NAME)); mCursor.getFloat(mCursor.getColumnIndex(FLOAT_COLUMN_NAME)); mCursor.getInt(mCursor.getColumnIndex(INT_COLUMN_NAME)); mCursor.getLong(mCursor.getColumnIndex(LONG_COLUMN_NAME)); mCursor.getShort(mCursor.getColumnIndex(SHORT_COLUMN_NAME)); mCursor.getString(mCursor.getColumnIndex(STRING_COLUMN_NAME)); } }else{ //... } Query - Cursor
  • 9. Content Providers ContentResolver.insert() method inserts a new rowin the provider table and returns the created Uri. content://user_dictionary/words/<id_value> The insertion takes place via a contentValues object that contains the values of the new row. You do not need to manage _ID because it is handled automatically. To insert a null value use ContentValues.putNull(). Uri mNewUri; ContentValues mNewValues = new ContentValues(); mNewValues.put(UserDictionary.Words.APP_ID, "example.user"); mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); mNewValues.put(UserDictionary.Words.WORD, "insert"); mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); mNewUri = getContentResolver().insert( UserDictionary.Word.CONTENT_URI,// the user dictionary content URI mNewValues // the values to insert ); String mSelectionClause = "var = ?"; String[] selectionArgs = {""}; selectionArgs[0] = mUserInput; Insert
  • 10. Content Providers The method ContentResolver.update() updates the rows resulting from the query with the values specified by contentValues. ContentValues mUpdateValues = new ContentValues(); String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?"; String[] mSelectionArgs = {"en_%"}; int mRowsUpdated = 0; mUpdateValues.putNull(UserDictionary.Words.LOCALE); mRowsUpdated = getContentResolver().update( UserDictionary.Words.CONTENT_URI,// the user dictionary content URI mUpdateValues // the columns to update mSelectionClause // the column to select on mSelectionArgs // the value to compare to ); Update
  • 11. Content Providers The method ContentResolver.delete() delete query resulting rows. String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?"; String[] mSelectionArgs = {"user"}; int mRowsDeleted = 0; mRowsDeleted = getContentResolver().delete( UserDictionary.Words.CONTENT_URI,// the user dictionary content URI mSelectionClause // the column to select on mSelectionArgs // the value to compare to ); Delete
  • 12. Content Providers A ContentProvider is necessary only if: ● You need to share data with other applications ● You want to develop a custom search system for autocomplete text In all other cases a Database SQLite is sufficient to achieve the purpose. Creation - Analysis
  • 13. Content Providers To configure a Content Provider you must establish its Authority and the Path of its tables. ● Authority: to avoid conflicts extend the application package to make it unique <provider android:name=".ExampleProvider" android:authorities="com.example.<appname>.provider" > </provider> ● Path: to define tables path extend Authority: com.example.<appname>.provider/table1 com.example.<appname>.provider/table2 ... Creation - Design
  • 14. Content Providers When a ContentProvider receives a Uri it must understand what action to take. Use a UriMatcher that maps Uri to integers. public class ExampleProvider extends ContentProvider { private static final UriMatcher sUriMatcher; static { sUriMatcher.addURI("com.example.app.provider", "table3", 1); sUriMatcher.addURI("com.example.app.provider", "table3/#", 2); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (sUriMatcher.match(uri)) { // If the incoming URI was for all of table3 case 1: if (TextUtils.isEmpty(sortOrder)) sortOrder = "_ID ASC"; break; // If the incoming URI was for a single row case 2: selection = selection + "_ID = " + uri.getLastPathSegment(); break; default: // If the URI is not recognized, you should do some error handling here. } // call the code to actually do the query } } Creation - Design
  • 15. Content Providers The abstract class ContentProvider must manage accesses to data by calls from a ContentResolver from other processes and/or applications. The methods to implement are: ● ContentProvider.query() ● ContentProvider.insert() ● ContentProvider.update() ● ContentProvider.delete() ● ContentProvider.getType() ● ContentProvider.onCreate() These methods have the same signature as the ContentResolver ones to simplify their use. Creation - Implementation
  • 16. Content Providers Using a SQLite Database simply return the Cursor returned by SQLiteDatabase.query() @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder.setTables(TABLE); int uriType = sURIMatcher.match(uri); switch (uriType) { case 1: queryBuilder.appendWhere(Contract._ID + "=" + uri.getLastPathSegment()); break; case 2: break; default: throw new IllegalArgumentException("Unknown URI"); } Cursor cursor = queryBuilder.query(database, projection, selection, selectionArgs, groupBy, having, sortOrder); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; } Creation – Implementation - query()
  • 17. Content Providers @Override public Uri insert(Uri uri, ContentValues values) { int uriType = sURIMatcher.match(uri); long id; switch (uriType) { case 1: id = database.insert(TABLE_NAME, null, values); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return ContentUris.withAppendedId(Contract.CONTENT_URI, id); } Creation – Implementation - insert()
  • 18. Content Providers @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int rowsDeleted = database.delete(TABLE_NAME, selection, selectionArgs); getContext().getContentResolver().notifyChange(uri, null); return rowsDeleted; } Creation – Implementation - delete()
  • 19. Content Providers @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int rowsUpdated = 0; int uriType = sURIMatcher.match(uri); switch (uriType) { case 1: rowsUpdated = database.update(TABLE_NAME, values, selection, selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return rowsUpdated; } Creation – Implementation - update()
  • 20. Content Providers @Override public boolean onCreate() { dbHelper = new DatabaseHelper(getContext()); database = dbHelper.getWritableDatabase(); return database != null; } protected static final class DatabaseHelper extends SQLiteOpenHelper { private static final String DBNAME = "db_name"; private static final String SQL_CREATE = "CREATE TABLE " + "main ( _ID INTEGER PRIMARY KEY, WORD TEXT, FREQUENCY INTEGER, LOCALE TEXT )"; public WeatherDatabaseHelper(Context context) { super(context, DBNAME, null, 1); } public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } Creation – Implementation - onCreate()
  • 21. Content Providers The method returns a string in MIME format that describes the type of data returned by the Uri argument. In the case of the SQLite Database, string is composed in this way: ● Type: vnd ● Subtype: ● Single row: android.cursor.item/ ● Multiple rows: android.cursor.dir/ ● Provider: vnd.<name>.<type> @Override public String getType(Uri uri) { switch (sURIMatcher.match(uri)){ case 1: return "vnd.android.cursor.dir/vnd.com.example.provider"; case 2: return "vnd.android.cursor.item/vnd.com.example.provider"; default: throw new IllegalArgumentException("Unsupported URI: " + uri); } } Creation – Implementation - getType()
  • 22. Content Providers To allow other applications access to the Content Provider you must specify required permissions in the Manifest file. <provider android:name=".ExampleProvider" android:authorities="com.example.android.provider" android:permission="com.example.android.provider.PERMISSION" android:readPermission="com.example.android.provider.READ_PERMISSION" android:writePermission="com.example.android.provider.WRITE_PERMISSION" > </provider> Creation - Permission