Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Document-centric Android

cketti
June 04, 2015

Document-centric Android

This talk shows how to use the Storage Access Framework and also how to make use of the option to have multiple entries per app in the Overview Screen (previously: Recents Screen).

http://droidcon.de/session/be-part-document-centric-android
https://www.youtube.com/watch?v=sMlsBAXMXok

cketti

June 04, 2015
Tweet

More Decks by cketti

Other Decks in Programming

Transcript

  1. Storage Access Framework • framework to browse and open documents,

    images, and other files across preferred document storage providers • standard, easy-to-use UI to let users access documents in a consistent way across apps
  2. Getting content the “new” way Android 4.4+ • Document Providers

    at the top • “Legacy” providers at the bottom
  3. Getting content Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); Intent

    chooserIntent = Intent.createChooser(i, null); startActivityForResult(chooserIntent, requestCode);
  4. Getting content Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,

    true); Intent chooserIntent = Intent.createChooser(i, null); startActivityForResult(chooserIntent, requestCode); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
  5. Getting content - onActivityResult if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ClipData

    clipData = data.getClipData(); if (clipData != null) { // read URIs from ClipData return; } } // read URI from Intent.getData()
  6. Options for saving content • Download Manager • Share Intent

    • Write to hardcoded directories • …
  7. Picking a directory Use third-party app to select directory private

    static final String[][] PICK_DIRECTORY_INTENTS = { { "org.openintents.action.PICK_DIRECTORY", "file://" }, { "com.estrongs.action.PICK_DIRECTORY", "file://" }, { Intent.ACTION_PICK, "folder://" }, { "com.androidworkz.action.PICK_DIRECTORY", "file://" } };
  8. Saving content the new way Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    intent.setType("image/jpeg"); intent.putExtra(Intent.EXTRA_TITLE, "corgi.jpg"); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(intent, requestCode);
  9. Picking a directory the new way Intent intent = new

    Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, requestCode); // in onActivityResult - receive URI for document tree String rootDocumentId = DocumentsContract.getTreeDocumentId(treeUri); Uri rootDocumentUri = DocumentsContract.buildDocumentUriUsingTree( treeUri, rootDocumentId); Uri newDocumentUri = DocumentsContract.createDocument( contentResolver, rootDocumentUri, "image/png", "new.png");
  10. How to create a new document task? Ideally, it just

    works Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("mailto:[email protected]")); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(intent);
  11. How to create a new document task? Ideally, it just

    works Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("mailto:[email protected]")); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(intent); Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
  12. How to create a new document task? Ideally, it just

    works Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("mailto:[email protected]")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); startActivity(intent); Intent.FLAG_ACTIVITY_NEW_DOCUMENT
  13. When to use FLAG_ACTIVITY_NEW_DOCUMENT? • Potentially whenever starting an external

    activity • When multiple entries for your app in the Overview Screen are desirable
  14. More Activity Flags • With FLAG_ACTIVITY_NEW_DOCUMENT existing tasks will be

    brought to the front • Use FLAG_ACTIVITY_MULTIPLE_TASK to force creation of a new task
  15. documentLaunchMode • always • intoExisting • never • none Use

    this rather than the Intent flag! <activity> attribute in manifest
  16. Summary • SAF makes many things easier ◦ Not a

    solution for pre-KitKat devices ◦ Support isn’t great right now :( • Document tasks solve many small issues • It’s easy to get started. Do it now!