summaryrefslogtreecommitdiffstats
path: root/packages/DocumentsUI
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2013-04-26 16:54:55 -0700
committerJeff Sharkey <jsharkey@android.com>2013-05-01 17:44:42 -0700
commit9e0036ed7d3260d79cc5f9ffd8e3bbe760699924 (patch)
tree88ca52627f99558d18a29acb207bd15cec379ba4 /packages/DocumentsUI
parent9ecfee03fa188aebfbd9778b4e020323903495ee (diff)
downloadframeworks_base-9e0036ed7d3260d79cc5f9ffd8e3bbe760699924.zip
frameworks_base-9e0036ed7d3260d79cc5f9ffd8e3bbe760699924.tar.gz
frameworks_base-9e0036ed7d3260d79cc5f9ffd8e3bbe760699924.tar.bz2
External storage provider, document picker UI.
Continuing to flesh out storage backends by adding an external storage document backend. Still rough, but it can traverse files and directories. Early pass at OPEN/CREATE_DOC picker UI, which offers to traverse any known storage backends. Supports opening subdirectories and returning a picked file. Change-Id: Idc3554036b3816a93d9b465ee8a620746859d2ae
Diffstat (limited to 'packages/DocumentsUI')
-rw-r--r--packages/DocumentsUI/Android.mk11
-rw-r--r--packages/DocumentsUI/AndroidManifest.xml21
-rw-r--r--packages/DocumentsUI/res/values/strings.xml19
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java168
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java115
5 files changed, 334 insertions, 0 deletions
diff --git a/packages/DocumentsUI/Android.mk b/packages/DocumentsUI/Android.mk
new file mode 100644
index 0000000..1e45807
--- /dev/null
+++ b/packages/DocumentsUI/Android.mk
@@ -0,0 +1,11 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
+LOCAL_PACKAGE_NAME := DocumentsUI
+LOCAL_CERTIFICATE := platform
+
+include $(BUILD_PACKAGE)
diff --git a/packages/DocumentsUI/AndroidManifest.xml b/packages/DocumentsUI/AndroidManifest.xml
new file mode 100644
index 0000000..84c5474
--- /dev/null
+++ b/packages/DocumentsUI/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.documentsui">
+
+ <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
+
+ <application android:label="@string/app_label">
+ <activity
+ android:name=".DocumentsActivity"
+ android:finishOnCloseSystemDialogs="true"
+ android:excludeFromRecents="true">
+ <intent-filter android:priority="100">
+ <action android:name="android.intent.action.OPEN_DOCUMENT" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ <intent-filter android:priority="100">
+ <action android:name="android.intent.action.CREATE_DOCUMENT" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/packages/DocumentsUI/res/values/strings.xml b/packages/DocumentsUI/res/values/strings.xml
new file mode 100644
index 0000000..89f6496
--- /dev/null
+++ b/packages/DocumentsUI/res/values/strings.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<resources>
+ <string name="app_label">Documents</string>
+</resources>
diff --git a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
new file mode 100644
index 0000000..d43abde
--- /dev/null
+++ b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui;
+
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.app.ListFragment;
+import android.app.LoaderManager.LoaderCallbacks;
+import android.content.Context;
+import android.content.CursorLoader;
+import android.content.Loader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.DocumentsContract;
+import android.provider.DocumentsContract.DocumentColumns;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.CursorAdapter;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.TextView;
+
+public class DirectoryFragment extends ListFragment {
+ private DocumentsAdapter mAdapter;
+ private LoaderCallbacks<Cursor> mCallbacks;
+
+ private static final String EXTRA_URI = "uri";
+
+ private static final int LOADER_DOCUMENTS = 2;
+
+ public static void show(FragmentManager fm, Uri uri, CharSequence title) {
+ final Bundle args = new Bundle();
+ args.putParcelable(EXTRA_URI, uri);
+
+ final DirectoryFragment fragment = new DirectoryFragment();
+ fragment.setArguments(args);
+
+ final FragmentTransaction ft = fm.beginTransaction();
+ ft.replace(android.R.id.content, fragment);
+ ft.addToBackStack(title.toString());
+ ft.setBreadCrumbTitle(title);
+ ft.commitAllowingStateLoss();
+ }
+
+ @Override
+ public View onCreateView(
+ LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+ final Context context = inflater.getContext();
+
+ mAdapter = new DocumentsAdapter(context);
+ setListAdapter(mAdapter);
+
+ mCallbacks = new LoaderCallbacks<Cursor>() {
+ @Override
+ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+ final Uri uri = args.getParcelable(EXTRA_URI);
+ return new CursorLoader(context, uri, null, null, null, null);
+ }
+
+ @Override
+ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+ mAdapter.swapCursor(data);
+ }
+
+ @Override
+ public void onLoaderReset(Loader<Cursor> loader) {
+ mAdapter.swapCursor(null);
+ }
+ };
+
+ return super.onCreateView(inflater, container, savedInstanceState);
+ }
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ getLoaderManager().restartLoader(LOADER_DOCUMENTS, getArguments(), mCallbacks);
+ }
+
+ @Override
+ public void onStop() {
+ super.onStop();
+ getLoaderManager().destroyLoader(LOADER_DOCUMENTS);
+ }
+
+ @Override
+ public void onListItemClick(ListView l, View v, int position, long id) {
+ final Cursor cursor = (Cursor) mAdapter.getItem(position);
+ final String guid = getCursorString(cursor, DocumentColumns.GUID);
+ final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE);
+
+ final Uri uri = getArguments().getParcelable(EXTRA_URI);
+ final Uri childUri = DocumentsContract.buildDocumentUri(uri.getAuthority(), guid);
+
+ if (DocumentsContract.MIME_TYPE_DIRECTORY.equals(mimeType)) {
+ // Nested directory picked, recurse using new fragment
+ final Uri childContentsUri = DocumentsContract.buildContentsUri(childUri);
+ final String displayName = cursor.getString(
+ cursor.getColumnIndex(DocumentColumns.DISPLAY_NAME));
+ DirectoryFragment.show(getFragmentManager(), childContentsUri, displayName);
+ } else {
+ // Explicit file picked, return
+ ((DocumentsActivity) getActivity()).onDocumentPicked(childUri);
+ }
+ }
+
+ private class DocumentsAdapter extends CursorAdapter {
+ public DocumentsAdapter(Context context) {
+ super(context, null, false);
+ }
+
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ return LayoutInflater.from(context)
+ .inflate(com.android.internal.R.layout.preference, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ final TextView title = (TextView) view.findViewById(android.R.id.title);
+ final TextView summary = (TextView) view.findViewById(android.R.id.summary);
+ final ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
+
+ icon.setMaxWidth(128);
+ icon.setMaxHeight(128);
+
+ final String guid = getCursorString(cursor, DocumentColumns.GUID);
+ final String displayName = getCursorString(cursor, DocumentColumns.DISPLAY_NAME);
+ final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE);
+ final int flags = getCursorInt(cursor, DocumentColumns.FLAGS);
+
+ if ((flags & DocumentsContract.FLAG_SUPPORTS_THUMBNAIL) != 0) {
+ final Uri uri = getArguments().getParcelable(EXTRA_URI);
+ final Uri childUri = DocumentsContract.buildDocumentUri(uri.getAuthority(), guid);
+ icon.setImageURI(childUri);
+ } else {
+ icon.setImageURI(null);
+ }
+
+ title.setText(displayName);
+ summary.setText(mimeType);
+ }
+ }
+
+ private static String getCursorString(Cursor cursor, String columnName) {
+ return cursor.getString(cursor.getColumnIndex(columnName));
+ }
+
+ private static int getCursorInt(Cursor cursor, String columnName) {
+ return cursor.getInt(cursor.getColumnIndex(columnName));
+ }
+}
diff --git a/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java b/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
new file mode 100644
index 0000000..196776b
--- /dev/null
+++ b/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui;
+
+import android.app.Activity;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.app.ListFragment;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ProviderInfo;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.DocumentsContract;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+import com.google.android.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DocumentsActivity extends Activity {
+ private static final String TAG = "Documents";
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+
+ SourceFragment.show(getFragmentManager());
+ setResult(Activity.RESULT_CANCELED);
+ }
+
+ public void onDocumentPicked(Uri uri) {
+ Log.d(TAG, "onDocumentPicked() " + uri);
+
+ final Intent intent = new Intent();
+ intent.setData(uri);
+
+ intent.addFlags(
+ Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_PERSIST_GRANT_URI_PERMISSION);
+ if (Intent.ACTION_CREATE_DOCUMENT.equals(getIntent().getAction())) {
+ intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+ }
+
+ setResult(Activity.RESULT_OK, intent);
+ finish();
+ }
+
+ public static class SourceFragment extends ListFragment {
+ private ArrayList<ProviderInfo> mProviders = Lists.newArrayList();
+ private ArrayAdapter<ProviderInfo> mAdapter;
+
+ public static void show(FragmentManager fm) {
+ final SourceFragment fragment = new SourceFragment();
+
+ final FragmentTransaction ft = fm.beginTransaction();
+ ft.replace(android.R.id.content, fragment);
+ ft.setBreadCrumbTitle("TOP");
+ ft.commitAllowingStateLoss();
+ }
+
+ @Override
+ public View onCreateView(
+ LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+ final Context context = inflater.getContext();
+
+ // Gather known storage providers
+ mProviders.clear();
+ final List<ProviderInfo> providers = context.getPackageManager()
+ .queryContentProviders(null, -1, PackageManager.GET_META_DATA);
+ for (ProviderInfo info : providers) {
+ if (info.metaData != null
+ && info.metaData.containsKey(
+ DocumentsContract.META_DATA_DOCUMENT_PROVIDER)) {
+ mProviders.add(info);
+ }
+ }
+
+ mAdapter = new ArrayAdapter<ProviderInfo>(
+ context, android.R.layout.simple_list_item_1, mProviders);
+ setListAdapter(mAdapter);
+
+ return super.onCreateView(inflater, container, savedInstanceState);
+ }
+
+ @Override
+ public void onListItemClick(ListView l, View v, int position, long id) {
+ final ProviderInfo info = mAdapter.getItem(position);
+ final Uri uri = DocumentsContract.buildContentsUri(DocumentsContract.buildDocumentUri(
+ info.authority, DocumentsContract.ROOT_GUID));
+ final String displayName = info.name;
+ DirectoryFragment.show(getFragmentManager(), uri, displayName);
+ }
+ }
+}