blob: b7a3e4bcd9ab1161d532c8a35a5e38468b29c214 [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;
Steve McKayfefcd702015-08-20 16:19:38 +000027import static com.android.documentsui.Shared.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 McKayef280152015-06-11 10:10:49 -070031import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKayd57f5fa2015-07-23 16:33:41 -070032import static com.android.internal.util.Preconditions.checkState;
Steve McKay0599a442015-05-05 14:50:00 -070033
Ben Kwaf5858932015-04-07 15:43:39 -070034import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070035import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070036import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.FragmentManager;
38import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070040import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070041import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070043import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070045import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070047import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070050import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.graphics.drawable.Drawable;
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;
Makoto Onuki77778752015-07-01 14:55:14 -070057import android.os.Handler;
58import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070059import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070060import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070061import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070063import android.provider.DocumentsContract.Document;
Ben Kwa24be5d32015-08-27 16:04:46 -070064import android.support.annotation.Nullable;
Ben Kwa91923182015-08-27 16:06:33 -070065import android.support.design.widget.Snackbar;
Steve McKayef280152015-06-11 10:10:49 -070066import android.support.v7.widget.GridLayoutManager;
67import android.support.v7.widget.LinearLayoutManager;
68import android.support.v7.widget.RecyclerView;
69import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070070import android.support.v7.widget.RecyclerView.RecyclerListener;
71import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070072import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070073import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070074import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070075import android.text.format.Time;
76import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070077import android.util.SparseArray;
Ben Kwa91923182015-08-27 16:06:33 -070078import android.util.SparseBooleanArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070079import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070080import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070081import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070082import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070083import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070084import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070085import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070086import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070087import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070088import android.view.ViewGroup;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070089import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070090import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070091import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070092
Steve McKay351a7492015-08-04 10:11:01 -070093import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070094import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070095import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070096import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070097import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070098import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090099import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700100import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -0700101import com.android.internal.util.Preconditions;
Steve McKayfefcd702015-08-20 16:19:38 +0000102import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700103
104import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700105import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700106import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700107
108/**
109 * Display the documents inside a single directory.
110 */
111public class DirectoryFragment extends Fragment {
112
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700113 public static final int TYPE_NORMAL = 1;
114 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700115 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700116
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700117 public static final int ANIM_NONE = 1;
118 public static final int ANIM_SIDE = 2;
119 public static final int ANIM_DOWN = 3;
120 public static final int ANIM_UP = 4;
121
Ben Kwaf5858932015-04-07 15:43:39 -0700122 public static final int REQUEST_COPY_DESTINATION = 1;
123
Steve McKayef280152015-06-11 10:10:49 -0700124 private static final int LOADER_ID = 42;
125 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700126 private static final boolean DEBUG_ENABLE_DND = false;
127
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700128 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700129 private static final String EXTRA_ROOT = "root";
130 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700131 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700132 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133
Ben Kwa24be5d32015-08-27 16:04:46 -0700134 private final Model mModel = new Model();
135
Steve McKayef280152015-06-11 10:10:49 -0700136 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700137
Steve McKayef280152015-06-11 10:10:49 -0700138 private View mEmptyView;
139 private RecyclerView mRecView;
140
141 private int mType = TYPE_NORMAL;
142 private String mStateKey;
143
144 private int mLastMode = MODE_UNKNOWN;
145 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
146 private boolean mLastShowSize;
147 private boolean mHideGridTitles;
148 private boolean mSvelteRecents;
149 private Point mThumbSize;
150 private DocumentsAdapter mAdapter;
151 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700152 private FragmentTuner mFragmentTuner;
153 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700154 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700155 private LinearLayoutManager mListLayout;
156 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700157 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700158
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700159 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
160 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700161 }
162
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700163 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
164 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700165 }
166
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700167 public static void showRecentsOpen(FragmentManager fm, int anim) {
168 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700169 }
170
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700171 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
172 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700173 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700174 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700175 args.putParcelable(EXTRA_ROOT, root);
176 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700177 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700178
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700179 final FragmentTransaction ft = fm.beginTransaction();
180 switch (anim) {
181 case ANIM_SIDE:
182 args.putBoolean(EXTRA_IGNORE_STATE, true);
183 break;
184 case ANIM_DOWN:
185 args.putBoolean(EXTRA_IGNORE_STATE, true);
186 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
187 break;
188 case ANIM_UP:
189 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
190 break;
191 }
192
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700193 final DirectoryFragment fragment = new DirectoryFragment();
194 fragment.setArguments(args);
195
Jeff Sharkey76112212013-08-06 11:26:10 -0700196 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700197 ft.commitAllowingStateLoss();
198 }
199
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700200 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
201 final StringBuilder builder = new StringBuilder();
202 builder.append(root != null ? root.authority : "null").append(';');
203 builder.append(root != null ? root.rootId : "null").append(';');
204 builder.append(doc != null ? doc.documentId : "null");
205 return builder.toString();
206 }
207
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700208 public static DirectoryFragment get(FragmentManager fm) {
209 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700210 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700211 }
212
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700213 @Override
214 public View onCreateView(
215 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
216 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700217 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700218 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
219
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700220 mEmptyView = view.findViewById(android.R.id.empty);
221
Steve McKayef280152015-06-11 10:10:49 -0700222 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
223 mRecView.setRecyclerListener(
224 new RecyclerListener() {
225 @Override
226 public void onViewRecycled(ViewHolder holder) {
227 cancelThumbnailTask(holder.itemView);
228 }
229 });
Steve McKay8e258c62015-05-06 14:27:57 -0700230
Steve McKayd57f5fa2015-07-23 16:33:41 -0700231 // TODO: Rather than update columns on layout changes, push this
232 // code (or something like it) into GridLayoutManager.
233 mRecView.addOnLayoutChangeListener(
234 new OnLayoutChangeListener() {
235
236 @Override
237 public void onLayoutChange(
238 View v, int left, int top, int right, int bottom, int oldLeft,
239 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000240 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700241 if (mGridLayout != null) {
242 mGridLayout.setSpanCount(mColumnCount);
243 }
244 }
245 });
246
247 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700248 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700249 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700250 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700251
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252 return view;
253 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700254
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700256 public void onDestroyView() {
257 super.onDestroyView();
258
259 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700260 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700261 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700262 final View view = mRecView.getChildAt(i);
263 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700265
Steve McKayef280152015-06-11 10:10:49 -0700266 // Clear any outstanding selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700267 mModel.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700268 }
269
270 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700271 public void onActivityCreated(Bundle savedInstanceState) {
272 super.onActivityCreated(savedInstanceState);
273
274 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700275 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700276
Jeff Sharkey9656a532013-09-13 13:42:19 -0700277 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
278 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
279
Steve McKayef280152015-06-11 10:10:49 -0700280 mAdapter = new DocumentsAdapter(context);
281 mRecView.setAdapter(mAdapter);
282
283 GestureDetector.SimpleOnGestureListener listener =
284 new GestureDetector.SimpleOnGestureListener() {
285 @Override
286 public boolean onSingleTapUp(MotionEvent e) {
287 return DirectoryFragment.this.onSingleTapUp(e);
288 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700289 @Override
290 public boolean onDoubleTap(MotionEvent e) {
291 Log.d(TAG, "Handling double tap.");
292 return DirectoryFragment.this.onDoubleTap(e);
293 }
Steve McKayef280152015-06-11 10:10:49 -0700294 };
295
Ben Kwa24be5d32015-08-27 16:04:46 -0700296 // TODO: instead of inserting the view into the constructor, extract listener-creation code
297 // and set the listener on the view after the fact. Then the view doesn't need to be passed
298 // into the selection manager which is passed into the model.
299 MultiSelectManager selMgr= new MultiSelectManager(
Steve McKay57394872015-08-12 14:48:34 -0700300 mRecView,
301 listener,
302 state.allowMultiple
303 ? MultiSelectManager.MODE_MULTIPLE
304 : MultiSelectManager.MODE_SINGLE);
Ben Kwa24be5d32015-08-27 16:04:46 -0700305 selMgr.addCallback(new SelectionModeListener());
306 mModel.setSelectionManager(selMgr);
Steve McKayef280152015-06-11 10:10:49 -0700307
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700308 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700309 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700310
Steve McKay1f199482015-05-20 15:58:42 -0700311 mFragmentTuner = pickFragmentTuner(state);
312 mClipper = new DocumentClipper(context);
313
Jeff Sharkey9656a532013-09-13 13:42:19 -0700314 if (mType == TYPE_RECENT_OPEN) {
315 // Hide titles when showing recents for picking images/videos
316 mHideGridTitles = MimePredicate.mimeMatches(
317 MimePredicate.VISUAL_MIMES, state.acceptMimes);
318 } else {
319 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
320 }
321
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700322 final ActivityManager am = (ActivityManager) context.getSystemService(
323 Context.ACTIVITY_SERVICE);
324 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
325
Jeff Sharkey46899c82013-08-18 22:26:48 -0700326 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700327 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700328 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700329 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700330
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700331 Uri contentsUri;
332 switch (mType) {
333 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700334 contentsUri = DocumentsContract.buildChildDocumentsUri(
335 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700336 if (state.action == ACTION_MANAGE) {
337 contentsUri = DocumentsContract.setManageMode(contentsUri);
338 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700339 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700340 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700341 case TYPE_SEARCH:
342 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700343 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700344 if (state.action == ACTION_MANAGE) {
345 contentsUri = DocumentsContract.setManageMode(contentsUri);
346 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700347 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700348 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700349 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700350 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700351 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700352 default:
353 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700354 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700355 }
356
357 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700358 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700359 if (result == null || result.exception != null) {
360 // onBackPressed does a fragment transaction, which can't be done inside
361 // onLoadFinished
362 mHandler.post(new Runnable() {
363 @Override
364 public void run() {
365 final Activity activity = getActivity();
366 if (activity != null) {
367 activity.onBackPressed();
368 }
369 }
370 });
371 return;
372 }
373
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700374 if (!isAdded()) return;
375
Ben Kwa24be5d32015-08-27 16:04:46 -0700376 // TODO: make the adapter listen to the model
377 mModel.update(result);
378 mAdapter.update();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700379
380 // Push latest state up to UI
381 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700382 if (result.mode != MODE_UNKNOWN) {
383 state.derivedMode = result.mode;
384 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700385 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700386 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700387
388 updateDisplayState();
389
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700390 // When launched into empty recents, show drawer
Ben Kwa24be5d32015-08-27 16:04:46 -0700391 if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
Steve McKayb68dd222015-04-20 17:18:15 -0700392 context instanceof DocumentsActivity) {
393 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700394 }
395
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700396 // Restore any previous instance state
397 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
398 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
399 getView().restoreHierarchyState(container);
400 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700401 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700402 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700403
404 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700405 }
406
407 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700408 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700409 // TODO: make the adapter listen to the model.
410 mModel.update(null);
411 mAdapter.update();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700412 }
413 };
414
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700415 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700416 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700417
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700418 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700419 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700420 }
421
Jeff Sharkey42d26792013-09-06 13:22:09 -0700422 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700423 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700424 // There's only one request code right now. Replace this with a switch statement or
425 // something more scalable when more codes are added.
426 if (requestCode != REQUEST_COPY_DESTINATION) {
427 return;
428 }
429 if (resultCode == Activity.RESULT_CANCELED || data == null) {
430 // User pressed the back button or otherwise cancelled the destination pick. Don't
431 // proceed with the copy.
432 return;
433 }
434
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900435 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700436 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
437 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700438 }
439
Steve McKayef280152015-06-11 10:10:49 -0700440 private int getEventAdapterPosition(MotionEvent e) {
441 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
442 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
443 }
444
445 private boolean onSingleTapUp(MotionEvent e) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700446 if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700447 int position = getEventAdapterPosition(e);
448 if (position != RecyclerView.NO_POSITION) {
449 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700450 }
451 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700452 return false;
453 }
Steve McKayef280152015-06-11 10:10:49 -0700454
Steve McKay93d8ef42015-07-30 12:27:44 -0700455 protected boolean onDoubleTap(MotionEvent e) {
456 if (Events.isMouseEvent(e)) {
457 Log.d(TAG, "Handling double tap from mouse.");
458 int position = getEventAdapterPosition(e);
459 if (position != RecyclerView.NO_POSITION) {
460 return handleViewItem(position);
461 }
462 }
463 return false;
464 }
465
466 private boolean handleViewItem(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700467 final Cursor cursor = mModel.getItem(position);
Steve McKay93d8ef42015-07-30 12:27:44 -0700468 checkNotNull(cursor, "Cursor cannot be null.");
469 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);
Ben Kwa24be5d32015-08-27 16:04:46 -0700473 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
474 mModel.clearSelection();
Steve McKay93d8ef42015-07-30 12:27:44 -0700475 return true;
476 }
Steve McKayef280152015-06-11 10:10:49 -0700477 return false;
478 }
479
Ben Kwaf5858932015-04-07 15:43:39 -0700480 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700481 public void onStop() {
482 super.onStop();
483
484 // Remember last scroll location
485 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
486 getView().saveHierarchyState(container);
487 final State state = getDisplayState(this);
488 state.dirState.put(mStateKey, container);
489 }
490
491 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700492 public void onResume() {
493 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700494 updateDisplayState();
495 }
496
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700497 public void onDisplayStateChanged() {
498 updateDisplayState();
499 }
500
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700501 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700502 // Sort order change always triggers reload; we'll trigger state change
503 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700504 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700505 }
506
507 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700508 final ContentResolver resolver = getActivity().getContentResolver();
509 final State state = getDisplayState(this);
510
511 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
512 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
513
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700514 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700515 final Uri stateUri = RecentsProvider.buildState(
516 root.authority, root.rootId, doc.documentId);
517 final ContentValues values = new ContentValues();
518 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700519
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700520 new AsyncTask<Void, Void, Void>() {
521 @Override
522 protected Void doInBackground(Void... params) {
523 resolver.insert(stateUri, values);
524 return null;
525 }
526 }.execute();
527 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700528
529 // Mode change is just visual change; no need to kick loader, and
530 // deliver change event immediately.
531 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700532 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700533
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700534 updateDisplayState();
535 }
536
537 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700538 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700539
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700540 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700541 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700542 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700543
Steve McKayef280152015-06-11 10:10:49 -0700544 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700545
Steve McKayef280152015-06-11 10:10:49 -0700546 mRecView.setAdapter(mAdapter);
547 }
548
549 /**
550 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
551 * classes as needed.
552 */
553 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700554 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700555
556 final LayoutManager layout;
557 switch (mode) {
558 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700559 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700560 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700561 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700562 }
Steve McKayef280152015-06-11 10:10:49 -0700563 layout = mGridLayout;
564 break;
565 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700566 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700567 if (mListLayout == null) {
568 mListLayout = new LinearLayoutManager(getContext());
569 }
Steve McKayef280152015-06-11 10:10:49 -0700570 layout = mListLayout;
571 break;
572 case MODE_UNKNOWN:
573 default:
574 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700575 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700576
Steve McKayef280152015-06-11 10:10:49 -0700577 mRecView.setLayoutManager(layout);
Kyle Horimoto2da6e4a2015-08-27 16:44:00 -0700578 // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
579 // imperatively calling this function.
Steve McKay9058e042015-09-01 12:31:24 -0700580 mModel.mSelectionManager.handleLayoutChanged();
Steve McKayef280152015-06-11 10:10:49 -0700581 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700582 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700583 }
584
Steve McKayfefcd702015-08-20 16:19:38 +0000585 private int calculateColumnCount() {
586 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
587 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700588 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000589
Steve McKayd57f5fa2015-07-23 16:33:41 -0700590 checkState(mRecView.getWidth() > 0);
591 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000592 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
593
Steve McKayd57f5fa2015-07-23 16:33:41 -0700594 return columnCount;
595 }
596
Steve McKayef280152015-06-11 10:10:49 -0700597 /**
598 * Manages the integration between our ActionMode and MultiSelectManager, initiating
599 * ActionMode when there is a selection, canceling it when there is no selection,
600 * and clearing selection when action mode is explicitly exited by the user.
601 */
602 private final class SelectionModeListener
603 implements MultiSelectManager.Callback, ActionMode.Callback {
604
605 private Selection mSelected = new Selection();
606 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700607 private int mNoDeleteCount = 0;
608 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700609
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700610 @Override
Steve McKayef280152015-06-11 10:10:49 -0700611 public boolean onBeforeItemStateChange(int position, boolean selected) {
612 // Directories and footer items cannot be checked
613 if (selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700614 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700615 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700616 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
617 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700618 return isDocumentEnabled(docMimeType, docFlags);
619 }
620 return true;
621 }
622
623 @Override
624 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700625
Ben Kwa24be5d32015-08-27 16:04:46 -0700626 final Cursor cursor = mModel.getItem(position);
Steve McKay4f4232d2015-07-22 12:13:46 -0700627 checkNotNull(cursor, "Cursor cannot be null.");
628
629 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
630 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
631 mNoDeleteCount += selected ? 1 : -1;
632 }
Steve McKay57394872015-08-12 14:48:34 -0700633 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700634
Steve McKay57394872015-08-12 14:48:34 -0700635 @Override
636 public void onSelectionChanged() {
Ben Kwa24be5d32015-08-27 16:04:46 -0700637 mModel.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700638 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700639 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
640 if (mActionMode == null) {
641 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
642 mActionMode = getActivity().startActionMode(this);
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900643 getActivity().getWindow().setStatusBarColor(
644 getResources().getColor(R.color.action_mode_status_bar_background));
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700645 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700646 updateActionMenu();
647 } else {
648 if (DEBUG) Log.d(TAG, "Finishing action mode.");
649 if (mActionMode != null) {
650 mActionMode.finish();
651 }
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900652 getActivity().getWindow().setStatusBarColor(
653 getResources().getColor(R.color.status_bar_background));
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700654 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700655
Steve McKayef280152015-06-11 10:10:49 -0700656 if (mActionMode != null) {
657 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
658 }
659 }
660
661 // Called when the user exits the action mode
662 @Override
663 public void onDestroyActionMode(ActionMode mode) {
664 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
665 mActionMode = null;
666 // clear selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700667 mModel.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700668 mSelected.clear();
669 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700670 }
671
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700672 @Override
673 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
674 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa24be5d32015-08-27 16:04:46 -0700675 mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
676 return mModel.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700677 }
678
679 @Override
680 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700681 mMenu = menu;
682 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700683 return true;
684 }
685
Steve McKay4f4232d2015-07-22 12:13:46 -0700686 private void updateActionMenu() {
687 checkNotNull(mMenu);
688 // Delegate update logic to our owning action, since specialized logic is desired.
689 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
690 }
691
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700692 @Override
Steve McKayef280152015-06-11 10:10:49 -0700693 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700694
Ben Kwa24be5d32015-08-27 16:04:46 -0700695 Selection selection = mModel.getSelection(new Selection());
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700696
Jeff Sharkey873daa32013-08-18 17:38:20 -0700697 final int id = item.getItemId();
698 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700699 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700700 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700701 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700702
703 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700704 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700705 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700706 return true;
707
708 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700709 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700710 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700711 return true;
712
Steve McKay1f199482015-05-20 15:58:42 -0700713 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700714 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700715 mode.finish();
716 return true;
717
Steve McKay1f199482015-05-20 15:58:42 -0700718 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700719 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700720 mode.finish();
721 return true;
722
Steve McKay1f199482015-05-20 15:58:42 -0700723 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700724 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700725 mode.finish();
726 return true;
727
Ben Kwa512a6ba2015-03-31 08:15:21 -0700728 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700729 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700730 return true;
731
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700732 } else {
733 return false;
734 }
735 }
Steve McKayef280152015-06-11 10:10:49 -0700736 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700737
Steve McKayef280152015-06-11 10:10:49 -0700738 private static void cancelThumbnailTask(View view) {
739 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
740 if (iconThumb != null) {
741 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
742 if (oldTask != null) {
743 oldTask.preempt();
744 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700745 }
746 }
Steve McKayef280152015-06-11 10:10:49 -0700747 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700748
Steve McKayef280152015-06-11 10:10:49 -0700749 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700750 new GetDocumentsTask() {
751 @Override
752 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700753 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700754 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700755 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700756 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700757 }
758
Steve McKayef280152015-06-11 10:10:49 -0700759 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700760 new GetDocumentsTask() {
761 @Override
762 void onDocumentsReady(List<DocumentInfo> docs) {
763 Intent intent;
764
765 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000766 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700767 for (DocumentInfo doc: docs) {
768 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
769 docsForSend.add(doc);
770 }
771 }
772
773 if (docsForSend.size() == 1) {
774 final DocumentInfo doc = docsForSend.get(0);
775
776 intent = new Intent(Intent.ACTION_SEND);
777 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
778 intent.addCategory(Intent.CATEGORY_DEFAULT);
779 intent.setType(doc.mimeType);
780 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
781
782 } else if (docsForSend.size() > 1) {
783 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
784 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
785 intent.addCategory(Intent.CATEGORY_DEFAULT);
786
Steve McKayfefcd702015-08-20 16:19:38 +0000787 final ArrayList<String> mimeTypes = new ArrayList<>();
788 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700789 for (DocumentInfo doc : docsForSend) {
790 mimeTypes.add(doc.mimeType);
791 uris.add(doc.derivedUri);
792 }
793
794 intent.setType(findCommonMimeType(mimeTypes));
795 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
796
797 } else {
798 return;
799 }
800
801 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
802 startActivity(intent);
803 }
804 }.execute(selected);
805 }
806
Steve McKayef280152015-06-11 10:10:49 -0700807 private void deleteDocuments(final Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -0700808 Context context = getActivity();
809 ContentResolver resolver = context.getContentResolver();
810 String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700811
Ben Kwa91923182015-08-27 16:06:33 -0700812 mModel.markForDeletion(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700813
Ben Kwa91923182015-08-27 16:06:33 -0700814 Activity activity = getActivity();
815 Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG)
816 .setAction(
817 R.string.undo,
818 new android.view.View.OnClickListener() {
819 @Override
820 public void onClick(View view) {}
821 })
822 .setCallback(
823 new Snackbar.Callback() {
824 @Override
825 public void onDismissed(Snackbar snackbar, int event) {
826 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
827 mModel.undoDeletion();
828 } else {
829 mModel.finalizeDeletion();
830 }
831 ;
832 }
833 })
834 .show();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700835 }
836
Steve McKayef280152015-06-11 10:10:49 -0700837 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700838 // Pop up a dialog to pick a destination. This is inadequate but works for now.
839 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900840 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900841 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900842 Uri.EMPTY,
843 getActivity(),
844 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700845
846 new GetDocumentsTask() {
847 @Override
848 void onDocumentsReady(List<DocumentInfo> docs) {
849 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
850
851 boolean directoryCopy = false;
852 for (DocumentInfo info : docs) {
853 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
854 directoryCopy = true;
855 break;
856 }
857 }
858 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
859 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
860 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900861 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700862 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700863 }
864
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700865 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700866 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700867 }
868
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700869 private static abstract class Footer {
870 private final int mItemViewType;
871
872 public Footer(int itemViewType) {
873 mItemViewType = itemViewType;
874 }
875
876 public abstract View getView(View convertView, ViewGroup parent);
877
878 public int getItemViewType() {
879 return mItemViewType;
880 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700881 }
882
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700883 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700884 public LoadingFooter() {
885 super(1);
886 }
887
Jeff Sharkey20b32272013-09-03 15:25:52 -0700888 @Override
889 public View getView(View convertView, ViewGroup parent) {
890 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700891 final State state = getDisplayState(DirectoryFragment.this);
892
Jeff Sharkey20b32272013-09-03 15:25:52 -0700893 if (convertView == null) {
894 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700895 if (state.derivedMode == MODE_LIST) {
896 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
897 } else if (state.derivedMode == MODE_GRID) {
898 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
899 } else {
900 throw new IllegalStateException();
901 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700902 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700903
Jeff Sharkey20b32272013-09-03 15:25:52 -0700904 return convertView;
905 }
906 }
907
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700908 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700909 private final int mIcon;
910 private final String mMessage;
911
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700912 public MessageFooter(int itemViewType, int icon, String message) {
913 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700914 mIcon = icon;
915 mMessage = message;
916 }
917
918 @Override
919 public View getView(View convertView, ViewGroup parent) {
920 final Context context = parent.getContext();
921 final State state = getDisplayState(DirectoryFragment.this);
922
923 if (convertView == null) {
924 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700925 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700926 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700927 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700928 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
929 } else {
930 throw new IllegalStateException();
931 }
932 }
933
934 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
935 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
936 icon.setImageResource(mIcon);
937 title.setText(mMessage);
938 return convertView;
939 }
940 }
941
Steve McKayef280152015-06-11 10:10:49 -0700942 // Provide a reference to the views for each data item
943 // Complex data items may need more than one view per item, and
944 // you provide access to all the views for a data item in a view holder
945 private static final class DocumentHolder extends RecyclerView.ViewHolder {
946 // each data item is just a string in this case
947 public View view;
948 public String docId; // The stable document id.
949 public DocumentHolder(View view) {
950 super(view);
951 this.view = view;
952 }
953 }
954
Ben Kwa24be5d32015-08-27 16:04:46 -0700955 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
Steve McKayef280152015-06-11 10:10:49 -0700956
957 private final Context mContext;
958 private final LayoutInflater mInflater;
959 // TODO: Bring back support for footers.
Steve McKayfefcd702015-08-20 16:19:38 +0000960 private final List<Footer> mFooters = new ArrayList<>();
Steve McKayef280152015-06-11 10:10:49 -0700961
Steve McKayef280152015-06-11 10:10:49 -0700962 public DocumentsAdapter(Context context) {
963 mContext = context;
964 mInflater = LayoutInflater.from(context);
965 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700966
Ben Kwa24be5d32015-08-27 16:04:46 -0700967 public void update() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700968 mFooters.clear();
Ben Kwa24be5d32015-08-27 16:04:46 -0700969 if (mModel.info != null) {
970 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, mModel.info));
971 }
972 if (mModel.error != null) {
973 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, mModel.error));
974 }
975 if (mModel.isLoading()) {
976 mFooters.add(new LoadingFooter());
Jeff Sharkey20b32272013-09-03 15:25:52 -0700977 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700978
Ben Kwa24be5d32015-08-27 16:04:46 -0700979 if (mModel.isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700980 mEmptyView.setVisibility(View.VISIBLE);
981 } else {
982 mEmptyView.setVisibility(View.GONE);
983 }
984
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700985 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700986 }
987
988 @Override
Steve McKayef280152015-06-11 10:10:49 -0700989 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
990 final State state = getDisplayState(DirectoryFragment.this);
991 final LayoutInflater inflater = LayoutInflater.from(getContext());
992 switch (state.derivedMode) {
993 case MODE_GRID:
994 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
995 case MODE_LIST:
996 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
997 case MODE_UNKNOWN:
998 default:
999 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -07001000 }
1001 }
1002
Steve McKayef280152015-06-11 10:10:49 -07001003 @Override
1004 public void onBindViewHolder(DocumentHolder holder, int position) {
1005
1006 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001007 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001008 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -07001009 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1010 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1011 context, mThumbSize);
1012
Ben Kwa24be5d32015-08-27 16:04:46 -07001013 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001014 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001015
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001016 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1017 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001018 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1019 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1020 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1021 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1022 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1023 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1024 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1025 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001026
Steve McKayef280152015-06-11 10:10:49 -07001027 holder.docId = docId;
1028 final View itemView = holder.view;
Ben Kwa24be5d32015-08-27 16:04:46 -07001029 itemView.setActivated(mModel.isSelected(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001030
Steve McKayef280152015-06-11 10:10:49 -07001031 final View line1 = itemView.findViewById(R.id.line1);
1032 final View line2 = itemView.findViewById(R.id.line2);
1033
1034 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1035 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1036 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1037 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1038 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1039 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1040 final TextView date = (TextView) itemView.findViewById(R.id.date);
1041 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001042
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001043 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001044 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001045 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001046 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001047 }
1048
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001049 iconMime.animate().cancel();
1050 iconThumb.animate().cancel();
1051
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001052 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001053 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001054 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001055 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001056
Jeff Sharkey7e544612014-08-29 15:38:27 -07001057 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1058 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1059
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001060 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001061 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001062 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001063 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001064 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001065 iconThumb.setImageBitmap(cachedResult);
1066 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001067 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001068 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001069 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001071 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001072 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001073 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001074 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001075 }
1076
1077 // Always throw MIME icon into place, even when a thumbnail is being
1078 // loaded in background.
1079 if (cacheHit) {
1080 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001081 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001082 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001083 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001084 iconMime.setAlpha(1f);
1085 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001086 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001087 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001088 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001089 }
1090
Jeff Sharkey9656a532013-09-13 13:42:19 -07001091 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001092 boolean hasLine2 = false;
1093
Jeff Sharkey9656a532013-09-13 13:42:19 -07001094 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1095 if (!hideTitle) {
1096 title.setText(docDisplayName);
1097 hasLine1 = true;
1098 }
1099
1100 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001101 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001102 // We've already had to enumerate roots before any results can
1103 // be shown, so this will never block.
1104 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001105 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001106 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001107 } else {
Steve McKayef280152015-06-11 10:10:49 -07001108 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001109 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001110
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001111 if (summary != null) {
1112 final boolean alwaysShowSummary = getResources()
1113 .getBoolean(R.bool.always_show_summary);
1114 if (alwaysShowSummary) {
1115 summary.setText(root.getDirectoryString());
1116 summary.setVisibility(View.VISIBLE);
1117 hasLine2 = true;
1118 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001119 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001120 // No summary needed if icon speaks for itself
1121 summary.setVisibility(View.INVISIBLE);
1122 } else {
1123 summary.setText(root.getDirectoryString());
1124 summary.setVisibility(View.VISIBLE);
1125 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1126 hasLine2 = true;
1127 }
1128 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001129 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001130 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001131 // Directories showing thumbnails in grid mode get a little icon
1132 // hint to remind user they're a directory.
1133 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1134 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001135 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001136 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001137 }
1138
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001139 if (summary != null) {
1140 if (docSummary != null) {
1141 summary.setText(docSummary);
1142 summary.setVisibility(View.VISIBLE);
1143 hasLine2 = true;
1144 } else {
1145 summary.setVisibility(View.INVISIBLE);
1146 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001147 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001148 }
1149
Jeff Sharkey9656a532013-09-13 13:42:19 -07001150 if (icon1 != null) icon1.setVisibility(View.GONE);
1151 if (icon2 != null) icon2.setVisibility(View.GONE);
1152
1153 if (iconDrawable != null) {
1154 if (hasLine1) {
1155 icon1.setVisibility(View.VISIBLE);
1156 icon1.setImageDrawable(iconDrawable);
1157 } else {
1158 icon2.setVisibility(View.VISIBLE);
1159 icon2.setImageDrawable(iconDrawable);
1160 }
1161 }
1162
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001163 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001164 date.setText(null);
1165 } else {
Steve McKayef280152015-06-11 10:10:49 -07001166 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001167 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001168 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001169
1170 if (state.showSize) {
1171 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001172 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001173 size.setText(null);
1174 } else {
Steve McKayef280152015-06-11 10:10:49 -07001175 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001176 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001177 }
1178 } else {
1179 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001180 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001181
Jeff Sharkey9656a532013-09-13 13:42:19 -07001182 if (line1 != null) {
1183 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1184 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001185 if (line2 != null) {
1186 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1187 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001188
Steve McKayef280152015-06-11 10:10:49 -07001189 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001190
1191 iconMime.setAlpha(iconAlpha);
1192 iconThumb.setAlpha(iconAlpha);
1193 if (icon1 != null) icon1.setAlpha(iconAlpha);
1194 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001195
Steve McKay8e258c62015-05-06 14:27:57 -07001196 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001197 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001198 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001199 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001200
Steve McKay351a7492015-08-04 10:11:01 -07001201 @Override
Steve McKayef280152015-06-11 10:10:49 -07001202 public int getItemCount() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001203 return mModel.getItemCount();
Steve McKayef280152015-06-11 10:10:49 -07001204 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001205 }
1206
1207 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001208 public int getItemViewType(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001209 final int itemCount = mModel.getItemCount();
1210 if (position < itemCount) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001211 return 0;
1212 } else {
Ben Kwa24be5d32015-08-27 16:04:46 -07001213 position -= itemCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001214 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001215 }
1216 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001217 }
1218
1219 private static String formatTime(Context context, long when) {
1220 // TODO: DateUtils should make this easier
1221 Time then = new Time();
1222 then.set(when);
1223 Time now = new Time();
1224 now.setToNow();
1225
1226 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1227 | DateUtils.FORMAT_ABBREV_ALL;
1228
1229 if (then.year != now.year) {
1230 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1231 } else if (then.yearDay != now.yearDay) {
1232 flags |= DateUtils.FORMAT_SHOW_DATE;
1233 } else {
1234 flags |= DateUtils.FORMAT_SHOW_TIME;
1235 }
1236
1237 return DateUtils.formatDateTime(context, when, flags);
1238 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001239
1240 private String findCommonMimeType(List<String> mimeTypes) {
1241 String[] commonType = mimeTypes.get(0).split("/");
1242 if (commonType.length != 2) {
1243 return "*/*";
1244 }
1245
1246 for (int i = 1; i < mimeTypes.size(); i++) {
1247 String[] type = mimeTypes.get(i).split("/");
1248 if (type.length != 2) continue;
1249
1250 if (!commonType[1].equals(type[1])) {
1251 commonType[1] = "*";
1252 }
1253
1254 if (!commonType[0].equals(type[0])) {
1255 commonType[0] = "*";
1256 commonType[1] = "*";
1257 break;
1258 }
1259 }
1260
1261 return commonType[0] + "/" + commonType[1];
1262 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001263
1264 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001265 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001266 if (v.isEnabled() == enabled) return;
1267 v.setEnabled(enabled);
1268
1269 if (v instanceof ViewGroup) {
1270 final ViewGroup vg = (ViewGroup) v;
1271 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1272 setEnabledRecursive(vg.getChildAt(i), enabled);
1273 }
1274 }
1275 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001276
1277 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1278 final State state = getDisplayState(DirectoryFragment.this);
1279
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001280 // Directories are always enabled
1281 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1282 return true;
1283 }
1284
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001285 // Read-only files are disabled when creating
1286 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1287 return false;
1288 }
1289
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001290 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1291 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001292
Steve McKay1f199482015-05-20 15:58:42 -07001293 private void copyFromClipboard() {
1294 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1295
1296 @Override
1297 protected List<DocumentInfo> doInBackground(Void... params) {
1298 return mClipper.getClippedDocuments();
1299 }
1300
1301 @Override
1302 protected void onPostExecute(List<DocumentInfo> docs) {
1303 DocumentInfo destination =
1304 ((BaseActivity) getActivity()).getCurrentDirectory();
1305 copyDocuments(docs, destination);
1306 }
1307 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001308 }
1309
Steve McKay1f199482015-05-20 15:58:42 -07001310 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001311 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001312 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001313
Steve McKay1f199482015-05-20 15:58:42 -07001314 @Override
1315 protected List<DocumentInfo> doInBackground(Void... params) {
1316 return mClipper.getDocumentsFromClipData(clipData);
1317 }
1318
1319 @Override
1320 protected void onPostExecute(List<DocumentInfo> docs) {
1321 copyDocuments(docs, destination);
1322 }
1323 }.execute();
1324 }
1325
1326 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1327 if (!canCopy(docs, destination)) {
1328 Toast.makeText(
1329 getActivity(),
1330 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001331 return;
1332 }
1333
Steve McKay1f199482015-05-20 15:58:42 -07001334 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001335 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001336 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001337
Steve McKay1f199482015-05-20 15:58:42 -07001338 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001339 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001340 if (destination != null) {
1341 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001342 tmpStack.addAll(curStack);
1343 } else {
1344 tmpStack = curStack;
1345 }
1346
Steve McKay1f199482015-05-20 15:58:42 -07001347 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001348 }
1349
1350 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1351 Context context = getActivity();
1352 final ContentResolver resolver = context.getContentResolver();
1353 ClipData clipData = null;
1354 for (DocumentInfo doc : docs) {
1355 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1356 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001357 // TODO: figure out what this string should be.
1358 // Currently it is not displayed anywhere in the UI, but this might change.
1359 final String label = "";
1360 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001361 } else {
1362 // TODO: update list of mime types in ClipData.
1363 clipData.addItem(new ClipData.Item(uri));
1364 }
1365 }
1366 return clipData;
1367 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001368
Steve McKay1f199482015-05-20 15:58:42 -07001369 void copySelectedToClipboard() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001370 Selection sel = mModel.getSelection(new Selection());
Steve McKayef280152015-06-11 10:10:49 -07001371 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001372 }
Steve McKay0599a442015-05-05 14:50:00 -07001373
Steve McKayef280152015-06-11 10:10:49 -07001374 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001375 new GetDocumentsTask() {
1376 @Override
1377 void onDocumentsReady(List<DocumentInfo> docs) {
1378 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001379 Activity activity = getActivity();
1380 Toast.makeText(activity,
1381 activity.getResources().getQuantityString(
1382 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1383 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001384 }
Steve McKayef280152015-06-11 10:10:49 -07001385 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001386 }
1387
1388 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001389 copyFromClipboard();
1390 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001391 }
1392
Steve McKay0599a442015-05-05 14:50:00 -07001393 /**
1394 * Returns true if the list of files can be copied to destination. Note that this
1395 * is a policy check only. Currently the method does not attempt to verify
1396 * available space or any other environmental aspects possibly resulting in
1397 * failure to copy.
1398 *
1399 * @return true if the list of files can be copied to destination.
1400 */
1401 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
Ben Kwa91923182015-08-27 16:06:33 -07001402 BaseActivity activity = (BaseActivity) getActivity();
Steve McKay0599a442015-05-05 14:50:00 -07001403
1404 final RootInfo root = activity.getCurrentRoot();
1405
1406 // Can't copy folders to Downloads.
1407 if (root.isDownloads()) {
1408 for (DocumentInfo docs : files) {
1409 if (docs.isDirectory()) {
1410 return false;
1411 }
1412 }
1413 }
1414
1415 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1416 }
1417
1418 void selectAllFiles() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001419 boolean changed = mModel.selectAll();
Steve McKay9459a7c2015-07-24 13:14:20 -07001420 if (changed) {
1421 updateDisplayState();
1422 }
Steve McKay0599a442015-05-05 14:50:00 -07001423 }
1424
Steve McKayef280152015-06-11 10:10:49 -07001425 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001426 // Listen for drops on non-directory items and empty space.
1427 view.setOnDragListener(mOnDragListener);
1428 }
1429
1430 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1431 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1432 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1433 // Make a directory item a drop target. Drop on non-directories and empty space
1434 // is handled at the list/grid view level.
1435 view.setOnDragListener(mOnDragListener);
1436 }
1437
1438 // Temporary: attaching the listener to the title only.
1439 // Attaching to the entire item conflicts with the item long click handler responsible
1440 // for item selection.
1441 final View title = view.findViewById(android.R.id.title);
1442 title.setOnLongClickListener(mLongClickListener);
1443 }
1444
1445 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1446 @Override
1447 public boolean onDrag(View v, DragEvent event) {
1448 switch (event.getAction()) {
1449 case DragEvent.ACTION_DRAG_STARTED:
1450 // TODO: Check if the event contains droppable data.
1451 return true;
1452
1453 // TODO: Highlight potential drop target directory?
1454 // TODO: Expand drop target directory on hover?
1455 case DragEvent.ACTION_DRAG_ENTERED:
1456 case DragEvent.ACTION_DRAG_LOCATION:
1457 case DragEvent.ACTION_DRAG_EXITED:
1458 case DragEvent.ACTION_DRAG_ENDED:
1459 return true;
1460
1461 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001462 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001463 DocumentInfo dstDir = null;
1464 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001465 Cursor dstCursor = mModel.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001466 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001467 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1468 // TODO: Do not drop into the directory where the documents came from.
1469 }
1470 copyFromClipData(event.getClipData(), dstDir);
1471 return true;
1472 }
1473 return false;
1474 }
1475 };
1476
1477 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1478 @Override
1479 public boolean onLongClick(View v) {
1480 final List<DocumentInfo> docs = getDraggableDocuments(v);
1481 if (docs.isEmpty()) {
1482 return false;
1483 }
1484 v.startDrag(
1485 getClipDataFromDocuments(docs),
1486 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1487 null,
1488 View.DRAG_FLAG_GLOBAL
1489 );
1490 return true;
1491 }
1492 };
1493
1494 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001495 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001496 if (position == android.widget.AdapterView.INVALID_POSITION) {
1497 return Collections.EMPTY_LIST;
1498 }
1499
Ben Kwa24be5d32015-08-27 16:04:46 -07001500 final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001501 if (!selectedDocs.isEmpty()) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001502 if (!mModel.isSelected(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001503 // There is a selection that does not include the current item, drag nothing.
1504 return Collections.EMPTY_LIST;
1505 }
1506 return selectedDocs;
1507 }
1508
Ben Kwa24be5d32015-08-27 16:04:46 -07001509 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001510 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001511 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001512
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001513 return Lists.newArrayList(doc);
1514 }
1515
1516 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1517 if (docs.size() == 1) {
1518 final DocumentInfo doc = docs.get(0);
1519 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1520 doc.mimeType, doc.icon, getDisplayState(this));
1521 }
1522 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1523 }
1524
1525 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1526 String docMimeType, int docIcon, State state) {
1527 if (docIcon != 0) {
1528 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1529 } else {
1530 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1531 state.derivedMode);
1532 }
1533 }
1534
Steve McKayef280152015-06-11 10:10:49 -07001535 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1536 implements Preemptable {
1537 private final Uri mUri;
1538 private final ImageView mIconMime;
1539 private final ImageView mIconThumb;
1540 private final Point mThumbSize;
1541 private final float mTargetAlpha;
1542 private final CancellationSignal mSignal;
1543
1544 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1545 float targetAlpha) {
1546 mUri = uri;
1547 mIconMime = iconMime;
1548 mIconThumb = iconThumb;
1549 mThumbSize = thumbSize;
1550 mTargetAlpha = targetAlpha;
1551 mSignal = new CancellationSignal();
1552 }
1553
1554 @Override
1555 public void preempt() {
1556 cancel(false);
1557 mSignal.cancel();
1558 }
1559
1560 @Override
1561 protected Bitmap doInBackground(Uri... params) {
1562 if (isCancelled()) return null;
1563
1564 final Context context = mIconThumb.getContext();
1565 final ContentResolver resolver = context.getContentResolver();
1566
1567 ContentProviderClient client = null;
1568 Bitmap result = null;
1569 try {
1570 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1571 resolver, mUri.getAuthority());
1572 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1573 if (result != null) {
1574 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1575 context, mThumbSize);
1576 thumbs.put(mUri, result);
1577 }
1578 } catch (Exception e) {
1579 if (!(e instanceof OperationCanceledException)) {
1580 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1581 }
1582 } finally {
1583 ContentProviderClient.releaseQuietly(client);
1584 }
1585 return result;
1586 }
1587
1588 @Override
1589 protected void onPostExecute(Bitmap result) {
1590 if (mIconThumb.getTag() == this && result != null) {
1591 mIconThumb.setTag(null);
1592 mIconThumb.setImageBitmap(result);
1593
1594 mIconMime.setAlpha(mTargetAlpha);
1595 mIconMime.animate().alpha(0f).start();
1596 mIconThumb.setAlpha(0f);
1597 mIconThumb.animate().alpha(mTargetAlpha).start();
1598 }
1599 }
1600 }
1601
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001602 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1603
1604 private final Drawable mShadow;
1605
1606 private final int mShadowDimension;
1607
1608 public DrawableShadowBuilder(Drawable shadow) {
1609 mShadow = shadow;
1610 mShadowDimension = getResources().getDimensionPixelSize(
1611 R.dimen.drag_shadow_size);
1612 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1613 }
1614
Ben Kwa24be5d32015-08-27 16:04:46 -07001615 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001616 public void onProvideShadowMetrics(
1617 Point shadowSize, Point shadowTouchPoint) {
1618 shadowSize.set(mShadowDimension, mShadowDimension);
1619 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1620 }
1621
Ben Kwa24be5d32015-08-27 16:04:46 -07001622 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001623 public void onDrawShadow(Canvas canvas) {
1624 mShadow.draw(canvas);
1625 }
1626 }
Steve McKay1f199482015-05-20 15:58:42 -07001627
1628 private FragmentTuner pickFragmentTuner(final State state) {
1629 return state.action == ACTION_BROWSE_ALL
Steve McKay0fbfc652015-08-20 16:48:49 -07001630 ? new FilesTuner()
Steve McKay1f199482015-05-20 15:58:42 -07001631 : new DefaultTuner(state);
1632 }
1633
1634 /**
1635 * Interface for specializing the Fragment for the "host" Activity.
1636 * Feel free to expand the role of this class to handle other specializations.
1637 */
1638 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001639 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001640 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001641 }
1642
1643 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001644 * Abstract task providing support for loading documents *off*
1645 * the main thread. And if it isn't obvious, creating a list
1646 * of documents (especially large lists) can be pretty expensive.
1647 */
1648 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001649 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001650 @Override
Steve McKayef280152015-06-11 10:10:49 -07001651 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001652 return mModel.getDocuments(selected[0]);
Steve McKay9276f3b2015-05-27 16:11:42 -07001653 }
1654
1655 @Override
1656 protected final void onPostExecute(List<DocumentInfo> docs) {
1657 onDocumentsReady(docs);
1658 }
1659
1660 abstract void onDocumentsReady(List<DocumentInfo> docs);
1661 }
1662
1663 /**
Steve McKay1f199482015-05-20 15:58:42 -07001664 * Provides support for Platform specific specializations of DirectoryFragment.
1665 */
1666 private static final class DefaultTuner implements FragmentTuner {
1667
1668 private final State mState;
1669
1670 public DefaultTuner(State state) {
1671 mState = state;
1672 }
1673
1674 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001675 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001676 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1677
1678 final MenuItem open = menu.findItem(R.id.menu_open);
1679 final MenuItem share = menu.findItem(R.id.menu_share);
1680 final MenuItem delete = menu.findItem(R.id.menu_delete);
1681 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1682 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1683 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1684
1685 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1686 || mState.action == ACTION_BROWSE);
1687
1688 open.setVisible(!manageOrBrowse);
1689 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001690 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001691 // Disable copying from the Recents view.
1692 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1693 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1694
Steve McKay0fbfc652015-08-20 16:48:49 -07001695 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001696 copyToClipboard.setVisible(false);
1697 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001698
1699 @Override
1700 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001701 }
1702
1703 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001704 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001705 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001706 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001707 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001708 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001709 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001710 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001711 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1712
1713 menu.findItem(R.id.menu_open).setVisible(false);
1714 menu.findItem(R.id.menu_copy_to).setVisible(false);
1715 menu.findItem(R.id.menu_move_to).setVisible(false);
1716 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001717
1718 @Override
Kyle Horimoto62a7fd02015-08-18 13:25:29 -07001719 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001720 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001721
1722 /**
1723 * The data model for the current loaded directory.
1724 */
1725 private final class Model implements DocumentContext {
1726 private MultiSelectManager mSelectionManager;
1727 private int mCursorCount;
1728 private boolean mIsLoading;
1729 @Nullable private Cursor mCursor;
1730 @Nullable private String info;
1731 @Nullable private String error;
Ben Kwa91923182015-08-27 16:06:33 -07001732 private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
Ben Kwa24be5d32015-08-27 16:04:46 -07001733
1734 /**
1735 * Sets the selection manager used by the model.
1736 * TODO: the model should instantiate the selection manager. See onActivityCreated.
1737 */
1738 void setSelectionManager(MultiSelectManager mgr) {
1739 mSelectionManager = mgr;
1740 }
1741
1742 /**
1743 * Selects all files in the current directory.
1744 * @return true if the selection state changed for any files.
1745 */
1746 boolean selectAll() {
1747 return mSelectionManager.setItemsSelected(0, mCursorCount, true);
1748 }
1749
1750 /**
1751 * Clones the current selection into the given Selection object.
1752 * @param selection
1753 * @return The selection that was passed in, for convenience.
1754 */
1755 Selection getSelection(Selection selection) {
1756 return mSelectionManager.getSelection(selection);
1757 }
1758
1759 /**
1760 * @return The current selection (the live instance, not a copy).
1761 */
1762 Selection getSelection() {
1763 return mSelectionManager.getSelection();
1764 }
1765
1766 boolean isSelected(int position) {
1767 return mSelectionManager.getSelection().contains(position);
1768 }
1769
1770 void clearSelection() {
1771 mSelectionManager.clearSelection();
1772 }
1773
1774 void update(DirectoryResult result) {
1775 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
1776
1777 if (result == null) {
1778 mCursor = null;
1779 mCursorCount = 0;
1780 info = null;
1781 error = null;
1782 mIsLoading = false;
1783 return;
1784 }
1785
1786 if (result.exception != null) {
1787 Log.e(TAG, "Error while loading directory contents", result.exception);
1788 error = getString(R.string.query_error);
1789 return;
1790 }
1791
1792 mCursor = result.cursor;
1793 mCursorCount = mCursor.getCount();
1794
1795 final Bundle extras = mCursor.getExtras();
1796 if (extras != null) {
1797 info = extras.getString(DocumentsContract.EXTRA_INFO);
1798 error = extras.getString(DocumentsContract.EXTRA_ERROR);
1799 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
1800 }
1801 }
1802
1803 private int getItemCount() {
Ben Kwa91923182015-08-27 16:06:33 -07001804 return mCursorCount - mMarkedForDeletion.size();
Ben Kwa24be5d32015-08-27 16:04:46 -07001805 }
1806
1807 private Cursor getItem(int position) {
Ben Kwa91923182015-08-27 16:06:33 -07001808 // Items marked for deletion are masked out of the UI. To do this, for every marked
1809 // item whose position is less than the requested item position, advance the requested
1810 // position by 1.
1811 final int originalPos = position;
1812 final int size = mMarkedForDeletion.size();
1813 for (int i = 0; i <= size; ++i) {
1814 // It'd be more concise, but less efficient, to iterate over positions while calling
1815 // mMarkedForDeletion.get. Instead, iterate over deleted entries.
1816 if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
1817 ++position;
1818 }
1819 }
1820
1821 if (DEBUG) {
1822 Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos
1823 + " Adjusted: " + position);
1824 }
1825
Ben Kwa24be5d32015-08-27 16:04:46 -07001826 if (position >= mCursorCount) {
1827 throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
1828 mCursorCount + " items");
1829 }
1830
1831 mCursor.moveToPosition(position);
1832 return mCursor;
1833 }
1834
1835 private boolean isEmpty() {
1836 return mCursorCount == 0;
1837 }
1838
1839 private boolean isLoading() {
1840 return mIsLoading;
1841 }
1842
1843 private List<DocumentInfo> getSelectedDocuments() {
1844 Selection sel = getSelection(new Selection());
1845 return getDocuments(sel);
1846 }
1847
1848 private List<DocumentInfo> getDocuments(Selection items) {
Ben Kwa91923182015-08-27 16:06:33 -07001849 final int size = (items != null) ? items.size() : 0;
Ben Kwa24be5d32015-08-27 16:04:46 -07001850
Ben Kwa91923182015-08-27 16:06:33 -07001851 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwa24be5d32015-08-27 16:04:46 -07001852 for (int i = 0; i < size; i++) {
1853 final Cursor cursor = getItem(items.get(i));
1854 checkNotNull(cursor, "Cursor cannot be null.");
1855 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1856 docs.add(doc);
1857 }
1858 return docs;
1859 }
1860
1861 @Override
1862 public Cursor getCursor() {
1863 if (Looper.myLooper() != Looper.getMainLooper()) {
1864 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1865 }
1866 return mCursor;
1867 }
Ben Kwa91923182015-08-27 16:06:33 -07001868
1869 private List<DocumentInfo> getDocumentsMarkedForDeletion() {
1870 final int size = mMarkedForDeletion.size();
1871 List<DocumentInfo> docs = new ArrayList<>(size);
1872
1873 for (int i = 0; i < size; ++i) {
1874 final int position = mMarkedForDeletion.keyAt(i);
1875 checkState(position < mCursorCount);
1876 mCursor.moveToPosition(position);
1877 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
1878 docs.add(doc);
1879 }
1880 return docs;
1881 }
1882
1883 /**
1884 * Marks the given files for deletion. This will remove them from the UI. Clients must then
1885 * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
1886 * the deletion, respectively. Only one deletion operation is allowed at a time.
1887 *
1888 * @param selected A selection representing the files to delete.
1889 */
1890 public void markForDeletion(Selection selected) {
1891 // Only one deletion operation at a time.
1892 checkState(mMarkedForDeletion.size() == 0);
1893 // There should never be more to delete than what exists.
1894 checkState(mCursorCount >= selected.size());
1895
1896 final int size = selected.size();
1897 for (int i = 0; i < size; ++i) {
1898 int position = selected.get(i);
1899 if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
1900 mMarkedForDeletion.append(position, true);
1901 mAdapter.notifyItemRemoved(position);
1902 }
1903 }
1904
1905 /**
1906 * Cancels an ongoing deletion operation. All files currently marked for deletion will be
1907 * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}.
1908 */
1909 public void undoDeletion() {
1910 // Iterate over deleted items, temporarily marking them false in the deletion list, and
1911 // re-adding them to the UI.
1912 final int size = mMarkedForDeletion.size();
1913 for (int i = 0; i < size; ++i) {
1914 final int position = mMarkedForDeletion.keyAt(i);
1915 mMarkedForDeletion.put(position, false);
1916 mAdapter.notifyItemInserted(position);
1917 }
1918
1919 // Then, clear the deletion list.
1920 mMarkedForDeletion.clear();
1921 }
1922
1923 /**
1924 * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
1925 * deleted. See {@link #markForDeletion(Selection)}.
1926 */
1927 public void finalizeDeletion() {
1928 final Context context = getActivity();
1929 final ContentResolver resolver = context.getContentResolver();
1930 new DeleteFilesTask(resolver).execute();
1931 }
1932
1933 /**
1934 * A Task which collects the DocumentInfo for documents that have been marked for deletion,
1935 * and actually deletes them.
1936 */
1937 private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
1938 private ContentResolver mResolver;
1939
1940 public DeleteFilesTask(ContentResolver resolver) {
1941 mResolver = resolver;
1942 }
1943
1944 @Override
1945 protected List<DocumentInfo> doInBackground(Void... params) {
1946 return getDocumentsMarkedForDeletion();
1947 }
1948
1949 @Override
1950 protected void onPostExecute(List<DocumentInfo> docs) {
1951 boolean hadTrouble = false;
1952 for (DocumentInfo doc : docs) {
1953 if (!doc.isDeleteSupported()) {
1954 Log.w(TAG, doc + " could not be deleted. Skipping...");
1955 hadTrouble = true;
1956 continue;
1957 }
1958
1959 ContentProviderClient client = null;
1960 try {
1961 if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
1962 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1963 mResolver, doc.derivedUri.getAuthority());
1964 DocumentsContract.deleteDocument(client, doc.derivedUri);
1965 } catch (Exception e) {
1966 Log.w(TAG, "Failed to delete " + doc);
1967 hadTrouble = true;
1968 } finally {
1969 ContentProviderClient.releaseQuietly(client);
1970 }
1971 }
1972
1973 if (hadTrouble) {
1974 // TODO show which files failed?
1975 Snackbar.make(DirectoryFragment.this.getView(),
1976 R.string.toast_failed_delete,
1977 Snackbar.LENGTH_LONG).show();
1978 if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed.");
1979 } else {
1980 if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
1981 }
1982 mMarkedForDeletion.clear();
1983 }
1984 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001985 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001986}