android jam - contentproviders - udacity lesson 4b
TRANSCRIPT
Overview
● Sponsor Message - Novoda● Retro
○ Lesson 4b ContentProviders● Looking forward
○ Lesson 4c Loaders
● Stickers + Booklets● Participant Survey● Next Tue 7-8pm SpacePortX
Prime Directive
‘Regardless of where we are up to, we understand and truly believe that everyone did the best job he or she could, given his or her skills and abilities, the personal time available, and the situation at hand.’
Difficulty Check● Who completed the class?
● How long did it take?
● Hands up 1-10 difficulty of the weeks lesson
● Any specific areas of the lesson that require particular focus?
Retro lesson 4b Content Providers
What we learnt last week● Content Provider API● Sharing Data● Uri Matching● Efficiency with bulk inserts
Why Content Providers ● Share data across process
(one app to another)
○ Security○ Standard API○ Ease of use
● Share data within app (one component to another)
○ Framework considers all data
to come from ContentProdviders
○ Abstract underlying datasource○ Separates UI code from data layers
Bonus Lesson
https://www.udacity.com/course/ud258
UserDictionaryContentProvider
Updating WeatherContract
● Agreement between data model & UI on how data is accessed
● Define Uri’s for how to access data ● Content Authority (package name)● Base Uri (content provider base)● Path (table name)● MimeType
UriMatchers
● Mapping from Uri to Integer● Allows a way to use the Uri as a key● Can then be used in switch statement● * is String● # is Number
● http://developer.android.com/reference/android/content/UriMatcher.html
UriMatcher code example public String getType(Uri url) {
int match = sURIMatcher.match(url);
switch (match) {
case PEOPLE:
return "vnd.android.cursor.dir/person";
case PEOPLE_ID:
return "vnd.android.cursor.item/person";
... snip ...
return "vnd.android.cursor.dir/snail-mail";
case PEOPLE_ADDRESS_ID:
return "vnd.android.cursor.item/snail-mail";
default:
return null;
}
}
public String getType(Uri url) {
List pathSegments = url.getPathSegments();
if (pathSegments.size() >= 2) {
if ("people".equals(pathSegments.get(1))) {
if (pathSegments.size() == 2) {
return "vnd.android.cursor.dir/person";
} else if (pathSegments.size() == 3) {
return "vnd.android.cursor.item/person";
... snip ...
return "vnd.android.cursor.dir/snail-mail";
} else if (pathSegments.size() == 3) {
return "vnd.android.cursor.item/snail-mail";
}
}
}
return null;
}
● Register ContentProvider in AndroidManifest● ContentResolver talks to ContentProvider● Uses your Authority to locate Provider● Returns Cursor’s to manipulate● ProTip: use Gradle to create 1 constant for authority
string
ContentResolver
Content Provider
● Uses WeatherContract● Uses WeatherDbHelper● Uses UriMatcher● query(), insert(), delete(), update()● getType
○ type item or type directory○ returns content type (Mime type)
● setNotificationUri() registers ContentObserver○ notify UI when content changes (insert, update)
Data change notifications
● notifyChange() on ContentResolver● Notifying the base notifies all descendents● Be careful which uri you choose to watch● Loaders take advantage of this mechanism
Efficient Inserts
● ContentProvider.bulkInsert● Much more efficient than multiple single
imports● beginTransaction() & endTransaction()● setTransactionSuccessful() or insert is rolled
back
Let someone else do it - Libraries
https://github.com/novoda/sqlite-provider
- Convention Uri queries- No need to create DatabaseHelper- Create database using SQL files in /assets/- Just create the contract class and thats it
https://github.com/novoda/sqlite-analyzer
- Creates the contract class for you- Based on SQL files in /assets/ or existing database
What went well
● Tests for ContentProvider given to sanity check against
● The secondary lesson was great at explaining ContentProviders (https://www.
udacity.com/course/ud258)
What didn’t go so well
● Did anyone actually use or run the tests? ( I didn’t bother)
● Talking about Joins - not android necessity
● Don’t close your database! (instructor notes)
● Queries on the UI thread
Next Lesson - Loaders
Learning Objectives:- Creating a Loader- Advantages & Disadvantages- CursorLoader- CursorAdapter- Projections- Leveraging the content provider
See you next week..
- Time: Tuesday 7pm - Place: SpacePort- Things needed: You + Questions +
Feedback- Can contact us if issues:
@blundell_apps / @android_mcr