blob: d34fb0758661e387f5344d4fc05385d3f6eb470e [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
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
17package com.android.documentsui;
18
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070019import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkey6defd072013-09-23 14:23:41 -070020import static com.android.documentsui.DocumentsActivity.State.ACTION_CREATE;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070021import static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE;
22import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
23import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070024import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
25import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070026import static com.android.documentsui.model.DocumentInfo.getCursorInt;
27import static com.android.documentsui.model.DocumentInfo.getCursorLong;
28import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070029
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070030import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070031import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070032import android.app.FragmentManager;
33import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070034import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070035import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070036import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070037import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070039import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070041import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070042import android.graphics.Bitmap;
43import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070044import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070045import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070047import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070048import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070049import android.os.CancellationSignal;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070050import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070052import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070053import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070054import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070055import android.text.format.Time;
56import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070057import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070058import android.util.SparseBooleanArray;
59import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070060import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070061import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070062import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070063import android.view.View;
64import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070065import android.widget.AbsListView;
66import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070067import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070068import android.widget.AdapterView;
69import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070070import android.widget.BaseAdapter;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070071import android.widget.FrameLayout;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070073import android.widget.ImageView;
74import android.widget.ListView;
75import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070076import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070077
Jeff Sharkeyb3620442013-09-01 18:41:04 -070078import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070079import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070080import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070081import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070082import com.google.android.collect.Lists;
83
84import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070085import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070086import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070087
88/**
89 * Display the documents inside a single directory.
90 */
91public class DirectoryFragment extends Fragment {
92
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070093 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070094 private ListView mListView;
95 private GridView mGridView;
96
Jeff Sharkeyc317af82013-07-01 16:56:54 -070097 private AbsListView mCurrentView;
98
Jeff Sharkeya5defe32013-08-05 17:56:48 -070099 public static final int TYPE_NORMAL = 1;
100 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700101 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700102
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700103 public static final int ANIM_NONE = 1;
104 public static final int ANIM_SIDE = 2;
105 public static final int ANIM_DOWN = 3;
106 public static final int ANIM_UP = 4;
107
Jeff Sharkey5b535922013-08-02 15:55:26 -0700108 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700109 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700110
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700111 private int mLastMode = MODE_UNKNOWN;
112 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700113 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700114
Jeff Sharkey9656a532013-09-13 13:42:19 -0700115 private boolean mHideGridTitles = false;
116
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700117 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700118 private Point mThumbSize;
119
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700120 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700121 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700122
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700123 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700124 private static final String EXTRA_ROOT = "root";
125 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700126 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700127 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700129 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700130
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 private final int mLoaderId = sLoaderId.incrementAndGet();
132
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700133 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
134 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700135 }
136
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700137 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
138 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700139 }
140
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700141 public static void showRecentsOpen(FragmentManager fm, int anim) {
142 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700143 }
144
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700145 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
146 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700147 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700148 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700149 args.putParcelable(EXTRA_ROOT, root);
150 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700151 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700152
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700153 final FragmentTransaction ft = fm.beginTransaction();
154 switch (anim) {
155 case ANIM_SIDE:
156 args.putBoolean(EXTRA_IGNORE_STATE, true);
157 break;
158 case ANIM_DOWN:
159 args.putBoolean(EXTRA_IGNORE_STATE, true);
160 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
161 break;
162 case ANIM_UP:
163 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
164 break;
165 }
166
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700167 final DirectoryFragment fragment = new DirectoryFragment();
168 fragment.setArguments(args);
169
Jeff Sharkey76112212013-08-06 11:26:10 -0700170 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700171 ft.commitAllowingStateLoss();
172 }
173
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700174 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
175 final StringBuilder builder = new StringBuilder();
176 builder.append(root != null ? root.authority : "null").append(';');
177 builder.append(root != null ? root.rootId : "null").append(';');
178 builder.append(doc != null ? doc.documentId : "null");
179 return builder.toString();
180 }
181
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700182 public static DirectoryFragment get(FragmentManager fm) {
183 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700184 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700185 }
186
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700187 @Override
188 public View onCreateView(
189 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
190 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700191 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
192
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700193 mEmptyView = view.findViewById(android.R.id.empty);
194
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700195 mListView = (ListView) view.findViewById(R.id.list);
196 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700197 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700198 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700199
200 mGridView = (GridView) view.findViewById(R.id.grid);
201 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700202 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700203 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700204
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700205 return view;
206 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700207
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700208 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700209 public void onDestroyView() {
210 super.onDestroyView();
211
212 // Cancel any outstanding thumbnail requests
213 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
214 final int count = target.getChildCount();
215 for (int i = 0; i < count; i++) {
216 final View view = target.getChildAt(i);
217 mRecycleListener.onMovedToScrapHeap(view);
218 }
219 }
220
221 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700222 public void onActivityCreated(Bundle savedInstanceState) {
223 super.onActivityCreated(savedInstanceState);
224
225 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700226 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700227
Jeff Sharkey9656a532013-09-13 13:42:19 -0700228 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
229 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
230
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700231 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700232 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700233 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700234
Jeff Sharkey9656a532013-09-13 13:42:19 -0700235 if (mType == TYPE_RECENT_OPEN) {
236 // Hide titles when showing recents for picking images/videos
237 mHideGridTitles = MimePredicate.mimeMatches(
238 MimePredicate.VISUAL_MIMES, state.acceptMimes);
239 } else {
240 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
241 }
242
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700243 final ActivityManager am = (ActivityManager) context.getSystemService(
244 Context.ACTIVITY_SERVICE);
245 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
246
Jeff Sharkey46899c82013-08-18 22:26:48 -0700247 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700248 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700249 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700250 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700251
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700252 Uri contentsUri;
253 switch (mType) {
254 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700255 contentsUri = DocumentsContract.buildChildDocumentsUri(
256 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700257 if (state.action == ACTION_MANAGE) {
258 contentsUri = DocumentsContract.setManageMode(contentsUri);
259 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700260 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700261 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700262 case TYPE_SEARCH:
263 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700264 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700265 if (state.action == ACTION_MANAGE) {
266 contentsUri = DocumentsContract.setManageMode(contentsUri);
267 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700268 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700269 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700270 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700271 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700272 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700273 default:
274 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700275 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700276 }
277
278 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700279 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700280 if (!isAdded()) return;
281
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700282 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700283
284 // Push latest state up to UI
285 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700286 if (result.mode != MODE_UNKNOWN) {
287 state.derivedMode = result.mode;
288 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700289 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700290 ((DocumentsActivity) context).onStateChanged();
291
292 updateDisplayState();
293
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700294 // When launched into empty recents, show drawer
295 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
296 ((DocumentsActivity) context).setRootsDrawerOpen(true);
297 }
298
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700299 // Restore any previous instance state
300 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
301 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
302 getView().restoreHierarchyState(container);
303 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700304 mListView.smoothScrollToPosition(0);
305 mGridView.smoothScrollToPosition(0);
306 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700307
308 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700309 }
310
311 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700312 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700313 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700314 }
315 };
316
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700317 // Kick off loader at least once
318 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
319
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700320 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700321 }
322
Jeff Sharkey42d26792013-09-06 13:22:09 -0700323 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700324 public void onStop() {
325 super.onStop();
326
327 // Remember last scroll location
328 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
329 getView().saveHierarchyState(container);
330 final State state = getDisplayState(this);
331 state.dirState.put(mStateKey, container);
332 }
333
334 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700335 public void onResume() {
336 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700337 updateDisplayState();
338 }
339
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700340 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700341 // Sort order change always triggers reload; we'll trigger state change
342 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700343 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
344 }
345
346 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700347 final ContentResolver resolver = getActivity().getContentResolver();
348 final State state = getDisplayState(this);
349
350 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
351 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
352
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700353 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700354 final Uri stateUri = RecentsProvider.buildState(
355 root.authority, root.rootId, doc.documentId);
356 final ContentValues values = new ContentValues();
357 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700358
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700359 new AsyncTask<Void, Void, Void>() {
360 @Override
361 protected Void doInBackground(Void... params) {
362 resolver.insert(stateUri, values);
363 return null;
364 }
365 }.execute();
366 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700367
368 // Mode change is just visual change; no need to kick loader, and
369 // deliver change event immediately.
370 state.derivedMode = state.userMode;
371 ((DocumentsActivity) getActivity()).onStateChanged();
372
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700373 updateDisplayState();
374 }
375
376 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700377 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700378
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700379 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700380 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700381 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700382
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700383 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
384 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700385
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700386 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700387 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700388 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
389 } else {
390 choiceMode = ListView.CHOICE_MODE_NONE;
391 }
392
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700393 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700394 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700395 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700396 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700397 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700398 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700399 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700400 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700401 mGridView.setChoiceMode(choiceMode);
402 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700403 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700404 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700405 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700406 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700407 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700408 mListView.setChoiceMode(choiceMode);
409 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700410 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700411 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700412 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700413
414 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700415 }
416
417 private OnItemClickListener mItemListener = new OnItemClickListener() {
418 @Override
419 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700420 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700421 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700422 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
423 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
424 if (isDocumentEnabled(docMimeType, docFlags)) {
425 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700426 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
427 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700428 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700429 }
430 };
431
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700432 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
433 @Override
434 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
435 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
436 return true;
437 }
438
439 @Override
440 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700441 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700442
443 final MenuItem open = menu.findItem(R.id.menu_open);
444 final MenuItem share = menu.findItem(R.id.menu_share);
445 final MenuItem delete = menu.findItem(R.id.menu_delete);
446
447 final boolean manageMode = state.action == ACTION_MANAGE;
448 open.setVisible(!manageMode);
449 share.setVisible(manageMode);
450 delete.setVisible(manageMode);
451
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700452 return true;
453 }
454
455 @Override
456 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700457 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700458 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700459 final int size = checked.size();
460 for (int i = 0; i < size; i++) {
461 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700462 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700463 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700464 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700465 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700466 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700467
Jeff Sharkey873daa32013-08-18 17:38:20 -0700468 final int id = item.getItemId();
469 if (id == R.id.menu_open) {
470 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700471 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700472 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700473
474 } else if (id == R.id.menu_share) {
475 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700476 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700477 return true;
478
479 } else if (id == R.id.menu_delete) {
480 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700481 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700482 return true;
483
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700484 } else {
485 return false;
486 }
487 }
488
489 @Override
490 public void onDestroyActionMode(ActionMode mode) {
491 // ignored
492 }
493
494 @Override
495 public void onItemCheckedStateChanged(
496 ActionMode mode, int position, long id, boolean checked) {
497 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700498 // Directories and footer items cannot be checked
499 boolean valid = false;
500
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700501 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700502 if (cursor != null) {
503 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700504 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
505 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
506 valid = isDocumentEnabled(docMimeType, docFlags);
507 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700508 }
509
510 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700511 mCurrentView.setItemChecked(position, false);
512 }
513 }
514
515 mode.setTitle(getResources()
516 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
517 }
518 };
519
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700520 private RecyclerListener mRecycleListener = new RecyclerListener() {
521 @Override
522 public void onMovedToScrapHeap(View view) {
523 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
524 if (iconThumb != null) {
525 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
526 if (oldTask != null) {
527 oldTask.reallyCancel();
528 iconThumb.setTag(null);
529 }
530 }
531 }
532 };
533
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700534 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700535 Intent intent;
536 if (docs.size() == 1) {
537 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700538
Jeff Sharkey873daa32013-08-18 17:38:20 -0700539 intent = new Intent(Intent.ACTION_SEND);
540 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
541 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700542 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700543 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700544
545 } else if (docs.size() > 1) {
546 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
547 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
548 intent.addCategory(Intent.CATEGORY_DEFAULT);
549
550 final ArrayList<String> mimeTypes = Lists.newArrayList();
551 final ArrayList<Uri> uris = Lists.newArrayList();
552 for (DocumentInfo doc : docs) {
553 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700554 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700555 }
556
557 intent.setType(findCommonMimeType(mimeTypes));
558 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
559
560 } else {
561 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700562 }
563
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700564 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700565 startActivity(intent);
566 }
567
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700568 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700569 final Context context = getActivity();
570 final ContentResolver resolver = context.getContentResolver();
571
572 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700573 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700574 if (!doc.isDeleteSupported()) {
575 Log.w(TAG, "Skipping " + doc);
576 hadTrouble = true;
577 continue;
578 }
579
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700580 ContentProviderClient client = null;
581 try {
582 client = DocumentsApplication.acquireUnstableProviderOrThrow(
583 resolver, doc.derivedUri.getAuthority());
584 DocumentsContract.deleteDocument(client, doc.derivedUri);
585 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700586 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700587 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700588 } finally {
589 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700590 }
591 }
592
593 if (hadTrouble) {
594 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
595 }
596 }
597
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700598 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700599 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700600 }
601
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700602 private static abstract class Footer {
603 private final int mItemViewType;
604
605 public Footer(int itemViewType) {
606 mItemViewType = itemViewType;
607 }
608
609 public abstract View getView(View convertView, ViewGroup parent);
610
611 public int getItemViewType() {
612 return mItemViewType;
613 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700614 }
615
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700616 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700617 public LoadingFooter() {
618 super(1);
619 }
620
Jeff Sharkey20b32272013-09-03 15:25:52 -0700621 @Override
622 public View getView(View convertView, ViewGroup parent) {
623 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700624 final State state = getDisplayState(DirectoryFragment.this);
625
Jeff Sharkey20b32272013-09-03 15:25:52 -0700626 if (convertView == null) {
627 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700628 if (state.derivedMode == MODE_LIST) {
629 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
630 } else if (state.derivedMode == MODE_GRID) {
631 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
632 } else {
633 throw new IllegalStateException();
634 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700635 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700636
Jeff Sharkey20b32272013-09-03 15:25:52 -0700637 return convertView;
638 }
639 }
640
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700641 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700642 private final int mIcon;
643 private final String mMessage;
644
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700645 public MessageFooter(int itemViewType, int icon, String message) {
646 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700647 mIcon = icon;
648 mMessage = message;
649 }
650
651 @Override
652 public View getView(View convertView, ViewGroup parent) {
653 final Context context = parent.getContext();
654 final State state = getDisplayState(DirectoryFragment.this);
655
656 if (convertView == null) {
657 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700658 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700659 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700660 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700661 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
662 } else {
663 throw new IllegalStateException();
664 }
665 }
666
667 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
668 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
669 icon.setImageResource(mIcon);
670 title.setText(mMessage);
671 return convertView;
672 }
673 }
674
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700675 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700676 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700677 private int mCursorCount;
678
679 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700680
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700681 public void swapResult(DirectoryResult result) {
682 mCursor = result != null ? result.cursor : null;
683 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700684
685 mFooters.clear();
686
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700687 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700688 if (extras != null) {
689 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
690 if (info != null) {
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700691 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700692 }
693 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
694 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700695 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700696 }
697 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
698 mFooters.add(new LoadingFooter());
699 }
700 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700701
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700702 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700703 mFooters.add(new MessageFooter(
704 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
705 }
706
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700707 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700708 mEmptyView.setVisibility(View.VISIBLE);
709 } else {
710 mEmptyView.setVisibility(View.GONE);
711 }
712
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700713 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700714 }
715
716 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700717 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700718 if (position < mCursorCount) {
719 return getDocumentView(position, convertView, parent);
720 } else {
721 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700722 convertView = mFooters.get(position).getView(convertView, parent);
723 // Only the view itself is disabled; contents inside shouldn't
724 // be dimmed.
725 convertView.setEnabled(false);
726 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700727 }
728 }
729
730 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700731 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700732 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700733
Jeff Sharkey9656a532013-09-13 13:42:19 -0700734 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
735
Jeff Sharkey873daa32013-08-18 17:38:20 -0700736 final RootsCache roots = DocumentsApplication.getRootsCache(context);
737 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
738 context, mThumbSize);
739
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700740 if (convertView == null) {
741 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700742 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700743 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700744 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700745 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700746
747 // Apply padding to grid items
748 final FrameLayout grid = (FrameLayout) convertView;
749 final int gridPadding = getResources()
750 .getDimensionPixelSize(R.dimen.grid_padding);
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700751
752 // Tricksy hobbitses! We need to fully clear the drawable so
753 // the view doesn't clobber the new InsetDrawable callback
754 // when setting back later.
755 final Drawable fg = grid.getForeground();
756 final Drawable bg = grid.getBackground();
757 grid.setForeground(null);
758 grid.setBackground(null);
759 grid.setForeground(new InsetDrawable(fg, gridPadding));
760 grid.setBackground(new InsetDrawable(bg, gridPadding));
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700761 } else {
762 throw new IllegalStateException();
763 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700764 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700765
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700766 final Cursor cursor = getItem(position);
767
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700768 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
769 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700770 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
771 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
772 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
773 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
774 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
775 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
776 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
777 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700778
Jeff Sharkey9656a532013-09-13 13:42:19 -0700779 final View line1 = convertView.findViewById(R.id.line1);
780 final View line2 = convertView.findViewById(R.id.line2);
781
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700782 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
783 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700784 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
785 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700786 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700787 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
788 final TextView date = (TextView) convertView.findViewById(R.id.date);
789 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700790
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700791 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700792 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700793 oldTask.reallyCancel();
794 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700795 }
796
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700797 iconMime.animate().cancel();
798 iconThumb.animate().cancel();
799
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700800 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700801 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700802 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700803 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700804
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700805 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700806 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700807 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700808 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700809 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700810 iconThumb.setImageBitmap(cachedResult);
811 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700812 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700813 iconThumb.setImageDrawable(null);
814 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
815 uri, iconMime, iconThumb, mThumbSize);
816 iconThumb.setTag(task);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700817 task.executeOnExecutor(ProviderExecutor.forAuthority(docAuthority));
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700818 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700819 }
820
821 // Always throw MIME icon into place, even when a thumbnail is being
822 // loaded in background.
823 if (cacheHit) {
824 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700825 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700826 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700827 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700828 iconMime.setAlpha(1f);
829 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700830 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700831 if (docIcon != 0) {
832 iconMime.setImageDrawable(
833 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
834 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700835 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
836 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700837 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700838 }
839
Jeff Sharkey9656a532013-09-13 13:42:19 -0700840 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700841 boolean hasLine2 = false;
842
Jeff Sharkey9656a532013-09-13 13:42:19 -0700843 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
844 if (!hideTitle) {
845 title.setText(docDisplayName);
846 hasLine1 = true;
847 }
848
849 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700850 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700851 // We've already had to enumerate roots before any results can
852 // be shown, so this will never block.
853 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700854 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700855
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700856 if (summary != null) {
857 final boolean alwaysShowSummary = getResources()
858 .getBoolean(R.bool.always_show_summary);
859 if (alwaysShowSummary) {
860 summary.setText(root.getDirectoryString());
861 summary.setVisibility(View.VISIBLE);
862 hasLine2 = true;
863 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700864 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700865 // No summary needed if icon speaks for itself
866 summary.setVisibility(View.INVISIBLE);
867 } else {
868 summary.setText(root.getDirectoryString());
869 summary.setVisibility(View.VISIBLE);
870 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
871 hasLine2 = true;
872 }
873 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700874 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700875 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700876 // Directories showing thumbnails in grid mode get a little icon
877 // hint to remind user they're a directory.
878 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
879 && showThumbnail) {
880 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
881 }
882
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700883 if (summary != null) {
884 if (docSummary != null) {
885 summary.setText(docSummary);
886 summary.setVisibility(View.VISIBLE);
887 hasLine2 = true;
888 } else {
889 summary.setVisibility(View.INVISIBLE);
890 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700891 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700892 }
893
Jeff Sharkey9656a532013-09-13 13:42:19 -0700894 if (icon1 != null) icon1.setVisibility(View.GONE);
895 if (icon2 != null) icon2.setVisibility(View.GONE);
896
897 if (iconDrawable != null) {
898 if (hasLine1) {
899 icon1.setVisibility(View.VISIBLE);
900 icon1.setImageDrawable(iconDrawable);
901 } else {
902 icon2.setVisibility(View.VISIBLE);
903 icon2.setImageDrawable(iconDrawable);
904 }
905 }
906
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700907 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700908 date.setText(null);
909 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700910 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700911 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700912 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700913
914 if (state.showSize) {
915 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700916 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700917 size.setText(null);
918 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700919 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700920 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700921 }
922 } else {
923 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700924 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700925
Jeff Sharkey9656a532013-09-13 13:42:19 -0700926 if (line1 != null) {
927 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
928 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700929 if (line2 != null) {
930 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
931 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700932
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700933 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700934 if (enabled) {
935 setEnabledRecursive(convertView, true);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700936 iconMime.setAlpha(1f);
937 iconThumb.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700938 if (icon1 != null) icon1.setAlpha(1f);
939 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700940 } else {
941 setEnabledRecursive(convertView, false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700942 iconMime.setAlpha(0.5f);
943 iconThumb.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700944 if (icon1 != null) icon1.setAlpha(0.5f);
945 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700946 }
947
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700948 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700949 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700950
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700951 @Override
952 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700953 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700954 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700955
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700956 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700957 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700958 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700959 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700960 return mCursor;
961 } else {
962 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700963 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700964 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700965
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700966 @Override
967 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700968 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700969 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700970
971 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700972 public int getViewTypeCount() {
973 return 4;
974 }
975
976 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700977 public int getItemViewType(int position) {
978 if (position < mCursorCount) {
979 return 0;
980 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700981 position -= mCursorCount;
982 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700983 }
984 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700985 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700986
987 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700988 private final Uri mUri;
989 private final ImageView mIconMime;
990 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700991 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700992 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700993
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700994 public ThumbnailAsyncTask(
995 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
996 mUri = uri;
997 mIconMime = iconMime;
998 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700999 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001000 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001001 }
1002
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001003 public void reallyCancel() {
1004 cancel(false);
1005 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001006 }
1007
1008 @Override
1009 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001010 if (isCancelled()) return null;
1011
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001012 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001013 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001014
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001015 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001016 Bitmap result = null;
1017 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001018 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1019 resolver, mUri.getAuthority());
1020 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001021 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001022 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1023 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001024 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001025 }
1026 } catch (Exception e) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001027 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1028 } finally {
1029 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001030 }
1031 return result;
1032 }
1033
1034 @Override
1035 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001036 if (mIconThumb.getTag() == this && result != null) {
1037 mIconThumb.setTag(null);
1038 mIconThumb.setImageBitmap(result);
1039
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001040 final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
1041 mIconMime.setAlpha(targetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001042 mIconMime.animate().alpha(0f).start();
1043 mIconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001044 mIconThumb.animate().alpha(targetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001045 }
1046 }
1047 }
1048
1049 private static String formatTime(Context context, long when) {
1050 // TODO: DateUtils should make this easier
1051 Time then = new Time();
1052 then.set(when);
1053 Time now = new Time();
1054 now.setToNow();
1055
1056 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1057 | DateUtils.FORMAT_ABBREV_ALL;
1058
1059 if (then.year != now.year) {
1060 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1061 } else if (then.yearDay != now.yearDay) {
1062 flags |= DateUtils.FORMAT_SHOW_DATE;
1063 } else {
1064 flags |= DateUtils.FORMAT_SHOW_TIME;
1065 }
1066
1067 return DateUtils.formatDateTime(context, when, flags);
1068 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001069
1070 private String findCommonMimeType(List<String> mimeTypes) {
1071 String[] commonType = mimeTypes.get(0).split("/");
1072 if (commonType.length != 2) {
1073 return "*/*";
1074 }
1075
1076 for (int i = 1; i < mimeTypes.size(); i++) {
1077 String[] type = mimeTypes.get(i).split("/");
1078 if (type.length != 2) continue;
1079
1080 if (!commonType[1].equals(type[1])) {
1081 commonType[1] = "*";
1082 }
1083
1084 if (!commonType[0].equals(type[0])) {
1085 commonType[0] = "*";
1086 commonType[1] = "*";
1087 break;
1088 }
1089 }
1090
1091 return commonType[0] + "/" + commonType[1];
1092 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001093
1094 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001095 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001096 if (v.isEnabled() == enabled) return;
1097 v.setEnabled(enabled);
1098
1099 if (v instanceof ViewGroup) {
1100 final ViewGroup vg = (ViewGroup) v;
1101 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1102 setEnabledRecursive(vg.getChildAt(i), enabled);
1103 }
1104 }
1105 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001106
1107 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1108 final State state = getDisplayState(DirectoryFragment.this);
1109
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001110 // Directories are always enabled
1111 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1112 return true;
1113 }
1114
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001115 // Read-only files are disabled when creating
1116 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1117 return false;
1118 }
1119
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001120 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1121 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001122}