blob: a891cfdc718e4993de27473cd79c22592593deb9 [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 Kwa7461a952015-09-01 11:03:01 -070065import android.support.annotation.VisibleForTesting;
Ben Kwa91923182015-08-27 16:06:33 -070066import android.support.design.widget.Snackbar;
Steve McKayef280152015-06-11 10:10:49 -070067import android.support.v7.widget.GridLayoutManager;
68import android.support.v7.widget.LinearLayoutManager;
69import android.support.v7.widget.RecyclerView;
70import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070071import android.support.v7.widget.RecyclerView.RecyclerListener;
72import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070073import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070074import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070075import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070076import android.text.format.Time;
77import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070078import android.util.SparseArray;
Ben Kwa91923182015-08-27 16:06:33 -070079import android.util.SparseBooleanArray;
Ben Kwa0574b182015-09-08 07:31:19 -070080import android.util.TypedValue;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070081import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070082import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070085import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070086import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070087import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070088import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070089import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070090import android.view.ViewGroup;
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -070091import android.view.ViewParent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070092import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070093import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070094import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070095
Steve McKay351a7492015-08-04 10:11:01 -070096import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070097import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070098import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070099import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700100import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700101import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900102import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700103import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -0700104import com.android.internal.util.Preconditions;
Steve McKayfad3d4a2015-09-22 15:09:21 -0700105
Steve McKayfefcd702015-08-20 16:19:38 +0000106import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700107
108import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700109import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700110import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700111
112/**
113 * Display the documents inside a single directory.
114 */
115public class DirectoryFragment extends Fragment {
116
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700117 public static final int TYPE_NORMAL = 1;
118 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700119 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700120
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700121 public static final int ANIM_NONE = 1;
122 public static final int ANIM_SIDE = 2;
123 public static final int ANIM_DOWN = 3;
124 public static final int ANIM_UP = 4;
125
Ben Kwaf5858932015-04-07 15:43:39 -0700126 public static final int REQUEST_COPY_DESTINATION = 1;
127
Steve McKayef280152015-06-11 10:10:49 -0700128 private static final int LOADER_ID = 42;
129 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700130 private static final boolean DEBUG_ENABLE_DND = false;
131
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700132 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700133 private static final String EXTRA_ROOT = "root";
134 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700135 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700136 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700137
Ben Kwa7461a952015-09-01 11:03:01 -0700138 private Model mModel;
Ben Kwa379e1762015-09-21 10:49:52 -0700139 private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
Ben Kwa24be5d32015-08-27 16:04:46 -0700140
Steve McKayef280152015-06-11 10:10:49 -0700141 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700142
Steve McKayef280152015-06-11 10:10:49 -0700143 private View mEmptyView;
144 private RecyclerView mRecView;
145
146 private int mType = TYPE_NORMAL;
147 private String mStateKey;
148
149 private int mLastMode = MODE_UNKNOWN;
150 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
151 private boolean mLastShowSize;
152 private boolean mHideGridTitles;
153 private boolean mSvelteRecents;
154 private Point mThumbSize;
155 private DocumentsAdapter mAdapter;
156 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700157 private FragmentTuner mFragmentTuner;
158 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700159 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700160 private LinearLayoutManager mListLayout;
161 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700162 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700163
Ben Kwac42fa402015-09-16 08:04:37 -0700164 private MessageBar mMessageBar;
Ben Kwa379e1762015-09-21 10:49:52 -0700165 private View mProgressBar;
Ben Kwac42fa402015-09-16 08:04:37 -0700166
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700167 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
168 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700169 }
170
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700171 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
172 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700173 }
174
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700175 public static void showRecentsOpen(FragmentManager fm, int anim) {
176 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700177 }
178
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700179 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
180 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700181 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700182 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700183 args.putParcelable(EXTRA_ROOT, root);
184 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700185 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700186
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700187 final FragmentTransaction ft = fm.beginTransaction();
188 switch (anim) {
189 case ANIM_SIDE:
190 args.putBoolean(EXTRA_IGNORE_STATE, true);
191 break;
192 case ANIM_DOWN:
193 args.putBoolean(EXTRA_IGNORE_STATE, true);
194 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
195 break;
196 case ANIM_UP:
197 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
198 break;
199 }
200
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700201 final DirectoryFragment fragment = new DirectoryFragment();
202 fragment.setArguments(args);
203
Jeff Sharkey76112212013-08-06 11:26:10 -0700204 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700205 ft.commitAllowingStateLoss();
206 }
207
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700208 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
209 final StringBuilder builder = new StringBuilder();
210 builder.append(root != null ? root.authority : "null").append(';');
211 builder.append(root != null ? root.rootId : "null").append(';');
212 builder.append(doc != null ? doc.documentId : "null");
213 return builder.toString();
214 }
215
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700216 public static DirectoryFragment get(FragmentManager fm) {
217 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700218 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700219 }
220
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700221 @Override
222 public View onCreateView(
223 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
224 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700225 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700226 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
227
Ben Kwac42fa402015-09-16 08:04:37 -0700228 mMessageBar = MessageBar.create(getChildFragmentManager());
Ben Kwa379e1762015-09-21 10:49:52 -0700229 mProgressBar = view.findViewById(R.id.progressbar);
Ben Kwac42fa402015-09-16 08:04:37 -0700230
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700231 mEmptyView = view.findViewById(android.R.id.empty);
232
Steve McKayef280152015-06-11 10:10:49 -0700233 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
234 mRecView.setRecyclerListener(
235 new RecyclerListener() {
236 @Override
237 public void onViewRecycled(ViewHolder holder) {
238 cancelThumbnailTask(holder.itemView);
239 }
240 });
Steve McKay8e258c62015-05-06 14:27:57 -0700241
Steve McKayd57f5fa2015-07-23 16:33:41 -0700242 // TODO: Rather than update columns on layout changes, push this
243 // code (or something like it) into GridLayoutManager.
244 mRecView.addOnLayoutChangeListener(
245 new OnLayoutChangeListener() {
246
247 @Override
248 public void onLayoutChange(
249 View v, int left, int top, int right, int bottom, int oldLeft,
250 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000251 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700252 if (mGridLayout != null) {
253 mGridLayout.setSpanCount(mColumnCount);
254 }
255 }
256 });
257
258 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700259 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700260 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700261 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700262
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700263 return view;
264 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700265
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700266 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700267 public void onDestroyView() {
268 super.onDestroyView();
269
270 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700271 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700272 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700273 final View view = mRecView.getChildAt(i);
274 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700275 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700276
Steve McKayef280152015-06-11 10:10:49 -0700277 // Clear any outstanding selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700278 mModel.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700279 }
280
281 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700282 public void onActivityCreated(Bundle savedInstanceState) {
283 super.onActivityCreated(savedInstanceState);
284
285 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700286 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700287
Jeff Sharkey9656a532013-09-13 13:42:19 -0700288 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
289 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
290
Steve McKayef280152015-06-11 10:10:49 -0700291 mAdapter = new DocumentsAdapter(context);
292 mRecView.setAdapter(mAdapter);
293
294 GestureDetector.SimpleOnGestureListener listener =
295 new GestureDetector.SimpleOnGestureListener() {
296 @Override
297 public boolean onSingleTapUp(MotionEvent e) {
298 return DirectoryFragment.this.onSingleTapUp(e);
299 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700300 @Override
301 public boolean onDoubleTap(MotionEvent e) {
302 Log.d(TAG, "Handling double tap.");
303 return DirectoryFragment.this.onDoubleTap(e);
304 }
Steve McKayef280152015-06-11 10:10:49 -0700305 };
306
Ben Kwa24be5d32015-08-27 16:04:46 -0700307 // TODO: instead of inserting the view into the constructor, extract listener-creation code
308 // and set the listener on the view after the fact. Then the view doesn't need to be passed
309 // into the selection manager which is passed into the model.
310 MultiSelectManager selMgr= new MultiSelectManager(
Steve McKay57394872015-08-12 14:48:34 -0700311 mRecView,
312 listener,
313 state.allowMultiple
314 ? MultiSelectManager.MODE_MULTIPLE
315 : MultiSelectManager.MODE_SINGLE);
Ben Kwa24be5d32015-08-27 16:04:46 -0700316 selMgr.addCallback(new SelectionModeListener());
Ben Kwa7461a952015-09-01 11:03:01 -0700317
Ben Kwa379e1762015-09-21 10:49:52 -0700318 mModel = new Model(context, selMgr, mAdapter);
319 mModel.addUpdateListener(mModelUpdateListener);
Steve McKayef280152015-06-11 10:10:49 -0700320
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700321 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700322 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700323
Steve McKay1f199482015-05-20 15:58:42 -0700324 mFragmentTuner = pickFragmentTuner(state);
325 mClipper = new DocumentClipper(context);
326
Jeff Sharkey9656a532013-09-13 13:42:19 -0700327 if (mType == TYPE_RECENT_OPEN) {
328 // Hide titles when showing recents for picking images/videos
329 mHideGridTitles = MimePredicate.mimeMatches(
330 MimePredicate.VISUAL_MIMES, state.acceptMimes);
331 } else {
332 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
333 }
334
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700335 final ActivityManager am = (ActivityManager) context.getSystemService(
336 Context.ACTIVITY_SERVICE);
337 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
338
Jeff Sharkey46899c82013-08-18 22:26:48 -0700339 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700340 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700341 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700342 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700343
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700344 Uri contentsUri;
345 switch (mType) {
346 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700347 contentsUri = DocumentsContract.buildChildDocumentsUri(
348 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700349 if (state.action == ACTION_MANAGE) {
350 contentsUri = DocumentsContract.setManageMode(contentsUri);
351 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700352 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700353 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700354 case TYPE_SEARCH:
355 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700356 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700357 if (state.action == ACTION_MANAGE) {
358 contentsUri = DocumentsContract.setManageMode(contentsUri);
359 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700360 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700361 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700362 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700363 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700364 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700365 default:
366 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700367 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700368 }
369
370 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700371 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700372 if (!isAdded()) return;
373
Ben Kwa24be5d32015-08-27 16:04:46 -0700374 mModel.update(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700375
376 // Push latest state up to UI
377 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700378 if (result.mode != MODE_UNKNOWN) {
379 state.derivedMode = result.mode;
380 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700381 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700382 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700383
384 updateDisplayState();
385
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700386 // When launched into empty recents, show drawer
Ben Kwa24be5d32015-08-27 16:04:46 -0700387 if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
Steve McKayb68dd222015-04-20 17:18:15 -0700388 context instanceof DocumentsActivity) {
389 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700390 }
391
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700392 // Restore any previous instance state
393 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
394 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
395 getView().restoreHierarchyState(container);
396 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700397 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700398 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700399
400 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700401 }
402
403 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700404 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700405 mModel.update(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700406 }
407 };
408
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700409 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700410 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700411
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700412 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700413 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700414 }
415
Jeff Sharkey42d26792013-09-06 13:22:09 -0700416 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700417 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700418 // There's only one request code right now. Replace this with a switch statement or
419 // something more scalable when more codes are added.
420 if (requestCode != REQUEST_COPY_DESTINATION) {
421 return;
422 }
423 if (resultCode == Activity.RESULT_CANCELED || data == null) {
424 // User pressed the back button or otherwise cancelled the destination pick. Don't
425 // proceed with the copy.
426 return;
427 }
428
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900429 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700430 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
431 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700432 }
433
Steve McKayef280152015-06-11 10:10:49 -0700434 private int getEventAdapterPosition(MotionEvent e) {
435 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
436 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
437 }
438
439 private boolean onSingleTapUp(MotionEvent e) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700440 if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700441 int position = getEventAdapterPosition(e);
442 if (position != RecyclerView.NO_POSITION) {
443 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700444 }
445 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700446 return false;
447 }
Steve McKayef280152015-06-11 10:10:49 -0700448
Steve McKay93d8ef42015-07-30 12:27:44 -0700449 protected boolean onDoubleTap(MotionEvent e) {
450 if (Events.isMouseEvent(e)) {
451 Log.d(TAG, "Handling double tap from mouse.");
452 int position = getEventAdapterPosition(e);
453 if (position != RecyclerView.NO_POSITION) {
454 return handleViewItem(position);
455 }
456 }
457 return false;
458 }
459
460 private boolean handleViewItem(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700461 final Cursor cursor = mModel.getItem(position);
Steve McKay93d8ef42015-07-30 12:27:44 -0700462 checkNotNull(cursor, "Cursor cannot be null.");
463 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
464 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
465 if (isDocumentEnabled(docMimeType, docFlags)) {
466 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwa24be5d32015-08-27 16:04:46 -0700467 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
468 mModel.clearSelection();
Steve McKay93d8ef42015-07-30 12:27:44 -0700469 return true;
470 }
Steve McKayef280152015-06-11 10:10:49 -0700471 return false;
472 }
473
Ben Kwaf5858932015-04-07 15:43:39 -0700474 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700475 public void onStop() {
476 super.onStop();
477
478 // Remember last scroll location
479 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
480 getView().saveHierarchyState(container);
481 final State state = getDisplayState(this);
482 state.dirState.put(mStateKey, container);
483 }
484
485 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700486 public void onResume() {
487 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700488 updateDisplayState();
489 }
490
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700491 public void onDisplayStateChanged() {
492 updateDisplayState();
493 }
494
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700495 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700496 // Sort order change always triggers reload; we'll trigger state change
497 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700498 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700499 }
500
501 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700502 final ContentResolver resolver = getActivity().getContentResolver();
503 final State state = getDisplayState(this);
504
505 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
506 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
507
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700508 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700509 final Uri stateUri = RecentsProvider.buildState(
510 root.authority, root.rootId, doc.documentId);
511 final ContentValues values = new ContentValues();
512 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700513
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700514 new AsyncTask<Void, Void, Void>() {
515 @Override
516 protected Void doInBackground(Void... params) {
517 resolver.insert(stateUri, values);
518 return null;
519 }
520 }.execute();
521 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700522
523 // Mode change is just visual change; no need to kick loader, and
524 // deliver change event immediately.
525 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700526 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700527
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700528 updateDisplayState();
529 }
530
531 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700532 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700533
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700534 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700535 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700536 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700537
Steve McKayef280152015-06-11 10:10:49 -0700538 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700539
Steve McKayef280152015-06-11 10:10:49 -0700540 mRecView.setAdapter(mAdapter);
541 }
542
543 /**
544 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
545 * classes as needed.
546 */
547 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700548 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700549
550 final LayoutManager layout;
551 switch (mode) {
552 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700553 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700554 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700555 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700556 }
Steve McKayef280152015-06-11 10:10:49 -0700557 layout = mGridLayout;
558 break;
559 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700560 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700561 if (mListLayout == null) {
562 mListLayout = new LinearLayoutManager(getContext());
563 }
Steve McKayef280152015-06-11 10:10:49 -0700564 layout = mListLayout;
565 break;
566 case MODE_UNKNOWN:
567 default:
568 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700569 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700570
Steve McKayef280152015-06-11 10:10:49 -0700571 mRecView.setLayoutManager(layout);
Kyle Horimoto2da6e4a2015-08-27 16:44:00 -0700572 // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
573 // imperatively calling this function.
Steve McKay9058e042015-09-01 12:31:24 -0700574 mModel.mSelectionManager.handleLayoutChanged();
Steve McKayef280152015-06-11 10:10:49 -0700575 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700576 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700577 }
578
Steve McKayfefcd702015-08-20 16:19:38 +0000579 private int calculateColumnCount() {
580 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
581 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700582 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000583
Steve McKayd57f5fa2015-07-23 16:33:41 -0700584 checkState(mRecView.getWidth() > 0);
585 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000586 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
587
Steve McKayd57f5fa2015-07-23 16:33:41 -0700588 return columnCount;
589 }
590
Steve McKayef280152015-06-11 10:10:49 -0700591 /**
592 * Manages the integration between our ActionMode and MultiSelectManager, initiating
593 * ActionMode when there is a selection, canceling it when there is no selection,
594 * and clearing selection when action mode is explicitly exited by the user.
595 */
596 private final class SelectionModeListener
597 implements MultiSelectManager.Callback, ActionMode.Callback {
598
599 private Selection mSelected = new Selection();
600 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700601 private int mNoDeleteCount = 0;
602 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700603
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700604 @Override
Steve McKayef280152015-06-11 10:10:49 -0700605 public boolean onBeforeItemStateChange(int position, boolean selected) {
Ben Kwac42fa402015-09-16 08:04:37 -0700606 // Directories cannot be checked
Steve McKayef280152015-06-11 10:10:49 -0700607 if (selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700608 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700609 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700610 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
611 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700612 return isDocumentEnabled(docMimeType, docFlags);
613 }
614 return true;
615 }
616
617 @Override
618 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700619
Ben Kwa24be5d32015-08-27 16:04:46 -0700620 final Cursor cursor = mModel.getItem(position);
Steve McKay4f4232d2015-07-22 12:13:46 -0700621 checkNotNull(cursor, "Cursor cannot be null.");
622
623 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
624 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
625 mNoDeleteCount += selected ? 1 : -1;
626 }
Steve McKay57394872015-08-12 14:48:34 -0700627 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700628
Steve McKay57394872015-08-12 14:48:34 -0700629 @Override
630 public void onSelectionChanged() {
Ben Kwa24be5d32015-08-27 16:04:46 -0700631 mModel.getSelection(mSelected);
Ben Kwafe18c1b2015-09-11 15:40:18 -0700632 TypedValue color = new TypedValue();
Steve McKay4f4232d2015-07-22 12:13:46 -0700633 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700634 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
635 if (mActionMode == null) {
636 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
637 mActionMode = getActivity().startActionMode(this);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700638 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700639 getActivity().getTheme().resolveAttribute(
640 R.attr.colorActionMode, color, true);
Steve McKay4f4232d2015-07-22 12:13:46 -0700641 updateActionMenu();
642 } else {
643 if (DEBUG) Log.d(TAG, "Finishing action mode.");
644 if (mActionMode != null) {
645 mActionMode.finish();
646 }
Ben Kwa0574b182015-09-08 07:31:19 -0700647 getActivity().getTheme().resolveAttribute(
648 android.R.attr.colorPrimaryDark, color, true);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700649 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700650 getActivity().getWindow().setStatusBarColor(color.data);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700651
Steve McKayef280152015-06-11 10:10:49 -0700652 if (mActionMode != null) {
653 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
654 }
655 }
656
657 // Called when the user exits the action mode
658 @Override
659 public void onDestroyActionMode(ActionMode mode) {
660 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
661 mActionMode = null;
662 // clear selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700663 mModel.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700664 mSelected.clear();
665 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700666 }
667
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700668 @Override
669 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
670 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa24be5d32015-08-27 16:04:46 -0700671 mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
672 return mModel.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700673 }
674
675 @Override
676 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700677 mMenu = menu;
678 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700679 return true;
680 }
681
Steve McKay4f4232d2015-07-22 12:13:46 -0700682 private void updateActionMenu() {
683 checkNotNull(mMenu);
684 // Delegate update logic to our owning action, since specialized logic is desired.
685 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
686 }
687
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700688 @Override
Steve McKayef280152015-06-11 10:10:49 -0700689 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700690
Ben Kwa24be5d32015-08-27 16:04:46 -0700691 Selection selection = mModel.getSelection(new Selection());
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700692
Jeff Sharkey873daa32013-08-18 17:38:20 -0700693 final int id = item.getItemId();
694 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700695 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700696 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700697 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700698
699 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700700 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700701 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700702 return true;
703
704 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700705 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700706 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700707 return true;
708
Steve McKay1f199482015-05-20 15:58:42 -0700709 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700710 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700711 mode.finish();
712 return true;
713
Steve McKay1f199482015-05-20 15:58:42 -0700714 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700715 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700716 mode.finish();
717 return true;
718
Steve McKay1f199482015-05-20 15:58:42 -0700719 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700720 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700721 mode.finish();
722 return true;
723
Ben Kwa512a6ba2015-03-31 08:15:21 -0700724 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700725 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700726 return true;
727
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700728 } else {
729 return false;
730 }
731 }
Steve McKayef280152015-06-11 10:10:49 -0700732 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700733
Steve McKayef280152015-06-11 10:10:49 -0700734 private static void cancelThumbnailTask(View view) {
735 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
736 if (iconThumb != null) {
737 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
738 if (oldTask != null) {
739 oldTask.preempt();
740 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700741 }
742 }
Steve McKayef280152015-06-11 10:10:49 -0700743 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700744
Steve McKayef280152015-06-11 10:10:49 -0700745 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700746 new GetDocumentsTask() {
747 @Override
748 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700749 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700750 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700751 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700752 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700753 }
754
Steve McKayef280152015-06-11 10:10:49 -0700755 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700756 new GetDocumentsTask() {
757 @Override
758 void onDocumentsReady(List<DocumentInfo> docs) {
759 Intent intent;
760
761 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000762 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700763 for (DocumentInfo doc: docs) {
764 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
765 docsForSend.add(doc);
766 }
767 }
768
769 if (docsForSend.size() == 1) {
770 final DocumentInfo doc = docsForSend.get(0);
771
772 intent = new Intent(Intent.ACTION_SEND);
773 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
774 intent.addCategory(Intent.CATEGORY_DEFAULT);
775 intent.setType(doc.mimeType);
776 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
777
778 } else if (docsForSend.size() > 1) {
779 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
780 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
781 intent.addCategory(Intent.CATEGORY_DEFAULT);
782
Steve McKayfefcd702015-08-20 16:19:38 +0000783 final ArrayList<String> mimeTypes = new ArrayList<>();
784 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700785 for (DocumentInfo doc : docsForSend) {
786 mimeTypes.add(doc.mimeType);
787 uris.add(doc.derivedUri);
788 }
789
790 intent.setType(findCommonMimeType(mimeTypes));
791 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
792
793 } else {
794 return;
795 }
796
797 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
798 startActivity(intent);
799 }
800 }.execute(selected);
801 }
802
Steve McKayef280152015-06-11 10:10:49 -0700803 private void deleteDocuments(final Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -0700804 Context context = getActivity();
805 ContentResolver resolver = context.getContentResolver();
806 String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700807
Ben Kwa91923182015-08-27 16:06:33 -0700808 mModel.markForDeletion(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700809
Ben Kwa91923182015-08-27 16:06:33 -0700810 Activity activity = getActivity();
811 Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG)
812 .setAction(
813 R.string.undo,
814 new android.view.View.OnClickListener() {
815 @Override
816 public void onClick(View view) {}
817 })
818 .setCallback(
819 new Snackbar.Callback() {
820 @Override
821 public void onDismissed(Snackbar snackbar, int event) {
822 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
823 mModel.undoDeletion();
824 } else {
Ben Kwa83cedf22015-09-11 15:15:45 -0700825 mModel.finalizeDeletion(
826 new Runnable() {
827 @Override
828 public void run() {
829 Snackbar.make(
830 DirectoryFragment.this.getView(),
831 R.string.toast_failed_delete,
832 Snackbar.LENGTH_LONG)
833 .show();
834
835 }
836 });
Ben Kwa91923182015-08-27 16:06:33 -0700837 }
Ben Kwa91923182015-08-27 16:06:33 -0700838 }
839 })
840 .show();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700841 }
842
Steve McKayef280152015-06-11 10:10:49 -0700843 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700844 // Pop up a dialog to pick a destination. This is inadequate but works for now.
845 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900846 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900847 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900848 Uri.EMPTY,
849 getActivity(),
850 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700851
852 new GetDocumentsTask() {
853 @Override
854 void onDocumentsReady(List<DocumentInfo> docs) {
855 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
856
857 boolean directoryCopy = false;
858 for (DocumentInfo info : docs) {
859 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
860 directoryCopy = true;
861 break;
862 }
863 }
864 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
865 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
866 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900867 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700868 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700869 }
870
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700871 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700872 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700873 }
874
Steve McKayef280152015-06-11 10:10:49 -0700875 // Provide a reference to the views for each data item
876 // Complex data items may need more than one view per item, and
877 // you provide access to all the views for a data item in a view holder
878 private static final class DocumentHolder extends RecyclerView.ViewHolder {
879 // each data item is just a string in this case
880 public View view;
881 public String docId; // The stable document id.
882 public DocumentHolder(View view) {
883 super(view);
884 this.view = view;
885 }
886 }
887
Ben Kwa2f975262015-09-16 13:15:38 -0700888 void showEmptyView() {
889 mEmptyView.setVisibility(View.VISIBLE);
890 mRecView.setVisibility(View.GONE);
891 TextView msg = (TextView) mEmptyView.findViewById(R.id.message);
892 msg.setText(R.string.empty);
893 // No retry button for the empty view.
894 mEmptyView.findViewById(R.id.button_retry).setVisibility(View.GONE);
895 }
896
897 void showErrorView() {
898 mEmptyView.setVisibility(View.VISIBLE);
899 mRecView.setVisibility(View.GONE);
900 TextView msg = (TextView) mEmptyView.findViewById(R.id.message);
901 msg.setText(R.string.query_error);
902 // TODO: Enable this once the retry button does something.
903 mEmptyView.findViewById(R.id.button_retry).setVisibility(View.GONE);
904 }
905
906 void showRecyclerView() {
907 mEmptyView.setVisibility(View.GONE);
908 mRecView.setVisibility(View.VISIBLE);
909 }
910
Ben Kwa379e1762015-09-21 10:49:52 -0700911 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
Steve McKayef280152015-06-11 10:10:49 -0700912
913 private final Context mContext;
914 private final LayoutInflater mInflater;
Steve McKayef280152015-06-11 10:10:49 -0700915
Steve McKayef280152015-06-11 10:10:49 -0700916 public DocumentsAdapter(Context context) {
917 mContext = context;
918 mInflater = LayoutInflater.from(context);
919 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700920
Ben Kwac42fa402015-09-16 08:04:37 -0700921 @Override
Steve McKayef280152015-06-11 10:10:49 -0700922 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
923 final State state = getDisplayState(DirectoryFragment.this);
924 final LayoutInflater inflater = LayoutInflater.from(getContext());
925 switch (state.derivedMode) {
926 case MODE_GRID:
927 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
928 case MODE_LIST:
929 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
930 case MODE_UNKNOWN:
931 default:
932 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -0700933 }
934 }
935
Steve McKayef280152015-06-11 10:10:49 -0700936 @Override
937 public void onBindViewHolder(DocumentHolder holder, int position) {
938
939 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700940 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700941 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700942 final RootsCache roots = DocumentsApplication.getRootsCache(context);
943 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
944 context, mThumbSize);
945
Ben Kwa24be5d32015-08-27 16:04:46 -0700946 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700947 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700948
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700949 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
950 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700951 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
952 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
953 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
954 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
955 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
956 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
957 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
958 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700959
Steve McKayef280152015-06-11 10:10:49 -0700960 holder.docId = docId;
961 final View itemView = holder.view;
Ben Kwa24be5d32015-08-27 16:04:46 -0700962 itemView.setActivated(mModel.isSelected(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -0700963
Steve McKayef280152015-06-11 10:10:49 -0700964 final View line1 = itemView.findViewById(R.id.line1);
965 final View line2 = itemView.findViewById(R.id.line2);
966
967 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
968 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
969 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
970 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
971 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
972 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
973 final TextView date = (TextView) itemView.findViewById(R.id.date);
974 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700975
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700976 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700977 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700978 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700979 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700980 }
981
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700982 iconMime.animate().cancel();
983 iconThumb.animate().cancel();
984
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700985 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700986 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700987 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700988 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700989
Jeff Sharkey7e544612014-08-29 15:38:27 -0700990 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
991 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
992
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700993 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700994 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700995 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700996 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700997 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700998 iconThumb.setImageBitmap(cachedResult);
999 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001000 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001001 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001002 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001003 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001004 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001005 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001006 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001007 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001008 }
1009
1010 // Always throw MIME icon into place, even when a thumbnail is being
1011 // loaded in background.
1012 if (cacheHit) {
1013 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001014 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001015 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001016 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001017 iconMime.setAlpha(1f);
1018 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001019 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001020 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001021 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001022 }
1023
Jeff Sharkey9656a532013-09-13 13:42:19 -07001024 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001025 boolean hasLine2 = false;
1026
Jeff Sharkey9656a532013-09-13 13:42:19 -07001027 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1028 if (!hideTitle) {
1029 title.setText(docDisplayName);
1030 hasLine1 = true;
1031 }
1032
1033 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001034 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001035 // We've already had to enumerate roots before any results can
1036 // be shown, so this will never block.
1037 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001038 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001039 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001040 } else {
Steve McKayef280152015-06-11 10:10:49 -07001041 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001042 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001043
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001044 if (summary != null) {
1045 final boolean alwaysShowSummary = getResources()
1046 .getBoolean(R.bool.always_show_summary);
1047 if (alwaysShowSummary) {
1048 summary.setText(root.getDirectoryString());
1049 summary.setVisibility(View.VISIBLE);
1050 hasLine2 = true;
1051 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001052 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001053 // No summary needed if icon speaks for itself
1054 summary.setVisibility(View.INVISIBLE);
1055 } else {
1056 summary.setText(root.getDirectoryString());
1057 summary.setVisibility(View.VISIBLE);
1058 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1059 hasLine2 = true;
1060 }
1061 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001062 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001063 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001064 // Directories showing thumbnails in grid mode get a little icon
1065 // hint to remind user they're a directory.
1066 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1067 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001068 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001069 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001070 }
1071
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001072 if (summary != null) {
1073 if (docSummary != null) {
1074 summary.setText(docSummary);
1075 summary.setVisibility(View.VISIBLE);
1076 hasLine2 = true;
1077 } else {
1078 summary.setVisibility(View.INVISIBLE);
1079 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001080 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001081 }
1082
Jeff Sharkey9656a532013-09-13 13:42:19 -07001083 if (icon1 != null) icon1.setVisibility(View.GONE);
1084 if (icon2 != null) icon2.setVisibility(View.GONE);
1085
1086 if (iconDrawable != null) {
1087 if (hasLine1) {
1088 icon1.setVisibility(View.VISIBLE);
1089 icon1.setImageDrawable(iconDrawable);
1090 } else {
1091 icon2.setVisibility(View.VISIBLE);
1092 icon2.setImageDrawable(iconDrawable);
1093 }
1094 }
1095
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001096 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001097 date.setText(null);
1098 } else {
Steve McKayef280152015-06-11 10:10:49 -07001099 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001100 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001101 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001102
1103 if (state.showSize) {
1104 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001105 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001106 size.setText(null);
1107 } else {
Steve McKayef280152015-06-11 10:10:49 -07001108 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001109 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001110 }
1111 } else {
1112 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001113 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001114
Jeff Sharkey9656a532013-09-13 13:42:19 -07001115 if (line1 != null) {
1116 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1117 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001118 if (line2 != null) {
1119 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1120 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001121
Steve McKayef280152015-06-11 10:10:49 -07001122 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001123
1124 iconMime.setAlpha(iconAlpha);
1125 iconThumb.setAlpha(iconAlpha);
1126 if (icon1 != null) icon1.setAlpha(iconAlpha);
1127 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001128
Steve McKay8e258c62015-05-06 14:27:57 -07001129 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001130 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001131 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001132 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001133
Steve McKay351a7492015-08-04 10:11:01 -07001134 @Override
Steve McKayef280152015-06-11 10:10:49 -07001135 public int getItemCount() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001136 return mModel.getItemCount();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001137 }
1138
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001139 }
1140
1141 private static String formatTime(Context context, long when) {
1142 // TODO: DateUtils should make this easier
1143 Time then = new Time();
1144 then.set(when);
1145 Time now = new Time();
1146 now.setToNow();
1147
1148 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1149 | DateUtils.FORMAT_ABBREV_ALL;
1150
1151 if (then.year != now.year) {
1152 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1153 } else if (then.yearDay != now.yearDay) {
1154 flags |= DateUtils.FORMAT_SHOW_DATE;
1155 } else {
1156 flags |= DateUtils.FORMAT_SHOW_TIME;
1157 }
1158
1159 return DateUtils.formatDateTime(context, when, flags);
1160 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001161
1162 private String findCommonMimeType(List<String> mimeTypes) {
1163 String[] commonType = mimeTypes.get(0).split("/");
1164 if (commonType.length != 2) {
1165 return "*/*";
1166 }
1167
1168 for (int i = 1; i < mimeTypes.size(); i++) {
1169 String[] type = mimeTypes.get(i).split("/");
1170 if (type.length != 2) continue;
1171
1172 if (!commonType[1].equals(type[1])) {
1173 commonType[1] = "*";
1174 }
1175
1176 if (!commonType[0].equals(type[0])) {
1177 commonType[0] = "*";
1178 commonType[1] = "*";
1179 break;
1180 }
1181 }
1182
1183 return commonType[0] + "/" + commonType[1];
1184 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001185
1186 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001187 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001188 if (v.isEnabled() == enabled) return;
1189 v.setEnabled(enabled);
1190
1191 if (v instanceof ViewGroup) {
1192 final ViewGroup vg = (ViewGroup) v;
1193 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1194 setEnabledRecursive(vg.getChildAt(i), enabled);
1195 }
1196 }
1197 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001198
1199 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1200 final State state = getDisplayState(DirectoryFragment.this);
1201
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001202 // Directories are always enabled
1203 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1204 return true;
1205 }
1206
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001207 // Read-only files are disabled when creating
1208 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1209 return false;
1210 }
1211
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001212 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1213 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001214
Steve McKay1f199482015-05-20 15:58:42 -07001215 private void copyFromClipboard() {
1216 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1217
1218 @Override
1219 protected List<DocumentInfo> doInBackground(Void... params) {
1220 return mClipper.getClippedDocuments();
1221 }
1222
1223 @Override
1224 protected void onPostExecute(List<DocumentInfo> docs) {
1225 DocumentInfo destination =
1226 ((BaseActivity) getActivity()).getCurrentDirectory();
1227 copyDocuments(docs, destination);
1228 }
1229 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001230 }
1231
Steve McKay1f199482015-05-20 15:58:42 -07001232 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001233 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001234 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001235
Steve McKay1f199482015-05-20 15:58:42 -07001236 @Override
1237 protected List<DocumentInfo> doInBackground(Void... params) {
1238 return mClipper.getDocumentsFromClipData(clipData);
1239 }
1240
1241 @Override
1242 protected void onPostExecute(List<DocumentInfo> docs) {
1243 copyDocuments(docs, destination);
1244 }
1245 }.execute();
1246 }
1247
1248 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1249 if (!canCopy(docs, destination)) {
1250 Toast.makeText(
1251 getActivity(),
1252 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001253 return;
1254 }
1255
Steve McKay1f199482015-05-20 15:58:42 -07001256 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001257 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001258 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001259
Steve McKay1f199482015-05-20 15:58:42 -07001260 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001261 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001262 if (destination != null) {
1263 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001264 tmpStack.addAll(curStack);
1265 } else {
1266 tmpStack = curStack;
1267 }
1268
Steve McKay1f199482015-05-20 15:58:42 -07001269 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001270 }
1271
1272 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1273 Context context = getActivity();
1274 final ContentResolver resolver = context.getContentResolver();
1275 ClipData clipData = null;
1276 for (DocumentInfo doc : docs) {
1277 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1278 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001279 // TODO: figure out what this string should be.
1280 // Currently it is not displayed anywhere in the UI, but this might change.
1281 final String label = "";
1282 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001283 } else {
1284 // TODO: update list of mime types in ClipData.
1285 clipData.addItem(new ClipData.Item(uri));
1286 }
1287 }
1288 return clipData;
1289 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001290
Steve McKay1f199482015-05-20 15:58:42 -07001291 void copySelectedToClipboard() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001292 Selection sel = mModel.getSelection(new Selection());
Steve McKayef280152015-06-11 10:10:49 -07001293 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001294 }
Steve McKay0599a442015-05-05 14:50:00 -07001295
Steve McKayef280152015-06-11 10:10:49 -07001296 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001297 new GetDocumentsTask() {
1298 @Override
1299 void onDocumentsReady(List<DocumentInfo> docs) {
1300 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001301 Activity activity = getActivity();
1302 Toast.makeText(activity,
1303 activity.getResources().getQuantityString(
1304 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1305 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001306 }
Steve McKayef280152015-06-11 10:10:49 -07001307 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001308 }
1309
1310 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001311 copyFromClipboard();
1312 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001313 }
1314
Steve McKay0599a442015-05-05 14:50:00 -07001315 /**
1316 * Returns true if the list of files can be copied to destination. Note that this
1317 * is a policy check only. Currently the method does not attempt to verify
1318 * available space or any other environmental aspects possibly resulting in
1319 * failure to copy.
1320 *
1321 * @return true if the list of files can be copied to destination.
1322 */
1323 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
Ben Kwa91923182015-08-27 16:06:33 -07001324 BaseActivity activity = (BaseActivity) getActivity();
Steve McKay0599a442015-05-05 14:50:00 -07001325
1326 final RootInfo root = activity.getCurrentRoot();
1327
1328 // Can't copy folders to Downloads.
1329 if (root.isDownloads()) {
1330 for (DocumentInfo docs : files) {
1331 if (docs.isDirectory()) {
1332 return false;
1333 }
1334 }
1335 }
1336
1337 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1338 }
1339
1340 void selectAllFiles() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001341 boolean changed = mModel.selectAll();
Steve McKay9459a7c2015-07-24 13:14:20 -07001342 if (changed) {
1343 updateDisplayState();
1344 }
Steve McKay0599a442015-05-05 14:50:00 -07001345 }
1346
Steve McKayef280152015-06-11 10:10:49 -07001347 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001348 // Listen for drops on non-directory items and empty space.
1349 view.setOnDragListener(mOnDragListener);
1350 }
1351
1352 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1353 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1354 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1355 // Make a directory item a drop target. Drop on non-directories and empty space
1356 // is handled at the list/grid view level.
1357 view.setOnDragListener(mOnDragListener);
1358 }
1359
1360 // Temporary: attaching the listener to the title only.
1361 // Attaching to the entire item conflicts with the item long click handler responsible
1362 // for item selection.
1363 final View title = view.findViewById(android.R.id.title);
1364 title.setOnLongClickListener(mLongClickListener);
1365 }
1366
1367 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1368 @Override
1369 public boolean onDrag(View v, DragEvent event) {
1370 switch (event.getAction()) {
1371 case DragEvent.ACTION_DRAG_STARTED:
1372 // TODO: Check if the event contains droppable data.
1373 return true;
1374
1375 // TODO: Highlight potential drop target directory?
1376 // TODO: Expand drop target directory on hover?
1377 case DragEvent.ACTION_DRAG_ENTERED:
1378 case DragEvent.ACTION_DRAG_LOCATION:
1379 case DragEvent.ACTION_DRAG_EXITED:
1380 case DragEvent.ACTION_DRAG_ENDED:
1381 return true;
1382
1383 case DragEvent.ACTION_DROP:
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001384 int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001385 DocumentInfo dstDir = null;
1386 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001387 Cursor dstCursor = mModel.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001388 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001389 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1390 // TODO: Do not drop into the directory where the documents came from.
1391 }
1392 copyFromClipData(event.getClipData(), dstDir);
1393 return true;
1394 }
1395 return false;
1396 }
1397 };
1398
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001399 private View getContainingItemView(View view) {
1400 while (true) {
1401 if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
1402 return view;
1403 }
1404 ViewParent parent = view.getParent();
1405 if (parent == null || !(parent instanceof View)) {
1406 return null;
1407 }
1408 view = (View) parent;
1409 }
1410 }
1411
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001412 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1413 @Override
1414 public boolean onLongClick(View v) {
1415 final List<DocumentInfo> docs = getDraggableDocuments(v);
1416 if (docs.isEmpty()) {
1417 return false;
1418 }
1419 v.startDrag(
1420 getClipDataFromDocuments(docs),
1421 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1422 null,
Vladislav Kaznacheeve3ce8a92015-07-15 18:04:04 -07001423 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
1424 View.DRAG_FLAG_GLOBAL_URI_WRITE
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001425 );
1426 return true;
1427 }
1428 };
1429
1430 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001431 int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001432 if (position == android.widget.AdapterView.INVALID_POSITION) {
1433 return Collections.EMPTY_LIST;
1434 }
1435
Ben Kwa24be5d32015-08-27 16:04:46 -07001436 final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001437 if (!selectedDocs.isEmpty()) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001438 if (!mModel.isSelected(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001439 // There is a selection that does not include the current item, drag nothing.
1440 return Collections.EMPTY_LIST;
1441 }
1442 return selectedDocs;
1443 }
1444
Ben Kwa24be5d32015-08-27 16:04:46 -07001445 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001446 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001447 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001448
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001449 return Lists.newArrayList(doc);
1450 }
1451
1452 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1453 if (docs.size() == 1) {
1454 final DocumentInfo doc = docs.get(0);
1455 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1456 doc.mimeType, doc.icon, getDisplayState(this));
1457 }
1458 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1459 }
1460
1461 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1462 String docMimeType, int docIcon, State state) {
1463 if (docIcon != 0) {
1464 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1465 } else {
1466 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1467 state.derivedMode);
1468 }
1469 }
1470
Steve McKayef280152015-06-11 10:10:49 -07001471 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1472 implements Preemptable {
1473 private final Uri mUri;
1474 private final ImageView mIconMime;
1475 private final ImageView mIconThumb;
1476 private final Point mThumbSize;
1477 private final float mTargetAlpha;
1478 private final CancellationSignal mSignal;
1479
1480 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1481 float targetAlpha) {
1482 mUri = uri;
1483 mIconMime = iconMime;
1484 mIconThumb = iconThumb;
1485 mThumbSize = thumbSize;
1486 mTargetAlpha = targetAlpha;
1487 mSignal = new CancellationSignal();
1488 }
1489
1490 @Override
1491 public void preempt() {
1492 cancel(false);
1493 mSignal.cancel();
1494 }
1495
1496 @Override
1497 protected Bitmap doInBackground(Uri... params) {
1498 if (isCancelled()) return null;
1499
1500 final Context context = mIconThumb.getContext();
1501 final ContentResolver resolver = context.getContentResolver();
1502
1503 ContentProviderClient client = null;
1504 Bitmap result = null;
1505 try {
1506 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1507 resolver, mUri.getAuthority());
1508 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1509 if (result != null) {
1510 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1511 context, mThumbSize);
1512 thumbs.put(mUri, result);
1513 }
1514 } catch (Exception e) {
1515 if (!(e instanceof OperationCanceledException)) {
1516 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1517 }
1518 } finally {
1519 ContentProviderClient.releaseQuietly(client);
1520 }
1521 return result;
1522 }
1523
1524 @Override
1525 protected void onPostExecute(Bitmap result) {
1526 if (mIconThumb.getTag() == this && result != null) {
1527 mIconThumb.setTag(null);
1528 mIconThumb.setImageBitmap(result);
1529
1530 mIconMime.setAlpha(mTargetAlpha);
1531 mIconMime.animate().alpha(0f).start();
1532 mIconThumb.setAlpha(0f);
1533 mIconThumb.animate().alpha(mTargetAlpha).start();
1534 }
1535 }
1536 }
1537
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001538 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1539
1540 private final Drawable mShadow;
1541
1542 private final int mShadowDimension;
1543
1544 public DrawableShadowBuilder(Drawable shadow) {
1545 mShadow = shadow;
1546 mShadowDimension = getResources().getDimensionPixelSize(
1547 R.dimen.drag_shadow_size);
1548 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1549 }
1550
Ben Kwa24be5d32015-08-27 16:04:46 -07001551 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001552 public void onProvideShadowMetrics(
1553 Point shadowSize, Point shadowTouchPoint) {
1554 shadowSize.set(mShadowDimension, mShadowDimension);
1555 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1556 }
1557
Ben Kwa24be5d32015-08-27 16:04:46 -07001558 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001559 public void onDrawShadow(Canvas canvas) {
1560 mShadow.draw(canvas);
1561 }
1562 }
Steve McKay1f199482015-05-20 15:58:42 -07001563
1564 private FragmentTuner pickFragmentTuner(final State state) {
1565 return state.action == ACTION_BROWSE_ALL
Steve McKay0fbfc652015-08-20 16:48:49 -07001566 ? new FilesTuner()
Steve McKay1f199482015-05-20 15:58:42 -07001567 : new DefaultTuner(state);
1568 }
1569
1570 /**
1571 * Interface for specializing the Fragment for the "host" Activity.
1572 * Feel free to expand the role of this class to handle other specializations.
1573 */
1574 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001575 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001576 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001577 }
1578
1579 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001580 * Abstract task providing support for loading documents *off*
1581 * the main thread. And if it isn't obvious, creating a list
1582 * of documents (especially large lists) can be pretty expensive.
1583 */
1584 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001585 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001586 @Override
Steve McKayef280152015-06-11 10:10:49 -07001587 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001588 return mModel.getDocuments(selected[0]);
Steve McKay9276f3b2015-05-27 16:11:42 -07001589 }
1590
1591 @Override
1592 protected final void onPostExecute(List<DocumentInfo> docs) {
1593 onDocumentsReady(docs);
1594 }
1595
1596 abstract void onDocumentsReady(List<DocumentInfo> docs);
1597 }
1598
1599 /**
Steve McKay1f199482015-05-20 15:58:42 -07001600 * Provides support for Platform specific specializations of DirectoryFragment.
1601 */
1602 private static final class DefaultTuner implements FragmentTuner {
1603
1604 private final State mState;
1605
1606 public DefaultTuner(State state) {
1607 mState = state;
1608 }
1609
1610 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001611 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001612 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1613
1614 final MenuItem open = menu.findItem(R.id.menu_open);
1615 final MenuItem share = menu.findItem(R.id.menu_share);
1616 final MenuItem delete = menu.findItem(R.id.menu_delete);
1617 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1618 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1619 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1620
1621 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1622 || mState.action == ACTION_BROWSE);
1623
1624 open.setVisible(!manageOrBrowse);
1625 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001626 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001627 // Disable copying from the Recents view.
1628 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1629 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1630
Steve McKay0fbfc652015-08-20 16:48:49 -07001631 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001632 copyToClipboard.setVisible(false);
1633 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001634
1635 @Override
1636 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001637 }
1638
1639 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001640 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001641 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001642 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001643 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001644 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001645 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001646 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001647 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1648
1649 menu.findItem(R.id.menu_open).setVisible(false);
1650 menu.findItem(R.id.menu_copy_to).setVisible(false);
1651 menu.findItem(R.id.menu_move_to).setVisible(false);
1652 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001653
1654 @Override
Kyle Horimoto62a7fd02015-08-18 13:25:29 -07001655 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001656 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001657
1658 /**
1659 * The data model for the current loaded directory.
1660 */
Ben Kwa7461a952015-09-01 11:03:01 -07001661 @VisibleForTesting
1662 public static final class Model implements DocumentContext {
Ben Kwa24be5d32015-08-27 16:04:46 -07001663 private MultiSelectManager mSelectionManager;
Ben Kwa379e1762015-09-21 10:49:52 -07001664 private RecyclerView.Adapter<?> mViewAdapter;
Ben Kwa7461a952015-09-01 11:03:01 -07001665 private Context mContext;
Ben Kwa24be5d32015-08-27 16:04:46 -07001666 private int mCursorCount;
1667 private boolean mIsLoading;
Ben Kwa7461a952015-09-01 11:03:01 -07001668 private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
1669 private UpdateListener mUpdateListener;
Ben Kwa24be5d32015-08-27 16:04:46 -07001670 @Nullable private Cursor mCursor;
1671 @Nullable private String info;
1672 @Nullable private String error;
Ben Kwa7461a952015-09-01 11:03:01 -07001673
Ben Kwa379e1762015-09-21 10:49:52 -07001674 Model(Context context, MultiSelectManager selectionManager,
1675 RecyclerView.Adapter<?> viewAdapter) {
Ben Kwa7461a952015-09-01 11:03:01 -07001676 mContext = context;
1677 mSelectionManager = selectionManager;
Ben Kwa379e1762015-09-21 10:49:52 -07001678 mViewAdapter = viewAdapter;
Ben Kwa24be5d32015-08-27 16:04:46 -07001679 }
1680
1681 /**
1682 * Selects all files in the current directory.
1683 * @return true if the selection state changed for any files.
1684 */
1685 boolean selectAll() {
1686 return mSelectionManager.setItemsSelected(0, mCursorCount, true);
1687 }
1688
1689 /**
1690 * Clones the current selection into the given Selection object.
1691 * @param selection
1692 * @return The selection that was passed in, for convenience.
1693 */
1694 Selection getSelection(Selection selection) {
1695 return mSelectionManager.getSelection(selection);
1696 }
1697
1698 /**
1699 * @return The current selection (the live instance, not a copy).
1700 */
1701 Selection getSelection() {
1702 return mSelectionManager.getSelection();
1703 }
1704
1705 boolean isSelected(int position) {
1706 return mSelectionManager.getSelection().contains(position);
1707 }
1708
1709 void clearSelection() {
1710 mSelectionManager.clearSelection();
1711 }
1712
1713 void update(DirectoryResult result) {
1714 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
1715
1716 if (result == null) {
1717 mCursor = null;
1718 mCursorCount = 0;
1719 info = null;
1720 error = null;
1721 mIsLoading = false;
Ben Kwa83cedf22015-09-11 15:15:45 -07001722 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001723 return;
1724 }
1725
1726 if (result.exception != null) {
1727 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwa83cedf22015-09-11 15:15:45 -07001728 mUpdateListener.onModelUpdateFailed(result.exception);
Ben Kwa24be5d32015-08-27 16:04:46 -07001729 return;
1730 }
1731
1732 mCursor = result.cursor;
1733 mCursorCount = mCursor.getCount();
1734
1735 final Bundle extras = mCursor.getExtras();
1736 if (extras != null) {
1737 info = extras.getString(DocumentsContract.EXTRA_INFO);
1738 error = extras.getString(DocumentsContract.EXTRA_ERROR);
1739 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
1740 }
Ben Kwa7461a952015-09-01 11:03:01 -07001741
Ben Kwa83cedf22015-09-11 15:15:45 -07001742 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001743 }
1744
Ben Kwa7461a952015-09-01 11:03:01 -07001745 int getItemCount() {
Ben Kwa91923182015-08-27 16:06:33 -07001746 return mCursorCount - mMarkedForDeletion.size();
Ben Kwa24be5d32015-08-27 16:04:46 -07001747 }
1748
Ben Kwa7461a952015-09-01 11:03:01 -07001749 Cursor getItem(int position) {
Ben Kwa91923182015-08-27 16:06:33 -07001750 // Items marked for deletion are masked out of the UI. To do this, for every marked
1751 // item whose position is less than the requested item position, advance the requested
1752 // position by 1.
1753 final int originalPos = position;
1754 final int size = mMarkedForDeletion.size();
Ben Kwa0d0daff2015-09-09 13:49:07 -07001755 for (int i = 0; i < size; ++i) {
Ben Kwa91923182015-08-27 16:06:33 -07001756 // It'd be more concise, but less efficient, to iterate over positions while calling
1757 // mMarkedForDeletion.get. Instead, iterate over deleted entries.
1758 if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
1759 ++position;
1760 }
1761 }
1762
1763 if (DEBUG) {
1764 Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos
1765 + " Adjusted: " + position);
1766 }
1767
Ben Kwa24be5d32015-08-27 16:04:46 -07001768 if (position >= mCursorCount) {
1769 throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
1770 mCursorCount + " items");
1771 }
1772
1773 mCursor.moveToPosition(position);
1774 return mCursor;
1775 }
1776
1777 private boolean isEmpty() {
1778 return mCursorCount == 0;
1779 }
1780
1781 private boolean isLoading() {
1782 return mIsLoading;
1783 }
1784
1785 private List<DocumentInfo> getSelectedDocuments() {
1786 Selection sel = getSelection(new Selection());
1787 return getDocuments(sel);
1788 }
1789
Ben Kwa7461a952015-09-01 11:03:01 -07001790 List<DocumentInfo> getDocuments(Selection items) {
Ben Kwa91923182015-08-27 16:06:33 -07001791 final int size = (items != null) ? items.size() : 0;
Ben Kwa24be5d32015-08-27 16:04:46 -07001792
Ben Kwa91923182015-08-27 16:06:33 -07001793 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwa24be5d32015-08-27 16:04:46 -07001794 for (int i = 0; i < size; i++) {
1795 final Cursor cursor = getItem(items.get(i));
1796 checkNotNull(cursor, "Cursor cannot be null.");
1797 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1798 docs.add(doc);
1799 }
1800 return docs;
1801 }
1802
1803 @Override
1804 public Cursor getCursor() {
1805 if (Looper.myLooper() != Looper.getMainLooper()) {
1806 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1807 }
1808 return mCursor;
1809 }
Ben Kwa91923182015-08-27 16:06:33 -07001810
Ben Kwa7461a952015-09-01 11:03:01 -07001811 List<DocumentInfo> getDocumentsMarkedForDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001812 final int size = mMarkedForDeletion.size();
1813 List<DocumentInfo> docs = new ArrayList<>(size);
1814
1815 for (int i = 0; i < size; ++i) {
1816 final int position = mMarkedForDeletion.keyAt(i);
1817 checkState(position < mCursorCount);
1818 mCursor.moveToPosition(position);
1819 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
1820 docs.add(doc);
1821 }
1822 return docs;
1823 }
1824
1825 /**
1826 * Marks the given files for deletion. This will remove them from the UI. Clients must then
1827 * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
1828 * the deletion, respectively. Only one deletion operation is allowed at a time.
1829 *
1830 * @param selected A selection representing the files to delete.
1831 */
Ben Kwa7461a952015-09-01 11:03:01 -07001832 void markForDeletion(Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -07001833 // Only one deletion operation at a time.
1834 checkState(mMarkedForDeletion.size() == 0);
1835 // There should never be more to delete than what exists.
1836 checkState(mCursorCount >= selected.size());
1837
1838 final int size = selected.size();
1839 for (int i = 0; i < size; ++i) {
1840 int position = selected.get(i);
1841 if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
1842 mMarkedForDeletion.append(position, true);
Ben Kwa379e1762015-09-21 10:49:52 -07001843 mViewAdapter.notifyItemRemoved(position);
Ben Kwa91923182015-08-27 16:06:33 -07001844 }
1845 }
1846
1847 /**
1848 * Cancels an ongoing deletion operation. All files currently marked for deletion will be
1849 * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}.
1850 */
Ben Kwa7461a952015-09-01 11:03:01 -07001851 void undoDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001852 // Iterate over deleted items, temporarily marking them false in the deletion list, and
1853 // re-adding them to the UI.
1854 final int size = mMarkedForDeletion.size();
1855 for (int i = 0; i < size; ++i) {
1856 final int position = mMarkedForDeletion.keyAt(i);
1857 mMarkedForDeletion.put(position, false);
Ben Kwa379e1762015-09-21 10:49:52 -07001858 mViewAdapter.notifyItemInserted(position);
Ben Kwa91923182015-08-27 16:06:33 -07001859 }
1860
1861 // Then, clear the deletion list.
1862 mMarkedForDeletion.clear();
1863 }
1864
1865 /**
1866 * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
1867 * deleted. See {@link #markForDeletion(Selection)}.
Ben Kwa7461a952015-09-01 11:03:01 -07001868 *
1869 * @param view The view which will be used to interact with the user (e.g. surfacing
1870 * snackbars) for errors, info, etc.
Ben Kwa91923182015-08-27 16:06:33 -07001871 */
Ben Kwa83cedf22015-09-11 15:15:45 -07001872 void finalizeDeletion(Runnable errorCallback) {
Ben Kwa7461a952015-09-01 11:03:01 -07001873 final ContentResolver resolver = mContext.getContentResolver();
Ben Kwa83cedf22015-09-11 15:15:45 -07001874 DeleteFilesTask task = new DeleteFilesTask(resolver, errorCallback);
Ben Kwa7461a952015-09-01 11:03:01 -07001875 task.execute();
Ben Kwa91923182015-08-27 16:06:33 -07001876 }
1877
1878 /**
1879 * A Task which collects the DocumentInfo for documents that have been marked for deletion,
1880 * and actually deletes them.
1881 */
1882 private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
1883 private ContentResolver mResolver;
Ben Kwa7461a952015-09-01 11:03:01 -07001884 private Runnable mErrorCallback;
Ben Kwa91923182015-08-27 16:06:33 -07001885
Ben Kwa7461a952015-09-01 11:03:01 -07001886 /**
1887 * @param resolver A ContentResolver for performing the actual file deletions.
1888 * @param errorCallback A Runnable that is executed in the event that one or more errors
1889 * occured while copying files. Execution will occur on the UI thread.
1890 */
1891 public DeleteFilesTask(ContentResolver resolver, Runnable errorCallback) {
Ben Kwa91923182015-08-27 16:06:33 -07001892 mResolver = resolver;
Ben Kwa7461a952015-09-01 11:03:01 -07001893 mErrorCallback = errorCallback;
Ben Kwa91923182015-08-27 16:06:33 -07001894 }
1895
1896 @Override
1897 protected List<DocumentInfo> doInBackground(Void... params) {
1898 return getDocumentsMarkedForDeletion();
1899 }
1900
1901 @Override
1902 protected void onPostExecute(List<DocumentInfo> docs) {
1903 boolean hadTrouble = false;
1904 for (DocumentInfo doc : docs) {
1905 if (!doc.isDeleteSupported()) {
1906 Log.w(TAG, doc + " could not be deleted. Skipping...");
1907 hadTrouble = true;
1908 continue;
1909 }
1910
1911 ContentProviderClient client = null;
1912 try {
1913 if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
1914 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1915 mResolver, doc.derivedUri.getAuthority());
1916 DocumentsContract.deleteDocument(client, doc.derivedUri);
1917 } catch (Exception e) {
1918 Log.w(TAG, "Failed to delete " + doc);
1919 hadTrouble = true;
1920 } finally {
1921 ContentProviderClient.releaseQuietly(client);
1922 }
1923 }
1924
1925 if (hadTrouble) {
Ben Kwa7461a952015-09-01 11:03:01 -07001926 // TODO show which files failed? b/23720103
1927 mErrorCallback.run();
Ben Kwa91923182015-08-27 16:06:33 -07001928 if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed.");
1929 } else {
1930 if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
1931 }
1932 mMarkedForDeletion.clear();
1933 }
1934 }
Ben Kwa7461a952015-09-01 11:03:01 -07001935
1936 void addUpdateListener(UpdateListener listener) {
1937 checkState(mUpdateListener == null);
1938 mUpdateListener = listener;
1939 }
1940
Ben Kwa379e1762015-09-21 10:49:52 -07001941 static class UpdateListener {
Ben Kwa7461a952015-09-01 11:03:01 -07001942 /**
1943 * Called when a successful update has occurred.
1944 */
Ben Kwa379e1762015-09-21 10:49:52 -07001945 void onModelUpdate(Model model) {}
Ben Kwa7461a952015-09-01 11:03:01 -07001946
1947 /**
1948 * Called when an update has been attempted but failed.
1949 */
Ben Kwa379e1762015-09-21 10:49:52 -07001950 void onModelUpdateFailed(Exception e) {}
1951 }
1952 }
Ben Kwa7461a952015-09-01 11:03:01 -07001953
Ben Kwa379e1762015-09-21 10:49:52 -07001954 private class ModelUpdateListener extends Model.UpdateListener {
1955 @Override
1956 public void onModelUpdate(Model model) {
1957 if (model.info != null || model.error != null) {
1958 mMessageBar.setInfo(model.info);
1959 mMessageBar.setError(model.error);
1960 mMessageBar.show();
1961 }
Ben Kwa7461a952015-09-01 11:03:01 -07001962
Ben Kwa379e1762015-09-21 10:49:52 -07001963 mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE);
1964
1965 if (model.isEmpty()) {
Ben Kwa2f975262015-09-16 13:15:38 -07001966 showEmptyView();
Ben Kwa379e1762015-09-21 10:49:52 -07001967 } else {
Ben Kwa2f975262015-09-16 13:15:38 -07001968 showRecyclerView();
1969 mAdapter.notifyDataSetChanged();
Ben Kwa379e1762015-09-21 10:49:52 -07001970 }
Ben Kwa379e1762015-09-21 10:49:52 -07001971 }
1972
1973 @Override
1974 public void onModelUpdateFailed(Exception e) {
Ben Kwa2f975262015-09-16 13:15:38 -07001975 showErrorView();
Ben Kwa7461a952015-09-01 11:03:01 -07001976 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001977 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001978}