blob: 054556e1d8bf23d08c16a2adedb21d82e5d8d6b2 [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 Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Ben Kwaf5858932015-04-07 15:43:39 -070031import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070032import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070033import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070034import android.app.FragmentManager;
35import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070037import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070038import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070039import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070040import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070041import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070043import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070044import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070045import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070046import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070047import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070048import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070049import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070050import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070052import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070054import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070055import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070056import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070057import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070058import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070059import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070060import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070061import android.text.format.Time;
62import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070063import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070064import android.util.SparseBooleanArray;
65import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070066import android.view.DragEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070067import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070068import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070069import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070070import android.view.View;
71import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070072import android.widget.AbsListView;
73import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070074import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070075import android.widget.AdapterView;
76import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070077import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070078import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070079import android.widget.ImageView;
80import android.widget.ListView;
81import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070082import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070083
Steve McKayd0a2a2c2015-03-25 14:35:33 -070084import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070085import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070086import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070087import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090088import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070089import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070090import com.google.android.collect.Lists;
91
92import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070093import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070094import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070095
96/**
97 * Display the documents inside a single directory.
98 */
99public class DirectoryFragment extends Fragment {
100
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700101 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700102 private ListView mListView;
103 private GridView mGridView;
104
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700105 private AbsListView mCurrentView;
106
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700107 public static final int TYPE_NORMAL = 1;
108 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700109 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700110
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700111 public static final int ANIM_NONE = 1;
112 public static final int ANIM_SIDE = 2;
113 public static final int ANIM_DOWN = 3;
114 public static final int ANIM_UP = 4;
115
Ben Kwaf5858932015-04-07 15:43:39 -0700116 public static final int REQUEST_COPY_DESTINATION = 1;
117
Jeff Sharkey5b535922013-08-02 15:55:26 -0700118 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700119 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700120
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700121 private int mLastMode = MODE_UNKNOWN;
122 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700123 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700124
Jeff Sharkey9656a532013-09-13 13:42:19 -0700125 private boolean mHideGridTitles = false;
126
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700127 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700128 private Point mThumbSize;
129
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700130 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700131 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700132
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700133 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700134 private static final String EXTRA_ROOT = "root";
135 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700136 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700137 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700138
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700139 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700141 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
142 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700143 }
144
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700145 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
146 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700147 }
148
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700149 public static void showRecentsOpen(FragmentManager fm, int anim) {
150 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700151 }
152
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700153 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
154 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700155 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700156 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700157 args.putParcelable(EXTRA_ROOT, root);
158 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700159 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700160
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700161 final FragmentTransaction ft = fm.beginTransaction();
162 switch (anim) {
163 case ANIM_SIDE:
164 args.putBoolean(EXTRA_IGNORE_STATE, true);
165 break;
166 case ANIM_DOWN:
167 args.putBoolean(EXTRA_IGNORE_STATE, true);
168 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
169 break;
170 case ANIM_UP:
171 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
172 break;
173 }
174
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700175 final DirectoryFragment fragment = new DirectoryFragment();
176 fragment.setArguments(args);
177
Jeff Sharkey76112212013-08-06 11:26:10 -0700178 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700179 ft.commitAllowingStateLoss();
180 }
181
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700182 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
183 final StringBuilder builder = new StringBuilder();
184 builder.append(root != null ? root.authority : "null").append(';');
185 builder.append(root != null ? root.rootId : "null").append(';');
186 builder.append(doc != null ? doc.documentId : "null");
187 return builder.toString();
188 }
189
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700190 public static DirectoryFragment get(FragmentManager fm) {
191 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700192 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700193 }
194
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700195 @Override
196 public View onCreateView(
197 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
198 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700199 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700200 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
201
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700202 mEmptyView = view.findViewById(android.R.id.empty);
203
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700204 mListView = (ListView) view.findViewById(R.id.list);
205 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700206 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700207 mListView.setRecyclerListener(mRecycleListener);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700208 setupDragAndDropOnDirectoryView(mListView);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700209
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700210 // Indent our list divider to align with text
211 final Drawable divider = mListView.getDivider();
212 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
213 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
214 if (insetLeft) {
215 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
216 } else {
217 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
218 }
219
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700220 mGridView = (GridView) view.findViewById(R.id.grid);
221 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700222 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700223 mGridView.setRecyclerListener(mRecycleListener);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700224 setupDragAndDropOnDirectoryView(mGridView);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700225
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700226 return view;
227 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700228
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700229 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700230 public void onDestroyView() {
231 super.onDestroyView();
232
233 // Cancel any outstanding thumbnail requests
234 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
235 final int count = target.getChildCount();
236 for (int i = 0; i < count; i++) {
237 final View view = target.getChildAt(i);
238 mRecycleListener.onMovedToScrapHeap(view);
239 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700240
241 // Tear down any selection in progress
242 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
243 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700244 }
245
246 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700247 public void onActivityCreated(Bundle savedInstanceState) {
248 super.onActivityCreated(savedInstanceState);
249
250 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700251 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252
Jeff Sharkey9656a532013-09-13 13:42:19 -0700253 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
254 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
255
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700256 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700257 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700258 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700259
Jeff Sharkey9656a532013-09-13 13:42:19 -0700260 if (mType == TYPE_RECENT_OPEN) {
261 // Hide titles when showing recents for picking images/videos
262 mHideGridTitles = MimePredicate.mimeMatches(
263 MimePredicate.VISUAL_MIMES, state.acceptMimes);
264 } else {
265 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
266 }
267
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700268 final ActivityManager am = (ActivityManager) context.getSystemService(
269 Context.ACTIVITY_SERVICE);
270 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
271
Jeff Sharkey46899c82013-08-18 22:26:48 -0700272 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700273 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700274 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700275 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700276
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700277 Uri contentsUri;
278 switch (mType) {
279 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700280 contentsUri = DocumentsContract.buildChildDocumentsUri(
281 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700282 if (state.action == ACTION_MANAGE) {
283 contentsUri = DocumentsContract.setManageMode(contentsUri);
284 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700285 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700286 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700287 case TYPE_SEARCH:
288 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700289 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700290 if (state.action == ACTION_MANAGE) {
291 contentsUri = DocumentsContract.setManageMode(contentsUri);
292 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700293 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700294 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700295 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700296 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700297 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700298 default:
299 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700300 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700301 }
302
303 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700304 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700305 if (!isAdded()) return;
306
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700307 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700308
309 // Push latest state up to UI
310 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700311 if (result.mode != MODE_UNKNOWN) {
312 state.derivedMode = result.mode;
313 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700314 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700315 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700316
317 updateDisplayState();
318
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700319 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700320 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
321 context instanceof DocumentsActivity) {
322 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700323 }
324
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700325 // Restore any previous instance state
326 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
327 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
328 getView().restoreHierarchyState(container);
329 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700330 mListView.smoothScrollToPosition(0);
331 mGridView.smoothScrollToPosition(0);
332 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700333
334 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700335 }
336
337 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700338 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700339 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700340 }
341 };
342
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700343 // Kick off loader at least once
344 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
345
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700346 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700347 }
348
Jeff Sharkey42d26792013-09-06 13:22:09 -0700349 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700350 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700351 // There's only one request code right now. Replace this with a switch statement or
352 // something more scalable when more codes are added.
353 if (requestCode != REQUEST_COPY_DESTINATION) {
354 return;
355 }
356 if (resultCode == Activity.RESULT_CANCELED || data == null) {
357 // User pressed the back button or otherwise cancelled the destination pick. Don't
358 // proceed with the copy.
359 return;
360 }
361
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900362 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
363 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK));
Ben Kwaf5858932015-04-07 15:43:39 -0700364 }
365
366 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700367 public void onStop() {
368 super.onStop();
369
370 // Remember last scroll location
371 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
372 getView().saveHierarchyState(container);
373 final State state = getDisplayState(this);
374 state.dirState.put(mStateKey, container);
375 }
376
377 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700378 public void onResume() {
379 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700380 updateDisplayState();
381 }
382
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700383 public void onDisplayStateChanged() {
384 updateDisplayState();
385 }
386
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700387 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700388 // Sort order change always triggers reload; we'll trigger state change
389 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700390 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
391 }
392
393 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700394 final ContentResolver resolver = getActivity().getContentResolver();
395 final State state = getDisplayState(this);
396
397 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
398 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
399
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700400 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700401 final Uri stateUri = RecentsProvider.buildState(
402 root.authority, root.rootId, doc.documentId);
403 final ContentValues values = new ContentValues();
404 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700405
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700406 new AsyncTask<Void, Void, Void>() {
407 @Override
408 protected Void doInBackground(Void... params) {
409 resolver.insert(stateUri, values);
410 return null;
411 }
412 }.execute();
413 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700414
415 // Mode change is just visual change; no need to kick loader, and
416 // deliver change event immediately.
417 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700418 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700419
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700420 updateDisplayState();
421 }
422
423 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700424 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700425
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700426 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700427 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700428 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700429
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700430 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
431 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700432
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700433 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700434 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700435 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
436 } else {
437 choiceMode = ListView.CHOICE_MODE_NONE;
438 }
439
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700440 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700441 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700442 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700443 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700444 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700445 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700446 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700447 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700448 mGridView.setChoiceMode(choiceMode);
449 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700450 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700451 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700452 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700453 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700454 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700455 mListView.setChoiceMode(choiceMode);
456 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700457 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700458 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700459 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700460
461 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700462 }
463
464 private OnItemClickListener mItemListener = new OnItemClickListener() {
465 @Override
466 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700467 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700468 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700469 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
470 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
471 if (isDocumentEnabled(docMimeType, docFlags)) {
472 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700473 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700474 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700475 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700476 }
477 };
478
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700479 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
480 @Override
481 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
482 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100483 mode.setTitle(getResources()
484 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700485 return true;
486 }
487
488 @Override
489 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700490 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700491
492 final MenuItem open = menu.findItem(R.id.menu_open);
493 final MenuItem share = menu.findItem(R.id.menu_share);
494 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700495 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700496
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700497 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
498 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
499
500 open.setVisible(!manageOrBrowse);
501 share.setVisible(manageOrBrowse);
502 delete.setVisible(manageOrBrowse);
Ben Kwada1cea52015-04-22 10:33:53 -0700503 // Disable copying from the Recents view.
504 copy.setVisible(manageOrBrowse && mType != TYPE_RECENT_OPEN);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700505
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700506 return true;
507 }
508
509 @Override
510 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -0700511 final List<DocumentInfo> docs = getSelectedDocuments();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700512
Jeff Sharkey873daa32013-08-18 17:38:20 -0700513 final int id = item.getItemId();
514 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700515 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700516 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700517 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700518
519 } else if (id == R.id.menu_share) {
520 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700521 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700522 return true;
523
524 } else if (id == R.id.menu_delete) {
525 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700526 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700527 return true;
528
Ben Kwa41b26c12015-03-31 10:11:43 -0700529 } else if (id == R.id.menu_copy) {
530 onCopyDocuments(docs);
531 mode.finish();
532 return true;
533
Ben Kwa512a6ba2015-03-31 08:15:21 -0700534 } else if (id == R.id.menu_select_all) {
535 int count = mCurrentView.getCount();
536 for (int i = 0; i < count; i++) {
537 mCurrentView.setItemChecked(i, true);
538 }
539 updateDisplayState();
540 return true;
541
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700542 } else {
543 return false;
544 }
545 }
546
547 @Override
548 public void onDestroyActionMode(ActionMode mode) {
549 // ignored
550 }
551
552 @Override
553 public void onItemCheckedStateChanged(
554 ActionMode mode, int position, long id, boolean checked) {
555 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700556 // Directories and footer items cannot be checked
557 boolean valid = false;
558
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700559 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700560 if (cursor != null) {
561 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700562 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700563 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700564 }
565
566 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700567 mCurrentView.setItemChecked(position, false);
568 }
569 }
570
571 mode.setTitle(getResources()
572 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
573 }
574 };
575
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700576 private RecyclerListener mRecycleListener = new RecyclerListener() {
577 @Override
578 public void onMovedToScrapHeap(View view) {
579 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
580 if (iconThumb != null) {
581 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
582 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700583 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700584 iconThumb.setTag(null);
585 }
586 }
587 }
588 };
589
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700590 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700591 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700592
593 // Filter out directories - those can't be shared.
594 List<DocumentInfo> docsForSend = Lists.newArrayList();
595 for (DocumentInfo doc: docs) {
596 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
597 docsForSend.add(doc);
598 }
599 }
600
601 if (docsForSend.size() == 1) {
602 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700603
Jeff Sharkey873daa32013-08-18 17:38:20 -0700604 intent = new Intent(Intent.ACTION_SEND);
605 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
606 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700607 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700608 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700609
Ben Kwaf527c632015-04-08 15:03:35 -0700610 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700611 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
612 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
613 intent.addCategory(Intent.CATEGORY_DEFAULT);
614
615 final ArrayList<String> mimeTypes = Lists.newArrayList();
616 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700617 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700618 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700619 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700620 }
621
622 intent.setType(findCommonMimeType(mimeTypes));
623 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
624
625 } else {
626 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700627 }
628
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700629 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700630 startActivity(intent);
631 }
632
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700633 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700634 final Context context = getActivity();
635 final ContentResolver resolver = context.getContentResolver();
636
637 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700638 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700639 if (!doc.isDeleteSupported()) {
640 Log.w(TAG, "Skipping " + doc);
641 hadTrouble = true;
642 continue;
643 }
644
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700645 ContentProviderClient client = null;
646 try {
647 client = DocumentsApplication.acquireUnstableProviderOrThrow(
648 resolver, doc.derivedUri.getAuthority());
649 DocumentsContract.deleteDocument(client, doc.derivedUri);
650 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700651 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700652 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700653 } finally {
654 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700655 }
656 }
657
658 if (hadTrouble) {
659 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
660 }
661 }
662
Ben Kwa41b26c12015-03-31 10:11:43 -0700663 private void onCopyDocuments(List<DocumentInfo> docs) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900664 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700665
Ben Kwaf5858932015-04-07 15:43:39 -0700666 // Pop up a dialog to pick a destination. This is inadequate but works for now.
667 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900668 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900669 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900670 Uri.EMPTY,
671 getActivity(),
672 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900673 boolean directoryCopy = false;
674 for (DocumentInfo info : docs) {
675 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
676 directoryCopy = true;
677 break;
678 }
679 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900680 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwaf5858932015-04-07 15:43:39 -0700681 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700682 }
683
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700684 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700685 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700686 }
687
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700688 private static abstract class Footer {
689 private final int mItemViewType;
690
691 public Footer(int itemViewType) {
692 mItemViewType = itemViewType;
693 }
694
695 public abstract View getView(View convertView, ViewGroup parent);
696
697 public int getItemViewType() {
698 return mItemViewType;
699 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700700 }
701
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700702 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700703 public LoadingFooter() {
704 super(1);
705 }
706
Jeff Sharkey20b32272013-09-03 15:25:52 -0700707 @Override
708 public View getView(View convertView, ViewGroup parent) {
709 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700710 final State state = getDisplayState(DirectoryFragment.this);
711
Jeff Sharkey20b32272013-09-03 15:25:52 -0700712 if (convertView == null) {
713 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700714 if (state.derivedMode == MODE_LIST) {
715 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
716 } else if (state.derivedMode == MODE_GRID) {
717 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
718 } else {
719 throw new IllegalStateException();
720 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700721 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700722
Jeff Sharkey20b32272013-09-03 15:25:52 -0700723 return convertView;
724 }
725 }
726
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700727 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700728 private final int mIcon;
729 private final String mMessage;
730
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700731 public MessageFooter(int itemViewType, int icon, String message) {
732 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700733 mIcon = icon;
734 mMessage = message;
735 }
736
737 @Override
738 public View getView(View convertView, ViewGroup parent) {
739 final Context context = parent.getContext();
740 final State state = getDisplayState(DirectoryFragment.this);
741
742 if (convertView == null) {
743 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700744 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700745 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700746 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700747 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
748 } else {
749 throw new IllegalStateException();
750 }
751 }
752
753 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
754 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
755 icon.setImageResource(mIcon);
756 title.setText(mMessage);
757 return convertView;
758 }
759 }
760
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700761 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700762 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700763 private int mCursorCount;
764
765 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700766
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700767 public void swapResult(DirectoryResult result) {
768 mCursor = result != null ? result.cursor : null;
769 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700770
771 mFooters.clear();
772
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700773 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700774 if (extras != null) {
775 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
776 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700777 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700778 }
779 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
780 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700781 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700782 }
783 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
784 mFooters.add(new LoadingFooter());
785 }
786 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700787
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700788 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700789 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700790 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700791 }
792
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700793 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700794 mEmptyView.setVisibility(View.VISIBLE);
795 } else {
796 mEmptyView.setVisibility(View.GONE);
797 }
798
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700799 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700800 }
801
802 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700803 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700804 if (position < mCursorCount) {
805 return getDocumentView(position, convertView, parent);
806 } else {
807 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700808 convertView = mFooters.get(position).getView(convertView, parent);
809 // Only the view itself is disabled; contents inside shouldn't
810 // be dimmed.
811 convertView.setEnabled(false);
812 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700813 }
814 }
815
816 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700817 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700818 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700819
Jeff Sharkey9656a532013-09-13 13:42:19 -0700820 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
821
Jeff Sharkey873daa32013-08-18 17:38:20 -0700822 final RootsCache roots = DocumentsApplication.getRootsCache(context);
823 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
824 context, mThumbSize);
825
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700826 if (convertView == null) {
827 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700828 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700829 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700830 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700831 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
832 } else {
833 throw new IllegalStateException();
834 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700835 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700836
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700837 final Cursor cursor = getItem(position);
838
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700839 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
840 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700841 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
842 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
843 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
844 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
845 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
846 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
847 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
848 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700849
Jeff Sharkey9656a532013-09-13 13:42:19 -0700850 final View line1 = convertView.findViewById(R.id.line1);
851 final View line2 = convertView.findViewById(R.id.line2);
852
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700853 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
854 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700855 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
856 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700857 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700858 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
859 final TextView date = (TextView) convertView.findViewById(R.id.date);
860 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700861
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700862 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700863 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700864 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700865 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700866 }
867
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700868 iconMime.animate().cancel();
869 iconThumb.animate().cancel();
870
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700871 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700872 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700873 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700874 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700875
Jeff Sharkey7e544612014-08-29 15:38:27 -0700876 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
877 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
878
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700879 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700880 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700881 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700882 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700883 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700884 iconThumb.setImageBitmap(cachedResult);
885 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700886 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700887 iconThumb.setImageDrawable(null);
888 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700889 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700890 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700891 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700892 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700893 }
894
895 // Always throw MIME icon into place, even when a thumbnail is being
896 // loaded in background.
897 if (cacheHit) {
898 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700899 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700900 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700901 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700902 iconMime.setAlpha(1f);
903 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700904 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700905 iconMime.setImageDrawable(
906 getDocumentIcon(context, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700907 }
908
Jeff Sharkey9656a532013-09-13 13:42:19 -0700909 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700910 boolean hasLine2 = false;
911
Jeff Sharkey9656a532013-09-13 13:42:19 -0700912 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
913 if (!hideTitle) {
914 title.setText(docDisplayName);
915 hasLine1 = true;
916 }
917
918 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700919 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700920 // We've already had to enumerate roots before any results can
921 // be shown, so this will never block.
922 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700923 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700924 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700925 } else {
926 iconDrawable = root.loadIcon(context);
927 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700928
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700929 if (summary != null) {
930 final boolean alwaysShowSummary = getResources()
931 .getBoolean(R.bool.always_show_summary);
932 if (alwaysShowSummary) {
933 summary.setText(root.getDirectoryString());
934 summary.setVisibility(View.VISIBLE);
935 hasLine2 = true;
936 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700937 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700938 // No summary needed if icon speaks for itself
939 summary.setVisibility(View.INVISIBLE);
940 } else {
941 summary.setText(root.getDirectoryString());
942 summary.setVisibility(View.VISIBLE);
943 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
944 hasLine2 = true;
945 }
946 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700947 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700948 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700949 // Directories showing thumbnails in grid mode get a little icon
950 // hint to remind user they're a directory.
951 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
952 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700953 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700954 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700955 }
956
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700957 if (summary != null) {
958 if (docSummary != null) {
959 summary.setText(docSummary);
960 summary.setVisibility(View.VISIBLE);
961 hasLine2 = true;
962 } else {
963 summary.setVisibility(View.INVISIBLE);
964 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700965 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700966 }
967
Jeff Sharkey9656a532013-09-13 13:42:19 -0700968 if (icon1 != null) icon1.setVisibility(View.GONE);
969 if (icon2 != null) icon2.setVisibility(View.GONE);
970
971 if (iconDrawable != null) {
972 if (hasLine1) {
973 icon1.setVisibility(View.VISIBLE);
974 icon1.setImageDrawable(iconDrawable);
975 } else {
976 icon2.setVisibility(View.VISIBLE);
977 icon2.setImageDrawable(iconDrawable);
978 }
979 }
980
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700981 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700982 date.setText(null);
983 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700984 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700985 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700986 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700987
988 if (state.showSize) {
989 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700990 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700991 size.setText(null);
992 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700993 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700994 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700995 }
996 } else {
997 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700998 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700999
Jeff Sharkey9656a532013-09-13 13:42:19 -07001000 if (line1 != null) {
1001 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1002 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001003 if (line2 != null) {
1004 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1005 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001006
Jeff Sharkey7e544612014-08-29 15:38:27 -07001007 setEnabledRecursive(convertView, enabled);
1008
1009 iconMime.setAlpha(iconAlpha);
1010 iconThumb.setAlpha(iconAlpha);
1011 if (icon1 != null) icon1.setAlpha(iconAlpha);
1012 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001013
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001014 setupDragAndDropOnDocumentView(convertView, cursor);
1015
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001016 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001017 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001018
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001019 @Override
1020 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001021 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001022 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001023
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001024 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001025 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001026 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001027 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001028 return mCursor;
1029 } else {
1030 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001031 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001032 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001033
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001034 @Override
1035 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001036 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001037 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001038
1039 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001040 public int getViewTypeCount() {
1041 return 4;
1042 }
1043
1044 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001045 public int getItemViewType(int position) {
1046 if (position < mCursorCount) {
1047 return 0;
1048 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001049 position -= mCursorCount;
1050 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001051 }
1052 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001053 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001054
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001055 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1056 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001057 private final Uri mUri;
1058 private final ImageView mIconMime;
1059 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001060 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001061 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001062 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001063
Jeff Sharkey7e544612014-08-29 15:38:27 -07001064 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1065 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001066 mUri = uri;
1067 mIconMime = iconMime;
1068 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001069 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001070 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001071 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001072 }
1073
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001074 @Override
1075 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001076 cancel(false);
1077 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001078 }
1079
1080 @Override
1081 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001082 if (isCancelled()) return null;
1083
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001084 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001085 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001086
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001087 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001088 Bitmap result = null;
1089 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001090 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1091 resolver, mUri.getAuthority());
1092 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001093 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001094 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1095 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001096 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001097 }
1098 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001099 if (!(e instanceof OperationCanceledException)) {
1100 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1101 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001102 } finally {
1103 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001104 }
1105 return result;
1106 }
1107
1108 @Override
1109 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001110 if (mIconThumb.getTag() == this && result != null) {
1111 mIconThumb.setTag(null);
1112 mIconThumb.setImageBitmap(result);
1113
Jeff Sharkey7e544612014-08-29 15:38:27 -07001114 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001115 mIconMime.animate().alpha(0f).start();
1116 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001117 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001118 }
1119 }
1120 }
1121
1122 private static String formatTime(Context context, long when) {
1123 // TODO: DateUtils should make this easier
1124 Time then = new Time();
1125 then.set(when);
1126 Time now = new Time();
1127 now.setToNow();
1128
1129 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1130 | DateUtils.FORMAT_ABBREV_ALL;
1131
1132 if (then.year != now.year) {
1133 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1134 } else if (then.yearDay != now.yearDay) {
1135 flags |= DateUtils.FORMAT_SHOW_DATE;
1136 } else {
1137 flags |= DateUtils.FORMAT_SHOW_TIME;
1138 }
1139
1140 return DateUtils.formatDateTime(context, when, flags);
1141 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001142
1143 private String findCommonMimeType(List<String> mimeTypes) {
1144 String[] commonType = mimeTypes.get(0).split("/");
1145 if (commonType.length != 2) {
1146 return "*/*";
1147 }
1148
1149 for (int i = 1; i < mimeTypes.size(); i++) {
1150 String[] type = mimeTypes.get(i).split("/");
1151 if (type.length != 2) continue;
1152
1153 if (!commonType[1].equals(type[1])) {
1154 commonType[1] = "*";
1155 }
1156
1157 if (!commonType[0].equals(type[0])) {
1158 commonType[0] = "*";
1159 commonType[1] = "*";
1160 break;
1161 }
1162 }
1163
1164 return commonType[0] + "/" + commonType[1];
1165 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001166
1167 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001168 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001169 if (v.isEnabled() == enabled) return;
1170 v.setEnabled(enabled);
1171
1172 if (v instanceof ViewGroup) {
1173 final ViewGroup vg = (ViewGroup) v;
1174 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1175 setEnabledRecursive(vg.getChildAt(i), enabled);
1176 }
1177 }
1178 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001179
1180 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1181 final State state = getDisplayState(DirectoryFragment.this);
1182
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001183 // Directories are always enabled
1184 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1185 return true;
1186 }
1187
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001188 // Read-only files are disabled when creating
1189 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1190 return false;
1191 }
1192
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001193 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1194 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001195
1196 public List<DocumentInfo> getSelectedDocuments() {
1197 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
1198 final List<DocumentInfo> docs = Lists.newArrayList();
1199 final int size = checked.size();
1200 for (int i = 0; i < size; i++) {
1201 if (checked.valueAt(i)) {
1202 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
1203 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1204 docs.add(doc);
1205 }
1206 }
1207 return docs;
1208 }
1209
1210 private void copyFromClipData(ClipData clipData, DocumentInfo dstDir) {
1211 final List<DocumentInfo> srcDocs = getDocumentsFromClipData(clipData);
1212
1213 if (srcDocs.isEmpty())
1214 return;
1215
1216 final DocumentStack curStack = getDisplayState(this).stack;
1217 DocumentStack tmpStack = new DocumentStack();
1218 if (dstDir != null) {
1219 tmpStack.push(dstDir);
1220 tmpStack.addAll(curStack);
1221 } else {
1222 tmpStack = curStack;
1223 }
1224
1225 CopyService.start(getActivity(), srcDocs, tmpStack);
1226 }
1227
1228 private List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
1229 final List<DocumentInfo> srcDocs = Lists.newArrayList();
1230
1231 Context context = getActivity();
1232 final ContentResolver resolver = context.getContentResolver();
1233
1234 int itemCount = clipData.getItemCount();
1235 for (int i = 0; i < itemCount; ++i) {
1236 ClipData.Item item = clipData.getItemAt(i);
1237 Uri itemUri = item.getUri();
1238 if (itemUri != null && DocumentsContract.isDocumentUri(context, itemUri)) {
1239 try {
1240 Cursor cursor = resolver.query(itemUri, null, null, null, null);
1241 cursor.moveToPosition(0);
1242 srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority()));
1243 } catch (Exception e) {
1244 Log.e(TAG, e.getMessage());
1245 }
1246 }
1247 }
1248
1249 return srcDocs;
1250 }
1251
1252 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1253 Context context = getActivity();
1254 final ContentResolver resolver = context.getContentResolver();
1255 ClipData clipData = null;
1256 for (DocumentInfo doc : docs) {
1257 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1258 if (clipData == null) {
1259 clipData = ClipData.newUri(resolver, "", uri);
1260 } else {
1261 // TODO: update list of mime types in ClipData.
1262 clipData.addItem(new ClipData.Item(uri));
1263 }
1264 }
1265 return clipData;
1266 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001267
1268 private void setupDragAndDropOnDirectoryView(AbsListView view) {
1269 // Listen for drops on non-directory items and empty space.
1270 view.setOnDragListener(mOnDragListener);
1271 }
1272
1273 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1274 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1275 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1276 // Make a directory item a drop target. Drop on non-directories and empty space
1277 // is handled at the list/grid view level.
1278 view.setOnDragListener(mOnDragListener);
1279 }
1280
1281 // Temporary: attaching the listener to the title only.
1282 // Attaching to the entire item conflicts with the item long click handler responsible
1283 // for item selection.
1284 final View title = view.findViewById(android.R.id.title);
1285 title.setOnLongClickListener(mLongClickListener);
1286 }
1287
1288 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1289 @Override
1290 public boolean onDrag(View v, DragEvent event) {
1291 switch (event.getAction()) {
1292 case DragEvent.ACTION_DRAG_STARTED:
1293 // TODO: Check if the event contains droppable data.
1294 return true;
1295
1296 // TODO: Highlight potential drop target directory?
1297 // TODO: Expand drop target directory on hover?
1298 case DragEvent.ACTION_DRAG_ENTERED:
1299 case DragEvent.ACTION_DRAG_LOCATION:
1300 case DragEvent.ACTION_DRAG_EXITED:
1301 case DragEvent.ACTION_DRAG_ENDED:
1302 return true;
1303
1304 case DragEvent.ACTION_DROP:
1305 int dstPosition = mCurrentView.getPositionForView(v);
1306 DocumentInfo dstDir = null;
1307 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1308 Cursor dstCursor = mAdapter.getItem(dstPosition);
1309 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1310 // TODO: Do not drop into the directory where the documents came from.
1311 }
1312 copyFromClipData(event.getClipData(), dstDir);
1313 return true;
1314 }
1315 return false;
1316 }
1317 };
1318
1319 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1320 @Override
1321 public boolean onLongClick(View v) {
1322 final List<DocumentInfo> docs = getDraggableDocuments(v);
1323 if (docs.isEmpty()) {
1324 return false;
1325 }
1326 v.startDrag(
1327 getClipDataFromDocuments(docs),
1328 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1329 null,
1330 View.DRAG_FLAG_GLOBAL
1331 );
1332 return true;
1333 }
1334 };
1335
1336 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
1337 final int position = mCurrentView.getPositionForView(currentItemView);
1338 if (position == android.widget.AdapterView.INVALID_POSITION) {
1339 return Collections.EMPTY_LIST;
1340 }
1341
1342 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1343 if (!selectedDocs.isEmpty()) {
1344 if (!mCurrentView.isItemChecked(position)) {
1345 // There is a selection that does not include the current item, drag nothing.
1346 return Collections.EMPTY_LIST;
1347 }
1348 return selectedDocs;
1349 }
1350
1351 final Cursor cursor = mAdapter.getItem(position);
1352 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1353 return Lists.newArrayList(doc);
1354 }
1355
1356 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1357 if (docs.size() == 1) {
1358 final DocumentInfo doc = docs.get(0);
1359 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1360 doc.mimeType, doc.icon, getDisplayState(this));
1361 }
1362 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1363 }
1364
1365 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1366 String docMimeType, int docIcon, State state) {
1367 if (docIcon != 0) {
1368 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1369 } else {
1370 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1371 state.derivedMode);
1372 }
1373 }
1374
1375 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1376
1377 private final Drawable mShadow;
1378
1379 private final int mShadowDimension;
1380
1381 public DrawableShadowBuilder(Drawable shadow) {
1382 mShadow = shadow;
1383 mShadowDimension = getResources().getDimensionPixelSize(
1384 R.dimen.drag_shadow_size);
1385 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1386 }
1387
1388 public void onProvideShadowMetrics(
1389 Point shadowSize, Point shadowTouchPoint) {
1390 shadowSize.set(mShadowDimension, mShadowDimension);
1391 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1392 }
1393
1394 public void onDrawShadow(Canvas canvas) {
1395 mShadow.draw(canvas);
1396 }
1397 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001398}