Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
| 19 | import android.app.FragmentManager; |
| 20 | import android.app.FragmentTransaction; |
| 21 | import android.app.ListFragment; |
| 22 | import android.app.LoaderManager.LoaderCallbacks; |
| 23 | import android.content.Context; |
| 24 | import android.content.CursorLoader; |
| 25 | import android.content.Loader; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.DocumentsContract; |
| 30 | import android.provider.DocumentsContract.DocumentColumns; |
| 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
| 34 | import android.widget.CursorAdapter; |
| 35 | import android.widget.ImageView; |
| 36 | import android.widget.ListView; |
| 37 | import android.widget.TextView; |
| 38 | |
| 39 | public class DirectoryFragment extends ListFragment { |
| 40 | private DocumentsAdapter mAdapter; |
| 41 | private LoaderCallbacks<Cursor> mCallbacks; |
| 42 | |
| 43 | private static final String EXTRA_URI = "uri"; |
| 44 | |
| 45 | private static final int LOADER_DOCUMENTS = 2; |
| 46 | |
| 47 | public static void show(FragmentManager fm, Uri uri, CharSequence title) { |
| 48 | final Bundle args = new Bundle(); |
| 49 | args.putParcelable(EXTRA_URI, uri); |
| 50 | |
| 51 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 52 | fragment.setArguments(args); |
| 53 | |
| 54 | final FragmentTransaction ft = fm.beginTransaction(); |
| 55 | ft.replace(android.R.id.content, fragment); |
| 56 | ft.addToBackStack(title.toString()); |
| 57 | ft.setBreadCrumbTitle(title); |
| 58 | ft.commitAllowingStateLoss(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public View onCreateView( |
| 63 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 64 | final Context context = inflater.getContext(); |
| 65 | |
| 66 | mAdapter = new DocumentsAdapter(context); |
| 67 | setListAdapter(mAdapter); |
| 68 | |
| 69 | mCallbacks = new LoaderCallbacks<Cursor>() { |
| 70 | @Override |
| 71 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 72 | final Uri uri = args.getParcelable(EXTRA_URI); |
| 73 | return new CursorLoader(context, uri, null, null, null, null); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 78 | mAdapter.swapCursor(data); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onLoaderReset(Loader<Cursor> loader) { |
| 83 | mAdapter.swapCursor(null); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | return super.onCreateView(inflater, container, savedInstanceState); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onStart() { |
| 92 | super.onStart(); |
| 93 | getLoaderManager().restartLoader(LOADER_DOCUMENTS, getArguments(), mCallbacks); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onStop() { |
| 98 | super.onStop(); |
| 99 | getLoaderManager().destroyLoader(LOADER_DOCUMENTS); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void onListItemClick(ListView l, View v, int position, long id) { |
| 104 | final Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 105 | final String guid = getCursorString(cursor, DocumentColumns.GUID); |
| 106 | final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE); |
| 107 | |
| 108 | final Uri uri = getArguments().getParcelable(EXTRA_URI); |
| 109 | final Uri childUri = DocumentsContract.buildDocumentUri(uri.getAuthority(), guid); |
| 110 | |
| 111 | if (DocumentsContract.MIME_TYPE_DIRECTORY.equals(mimeType)) { |
| 112 | // Nested directory picked, recurse using new fragment |
| 113 | final Uri childContentsUri = DocumentsContract.buildContentsUri(childUri); |
| 114 | final String displayName = cursor.getString( |
| 115 | cursor.getColumnIndex(DocumentColumns.DISPLAY_NAME)); |
| 116 | DirectoryFragment.show(getFragmentManager(), childContentsUri, displayName); |
| 117 | } else { |
| 118 | // Explicit file picked, return |
| 119 | ((DocumentsActivity) getActivity()).onDocumentPicked(childUri); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private class DocumentsAdapter extends CursorAdapter { |
| 124 | public DocumentsAdapter(Context context) { |
| 125 | super(context, null, false); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 130 | return LayoutInflater.from(context) |
| 131 | .inflate(com.android.internal.R.layout.preference, parent, false); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void bindView(View view, Context context, Cursor cursor) { |
| 136 | final TextView title = (TextView) view.findViewById(android.R.id.title); |
| 137 | final TextView summary = (TextView) view.findViewById(android.R.id.summary); |
| 138 | final ImageView icon = (ImageView) view.findViewById(android.R.id.icon); |
| 139 | |
| 140 | icon.setMaxWidth(128); |
| 141 | icon.setMaxHeight(128); |
| 142 | |
| 143 | final String guid = getCursorString(cursor, DocumentColumns.GUID); |
| 144 | final String displayName = getCursorString(cursor, DocumentColumns.DISPLAY_NAME); |
| 145 | final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE); |
| 146 | final int flags = getCursorInt(cursor, DocumentColumns.FLAGS); |
| 147 | |
| 148 | if ((flags & DocumentsContract.FLAG_SUPPORTS_THUMBNAIL) != 0) { |
| 149 | final Uri uri = getArguments().getParcelable(EXTRA_URI); |
| 150 | final Uri childUri = DocumentsContract.buildDocumentUri(uri.getAuthority(), guid); |
| 151 | icon.setImageURI(childUri); |
| 152 | } else { |
| 153 | icon.setImageURI(null); |
| 154 | } |
| 155 | |
| 156 | title.setText(displayName); |
| 157 | summary.setText(mimeType); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private static String getCursorString(Cursor cursor, String columnName) { |
| 162 | return cursor.getString(cursor.getColumnIndex(columnName)); |
| 163 | } |
| 164 | |
| 165 | private static int getCursorInt(Cursor cursor, String columnName) { |
| 166 | return cursor.getInt(cursor.getColumnIndex(columnName)); |
| 167 | } |
| 168 | } |