blob: 0a7eedc5a9286e955a0ce24fc624bda115c76cc5 [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 Sharkey083d7e12014-07-27 21:01:45 -070041import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070042import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070043import android.graphics.Bitmap;
44import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070045import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070046import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070048import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070049import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070050import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070051import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070052import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070054import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070055import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070056import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070057import android.text.format.Time;
58import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070059import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070060import android.util.SparseBooleanArray;
61import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070063import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070064import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070065import android.view.View;
66import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070067import android.widget.AbsListView;
68import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070069import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070070import android.widget.AdapterView;
71import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070072import android.widget.BaseAdapter;
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 Sharkey083d7e12014-07-27 21:01:45 -0700190 final Resources res = context.getResources();
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
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700200 // Indent our list divider to align with text
201 final Drawable divider = mListView.getDivider();
202 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
203 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
204 if (insetLeft) {
205 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
206 } else {
207 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
208 }
209
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700210 mGridView = (GridView) view.findViewById(R.id.grid);
211 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700212 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700213 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700214
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700215 return view;
216 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700218 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700219 public void onDestroyView() {
220 super.onDestroyView();
221
222 // Cancel any outstanding thumbnail requests
223 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
224 final int count = target.getChildCount();
225 for (int i = 0; i < count; i++) {
226 final View view = target.getChildAt(i);
227 mRecycleListener.onMovedToScrapHeap(view);
228 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700229
230 // Tear down any selection in progress
231 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
232 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700233 }
234
235 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700236 public void onActivityCreated(Bundle savedInstanceState) {
237 super.onActivityCreated(savedInstanceState);
238
239 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700240 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700241
Jeff Sharkey9656a532013-09-13 13:42:19 -0700242 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
243 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
244
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700245 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700246 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700247 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700248
Jeff Sharkey9656a532013-09-13 13:42:19 -0700249 if (mType == TYPE_RECENT_OPEN) {
250 // Hide titles when showing recents for picking images/videos
251 mHideGridTitles = MimePredicate.mimeMatches(
252 MimePredicate.VISUAL_MIMES, state.acceptMimes);
253 } else {
254 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
255 }
256
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700257 final ActivityManager am = (ActivityManager) context.getSystemService(
258 Context.ACTIVITY_SERVICE);
259 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
260
Jeff Sharkey46899c82013-08-18 22:26:48 -0700261 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700262 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700263 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700264 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700265
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700266 Uri contentsUri;
267 switch (mType) {
268 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700269 contentsUri = DocumentsContract.buildChildDocumentsUri(
270 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700271 if (state.action == ACTION_MANAGE) {
272 contentsUri = DocumentsContract.setManageMode(contentsUri);
273 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700274 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700275 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700276 case TYPE_SEARCH:
277 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700278 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700279 if (state.action == ACTION_MANAGE) {
280 contentsUri = DocumentsContract.setManageMode(contentsUri);
281 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700282 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700283 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700284 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700285 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700286 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700287 default:
288 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700289 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700290 }
291
292 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700293 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700294 if (!isAdded()) return;
295
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700296 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700297
298 // Push latest state up to UI
299 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700300 if (result.mode != MODE_UNKNOWN) {
301 state.derivedMode = result.mode;
302 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700303 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700304 ((DocumentsActivity) context).onStateChanged();
305
306 updateDisplayState();
307
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700308 // When launched into empty recents, show drawer
309 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
310 ((DocumentsActivity) context).setRootsDrawerOpen(true);
311 }
312
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700313 // Restore any previous instance state
314 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
315 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
316 getView().restoreHierarchyState(container);
317 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700318 mListView.smoothScrollToPosition(0);
319 mGridView.smoothScrollToPosition(0);
320 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700321
322 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700323 }
324
325 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700326 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700327 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700328 }
329 };
330
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700331 // Kick off loader at least once
332 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
333
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700334 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700335 }
336
Jeff Sharkey42d26792013-09-06 13:22:09 -0700337 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700338 public void onStop() {
339 super.onStop();
340
341 // Remember last scroll location
342 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
343 getView().saveHierarchyState(container);
344 final State state = getDisplayState(this);
345 state.dirState.put(mStateKey, container);
346 }
347
348 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700349 public void onResume() {
350 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700351 updateDisplayState();
352 }
353
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700354 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700355 // Sort order change always triggers reload; we'll trigger state change
356 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700357 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
358 }
359
360 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700361 final ContentResolver resolver = getActivity().getContentResolver();
362 final State state = getDisplayState(this);
363
364 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
365 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
366
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700367 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700368 final Uri stateUri = RecentsProvider.buildState(
369 root.authority, root.rootId, doc.documentId);
370 final ContentValues values = new ContentValues();
371 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700372
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700373 new AsyncTask<Void, Void, Void>() {
374 @Override
375 protected Void doInBackground(Void... params) {
376 resolver.insert(stateUri, values);
377 return null;
378 }
379 }.execute();
380 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700381
382 // Mode change is just visual change; no need to kick loader, and
383 // deliver change event immediately.
384 state.derivedMode = state.userMode;
385 ((DocumentsActivity) getActivity()).onStateChanged();
386
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700387 updateDisplayState();
388 }
389
390 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700391 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700392
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700393 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700394 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700395 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700396
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700397 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
398 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700399
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700400 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700401 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700402 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
403 } else {
404 choiceMode = ListView.CHOICE_MODE_NONE;
405 }
406
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700407 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700408 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700409 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700410 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700411 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700412 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700413 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700414 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700415 mGridView.setChoiceMode(choiceMode);
416 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700417 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700418 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700419 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700420 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700421 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700422 mListView.setChoiceMode(choiceMode);
423 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700424 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700425 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700426 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700427
428 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700429 }
430
431 private OnItemClickListener mItemListener = new OnItemClickListener() {
432 @Override
433 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700434 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700435 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700436 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
437 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
438 if (isDocumentEnabled(docMimeType, docFlags)) {
439 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700440 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
441 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700442 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700443 }
444 };
445
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700446 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
447 @Override
448 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
449 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100450 mode.setTitle(getResources()
451 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700452 return true;
453 }
454
455 @Override
456 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700457 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700458
459 final MenuItem open = menu.findItem(R.id.menu_open);
460 final MenuItem share = menu.findItem(R.id.menu_share);
461 final MenuItem delete = menu.findItem(R.id.menu_delete);
462
463 final boolean manageMode = state.action == ACTION_MANAGE;
464 open.setVisible(!manageMode);
465 share.setVisible(manageMode);
466 delete.setVisible(manageMode);
467
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700468 return true;
469 }
470
471 @Override
472 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700473 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700474 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700475 final int size = checked.size();
476 for (int i = 0; i < size; i++) {
477 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700478 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700479 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700480 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700481 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700482 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700483
Jeff Sharkey873daa32013-08-18 17:38:20 -0700484 final int id = item.getItemId();
485 if (id == R.id.menu_open) {
486 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700487 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700488 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700489
490 } else if (id == R.id.menu_share) {
491 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700492 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700493 return true;
494
495 } else if (id == R.id.menu_delete) {
496 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700497 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700498 return true;
499
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700500 } else {
501 return false;
502 }
503 }
504
505 @Override
506 public void onDestroyActionMode(ActionMode mode) {
507 // ignored
508 }
509
510 @Override
511 public void onItemCheckedStateChanged(
512 ActionMode mode, int position, long id, boolean checked) {
513 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700514 // Directories and footer items cannot be checked
515 boolean valid = false;
516
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700517 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700518 if (cursor != null) {
519 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700520 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
521 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
522 valid = isDocumentEnabled(docMimeType, docFlags);
523 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700524 }
525
526 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700527 mCurrentView.setItemChecked(position, false);
528 }
529 }
530
531 mode.setTitle(getResources()
532 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
533 }
534 };
535
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700536 private RecyclerListener mRecycleListener = new RecyclerListener() {
537 @Override
538 public void onMovedToScrapHeap(View view) {
539 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
540 if (iconThumb != null) {
541 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
542 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700543 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700544 iconThumb.setTag(null);
545 }
546 }
547 }
548 };
549
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700550 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700551 Intent intent;
552 if (docs.size() == 1) {
553 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700554
Jeff Sharkey873daa32013-08-18 17:38:20 -0700555 intent = new Intent(Intent.ACTION_SEND);
556 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
557 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700558 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700559 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700560
561 } else if (docs.size() > 1) {
562 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
563 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
564 intent.addCategory(Intent.CATEGORY_DEFAULT);
565
566 final ArrayList<String> mimeTypes = Lists.newArrayList();
567 final ArrayList<Uri> uris = Lists.newArrayList();
568 for (DocumentInfo doc : docs) {
569 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700570 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700571 }
572
573 intent.setType(findCommonMimeType(mimeTypes));
574 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
575
576 } else {
577 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700578 }
579
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700580 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700581 startActivity(intent);
582 }
583
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700584 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700585 final Context context = getActivity();
586 final ContentResolver resolver = context.getContentResolver();
587
588 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700589 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700590 if (!doc.isDeleteSupported()) {
591 Log.w(TAG, "Skipping " + doc);
592 hadTrouble = true;
593 continue;
594 }
595
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700596 ContentProviderClient client = null;
597 try {
598 client = DocumentsApplication.acquireUnstableProviderOrThrow(
599 resolver, doc.derivedUri.getAuthority());
600 DocumentsContract.deleteDocument(client, doc.derivedUri);
601 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700602 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700603 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700604 } finally {
605 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700606 }
607 }
608
609 if (hadTrouble) {
610 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
611 }
612 }
613
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700614 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700615 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700616 }
617
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700618 private static abstract class Footer {
619 private final int mItemViewType;
620
621 public Footer(int itemViewType) {
622 mItemViewType = itemViewType;
623 }
624
625 public abstract View getView(View convertView, ViewGroup parent);
626
627 public int getItemViewType() {
628 return mItemViewType;
629 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700630 }
631
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700632 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700633 public LoadingFooter() {
634 super(1);
635 }
636
Jeff Sharkey20b32272013-09-03 15:25:52 -0700637 @Override
638 public View getView(View convertView, ViewGroup parent) {
639 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700640 final State state = getDisplayState(DirectoryFragment.this);
641
Jeff Sharkey20b32272013-09-03 15:25:52 -0700642 if (convertView == null) {
643 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700644 if (state.derivedMode == MODE_LIST) {
645 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
646 } else if (state.derivedMode == MODE_GRID) {
647 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
648 } else {
649 throw new IllegalStateException();
650 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700651 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700652
Jeff Sharkey20b32272013-09-03 15:25:52 -0700653 return convertView;
654 }
655 }
656
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700657 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700658 private final int mIcon;
659 private final String mMessage;
660
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700661 public MessageFooter(int itemViewType, int icon, String message) {
662 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700663 mIcon = icon;
664 mMessage = message;
665 }
666
667 @Override
668 public View getView(View convertView, ViewGroup parent) {
669 final Context context = parent.getContext();
670 final State state = getDisplayState(DirectoryFragment.this);
671
672 if (convertView == null) {
673 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700674 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700675 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700676 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700677 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
678 } else {
679 throw new IllegalStateException();
680 }
681 }
682
683 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
684 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
685 icon.setImageResource(mIcon);
686 title.setText(mMessage);
687 return convertView;
688 }
689 }
690
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700691 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700692 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700693 private int mCursorCount;
694
695 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700696
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700697 public void swapResult(DirectoryResult result) {
698 mCursor = result != null ? result.cursor : null;
699 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700700
701 mFooters.clear();
702
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700703 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700704 if (extras != null) {
705 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
706 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700707 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700708 }
709 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
710 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700711 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700712 }
713 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
714 mFooters.add(new LoadingFooter());
715 }
716 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700717
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700718 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700719 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700720 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700721 }
722
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700723 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700724 mEmptyView.setVisibility(View.VISIBLE);
725 } else {
726 mEmptyView.setVisibility(View.GONE);
727 }
728
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700729 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700730 }
731
732 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700733 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700734 if (position < mCursorCount) {
735 return getDocumentView(position, convertView, parent);
736 } else {
737 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700738 convertView = mFooters.get(position).getView(convertView, parent);
739 // Only the view itself is disabled; contents inside shouldn't
740 // be dimmed.
741 convertView.setEnabled(false);
742 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700743 }
744 }
745
746 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700747 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700748 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700749
Jeff Sharkey9656a532013-09-13 13:42:19 -0700750 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
751
Jeff Sharkey873daa32013-08-18 17:38:20 -0700752 final RootsCache roots = DocumentsApplication.getRootsCache(context);
753 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
754 context, mThumbSize);
755
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700756 if (convertView == null) {
757 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700758 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700759 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700760 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700761 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
762 } else {
763 throw new IllegalStateException();
764 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700765 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700766
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700767 final Cursor cursor = getItem(position);
768
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700769 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
770 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700771 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
772 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
773 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
774 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
775 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
776 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
777 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
778 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700779
Jeff Sharkey9656a532013-09-13 13:42:19 -0700780 final View line1 = convertView.findViewById(R.id.line1);
781 final View line2 = convertView.findViewById(R.id.line2);
782
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700783 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
784 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700785 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
786 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700787 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700788 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
789 final TextView date = (TextView) convertView.findViewById(R.id.date);
790 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700791
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700792 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700793 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700794 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700795 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700796 }
797
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700798 iconMime.animate().cancel();
799 iconThumb.animate().cancel();
800
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700801 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700802 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700803 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700804 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700805
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700806 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700807 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700808 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700809 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700810 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700811 iconThumb.setImageBitmap(cachedResult);
812 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700813 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700814 iconThumb.setImageDrawable(null);
815 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
816 uri, iconMime, iconThumb, mThumbSize);
817 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700818 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700819 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700820 }
821
822 // Always throw MIME icon into place, even when a thumbnail is being
823 // loaded in background.
824 if (cacheHit) {
825 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700826 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700827 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700828 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700829 iconMime.setAlpha(1f);
830 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700831 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700832 if (docIcon != 0) {
833 iconMime.setImageDrawable(
834 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
835 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700836 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
837 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700838 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700839 }
840
Jeff Sharkey9656a532013-09-13 13:42:19 -0700841 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700842 boolean hasLine2 = false;
843
Jeff Sharkey9656a532013-09-13 13:42:19 -0700844 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
845 if (!hideTitle) {
846 title.setText(docDisplayName);
847 hasLine1 = true;
848 }
849
850 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700851 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700852 // We've already had to enumerate roots before any results can
853 // be shown, so this will never block.
854 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700855 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700856 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700857 } else {
858 iconDrawable = root.loadIcon(context);
859 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700860
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700861 if (summary != null) {
862 final boolean alwaysShowSummary = getResources()
863 .getBoolean(R.bool.always_show_summary);
864 if (alwaysShowSummary) {
865 summary.setText(root.getDirectoryString());
866 summary.setVisibility(View.VISIBLE);
867 hasLine2 = true;
868 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700869 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700870 // No summary needed if icon speaks for itself
871 summary.setVisibility(View.INVISIBLE);
872 } else {
873 summary.setText(root.getDirectoryString());
874 summary.setVisibility(View.VISIBLE);
875 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
876 hasLine2 = true;
877 }
878 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700879 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700880 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700881 // Directories showing thumbnails in grid mode get a little icon
882 // hint to remind user they're a directory.
883 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
884 && showThumbnail) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700885 iconDrawable = IconUtils.applyTint(context, R.drawable.ic_doc_folder,
886 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700887 }
888
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700889 if (summary != null) {
890 if (docSummary != null) {
891 summary.setText(docSummary);
892 summary.setVisibility(View.VISIBLE);
893 hasLine2 = true;
894 } else {
895 summary.setVisibility(View.INVISIBLE);
896 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700897 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700898 }
899
Jeff Sharkey9656a532013-09-13 13:42:19 -0700900 if (icon1 != null) icon1.setVisibility(View.GONE);
901 if (icon2 != null) icon2.setVisibility(View.GONE);
902
903 if (iconDrawable != null) {
904 if (hasLine1) {
905 icon1.setVisibility(View.VISIBLE);
906 icon1.setImageDrawable(iconDrawable);
907 } else {
908 icon2.setVisibility(View.VISIBLE);
909 icon2.setImageDrawable(iconDrawable);
910 }
911 }
912
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700913 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700914 date.setText(null);
915 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700916 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700917 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700918 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700919
920 if (state.showSize) {
921 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700922 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700923 size.setText(null);
924 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700925 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700926 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700927 }
928 } else {
929 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700930 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700931
Jeff Sharkey9656a532013-09-13 13:42:19 -0700932 if (line1 != null) {
933 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
934 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700935 if (line2 != null) {
936 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
937 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700938
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700939 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700940 if (enabled) {
941 setEnabledRecursive(convertView, true);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700942 iconMime.setAlpha(1f);
943 iconThumb.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700944 if (icon1 != null) icon1.setAlpha(1f);
945 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700946 } else {
947 setEnabledRecursive(convertView, false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700948 iconMime.setAlpha(0.5f);
949 iconThumb.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700950 if (icon1 != null) icon1.setAlpha(0.5f);
951 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700952 }
953
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700954 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700955 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700956
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700957 @Override
958 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700959 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700960 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700961
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700962 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700963 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700964 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700965 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700966 return mCursor;
967 } else {
968 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700969 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700970 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700971
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700972 @Override
973 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700974 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700975 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700976
977 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700978 public int getViewTypeCount() {
979 return 4;
980 }
981
982 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700983 public int getItemViewType(int position) {
984 if (position < mCursorCount) {
985 return 0;
986 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700987 position -= mCursorCount;
988 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700989 }
990 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700991 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700992
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700993 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
994 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700995 private final Uri mUri;
996 private final ImageView mIconMime;
997 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700998 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700999 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001000
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001001 public ThumbnailAsyncTask(
1002 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
1003 mUri = uri;
1004 mIconMime = iconMime;
1005 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001006 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001007 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001008 }
1009
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001010 @Override
1011 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001012 cancel(false);
1013 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001014 }
1015
1016 @Override
1017 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001018 if (isCancelled()) return null;
1019
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001020 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001021 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001022
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001023 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001024 Bitmap result = null;
1025 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001026 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1027 resolver, mUri.getAuthority());
1028 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001029 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001030 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1031 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001032 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001033 }
1034 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001035 if (!(e instanceof OperationCanceledException)) {
1036 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1037 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001038 } finally {
1039 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001040 }
1041 return result;
1042 }
1043
1044 @Override
1045 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001046 if (mIconThumb.getTag() == this && result != null) {
1047 mIconThumb.setTag(null);
1048 mIconThumb.setImageBitmap(result);
1049
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001050 final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
1051 mIconMime.setAlpha(targetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001052 mIconMime.animate().alpha(0f).start();
1053 mIconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001054 mIconThumb.animate().alpha(targetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001055 }
1056 }
1057 }
1058
1059 private static String formatTime(Context context, long when) {
1060 // TODO: DateUtils should make this easier
1061 Time then = new Time();
1062 then.set(when);
1063 Time now = new Time();
1064 now.setToNow();
1065
1066 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1067 | DateUtils.FORMAT_ABBREV_ALL;
1068
1069 if (then.year != now.year) {
1070 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1071 } else if (then.yearDay != now.yearDay) {
1072 flags |= DateUtils.FORMAT_SHOW_DATE;
1073 } else {
1074 flags |= DateUtils.FORMAT_SHOW_TIME;
1075 }
1076
1077 return DateUtils.formatDateTime(context, when, flags);
1078 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001079
1080 private String findCommonMimeType(List<String> mimeTypes) {
1081 String[] commonType = mimeTypes.get(0).split("/");
1082 if (commonType.length != 2) {
1083 return "*/*";
1084 }
1085
1086 for (int i = 1; i < mimeTypes.size(); i++) {
1087 String[] type = mimeTypes.get(i).split("/");
1088 if (type.length != 2) continue;
1089
1090 if (!commonType[1].equals(type[1])) {
1091 commonType[1] = "*";
1092 }
1093
1094 if (!commonType[0].equals(type[0])) {
1095 commonType[0] = "*";
1096 commonType[1] = "*";
1097 break;
1098 }
1099 }
1100
1101 return commonType[0] + "/" + commonType[1];
1102 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001103
1104 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001105 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001106 if (v.isEnabled() == enabled) return;
1107 v.setEnabled(enabled);
1108
1109 if (v instanceof ViewGroup) {
1110 final ViewGroup vg = (ViewGroup) v;
1111 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1112 setEnabledRecursive(vg.getChildAt(i), enabled);
1113 }
1114 }
1115 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001116
1117 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1118 final State state = getDisplayState(DirectoryFragment.this);
1119
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001120 // Directories are always enabled
1121 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1122 return true;
1123 }
1124
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001125 // Read-only files are disabled when creating
1126 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1127 return false;
1128 }
1129
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001130 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1131 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001132}