blob: f55912c24b5cc6c191f1abd7b120daa622f82016 [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;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070020import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
21import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
22import static com.android.documentsui.BaseActivity.State.MODE_GRID;
23import static com.android.documentsui.BaseActivity.State.MODE_LIST;
24import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
25import static com.android.documentsui.BaseActivity.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
Steve McKayd0a2a2c2015-03-25 14:35:33 -070079import com.android.documentsui.BaseActivity.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;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700304 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700305
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) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700310 ((BaseActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700311 }
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 Sharkeye8d13ea2014-08-08 15:10:03 -0700354 public void onDisplayStateChanged() {
355 updateDisplayState();
356 }
357
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700358 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700359 // Sort order change always triggers reload; we'll trigger state change
360 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700361 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
362 }
363
364 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700365 final ContentResolver resolver = getActivity().getContentResolver();
366 final State state = getDisplayState(this);
367
368 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
369 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
370
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700371 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700372 final Uri stateUri = RecentsProvider.buildState(
373 root.authority, root.rootId, doc.documentId);
374 final ContentValues values = new ContentValues();
375 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700376
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700377 new AsyncTask<Void, Void, Void>() {
378 @Override
379 protected Void doInBackground(Void... params) {
380 resolver.insert(stateUri, values);
381 return null;
382 }
383 }.execute();
384 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700385
386 // Mode change is just visual change; no need to kick loader, and
387 // deliver change event immediately.
388 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700389 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700390
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700391 updateDisplayState();
392 }
393
394 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700395 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700396
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700397 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700398 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700399 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700400
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700401 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
402 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700403
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700404 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700405 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700406 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
407 } else {
408 choiceMode = ListView.CHOICE_MODE_NONE;
409 }
410
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700411 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700412 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700413 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700414 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700415 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700416 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700417 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700418 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700419 mGridView.setChoiceMode(choiceMode);
420 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700421 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700422 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700423 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700424 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700425 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700426 mListView.setChoiceMode(choiceMode);
427 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700428 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700429 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700430 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700431
432 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700433 }
434
435 private OnItemClickListener mItemListener = new OnItemClickListener() {
436 @Override
437 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700438 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700439 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700440 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
441 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
442 if (isDocumentEnabled(docMimeType, docFlags)) {
443 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700444 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700445 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700446 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700447 }
448 };
449
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700450 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
451 @Override
452 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
453 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100454 mode.setTitle(getResources()
455 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700456 return true;
457 }
458
459 @Override
460 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700461 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700462
463 final MenuItem open = menu.findItem(R.id.menu_open);
464 final MenuItem share = menu.findItem(R.id.menu_share);
465 final MenuItem delete = menu.findItem(R.id.menu_delete);
466
467 final boolean manageMode = state.action == ACTION_MANAGE;
468 open.setVisible(!manageMode);
469 share.setVisible(manageMode);
470 delete.setVisible(manageMode);
471
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700472 return true;
473 }
474
475 @Override
476 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700477 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700478 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700479 final int size = checked.size();
480 for (int i = 0; i < size; i++) {
481 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700482 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700483 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700484 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700485 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700486 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700487
Jeff Sharkey873daa32013-08-18 17:38:20 -0700488 final int id = item.getItemId();
489 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700490 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700491 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700492 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700493
494 } else if (id == R.id.menu_share) {
495 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700496 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700497 return true;
498
499 } else if (id == R.id.menu_delete) {
500 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700501 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700502 return true;
503
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700504 } else {
505 return false;
506 }
507 }
508
509 @Override
510 public void onDestroyActionMode(ActionMode mode) {
511 // ignored
512 }
513
514 @Override
515 public void onItemCheckedStateChanged(
516 ActionMode mode, int position, long id, boolean checked) {
517 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700518 // Directories and footer items cannot be checked
519 boolean valid = false;
520
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700521 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700522 if (cursor != null) {
523 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700524 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
525 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
526 valid = isDocumentEnabled(docMimeType, docFlags);
527 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700528 }
529
530 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700531 mCurrentView.setItemChecked(position, false);
532 }
533 }
534
535 mode.setTitle(getResources()
536 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
537 }
538 };
539
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700540 private RecyclerListener mRecycleListener = new RecyclerListener() {
541 @Override
542 public void onMovedToScrapHeap(View view) {
543 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
544 if (iconThumb != null) {
545 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
546 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700547 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700548 iconThumb.setTag(null);
549 }
550 }
551 }
552 };
553
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700554 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700555 Intent intent;
556 if (docs.size() == 1) {
557 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700558
Jeff Sharkey873daa32013-08-18 17:38:20 -0700559 intent = new Intent(Intent.ACTION_SEND);
560 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
561 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700562 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700563 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700564
565 } else if (docs.size() > 1) {
566 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
567 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
568 intent.addCategory(Intent.CATEGORY_DEFAULT);
569
570 final ArrayList<String> mimeTypes = Lists.newArrayList();
571 final ArrayList<Uri> uris = Lists.newArrayList();
572 for (DocumentInfo doc : docs) {
573 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700574 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700575 }
576
577 intent.setType(findCommonMimeType(mimeTypes));
578 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
579
580 } else {
581 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700582 }
583
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700584 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700585 startActivity(intent);
586 }
587
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700588 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700589 final Context context = getActivity();
590 final ContentResolver resolver = context.getContentResolver();
591
592 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700593 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700594 if (!doc.isDeleteSupported()) {
595 Log.w(TAG, "Skipping " + doc);
596 hadTrouble = true;
597 continue;
598 }
599
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700600 ContentProviderClient client = null;
601 try {
602 client = DocumentsApplication.acquireUnstableProviderOrThrow(
603 resolver, doc.derivedUri.getAuthority());
604 DocumentsContract.deleteDocument(client, doc.derivedUri);
605 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700606 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700607 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700608 } finally {
609 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700610 }
611 }
612
613 if (hadTrouble) {
614 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
615 }
616 }
617
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700618 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700619 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700620 }
621
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700622 private static abstract class Footer {
623 private final int mItemViewType;
624
625 public Footer(int itemViewType) {
626 mItemViewType = itemViewType;
627 }
628
629 public abstract View getView(View convertView, ViewGroup parent);
630
631 public int getItemViewType() {
632 return mItemViewType;
633 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700634 }
635
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700636 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700637 public LoadingFooter() {
638 super(1);
639 }
640
Jeff Sharkey20b32272013-09-03 15:25:52 -0700641 @Override
642 public View getView(View convertView, ViewGroup parent) {
643 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700644 final State state = getDisplayState(DirectoryFragment.this);
645
Jeff Sharkey20b32272013-09-03 15:25:52 -0700646 if (convertView == null) {
647 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700648 if (state.derivedMode == MODE_LIST) {
649 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
650 } else if (state.derivedMode == MODE_GRID) {
651 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
652 } else {
653 throw new IllegalStateException();
654 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700655 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700656
Jeff Sharkey20b32272013-09-03 15:25:52 -0700657 return convertView;
658 }
659 }
660
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700661 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700662 private final int mIcon;
663 private final String mMessage;
664
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700665 public MessageFooter(int itemViewType, int icon, String message) {
666 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700667 mIcon = icon;
668 mMessage = message;
669 }
670
671 @Override
672 public View getView(View convertView, ViewGroup parent) {
673 final Context context = parent.getContext();
674 final State state = getDisplayState(DirectoryFragment.this);
675
676 if (convertView == null) {
677 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700678 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700679 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700680 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700681 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
682 } else {
683 throw new IllegalStateException();
684 }
685 }
686
687 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
688 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
689 icon.setImageResource(mIcon);
690 title.setText(mMessage);
691 return convertView;
692 }
693 }
694
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700695 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700696 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700697 private int mCursorCount;
698
699 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700700
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700701 public void swapResult(DirectoryResult result) {
702 mCursor = result != null ? result.cursor : null;
703 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700704
705 mFooters.clear();
706
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700707 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700708 if (extras != null) {
709 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
710 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700711 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700712 }
713 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
714 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700715 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700716 }
717 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
718 mFooters.add(new LoadingFooter());
719 }
720 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700721
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700722 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700723 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700724 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700725 }
726
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700727 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700728 mEmptyView.setVisibility(View.VISIBLE);
729 } else {
730 mEmptyView.setVisibility(View.GONE);
731 }
732
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700733 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700734 }
735
736 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700737 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700738 if (position < mCursorCount) {
739 return getDocumentView(position, convertView, parent);
740 } else {
741 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700742 convertView = mFooters.get(position).getView(convertView, parent);
743 // Only the view itself is disabled; contents inside shouldn't
744 // be dimmed.
745 convertView.setEnabled(false);
746 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700747 }
748 }
749
750 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700751 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700752 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700753
Jeff Sharkey9656a532013-09-13 13:42:19 -0700754 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
755
Jeff Sharkey873daa32013-08-18 17:38:20 -0700756 final RootsCache roots = DocumentsApplication.getRootsCache(context);
757 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
758 context, mThumbSize);
759
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700760 if (convertView == null) {
761 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700762 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700763 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700764 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700765 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
766 } else {
767 throw new IllegalStateException();
768 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700769 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700770
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700771 final Cursor cursor = getItem(position);
772
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700773 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
774 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700775 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
776 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
777 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
778 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
779 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
780 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
781 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
782 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700783
Jeff Sharkey9656a532013-09-13 13:42:19 -0700784 final View line1 = convertView.findViewById(R.id.line1);
785 final View line2 = convertView.findViewById(R.id.line2);
786
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700787 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
788 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700789 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
790 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700791 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700792 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
793 final TextView date = (TextView) convertView.findViewById(R.id.date);
794 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700795
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700796 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700797 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700798 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700799 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700800 }
801
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700802 iconMime.animate().cancel();
803 iconThumb.animate().cancel();
804
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700805 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700806 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700807 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700808 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700809
Jeff Sharkey7e544612014-08-29 15:38:27 -0700810 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
811 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
812
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700813 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700814 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700815 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700816 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700817 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700818 iconThumb.setImageBitmap(cachedResult);
819 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700820 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700821 iconThumb.setImageDrawable(null);
822 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700823 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700824 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700825 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700826 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700827 }
828
829 // Always throw MIME icon into place, even when a thumbnail is being
830 // loaded in background.
831 if (cacheHit) {
832 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700833 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700834 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700835 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700836 iconMime.setAlpha(1f);
837 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700838 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700839 if (docIcon != 0) {
840 iconMime.setImageDrawable(
841 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
842 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700843 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
844 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700845 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700846 }
847
Jeff Sharkey9656a532013-09-13 13:42:19 -0700848 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700849 boolean hasLine2 = false;
850
Jeff Sharkey9656a532013-09-13 13:42:19 -0700851 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
852 if (!hideTitle) {
853 title.setText(docDisplayName);
854 hasLine1 = true;
855 }
856
857 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700858 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700859 // We've already had to enumerate roots before any results can
860 // be shown, so this will never block.
861 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700862 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700863 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700864 } else {
865 iconDrawable = root.loadIcon(context);
866 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700867
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700868 if (summary != null) {
869 final boolean alwaysShowSummary = getResources()
870 .getBoolean(R.bool.always_show_summary);
871 if (alwaysShowSummary) {
872 summary.setText(root.getDirectoryString());
873 summary.setVisibility(View.VISIBLE);
874 hasLine2 = true;
875 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700876 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700877 // No summary needed if icon speaks for itself
878 summary.setVisibility(View.INVISIBLE);
879 } else {
880 summary.setText(root.getDirectoryString());
881 summary.setVisibility(View.VISIBLE);
882 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
883 hasLine2 = true;
884 }
885 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700886 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700887 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700888 // Directories showing thumbnails in grid mode get a little icon
889 // hint to remind user they're a directory.
890 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
891 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700892 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700893 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700894 }
895
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700896 if (summary != null) {
897 if (docSummary != null) {
898 summary.setText(docSummary);
899 summary.setVisibility(View.VISIBLE);
900 hasLine2 = true;
901 } else {
902 summary.setVisibility(View.INVISIBLE);
903 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700904 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700905 }
906
Jeff Sharkey9656a532013-09-13 13:42:19 -0700907 if (icon1 != null) icon1.setVisibility(View.GONE);
908 if (icon2 != null) icon2.setVisibility(View.GONE);
909
910 if (iconDrawable != null) {
911 if (hasLine1) {
912 icon1.setVisibility(View.VISIBLE);
913 icon1.setImageDrawable(iconDrawable);
914 } else {
915 icon2.setVisibility(View.VISIBLE);
916 icon2.setImageDrawable(iconDrawable);
917 }
918 }
919
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700920 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700921 date.setText(null);
922 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700923 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700924 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700925 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700926
927 if (state.showSize) {
928 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700929 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700930 size.setText(null);
931 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700932 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700933 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700934 }
935 } else {
936 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700937 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700938
Jeff Sharkey9656a532013-09-13 13:42:19 -0700939 if (line1 != null) {
940 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
941 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700942 if (line2 != null) {
943 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
944 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700945
Jeff Sharkey7e544612014-08-29 15:38:27 -0700946 setEnabledRecursive(convertView, enabled);
947
948 iconMime.setAlpha(iconAlpha);
949 iconThumb.setAlpha(iconAlpha);
950 if (icon1 != null) icon1.setAlpha(iconAlpha);
951 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700952
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700953 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700954 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700955
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700956 @Override
957 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700958 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700959 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700960
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700961 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700962 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700963 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700964 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700965 return mCursor;
966 } else {
967 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700968 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700969 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700970
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700971 @Override
972 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700973 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700974 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700975
976 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700977 public int getViewTypeCount() {
978 return 4;
979 }
980
981 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700982 public int getItemViewType(int position) {
983 if (position < mCursorCount) {
984 return 0;
985 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700986 position -= mCursorCount;
987 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700988 }
989 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700990 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700991
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700992 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
993 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700994 private final Uri mUri;
995 private final ImageView mIconMime;
996 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700997 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -0700998 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700999 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001000
Jeff Sharkey7e544612014-08-29 15:38:27 -07001001 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1002 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001003 mUri = uri;
1004 mIconMime = iconMime;
1005 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001006 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001007 mTargetAlpha = targetAlpha;
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 Sharkey7e544612014-08-29 15:38:27 -07001051 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001052 mIconMime.animate().alpha(0f).start();
1053 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001054 mIconThumb.animate().alpha(mTargetAlpha).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}