Content URIs & URI Processing                 on Android                      Vladimir Kulyukinhttp://www.youtube.com/vked...
Outline   ●       Content URIs   ●       URI Processing on Android          – URI Elements          – URI Matching Codes &...
Uniform Resource Indentifiershttp://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
Uniform Resource Identifier (URI)    ●        A URI is a string of characters that identifies a resource    ●        Unifo...
Common Abbreviations: URI, URL, URN    ●        URL – Uniform Resource Locator    ●        URN – Uniform Resource Name    ...
URI Examples    ●        ftp://ftp.org.sample/rfc/rfc1001.txt    ●        gopher://univ.edu/classes/cs    ●        http://...
Hierarchical & Relative URIs    ●        Absolute URI refers to a resource regardless of        the context in which it is...
Generic URI Syntax  <scheme>://<authority><path>?<query>http://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
Example 01 in Color    ●        ftp://org.vkedco.mobappdev/resources/README.txt    ●        SCHEME:             ftp    ●  ...
Example 02       content://org.vkedco.mobappdev.content_providers.books/book_title/id   ●       SCHEME: content   ●       ...
Example 03        content://org.vkedco.mobappdev.content_providers.books/book_title/id/10    ●        SCHEME: content    ●...
Example 04        content://org.vkedco.mobappdev.content_providers.books/book_title?title=essential_rumi    ●        SCHEM...
Example 05        http://vkedco_content_server:8080/book_title?title=essential_rumi&author=rumi    ●        SCHEME: http  ...
URI Processing & Design                        source code is herehttp://www.youtube.com/vkedco   http://www.vkedco.blogsp...
URI Access           <scheme>://<authority><path>?<query>       ●           Uri.getScheme()       ●           Uri.getAutho...
Getting URI Elements       String getUriInfo(Uri uri) {           StringBuilder sb = new StringBuilder(""); sb.append("URI...
URI Design Criteria    ●        URI should be a sequence of characters and not        always a sequence of octets (in CS, ...
URI Processing Steps    ●        Choose the scheme, e.g., content, http, etc.    ●        Design an authority    ●        ...
Sample Problem      Suppose we want to create a resource that allows      its clients to retrieve books, book titles, and ...
URIs: Scheme, Authority, Paths    ●        Scheme: content    ●        Authority: org.vkedco.mobappdev.content_providers.b...
URIs: Matching Codes    ●        BOOK_ID_ALL = 0    ●        BOOK_ID_SIGNLE = 1    ●        BOOK_TITLE_ALL = 2    ●       ...
Adding URIs to UriMatcher     static final String AUTHORITY = "org.vkedco.content_providers.books";     UriMatcher mUriMat...
Adding URIs to UriMatcher     static final String AUTHORITY = "org.vkedco.mobappdev.content_providers.books";     UriMatch...
URI Matching Codes and Mimes          // uri mime types          static final String BOOK_TITLE_ID_ALL_MIME               ...
Basic URI Matching Pattern      void matchUri(Uri uri) {          int uri_match_code = mUriMatcher.match(uri);          sw...
References    ●        http://developer.android.com/guide/topics/providers/content-providers.html    ●        http://tools...
Feedback        Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
Upcoming SlideShare
Loading in...5
×

Content URIs & URI Processing on Android

2,549

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,549
On Slideshare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Content URIs & URI Processing on Android"

  1. 1. Content URIs & URI Processing on Android Vladimir Kulyukinhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Content URIs ● URI Processing on Android – URI Elements – URI Matching Codes & Mimes – Matching URIshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  3. 3. Uniform Resource Indentifiershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  4. 4. Uniform Resource Identifier (URI) ● A URI is a string of characters that identifies a resource ● Uniform – means that different types of resource identifiers can be used in the same context even when their access mechanisms are different ● Resource – means anything that has an identity (e.g., a document, an image, a service) ● Identifier – means that it is a reference to a resource that with an identity (e.g., a sequence of characters with specific syntactic restrictions)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  5. 5. Common Abbreviations: URI, URL, URN ● URL – Uniform Resource Locator ● URN – Uniform Resource Name ● Although the terms URI, URL, & URN are frequently used interchangeably, URL and URN are technically subsets of URI ● URL is the subset of URIs that identify resources via their primary access mechanisms (http, ftp, gopher, etc.) ● URN is the subset of URIs that are required to remain globally unique and persistent (even when the resources cease to exist)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  6. 6. URI Examples ● ftp://ftp.org.sample/rfc/rfc1001.txt ● gopher://univ.edu/classes/cs ● http://some_server:8080/public/home.html ● telnet://my_company.net/ ● mailto:user12345@gmail.com ● news:comp.infosystems.www.servers.unixhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  7. 7. Hierarchical & Relative URIs ● Absolute URI refers to a resource regardless of the context in which it is used ● Relative URI refers to a resource within a specific namespace (e.g., absolute vs. relative file paths) ● Some URI schemes support hierarchical naming systems ● URI naming hierarchies use the “/” delimitershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  8. 8. Generic URI Syntax <scheme>://<authority><path>?<query>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  9. 9. Example 01 in Color ● ftp://org.vkedco.mobappdev/resources/README.txt ● SCHEME: ftp ● AUTHORITY: org.vkedco.mobappdev ● PATH: /resources/README.txt ● PATH_SEGMENTS: /resources; README.txt ● PORT: -1 // it is not specified so defaults to -1 ● QUERY: null // not specified so defaults to nullhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  10. 10. Example 02 content://org.vkedco.mobappdev.content_providers.books/book_title/id ● SCHEME: content ● AUTHORITY: org.vkedco.mobappdev.content_providers.books ● PATH: /book_title/id ● PATH_SEGMENTS: book_title; id ● PORT: -1 // not specified, defaults to -1 ● QUERY: null // not specified, defaults to nullhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  11. 11. Example 03 content://org.vkedco.mobappdev.content_providers.books/book_title/id/10 ● SCHEME: content ● AUTHORITY: org.vkedco.mobappdev.content_providers.books ● PATH: /book_title/id/10 ● PATH_SEGMENTS: book_title; id ; 10 ● PORT: -1 ● QUERY: nullhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  12. 12. Example 04 content://org.vkedco.mobappdev.content_providers.books/book_title?title=essential_rumi ● SCHEME: content ● AUTHORITY: org.vkedco.mobappdev.content_providers.books ● PATH: /book_title ● PATH_SEGMENTS: book_title ● PORT: -1 ● QUERY: title=essential_rumihttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  13. 13. Example 05 http://vkedco_content_server:8080/book_title?title=essential_rumi&author=rumi ● SCHEME: http ● AUTHORITY: vkedco_content_server:8080 ● PATH: /book_title ● PATH_SEGMENTS: book_title ● PORT: 8080 ● QUERY: title=essential_rumi&author=rumihttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  14. 14. URI Processing & Design source code is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  15. 15. URI Access <scheme>://<authority><path>?<query> ● Uri.getScheme() ● Uri.getAuthority() ● Uri.getPath() ● Uri.getPathSegments() ● Uri.getPort() ● Uri.getQuery() ● Uri.getQueryParameter()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  16. 16. Getting URI Elements String getUriInfo(Uri uri) { StringBuilder sb = new StringBuilder(""); sb.append("URI: " + uri.toString() + "n"); sb.append("SCHEME: " + uri.getScheme() + "n"); sb.append("AUTHORITY: " + uri.getAuthority() + "n"); sb.append("PATH=" + uri.getPath() + "n"); sb.append("PATH SEGMENTS:n"); List<String> pathSegments = uri.getPathSegments(); for(int i = 0; i < pathSegments.size(); i++) { sb.append("path_sgmnt[" + i + "]=" + pathSegments.get(i) + "n"); } sb.append("PORT: " + uri.getPort() + "n"); sb.append("QUERY: " + uri.getQuery() + "n"); sb.append("TITLE_PARAM: " + uri.getQueryParameter("title") + "n"); sb.append("AUTHOR_PARAM: " + uri.getQueryParameter("author") + "n"); return sb.toString(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  17. 17. URI Design Criteria ● URI should be a sequence of characters and not always a sequence of octets (in CS, octet is a sequence of 8 bits, aka byte) ● URI may be transcribed from a non-network source and should consist of characters sensitive to constraints imposed by keyboards and locales ● URI should be remembered by people (should have meaningful components)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  18. 18. URI Processing Steps ● Choose the scheme, e.g., content, http, etc. ● Design an authority ● Design MIMEs (Multipurpose Internet Mail Extensions) or use existing ones ● Design match codes to manage uri matching ● Use UriMatcher objects to add and match URIshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  19. 19. Sample Problem Suppose we want to create a resource that allows its clients to retrieve books, book titles, and book authors. What URIs do we need to support this resource?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  20. 20. URIs: Scheme, Authority, Paths ● Scheme: content ● Authority: org.vkedco.mobappdev.content_providers.books ● Paths: – book_title/id // retrieve all book ids – book_title/id/# // retreive a specific book title id // (e.g., book_title/ids/1, book_title/ids/2, etc) – book_author/author // retrieve all authors – book_author/author/# // retrieve a specific book author whos // id is # – book_cover_image/cover_image // retrieve all images – book_cover_image/cover_image/# // retrieve a specific imagehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  21. 21. URIs: Matching Codes ● BOOK_ID_ALL = 0 ● BOOK_ID_SIGNLE = 1 ● BOOK_TITLE_ALL = 2 ● BOOK_TITLE_SINGLE = 3 ● BOOK_AUTHOR_ALL = 4 ● BOOK_AUTHOR_SINGLE = 5 ● BOOK_COVER_IMAGE_ALL = 6 ● BOOK_COVER_IMAGE_SINGLE = 7http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  22. 22. Adding URIs to UriMatcher static final String AUTHORITY = "org.vkedco.content_providers.books"; UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static final String BOOK_ID_PATH = “/books/id/”; static final int CODE_BOOK_ID_ALL = 0; mUriMatcher.addURI(AUTHORITY, BOOK_ID_PATH, BOOK_ID_ALL);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  23. 23. Adding URIs to UriMatcher static final String AUTHORITY = "org.vkedco.mobappdev.content_providers.books"; UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static final String BOOK_ID_PATH = “/books/id/”; static final int CODE_BOOK_ID_ALL = 0; mUriMatcher.addURI(AUTHORITY, BOOK_ID_PATH, BOOK_ID_ALL); static final int CODE_BOOK_ID_SINGLE = 1; // uri matching code names static final String CODE_BOOK_ID_ALL_NAME = "CODE_BOOK_ID_ALL"; static final String CODE_BOOK_ID_SINGLE_NAME = "CODE_BOOK_ID_SINGLE"; // uri mime types static final String BOOK_TITLE_ID_ALL_MIME = "vnd.android.cursor.dir/vnd.vkedco.mobappdev.book_id_all"; static final String BOOK_TITLE_ID_SINGLE_MIME = "vnd.android.cursor.item/vnd.vkedco.mobappdev.book_id_single";http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  24. 24. URI Matching Codes and Mimes // uri mime types static final String BOOK_TITLE_ID_ALL_MIME = "vnd.android.cursor.dir/vnd.vkedco.mobappdev.book_id_all"; static final String BOOK_TITLE_ID_SINGLE_MIME = "vnd.android.cursor.item/vnd.vkedco.mobappdev.book_id_single"; public String getType (Uri uri) { switch ( mUriMatcher.match(uri) ) { case BOOK_ID_SINGLE: return BOOK_TITLE_ID_SINGLE_MIME; case BOOK_ID_ALL: return BOOK_TITLE_ID_ALL_MIME; // etc }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  25. 25. Basic URI Matching Pattern void matchUri(Uri uri) { int uri_match_code = mUriMatcher.match(uri); switch ( uri_match_code ) { case CODE_BOOK_ID_ALL: // do something with this URI break; case CODE_BOOK_ID_SINGLE: // do something with this URI break; // handle other cases }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  26. 26. 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://www.faqs.org/rfcs/rfc2396.htmlhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×