blob: a75dc426c2bd9d9602f92835252a84491c8dc91d [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
Ben Kwa512a6ba2015-03-31 08:15:21 -0700504 } else if (id == R.id.menu_select_all) {
505 int count = mCurrentView.getCount();
506 for (int i = 0; i < count; i++) {
507 mCurrentView.setItemChecked(i, true);
508 }
509 updateDisplayState();
510 return true;
511
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700512 } else {
513 return false;
514 }
515 }
516
517 @Override
518 public void onDestroyActionMode(ActionMode mode) {
519 // ignored
520 }
521
522 @Override
523 public void onItemCheckedStateChanged(
524 ActionMode mode, int position, long id, boolean checked) {
525 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700526 // Directories and footer items cannot be checked
527 boolean valid = false;
528
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700529 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700530 if (cursor != null) {
531 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700532 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
533 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
534 valid = isDocumentEnabled(docMimeType, docFlags);
535 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700536 }
537
538 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700539 mCurrentView.setItemChecked(position, false);
540 }
541 }
542
543 mode.setTitle(getResources()
544 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
545 }
546 };
547
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700548 private RecyclerListener mRecycleListener = new RecyclerListener() {
549 @Override
550 public void onMovedToScrapHeap(View view) {
551 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
552 if (iconThumb != null) {
553 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
554 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700555 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700556 iconThumb.setTag(null);
557 }
558 }
559 }
560 };
561
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700562 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700563 Intent intent;
564 if (docs.size() == 1) {
565 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700566
Jeff Sharkey873daa32013-08-18 17:38:20 -0700567 intent = new Intent(Intent.ACTION_SEND);
568 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
569 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700570 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700571 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700572
573 } else if (docs.size() > 1) {
574 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
575 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
576 intent.addCategory(Intent.CATEGORY_DEFAULT);
577
578 final ArrayList<String> mimeTypes = Lists.newArrayList();
579 final ArrayList<Uri> uris = Lists.newArrayList();
580 for (DocumentInfo doc : docs) {
581 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700582 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700583 }
584
585 intent.setType(findCommonMimeType(mimeTypes));
586 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
587
588 } else {
589 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700590 }
591
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700592 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700593 startActivity(intent);
594 }
595
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700596 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700597 final Context context = getActivity();
598 final ContentResolver resolver = context.getContentResolver();
599
600 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700601 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700602 if (!doc.isDeleteSupported()) {
603 Log.w(TAG, "Skipping " + doc);
604 hadTrouble = true;
605 continue;
606 }
607
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700608 ContentProviderClient client = null;
609 try {
610 client = DocumentsApplication.acquireUnstableProviderOrThrow(
611 resolver, doc.derivedUri.getAuthority());
612 DocumentsContract.deleteDocument(client, doc.derivedUri);
613 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700614 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700615 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700616 } finally {
617 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700618 }
619 }
620
621 if (hadTrouble) {
622 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
623 }
624 }
625
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700626 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700627 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700628 }
629
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700630 private static abstract class Footer {
631 private final int mItemViewType;
632
633 public Footer(int itemViewType) {
634 mItemViewType = itemViewType;
635 }
636
637 public abstract View getView(View convertView, ViewGroup parent);
638
639 public int getItemViewType() {
640 return mItemViewType;
641 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700642 }
643
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700644 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700645 public LoadingFooter() {
646 super(1);
647 }
648
Jeff Sharkey20b32272013-09-03 15:25:52 -0700649 @Override
650 public View getView(View convertView, ViewGroup parent) {
651 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700652 final State state = getDisplayState(DirectoryFragment.this);
653
Jeff Sharkey20b32272013-09-03 15:25:52 -0700654 if (convertView == null) {
655 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700656 if (state.derivedMode == MODE_LIST) {
657 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
658 } else if (state.derivedMode == MODE_GRID) {
659 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
660 } else {
661 throw new IllegalStateException();
662 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700663 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700664
Jeff Sharkey20b32272013-09-03 15:25:52 -0700665 return convertView;
666 }
667 }
668
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700669 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700670 private final int mIcon;
671 private final String mMessage;
672
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700673 public MessageFooter(int itemViewType, int icon, String message) {
674 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700675 mIcon = icon;
676 mMessage = message;
677 }
678
679 @Override
680 public View getView(View convertView, ViewGroup parent) {
681 final Context context = parent.getContext();
682 final State state = getDisplayState(DirectoryFragment.this);
683
684 if (convertView == null) {
685 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700686 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700687 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700688 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700689 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
690 } else {
691 throw new IllegalStateException();
692 }
693 }
694
695 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
696 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
697 icon.setImageResource(mIcon);
698 title.setText(mMessage);
699 return convertView;
700 }
701 }
702
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700703 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700704 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700705 private int mCursorCount;
706
707 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700708
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700709 public void swapResult(DirectoryResult result) {
710 mCursor = result != null ? result.cursor : null;
711 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700712
713 mFooters.clear();
714
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700715 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700716 if (extras != null) {
717 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
718 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700719 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700720 }
721 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
722 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700723 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700724 }
725 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
726 mFooters.add(new LoadingFooter());
727 }
728 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700729
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700730 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700731 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700732 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700733 }
734
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700735 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700736 mEmptyView.setVisibility(View.VISIBLE);
737 } else {
738 mEmptyView.setVisibility(View.GONE);
739 }
740
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700741 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700742 }
743
744 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700745 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700746 if (position < mCursorCount) {
747 return getDocumentView(position, convertView, parent);
748 } else {
749 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700750 convertView = mFooters.get(position).getView(convertView, parent);
751 // Only the view itself is disabled; contents inside shouldn't
752 // be dimmed.
753 convertView.setEnabled(false);
754 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700755 }
756 }
757
758 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700759 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700760 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700761
Jeff Sharkey9656a532013-09-13 13:42:19 -0700762 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
763
Jeff Sharkey873daa32013-08-18 17:38:20 -0700764 final RootsCache roots = DocumentsApplication.getRootsCache(context);
765 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
766 context, mThumbSize);
767
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700768 if (convertView == null) {
769 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700770 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700771 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700772 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700773 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
774 } else {
775 throw new IllegalStateException();
776 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700777 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700778
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700779 final Cursor cursor = getItem(position);
780
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700781 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
782 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700783 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
784 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
785 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
786 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
787 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
788 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
789 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
790 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700791
Jeff Sharkey9656a532013-09-13 13:42:19 -0700792 final View line1 = convertView.findViewById(R.id.line1);
793 final View line2 = convertView.findViewById(R.id.line2);
794
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700795 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
796 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700797 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
798 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700799 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700800 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
801 final TextView date = (TextView) convertView.findViewById(R.id.date);
802 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700803
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700804 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700805 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700806 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700807 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700808 }
809
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700810 iconMime.animate().cancel();
811 iconThumb.animate().cancel();
812
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700813 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700814 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700815 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700816 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700817
Jeff Sharkey7e544612014-08-29 15:38:27 -0700818 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
819 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
820
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700821 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700822 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700823 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700824 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700825 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700826 iconThumb.setImageBitmap(cachedResult);
827 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700828 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700829 iconThumb.setImageDrawable(null);
830 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700831 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700832 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700833 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700834 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700835 }
836
837 // Always throw MIME icon into place, even when a thumbnail is being
838 // loaded in background.
839 if (cacheHit) {
840 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700841 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700842 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700843 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700844 iconMime.setAlpha(1f);
845 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700846 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700847 if (docIcon != 0) {
848 iconMime.setImageDrawable(
849 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
850 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700851 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
852 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700853 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700854 }
855
Jeff Sharkey9656a532013-09-13 13:42:19 -0700856 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700857 boolean hasLine2 = false;
858
Jeff Sharkey9656a532013-09-13 13:42:19 -0700859 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
860 if (!hideTitle) {
861 title.setText(docDisplayName);
862 hasLine1 = true;
863 }
864
865 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700866 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700867 // We've already had to enumerate roots before any results can
868 // be shown, so this will never block.
869 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700870 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700871 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700872 } else {
873 iconDrawable = root.loadIcon(context);
874 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700875
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700876 if (summary != null) {
877 final boolean alwaysShowSummary = getResources()
878 .getBoolean(R.bool.always_show_summary);
879 if (alwaysShowSummary) {
880 summary.setText(root.getDirectoryString());
881 summary.setVisibility(View.VISIBLE);
882 hasLine2 = true;
883 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700884 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700885 // No summary needed if icon speaks for itself
886 summary.setVisibility(View.INVISIBLE);
887 } else {
888 summary.setText(root.getDirectoryString());
889 summary.setVisibility(View.VISIBLE);
890 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
891 hasLine2 = true;
892 }
893 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700894 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700895 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700896 // Directories showing thumbnails in grid mode get a little icon
897 // hint to remind user they're a directory.
898 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
899 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700900 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700901 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700902 }
903
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700904 if (summary != null) {
905 if (docSummary != null) {
906 summary.setText(docSummary);
907 summary.setVisibility(View.VISIBLE);
908 hasLine2 = true;
909 } else {
910 summary.setVisibility(View.INVISIBLE);
911 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700912 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700913 }
914
Jeff Sharkey9656a532013-09-13 13:42:19 -0700915 if (icon1 != null) icon1.setVisibility(View.GONE);
916 if (icon2 != null) icon2.setVisibility(View.GONE);
917
918 if (iconDrawable != null) {
919 if (hasLine1) {
920 icon1.setVisibility(View.VISIBLE);
921 icon1.setImageDrawable(iconDrawable);
922 } else {
923 icon2.setVisibility(View.VISIBLE);
924 icon2.setImageDrawable(iconDrawable);
925 }
926 }
927
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700928 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700929 date.setText(null);
930 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700931 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700932 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700933 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700934
935 if (state.showSize) {
936 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700937 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700938 size.setText(null);
939 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700940 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700941 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700942 }
943 } else {
944 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700945 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700946
Jeff Sharkey9656a532013-09-13 13:42:19 -0700947 if (line1 != null) {
948 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
949 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700950 if (line2 != null) {
951 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
952 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700953
Jeff Sharkey7e544612014-08-29 15:38:27 -0700954 setEnabledRecursive(convertView, enabled);
955
956 iconMime.setAlpha(iconAlpha);
957 iconThumb.setAlpha(iconAlpha);
958 if (icon1 != null) icon1.setAlpha(iconAlpha);
959 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700960
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700961 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700962 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700963
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700964 @Override
965 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700966 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700967 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700968
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700969 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700970 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700971 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700972 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700973 return mCursor;
974 } else {
975 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700976 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700977 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700978
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700979 @Override
980 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700981 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700982 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700983
984 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700985 public int getViewTypeCount() {
986 return 4;
987 }
988
989 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700990 public int getItemViewType(int position) {
991 if (position < mCursorCount) {
992 return 0;
993 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700994 position -= mCursorCount;
995 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700996 }
997 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700998 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700999
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001000 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1001 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001002 private final Uri mUri;
1003 private final ImageView mIconMime;
1004 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001005 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001006 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001007 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001008
Jeff Sharkey7e544612014-08-29 15:38:27 -07001009 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1010 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001011 mUri = uri;
1012 mIconMime = iconMime;
1013 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001014 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001015 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001016 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001017 }
1018
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001019 @Override
1020 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001021 cancel(false);
1022 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001023 }
1024
1025 @Override
1026 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001027 if (isCancelled()) return null;
1028
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001029 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001030 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001031
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001032 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001033 Bitmap result = null;
1034 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001035 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1036 resolver, mUri.getAuthority());
1037 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001038 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001039 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1040 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001041 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001042 }
1043 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001044 if (!(e instanceof OperationCanceledException)) {
1045 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1046 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001047 } finally {
1048 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001049 }
1050 return result;
1051 }
1052
1053 @Override
1054 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001055 if (mIconThumb.getTag() == this && result != null) {
1056 mIconThumb.setTag(null);
1057 mIconThumb.setImageBitmap(result);
1058
Jeff Sharkey7e544612014-08-29 15:38:27 -07001059 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001060 mIconMime.animate().alpha(0f).start();
1061 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001062 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001063 }
1064 }
1065 }
1066
1067 private static String formatTime(Context context, long when) {
1068 // TODO: DateUtils should make this easier
1069 Time then = new Time();
1070 then.set(when);
1071 Time now = new Time();
1072 now.setToNow();
1073
1074 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1075 | DateUtils.FORMAT_ABBREV_ALL;
1076
1077 if (then.year != now.year) {
1078 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1079 } else if (then.yearDay != now.yearDay) {
1080 flags |= DateUtils.FORMAT_SHOW_DATE;
1081 } else {
1082 flags |= DateUtils.FORMAT_SHOW_TIME;
1083 }
1084
1085 return DateUtils.formatDateTime(context, when, flags);
1086 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001087
1088 private String findCommonMimeType(List<String> mimeTypes) {
1089 String[] commonType = mimeTypes.get(0).split("/");
1090 if (commonType.length != 2) {
1091 return "*/*";
1092 }
1093
1094 for (int i = 1; i < mimeTypes.size(); i++) {
1095 String[] type = mimeTypes.get(i).split("/");
1096 if (type.length != 2) continue;
1097
1098 if (!commonType[1].equals(type[1])) {
1099 commonType[1] = "*";
1100 }
1101
1102 if (!commonType[0].equals(type[0])) {
1103 commonType[0] = "*";
1104 commonType[1] = "*";
1105 break;
1106 }
1107 }
1108
1109 return commonType[0] + "/" + commonType[1];
1110 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001111
1112 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001113 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001114 if (v.isEnabled() == enabled) return;
1115 v.setEnabled(enabled);
1116
1117 if (v instanceof ViewGroup) {
1118 final ViewGroup vg = (ViewGroup) v;
1119 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1120 setEnabledRecursive(vg.getChildAt(i), enabled);
1121 }
1122 }
1123 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001124
1125 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1126 final State state = getDisplayState(DirectoryFragment.this);
1127
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001128 // Directories are always enabled
1129 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1130 return true;
1131 }
1132
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001133 // Read-only files are disabled when creating
1134 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1135 return false;
1136 }
1137
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001138 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1139 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001140}