blob: 59caad0ecf5736b7b50694af038a87fbe8272208 [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 Sharkeyc8ae7a52013-09-18 16:26:49 -0700294 // Restore any previous instance state
295 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
296 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
297 getView().restoreHierarchyState(container);
298 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700299 mListView.smoothScrollToPosition(0);
300 mGridView.smoothScrollToPosition(0);
301 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700302
303 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700304 }
305
306 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700307 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700308 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700309 }
310 };
311
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700312 // Kick off loader at least once
313 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
314
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700315 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700316 }
317
Jeff Sharkey42d26792013-09-06 13:22:09 -0700318 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700319 public void onStop() {
320 super.onStop();
321
322 // Remember last scroll location
323 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
324 getView().saveHierarchyState(container);
325 final State state = getDisplayState(this);
326 state.dirState.put(mStateKey, container);
327 }
328
329 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700330 public void onResume() {
331 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700332 updateDisplayState();
333 }
334
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700335 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700336 // Sort order change always triggers reload; we'll trigger state change
337 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700338 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
339 }
340
341 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700342 final ContentResolver resolver = getActivity().getContentResolver();
343 final State state = getDisplayState(this);
344
345 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
346 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
347
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700348 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700349 final Uri stateUri = RecentsProvider.buildState(
350 root.authority, root.rootId, doc.documentId);
351 final ContentValues values = new ContentValues();
352 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700353
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700354 new AsyncTask<Void, Void, Void>() {
355 @Override
356 protected Void doInBackground(Void... params) {
357 resolver.insert(stateUri, values);
358 return null;
359 }
360 }.execute();
361 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700362
363 // Mode change is just visual change; no need to kick loader, and
364 // deliver change event immediately.
365 state.derivedMode = state.userMode;
366 ((DocumentsActivity) getActivity()).onStateChanged();
367
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700368 updateDisplayState();
369 }
370
371 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700372 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700373
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700374 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700375 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700376 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700377
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700378 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
379 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700380
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700381 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700382 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700383 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
384 } else {
385 choiceMode = ListView.CHOICE_MODE_NONE;
386 }
387
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700388 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700389 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700390 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700391 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700392 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700393 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700394 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700395 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700396 mGridView.setChoiceMode(choiceMode);
397 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700398 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700399 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700400 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700401 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700402 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700403 mListView.setChoiceMode(choiceMode);
404 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700405 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700406 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700407 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700408
409 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700410 }
411
412 private OnItemClickListener mItemListener = new OnItemClickListener() {
413 @Override
414 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700415 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700416 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700417 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
418 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
419 if (isDocumentEnabled(docMimeType, docFlags)) {
420 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700421 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
422 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700423 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700424 }
425 };
426
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700427 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
428 @Override
429 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
430 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
431 return true;
432 }
433
434 @Override
435 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700436 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700437
438 final MenuItem open = menu.findItem(R.id.menu_open);
439 final MenuItem share = menu.findItem(R.id.menu_share);
440 final MenuItem delete = menu.findItem(R.id.menu_delete);
441
442 final boolean manageMode = state.action == ACTION_MANAGE;
443 open.setVisible(!manageMode);
444 share.setVisible(manageMode);
445 delete.setVisible(manageMode);
446
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 return true;
448 }
449
450 @Override
451 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700452 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700453 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700454 final int size = checked.size();
455 for (int i = 0; i < size; i++) {
456 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700457 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700458 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700459 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700460 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700461 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700462
Jeff Sharkey873daa32013-08-18 17:38:20 -0700463 final int id = item.getItemId();
464 if (id == R.id.menu_open) {
465 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700466 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700467 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700468
469 } else if (id == R.id.menu_share) {
470 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700471 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700472 return true;
473
474 } else if (id == R.id.menu_delete) {
475 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700476 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700477 return true;
478
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700479 } else {
480 return false;
481 }
482 }
483
484 @Override
485 public void onDestroyActionMode(ActionMode mode) {
486 // ignored
487 }
488
489 @Override
490 public void onItemCheckedStateChanged(
491 ActionMode mode, int position, long id, boolean checked) {
492 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700493 // Directories and footer items cannot be checked
494 boolean valid = false;
495
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700496 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700497 if (cursor != null) {
498 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700499 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
500 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
501 valid = isDocumentEnabled(docMimeType, docFlags);
502 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700503 }
504
505 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700506 mCurrentView.setItemChecked(position, false);
507 }
508 }
509
510 mode.setTitle(getResources()
511 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
512 }
513 };
514
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700515 private RecyclerListener mRecycleListener = new RecyclerListener() {
516 @Override
517 public void onMovedToScrapHeap(View view) {
518 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
519 if (iconThumb != null) {
520 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
521 if (oldTask != null) {
522 oldTask.reallyCancel();
523 iconThumb.setTag(null);
524 }
525 }
526 }
527 };
528
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700529 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700530 Intent intent;
531 if (docs.size() == 1) {
532 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700533
Jeff Sharkey873daa32013-08-18 17:38:20 -0700534 intent = new Intent(Intent.ACTION_SEND);
535 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
536 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700537 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700538 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700539
540 } else if (docs.size() > 1) {
541 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
542 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
543 intent.addCategory(Intent.CATEGORY_DEFAULT);
544
545 final ArrayList<String> mimeTypes = Lists.newArrayList();
546 final ArrayList<Uri> uris = Lists.newArrayList();
547 for (DocumentInfo doc : docs) {
548 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700549 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700550 }
551
552 intent.setType(findCommonMimeType(mimeTypes));
553 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
554
555 } else {
556 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700557 }
558
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700559 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700560 startActivity(intent);
561 }
562
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700563 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700564 final Context context = getActivity();
565 final ContentResolver resolver = context.getContentResolver();
566
567 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700568 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700569 if (!doc.isDeleteSupported()) {
570 Log.w(TAG, "Skipping " + doc);
571 hadTrouble = true;
572 continue;
573 }
574
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700575 ContentProviderClient client = null;
576 try {
577 client = DocumentsApplication.acquireUnstableProviderOrThrow(
578 resolver, doc.derivedUri.getAuthority());
579 DocumentsContract.deleteDocument(client, doc.derivedUri);
580 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700581 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700582 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700583 } finally {
584 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700585 }
586 }
587
588 if (hadTrouble) {
589 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
590 }
591 }
592
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700593 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700594 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700595 }
596
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700597 private static abstract class Footer {
598 private final int mItemViewType;
599
600 public Footer(int itemViewType) {
601 mItemViewType = itemViewType;
602 }
603
604 public abstract View getView(View convertView, ViewGroup parent);
605
606 public int getItemViewType() {
607 return mItemViewType;
608 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700609 }
610
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700611 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700612 public LoadingFooter() {
613 super(1);
614 }
615
Jeff Sharkey20b32272013-09-03 15:25:52 -0700616 @Override
617 public View getView(View convertView, ViewGroup parent) {
618 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700619 final State state = getDisplayState(DirectoryFragment.this);
620
Jeff Sharkey20b32272013-09-03 15:25:52 -0700621 if (convertView == null) {
622 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700623 if (state.derivedMode == MODE_LIST) {
624 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
625 } else if (state.derivedMode == MODE_GRID) {
626 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
627 } else {
628 throw new IllegalStateException();
629 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700630 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700631
Jeff Sharkey20b32272013-09-03 15:25:52 -0700632 return convertView;
633 }
634 }
635
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700636 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700637 private final int mIcon;
638 private final String mMessage;
639
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700640 public MessageFooter(int itemViewType, int icon, String message) {
641 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700642 mIcon = icon;
643 mMessage = message;
644 }
645
646 @Override
647 public View getView(View convertView, ViewGroup parent) {
648 final Context context = parent.getContext();
649 final State state = getDisplayState(DirectoryFragment.this);
650
651 if (convertView == null) {
652 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700653 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700654 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700655 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700656 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
657 } else {
658 throw new IllegalStateException();
659 }
660 }
661
662 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
663 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
664 icon.setImageResource(mIcon);
665 title.setText(mMessage);
666 return convertView;
667 }
668 }
669
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700670 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700671 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700672 private int mCursorCount;
673
674 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700675
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700676 public void swapResult(DirectoryResult result) {
677 mCursor = result != null ? result.cursor : null;
678 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700679
680 mFooters.clear();
681
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700682 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700683 if (extras != null) {
684 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
685 if (info != null) {
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700686 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700687 }
688 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
689 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700690 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700691 }
692 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
693 mFooters.add(new LoadingFooter());
694 }
695 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700696
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700697 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700698 mFooters.add(new MessageFooter(
699 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
700 }
701
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700702 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700703 mEmptyView.setVisibility(View.VISIBLE);
704 } else {
705 mEmptyView.setVisibility(View.GONE);
706 }
707
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700708 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700709 }
710
711 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700712 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700713 if (position < mCursorCount) {
714 return getDocumentView(position, convertView, parent);
715 } else {
716 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700717 convertView = mFooters.get(position).getView(convertView, parent);
718 // Only the view itself is disabled; contents inside shouldn't
719 // be dimmed.
720 convertView.setEnabled(false);
721 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700722 }
723 }
724
725 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700726 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700727 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700728
Jeff Sharkey9656a532013-09-13 13:42:19 -0700729 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
730
Jeff Sharkey873daa32013-08-18 17:38:20 -0700731 final RootsCache roots = DocumentsApplication.getRootsCache(context);
732 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
733 context, mThumbSize);
734
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700735 if (convertView == null) {
736 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700737 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700738 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700739 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700740 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700741
742 // Apply padding to grid items
743 final FrameLayout grid = (FrameLayout) convertView;
744 final int gridPadding = getResources()
745 .getDimensionPixelSize(R.dimen.grid_padding);
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700746
747 // Tricksy hobbitses! We need to fully clear the drawable so
748 // the view doesn't clobber the new InsetDrawable callback
749 // when setting back later.
750 final Drawable fg = grid.getForeground();
751 final Drawable bg = grid.getBackground();
752 grid.setForeground(null);
753 grid.setBackground(null);
754 grid.setForeground(new InsetDrawable(fg, gridPadding));
755 grid.setBackground(new InsetDrawable(bg, gridPadding));
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700756 } else {
757 throw new IllegalStateException();
758 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700759 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700760
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700761 final Cursor cursor = getItem(position);
762
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700763 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
764 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700765 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
766 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
767 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
768 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
769 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
770 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
771 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
772 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700773
Jeff Sharkey9656a532013-09-13 13:42:19 -0700774 final View line1 = convertView.findViewById(R.id.line1);
775 final View line2 = convertView.findViewById(R.id.line2);
776
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700777 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
778 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700779 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
780 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700781 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700782 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
783 final TextView date = (TextView) convertView.findViewById(R.id.date);
784 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700785
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700786 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700787 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700788 oldTask.reallyCancel();
789 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700790 }
791
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700792 iconMime.animate().cancel();
793 iconThumb.animate().cancel();
794
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700795 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700796 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700797 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700798 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700799
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700800 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700801 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700802 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700803 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700804 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700805 iconThumb.setImageBitmap(cachedResult);
806 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700807 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700808 iconThumb.setImageDrawable(null);
809 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
810 uri, iconMime, iconThumb, mThumbSize);
811 iconThumb.setTag(task);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700812 task.executeOnExecutor(ProviderExecutor.forAuthority(docAuthority));
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700813 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700814 }
815
816 // Always throw MIME icon into place, even when a thumbnail is being
817 // loaded in background.
818 if (cacheHit) {
819 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700820 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700821 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700822 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700823 iconMime.setAlpha(1f);
824 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700825 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700826 if (docIcon != 0) {
827 iconMime.setImageDrawable(
828 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
829 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700830 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
831 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700832 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700833 }
834
Jeff Sharkey9656a532013-09-13 13:42:19 -0700835 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700836 boolean hasLine2 = false;
837
Jeff Sharkey9656a532013-09-13 13:42:19 -0700838 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
839 if (!hideTitle) {
840 title.setText(docDisplayName);
841 hasLine1 = true;
842 }
843
844 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700845 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700846 // We've already had to enumerate roots before any results can
847 // be shown, so this will never block.
848 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700849 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700850
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700851 if (summary != null) {
852 final boolean alwaysShowSummary = getResources()
853 .getBoolean(R.bool.always_show_summary);
854 if (alwaysShowSummary) {
855 summary.setText(root.getDirectoryString());
856 summary.setVisibility(View.VISIBLE);
857 hasLine2 = true;
858 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700859 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700860 // No summary needed if icon speaks for itself
861 summary.setVisibility(View.INVISIBLE);
862 } else {
863 summary.setText(root.getDirectoryString());
864 summary.setVisibility(View.VISIBLE);
865 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
866 hasLine2 = true;
867 }
868 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700869 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700870 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700871 // Directories showing thumbnails in grid mode get a little icon
872 // hint to remind user they're a directory.
873 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
874 && showThumbnail) {
875 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
876 }
877
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700878 if (summary != null) {
879 if (docSummary != null) {
880 summary.setText(docSummary);
881 summary.setVisibility(View.VISIBLE);
882 hasLine2 = true;
883 } else {
884 summary.setVisibility(View.INVISIBLE);
885 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700886 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700887 }
888
Jeff Sharkey9656a532013-09-13 13:42:19 -0700889 if (icon1 != null) icon1.setVisibility(View.GONE);
890 if (icon2 != null) icon2.setVisibility(View.GONE);
891
892 if (iconDrawable != null) {
893 if (hasLine1) {
894 icon1.setVisibility(View.VISIBLE);
895 icon1.setImageDrawable(iconDrawable);
896 } else {
897 icon2.setVisibility(View.VISIBLE);
898 icon2.setImageDrawable(iconDrawable);
899 }
900 }
901
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700902 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700903 date.setText(null);
904 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700905 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700906 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700907 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700908
909 if (state.showSize) {
910 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700911 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700912 size.setText(null);
913 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700914 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700915 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700916 }
917 } else {
918 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700919 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700920
Jeff Sharkey9656a532013-09-13 13:42:19 -0700921 if (line1 != null) {
922 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
923 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700924 if (line2 != null) {
925 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
926 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700927
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700928 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700929 if (enabled) {
930 setEnabledRecursive(convertView, true);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700931 iconMime.setAlpha(1f);
932 iconThumb.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700933 if (icon1 != null) icon1.setAlpha(1f);
934 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700935 } else {
936 setEnabledRecursive(convertView, false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700937 iconMime.setAlpha(0.5f);
938 iconThumb.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700939 if (icon1 != null) icon1.setAlpha(0.5f);
940 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700941 }
942
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700943 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700944 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700945
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700946 @Override
947 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700948 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700949 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700950
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700951 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700952 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700953 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700954 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700955 return mCursor;
956 } else {
957 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700958 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700959 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700960
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700961 @Override
962 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700963 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700964 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700965
966 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700967 public int getViewTypeCount() {
968 return 4;
969 }
970
971 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700972 public int getItemViewType(int position) {
973 if (position < mCursorCount) {
974 return 0;
975 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700976 position -= mCursorCount;
977 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700978 }
979 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700980 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700981
982 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700983 private final Uri mUri;
984 private final ImageView mIconMime;
985 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700986 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700987 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700988
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700989 public ThumbnailAsyncTask(
990 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
991 mUri = uri;
992 mIconMime = iconMime;
993 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700994 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700995 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700996 }
997
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700998 public void reallyCancel() {
999 cancel(false);
1000 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001001 }
1002
1003 @Override
1004 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001005 if (isCancelled()) return null;
1006
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001007 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001008 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001009
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001010 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001011 Bitmap result = null;
1012 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001013 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1014 resolver, mUri.getAuthority());
1015 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001016 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001017 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1018 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001019 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001020 }
1021 } catch (Exception e) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001022 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1023 } finally {
1024 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001025 }
1026 return result;
1027 }
1028
1029 @Override
1030 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001031 if (mIconThumb.getTag() == this && result != null) {
1032 mIconThumb.setTag(null);
1033 mIconThumb.setImageBitmap(result);
1034
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001035 final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
1036 mIconMime.setAlpha(targetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001037 mIconMime.animate().alpha(0f).start();
1038 mIconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001039 mIconThumb.animate().alpha(targetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001040 }
1041 }
1042 }
1043
1044 private static String formatTime(Context context, long when) {
1045 // TODO: DateUtils should make this easier
1046 Time then = new Time();
1047 then.set(when);
1048 Time now = new Time();
1049 now.setToNow();
1050
1051 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1052 | DateUtils.FORMAT_ABBREV_ALL;
1053
1054 if (then.year != now.year) {
1055 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1056 } else if (then.yearDay != now.yearDay) {
1057 flags |= DateUtils.FORMAT_SHOW_DATE;
1058 } else {
1059 flags |= DateUtils.FORMAT_SHOW_TIME;
1060 }
1061
1062 return DateUtils.formatDateTime(context, when, flags);
1063 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001064
1065 private String findCommonMimeType(List<String> mimeTypes) {
1066 String[] commonType = mimeTypes.get(0).split("/");
1067 if (commonType.length != 2) {
1068 return "*/*";
1069 }
1070
1071 for (int i = 1; i < mimeTypes.size(); i++) {
1072 String[] type = mimeTypes.get(i).split("/");
1073 if (type.length != 2) continue;
1074
1075 if (!commonType[1].equals(type[1])) {
1076 commonType[1] = "*";
1077 }
1078
1079 if (!commonType[0].equals(type[0])) {
1080 commonType[0] = "*";
1081 commonType[1] = "*";
1082 break;
1083 }
1084 }
1085
1086 return commonType[0] + "/" + commonType[1];
1087 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001088
1089 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001090 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001091 if (v.isEnabled() == enabled) return;
1092 v.setEnabled(enabled);
1093
1094 if (v instanceof ViewGroup) {
1095 final ViewGroup vg = (ViewGroup) v;
1096 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1097 setEnabledRecursive(vg.getChildAt(i), enabled);
1098 }
1099 }
1100 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001101
1102 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1103 final State state = getDisplayState(DirectoryFragment.this);
1104
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001105 // Directories are always enabled
1106 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1107 return true;
1108 }
1109
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001110 // Read-only files are disabled when creating
1111 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1112 return false;
1113 }
1114
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001115 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1116 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001117}