Android Content Providers
Android Content Providers
Android Content Providers
Providers
Using Media Data
Media Music
Manager Player
playlists, songs, artists…
2
Using Media Data
Media Music
Manager Player
3
Client-side: content resolver
• Implemented
Media by Android:
Music
getContentResolver()
Manager Player
http://developer.android.com/reference/android/content/
ContentResolver.html
4
Service-side: content provider
a few functions query
framework
Android’s
implemented by the
content provider’s insert
owner (Media) update
5
Built-in content providers
• Contacts
• Media
• Calendar
• User Dictionary
• …
6
Simple example:
user dictionary (built-in)
• Stores the spellings of non-standard words that the
user wants to keepI
7
Query from another app
get the
ContentResolver
object
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // 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
URI: an identifier
to locate the user
dictionary
8
Locating resources using
Content URIs
•
•
scheme - always "content"
authority - name of entire provider
} used by Android to
identify a content provider
}
• path (optional) used by the content
• data type path provider to identify
• instance identifier internal objects
Path
}
content://user_dictionary/words/5
}
}
scheme authority
must be “content” *For non-built-in apps: com.example.<appname>.provider
9
Uri class
• Example:
Uri.parse("content://contacts/people");
10
Creating a content provider
• Why?
11
Creating a content provider
• Manifest declaration
• Implementation
• Permissions
http://developer.android.com/guide/topics/providers/content-provider-creating.html
12
Design URI-to-data
mapping
• authority: user_dictionary
• path:
• /words: all words
• /words/<id>: a specific word
• Use UriMatcher
http://androidxref.com/4.4.3_r1.1/xref/packages/providers/UserDictionaryProvider/src/com/android/providers/
userdictionary/UserDictionaryProvider.java
13
Declare in manifest
A content provider is an app component
http://developer.android.com/guide/topics/manifest/provider-element.html
</application>
…
<!-- The Content Provider is declared -->
<provider android:name="UserDictionaryProvider"
android:authorities=“user_dictionary"
android:syncable="false"
android:multiprocess="false"
android:exported="true"
android:readPermission="android.permission.READ_USER_DICTIONARY"
android:writePermission="android.permission.WRITE_USER_DICTIONARY" />
</application>
http://androidxref.com/4.4.3_r1.1/xref/packages/providers/UserDictionaryProvider/
AndroidManifest.xml
14
Implementation
15
Implementing query
16
Match Uri
content://user_dictionary/words/1
switch (sUriMatcher.match(uri)) {
case WORDS:
path segments: [“words”,
qb.setTables(USERDICT_TABLE_NAME);
“1”]
qb.setProjectionMap(sDictProjectionMap);
break;
case WORD_ID:
qb.setTables(USERDICT_TABLE_NAME);
qb.setProjectionMap(sDictProjectionMap);
qb.appendWhere(
"_id" + "=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException(
"Unknown URI " + uri);
}
17
Query DB, then return cursor
// If no sort order aisContentObserver
Register specified use the default
String orderBy;
if (TextUtils.isEmpty(sortOrder)) {
orderBy = Words.DEFAULT_SORT_ORDER;
Allow Android’s “CursorLoader”
} else {
mechanism to automatically re-fetch data
orderBy = sortOrder;
}
return c;
18
Implementing insert
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
// Validate the requested uri
if (sUriMatcher.match(uri) != WORDS) {
throw new IllegalArgumentException("Unknown URI " + uri);
} return the inserted URI
ContentValues values;
… // sanitize initialValues and store to values
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowId = db.insert(
USERDICT_TABLE_NAME, Words.WORD, values);
if (rowId > 0) {
Uri wordUri = ContentUris.withAppendedId(
UserDictionary.Words.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(
wordUri, null);
mBackupManager.dataChanged();
return wordUri;
} notify content observers
throw new SQLException("Failed to insert row into " + uri);
}
19
Permissions in manifest
http://developer.android.com/guide/topics/manifest/provider-element.html
exported: enable to share with
</application>
…
other apps
<!-- The Content Provider is declared -->
<provider android:name="UserDictionaryProvider"
android:authorities=“user_dictionary"
android:syncable="false"
android:multiprocess="false"
android:exported="true"
android:readPermission="android.permission.READ_USER_DICTIONARY"
android:writePermission="android.permission.WRITE_USER_DICTIONARY" />
</application>
http://androidxref.com/4.4.3_r1.1/xref/packages/providers/UserDictionaryProvider/
AndroidManifest.xml
20
Permissions in manifest
http://developer.android.com/guide/topics/manifest/provider-element.html
</application>
… read/write permissions
<!-- The Content Provider is declared -->
<provider android:name="UserDictionaryProvider"
android:authorities=“user_dictionary"
android:syncable="false"
android:multiprocess="false"
android:exported="true"
android:readPermission="android.permission.READ_USER_DICTIONARY"
android:writePermission="android.permission.WRITE_USER_DICTIONARY" />
</application>
http://androidxref.com/4.4.3_r1.1/xref/packages/providers/UserDictionaryProvider/
AndroidManifest.xml
21
Permissions on whole
content provider
• Single read-write provider-level permission
22
Path-level permissions
23
Temporary permissions
24
Example: email attachments
a l ly:
m
Nor ccess
an’t a
c
attachments
25
Example: email attachments
user clicks
a r i l y
m p o r
a n t e
c c e s s
ac
attachments
i ng i n t e nt
s
invoke u
27
Example: email attachments
Invoke using intent
28
Example: email attachments
Enable in manifest
Example: email attachments
http://androidxref.com/4.4.3_r1.1/xref/packages/apps/Email/src/com/android/email/provider/AttachmentProvider.java
30
Android’s built-in file-backed
content provider class
• FileProvider: a subclass of ContentProvider
• Implemented by Android
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
31