blob: 0e3016d3df1ea2c8d8e673837f7bbf34711ed093 [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
Ben Kwaf5858932015-04-07 15:43:39 -070030import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070031import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070032import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070033import android.app.FragmentManager;
34import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070035import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070036import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070037import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070038import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070040import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070041import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070042import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070043import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070044import android.graphics.Bitmap;
45import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070046import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070047import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070048import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070050import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070051import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070052import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070053import android.os.Parcelable;
Ben Kwa41b26c12015-03-31 10:11:43 -070054import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070056import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070057import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070058import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070059import android.text.format.Time;
60import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070061import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070062import android.util.SparseBooleanArray;
63import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070064import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070065import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070066import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070067import android.view.View;
68import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070069import android.widget.AbsListView;
70import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070071import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.widget.AdapterView;
73import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070074import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070075import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070076import android.widget.ImageView;
77import android.widget.ListView;
78import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070079import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080
Steve McKayd0a2a2c2015-03-25 14:35:33 -070081import com.android.documentsui.BaseActivity.State;
Ben Kwa41b26c12015-03-31 10:11:43 -070082import com.android.documentsui.CopyService;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070083import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070084import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070085import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070086import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070087import com.google.android.collect.Lists;
88
89import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070090import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070091
92/**
93 * Display the documents inside a single directory.
94 */
95public class DirectoryFragment extends Fragment {
96
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070097 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070098 private ListView mListView;
99 private GridView mGridView;
100
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101 private AbsListView mCurrentView;
102
Ben Kwaf5858932015-04-07 15:43:39 -0700103 private List<DocumentInfo> mSelectedDocumentsForCopy;
104
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700105 public static final int TYPE_NORMAL = 1;
106 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700107 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700108
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700109 public static final int ANIM_NONE = 1;
110 public static final int ANIM_SIDE = 2;
111 public static final int ANIM_DOWN = 3;
112 public static final int ANIM_UP = 4;
113
Ben Kwaf5858932015-04-07 15:43:39 -0700114 public static final int REQUEST_COPY_DESTINATION = 1;
115
Jeff Sharkey5b535922013-08-02 15:55:26 -0700116 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700117 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700118
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700119 private int mLastMode = MODE_UNKNOWN;
120 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700121 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700122
Jeff Sharkey9656a532013-09-13 13:42:19 -0700123 private boolean mHideGridTitles = false;
124
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700125 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700126 private Point mThumbSize;
127
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700129 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700130
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700132 private static final String EXTRA_ROOT = "root";
133 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700134 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700135 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700136
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700137 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700138
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700139 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
140 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700141 }
142
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700143 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
144 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700145 }
146
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700147 public static void showRecentsOpen(FragmentManager fm, int anim) {
148 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700149 }
150
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700151 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
152 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700153 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700154 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700155 args.putParcelable(EXTRA_ROOT, root);
156 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700157 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700158
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700159 final FragmentTransaction ft = fm.beginTransaction();
160 switch (anim) {
161 case ANIM_SIDE:
162 args.putBoolean(EXTRA_IGNORE_STATE, true);
163 break;
164 case ANIM_DOWN:
165 args.putBoolean(EXTRA_IGNORE_STATE, true);
166 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
167 break;
168 case ANIM_UP:
169 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
170 break;
171 }
172
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700173 final DirectoryFragment fragment = new DirectoryFragment();
174 fragment.setArguments(args);
175
Jeff Sharkey76112212013-08-06 11:26:10 -0700176 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177 ft.commitAllowingStateLoss();
178 }
179
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700180 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
181 final StringBuilder builder = new StringBuilder();
182 builder.append(root != null ? root.authority : "null").append(';');
183 builder.append(root != null ? root.rootId : "null").append(';');
184 builder.append(doc != null ? doc.documentId : "null");
185 return builder.toString();
186 }
187
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700188 public static DirectoryFragment get(FragmentManager fm) {
189 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700190 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700191 }
192
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700193 @Override
194 public View onCreateView(
195 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
196 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700197 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700198 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
199
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700200 mEmptyView = view.findViewById(android.R.id.empty);
201
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700202 mListView = (ListView) view.findViewById(R.id.list);
203 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700204 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700205 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700206
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700207 // Indent our list divider to align with text
208 final Drawable divider = mListView.getDivider();
209 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
210 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
211 if (insetLeft) {
212 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
213 } else {
214 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
215 }
216
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217 mGridView = (GridView) view.findViewById(R.id.grid);
218 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700219 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700220 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700221
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700222 return view;
223 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700224
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700225 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700226 public void onDestroyView() {
227 super.onDestroyView();
228
229 // Cancel any outstanding thumbnail requests
230 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
231 final int count = target.getChildCount();
232 for (int i = 0; i < count; i++) {
233 final View view = target.getChildAt(i);
234 mRecycleListener.onMovedToScrapHeap(view);
235 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700236
237 // Tear down any selection in progress
238 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
239 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700240 }
241
242 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700243 public void onActivityCreated(Bundle savedInstanceState) {
244 super.onActivityCreated(savedInstanceState);
245
246 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700247 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700248
Jeff Sharkey9656a532013-09-13 13:42:19 -0700249 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
250 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
251
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700252 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700253 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700254 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700255
Jeff Sharkey9656a532013-09-13 13:42:19 -0700256 if (mType == TYPE_RECENT_OPEN) {
257 // Hide titles when showing recents for picking images/videos
258 mHideGridTitles = MimePredicate.mimeMatches(
259 MimePredicate.VISUAL_MIMES, state.acceptMimes);
260 } else {
261 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
262 }
263
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 final ActivityManager am = (ActivityManager) context.getSystemService(
265 Context.ACTIVITY_SERVICE);
266 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
267
Jeff Sharkey46899c82013-08-18 22:26:48 -0700268 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700269 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700270 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700271 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700272
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700273 Uri contentsUri;
274 switch (mType) {
275 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700276 contentsUri = DocumentsContract.buildChildDocumentsUri(
277 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700278 if (state.action == ACTION_MANAGE) {
279 contentsUri = DocumentsContract.setManageMode(contentsUri);
280 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700281 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700282 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700283 case TYPE_SEARCH:
284 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700285 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700286 if (state.action == ACTION_MANAGE) {
287 contentsUri = DocumentsContract.setManageMode(contentsUri);
288 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700289 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700290 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700291 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700292 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700293 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700294 default:
295 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700296 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700297 }
298
299 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700300 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700301 if (!isAdded()) return;
302
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700303 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700304
305 // Push latest state up to UI
306 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700307 if (result.mode != MODE_UNKNOWN) {
308 state.derivedMode = result.mode;
309 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700310 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700311 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700312
313 updateDisplayState();
314
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700315 // When launched into empty recents, show drawer
316 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700317 ((BaseActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700318 }
319
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700320 // Restore any previous instance state
321 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
322 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
323 getView().restoreHierarchyState(container);
324 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700325 mListView.smoothScrollToPosition(0);
326 mGridView.smoothScrollToPosition(0);
327 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700328
329 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700330 }
331
332 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700333 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700334 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700335 }
336 };
337
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700338 // Kick off loader at least once
339 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
340
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700341 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700342 }
343
Jeff Sharkey42d26792013-09-06 13:22:09 -0700344 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700345 public void onActivityResult(int requestCode, int resultCode, Intent data) {
346 final Context context = getActivity();
347 final Resources res = context.getResources();
348
349 // There's only one request code right now. Replace this with a switch statement or
350 // something more scalable when more codes are added.
351 if (requestCode != REQUEST_COPY_DESTINATION) {
352 return;
353 }
354 if (resultCode == Activity.RESULT_CANCELED || data == null) {
355 // User pressed the back button or otherwise cancelled the destination pick. Don't
356 // proceed with the copy.
357 return;
358 }
359
Ben Kwaf527c632015-04-08 15:03:35 -0700360 // Because the destination picker is launched using an open tree intent, the URI returned is
361 // a tree URI. Convert it to a document URI.
362 // TODO: Remove this step when the destination picker returns a document URI.
363 final Uri destinationTree = data.getData();
364 final Uri destination = DocumentsContract.buildDocumentUriUsingTree(destinationTree,
365 DocumentsContract.getTreeDocumentId(destinationTree));
Ben Kwaf5858932015-04-07 15:43:39 -0700366
367 List<DocumentInfo> docs = mSelectedDocumentsForCopy;
368 Intent copyIntent = new Intent(context, CopyService.class);
369 copyIntent.putParcelableArrayListExtra(CopyService.EXTRA_SRC_LIST,
370 new ArrayList<DocumentInfo>(docs));
371 copyIntent.setData(destination);
372
373 Toast.makeText(context,
374 res.getQuantityString(R.plurals.copy_begin, docs.size(), docs.size()),
375 Toast.LENGTH_SHORT).show();
376 context.startService(copyIntent);
377 }
378
379 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700380 public void onStop() {
381 super.onStop();
382
383 // Remember last scroll location
384 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
385 getView().saveHierarchyState(container);
386 final State state = getDisplayState(this);
387 state.dirState.put(mStateKey, container);
388 }
389
390 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700391 public void onResume() {
392 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700393 updateDisplayState();
394 }
395
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700396 public void onDisplayStateChanged() {
397 updateDisplayState();
398 }
399
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700400 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700401 // Sort order change always triggers reload; we'll trigger state change
402 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700403 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
404 }
405
406 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700407 final ContentResolver resolver = getActivity().getContentResolver();
408 final State state = getDisplayState(this);
409
410 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
411 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
412
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700413 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700414 final Uri stateUri = RecentsProvider.buildState(
415 root.authority, root.rootId, doc.documentId);
416 final ContentValues values = new ContentValues();
417 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700418
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700419 new AsyncTask<Void, Void, Void>() {
420 @Override
421 protected Void doInBackground(Void... params) {
422 resolver.insert(stateUri, values);
423 return null;
424 }
425 }.execute();
426 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700427
428 // Mode change is just visual change; no need to kick loader, and
429 // deliver change event immediately.
430 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700431 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700432
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700433 updateDisplayState();
434 }
435
436 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700437 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700438
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700439 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700440 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700441 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700442
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700443 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
444 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700445
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700446 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700447 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700448 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
449 } else {
450 choiceMode = ListView.CHOICE_MODE_NONE;
451 }
452
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700453 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700454 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700455 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700456 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700457 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700458 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700459 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700460 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700461 mGridView.setChoiceMode(choiceMode);
462 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700463 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700464 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700465 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700466 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700467 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700468 mListView.setChoiceMode(choiceMode);
469 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700470 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700471 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700472 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700473
474 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700475 }
476
477 private OnItemClickListener mItemListener = new OnItemClickListener() {
478 @Override
479 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700480 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700481 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700482 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
483 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
484 if (isDocumentEnabled(docMimeType, docFlags)) {
485 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700486 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700487 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700488 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700489 }
490 };
491
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700492 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
493 @Override
494 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
495 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100496 mode.setTitle(getResources()
497 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700498 return true;
499 }
500
501 @Override
502 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700503 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700504
505 final MenuItem open = menu.findItem(R.id.menu_open);
506 final MenuItem share = menu.findItem(R.id.menu_share);
507 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700508 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700509
510 final boolean manageMode = state.action == ACTION_MANAGE;
511 open.setVisible(!manageMode);
512 share.setVisible(manageMode);
513 delete.setVisible(manageMode);
Ben Kwaf527c632015-04-08 15:03:35 -0700514 // Hide the copy menu item in the recents folder. For now, also hide it by default
515 // unless the debug flag is enabled.
516 copy.setVisible((mType != TYPE_RECENT_OPEN) &&
517 SystemProperties.getBoolean("debug.documentsui.enable_copy", false));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700518
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700519 return true;
520 }
521
522 @Override
523 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700525 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700526 final int size = checked.size();
527 for (int i = 0; i < size; i++) {
528 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700529 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700530 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700531 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700532 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700533 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700534
Jeff Sharkey873daa32013-08-18 17:38:20 -0700535 final int id = item.getItemId();
536 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700537 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700538 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700539 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700540
541 } else if (id == R.id.menu_share) {
542 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700543 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700544 return true;
545
546 } else if (id == R.id.menu_delete) {
547 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700548 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700549 return true;
550
Ben Kwa41b26c12015-03-31 10:11:43 -0700551 } else if (id == R.id.menu_copy) {
552 onCopyDocuments(docs);
553 mode.finish();
554 return true;
555
Ben Kwa512a6ba2015-03-31 08:15:21 -0700556 } else if (id == R.id.menu_select_all) {
557 int count = mCurrentView.getCount();
558 for (int i = 0; i < count; i++) {
559 mCurrentView.setItemChecked(i, true);
560 }
561 updateDisplayState();
562 return true;
563
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700564 } else {
565 return false;
566 }
567 }
568
569 @Override
570 public void onDestroyActionMode(ActionMode mode) {
571 // ignored
572 }
573
574 @Override
575 public void onItemCheckedStateChanged(
576 ActionMode mode, int position, long id, boolean checked) {
577 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700578 // Directories and footer items cannot be checked
579 boolean valid = false;
580
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700581 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700582 if (cursor != null) {
583 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700584 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700585 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700586 }
587
588 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700589 mCurrentView.setItemChecked(position, false);
590 }
591 }
592
593 mode.setTitle(getResources()
594 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
595 }
596 };
597
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700598 private RecyclerListener mRecycleListener = new RecyclerListener() {
599 @Override
600 public void onMovedToScrapHeap(View view) {
601 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
602 if (iconThumb != null) {
603 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
604 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700605 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700606 iconThumb.setTag(null);
607 }
608 }
609 }
610 };
611
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700612 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700613 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700614
615 // Filter out directories - those can't be shared.
616 List<DocumentInfo> docsForSend = Lists.newArrayList();
617 for (DocumentInfo doc: docs) {
618 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
619 docsForSend.add(doc);
620 }
621 }
622
623 if (docsForSend.size() == 1) {
624 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700625
Jeff Sharkey873daa32013-08-18 17:38:20 -0700626 intent = new Intent(Intent.ACTION_SEND);
627 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
628 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700629 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700630 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700631
Ben Kwaf527c632015-04-08 15:03:35 -0700632 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700633 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
634 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
635 intent.addCategory(Intent.CATEGORY_DEFAULT);
636
637 final ArrayList<String> mimeTypes = Lists.newArrayList();
638 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700639 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700640 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700641 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700642 }
643
644 intent.setType(findCommonMimeType(mimeTypes));
645 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
646
647 } else {
648 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700649 }
650
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700651 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700652 startActivity(intent);
653 }
654
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700655 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700656 final Context context = getActivity();
657 final ContentResolver resolver = context.getContentResolver();
658
659 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700660 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700661 if (!doc.isDeleteSupported()) {
662 Log.w(TAG, "Skipping " + doc);
663 hadTrouble = true;
664 continue;
665 }
666
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700667 ContentProviderClient client = null;
668 try {
669 client = DocumentsApplication.acquireUnstableProviderOrThrow(
670 resolver, doc.derivedUri.getAuthority());
671 DocumentsContract.deleteDocument(client, doc.derivedUri);
672 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700673 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700674 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700675 } finally {
676 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700677 }
678 }
679
680 if (hadTrouble) {
681 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
682 }
683 }
684
Ben Kwa41b26c12015-03-31 10:11:43 -0700685 private void onCopyDocuments(List<DocumentInfo> docs) {
Ben Kwaf5858932015-04-07 15:43:39 -0700686 mSelectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700687
Ben Kwaf5858932015-04-07 15:43:39 -0700688 // Pop up a dialog to pick a destination. This is inadequate but works for now.
689 // TODO: Implement a picker that is to spec.
690 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
691 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700692 }
693
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700694 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700695 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700696 }
697
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700698 private static abstract class Footer {
699 private final int mItemViewType;
700
701 public Footer(int itemViewType) {
702 mItemViewType = itemViewType;
703 }
704
705 public abstract View getView(View convertView, ViewGroup parent);
706
707 public int getItemViewType() {
708 return mItemViewType;
709 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700710 }
711
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700712 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700713 public LoadingFooter() {
714 super(1);
715 }
716
Jeff Sharkey20b32272013-09-03 15:25:52 -0700717 @Override
718 public View getView(View convertView, ViewGroup parent) {
719 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700720 final State state = getDisplayState(DirectoryFragment.this);
721
Jeff Sharkey20b32272013-09-03 15:25:52 -0700722 if (convertView == null) {
723 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700724 if (state.derivedMode == MODE_LIST) {
725 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
726 } else if (state.derivedMode == MODE_GRID) {
727 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
728 } else {
729 throw new IllegalStateException();
730 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700731 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700732
Jeff Sharkey20b32272013-09-03 15:25:52 -0700733 return convertView;
734 }
735 }
736
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700737 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700738 private final int mIcon;
739 private final String mMessage;
740
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700741 public MessageFooter(int itemViewType, int icon, String message) {
742 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700743 mIcon = icon;
744 mMessage = message;
745 }
746
747 @Override
748 public View getView(View convertView, ViewGroup parent) {
749 final Context context = parent.getContext();
750 final State state = getDisplayState(DirectoryFragment.this);
751
752 if (convertView == null) {
753 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700754 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700755 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700756 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700757 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
758 } else {
759 throw new IllegalStateException();
760 }
761 }
762
763 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
764 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
765 icon.setImageResource(mIcon);
766 title.setText(mMessage);
767 return convertView;
768 }
769 }
770
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700771 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700772 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700773 private int mCursorCount;
774
775 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700776
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700777 public void swapResult(DirectoryResult result) {
778 mCursor = result != null ? result.cursor : null;
779 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700780
781 mFooters.clear();
782
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700783 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700784 if (extras != null) {
785 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
786 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700787 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700788 }
789 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
790 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700791 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700792 }
793 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
794 mFooters.add(new LoadingFooter());
795 }
796 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700797
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700798 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700799 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700800 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700801 }
802
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700803 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700804 mEmptyView.setVisibility(View.VISIBLE);
805 } else {
806 mEmptyView.setVisibility(View.GONE);
807 }
808
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700809 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700810 }
811
812 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700813 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700814 if (position < mCursorCount) {
815 return getDocumentView(position, convertView, parent);
816 } else {
817 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700818 convertView = mFooters.get(position).getView(convertView, parent);
819 // Only the view itself is disabled; contents inside shouldn't
820 // be dimmed.
821 convertView.setEnabled(false);
822 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700823 }
824 }
825
826 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700827 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700828 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700829
Jeff Sharkey9656a532013-09-13 13:42:19 -0700830 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
831
Jeff Sharkey873daa32013-08-18 17:38:20 -0700832 final RootsCache roots = DocumentsApplication.getRootsCache(context);
833 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
834 context, mThumbSize);
835
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700836 if (convertView == null) {
837 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700838 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700839 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700840 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700841 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
842 } else {
843 throw new IllegalStateException();
844 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700845 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700846
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700847 final Cursor cursor = getItem(position);
848
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700849 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
850 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700851 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
852 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
853 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
854 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
855 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
856 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
857 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
858 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700859
Jeff Sharkey9656a532013-09-13 13:42:19 -0700860 final View line1 = convertView.findViewById(R.id.line1);
861 final View line2 = convertView.findViewById(R.id.line2);
862
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700863 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
864 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700865 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
866 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700867 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700868 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
869 final TextView date = (TextView) convertView.findViewById(R.id.date);
870 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700871
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700872 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700873 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700874 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700875 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700876 }
877
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700878 iconMime.animate().cancel();
879 iconThumb.animate().cancel();
880
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700881 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700882 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700883 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700884 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700885
Jeff Sharkey7e544612014-08-29 15:38:27 -0700886 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
887 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
888
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700889 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700890 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700891 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700892 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700893 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700894 iconThumb.setImageBitmap(cachedResult);
895 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700896 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700897 iconThumb.setImageDrawable(null);
898 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700899 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700900 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700901 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700902 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700903 }
904
905 // Always throw MIME icon into place, even when a thumbnail is being
906 // loaded in background.
907 if (cacheHit) {
908 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700909 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700910 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700911 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700912 iconMime.setAlpha(1f);
913 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700914 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700915 if (docIcon != 0) {
916 iconMime.setImageDrawable(
917 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
918 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700919 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
920 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700921 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700922 }
923
Jeff Sharkey9656a532013-09-13 13:42:19 -0700924 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700925 boolean hasLine2 = false;
926
Jeff Sharkey9656a532013-09-13 13:42:19 -0700927 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
928 if (!hideTitle) {
929 title.setText(docDisplayName);
930 hasLine1 = true;
931 }
932
933 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700934 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700935 // We've already had to enumerate roots before any results can
936 // be shown, so this will never block.
937 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700938 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700939 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700940 } else {
941 iconDrawable = root.loadIcon(context);
942 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700943
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700944 if (summary != null) {
945 final boolean alwaysShowSummary = getResources()
946 .getBoolean(R.bool.always_show_summary);
947 if (alwaysShowSummary) {
948 summary.setText(root.getDirectoryString());
949 summary.setVisibility(View.VISIBLE);
950 hasLine2 = true;
951 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700952 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700953 // No summary needed if icon speaks for itself
954 summary.setVisibility(View.INVISIBLE);
955 } else {
956 summary.setText(root.getDirectoryString());
957 summary.setVisibility(View.VISIBLE);
958 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
959 hasLine2 = true;
960 }
961 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700962 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700963 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700964 // Directories showing thumbnails in grid mode get a little icon
965 // hint to remind user they're a directory.
966 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
967 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700968 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700969 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700970 }
971
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700972 if (summary != null) {
973 if (docSummary != null) {
974 summary.setText(docSummary);
975 summary.setVisibility(View.VISIBLE);
976 hasLine2 = true;
977 } else {
978 summary.setVisibility(View.INVISIBLE);
979 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700980 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700981 }
982
Jeff Sharkey9656a532013-09-13 13:42:19 -0700983 if (icon1 != null) icon1.setVisibility(View.GONE);
984 if (icon2 != null) icon2.setVisibility(View.GONE);
985
986 if (iconDrawable != null) {
987 if (hasLine1) {
988 icon1.setVisibility(View.VISIBLE);
989 icon1.setImageDrawable(iconDrawable);
990 } else {
991 icon2.setVisibility(View.VISIBLE);
992 icon2.setImageDrawable(iconDrawable);
993 }
994 }
995
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700996 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700997 date.setText(null);
998 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700999 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001000 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001001 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001002
1003 if (state.showSize) {
1004 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001005 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001006 size.setText(null);
1007 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001008 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001009 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001010 }
1011 } else {
1012 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001013 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001014
Jeff Sharkey9656a532013-09-13 13:42:19 -07001015 if (line1 != null) {
1016 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1017 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001018 if (line2 != null) {
1019 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1020 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001021
Jeff Sharkey7e544612014-08-29 15:38:27 -07001022 setEnabledRecursive(convertView, enabled);
1023
1024 iconMime.setAlpha(iconAlpha);
1025 iconThumb.setAlpha(iconAlpha);
1026 if (icon1 != null) icon1.setAlpha(iconAlpha);
1027 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001028
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001029 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001030 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001031
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001032 @Override
1033 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001034 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001035 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001036
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001037 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001038 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001039 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001040 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001041 return mCursor;
1042 } else {
1043 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001044 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001045 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001046
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001047 @Override
1048 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001049 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001050 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001051
1052 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001053 public int getViewTypeCount() {
1054 return 4;
1055 }
1056
1057 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001058 public int getItemViewType(int position) {
1059 if (position < mCursorCount) {
1060 return 0;
1061 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001062 position -= mCursorCount;
1063 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001064 }
1065 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001066 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001067
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001068 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1069 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 private final Uri mUri;
1071 private final ImageView mIconMime;
1072 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001073 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001074 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001075 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001076
Jeff Sharkey7e544612014-08-29 15:38:27 -07001077 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1078 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001079 mUri = uri;
1080 mIconMime = iconMime;
1081 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001082 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001083 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001084 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001085 }
1086
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001087 @Override
1088 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001089 cancel(false);
1090 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001091 }
1092
1093 @Override
1094 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001095 if (isCancelled()) return null;
1096
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001097 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001098 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001099
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001100 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001101 Bitmap result = null;
1102 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001103 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1104 resolver, mUri.getAuthority());
1105 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001106 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001107 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1108 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001109 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001110 }
1111 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001112 if (!(e instanceof OperationCanceledException)) {
1113 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1114 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001115 } finally {
1116 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001117 }
1118 return result;
1119 }
1120
1121 @Override
1122 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001123 if (mIconThumb.getTag() == this && result != null) {
1124 mIconThumb.setTag(null);
1125 mIconThumb.setImageBitmap(result);
1126
Jeff Sharkey7e544612014-08-29 15:38:27 -07001127 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001128 mIconMime.animate().alpha(0f).start();
1129 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001130 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001131 }
1132 }
1133 }
1134
1135 private static String formatTime(Context context, long when) {
1136 // TODO: DateUtils should make this easier
1137 Time then = new Time();
1138 then.set(when);
1139 Time now = new Time();
1140 now.setToNow();
1141
1142 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1143 | DateUtils.FORMAT_ABBREV_ALL;
1144
1145 if (then.year != now.year) {
1146 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1147 } else if (then.yearDay != now.yearDay) {
1148 flags |= DateUtils.FORMAT_SHOW_DATE;
1149 } else {
1150 flags |= DateUtils.FORMAT_SHOW_TIME;
1151 }
1152
1153 return DateUtils.formatDateTime(context, when, flags);
1154 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001155
1156 private String findCommonMimeType(List<String> mimeTypes) {
1157 String[] commonType = mimeTypes.get(0).split("/");
1158 if (commonType.length != 2) {
1159 return "*/*";
1160 }
1161
1162 for (int i = 1; i < mimeTypes.size(); i++) {
1163 String[] type = mimeTypes.get(i).split("/");
1164 if (type.length != 2) continue;
1165
1166 if (!commonType[1].equals(type[1])) {
1167 commonType[1] = "*";
1168 }
1169
1170 if (!commonType[0].equals(type[0])) {
1171 commonType[0] = "*";
1172 commonType[1] = "*";
1173 break;
1174 }
1175 }
1176
1177 return commonType[0] + "/" + commonType[1];
1178 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001179
1180 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001181 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001182 if (v.isEnabled() == enabled) return;
1183 v.setEnabled(enabled);
1184
1185 if (v instanceof ViewGroup) {
1186 final ViewGroup vg = (ViewGroup) v;
1187 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1188 setEnabledRecursive(vg.getChildAt(i), enabled);
1189 }
1190 }
1191 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001192
1193 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1194 final State state = getDisplayState(DirectoryFragment.this);
1195
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001196 // Directories are always enabled
1197 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1198 return true;
1199 }
1200
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001201 // Read-only files are disabled when creating
1202 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1203 return false;
1204 }
1205
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001206 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1207 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001208}