http://developerstudyjams.com/
Week 6 - Retrospectively looking at Udacity Lesson 4b ContentProviders
Google Developers Study Jams is a free series of global, community-run, in-person study groups. The first Study Jams series is presented in partnership with Udacity and will follow their Android Fundamentals curriculum. Google Developer Group facilitators certified in the course will lead the Study Jams that meet once weekly for 8 - 9 weeks.
Promote using hashtag #devstudyjam
4. 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.’
5. 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?
6. Retro lesson 4b Content Providers
What we learnt last week
● Content Provider API
● Sharing Data
● Uri Matching
● Efficiency with bulk inserts
7. 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
13. 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
14. 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
15. 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;
}
16. ● 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
17. 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)
18. 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
20. 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
22. 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)
24. 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
30. See you next week..
- Time: Tuesday 7pm
- Place: SpacePort
- Things needed: You + Questions +
Feedback
- Can contact us if issues:
@blundell_apps / @android_mcr