blob: b2b2bd85f783b2798c0982f88bed112e1ec1b395 [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 Sharkeye39a89b2013-10-29 11:56:37 -070050import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070051import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070052import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070053import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070054import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070055import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070056import android.text.format.Time;
57import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070058import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070059import android.util.SparseBooleanArray;
60import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070061import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070062import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070063import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070064import android.view.View;
65import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070066import android.widget.AbsListView;
67import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070068import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070069import android.widget.AdapterView;
70import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070071import android.widget.BaseAdapter;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070072import android.widget.FrameLayout;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070073import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070074import android.widget.ImageView;
75import android.widget.ListView;
76import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070077import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070078
Jeff Sharkeyb3620442013-09-01 18:41:04 -070079import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070080import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070081import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070082import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070083import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070084import com.google.android.collect.Lists;
85
86import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070087import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070088
89/**
90 * Display the documents inside a single directory.
91 */
92public class DirectoryFragment extends Fragment {
93
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070094 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070095 private ListView mListView;
96 private GridView mGridView;
97
Jeff Sharkeyc317af82013-07-01 16:56:54 -070098 private AbsListView mCurrentView;
99
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700100 public static final int TYPE_NORMAL = 1;
101 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700102 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700103
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700104 public static final int ANIM_NONE = 1;
105 public static final int ANIM_SIDE = 2;
106 public static final int ANIM_DOWN = 3;
107 public static final int ANIM_UP = 4;
108
Jeff Sharkey5b535922013-08-02 15:55:26 -0700109 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700110 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700111
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700112 private int mLastMode = MODE_UNKNOWN;
113 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700114 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700115
Jeff Sharkey9656a532013-09-13 13:42:19 -0700116 private boolean mHideGridTitles = false;
117
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700118 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700119 private Point mThumbSize;
120
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700121 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700122 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700123
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700124 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700125 private static final String EXTRA_ROOT = "root";
126 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700127 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700128 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700129
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700130 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700132 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
133 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134 }
135
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700136 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
137 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700138 }
139
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700140 public static void showRecentsOpen(FragmentManager fm, int anim) {
141 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700142 }
143
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700144 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
145 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700146 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700147 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700148 args.putParcelable(EXTRA_ROOT, root);
149 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700150 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700151
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700152 final FragmentTransaction ft = fm.beginTransaction();
153 switch (anim) {
154 case ANIM_SIDE:
155 args.putBoolean(EXTRA_IGNORE_STATE, true);
156 break;
157 case ANIM_DOWN:
158 args.putBoolean(EXTRA_IGNORE_STATE, true);
159 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
160 break;
161 case ANIM_UP:
162 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
163 break;
164 }
165
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700166 final DirectoryFragment fragment = new DirectoryFragment();
167 fragment.setArguments(args);
168
Jeff Sharkey76112212013-08-06 11:26:10 -0700169 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700170 ft.commitAllowingStateLoss();
171 }
172
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700173 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
174 final StringBuilder builder = new StringBuilder();
175 builder.append(root != null ? root.authority : "null").append(';');
176 builder.append(root != null ? root.rootId : "null").append(';');
177 builder.append(doc != null ? doc.documentId : "null");
178 return builder.toString();
179 }
180
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700181 public static DirectoryFragment get(FragmentManager fm) {
182 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700183 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700184 }
185
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700186 @Override
187 public View onCreateView(
188 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
189 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700190 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
191
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700192 mEmptyView = view.findViewById(android.R.id.empty);
193
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700194 mListView = (ListView) view.findViewById(R.id.list);
195 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700196 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700197 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700198
199 mGridView = (GridView) view.findViewById(R.id.grid);
200 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700201 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700202 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700203
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700204 return view;
205 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700206
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700207 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700208 public void onDestroyView() {
209 super.onDestroyView();
210
211 // Cancel any outstanding thumbnail requests
212 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
213 final int count = target.getChildCount();
214 for (int i = 0; i < count; i++) {
215 final View view = target.getChildAt(i);
216 mRecycleListener.onMovedToScrapHeap(view);
217 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700218
219 // Tear down any selection in progress
220 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
221 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700222 }
223
224 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700225 public void onActivityCreated(Bundle savedInstanceState) {
226 super.onActivityCreated(savedInstanceState);
227
228 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700229 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700230
Jeff Sharkey9656a532013-09-13 13:42:19 -0700231 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
232 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
233
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700234 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700235 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700236 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700237
Jeff Sharkey9656a532013-09-13 13:42:19 -0700238 if (mType == TYPE_RECENT_OPEN) {
239 // Hide titles when showing recents for picking images/videos
240 mHideGridTitles = MimePredicate.mimeMatches(
241 MimePredicate.VISUAL_MIMES, state.acceptMimes);
242 } else {
243 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
244 }
245
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700246 final ActivityManager am = (ActivityManager) context.getSystemService(
247 Context.ACTIVITY_SERVICE);
248 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
249
Jeff Sharkey46899c82013-08-18 22:26:48 -0700250 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700251 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700252 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700253 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700254
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700255 Uri contentsUri;
256 switch (mType) {
257 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700258 contentsUri = DocumentsContract.buildChildDocumentsUri(
259 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700260 if (state.action == ACTION_MANAGE) {
261 contentsUri = DocumentsContract.setManageMode(contentsUri);
262 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700263 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700264 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700265 case TYPE_SEARCH:
266 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700267 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700268 if (state.action == ACTION_MANAGE) {
269 contentsUri = DocumentsContract.setManageMode(contentsUri);
270 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700271 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700272 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700273 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700274 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700275 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700276 default:
277 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700278 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700279 }
280
281 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700282 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700283 if (!isAdded()) return;
284
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700285 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700286
287 // Push latest state up to UI
288 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700289 if (result.mode != MODE_UNKNOWN) {
290 state.derivedMode = result.mode;
291 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700292 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700293 ((DocumentsActivity) context).onStateChanged();
294
295 updateDisplayState();
296
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700297 // When launched into empty recents, show drawer
298 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
299 ((DocumentsActivity) context).setRootsDrawerOpen(true);
300 }
301
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700302 // Restore any previous instance state
303 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
304 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
305 getView().restoreHierarchyState(container);
306 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700307 mListView.smoothScrollToPosition(0);
308 mGridView.smoothScrollToPosition(0);
309 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700310
311 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700312 }
313
314 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700315 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700316 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700317 }
318 };
319
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700320 // Kick off loader at least once
321 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
322
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700323 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700324 }
325
Jeff Sharkey42d26792013-09-06 13:22:09 -0700326 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700327 public void onStop() {
328 super.onStop();
329
330 // Remember last scroll location
331 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
332 getView().saveHierarchyState(container);
333 final State state = getDisplayState(this);
334 state.dirState.put(mStateKey, container);
335 }
336
337 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700338 public void onResume() {
339 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700340 updateDisplayState();
341 }
342
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700343 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700344 // Sort order change always triggers reload; we'll trigger state change
345 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700346 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
347 }
348
349 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700350 final ContentResolver resolver = getActivity().getContentResolver();
351 final State state = getDisplayState(this);
352
353 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
354 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
355
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700356 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700357 final Uri stateUri = RecentsProvider.buildState(
358 root.authority, root.rootId, doc.documentId);
359 final ContentValues values = new ContentValues();
360 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700361
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700362 new AsyncTask<Void, Void, Void>() {
363 @Override
364 protected Void doInBackground(Void... params) {
365 resolver.insert(stateUri, values);
366 return null;
367 }
368 }.execute();
369 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700370
371 // Mode change is just visual change; no need to kick loader, and
372 // deliver change event immediately.
373 state.derivedMode = state.userMode;
374 ((DocumentsActivity) getActivity()).onStateChanged();
375
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700376 updateDisplayState();
377 }
378
379 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700380 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700381
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700382 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700383 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700384 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700385
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700386 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
387 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700388
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700389 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700390 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700391 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
392 } else {
393 choiceMode = ListView.CHOICE_MODE_NONE;
394 }
395
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700396 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700397 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700398 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700399 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700400 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700401 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700402 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700403 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700404 mGridView.setChoiceMode(choiceMode);
405 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700406 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700407 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700408 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700409 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700410 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700411 mListView.setChoiceMode(choiceMode);
412 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700413 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700414 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700415 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700416
417 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700418 }
419
420 private OnItemClickListener mItemListener = new OnItemClickListener() {
421 @Override
422 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700423 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700424 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700425 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
426 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
427 if (isDocumentEnabled(docMimeType, docFlags)) {
428 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700429 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
430 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700431 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700432 }
433 };
434
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700435 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
436 @Override
437 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
438 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
439 return true;
440 }
441
442 @Override
443 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700444 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700445
446 final MenuItem open = menu.findItem(R.id.menu_open);
447 final MenuItem share = menu.findItem(R.id.menu_share);
448 final MenuItem delete = menu.findItem(R.id.menu_delete);
449
450 final boolean manageMode = state.action == ACTION_MANAGE;
451 open.setVisible(!manageMode);
452 share.setVisible(manageMode);
453 delete.setVisible(manageMode);
454
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700455 return true;
456 }
457
458 @Override
459 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700460 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700461 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700462 final int size = checked.size();
463 for (int i = 0; i < size; i++) {
464 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700465 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700466 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700467 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700468 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700469 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700470
Jeff Sharkey873daa32013-08-18 17:38:20 -0700471 final int id = item.getItemId();
472 if (id == R.id.menu_open) {
473 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700474 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700475 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700476
477 } else if (id == R.id.menu_share) {
478 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700479 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700480 return true;
481
482 } else if (id == R.id.menu_delete) {
483 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700484 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700485 return true;
486
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700487 } else {
488 return false;
489 }
490 }
491
492 @Override
493 public void onDestroyActionMode(ActionMode mode) {
494 // ignored
495 }
496
497 @Override
498 public void onItemCheckedStateChanged(
499 ActionMode mode, int position, long id, boolean checked) {
500 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700501 // Directories and footer items cannot be checked
502 boolean valid = false;
503
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700504 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700505 if (cursor != null) {
506 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700507 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
508 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
509 valid = isDocumentEnabled(docMimeType, docFlags);
510 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700511 }
512
513 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700514 mCurrentView.setItemChecked(position, false);
515 }
516 }
517
518 mode.setTitle(getResources()
519 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
520 }
521 };
522
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700523 private RecyclerListener mRecycleListener = new RecyclerListener() {
524 @Override
525 public void onMovedToScrapHeap(View view) {
526 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
527 if (iconThumb != null) {
528 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
529 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700530 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700531 iconThumb.setTag(null);
532 }
533 }
534 }
535 };
536
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700537 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700538 Intent intent;
539 if (docs.size() == 1) {
540 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700541
Jeff Sharkey873daa32013-08-18 17:38:20 -0700542 intent = new Intent(Intent.ACTION_SEND);
543 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
544 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700545 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700546 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700547
548 } else if (docs.size() > 1) {
549 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
550 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
551 intent.addCategory(Intent.CATEGORY_DEFAULT);
552
553 final ArrayList<String> mimeTypes = Lists.newArrayList();
554 final ArrayList<Uri> uris = Lists.newArrayList();
555 for (DocumentInfo doc : docs) {
556 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700557 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700558 }
559
560 intent.setType(findCommonMimeType(mimeTypes));
561 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
562
563 } else {
564 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700565 }
566
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700567 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700568 startActivity(intent);
569 }
570
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700571 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700572 final Context context = getActivity();
573 final ContentResolver resolver = context.getContentResolver();
574
575 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700576 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700577 if (!doc.isDeleteSupported()) {
578 Log.w(TAG, "Skipping " + doc);
579 hadTrouble = true;
580 continue;
581 }
582
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700583 ContentProviderClient client = null;
584 try {
585 client = DocumentsApplication.acquireUnstableProviderOrThrow(
586 resolver, doc.derivedUri.getAuthority());
587 DocumentsContract.deleteDocument(client, doc.derivedUri);
588 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700589 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700590 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700591 } finally {
592 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700593 }
594 }
595
596 if (hadTrouble) {
597 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
598 }
599 }
600
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700601 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700602 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700603 }
604
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700605 private static abstract class Footer {
606 private final int mItemViewType;
607
608 public Footer(int itemViewType) {
609 mItemViewType = itemViewType;
610 }
611
612 public abstract View getView(View convertView, ViewGroup parent);
613
614 public int getItemViewType() {
615 return mItemViewType;
616 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700617 }
618
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700619 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700620 public LoadingFooter() {
621 super(1);
622 }
623
Jeff Sharkey20b32272013-09-03 15:25:52 -0700624 @Override
625 public View getView(View convertView, ViewGroup parent) {
626 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700627 final State state = getDisplayState(DirectoryFragment.this);
628
Jeff Sharkey20b32272013-09-03 15:25:52 -0700629 if (convertView == null) {
630 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700631 if (state.derivedMode == MODE_LIST) {
632 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
633 } else if (state.derivedMode == MODE_GRID) {
634 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
635 } else {
636 throw new IllegalStateException();
637 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700638 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700639
Jeff Sharkey20b32272013-09-03 15:25:52 -0700640 return convertView;
641 }
642 }
643
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700644 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700645 private final int mIcon;
646 private final String mMessage;
647
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700648 public MessageFooter(int itemViewType, int icon, String message) {
649 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700650 mIcon = icon;
651 mMessage = message;
652 }
653
654 @Override
655 public View getView(View convertView, ViewGroup parent) {
656 final Context context = parent.getContext();
657 final State state = getDisplayState(DirectoryFragment.this);
658
659 if (convertView == null) {
660 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700661 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700662 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700663 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700664 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
665 } else {
666 throw new IllegalStateException();
667 }
668 }
669
670 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
671 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
672 icon.setImageResource(mIcon);
673 title.setText(mMessage);
674 return convertView;
675 }
676 }
677
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700678 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700679 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700680 private int mCursorCount;
681
682 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700683
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700684 public void swapResult(DirectoryResult result) {
685 mCursor = result != null ? result.cursor : null;
686 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700687
688 mFooters.clear();
689
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700690 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700691 if (extras != null) {
692 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
693 if (info != null) {
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700694 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700695 }
696 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
697 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700698 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700699 }
700 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
701 mFooters.add(new LoadingFooter());
702 }
703 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700704
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700705 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700706 mFooters.add(new MessageFooter(
707 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
708 }
709
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700710 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700711 mEmptyView.setVisibility(View.VISIBLE);
712 } else {
713 mEmptyView.setVisibility(View.GONE);
714 }
715
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700716 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700717 }
718
719 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700720 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700721 if (position < mCursorCount) {
722 return getDocumentView(position, convertView, parent);
723 } else {
724 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700725 convertView = mFooters.get(position).getView(convertView, parent);
726 // Only the view itself is disabled; contents inside shouldn't
727 // be dimmed.
728 convertView.setEnabled(false);
729 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700730 }
731 }
732
733 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700734 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700735 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700736
Jeff Sharkey9656a532013-09-13 13:42:19 -0700737 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
738
Jeff Sharkey873daa32013-08-18 17:38:20 -0700739 final RootsCache roots = DocumentsApplication.getRootsCache(context);
740 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
741 context, mThumbSize);
742
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700743 if (convertView == null) {
744 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700745 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700746 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700747 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700748 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700749
750 // Apply padding to grid items
751 final FrameLayout grid = (FrameLayout) convertView;
752 final int gridPadding = getResources()
753 .getDimensionPixelSize(R.dimen.grid_padding);
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700754
755 // Tricksy hobbitses! We need to fully clear the drawable so
756 // the view doesn't clobber the new InsetDrawable callback
757 // when setting back later.
758 final Drawable fg = grid.getForeground();
759 final Drawable bg = grid.getBackground();
760 grid.setForeground(null);
761 grid.setBackground(null);
762 grid.setForeground(new InsetDrawable(fg, gridPadding));
763 grid.setBackground(new InsetDrawable(bg, gridPadding));
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700764 } else {
765 throw new IllegalStateException();
766 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700767 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700768
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700769 final Cursor cursor = getItem(position);
770
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700771 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
772 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700773 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
774 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
775 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
776 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
777 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
778 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
779 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
780 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700781
Jeff Sharkey9656a532013-09-13 13:42:19 -0700782 final View line1 = convertView.findViewById(R.id.line1);
783 final View line2 = convertView.findViewById(R.id.line2);
784
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700785 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
786 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700787 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
788 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700789 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700790 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
791 final TextView date = (TextView) convertView.findViewById(R.id.date);
792 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700793
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700794 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700795 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700796 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700797 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700798 }
799
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700800 iconMime.animate().cancel();
801 iconThumb.animate().cancel();
802
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700803 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700804 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700805 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700806 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700807
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700808 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700809 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700810 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700811 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700812 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700813 iconThumb.setImageBitmap(cachedResult);
814 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700815 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700816 iconThumb.setImageDrawable(null);
817 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
818 uri, iconMime, iconThumb, mThumbSize);
819 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700820 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700821 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700822 }
823
824 // Always throw MIME icon into place, even when a thumbnail is being
825 // loaded in background.
826 if (cacheHit) {
827 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700828 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700829 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700830 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700831 iconMime.setAlpha(1f);
832 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700833 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700834 if (docIcon != 0) {
835 iconMime.setImageDrawable(
836 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
837 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700838 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
839 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700840 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700841 }
842
Jeff Sharkey9656a532013-09-13 13:42:19 -0700843 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700844 boolean hasLine2 = false;
845
Jeff Sharkey9656a532013-09-13 13:42:19 -0700846 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
847 if (!hideTitle) {
848 title.setText(docDisplayName);
849 hasLine1 = true;
850 }
851
852 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700853 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700854 // We've already had to enumerate roots before any results can
855 // be shown, so this will never block.
856 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700857 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700858
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700859 if (summary != null) {
860 final boolean alwaysShowSummary = getResources()
861 .getBoolean(R.bool.always_show_summary);
862 if (alwaysShowSummary) {
863 summary.setText(root.getDirectoryString());
864 summary.setVisibility(View.VISIBLE);
865 hasLine2 = true;
866 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700867 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700868 // No summary needed if icon speaks for itself
869 summary.setVisibility(View.INVISIBLE);
870 } else {
871 summary.setText(root.getDirectoryString());
872 summary.setVisibility(View.VISIBLE);
873 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
874 hasLine2 = true;
875 }
876 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700877 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700878 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700879 // Directories showing thumbnails in grid mode get a little icon
880 // hint to remind user they're a directory.
881 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
882 && showThumbnail) {
883 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
884 }
885
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700886 if (summary != null) {
887 if (docSummary != null) {
888 summary.setText(docSummary);
889 summary.setVisibility(View.VISIBLE);
890 hasLine2 = true;
891 } else {
892 summary.setVisibility(View.INVISIBLE);
893 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700894 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700895 }
896
Jeff Sharkey9656a532013-09-13 13:42:19 -0700897 if (icon1 != null) icon1.setVisibility(View.GONE);
898 if (icon2 != null) icon2.setVisibility(View.GONE);
899
900 if (iconDrawable != null) {
901 if (hasLine1) {
902 icon1.setVisibility(View.VISIBLE);
903 icon1.setImageDrawable(iconDrawable);
904 } else {
905 icon2.setVisibility(View.VISIBLE);
906 icon2.setImageDrawable(iconDrawable);
907 }
908 }
909
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700910 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700911 date.setText(null);
912 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700913 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700914 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700915 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700916
917 if (state.showSize) {
918 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700919 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700920 size.setText(null);
921 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700922 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700923 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700924 }
925 } else {
926 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700927 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700928
Jeff Sharkey9656a532013-09-13 13:42:19 -0700929 if (line1 != null) {
930 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
931 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700932 if (line2 != null) {
933 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
934 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700935
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700936 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700937 if (enabled) {
938 setEnabledRecursive(convertView, true);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700939 iconMime.setAlpha(1f);
940 iconThumb.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700941 if (icon1 != null) icon1.setAlpha(1f);
942 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700943 } else {
944 setEnabledRecursive(convertView, false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700945 iconMime.setAlpha(0.5f);
946 iconThumb.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700947 if (icon1 != null) icon1.setAlpha(0.5f);
948 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700949 }
950
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700951 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700952 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700953
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700954 @Override
955 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700956 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700957 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700958
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700959 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700960 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700961 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700962 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700963 return mCursor;
964 } else {
965 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700966 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700967 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700968
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700969 @Override
970 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700971 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700972 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700973
974 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700975 public int getViewTypeCount() {
976 return 4;
977 }
978
979 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700980 public int getItemViewType(int position) {
981 if (position < mCursorCount) {
982 return 0;
983 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700984 position -= mCursorCount;
985 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700986 }
987 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700988 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700989
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700990 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
991 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700992 private final Uri mUri;
993 private final ImageView mIconMime;
994 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700995 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700996 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700997
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700998 public ThumbnailAsyncTask(
999 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
1000 mUri = uri;
1001 mIconMime = iconMime;
1002 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001003 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001004 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001005 }
1006
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001007 @Override
1008 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001009 cancel(false);
1010 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001011 }
1012
1013 @Override
1014 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001015 if (isCancelled()) return null;
1016
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001017 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001018 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001019
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001020 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001021 Bitmap result = null;
1022 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001023 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1024 resolver, mUri.getAuthority());
1025 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001026 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001027 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1028 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001029 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001030 }
1031 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001032 if (!(e instanceof OperationCanceledException)) {
1033 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1034 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001035 } finally {
1036 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001037 }
1038 return result;
1039 }
1040
1041 @Override
1042 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001043 if (mIconThumb.getTag() == this && result != null) {
1044 mIconThumb.setTag(null);
1045 mIconThumb.setImageBitmap(result);
1046
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001047 final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
1048 mIconMime.setAlpha(targetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001049 mIconMime.animate().alpha(0f).start();
1050 mIconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001051 mIconThumb.animate().alpha(targetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001052 }
1053 }
1054 }
1055
1056 private static String formatTime(Context context, long when) {
1057 // TODO: DateUtils should make this easier
1058 Time then = new Time();
1059 then.set(when);
1060 Time now = new Time();
1061 now.setToNow();
1062
1063 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1064 | DateUtils.FORMAT_ABBREV_ALL;
1065
1066 if (then.year != now.year) {
1067 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1068 } else if (then.yearDay != now.yearDay) {
1069 flags |= DateUtils.FORMAT_SHOW_DATE;
1070 } else {
1071 flags |= DateUtils.FORMAT_SHOW_TIME;
1072 }
1073
1074 return DateUtils.formatDateTime(context, when, flags);
1075 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001076
1077 private String findCommonMimeType(List<String> mimeTypes) {
1078 String[] commonType = mimeTypes.get(0).split("/");
1079 if (commonType.length != 2) {
1080 return "*/*";
1081 }
1082
1083 for (int i = 1; i < mimeTypes.size(); i++) {
1084 String[] type = mimeTypes.get(i).split("/");
1085 if (type.length != 2) continue;
1086
1087 if (!commonType[1].equals(type[1])) {
1088 commonType[1] = "*";
1089 }
1090
1091 if (!commonType[0].equals(type[0])) {
1092 commonType[0] = "*";
1093 commonType[1] = "*";
1094 break;
1095 }
1096 }
1097
1098 return commonType[0] + "/" + commonType[1];
1099 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001100
1101 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001102 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001103 if (v.isEnabled() == enabled) return;
1104 v.setEnabled(enabled);
1105
1106 if (v instanceof ViewGroup) {
1107 final ViewGroup vg = (ViewGroup) v;
1108 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1109 setEnabledRecursive(vg.getChildAt(i), enabled);
1110 }
1111 }
1112 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001113
1114 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1115 final State state = getDisplayState(DirectoryFragment.this);
1116
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001117 // Directories are always enabled
1118 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1119 return true;
1120 }
1121
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001122 // Read-only files are disabled when creating
1123 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1124 return false;
1125 }
1126
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001127 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1128 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001129}