blob: 001cac4638341302e2fdb89ab32ed8d70e272d75 [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 Sharkeydb884f52013-09-21 15:11:19 -070073import android.widget.FrameLayout;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070074import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070075import android.widget.ImageView;
76import android.widget.ListView;
77import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070078import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070079
Jeff Sharkeyb3620442013-09-01 18:41:04 -070080import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070081import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070082import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070083import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070084import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070085import com.google.android.collect.Lists;
86
87import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070088import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070089
90/**
91 * Display the documents inside a single directory.
92 */
93public class DirectoryFragment extends Fragment {
94
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070095 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070096 private ListView mListView;
97 private GridView mGridView;
98
Jeff Sharkeyc317af82013-07-01 16:56:54 -070099 private AbsListView mCurrentView;
100
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700101 public static final int TYPE_NORMAL = 1;
102 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700103 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700104
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700105 public static final int ANIM_NONE = 1;
106 public static final int ANIM_SIDE = 2;
107 public static final int ANIM_DOWN = 3;
108 public static final int ANIM_UP = 4;
109
Jeff Sharkey5b535922013-08-02 15:55:26 -0700110 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700111 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700112
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700113 private int mLastMode = MODE_UNKNOWN;
114 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700115 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700116
Jeff Sharkey9656a532013-09-13 13:42:19 -0700117 private boolean mHideGridTitles = false;
118
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700119 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700120 private Point mThumbSize;
121
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700122 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700123 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700124
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700125 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700126 private static final String EXTRA_ROOT = "root";
127 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700128 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700129 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700130
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700131 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700132
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700133 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
134 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700135 }
136
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700137 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
138 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700139 }
140
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700141 public static void showRecentsOpen(FragmentManager fm, int anim) {
142 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700143 }
144
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700145 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
146 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700147 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700148 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700149 args.putParcelable(EXTRA_ROOT, root);
150 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700151 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700152
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700153 final FragmentTransaction ft = fm.beginTransaction();
154 switch (anim) {
155 case ANIM_SIDE:
156 args.putBoolean(EXTRA_IGNORE_STATE, true);
157 break;
158 case ANIM_DOWN:
159 args.putBoolean(EXTRA_IGNORE_STATE, true);
160 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
161 break;
162 case ANIM_UP:
163 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
164 break;
165 }
166
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700167 final DirectoryFragment fragment = new DirectoryFragment();
168 fragment.setArguments(args);
169
Jeff Sharkey76112212013-08-06 11:26:10 -0700170 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700171 ft.commitAllowingStateLoss();
172 }
173
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700174 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
175 final StringBuilder builder = new StringBuilder();
176 builder.append(root != null ? root.authority : "null").append(';');
177 builder.append(root != null ? root.rootId : "null").append(';');
178 builder.append(doc != null ? doc.documentId : "null");
179 return builder.toString();
180 }
181
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700182 public static DirectoryFragment get(FragmentManager fm) {
183 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700184 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700185 }
186
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700187 @Override
188 public View onCreateView(
189 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
190 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700191 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700192 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
193
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700194 mEmptyView = view.findViewById(android.R.id.empty);
195
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700196 mListView = (ListView) view.findViewById(R.id.list);
197 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700198 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700199 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700200
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700201 // Indent our list divider to align with text
202 final Drawable divider = mListView.getDivider();
203 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
204 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
205 if (insetLeft) {
206 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
207 } else {
208 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
209 }
210
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700211 mGridView = (GridView) view.findViewById(R.id.grid);
212 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700213 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700214 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700215
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700216 return view;
217 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700218
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700219 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700220 public void onDestroyView() {
221 super.onDestroyView();
222
223 // Cancel any outstanding thumbnail requests
224 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
225 final int count = target.getChildCount();
226 for (int i = 0; i < count; i++) {
227 final View view = target.getChildAt(i);
228 mRecycleListener.onMovedToScrapHeap(view);
229 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700230
231 // Tear down any selection in progress
232 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
233 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700234 }
235
236 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700237 public void onActivityCreated(Bundle savedInstanceState) {
238 super.onActivityCreated(savedInstanceState);
239
240 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700241 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700242
Jeff Sharkey9656a532013-09-13 13:42:19 -0700243 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
244 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
245
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700246 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700247 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700248 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700249
Jeff Sharkey9656a532013-09-13 13:42:19 -0700250 if (mType == TYPE_RECENT_OPEN) {
251 // Hide titles when showing recents for picking images/videos
252 mHideGridTitles = MimePredicate.mimeMatches(
253 MimePredicate.VISUAL_MIMES, state.acceptMimes);
254 } else {
255 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
256 }
257
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700258 final ActivityManager am = (ActivityManager) context.getSystemService(
259 Context.ACTIVITY_SERVICE);
260 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
261
Jeff Sharkey46899c82013-08-18 22:26:48 -0700262 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700263 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700264 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700265 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700266
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700267 Uri contentsUri;
268 switch (mType) {
269 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700270 contentsUri = DocumentsContract.buildChildDocumentsUri(
271 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700272 if (state.action == ACTION_MANAGE) {
273 contentsUri = DocumentsContract.setManageMode(contentsUri);
274 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700275 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700276 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700277 case TYPE_SEARCH:
278 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700279 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700280 if (state.action == ACTION_MANAGE) {
281 contentsUri = DocumentsContract.setManageMode(contentsUri);
282 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700283 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700284 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700285 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700286 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700287 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700288 default:
289 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700290 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700291 }
292
293 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700294 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700295 if (!isAdded()) return;
296
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700297 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700298
299 // Push latest state up to UI
300 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700301 if (result.mode != MODE_UNKNOWN) {
302 state.derivedMode = result.mode;
303 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700304 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700305 ((DocumentsActivity) context).onStateChanged();
306
307 updateDisplayState();
308
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700309 // When launched into empty recents, show drawer
310 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
311 ((DocumentsActivity) context).setRootsDrawerOpen(true);
312 }
313
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700314 // Restore any previous instance state
315 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
316 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
317 getView().restoreHierarchyState(container);
318 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700319 mListView.smoothScrollToPosition(0);
320 mGridView.smoothScrollToPosition(0);
321 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700322
323 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700324 }
325
326 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700327 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700328 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700329 }
330 };
331
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700332 // Kick off loader at least once
333 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
334
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700335 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700336 }
337
Jeff Sharkey42d26792013-09-06 13:22:09 -0700338 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700339 public void onStop() {
340 super.onStop();
341
342 // Remember last scroll location
343 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
344 getView().saveHierarchyState(container);
345 final State state = getDisplayState(this);
346 state.dirState.put(mStateKey, container);
347 }
348
349 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700350 public void onResume() {
351 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700352 updateDisplayState();
353 }
354
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700355 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700356 // Sort order change always triggers reload; we'll trigger state change
357 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700358 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
359 }
360
361 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700362 final ContentResolver resolver = getActivity().getContentResolver();
363 final State state = getDisplayState(this);
364
365 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
366 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
367
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700368 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700369 final Uri stateUri = RecentsProvider.buildState(
370 root.authority, root.rootId, doc.documentId);
371 final ContentValues values = new ContentValues();
372 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700373
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700374 new AsyncTask<Void, Void, Void>() {
375 @Override
376 protected Void doInBackground(Void... params) {
377 resolver.insert(stateUri, values);
378 return null;
379 }
380 }.execute();
381 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700382
383 // Mode change is just visual change; no need to kick loader, and
384 // deliver change event immediately.
385 state.derivedMode = state.userMode;
386 ((DocumentsActivity) getActivity()).onStateChanged();
387
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700388 updateDisplayState();
389 }
390
391 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700392 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700393
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700394 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700395 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700396 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700397
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700398 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
399 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700400
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700401 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700402 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700403 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
404 } else {
405 choiceMode = ListView.CHOICE_MODE_NONE;
406 }
407
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700408 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700409 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700410 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700411 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700412 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700413 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700414 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700415 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700416 mGridView.setChoiceMode(choiceMode);
417 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700418 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700419 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700420 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700421 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700422 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700423 mListView.setChoiceMode(choiceMode);
424 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700425 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700426 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700427 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700428
429 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700430 }
431
432 private OnItemClickListener mItemListener = new OnItemClickListener() {
433 @Override
434 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700435 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700436 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700437 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
438 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
439 if (isDocumentEnabled(docMimeType, docFlags)) {
440 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700441 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
442 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700443 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700444 }
445 };
446
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
448 @Override
449 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
450 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100451 mode.setTitle(getResources()
452 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700453 return true;
454 }
455
456 @Override
457 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700458 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700459
460 final MenuItem open = menu.findItem(R.id.menu_open);
461 final MenuItem share = menu.findItem(R.id.menu_share);
462 final MenuItem delete = menu.findItem(R.id.menu_delete);
463
464 final boolean manageMode = state.action == ACTION_MANAGE;
465 open.setVisible(!manageMode);
466 share.setVisible(manageMode);
467 delete.setVisible(manageMode);
468
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700469 return true;
470 }
471
472 @Override
473 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700474 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700475 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700476 final int size = checked.size();
477 for (int i = 0; i < size; i++) {
478 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700479 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700480 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700481 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700482 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700483 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700484
Jeff Sharkey873daa32013-08-18 17:38:20 -0700485 final int id = item.getItemId();
486 if (id == R.id.menu_open) {
487 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700488 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700489 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700490
491 } else if (id == R.id.menu_share) {
492 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700493 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700494 return true;
495
496 } else if (id == R.id.menu_delete) {
497 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700498 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499 return true;
500
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700501 } else {
502 return false;
503 }
504 }
505
506 @Override
507 public void onDestroyActionMode(ActionMode mode) {
508 // ignored
509 }
510
511 @Override
512 public void onItemCheckedStateChanged(
513 ActionMode mode, int position, long id, boolean checked) {
514 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700515 // Directories and footer items cannot be checked
516 boolean valid = false;
517
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700518 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700519 if (cursor != null) {
520 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700521 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
522 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
523 valid = isDocumentEnabled(docMimeType, docFlags);
524 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700525 }
526
527 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700528 mCurrentView.setItemChecked(position, false);
529 }
530 }
531
532 mode.setTitle(getResources()
533 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
534 }
535 };
536
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700537 private RecyclerListener mRecycleListener = new RecyclerListener() {
538 @Override
539 public void onMovedToScrapHeap(View view) {
540 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
541 if (iconThumb != null) {
542 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
543 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700544 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700545 iconThumb.setTag(null);
546 }
547 }
548 }
549 };
550
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700551 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700552 Intent intent;
553 if (docs.size() == 1) {
554 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700555
Jeff Sharkey873daa32013-08-18 17:38:20 -0700556 intent = new Intent(Intent.ACTION_SEND);
557 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
558 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700559 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700560 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700561
562 } else if (docs.size() > 1) {
563 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
564 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
565 intent.addCategory(Intent.CATEGORY_DEFAULT);
566
567 final ArrayList<String> mimeTypes = Lists.newArrayList();
568 final ArrayList<Uri> uris = Lists.newArrayList();
569 for (DocumentInfo doc : docs) {
570 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700571 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700572 }
573
574 intent.setType(findCommonMimeType(mimeTypes));
575 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
576
577 } else {
578 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700579 }
580
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700581 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700582 startActivity(intent);
583 }
584
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700585 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700586 final Context context = getActivity();
587 final ContentResolver resolver = context.getContentResolver();
588
589 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700590 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700591 if (!doc.isDeleteSupported()) {
592 Log.w(TAG, "Skipping " + doc);
593 hadTrouble = true;
594 continue;
595 }
596
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700597 ContentProviderClient client = null;
598 try {
599 client = DocumentsApplication.acquireUnstableProviderOrThrow(
600 resolver, doc.derivedUri.getAuthority());
601 DocumentsContract.deleteDocument(client, doc.derivedUri);
602 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700603 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700604 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700605 } finally {
606 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700607 }
608 }
609
610 if (hadTrouble) {
611 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
612 }
613 }
614
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700615 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700616 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700617 }
618
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700619 private static abstract class Footer {
620 private final int mItemViewType;
621
622 public Footer(int itemViewType) {
623 mItemViewType = itemViewType;
624 }
625
626 public abstract View getView(View convertView, ViewGroup parent);
627
628 public int getItemViewType() {
629 return mItemViewType;
630 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700631 }
632
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700633 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700634 public LoadingFooter() {
635 super(1);
636 }
637
Jeff Sharkey20b32272013-09-03 15:25:52 -0700638 @Override
639 public View getView(View convertView, ViewGroup parent) {
640 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700641 final State state = getDisplayState(DirectoryFragment.this);
642
Jeff Sharkey20b32272013-09-03 15:25:52 -0700643 if (convertView == null) {
644 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700645 if (state.derivedMode == MODE_LIST) {
646 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
647 } else if (state.derivedMode == MODE_GRID) {
648 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
649 } else {
650 throw new IllegalStateException();
651 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700652 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700653
Jeff Sharkey20b32272013-09-03 15:25:52 -0700654 return convertView;
655 }
656 }
657
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700658 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700659 private final int mIcon;
660 private final String mMessage;
661
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700662 public MessageFooter(int itemViewType, int icon, String message) {
663 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700664 mIcon = icon;
665 mMessage = message;
666 }
667
668 @Override
669 public View getView(View convertView, ViewGroup parent) {
670 final Context context = parent.getContext();
671 final State state = getDisplayState(DirectoryFragment.this);
672
673 if (convertView == null) {
674 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700675 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700676 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700677 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700678 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
679 } else {
680 throw new IllegalStateException();
681 }
682 }
683
684 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
685 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
686 icon.setImageResource(mIcon);
687 title.setText(mMessage);
688 return convertView;
689 }
690 }
691
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700692 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700693 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700694 private int mCursorCount;
695
696 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700697
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700698 public void swapResult(DirectoryResult result) {
699 mCursor = result != null ? result.cursor : null;
700 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700701
702 mFooters.clear();
703
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700704 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700705 if (extras != null) {
706 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
707 if (info != null) {
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700708 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info_dark, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700709 }
710 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
711 if (error != null) {
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700712 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert_dark, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700713 }
714 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
715 mFooters.add(new LoadingFooter());
716 }
717 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700718
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700719 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700720 mFooters.add(new MessageFooter(
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700721 3, R.drawable.ic_dialog_alert_dark, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700722 }
723
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700724 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700725 mEmptyView.setVisibility(View.VISIBLE);
726 } else {
727 mEmptyView.setVisibility(View.GONE);
728 }
729
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700730 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700731 }
732
733 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700734 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700735 if (position < mCursorCount) {
736 return getDocumentView(position, convertView, parent);
737 } else {
738 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700739 convertView = mFooters.get(position).getView(convertView, parent);
740 // Only the view itself is disabled; contents inside shouldn't
741 // be dimmed.
742 convertView.setEnabled(false);
743 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700744 }
745 }
746
747 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700748 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700749 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700750
Jeff Sharkey9656a532013-09-13 13:42:19 -0700751 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
752
Jeff Sharkey873daa32013-08-18 17:38:20 -0700753 final RootsCache roots = DocumentsApplication.getRootsCache(context);
754 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
755 context, mThumbSize);
756
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700757 if (convertView == null) {
758 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700759 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700760 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700761 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700762 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
763 } else {
764 throw new IllegalStateException();
765 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700766 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700767
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700768 final Cursor cursor = getItem(position);
769
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700770 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
771 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700772 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
773 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
774 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
775 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
776 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
777 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
778 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
779 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700780
Jeff Sharkey9656a532013-09-13 13:42:19 -0700781 final View line1 = convertView.findViewById(R.id.line1);
782 final View line2 = convertView.findViewById(R.id.line2);
783
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700784 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
785 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700786 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
787 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700788 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700789 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
790 final TextView date = (TextView) convertView.findViewById(R.id.date);
791 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700792
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700793 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700794 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700795 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700796 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700797 }
798
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700799 iconMime.animate().cancel();
800 iconThumb.animate().cancel();
801
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700802 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700803 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700804 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700805 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700806
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700807 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700808 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700809 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700810 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700811 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700812 iconThumb.setImageBitmap(cachedResult);
813 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700814 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700815 iconThumb.setImageDrawable(null);
816 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
817 uri, iconMime, iconThumb, mThumbSize);
818 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700819 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700820 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700821 }
822
823 // Always throw MIME icon into place, even when a thumbnail is being
824 // loaded in background.
825 if (cacheHit) {
826 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700827 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700828 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700829 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700830 iconMime.setAlpha(1f);
831 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700832 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700833 if (docIcon != 0) {
834 iconMime.setImageDrawable(
835 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
836 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700837 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
838 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700839 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700840 }
841
Jeff Sharkey9656a532013-09-13 13:42:19 -0700842 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700843 boolean hasLine2 = false;
844
Jeff Sharkey9656a532013-09-13 13:42:19 -0700845 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
846 if (!hideTitle) {
847 title.setText(docDisplayName);
848 hasLine1 = true;
849 }
850
851 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700852 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700853 // We've already had to enumerate roots before any results can
854 // be shown, so this will never block.
855 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700856 if (state.derivedMode == MODE_GRID) {
857 iconDrawable = root.loadLightIcon(context);
858 } else {
859 iconDrawable = root.loadIcon(context);
860 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700861
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700862 if (summary != null) {
863 final boolean alwaysShowSummary = getResources()
864 .getBoolean(R.bool.always_show_summary);
865 if (alwaysShowSummary) {
866 summary.setText(root.getDirectoryString());
867 summary.setVisibility(View.VISIBLE);
868 hasLine2 = true;
869 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700870 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700871 // No summary needed if icon speaks for itself
872 summary.setVisibility(View.INVISIBLE);
873 } else {
874 summary.setText(root.getDirectoryString());
875 summary.setVisibility(View.VISIBLE);
876 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
877 hasLine2 = true;
878 }
879 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700880 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700881 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700882 // Directories showing thumbnails in grid mode get a little icon
883 // hint to remind user they're a directory.
884 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
885 && showThumbnail) {
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700886 iconDrawable = context.getResources().getDrawable(
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700887 R.drawable.ic_root_folder_light);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700888 }
889
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700890 if (summary != null) {
891 if (docSummary != null) {
892 summary.setText(docSummary);
893 summary.setVisibility(View.VISIBLE);
894 hasLine2 = true;
895 } else {
896 summary.setVisibility(View.INVISIBLE);
897 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700898 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700899 }
900
Jeff Sharkey9656a532013-09-13 13:42:19 -0700901 if (icon1 != null) icon1.setVisibility(View.GONE);
902 if (icon2 != null) icon2.setVisibility(View.GONE);
903
904 if (iconDrawable != null) {
905 if (hasLine1) {
906 icon1.setVisibility(View.VISIBLE);
907 icon1.setImageDrawable(iconDrawable);
908 } else {
909 icon2.setVisibility(View.VISIBLE);
910 icon2.setImageDrawable(iconDrawable);
911 }
912 }
913
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700914 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700915 date.setText(null);
916 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700917 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700918 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700919 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700920
921 if (state.showSize) {
922 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700923 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700924 size.setText(null);
925 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700926 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700927 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700928 }
929 } else {
930 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700931 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700932
Jeff Sharkey9656a532013-09-13 13:42:19 -0700933 if (line1 != null) {
934 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
935 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700936 if (line2 != null) {
937 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
938 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700939
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700940 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700941 if (enabled) {
942 setEnabledRecursive(convertView, true);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700943 iconMime.setAlpha(1f);
944 iconThumb.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700945 if (icon1 != null) icon1.setAlpha(1f);
946 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700947 } else {
948 setEnabledRecursive(convertView, false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700949 iconMime.setAlpha(0.5f);
950 iconThumb.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700951 if (icon1 != null) icon1.setAlpha(0.5f);
952 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700953 }
954
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700955 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700956 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700957
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700958 @Override
959 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700960 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700961 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700962
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700963 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700964 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700965 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700966 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700967 return mCursor;
968 } else {
969 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700970 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700971 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700972
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700973 @Override
974 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700975 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700976 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700977
978 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700979 public int getViewTypeCount() {
980 return 4;
981 }
982
983 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700984 public int getItemViewType(int position) {
985 if (position < mCursorCount) {
986 return 0;
987 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700988 position -= mCursorCount;
989 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700990 }
991 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700992 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700993
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700994 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
995 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700996 private final Uri mUri;
997 private final ImageView mIconMime;
998 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700999 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001000 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001001
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001002 public ThumbnailAsyncTask(
1003 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
1004 mUri = uri;
1005 mIconMime = iconMime;
1006 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001007 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001008 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001009 }
1010
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001011 @Override
1012 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001013 cancel(false);
1014 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001015 }
1016
1017 @Override
1018 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001019 if (isCancelled()) return null;
1020
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001021 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001022 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001023
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001024 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001025 Bitmap result = null;
1026 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001027 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1028 resolver, mUri.getAuthority());
1029 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001030 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001031 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1032 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001033 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001034 }
1035 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001036 if (!(e instanceof OperationCanceledException)) {
1037 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1038 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001039 } finally {
1040 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001041 }
1042 return result;
1043 }
1044
1045 @Override
1046 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001047 if (mIconThumb.getTag() == this && result != null) {
1048 mIconThumb.setTag(null);
1049 mIconThumb.setImageBitmap(result);
1050
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001051 final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
1052 mIconMime.setAlpha(targetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001053 mIconMime.animate().alpha(0f).start();
1054 mIconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001055 mIconThumb.animate().alpha(targetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001056 }
1057 }
1058 }
1059
1060 private static String formatTime(Context context, long when) {
1061 // TODO: DateUtils should make this easier
1062 Time then = new Time();
1063 then.set(when);
1064 Time now = new Time();
1065 now.setToNow();
1066
1067 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1068 | DateUtils.FORMAT_ABBREV_ALL;
1069
1070 if (then.year != now.year) {
1071 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1072 } else if (then.yearDay != now.yearDay) {
1073 flags |= DateUtils.FORMAT_SHOW_DATE;
1074 } else {
1075 flags |= DateUtils.FORMAT_SHOW_TIME;
1076 }
1077
1078 return DateUtils.formatDateTime(context, when, flags);
1079 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001080
1081 private String findCommonMimeType(List<String> mimeTypes) {
1082 String[] commonType = mimeTypes.get(0).split("/");
1083 if (commonType.length != 2) {
1084 return "*/*";
1085 }
1086
1087 for (int i = 1; i < mimeTypes.size(); i++) {
1088 String[] type = mimeTypes.get(i).split("/");
1089 if (type.length != 2) continue;
1090
1091 if (!commonType[1].equals(type[1])) {
1092 commonType[1] = "*";
1093 }
1094
1095 if (!commonType[0].equals(type[0])) {
1096 commonType[0] = "*";
1097 commonType[1] = "*";
1098 break;
1099 }
1100 }
1101
1102 return commonType[0] + "/" + commonType[1];
1103 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001104
1105 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001106 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001107 if (v.isEnabled() == enabled) return;
1108 v.setEnabled(enabled);
1109
1110 if (v instanceof ViewGroup) {
1111 final ViewGroup vg = (ViewGroup) v;
1112 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1113 setEnabledRecursive(vg.getChildAt(i), enabled);
1114 }
1115 }
1116 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001117
1118 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1119 final State state = getDisplayState(DirectoryFragment.this);
1120
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001121 // Directories are always enabled
1122 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1123 return true;
1124 }
1125
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001126 // Read-only files are disabled when creating
1127 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1128 return false;
1129 }
1130
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001131 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1132 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001133}