blob: 7433c217e2c08c1811ea7e7115e4ab59e038eebe [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;
Steve McKay0599a442015-05-05 14:50:00 -070031
Ben Kwaf5858932015-04-07 15:43:39 -070032import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070033import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070034import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070035import android.app.FragmentManager;
36import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070038import android.content.ClipData;
Steve McKay0599a442015-05-05 14:50:00 -070039import android.content.ClipboardManager;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070040import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070041import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070042import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070043import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070044import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070045import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070046import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070047import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070048import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070049import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070051import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070052import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070054import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070056import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070057import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070058import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070059import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070060import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070061import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070062import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070063import android.text.format.Time;
64import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070065import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070066import android.util.SparseBooleanArray;
67import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070068import android.view.DragEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070069import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070070import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070071import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070072import android.view.View;
73import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070074import android.widget.AbsListView;
75import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070076import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070077import android.widget.AdapterView;
78import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070079import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070080import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070081import android.widget.ImageView;
82import android.widget.ListView;
83import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070084import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070085
Steve McKayd0a2a2c2015-03-25 14:35:33 -070086import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070087import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070088import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070089import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090090import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070091import com.android.documentsui.model.RootInfo;
Steve McKay0599a442015-05-05 14:50:00 -070092
Jeff Sharkeyc317af82013-07-01 16:56:54 -070093import com.google.android.collect.Lists;
94
95import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070096import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070097import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070098
99/**
100 * Display the documents inside a single directory.
101 */
102public class DirectoryFragment extends Fragment {
103
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700104 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700105 private ListView mListView;
106 private GridView mGridView;
107
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700108 private AbsListView mCurrentView;
109
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700110 public static final int TYPE_NORMAL = 1;
111 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700112 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700113
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700114 public static final int ANIM_NONE = 1;
115 public static final int ANIM_SIDE = 2;
116 public static final int ANIM_DOWN = 3;
117 public static final int ANIM_UP = 4;
118
Ben Kwaf5858932015-04-07 15:43:39 -0700119 public static final int REQUEST_COPY_DESTINATION = 1;
120
Jeff Sharkey5b535922013-08-02 15:55:26 -0700121 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700122 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700123
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700124 private int mLastMode = MODE_UNKNOWN;
125 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700126 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700127
Jeff Sharkey9656a532013-09-13 13:42:19 -0700128 private boolean mHideGridTitles = false;
129
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700130 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700131 private Point mThumbSize;
132
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700134 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700135
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700136 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700137 private static final String EXTRA_ROOT = "root";
138 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700139 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700140 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700141
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700142 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700143
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700144 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
145 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700146 }
147
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700148 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
149 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700150 }
151
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700152 public static void showRecentsOpen(FragmentManager fm, int anim) {
153 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700154 }
155
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700156 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
157 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700158 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700159 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700160 args.putParcelable(EXTRA_ROOT, root);
161 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700162 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700163
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700164 final FragmentTransaction ft = fm.beginTransaction();
165 switch (anim) {
166 case ANIM_SIDE:
167 args.putBoolean(EXTRA_IGNORE_STATE, true);
168 break;
169 case ANIM_DOWN:
170 args.putBoolean(EXTRA_IGNORE_STATE, true);
171 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
172 break;
173 case ANIM_UP:
174 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
175 break;
176 }
177
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700178 final DirectoryFragment fragment = new DirectoryFragment();
179 fragment.setArguments(args);
180
Jeff Sharkey76112212013-08-06 11:26:10 -0700181 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700182 ft.commitAllowingStateLoss();
183 }
184
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700185 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
186 final StringBuilder builder = new StringBuilder();
187 builder.append(root != null ? root.authority : "null").append(';');
188 builder.append(root != null ? root.rootId : "null").append(';');
189 builder.append(doc != null ? doc.documentId : "null");
190 return builder.toString();
191 }
192
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700193 public static DirectoryFragment get(FragmentManager fm) {
194 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700195 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700196 }
197
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700198 @Override
199 public View onCreateView(
200 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
201 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700202 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700203 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
204
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700205 mEmptyView = view.findViewById(android.R.id.empty);
206
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700207 mListView = (ListView) view.findViewById(R.id.list);
208 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700209 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700210 mListView.setRecyclerListener(mRecycleListener);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700211 setupDragAndDropOnDirectoryView(mListView);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700212
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700213 // Indent our list divider to align with text
214 final Drawable divider = mListView.getDivider();
215 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
216 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
217 if (insetLeft) {
218 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
219 } else {
220 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
221 }
222
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700223 mGridView = (GridView) view.findViewById(R.id.grid);
224 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700225 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700226 mGridView.setRecyclerListener(mRecycleListener);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700227 setupDragAndDropOnDirectoryView(mGridView);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700228
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700229 return view;
230 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700231
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700232 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700233 public void onDestroyView() {
234 super.onDestroyView();
235
236 // Cancel any outstanding thumbnail requests
237 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
238 final int count = target.getChildCount();
239 for (int i = 0; i < count; i++) {
240 final View view = target.getChildAt(i);
241 mRecycleListener.onMovedToScrapHeap(view);
242 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700243
244 // Tear down any selection in progress
245 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
246 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700247 }
248
249 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700250 public void onActivityCreated(Bundle savedInstanceState) {
251 super.onActivityCreated(savedInstanceState);
252
253 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700254 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255
Jeff Sharkey9656a532013-09-13 13:42:19 -0700256 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
257 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
258
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700259 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700260 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700261 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700262
Jeff Sharkey9656a532013-09-13 13:42:19 -0700263 if (mType == TYPE_RECENT_OPEN) {
264 // Hide titles when showing recents for picking images/videos
265 mHideGridTitles = MimePredicate.mimeMatches(
266 MimePredicate.VISUAL_MIMES, state.acceptMimes);
267 } else {
268 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
269 }
270
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700271 final ActivityManager am = (ActivityManager) context.getSystemService(
272 Context.ACTIVITY_SERVICE);
273 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
274
Jeff Sharkey46899c82013-08-18 22:26:48 -0700275 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700276 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700277 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700278 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700279
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700280 Uri contentsUri;
281 switch (mType) {
282 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700283 contentsUri = DocumentsContract.buildChildDocumentsUri(
284 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700285 if (state.action == ACTION_MANAGE) {
286 contentsUri = DocumentsContract.setManageMode(contentsUri);
287 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700288 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700289 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700290 case TYPE_SEARCH:
291 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700292 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700293 if (state.action == ACTION_MANAGE) {
294 contentsUri = DocumentsContract.setManageMode(contentsUri);
295 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700296 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700297 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700298 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700299 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700300 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700301 default:
302 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700303 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700304 }
305
306 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700307 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700308 if (!isAdded()) return;
309
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700310 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700311
312 // Push latest state up to UI
313 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700314 if (result.mode != MODE_UNKNOWN) {
315 state.derivedMode = result.mode;
316 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700317 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700318 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700319
320 updateDisplayState();
321
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700322 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700323 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
324 context instanceof DocumentsActivity) {
325 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700326 }
327
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700328 // Restore any previous instance state
329 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
330 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
331 getView().restoreHierarchyState(container);
332 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700333 mListView.smoothScrollToPosition(0);
334 mGridView.smoothScrollToPosition(0);
335 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700336
337 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700338 }
339
340 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700341 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700342 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700343 }
344 };
345
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700346 // Kick off loader at least once
347 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
348
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700349 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700350 }
351
Jeff Sharkey42d26792013-09-06 13:22:09 -0700352 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700353 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700354 // There's only one request code right now. Replace this with a switch statement or
355 // something more scalable when more codes are added.
356 if (requestCode != REQUEST_COPY_DESTINATION) {
357 return;
358 }
359 if (resultCode == Activity.RESULT_CANCELED || data == null) {
360 // User pressed the back button or otherwise cancelled the destination pick. Don't
361 // proceed with the copy.
362 return;
363 }
364
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900365 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
366 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK));
Ben Kwaf5858932015-04-07 15:43:39 -0700367 }
368
369 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700370 public void onStop() {
371 super.onStop();
372
373 // Remember last scroll location
374 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
375 getView().saveHierarchyState(container);
376 final State state = getDisplayState(this);
377 state.dirState.put(mStateKey, container);
378 }
379
380 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700381 public void onResume() {
382 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700383 updateDisplayState();
384 }
385
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700386 public void onDisplayStateChanged() {
387 updateDisplayState();
388 }
389
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700390 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700391 // Sort order change always triggers reload; we'll trigger state change
392 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700393 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
394 }
395
396 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700397 final ContentResolver resolver = getActivity().getContentResolver();
398 final State state = getDisplayState(this);
399
400 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
401 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
402
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700403 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700404 final Uri stateUri = RecentsProvider.buildState(
405 root.authority, root.rootId, doc.documentId);
406 final ContentValues values = new ContentValues();
407 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700408
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700409 new AsyncTask<Void, Void, Void>() {
410 @Override
411 protected Void doInBackground(Void... params) {
412 resolver.insert(stateUri, values);
413 return null;
414 }
415 }.execute();
416 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700417
418 // Mode change is just visual change; no need to kick loader, and
419 // deliver change event immediately.
420 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700421 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700422
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700423 updateDisplayState();
424 }
425
426 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700427 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700428
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700429 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700430 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700431 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700432
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700433 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
434 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700435
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700436 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700437 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700438 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
439 } else {
440 choiceMode = ListView.CHOICE_MODE_NONE;
441 }
442
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700443 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700444 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700445 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700446 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700448 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700449 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700450 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700451 mGridView.setChoiceMode(choiceMode);
452 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700453 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700454 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700455 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700456 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700457 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700458 mListView.setChoiceMode(choiceMode);
459 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700460 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700461 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700462 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700463
464 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700465 }
466
467 private OnItemClickListener mItemListener = new OnItemClickListener() {
468 @Override
469 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700470 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700471 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700472 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
473 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
474 if (isDocumentEnabled(docMimeType, docFlags)) {
475 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700476 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700477 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700478 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700479 }
480 };
481
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700482 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
483 @Override
484 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
485 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100486 mode.setTitle(getResources()
487 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700488 return true;
489 }
490
491 @Override
492 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700493 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700494
495 final MenuItem open = menu.findItem(R.id.menu_open);
496 final MenuItem share = menu.findItem(R.id.menu_share);
497 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700498 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700500 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
501 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
502
503 open.setVisible(!manageOrBrowse);
504 share.setVisible(manageOrBrowse);
505 delete.setVisible(manageOrBrowse);
Ben Kwada1cea52015-04-22 10:33:53 -0700506 // Disable copying from the Recents view.
507 copy.setVisible(manageOrBrowse && mType != TYPE_RECENT_OPEN);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700508
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700509 return true;
510 }
511
512 @Override
513 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -0700514 final List<DocumentInfo> docs = getSelectedDocuments();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700515
Jeff Sharkey873daa32013-08-18 17:38:20 -0700516 final int id = item.getItemId();
517 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700518 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700519 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700520 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700521
522 } else if (id == R.id.menu_share) {
523 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700524 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700525 return true;
526
527 } else if (id == R.id.menu_delete) {
528 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700529 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700530 return true;
531
Ben Kwa41b26c12015-03-31 10:11:43 -0700532 } else if (id == R.id.menu_copy) {
533 onCopyDocuments(docs);
534 mode.finish();
535 return true;
536
Ben Kwa512a6ba2015-03-31 08:15:21 -0700537 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700538 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700539 return true;
540
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700541 } else {
542 return false;
543 }
544 }
545
546 @Override
547 public void onDestroyActionMode(ActionMode mode) {
548 // ignored
549 }
550
551 @Override
552 public void onItemCheckedStateChanged(
553 ActionMode mode, int position, long id, boolean checked) {
554 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700555 // Directories and footer items cannot be checked
556 boolean valid = false;
557
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700558 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700559 if (cursor != null) {
560 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700561 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700562 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700563 }
564
565 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700566 mCurrentView.setItemChecked(position, false);
567 }
568 }
569
570 mode.setTitle(getResources()
571 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
572 }
573 };
574
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700575 private RecyclerListener mRecycleListener = new RecyclerListener() {
576 @Override
577 public void onMovedToScrapHeap(View view) {
578 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
579 if (iconThumb != null) {
580 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
581 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700582 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700583 iconThumb.setTag(null);
584 }
585 }
586 }
587 };
588
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700589 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700590 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700591
592 // Filter out directories - those can't be shared.
593 List<DocumentInfo> docsForSend = Lists.newArrayList();
594 for (DocumentInfo doc: docs) {
595 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
596 docsForSend.add(doc);
597 }
598 }
599
600 if (docsForSend.size() == 1) {
601 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700602
Jeff Sharkey873daa32013-08-18 17:38:20 -0700603 intent = new Intent(Intent.ACTION_SEND);
604 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
605 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700606 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700607 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700608
Ben Kwaf527c632015-04-08 15:03:35 -0700609 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700610 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
611 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
612 intent.addCategory(Intent.CATEGORY_DEFAULT);
613
614 final ArrayList<String> mimeTypes = Lists.newArrayList();
615 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700616 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700617 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700618 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700619 }
620
621 intent.setType(findCommonMimeType(mimeTypes));
622 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
623
624 } else {
625 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700626 }
627
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700628 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700629 startActivity(intent);
630 }
631
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700632 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700633 final Context context = getActivity();
634 final ContentResolver resolver = context.getContentResolver();
635
636 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700637 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700638 if (!doc.isDeleteSupported()) {
639 Log.w(TAG, "Skipping " + doc);
640 hadTrouble = true;
641 continue;
642 }
643
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700644 ContentProviderClient client = null;
645 try {
646 client = DocumentsApplication.acquireUnstableProviderOrThrow(
647 resolver, doc.derivedUri.getAuthority());
648 DocumentsContract.deleteDocument(client, doc.derivedUri);
649 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700650 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700651 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700652 } finally {
653 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700654 }
655 }
656
657 if (hadTrouble) {
658 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
659 }
660 }
661
Ben Kwa41b26c12015-03-31 10:11:43 -0700662 private void onCopyDocuments(List<DocumentInfo> docs) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900663 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700664
Ben Kwaf5858932015-04-07 15:43:39 -0700665 // Pop up a dialog to pick a destination. This is inadequate but works for now.
666 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900667 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900668 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900669 Uri.EMPTY,
670 getActivity(),
671 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900672 boolean directoryCopy = false;
673 for (DocumentInfo info : docs) {
674 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
675 directoryCopy = true;
676 break;
677 }
678 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900679 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwaf5858932015-04-07 15:43:39 -0700680 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700681 }
682
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700683 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700684 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700685 }
686
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700687 private static abstract class Footer {
688 private final int mItemViewType;
689
690 public Footer(int itemViewType) {
691 mItemViewType = itemViewType;
692 }
693
694 public abstract View getView(View convertView, ViewGroup parent);
695
696 public int getItemViewType() {
697 return mItemViewType;
698 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700699 }
700
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700701 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700702 public LoadingFooter() {
703 super(1);
704 }
705
Jeff Sharkey20b32272013-09-03 15:25:52 -0700706 @Override
707 public View getView(View convertView, ViewGroup parent) {
708 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700709 final State state = getDisplayState(DirectoryFragment.this);
710
Jeff Sharkey20b32272013-09-03 15:25:52 -0700711 if (convertView == null) {
712 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700713 if (state.derivedMode == MODE_LIST) {
714 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
715 } else if (state.derivedMode == MODE_GRID) {
716 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
717 } else {
718 throw new IllegalStateException();
719 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700720 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700721
Jeff Sharkey20b32272013-09-03 15:25:52 -0700722 return convertView;
723 }
724 }
725
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700726 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700727 private final int mIcon;
728 private final String mMessage;
729
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700730 public MessageFooter(int itemViewType, int icon, String message) {
731 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700732 mIcon = icon;
733 mMessage = message;
734 }
735
736 @Override
737 public View getView(View convertView, ViewGroup parent) {
738 final Context context = parent.getContext();
739 final State state = getDisplayState(DirectoryFragment.this);
740
741 if (convertView == null) {
742 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700743 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700744 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700745 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700746 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
747 } else {
748 throw new IllegalStateException();
749 }
750 }
751
752 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
753 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
754 icon.setImageResource(mIcon);
755 title.setText(mMessage);
756 return convertView;
757 }
758 }
759
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700760 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700761 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700762 private int mCursorCount;
763
764 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700765
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700766 public void swapResult(DirectoryResult result) {
767 mCursor = result != null ? result.cursor : null;
768 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700769
770 mFooters.clear();
771
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700772 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700773 if (extras != null) {
774 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
775 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700776 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700777 }
778 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
779 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700780 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700781 }
782 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
783 mFooters.add(new LoadingFooter());
784 }
785 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700786
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700787 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700788 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700789 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700790 }
791
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700792 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700793 mEmptyView.setVisibility(View.VISIBLE);
794 } else {
795 mEmptyView.setVisibility(View.GONE);
796 }
797
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700798 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700799 }
800
801 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700802 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700803 if (position < mCursorCount) {
804 return getDocumentView(position, convertView, parent);
805 } else {
806 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700807 convertView = mFooters.get(position).getView(convertView, parent);
808 // Only the view itself is disabled; contents inside shouldn't
809 // be dimmed.
810 convertView.setEnabled(false);
811 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700812 }
813 }
814
815 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700816 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700817 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700818
Jeff Sharkey9656a532013-09-13 13:42:19 -0700819 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
820
Jeff Sharkey873daa32013-08-18 17:38:20 -0700821 final RootsCache roots = DocumentsApplication.getRootsCache(context);
822 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
823 context, mThumbSize);
824
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700825 if (convertView == null) {
826 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700827 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700828 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700829 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700830 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
831 } else {
832 throw new IllegalStateException();
833 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700834 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700835
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700836 final Cursor cursor = getItem(position);
837
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700838 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
839 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700840 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
841 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
842 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
843 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
844 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
845 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
846 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
847 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700848
Jeff Sharkey9656a532013-09-13 13:42:19 -0700849 final View line1 = convertView.findViewById(R.id.line1);
850 final View line2 = convertView.findViewById(R.id.line2);
851
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700852 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
853 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700854 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
855 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700856 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700857 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
858 final TextView date = (TextView) convertView.findViewById(R.id.date);
859 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700860
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700861 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700862 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700863 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700864 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700865 }
866
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700867 iconMime.animate().cancel();
868 iconThumb.animate().cancel();
869
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700870 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700871 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700872 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700873 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700874
Jeff Sharkey7e544612014-08-29 15:38:27 -0700875 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
876 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
877
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700878 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700879 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700880 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700881 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700882 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700883 iconThumb.setImageBitmap(cachedResult);
884 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700885 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700886 iconThumb.setImageDrawable(null);
887 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700888 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700889 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700890 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700891 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700892 }
893
894 // Always throw MIME icon into place, even when a thumbnail is being
895 // loaded in background.
896 if (cacheHit) {
897 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700898 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700899 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700900 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700901 iconMime.setAlpha(1f);
902 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700903 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700904 iconMime.setImageDrawable(
905 getDocumentIcon(context, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700906 }
907
Jeff Sharkey9656a532013-09-13 13:42:19 -0700908 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700909 boolean hasLine2 = false;
910
Jeff Sharkey9656a532013-09-13 13:42:19 -0700911 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
912 if (!hideTitle) {
913 title.setText(docDisplayName);
914 hasLine1 = true;
915 }
916
917 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700918 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700919 // We've already had to enumerate roots before any results can
920 // be shown, so this will never block.
921 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700922 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700923 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700924 } else {
925 iconDrawable = root.loadIcon(context);
926 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700927
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700928 if (summary != null) {
929 final boolean alwaysShowSummary = getResources()
930 .getBoolean(R.bool.always_show_summary);
931 if (alwaysShowSummary) {
932 summary.setText(root.getDirectoryString());
933 summary.setVisibility(View.VISIBLE);
934 hasLine2 = true;
935 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700936 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700937 // No summary needed if icon speaks for itself
938 summary.setVisibility(View.INVISIBLE);
939 } else {
940 summary.setText(root.getDirectoryString());
941 summary.setVisibility(View.VISIBLE);
942 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
943 hasLine2 = true;
944 }
945 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700946 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700947 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700948 // Directories showing thumbnails in grid mode get a little icon
949 // hint to remind user they're a directory.
950 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
951 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700952 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700953 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700954 }
955
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700956 if (summary != null) {
957 if (docSummary != null) {
958 summary.setText(docSummary);
959 summary.setVisibility(View.VISIBLE);
960 hasLine2 = true;
961 } else {
962 summary.setVisibility(View.INVISIBLE);
963 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700964 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700965 }
966
Jeff Sharkey9656a532013-09-13 13:42:19 -0700967 if (icon1 != null) icon1.setVisibility(View.GONE);
968 if (icon2 != null) icon2.setVisibility(View.GONE);
969
970 if (iconDrawable != null) {
971 if (hasLine1) {
972 icon1.setVisibility(View.VISIBLE);
973 icon1.setImageDrawable(iconDrawable);
974 } else {
975 icon2.setVisibility(View.VISIBLE);
976 icon2.setImageDrawable(iconDrawable);
977 }
978 }
979
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700980 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700981 date.setText(null);
982 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700983 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700984 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700985 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700986
987 if (state.showSize) {
988 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700989 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700990 size.setText(null);
991 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700992 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700993 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700994 }
995 } else {
996 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700997 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700998
Jeff Sharkey9656a532013-09-13 13:42:19 -0700999 if (line1 != null) {
1000 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1001 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001002 if (line2 != null) {
1003 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1004 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001005
Jeff Sharkey7e544612014-08-29 15:38:27 -07001006 setEnabledRecursive(convertView, enabled);
1007
1008 iconMime.setAlpha(iconAlpha);
1009 iconThumb.setAlpha(iconAlpha);
1010 if (icon1 != null) icon1.setAlpha(iconAlpha);
1011 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001012
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001013 setupDragAndDropOnDocumentView(convertView, cursor);
1014
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001015 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001016 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001017
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001018 @Override
1019 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001020 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001021 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001022
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001023 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001024 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001025 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001026 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001027 return mCursor;
1028 } else {
1029 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001030 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001031 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001032
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001033 @Override
1034 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001035 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001036 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001037
1038 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001039 public int getViewTypeCount() {
1040 return 4;
1041 }
1042
1043 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001044 public int getItemViewType(int position) {
1045 if (position < mCursorCount) {
1046 return 0;
1047 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001048 position -= mCursorCount;
1049 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001050 }
1051 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001052 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001053
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001054 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1055 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001056 private final Uri mUri;
1057 private final ImageView mIconMime;
1058 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001059 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001060 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001061 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001062
Jeff Sharkey7e544612014-08-29 15:38:27 -07001063 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1064 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001065 mUri = uri;
1066 mIconMime = iconMime;
1067 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001068 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001069 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001071 }
1072
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001073 @Override
1074 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001075 cancel(false);
1076 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001077 }
1078
1079 @Override
1080 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001081 if (isCancelled()) return null;
1082
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001083 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001084 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001085
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001086 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001087 Bitmap result = null;
1088 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001089 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1090 resolver, mUri.getAuthority());
1091 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001092 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001093 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1094 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001095 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001096 }
1097 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001098 if (!(e instanceof OperationCanceledException)) {
1099 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1100 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001101 } finally {
1102 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001103 }
1104 return result;
1105 }
1106
1107 @Override
1108 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001109 if (mIconThumb.getTag() == this && result != null) {
1110 mIconThumb.setTag(null);
1111 mIconThumb.setImageBitmap(result);
1112
Jeff Sharkey7e544612014-08-29 15:38:27 -07001113 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001114 mIconMime.animate().alpha(0f).start();
1115 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001116 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001117 }
1118 }
1119 }
1120
1121 private static String formatTime(Context context, long when) {
1122 // TODO: DateUtils should make this easier
1123 Time then = new Time();
1124 then.set(when);
1125 Time now = new Time();
1126 now.setToNow();
1127
1128 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1129 | DateUtils.FORMAT_ABBREV_ALL;
1130
1131 if (then.year != now.year) {
1132 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1133 } else if (then.yearDay != now.yearDay) {
1134 flags |= DateUtils.FORMAT_SHOW_DATE;
1135 } else {
1136 flags |= DateUtils.FORMAT_SHOW_TIME;
1137 }
1138
1139 return DateUtils.formatDateTime(context, when, flags);
1140 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001141
1142 private String findCommonMimeType(List<String> mimeTypes) {
1143 String[] commonType = mimeTypes.get(0).split("/");
1144 if (commonType.length != 2) {
1145 return "*/*";
1146 }
1147
1148 for (int i = 1; i < mimeTypes.size(); i++) {
1149 String[] type = mimeTypes.get(i).split("/");
1150 if (type.length != 2) continue;
1151
1152 if (!commonType[1].equals(type[1])) {
1153 commonType[1] = "*";
1154 }
1155
1156 if (!commonType[0].equals(type[0])) {
1157 commonType[0] = "*";
1158 commonType[1] = "*";
1159 break;
1160 }
1161 }
1162
1163 return commonType[0] + "/" + commonType[1];
1164 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001165
1166 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001167 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001168 if (v.isEnabled() == enabled) return;
1169 v.setEnabled(enabled);
1170
1171 if (v instanceof ViewGroup) {
1172 final ViewGroup vg = (ViewGroup) v;
1173 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1174 setEnabledRecursive(vg.getChildAt(i), enabled);
1175 }
1176 }
1177 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001178
1179 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1180 final State state = getDisplayState(DirectoryFragment.this);
1181
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001182 // Directories are always enabled
1183 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1184 return true;
1185 }
1186
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001187 // Read-only files are disabled when creating
1188 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1189 return false;
1190 }
1191
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001192 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1193 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001194
1195 public List<DocumentInfo> getSelectedDocuments() {
1196 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
1197 final List<DocumentInfo> docs = Lists.newArrayList();
1198 final int size = checked.size();
1199 for (int i = 0; i < size; i++) {
1200 if (checked.valueAt(i)) {
1201 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
1202 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1203 docs.add(doc);
1204 }
1205 }
1206 return docs;
1207 }
1208
Steve McKay0599a442015-05-05 14:50:00 -07001209 private void copyFromClipData(ClipData clipData) {
1210 copyFromClipData(
1211 clipData,
1212 ((BaseActivity)getActivity()).getCurrentDirectory());
1213 }
1214
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001215 private void copyFromClipData(ClipData clipData, DocumentInfo dstDir) {
1216 final List<DocumentInfo> srcDocs = getDocumentsFromClipData(clipData);
1217
Steve McKay0599a442015-05-05 14:50:00 -07001218 if (!canCopy(srcDocs, dstDir)) {
1219 Toast.makeText(getActivity(), R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
1220 return;
1221 }
1222
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001223 if (srcDocs.isEmpty())
1224 return;
1225
1226 final DocumentStack curStack = getDisplayState(this).stack;
1227 DocumentStack tmpStack = new DocumentStack();
1228 if (dstDir != null) {
1229 tmpStack.push(dstDir);
1230 tmpStack.addAll(curStack);
1231 } else {
1232 tmpStack = curStack;
1233 }
1234
1235 CopyService.start(getActivity(), srcDocs, tmpStack);
1236 }
1237
1238 private List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
1239 final List<DocumentInfo> srcDocs = Lists.newArrayList();
1240
1241 Context context = getActivity();
1242 final ContentResolver resolver = context.getContentResolver();
1243
1244 int itemCount = clipData.getItemCount();
1245 for (int i = 0; i < itemCount; ++i) {
1246 ClipData.Item item = clipData.getItemAt(i);
1247 Uri itemUri = item.getUri();
1248 if (itemUri != null && DocumentsContract.isDocumentUri(context, itemUri)) {
1249 try {
1250 Cursor cursor = resolver.query(itemUri, null, null, null, null);
1251 cursor.moveToPosition(0);
1252 srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority()));
1253 } catch (Exception e) {
1254 Log.e(TAG, e.getMessage());
1255 }
1256 }
1257 }
1258
1259 return srcDocs;
1260 }
1261
1262 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1263 Context context = getActivity();
1264 final ContentResolver resolver = context.getContentResolver();
1265 ClipData clipData = null;
1266 for (DocumentInfo doc : docs) {
1267 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1268 if (clipData == null) {
1269 clipData = ClipData.newUri(resolver, "", uri);
1270 } else {
1271 // TODO: update list of mime types in ClipData.
1272 clipData.addItem(new ClipData.Item(uri));
1273 }
1274 }
1275 return clipData;
1276 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001277
Steve McKay0599a442015-05-05 14:50:00 -07001278 void copyToClipboard() {
1279 ClipboardManager clipboard = getClipboardManager();
1280 List<DocumentInfo> docs = getSelectedDocuments();
1281 ClipData data = getClipDataFromDocuments(docs);
1282 clipboard.setPrimaryClip(data);
1283
1284 Activity activity = getActivity();
1285 Toast.makeText(activity,
1286 activity.getResources().getQuantityString(
1287 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1288 Toast.LENGTH_SHORT).show();
1289 }
1290
1291 void pasteFromClipboard() {
1292 ClipboardManager clipboard = getClipboardManager();
1293 copyFromClipData(clipboard.getPrimaryClip());
1294 }
1295
1296 private ClipboardManager getClipboardManager() {
1297 return (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
1298 }
1299
1300 /**
1301 * Returns true if the list of files can be copied to destination. Note that this
1302 * is a policy check only. Currently the method does not attempt to verify
1303 * available space or any other environmental aspects possibly resulting in
1304 * failure to copy.
1305 *
1306 * @return true if the list of files can be copied to destination.
1307 */
1308 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1309 BaseActivity activity = (BaseActivity)getActivity();
1310
1311 final RootInfo root = activity.getCurrentRoot();
1312
1313 // Can't copy folders to Downloads.
1314 if (root.isDownloads()) {
1315 for (DocumentInfo docs : files) {
1316 if (docs.isDirectory()) {
1317 return false;
1318 }
1319 }
1320 }
1321
1322 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1323 }
1324
1325 void selectAllFiles() {
1326 int count = mCurrentView.getCount();
1327 for (int i = 0; i < count; i++) {
1328 mCurrentView.setItemChecked(i, true);
1329 }
1330 updateDisplayState();
1331 }
1332
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001333 private void setupDragAndDropOnDirectoryView(AbsListView view) {
1334 // Listen for drops on non-directory items and empty space.
1335 view.setOnDragListener(mOnDragListener);
1336 }
1337
1338 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1339 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1340 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1341 // Make a directory item a drop target. Drop on non-directories and empty space
1342 // is handled at the list/grid view level.
1343 view.setOnDragListener(mOnDragListener);
1344 }
1345
1346 // Temporary: attaching the listener to the title only.
1347 // Attaching to the entire item conflicts with the item long click handler responsible
1348 // for item selection.
1349 final View title = view.findViewById(android.R.id.title);
1350 title.setOnLongClickListener(mLongClickListener);
1351 }
1352
1353 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1354 @Override
1355 public boolean onDrag(View v, DragEvent event) {
1356 switch (event.getAction()) {
1357 case DragEvent.ACTION_DRAG_STARTED:
1358 // TODO: Check if the event contains droppable data.
1359 return true;
1360
1361 // TODO: Highlight potential drop target directory?
1362 // TODO: Expand drop target directory on hover?
1363 case DragEvent.ACTION_DRAG_ENTERED:
1364 case DragEvent.ACTION_DRAG_LOCATION:
1365 case DragEvent.ACTION_DRAG_EXITED:
1366 case DragEvent.ACTION_DRAG_ENDED:
1367 return true;
1368
1369 case DragEvent.ACTION_DROP:
1370 int dstPosition = mCurrentView.getPositionForView(v);
1371 DocumentInfo dstDir = null;
1372 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1373 Cursor dstCursor = mAdapter.getItem(dstPosition);
1374 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1375 // TODO: Do not drop into the directory where the documents came from.
1376 }
1377 copyFromClipData(event.getClipData(), dstDir);
1378 return true;
1379 }
1380 return false;
1381 }
1382 };
1383
1384 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1385 @Override
1386 public boolean onLongClick(View v) {
1387 final List<DocumentInfo> docs = getDraggableDocuments(v);
1388 if (docs.isEmpty()) {
1389 return false;
1390 }
1391 v.startDrag(
1392 getClipDataFromDocuments(docs),
1393 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1394 null,
1395 View.DRAG_FLAG_GLOBAL
1396 );
1397 return true;
1398 }
1399 };
1400
1401 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
1402 final int position = mCurrentView.getPositionForView(currentItemView);
1403 if (position == android.widget.AdapterView.INVALID_POSITION) {
1404 return Collections.EMPTY_LIST;
1405 }
1406
1407 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1408 if (!selectedDocs.isEmpty()) {
1409 if (!mCurrentView.isItemChecked(position)) {
1410 // There is a selection that does not include the current item, drag nothing.
1411 return Collections.EMPTY_LIST;
1412 }
1413 return selectedDocs;
1414 }
1415
1416 final Cursor cursor = mAdapter.getItem(position);
1417 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1418 return Lists.newArrayList(doc);
1419 }
1420
1421 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1422 if (docs.size() == 1) {
1423 final DocumentInfo doc = docs.get(0);
1424 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1425 doc.mimeType, doc.icon, getDisplayState(this));
1426 }
1427 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1428 }
1429
1430 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1431 String docMimeType, int docIcon, State state) {
1432 if (docIcon != 0) {
1433 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1434 } else {
1435 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1436 state.derivedMode);
1437 }
1438 }
1439
1440 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1441
1442 private final Drawable mShadow;
1443
1444 private final int mShadowDimension;
1445
1446 public DrawableShadowBuilder(Drawable shadow) {
1447 mShadow = shadow;
1448 mShadowDimension = getResources().getDimensionPixelSize(
1449 R.dimen.drag_shadow_size);
1450 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1451 }
1452
1453 public void onProvideShadowMetrics(
1454 Point shadowSize, Point shadowTouchPoint) {
1455 shadowSize.set(mShadowDimension, mShadowDimension);
1456 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1457 }
1458
1459 public void onDrawShadow(Canvas canvas) {
1460 mShadow.draw(canvas);
1461 }
1462 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001463}