blob: 4583decc14a8086ce9aa7126caa0c4e99825983f [file] [log] [blame]
Jeff Sharkey9e0036e2013-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
Steve McKayf8621552015-11-03 15:23:16 -080017package com.android.documentsui.dirlist;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070018
Steve McKay7a3b88c2015-09-23 17:21:40 -070019import static com.android.documentsui.Shared.DEBUG;
Steve McKay7a3b88c2015-09-23 17:21:40 -070020import static com.android.documentsui.State.ACTION_MANAGE;
21import static com.android.documentsui.State.MODE_GRID;
22import static com.android.documentsui.State.MODE_LIST;
Steve McKay7a3b88c2015-09-23 17:21:40 -070023import static com.android.documentsui.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070024import static com.android.documentsui.model.DocumentInfo.getCursorInt;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070025import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKay4b3a13c2015-06-11 10:10:49 -070026import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKay0ba3a142015-07-23 16:33:41 -070027import static com.android.internal.util.Preconditions.checkState;
Steve McKayd28c5c32015-12-07 16:31:42 -080028import static com.google.common.base.Preconditions.checkArgument;
Steve McKay3da8afc2015-05-05 14:50:00 -070029
Steve McKayaa15dae2016-02-09 16:17:24 -080030import android.annotation.IntDef;
Steve McKay9ed88a42016-01-21 18:46:15 -080031import android.annotation.StringRes;
Ben Kwaef3f2622015-04-07 15:43:39 -070032import android.app.Activity;
Jeff Sharkeyd01571e2013-10-01 17:57:41 -070033import android.app.ActivityManager;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070034import android.app.Fragment;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070035import android.app.FragmentManager;
36import android.app.FragmentTransaction;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070037import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -070038import android.content.ClipData;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -070039import android.content.ContentResolver;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070040import android.content.Context;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -070041import android.content.Intent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070042import android.content.Loader;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070043import android.database.Cursor;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -070044import android.graphics.Canvas;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070045import android.graphics.Point;
Jeff Sharkey4ec97392013-09-10 12:04:26 -070046import android.graphics.drawable.Drawable;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070047import android.net.Uri;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070048import android.os.AsyncTask;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070049import android.os.Bundle;
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -070050import android.os.Parcelable;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070051import android.provider.DocumentsContract;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070052import android.provider.DocumentsContract.Document;
Ben Kwac64cb252015-08-27 16:04:46 -070053import android.support.annotation.Nullable;
Ben Kwa304895a2015-08-27 16:06:33 -070054import android.support.design.widget.Snackbar;
Steve McKay4b3a13c2015-06-11 10:10:49 -070055import android.support.v7.widget.GridLayoutManager;
Steve McKayef16f5f2015-12-22 18:15:31 -080056import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;
Steve McKay4b3a13c2015-06-11 10:10:49 -070057import android.support.v7.widget.RecyclerView;
Steve McKay669ebe72015-10-19 12:04:21 -070058import android.support.v7.widget.RecyclerView.OnItemTouchListener;
Steve McKay4b3a13c2015-06-11 10:10:49 -070059import android.support.v7.widget.RecyclerView.RecyclerListener;
60import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkeyf491c722015-06-11 09:16:19 -070061import android.text.TextUtils;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070062import android.util.Log;
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -070063import android.util.SparseArray;
Ben Kwa0f7078f02015-09-08 07:31:19 -070064import android.util.TypedValue;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -070065import android.view.ActionMode;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -070066import android.view.DragEvent;
Steve McKay4b3a13c2015-06-11 10:10:49 -070067import android.view.GestureDetector;
Ben Kwa67924892016-01-27 09:58:36 -080068import android.view.KeyEvent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070069import android.view.LayoutInflater;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070070import android.view.Menu;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070071import android.view.MenuItem;
Steve McKay4b3a13c2015-06-11 10:10:49 -070072import android.view.MotionEvent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070073import android.view.View;
74import android.view.ViewGroup;
Vladislav Kaznacheevb5999ef2015-09-04 09:17:37 -070075import android.view.ViewParent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070076import android.widget.ImageView;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070077import android.widget.TextView;
78
Steve McKayf8621552015-11-03 15:23:16 -080079import com.android.documentsui.BaseActivity;
Steve McKayf8621552015-11-03 15:23:16 -080080import com.android.documentsui.DirectoryLoader;
81import com.android.documentsui.DirectoryResult;
82import com.android.documentsui.DocumentClipper;
83import com.android.documentsui.DocumentsActivity;
84import com.android.documentsui.DocumentsApplication;
85import com.android.documentsui.Events;
Ben Kwa0436a752016-01-15 10:43:24 -080086import com.android.documentsui.Events.MotionInputEvent;
Steve McKayf8621552015-11-03 15:23:16 -080087import com.android.documentsui.Menus;
88import com.android.documentsui.MessageBar;
89import com.android.documentsui.MimePredicate;
Steve McKayf8621552015-11-03 15:23:16 -080090import com.android.documentsui.R;
Steve McKaye2af0782016-02-18 09:48:39 -080091import com.android.documentsui.RecentsLoader;
Steve McKayf8621552015-11-03 15:23:16 -080092import com.android.documentsui.RootsCache;
93import com.android.documentsui.Shared;
94import com.android.documentsui.Snackbars;
95import com.android.documentsui.State;
Ben Kwa1e2fa5e2016-02-02 23:00:02 -080096import com.android.documentsui.State.ViewMode;
Steve McKayf8621552015-11-03 15:23:16 -080097import com.android.documentsui.dirlist.MultiSelectManager.Selection;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070098import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewskif8c3f322015-04-14 16:32:41 +090099import com.android.documentsui.model.DocumentStack;
Jeff Sharkey251097b2013-09-02 15:07:28 -0700100import com.android.documentsui.model.RootInfo;
Steve McKay14e827a2016-01-06 18:32:13 -0800101import com.android.documentsui.services.FileOperationService;
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800102import com.android.documentsui.services.FileOperationService.OpType;
Steve McKay14e827a2016-01-06 18:32:13 -0800103import com.android.documentsui.services.FileOperations;
Steve McKaya6bbeab2016-02-17 15:02:01 -0800104
Steve McKay58efce32015-08-20 16:19:38 +0000105import com.google.common.collect.Lists;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700106
Steve McKayaa15dae2016-02-09 16:17:24 -0800107import java.lang.annotation.Retention;
108import java.lang.annotation.RetentionPolicy;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700109import java.util.ArrayList;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -0700110import java.util.Collections;
Jeff Sharkeyef7184a2013-08-05 17:56:48 -0700111import java.util.List;
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700112
113/**
114 * Display the documents inside a single directory.
115 */
Steve McKayef16f5f2015-12-22 18:15:31 -0800116public class DirectoryFragment extends Fragment implements DocumentsAdapter.Environment {
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700117
Steve McKayaa15dae2016-02-09 16:17:24 -0800118 @IntDef(flag = true, value = {
119 TYPE_NORMAL,
120 TYPE_SEARCH,
121 TYPE_RECENT_OPEN
122 })
123 @Retention(RetentionPolicy.SOURCE)
124 public @interface ResultType {}
Jeff Sharkeyef7184a2013-08-05 17:56:48 -0700125 public static final int TYPE_NORMAL = 1;
126 public static final int TYPE_SEARCH = 2;
Jeff Sharkey251097b2013-09-02 15:07:28 -0700127 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkeydc2963a2013-08-02 15:55:26 -0700128
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700129 public static final int ANIM_NONE = 1;
130 public static final int ANIM_SIDE = 2;
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +0900131 public static final int ANIM_LEAVE = 3;
132 public static final int ANIM_ENTER = 4;
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700133
Steve McKaya6bbeab2016-02-17 15:02:01 -0800134 @IntDef(flag = true, value = {
135 REQUEST_COPY_DESTINATION
136 })
137 @Retention(RetentionPolicy.SOURCE)
138 public @interface RequestCode {}
Ben Kwaef3f2622015-04-07 15:43:39 -0700139 public static final int REQUEST_COPY_DESTINATION = 1;
140
Steve McKayef16f5f2015-12-22 18:15:31 -0800141 static final boolean DEBUG_ENABLE_DND = true;
Steve McKayb46383b2015-05-06 14:27:57 -0700142
Steve McKay35645432016-01-20 15:09:35 -0800143 private static final String TAG = "DirectoryFragment";
144 private static final int LOADER_ID = 42;
145 private static final int DELETE_UNDO_TIMEOUT = 5000;
146 private static final int DELETE_JOB_DELAY = 5500;
Steve McKay9ed88a42016-01-21 18:46:15 -0800147 private static final int EMPTY_REVEAL_DURATION = 250;
Steve McKay35645432016-01-20 15:09:35 -0800148
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700149 private static final String EXTRA_TYPE = "type";
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700150 private static final String EXTRA_ROOT = "root";
151 private static final String EXTRA_DOC = "doc";
Jeff Sharkey251097b2013-09-02 15:07:28 -0700152 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700153 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700154
Ben Kwa18fce3c2015-09-01 11:03:01 -0700155 private Model mModel;
Ben Kwa8250db42015-10-07 14:15:12 -0700156 private MultiSelectManager mSelectionManager;
Ben Kwa91caed82015-09-21 10:49:52 -0700157 private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
Ben Kwa0436a752016-01-15 10:43:24 -0800158 private ItemEventListener mItemEventListener = new ItemEventListener();
Ben Kwab0bfe2d2016-02-09 11:27:45 -0800159 private FocusManager mFocusManager;
Ben Kwac64cb252015-08-27 16:04:46 -0700160
Ben Kwad8391492015-12-17 10:37:00 -0800161 private IconHelper mIconHelper;
162
Steve McKay4b3a13c2015-06-11 10:10:49 -0700163 private View mEmptyView;
164 private RecyclerView mRecView;
Steve McKay41585d52016-01-21 15:10:39 -0800165 private ListeningGestureDetector mGestureDetector;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700166
Steve McKayaa15dae2016-02-09 16:17:24 -0800167 private @ResultType int mType = TYPE_NORMAL;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700168 private String mStateKey;
169
Steve McKay4b3a13c2015-06-11 10:10:49 -0700170 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700171 private DocumentsAdapter mAdapter;
172 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKayf69502c2015-11-09 17:38:35 +0900173 private FragmentTuner mTuner;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700174 private DocumentClipper mClipper;
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800175 private GridLayoutManager mLayout;
Steve McKay0ba3a142015-07-23 16:33:41 -0700176 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700177
Ben Kwa3b19e312015-09-16 08:04:37 -0700178 private MessageBar mMessageBar;
Ben Kwa91caed82015-09-21 10:49:52 -0700179 private View mProgressBar;
Ben Kwa3b19e312015-09-16 08:04:37 -0700180
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700181 @Override
182 public View onCreateView(
183 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700184 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
185
Ben Kwa3b19e312015-09-16 08:04:37 -0700186 mMessageBar = MessageBar.create(getChildFragmentManager());
Ben Kwa91caed82015-09-21 10:49:52 -0700187 mProgressBar = view.findViewById(R.id.progressbar);
Ben Kwa3b19e312015-09-16 08:04:37 -0700188
Jeff Sharkey9fb567b2013-08-07 16:22:02 -0700189 mEmptyView = view.findViewById(android.R.id.empty);
190
Ben Kwa2036dad2016-02-10 07:46:35 -0800191 mRecView = (RecyclerView) view.findViewById(R.id.dir_list);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700192 mRecView.setRecyclerListener(
193 new RecyclerListener() {
194 @Override
195 public void onViewRecycled(ViewHolder holder) {
196 cancelThumbnailTask(holder.itemView);
197 }
198 });
Steve McKayb46383b2015-05-06 14:27:57 -0700199
Ben Kwae48e4ca2015-10-20 15:02:33 -0700200 mRecView.setItemAnimator(new DirectoryItemAnimator(getActivity()));
Ben Kwabd964562015-10-14 08:00:27 -0700201
Steve McKay0ba3a142015-07-23 16:33:41 -0700202 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKayb46383b2015-05-06 14:27:57 -0700203 if (DEBUG_ENABLE_DND) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700204 setupDragAndDropOnDirectoryView(mRecView);
Steve McKayb46383b2015-05-06 14:27:57 -0700205 }
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700206
Jeff Sharkey5dfb3452013-08-31 21:27:44 -0700207 return view;
208 }
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700209
Jeff Sharkey5dfb3452013-08-31 21:27:44 -0700210 @Override
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700211 public void onDestroyView() {
212 super.onDestroyView();
213
214 // Cancel any outstanding thumbnail requests
Steve McKay4b3a13c2015-06-11 10:10:49 -0700215 final int count = mRecView.getChildCount();
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700216 for (int i = 0; i < count; i++) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700217 final View view = mRecView.getChildAt(i);
218 cancelThumbnailTask(view);
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700219 }
220 }
221
222 @Override
Jeff Sharkey5dfb3452013-08-31 21:27:44 -0700223 public void onActivityCreated(Bundle savedInstanceState) {
224 super.onActivityCreated(savedInstanceState);
225
226 final Context context = getActivity();
Steve McKayf69502c2015-11-09 17:38:35 +0900227 final State state = getDisplayState();
Jeff Sharkey5dfb3452013-08-31 21:27:44 -0700228
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700229 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
230 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Steve McKaye852d932016-02-08 19:09:42 -0800231 mStateKey = buildStateKey(root, doc);
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700232
Steve McKay3eb2d072016-01-25 19:00:22 -0800233 mIconHelper = new IconHelper(context, MODE_GRID);
Steve McKayef16f5f2015-12-22 18:15:31 -0800234
235 mAdapter = new SectionBreakDocumentsAdapterWrapper(
236 this, new ModelBackedDocumentsAdapter(this, mIconHelper));
237
Steve McKay4b3a13c2015-06-11 10:10:49 -0700238 mRecView.setAdapter(mAdapter);
239
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800240 mLayout = new GridLayoutManager(getContext(), mColumnCount);
241 SpanSizeLookup lookup = mAdapter.createSpanSizeLookup();
242 if (lookup != null) {
243 mLayout.setSpanSizeLookup(lookup);
244 }
245 mRecView.setLayoutManager(mLayout);
246
Steve McKay41585d52016-01-21 15:10:39 -0800247 mGestureDetector = new ListeningGestureDetector(this.getContext(), new GestureListener());
Steve McKay669ebe72015-10-19 12:04:21 -0700248
Steve McKay41585d52016-01-21 15:10:39 -0800249 mRecView.addOnItemTouchListener(mGestureDetector);
Steve McKay669ebe72015-10-19 12:04:21 -0700250
Steve McKaye852d932016-02-08 19:09:42 -0800251 // final here because we'll manually bump the listener iwhen we had an initial selection,
252 // but only after the model is fully loaded.
253 final SelectionModeListener selectionListener = new SelectionModeListener();
254 final Selection initialSelection = state.selectedDocuments.hasDirectoryKey(mStateKey)
255 ? state.selectedDocuments
256 : null;
257
Ben Kwac64cb252015-08-27 16:04:46 -0700258 // TODO: instead of inserting the view into the constructor, extract listener-creation code
259 // and set the listener on the view after the fact. Then the view doesn't need to be passed
Ben Kwa8250db42015-10-07 14:15:12 -0700260 // into the selection manager.
261 mSelectionManager = new MultiSelectManager(
Steve McKaydbec47a2015-08-12 14:48:34 -0700262 mRecView,
Steve McKay44408262016-01-05 15:27:17 -0800263 mAdapter,
Steve McKaydbec47a2015-08-12 14:48:34 -0700264 state.allowMultiple
265 ? MultiSelectManager.MODE_MULTIPLE
Steve McKaye852d932016-02-08 19:09:42 -0800266 : MultiSelectManager.MODE_SINGLE,
267 initialSelection);
268
269 mSelectionManager.addCallback(selectionListener);
Ben Kwa18fce3c2015-09-01 11:03:01 -0700270
Steve McKay35645432016-01-20 15:09:35 -0800271 mModel = new Model();
Ben Kwa743c7c22015-12-01 19:56:57 -0800272 mModel.addUpdateListener(mAdapter);
Ben Kwa91caed82015-09-21 10:49:52 -0700273 mModel.addUpdateListener(mModelUpdateListener);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700274
Ben Kwaa4acc902016-02-10 15:48:25 -0800275 // Make sure this is done after the RecyclerView is set up.
276 mFocusManager = new FocusManager(context, mRecView, mModel);
277
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700278 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700279
Steve McKayaa15dae2016-02-09 16:17:24 -0800280 mTuner = FragmentTuner.pick(getContext(), state);
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700281 mClipper = new DocumentClipper(context);
282
Ben Kwad8391492015-12-17 10:37:00 -0800283 boolean hideGridTitles;
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700284 if (mType == TYPE_RECENT_OPEN) {
285 // Hide titles when showing recents for picking images/videos
Ben Kwad8391492015-12-17 10:37:00 -0800286 hideGridTitles = MimePredicate.mimeMatches(
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700287 MimePredicate.VISUAL_MIMES, state.acceptMimes);
288 } else {
Ben Kwad8391492015-12-17 10:37:00 -0800289 hideGridTitles = (doc != null) && doc.isGridTitlesHidden();
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700290 }
Ben Kwad8391492015-12-17 10:37:00 -0800291 GridDocumentHolder.setHideTitles(hideGridTitles);
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700292
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700293 final ActivityManager am = (ActivityManager) context.getSystemService(
294 Context.ACTIVITY_SERVICE);
Ben Kwad8391492015-12-17 10:37:00 -0800295 boolean svelte = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
296 mIconHelper.setThumbnailsEnabled(!svelte);
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700297
Jeff Sharkeyb4486602013-08-18 22:26:48 -0700298 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700299 @Override
Jeff Sharkeyb4486602013-08-18 22:26:48 -0700300 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkey251097b2013-09-02 15:07:28 -0700301 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkeybc2971d2013-07-31 20:53:22 -0700302
Jeff Sharkey251097b2013-09-02 15:07:28 -0700303 Uri contentsUri;
304 switch (mType) {
305 case TYPE_NORMAL:
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700306 contentsUri = DocumentsContract.buildChildDocumentsUri(
307 doc.authority, doc.documentId);
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700308 if (state.action == ACTION_MANAGE) {
309 contentsUri = DocumentsContract.setManageMode(contentsUri);
310 }
Jeff Sharkeyfb3445c2013-09-09 17:35:46 -0700311 return new DirectoryLoader(
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700312 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkey251097b2013-09-02 15:07:28 -0700313 case TYPE_SEARCH:
314 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700315 root.authority, root.rootId, query);
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700316 if (state.action == ACTION_MANAGE) {
317 contentsUri = DocumentsContract.setManageMode(contentsUri);
318 }
Jeff Sharkeyfb3445c2013-09-09 17:35:46 -0700319 return new DirectoryLoader(
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700320 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkey251097b2013-09-02 15:07:28 -0700321 case TYPE_RECENT_OPEN:
Jeff Sharkey348ad682013-09-02 17:19:40 -0700322 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Steve McKaye2af0782016-02-18 09:48:39 -0800323 return new RecentsLoader(context, roots, state);
Jeff Sharkey251097b2013-09-02 15:07:28 -0700324 default:
325 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkey251097b2013-09-02 15:07:28 -0700326 }
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700327 }
328
329 @Override
Jeff Sharkeyb4486602013-08-18 22:26:48 -0700330 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700331 if (!isAdded()) return;
332
Ben Kwac64cb252015-08-27 16:04:46 -0700333 mModel.update(result);
Jeff Sharkeyfb3445c2013-09-09 17:35:46 -0700334 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700335
336 updateDisplayState();
337
Steve McKaye852d932016-02-08 19:09:42 -0800338 if (initialSelection != null) {
339 selectionListener.onSelectionChanged();
340 }
341
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700342 // Restore any previous instance state
343 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
344 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
345 getView().restoreHierarchyState(container);
346 } else if (mLastSortOrder != state.derivedSortOrder) {
Ben Kwa862b96412015-12-07 13:25:27 -0800347 // The derived sort order takes the user sort order into account, but applies
348 // directory-specific defaults when the user doesn't explicitly set the sort
349 // order. Scroll to the top if the sort order actually changed.
Steve McKay4b3a13c2015-06-11 10:10:49 -0700350 mRecView.smoothScrollToPosition(0);
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700351 }
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700352
353 mLastSortOrder = state.derivedSortOrder;
Steve McKayaa15dae2016-02-09 16:17:24 -0800354
355 mTuner.onModelLoaded(mModel, mType);
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700356 }
357
358 @Override
Jeff Sharkeyb4486602013-08-18 22:26:48 -0700359 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwac64cb252015-08-27 16:04:46 -0700360 mModel.update(null);
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700361 }
362 };
363
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700364 // Kick off loader at least once
Steve McKay4b3a13c2015-06-11 10:10:49 -0700365 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700366 }
367
Jeff Sharkey28c05ee2013-09-06 13:22:09 -0700368 @Override
Steve McKaye852d932016-02-08 19:09:42 -0800369 public void onSaveInstanceState(Bundle outState) {
370 State state = getDisplayState();
371 if (mSelectionManager.hasSelection()) {
372 mSelectionManager.getSelection(state.selectedDocuments);
373 state.selectedDocuments.setDirectoryKey(mStateKey);
374 if (!state.selectedDocuments.isEmpty()) {
375 if (DEBUG) Log.d(TAG, "Persisted selection: " + state.selectedDocuments);
376 }
377 }
378 }
379
380 @Override
Steve McKaya6bbeab2016-02-17 15:02:01 -0800381 public void onActivityResult(@RequestCode int requestCode, int resultCode, Intent data) {
382 switch(requestCode) {
383 case REQUEST_COPY_DESTINATION:
384 handleCopyResult(resultCode, data);
385 break;
386 default:
387 throw new UnsupportedOperationException("Unknown request code: " + requestCode);
Ben Kwaef3f2622015-04-07 15:43:39 -0700388 }
Steve McKaya6bbeab2016-02-17 15:02:01 -0800389 }
390
391 private void handleCopyResult(int resultCode, Intent data) {
Ben Kwaef3f2622015-04-07 15:43:39 -0700392 if (resultCode == Activity.RESULT_CANCELED || data == null) {
393 // User pressed the back button or otherwise cancelled the destination pick. Don't
394 // proceed with the copy.
395 return;
396 }
397
Steve McKaya6bbeab2016-02-17 15:02:01 -0800398 @OpType int operationType = data.getIntExtra(
Steve McKay14e827a2016-01-06 18:32:13 -0800399 FileOperationService.EXTRA_OPERATION,
400 FileOperationService.OPERATION_COPY);
401
402 FileOperations.start(
403 getActivity(),
404 getDisplayState().selectedDocumentsForCopy,
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900405 getDisplayState().stack.peek(),
Steve McKay323ee3e2015-09-25 16:02:56 -0700406 (DocumentStack) data.getParcelableExtra(Shared.EXTRA_STACK),
Steve McKay14e827a2016-01-06 18:32:13 -0800407 operationType);
Ben Kwaef3f2622015-04-07 15:43:39 -0700408 }
409
Steve McKay5353a1e2015-07-30 12:27:44 -0700410 protected boolean onDoubleTap(MotionEvent e) {
411 if (Events.isMouseEvent(e)) {
Ben Kwa003a9992015-11-30 23:00:02 -0800412 String id = getModelId(e);
413 if (id != null) {
414 return handleViewItem(id);
Steve McKay5353a1e2015-07-30 12:27:44 -0700415 }
416 }
417 return false;
418 }
419
Ben Kwa003a9992015-11-30 23:00:02 -0800420 private boolean handleViewItem(String id) {
421 final Cursor cursor = mModel.getItem(id);
Steve McKay5353a1e2015-07-30 12:27:44 -0700422 checkNotNull(cursor, "Cursor cannot be null.");
423 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
424 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayf69502c2015-11-09 17:38:35 +0900425 if (mTuner.isDocumentEnabled(docMimeType, docFlags)) {
Steve McKay5353a1e2015-07-30 12:27:44 -0700426 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwac64cb252015-08-27 16:04:46 -0700427 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
Ben Kwa8250db42015-10-07 14:15:12 -0700428 mSelectionManager.clearSelection();
Steve McKay5353a1e2015-07-30 12:27:44 -0700429 return true;
430 }
Steve McKay4b3a13c2015-06-11 10:10:49 -0700431 return false;
432 }
433
Ben Kwaef3f2622015-04-07 15:43:39 -0700434 @Override
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700435 public void onStop() {
436 super.onStop();
437
438 // Remember last scroll location
439 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
440 getView().saveHierarchyState(container);
Steve McKayf69502c2015-11-09 17:38:35 +0900441 final State state = getDisplayState();
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700442 state.dirState.put(mStateKey, container);
443 }
444
Jeff Sharkey669f8e72014-08-08 15:10:03 -0700445 public void onDisplayStateChanged() {
446 updateDisplayState();
447 }
448
Steve McKay3eb2d072016-01-25 19:00:22 -0800449 public void onSortOrderChanged() {
450 // Sort order is implemented as a sorting wrapper around directory
451 // results. So when sort order changes, we force a reload of the directory.
Steve McKay4b3a13c2015-06-11 10:10:49 -0700452 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700453 }
454
Steve McKay3eb2d072016-01-25 19:00:22 -0800455 public void onViewModeChanged() {
456 // Mode change is just visual change; no need to kick loader.
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700457 updateDisplayState();
458 }
459
460 private void updateDisplayState() {
Steve McKay3eb2d072016-01-25 19:00:22 -0800461 State state = getDisplayState();
Steve McKay4b3a13c2015-06-11 10:10:49 -0700462 updateLayout(state.derivedMode);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700463 mRecView.setAdapter(mAdapter);
464 }
465
466 /**
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800467 * Updates the layout after the view mode switches.
468 * @param mode The new view mode.
Steve McKay4b3a13c2015-06-11 10:10:49 -0700469 */
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800470 private void updateLayout(@ViewMode int mode) {
471 mColumnCount = calculateColumnCount(mode);
472 if (mLayout != null) {
473 mLayout.setSpanCount(mColumnCount);
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700474 }
Jeff Sharkey9d0843d2013-05-07 12:41:33 -0700475
Steve McKay76be6202016-01-12 11:14:33 -0800476 int pad = getDirectoryPadding(mode);
477 mRecView.setPadding(pad, pad, pad, pad);
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800478 mRecView.requestLayout();
Steve McKay76be6202016-01-12 11:14:33 -0800479 mSelectionManager.handleLayoutChanged(); // RecyclerView doesn't do this for us
Steve McKay3eb2d072016-01-25 19:00:22 -0800480 mIconHelper.setViewMode(mode);
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700481 }
482
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800483 private int calculateColumnCount(@ViewMode int mode) {
484 if (mode == MODE_LIST) {
485 // List mode is a "grid" with 1 column.
486 return 1;
487 }
488
Steve McKay58efce32015-08-20 16:19:38 +0000489 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
490 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKay0ba3a142015-07-23 16:33:41 -0700491 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKay58efce32015-08-20 16:19:38 +0000492
Steve McKay0ba3a142015-07-23 16:33:41 -0700493 checkState(mRecView.getWidth() > 0);
494 int columnCount = Math.max(1,
Steve McKay58efce32015-08-20 16:19:38 +0000495 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
496
Steve McKay0ba3a142015-07-23 16:33:41 -0700497 return columnCount;
498 }
499
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800500 private int getDirectoryPadding(@ViewMode int mode) {
Steve McKay76be6202016-01-12 11:14:33 -0800501 switch (mode) {
502 case MODE_GRID:
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800503 return getResources().getDimensionPixelSize(R.dimen.grid_container_padding);
Steve McKay76be6202016-01-12 11:14:33 -0800504 case MODE_LIST:
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800505 return getResources().getDimensionPixelSize(R.dimen.list_container_padding);
Steve McKay76be6202016-01-12 11:14:33 -0800506 default:
507 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
508 }
509 }
510
Steve McKayef16f5f2015-12-22 18:15:31 -0800511 @Override
512 public int getColumnCount() {
513 return mColumnCount;
514 }
515
Steve McKay4b3a13c2015-06-11 10:10:49 -0700516 /**
517 * Manages the integration between our ActionMode and MultiSelectManager, initiating
518 * ActionMode when there is a selection, canceling it when there is no selection,
519 * and clearing selection when action mode is explicitly exited by the user.
520 */
521 private final class SelectionModeListener
522 implements MultiSelectManager.Callback, ActionMode.Callback {
523
524 private Selection mSelected = new Selection();
525 private ActionMode mActionMode;
Steve McKay503648d2015-07-22 12:13:46 -0700526 private int mNoDeleteCount = 0;
Aga Wronska3b327ef2016-01-20 16:32:33 -0800527 private int mNoRenameCount = -1;
Steve McKay503648d2015-07-22 12:13:46 -0700528 private Menu mMenu;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700529
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700530 @Override
Ben Kwa743c7c22015-12-01 19:56:57 -0800531 public boolean onBeforeItemStateChange(String modelId, boolean selected) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700532 if (selected) {
Ben Kwa743c7c22015-12-01 19:56:57 -0800533 final Cursor cursor = mModel.getItem(modelId);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700534 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey2a030b02013-09-26 10:54:16 -0700535 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
536 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900537 return mTuner.canSelectType(docMimeType, docFlags);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700538 }
539 return true;
540 }
541
542 @Override
Ben Kwa743c7c22015-12-01 19:56:57 -0800543 public void onItemStateChanged(String modelId, boolean selected) {
544 final Cursor cursor = mModel.getItem(modelId);
Steve McKay30551a22016-02-16 13:08:10 -0800545 if (cursor == null) {
546 Log.e(TAG, "Model returned null cursor for document: " + modelId
547 + ". Ignoring state changed event.");
548 return;
549 }
Steve McKay503648d2015-07-22 12:13:46 -0700550
Ben Kwa67924892016-01-27 09:58:36 -0800551 // TODO: Should this be happening in onSelectionChanged? Technically this callback is
552 // triggered on "silent" selection updates (i.e. we might be reacting to unfinalized
553 // selection changes here)
Steve McKay503648d2015-07-22 12:13:46 -0700554 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
555 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
556 mNoDeleteCount += selected ? 1 : -1;
557 }
Aga Wronska3b327ef2016-01-20 16:32:33 -0800558 if ((docFlags & Document.FLAG_SUPPORTS_RENAME) != 0) {
559 mNoRenameCount += selected ? 1 : -1;
560 }
Steve McKaydbec47a2015-08-12 14:48:34 -0700561 }
Steve McKay503648d2015-07-22 12:13:46 -0700562
Steve McKaydbec47a2015-08-12 14:48:34 -0700563 @Override
564 public void onSelectionChanged() {
Ben Kwa8250db42015-10-07 14:15:12 -0700565 mSelectionManager.getSelection(mSelected);
Ben Kwa7cc80012015-09-11 15:40:18 -0700566 TypedValue color = new TypedValue();
Steve McKay503648d2015-07-22 12:13:46 -0700567 if (mSelected.size() > 0) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700568 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
569 if (mActionMode == null) {
570 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
571 mActionMode = getActivity().startActionMode(this);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700572 }
Ben Kwa8250db42015-10-07 14:15:12 -0700573 getActivity().getTheme().resolveAttribute(R.attr.colorActionMode, color, true);
Steve McKay503648d2015-07-22 12:13:46 -0700574 updateActionMenu();
575 } else {
576 if (DEBUG) Log.d(TAG, "Finishing action mode.");
577 if (mActionMode != null) {
578 mActionMode.finish();
579 }
Ben Kwa0f7078f02015-09-08 07:31:19 -0700580 getActivity().getTheme().resolveAttribute(
581 android.R.attr.colorPrimaryDark, color, true);
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700582 }
Ben Kwa7cc80012015-09-11 15:40:18 -0700583 getActivity().getWindow().setStatusBarColor(color.data);
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700584
Steve McKay4b3a13c2015-06-11 10:10:49 -0700585 if (mActionMode != null) {
Steve McKay5aacf1f2015-10-27 14:15:58 -0700586 mActionMode.setTitle(String.valueOf(mSelected.size()));
Steve McKay4b3a13c2015-06-11 10:10:49 -0700587 }
588 }
589
590 // Called when the user exits the action mode
591 @Override
592 public void onDestroyActionMode(ActionMode mode) {
593 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
594 mActionMode = null;
595 // clear selection
Ben Kwa8250db42015-10-07 14:15:12 -0700596 mSelectionManager.clearSelection();
Steve McKay503648d2015-07-22 12:13:46 -0700597 mSelected.clear();
598 mNoDeleteCount = 0;
Aga Wronska3b327ef2016-01-20 16:32:33 -0800599 mNoRenameCount = -1;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700600 }
601
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700602 @Override
603 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Ben Kwa8250db42015-10-07 14:15:12 -0700604 int size = mSelectionManager.getSelection().size();
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700605 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa8250db42015-10-07 14:15:12 -0700606 mode.setTitle(TextUtils.formatSelectedCount(size));
607 return (size > 0);
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700608 }
609
610 @Override
611 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay503648d2015-07-22 12:13:46 -0700612 mMenu = menu;
613 updateActionMenu();
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700614 return true;
615 }
616
Aga Wronska3b327ef2016-01-20 16:32:33 -0800617 boolean canRenameSelection() {
618 return mNoRenameCount == 0 && mSelectionManager.getSelection().size() == 1;
619 }
620
621 boolean canDeleteSelection() {
622 return mNoDeleteCount == 0;
623 }
624
Steve McKay503648d2015-07-22 12:13:46 -0700625 private void updateActionMenu() {
626 checkNotNull(mMenu);
Aga Wronska3b327ef2016-01-20 16:32:33 -0800627
Steve McKay503648d2015-07-22 12:13:46 -0700628 // Delegate update logic to our owning action, since specialized logic is desired.
Aga Wronska3b327ef2016-01-20 16:32:33 -0800629 mTuner.updateActionMenu(mMenu, mType, canDeleteSelection(), canRenameSelection());
Steve McKay5bbae102015-10-01 11:39:24 -0700630 Menus.disableHiddenItems(mMenu);
Steve McKay503648d2015-07-22 12:13:46 -0700631 }
632
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700633 @Override
Steve McKay4b3a13c2015-06-11 10:10:49 -0700634 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700635
Ben Kwa8250db42015-10-07 14:15:12 -0700636 Selection selection = mSelectionManager.getSelection(new Selection());
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700637
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800638 switch (item.getItemId()) {
639 case R.id.menu_open:
640 openDocuments(selection);
641 mode.finish();
642 return true;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700643
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800644 case R.id.menu_share:
645 shareDocuments(selection);
646 mode.finish();
647 return true;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700648
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800649 case R.id.menu_delete:
650 // Exit selection mode first, so we avoid deselecting deleted documents.
651 mode.finish();
652 deleteDocuments(selection);
653 return true;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700654
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800655 case R.id.menu_copy_to:
Steve McKay14e827a2016-01-06 18:32:13 -0800656 transferDocuments(selection, FileOperationService.OPERATION_COPY);
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800657 mode.finish();
658 return true;
Ben Kwa0b4a3c42015-05-05 11:50:11 -0700659
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800660 case R.id.menu_move_to:
661 // Exit selection mode first, so we avoid deselecting deleted documents.
662 mode.finish();
Steve McKay14e827a2016-01-06 18:32:13 -0800663 transferDocuments(selection, FileOperationService.OPERATION_MOVE);
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800664 return true;
Ben Kwad99109f2015-03-31 10:11:43 -0700665
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800666 case R.id.menu_copy_to_clipboard:
Steve McKaycbee5442016-01-28 15:30:10 -0800667 copySelectedToClipboard();
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800668 return true;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700669
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800670 case R.id.menu_select_all:
671 selectAllFiles();
672 return true;
Ben Kwa3bcc94882015-03-31 08:15:21 -0700673
Aga Wronska3b327ef2016-01-20 16:32:33 -0800674 case R.id.menu_rename:
675 renameDocuments(selection);
676 mode.finish();
677 return true;
678
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800679 default:
680 if (DEBUG) Log.d(TAG, "Unhandled menu item selected: " + item);
681 return false;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700682 }
683 }
Steve McKay4b3a13c2015-06-11 10:10:49 -0700684 }
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700685
Steve McKaycbee5442016-01-28 15:30:10 -0800686 public final boolean onBackPressed() {
687 if (mSelectionManager.hasSelection()) {
688 if (DEBUG) Log.d(TAG, "Clearing selection on back pressed.");
689 mSelectionManager.clearSelection();
690 return true;
691 }
692 return false;
693 }
694
Ben Kwad8391492015-12-17 10:37:00 -0800695 private void cancelThumbnailTask(View view) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700696 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
697 if (iconThumb != null) {
Ben Kwad8391492015-12-17 10:37:00 -0800698 mIconHelper.stopLoading(iconThumb);
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700699 }
Steve McKay4b3a13c2015-06-11 10:10:49 -0700700 }
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700701
Steve McKay4b3a13c2015-06-11 10:10:49 -0700702 private void openDocuments(final Selection selected) {
Steve McKayab7865c2015-05-27 16:11:42 -0700703 new GetDocumentsTask() {
704 @Override
705 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay12055472015-08-20 16:48:49 -0700706 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKayab7865c2015-05-27 16:11:42 -0700707 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwa726cf702015-04-08 15:03:35 -0700708 }
Steve McKayab7865c2015-05-27 16:11:42 -0700709 }.execute(selected);
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700710 }
711
Steve McKay4b3a13c2015-06-11 10:10:49 -0700712 private void shareDocuments(final Selection selected) {
Steve McKayab7865c2015-05-27 16:11:42 -0700713 new GetDocumentsTask() {
714 @Override
715 void onDocumentsReady(List<DocumentInfo> docs) {
716 Intent intent;
717
718 // Filter out directories - those can't be shared.
Steve McKay58efce32015-08-20 16:19:38 +0000719 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKayab7865c2015-05-27 16:11:42 -0700720 for (DocumentInfo doc: docs) {
721 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
722 docsForSend.add(doc);
723 }
724 }
725
726 if (docsForSend.size() == 1) {
727 final DocumentInfo doc = docsForSend.get(0);
728
729 intent = new Intent(Intent.ACTION_SEND);
730 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
731 intent.addCategory(Intent.CATEGORY_DEFAULT);
732 intent.setType(doc.mimeType);
733 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
734
735 } else if (docsForSend.size() > 1) {
736 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
737 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
738 intent.addCategory(Intent.CATEGORY_DEFAULT);
739
Steve McKay58efce32015-08-20 16:19:38 +0000740 final ArrayList<String> mimeTypes = new ArrayList<>();
741 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKayab7865c2015-05-27 16:11:42 -0700742 for (DocumentInfo doc : docsForSend) {
743 mimeTypes.add(doc.mimeType);
744 uris.add(doc.derivedUri);
745 }
746
747 intent.setType(findCommonMimeType(mimeTypes));
748 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
749
750 } else {
751 return;
752 }
753
754 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
755 startActivity(intent);
756 }
757 }.execute(selected);
758 }
759
Steve McKay4b3a13c2015-06-11 10:10:49 -0700760 private void deleteDocuments(final Selection selected) {
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700761
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900762 checkArgument(!selected.isEmpty());
763 final DocumentInfo srcParent = getDisplayState().stack.peek();
764 new GetDocumentsTask() {
765 @Override
766 void onDocumentsReady(List<DocumentInfo> docs) {
767 // Hide the files in the UI.
768 final SparseArray<String> hidden = mAdapter.hide(selected.getAll());
Steve McKay35645432016-01-20 15:09:35 -0800769
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900770 checkState(DELETE_JOB_DELAY > DELETE_UNDO_TIMEOUT);
771 String operationId = FileOperations.delete(
772 getActivity(), docs, srcParent, getDisplayState().stack,
773 DELETE_JOB_DELAY);
774 showDeleteSnackbar(hidden, operationId);
775 }
776 }.execute(selected);
Steve McKay35645432016-01-20 15:09:35 -0800777 }
778
779 private void showDeleteSnackbar(final SparseArray<String> hidden, final String jobId) {
780
781 Context context = getActivity();
782 String message = Shared.getQuantityString(context, R.plurals.deleting, hidden.size());
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700783
Ben Kwadb65cd52015-12-09 14:33:49 -0800784 // Show a snackbar informing the user that files will be deleted, and give them an option to
785 // cancel.
Ben Kwa94b486d2015-09-30 10:00:10 -0700786 final Activity activity = getActivity();
Steve McKay35645432016-01-20 15:09:35 -0800787 Snackbars.makeSnackbar(activity, message, DELETE_UNDO_TIMEOUT)
Ben Kwa304895a2015-08-27 16:06:33 -0700788 .setAction(
789 R.string.undo,
Ben Kwabd964562015-10-14 08:00:27 -0700790 new View.OnClickListener() {
Ben Kwa304895a2015-08-27 16:06:33 -0700791 @Override
792 public void onClick(View view) {}
793 })
794 .setCallback(
795 new Snackbar.Callback() {
796 @Override
797 public void onDismissed(Snackbar snackbar, int event) {
798 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
Ben Kwadb65cd52015-12-09 14:33:49 -0800799 // If the delete was cancelled, just unhide the files.
Steve McKay35645432016-01-20 15:09:35 -0800800 FileOperations.cancel(activity, jobId);
801 mAdapter.unhide(hidden);
Ben Kwa304895a2015-08-27 16:06:33 -0700802 }
Ben Kwa304895a2015-08-27 16:06:33 -0700803 }
804 })
805 .show();
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700806 }
807
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800808 private void transferDocuments(final Selection selected, final @OpType int mode) {
Ben Kwaef3f2622015-04-07 15:43:39 -0700809 // Pop up a dialog to pick a destination. This is inadequate but works for now.
810 // TODO: Implement a picker that is to spec.
Daichi Hironobbe22922015-04-10 15:50:38 +0900811 final Intent intent = new Intent(
Ben Kwa84cebbe2015-09-25 14:48:29 -0700812 Shared.ACTION_PICK_COPY_DESTINATION,
Daichi Hironobbe22922015-04-10 15:50:38 +0900813 Uri.EMPTY,
814 getActivity(),
815 DocumentsActivity.class);
Steve McKayab7865c2015-05-27 16:11:42 -0700816
Steve McKaya6bbeab2016-02-17 15:02:01 -0800817 // Set an appropriate title on the drawer when it is shown in the picker.
818 // Coupled with the fact that we auto-open the drawer for copy/move operations
819 // it should basically be the thing people see first.
820 int drawerTitleId = mode == FileOperationService.OPERATION_MOVE
821 ? R.string.menu_move : R.string.menu_copy;
822 intent.putExtra(DocumentsContract.EXTRA_PROMPT, getResources().getString(drawerTitleId));
823
Steve McKayab7865c2015-05-27 16:11:42 -0700824 new GetDocumentsTask() {
825 @Override
826 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKaya6bbeab2016-02-17 15:02:01 -0800827 // TODO: Can this move to Fragment bundle state?
Steve McKayf69502c2015-11-09 17:38:35 +0900828 getDisplayState().selectedDocumentsForCopy = docs;
Steve McKayab7865c2015-05-27 16:11:42 -0700829
Steve McKaya6bbeab2016-02-17 15:02:01 -0800830 // Determine if there is a directory in the set of documents
831 // to be copied? Why? Directory creation isn't supported by some roots
832 // (like Downloads). This informs DocumentsActivity (the "picker")
833 // to restrict available roots to just those with support.
834 intent.putExtra(Shared.EXTRA_DIRECTORY_COPY, hasDirectory(docs));
Steve McKay14e827a2016-01-06 18:32:13 -0800835 intent.putExtra(FileOperationService.EXTRA_OPERATION, mode);
Steve McKaya6bbeab2016-02-17 15:02:01 -0800836
837 // This just identifies the type of request...we'll check it
838 // when we reveive a response.
Steve McKayab7865c2015-05-27 16:11:42 -0700839 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hirono9be34292015-04-14 17:12:54 +0900840 }
Steve McKaya6bbeab2016-02-17 15:02:01 -0800841
Steve McKayab7865c2015-05-27 16:11:42 -0700842 }.execute(selected);
Ben Kwad99109f2015-03-31 10:11:43 -0700843 }
844
Steve McKaya6bbeab2016-02-17 15:02:01 -0800845 private static boolean hasDirectory(List<DocumentInfo> docs) {
846 for (DocumentInfo info : docs) {
847 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
848 return true;
849 }
850 }
851 return false;
852 }
853
Aga Wronska3b327ef2016-01-20 16:32:33 -0800854 private void renameDocuments(Selection selected) {
855 // Batch renaming not supported
856 // Rename option is only available in menu when 1 document selected
857 checkArgument(selected.size() == 1);
858
859 new GetDocumentsTask() {
860 @Override
861 void onDocumentsReady(List<DocumentInfo> docs) {
862 RenameDocumentFragment.show(getFragmentManager(), docs.get(0));
863 }
864 }.execute(selected);
865 }
866
Steve McKayef16f5f2015-12-22 18:15:31 -0800867 @Override
868 public void initDocumentHolder(DocumentHolder holder) {
Ben Kwa0436a752016-01-15 10:43:24 -0800869 holder.addEventListener(mItemEventListener);
Ben Kwa2036dad2016-02-10 07:46:35 -0800870 holder.itemView.setOnFocusChangeListener(mFocusManager);
Steve McKayef16f5f2015-12-22 18:15:31 -0800871 }
872
873 @Override
874 public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {
875 if (DEBUG_ENABLE_DND) {
876 setupDragAndDropOnDocumentView(holder.itemView, cursor);
877 }
878 }
879
880 @Override
881 public State getDisplayState() {
Steve McKayf69502c2015-11-09 17:38:35 +0900882 return ((BaseActivity) getActivity()).getDisplayState();
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700883 }
884
Steve McKayef16f5f2015-12-22 18:15:31 -0800885 @Override
886 public Model getModel() {
887 return mModel;
888 }
889
890 @Override
891 public boolean isDocumentEnabled(String docMimeType, int docFlags) {
892 return mTuner.isDocumentEnabled(docMimeType, docFlags);
893 }
894
Steve McKay9ed88a42016-01-21 18:46:15 -0800895 private void showEmptyDirectory() {
Ben Kwa0e9aae42016-02-04 16:35:27 -0800896 showEmptyView(R.string.empty, R.drawable.cabinet);
Ben Kwa91bec532015-09-16 13:15:38 -0700897 }
898
Steve McKay9ed88a42016-01-21 18:46:15 -0800899 private void showNoResults(RootInfo root) {
900 CharSequence msg = getContext().getResources().getText(R.string.no_results);
Ben Kwa0e9aae42016-02-04 16:35:27 -0800901 showEmptyView(String.format(String.valueOf(msg), root.title), R.drawable.cabinet);
Ben Kwa91bec532015-09-16 13:15:38 -0700902 }
903
Steve McKay9ed88a42016-01-21 18:46:15 -0800904 private void showQueryError() {
Ben Kwa0e9aae42016-02-04 16:35:27 -0800905 showEmptyView(R.string.query_error, R.drawable.hourglass);
Steve McKay9ed88a42016-01-21 18:46:15 -0800906 }
907
Ben Kwa0e9aae42016-02-04 16:35:27 -0800908 private void showEmptyView(@StringRes int id, int drawable) {
909 showEmptyView(getContext().getResources().getText(id), drawable);
Steve McKay9ed88a42016-01-21 18:46:15 -0800910 }
911
Ben Kwa0e9aae42016-02-04 16:35:27 -0800912 private void showEmptyView(CharSequence msg, int drawable) {
Steve McKay9ed88a42016-01-21 18:46:15 -0800913 View content = mEmptyView.findViewById(R.id.content);
914 TextView msgView = (TextView) mEmptyView.findViewById(R.id.message);
Ben Kwa0e9aae42016-02-04 16:35:27 -0800915 ImageView imageView = (ImageView) mEmptyView.findViewById(R.id.artwork);
Steve McKay9ed88a42016-01-21 18:46:15 -0800916 msgView.setText(msg);
Ben Kwa0e9aae42016-02-04 16:35:27 -0800917 imageView.setImageResource(drawable);
Steve McKay9ed88a42016-01-21 18:46:15 -0800918
Steve McKay9ed88a42016-01-21 18:46:15 -0800919 mEmptyView.setVisibility(View.VISIBLE);
Aga Wronskad4b17532016-02-12 09:15:32 -0800920 mEmptyView.requestFocus();
Steve McKay9ed88a42016-01-21 18:46:15 -0800921 mRecView.setVisibility(View.GONE);
Steve McKay9ed88a42016-01-21 18:46:15 -0800922 }
923
924 private void showDirectory() {
Ben Kwa91bec532015-09-16 13:15:38 -0700925 mEmptyView.setVisibility(View.GONE);
926 mRecView.setVisibility(View.VISIBLE);
Aga Wronskad4b17532016-02-12 09:15:32 -0800927 mRecView.requestFocus();
Ben Kwa91bec532015-09-16 13:15:38 -0700928 }
929
Jeff Sharkeyb5133112013-09-01 18:41:04 -0700930 private String findCommonMimeType(List<String> mimeTypes) {
931 String[] commonType = mimeTypes.get(0).split("/");
932 if (commonType.length != 2) {
933 return "*/*";
934 }
935
936 for (int i = 1; i < mimeTypes.size(); i++) {
937 String[] type = mimeTypes.get(i).split("/");
938 if (type.length != 2) continue;
939
940 if (!commonType[1].equals(type[1])) {
941 commonType[1] = "*";
942 }
943
944 if (!commonType[0].equals(type[0])) {
945 commonType[0] = "*";
946 commonType[1] = "*";
947 break;
948 }
949 }
950
951 return commonType[0] + "/" + commonType[1];
952 }
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700953
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700954 private void copyFromClipboard() {
955 new AsyncTask<Void, Void, List<DocumentInfo>>() {
956
957 @Override
958 protected List<DocumentInfo> doInBackground(Void... params) {
959 return mClipper.getClippedDocuments();
960 }
961
962 @Override
963 protected void onPostExecute(List<DocumentInfo> docs) {
964 DocumentInfo destination =
965 ((BaseActivity) getActivity()).getCurrentDirectory();
966 copyDocuments(docs, destination);
967 }
968 }.execute();
Steve McKay3da8afc2015-05-05 14:50:00 -0700969 }
970
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700971 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700972 checkNotNull(clipData);
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700973 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700974
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700975 @Override
976 protected List<DocumentInfo> doInBackground(Void... params) {
977 return mClipper.getDocumentsFromClipData(clipData);
978 }
979
980 @Override
981 protected void onPostExecute(List<DocumentInfo> docs) {
982 copyDocuments(docs, destination);
983 }
984 }.execute();
985 }
986
987 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
Steve McKay4a1ca862016-02-17 18:25:47 -0800988 BaseActivity activity = (BaseActivity) getActivity();
989 if (!canCopy(docs, activity.getCurrentRoot(), destination)) {
Steve McKay5bbae102015-10-01 11:39:24 -0700990 Snackbars.makeSnackbar(
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700991 getActivity(),
Ben Kwa94b486d2015-09-30 10:00:10 -0700992 R.string.clipboard_files_cannot_paste,
993 Snackbar.LENGTH_SHORT)
994 .show();
Steve McKay3da8afc2015-05-05 14:50:00 -0700995 return;
996 }
997
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700998 if (docs.isEmpty()) {
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700999 return;
Vladislav Kaznacheevd69d3c42015-05-05 12:09:47 -07001000 }
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -07001001
Steve McKayf69502c2015-11-09 17:38:35 +09001002 final DocumentStack curStack = getDisplayState().stack;
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -07001003 DocumentStack tmpStack = new DocumentStack();
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001004 if (destination != null) {
1005 tmpStack.push(destination);
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -07001006 tmpStack.addAll(curStack);
1007 } else {
1008 tmpStack = curStack;
1009 }
1010
Steve McKay14e827a2016-01-06 18:32:13 -08001011 FileOperations.copy(getActivity(), docs, tmpStack);
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -07001012 }
1013
1014 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1015 Context context = getActivity();
1016 final ContentResolver resolver = context.getContentResolver();
1017 ClipData clipData = null;
1018 for (DocumentInfo doc : docs) {
1019 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1020 if (clipData == null) {
Vladislav Kaznacheevd69d3c42015-05-05 12:09:47 -07001021 // TODO: figure out what this string should be.
1022 // Currently it is not displayed anywhere in the UI, but this might change.
1023 final String label = "";
1024 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -07001025 } else {
1026 // TODO: update list of mime types in ClipData.
1027 clipData.addItem(new ClipData.Item(uri));
1028 }
1029 }
1030 return clipData;
1031 }
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001032
Steve McKayf8621552015-11-03 15:23:16 -08001033 public void copySelectedToClipboard() {
Steve McKayd28c5c32015-12-07 16:31:42 -08001034 Selection selection = mSelectionManager.getSelection(new Selection());
1035 if (!selection.isEmpty()) {
1036 copySelectionToClipboard(selection);
Steve McKaycbee5442016-01-28 15:30:10 -08001037 mSelectionManager.clearSelection();
Steve McKayd28c5c32015-12-07 16:31:42 -08001038 }
Steve McKayab7865c2015-05-27 16:11:42 -07001039 }
Steve McKay3da8afc2015-05-05 14:50:00 -07001040
Steve McKayd28c5c32015-12-07 16:31:42 -08001041 void copySelectionToClipboard(Selection selection) {
1042 checkArgument(!selection.isEmpty());
Steve McKayab7865c2015-05-27 16:11:42 -07001043 new GetDocumentsTask() {
1044 @Override
1045 void onDocumentsReady(List<DocumentInfo> docs) {
1046 mClipper.clipDocuments(docs);
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001047 Activity activity = getActivity();
Steve McKay5bbae102015-10-01 11:39:24 -07001048 Snackbars.makeSnackbar(activity,
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001049 activity.getResources().getQuantityString(
1050 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
Ben Kwa862b96412015-12-07 13:25:27 -08001051 Snackbar.LENGTH_SHORT).show();
Steve McKayab7865c2015-05-27 16:11:42 -07001052 }
Steve McKayd28c5c32015-12-07 16:31:42 -08001053 }.execute(selection);
Steve McKay3da8afc2015-05-05 14:50:00 -07001054 }
1055
Steve McKayf8621552015-11-03 15:23:16 -08001056 public void pasteFromClipboard() {
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001057 copyFromClipboard();
1058 getActivity().invalidateOptionsMenu();
Steve McKay3da8afc2015-05-05 14:50:00 -07001059 }
1060
Steve McKay3da8afc2015-05-05 14:50:00 -07001061 /**
1062 * Returns true if the list of files can be copied to destination. Note that this
1063 * is a policy check only. Currently the method does not attempt to verify
1064 * available space or any other environmental aspects possibly resulting in
1065 * failure to copy.
1066 *
1067 * @return true if the list of files can be copied to destination.
1068 */
Steve McKay4a1ca862016-02-17 18:25:47 -08001069 private boolean canCopy(List<DocumentInfo> files, RootInfo root, DocumentInfo dest) {
1070 if (dest == null || !dest.isDirectory() || !dest.isCreateSupported()) {
1071 return false;
1072 }
Steve McKay3da8afc2015-05-05 14:50:00 -07001073
Steve McKay008e9482016-02-18 15:32:16 -08001074 // Can't copy folders to downloads, because we don't show folders there.
1075 if (!root.isDownloads()) {
Steve McKay3da8afc2015-05-05 14:50:00 -07001076 for (DocumentInfo docs : files) {
1077 if (docs.isDirectory()) {
1078 return false;
1079 }
1080 }
1081 }
1082
Steve McKay4a1ca862016-02-17 18:25:47 -08001083 return true;
Steve McKay3da8afc2015-05-05 14:50:00 -07001084 }
1085
Steve McKayf8621552015-11-03 15:23:16 -08001086 public void selectAllFiles() {
Ben Kwa862b96412015-12-07 13:25:27 -08001087 // Only select things currently visible in the adapter.
1088 boolean changed = mSelectionManager.setItemsSelected(mAdapter.getModelIds(), true);
Steve McKayb04b1642015-07-24 13:14:20 -07001089 if (changed) {
1090 updateDisplayState();
1091 }
Steve McKay3da8afc2015-05-05 14:50:00 -07001092 }
1093
Ben Kwa2036dad2016-02-10 07:46:35 -08001094 /**
1095 * Attempts to restore focus on the directory listing.
1096 */
1097 public void requestFocus() {
1098 mFocusManager.restoreLastFocus();
1099 }
1100
Steve McKay4b3a13c2015-06-11 10:10:49 -07001101 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001102 // Listen for drops on non-directory items and empty space.
1103 view.setOnDragListener(mOnDragListener);
1104 }
1105
1106 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1107 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1108 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1109 // Make a directory item a drop target. Drop on non-directories and empty space
1110 // is handled at the list/grid view level.
1111 view.setOnDragListener(mOnDragListener);
1112 }
1113
Steve McKay41585d52016-01-21 15:10:39 -08001114 view.setOnLongClickListener(mLongClickListener);
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001115 }
1116
1117 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1118 @Override
1119 public boolean onDrag(View v, DragEvent event) {
1120 switch (event.getAction()) {
1121 case DragEvent.ACTION_DRAG_STARTED:
1122 // TODO: Check if the event contains droppable data.
1123 return true;
1124
1125 // TODO: Highlight potential drop target directory?
1126 // TODO: Expand drop target directory on hover?
1127 case DragEvent.ACTION_DRAG_ENTERED:
1128 case DragEvent.ACTION_DRAG_LOCATION:
1129 case DragEvent.ACTION_DRAG_EXITED:
1130 case DragEvent.ACTION_DRAG_ENDED:
1131 return true;
1132
1133 case DragEvent.ACTION_DROP:
Ben Kwa003a9992015-11-30 23:00:02 -08001134 String dstId = getModelId(v);
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001135 DocumentInfo dstDir = null;
Ben Kwa003a9992015-11-30 23:00:02 -08001136 if (dstId != null) {
1137 Cursor dstCursor = mModel.getItem(dstId);
Steve McKay4b3a13c2015-06-11 10:10:49 -07001138 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001139 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1140 // TODO: Do not drop into the directory where the documents came from.
1141 }
1142 copyFromClipData(event.getClipData(), dstDir);
1143 return true;
1144 }
1145 return false;
1146 }
1147 };
1148
Ben Kwa003a9992015-11-30 23:00:02 -08001149 /**
1150 * Gets the model ID for a given motion event (using the event position)
1151 */
1152 private String getModelId(MotionEvent e) {
1153 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
1154 if (view == null) {
1155 return null;
1156 }
1157 RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(view);
1158 if (vh instanceof DocumentHolder) {
1159 return ((DocumentHolder) vh).modelId;
1160 } else {
1161 return null;
1162 }
1163 }
1164
1165 /**
1166 * Gets the model ID for a given RecyclerView item.
1167 * @param view A View that is a document item view, or a child of a document item view.
1168 * @return The Model ID for the given document, or null if the given view is not associated with
1169 * a document item view.
1170 */
1171 private String getModelId(View view) {
Vladislav Kaznacheevb5999ef2015-09-04 09:17:37 -07001172 while (true) {
1173 if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
Ben Kwa003a9992015-11-30 23:00:02 -08001174 RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(view);
1175 if (vh instanceof DocumentHolder) {
1176 return ((DocumentHolder) vh).modelId;
1177 } else {
1178 return null;
1179 }
Vladislav Kaznacheevb5999ef2015-09-04 09:17:37 -07001180 }
1181 ViewParent parent = view.getParent();
1182 if (parent == null || !(parent instanceof View)) {
1183 return null;
1184 }
1185 view = (View) parent;
1186 }
1187 }
1188
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001189 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Ben Kwa003a9992015-11-30 23:00:02 -08001190 String modelId = getModelId(currentItemView);
1191 if (modelId == null) {
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001192 return Collections.EMPTY_LIST;
1193 }
1194
Ben Kwa8250db42015-10-07 14:15:12 -07001195 final List<DocumentInfo> selectedDocs =
1196 mModel.getDocuments(mSelectionManager.getSelection());
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001197 if (!selectedDocs.isEmpty()) {
Ben Kwa003a9992015-11-30 23:00:02 -08001198 if (!isSelected(modelId)) {
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001199 // There is a selection that does not include the current item, drag nothing.
1200 return Collections.EMPTY_LIST;
1201 }
1202 return selectedDocs;
1203 }
1204
Ben Kwa003a9992015-11-30 23:00:02 -08001205 final Cursor cursor = mModel.getItem(modelId);
Steve McKay4b3a13c2015-06-11 10:10:49 -07001206 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001207 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKay58efce32015-08-20 16:19:38 +00001208
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001209 return Lists.newArrayList(doc);
1210 }
1211
1212 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1213 if (docs.size() == 1) {
1214 final DocumentInfo doc = docs.get(0);
Ben Kwad8391492015-12-17 10:37:00 -08001215 return mIconHelper.getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1216 doc.mimeType, doc.icon);
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001217 }
1218 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1219 }
1220
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001221 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1222
1223 private final Drawable mShadow;
1224
1225 private final int mShadowDimension;
1226
1227 public DrawableShadowBuilder(Drawable shadow) {
1228 mShadow = shadow;
1229 mShadowDimension = getResources().getDimensionPixelSize(
1230 R.dimen.drag_shadow_size);
1231 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1232 }
1233
Ben Kwac64cb252015-08-27 16:04:46 -07001234 @Override
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001235 public void onProvideShadowMetrics(
1236 Point shadowSize, Point shadowTouchPoint) {
1237 shadowSize.set(mShadowDimension, mShadowDimension);
1238 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1239 }
1240
Ben Kwac64cb252015-08-27 16:04:46 -07001241 @Override
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001242 public void onDrawShadow(Canvas canvas) {
1243 mShadow.draw(canvas);
1244 }
1245 }
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001246
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001247 /**
Steve McKayab7865c2015-05-27 16:11:42 -07001248 * Abstract task providing support for loading documents *off*
1249 * the main thread. And if it isn't obvious, creating a list
1250 * of documents (especially large lists) can be pretty expensive.
1251 */
1252 private abstract class GetDocumentsTask
Steve McKay4b3a13c2015-06-11 10:10:49 -07001253 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKayab7865c2015-05-27 16:11:42 -07001254 @Override
Steve McKay4b3a13c2015-06-11 10:10:49 -07001255 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwac64cb252015-08-27 16:04:46 -07001256 return mModel.getDocuments(selected[0]);
Steve McKayab7865c2015-05-27 16:11:42 -07001257 }
1258
1259 @Override
1260 protected final void onPostExecute(List<DocumentInfo> docs) {
1261 onDocumentsReady(docs);
1262 }
1263
1264 abstract void onDocumentsReady(List<DocumentInfo> docs);
1265 }
1266
Steve McKayef16f5f2015-12-22 18:15:31 -08001267 @Override
1268 public boolean isSelected(String modelId) {
Ben Kwa743c7c22015-12-01 19:56:57 -08001269 return mSelectionManager.getSelection().contains(modelId);
Ben Kwa91caed82015-09-21 10:49:52 -07001270 }
Ben Kwa18fce3c2015-09-01 11:03:01 -07001271
Ben Kwa0436a752016-01-15 10:43:24 -08001272 private class ItemEventListener implements DocumentHolder.EventListener {
Ben Kwabd964562015-10-14 08:00:27 -07001273 @Override
Ben Kwa0436a752016-01-15 10:43:24 -08001274 public boolean onActivate(DocumentHolder doc) {
Ben Kwa67924892016-01-27 09:58:36 -08001275 // Toggle selection if we're in selection mode, othewise, view item.
1276 if (mSelectionManager.hasSelection()) {
1277 mSelectionManager.toggleSelection(doc.modelId);
1278 } else {
1279 handleViewItem(doc.modelId);
1280 }
Ben Kwa0436a752016-01-15 10:43:24 -08001281 return true;
1282 }
1283
1284 @Override
1285 public boolean onSelect(DocumentHolder doc) {
1286 mSelectionManager.toggleSelection(doc.modelId);
1287 mSelectionManager.setSelectionRangeBegin(doc.getAdapterPosition());
1288 return true;
Ben Kwabd964562015-10-14 08:00:27 -07001289 }
Ben Kwa67924892016-01-27 09:58:36 -08001290
1291 @Override
1292 public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event) {
1293 // Only handle key-down events. This is simpler, consistent with most other UIs, and
1294 // enables the handling of repeated key events from holding down a key.
1295 if (event.getAction() != KeyEvent.ACTION_DOWN) {
1296 return false;
1297 }
1298
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001299 if (mFocusManager.handleKey(doc, keyCode, event)) {
Ben Kwa83df50f2016-02-10 14:01:19 -08001300 // Handle range selection adjustments. Extending the selection will adjust the
1301 // bounds of the in-progress range selection. Each time an unshifted navigation
1302 // event is received, the range selection is restarted.
1303 if (shouldExtendSelection(event)) {
1304 if (!mSelectionManager.isRangeSelectionActive()) {
1305 // Start a range selection if one isn't active
1306 mSelectionManager.startRangeSelection(doc.getAdapterPosition());
1307 }
1308 mSelectionManager.snapRangeSelection(mFocusManager.getFocusPosition());
1309 } else {
1310 mSelectionManager.endRangeSelection();
1311 }
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001312 return true;
Ben Kwa67924892016-01-27 09:58:36 -08001313 }
1314
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001315 // Handle enter key events
1316 if (keyCode == KeyEvent.KEYCODE_ENTER) {
Ben Kwa09eb77b2016-02-10 08:57:25 -08001317 if (event.isShiftPressed()) {
1318 return onSelect(doc);
1319 } else {
1320 return onActivate(doc);
1321 }
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001322 }
1323
1324 return false;
Ben Kwa67924892016-01-27 09:58:36 -08001325 }
Ben Kwa83df50f2016-02-10 14:01:19 -08001326
1327 private boolean shouldExtendSelection(KeyEvent event) {
1328 return Events.isNavigationKeyCode(event.getKeyCode()) &&
1329 event.isShiftPressed();
1330 }
Ben Kwabd964562015-10-14 08:00:27 -07001331 }
1332
Steve McKayef16f5f2015-12-22 18:15:31 -08001333 private final class ModelUpdateListener implements Model.UpdateListener {
Ben Kwa91caed82015-09-21 10:49:52 -07001334 @Override
1335 public void onModelUpdate(Model model) {
1336 if (model.info != null || model.error != null) {
1337 mMessageBar.setInfo(model.info);
1338 mMessageBar.setError(model.error);
1339 mMessageBar.show();
1340 }
Ben Kwa18fce3c2015-09-01 11:03:01 -07001341
Ben Kwa91caed82015-09-21 10:49:52 -07001342 mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE);
1343
1344 if (model.isEmpty()) {
Steve McKay9ed88a42016-01-21 18:46:15 -08001345 if (getDisplayState().currentSearch != null) {
1346 showNoResults(getDisplayState().stack.root);
1347 } else {
1348 showEmptyDirectory();
1349 }
Ben Kwa91caed82015-09-21 10:49:52 -07001350 } else {
Steve McKay9ed88a42016-01-21 18:46:15 -08001351 showDirectory();
Ben Kwa91bec532015-09-16 13:15:38 -07001352 mAdapter.notifyDataSetChanged();
Ben Kwa91caed82015-09-21 10:49:52 -07001353 }
Ben Kwa91caed82015-09-21 10:49:52 -07001354 }
1355
1356 @Override
1357 public void onModelUpdateFailed(Exception e) {
Steve McKay9ed88a42016-01-21 18:46:15 -08001358 showQueryError();
Ben Kwa18fce3c2015-09-01 11:03:01 -07001359 }
Ben Kwac64cb252015-08-27 16:04:46 -07001360 }
Ben Kwa0436a752016-01-15 10:43:24 -08001361
Steve McKay41585d52016-01-21 15:10:39 -08001362 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1363 @Override
1364 public boolean onLongClick(View v) {
1365 if (mGestureDetector.mouseSpawnedLastEvent()) {
1366 List<DocumentInfo> docs = getDraggableDocuments(v);
1367 if (docs.isEmpty()) {
1368 return false;
1369 }
1370 v.startDrag(
1371 getClipDataFromDocuments(docs),
1372 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1373 null,
1374 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
1375 View.DRAG_FLAG_GLOBAL_URI_WRITE
1376 );
1377 return true;
1378 }
1379
1380 return false;
1381 }
1382 };
1383
1384 // Previously we listened to events with one class, only to bounce them forward
1385 // to GestureDetector. We're still doing that here, but with a single class
1386 // that reduces overall complexity in our glue code.
1387 private static final class ListeningGestureDetector extends GestureDetector
1388 implements OnItemTouchListener {
1389
1390 private int mLastTool = -1;
1391
1392 public ListeningGestureDetector(Context context, GestureListener listener) {
1393 super(context, listener);
1394 setOnDoubleTapListener(listener);
1395 }
1396
1397 boolean mouseSpawnedLastEvent() {
1398 return Events.isMouseType(mLastTool);
1399 }
1400
1401 boolean touchSpawnedLastEvent() {
1402 return Events.isTouchType(mLastTool);
1403 }
1404
1405 @Override
1406 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
1407 mLastTool = e.getToolType(0);
1408 onTouchEvent(e); // bounce this forward to our detecty heart
1409 return false;
1410 }
1411
1412 @Override
1413 public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
1414
1415 @Override
1416 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
1417 }
1418
Ben Kwa0436a752016-01-15 10:43:24 -08001419 /**
1420 * The gesture listener for items in the list/grid view. Interprets gestures and sends the
1421 * events to the target DocumentHolder, whence they are routed to the appropriate listener.
1422 */
1423 private class GestureListener extends GestureDetector.SimpleOnGestureListener {
1424 @Override
1425 public boolean onSingleTapUp(MotionEvent e) {
1426 // Single tap logic:
1427 // If the selection manager is active, it gets first whack at handling tap
1428 // events. Otherwise, tap events are routed to the target DocumentHolder.
1429 boolean handled = mSelectionManager.onSingleTapUp(
1430 new MotionInputEvent(e, mRecView));
1431
1432 if (handled) {
1433 return handled;
1434 }
1435
1436 // Give the DocumentHolder a crack at the event.
1437 DocumentHolder holder = getTarget(e);
1438 if (holder != null) {
1439 handled = holder.onSingleTapUp(e);
1440 }
1441
1442 return handled;
1443 }
1444
1445 @Override
1446 public void onLongPress(MotionEvent e) {
1447 // Long-press events get routed directly to the selection manager. They can be
1448 // changed to route through the DocumentHolder if necessary.
1449 mSelectionManager.onLongPress(new MotionInputEvent(e, mRecView));
1450 }
1451
1452 @Override
1453 public boolean onDoubleTap(MotionEvent e) {
1454 // Double-tap events are handled directly by the DirectoryFragment. They can be changed
1455 // to route through the DocumentHolder if necessary.
1456 return DirectoryFragment.this.onDoubleTap(e);
1457 }
1458
1459 private @Nullable DocumentHolder getTarget(MotionEvent e) {
1460 View childView = mRecView.findChildViewUnder(e.getX(), e.getY());
1461 if (childView != null) {
1462 return (DocumentHolder) mRecView.getChildViewHolder(childView);
1463 } else {
1464 return null;
1465 }
1466 }
1467 }
Steve McKay9ed88a42016-01-21 18:46:15 -08001468
1469 public static void showDirectory(
1470 FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
1471 show(fm, TYPE_NORMAL, root, doc, null, anim);
1472 }
1473
1474 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
1475 show(fm, TYPE_SEARCH, root, null, query, anim);
1476 }
1477
1478 public static void showRecentsOpen(FragmentManager fm, int anim) {
1479 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
1480 }
1481
1482 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
1483 String query, int anim) {
1484 final Bundle args = new Bundle();
1485 args.putInt(EXTRA_TYPE, type);
1486 args.putParcelable(EXTRA_ROOT, root);
1487 args.putParcelable(EXTRA_DOC, doc);
1488 args.putString(EXTRA_QUERY, query);
1489
1490 final FragmentTransaction ft = fm.beginTransaction();
1491 switch (anim) {
1492 case ANIM_SIDE:
1493 args.putBoolean(EXTRA_IGNORE_STATE, true);
1494 break;
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +09001495 case ANIM_ENTER:
Steve McKay9ed88a42016-01-21 18:46:15 -08001496 args.putBoolean(EXTRA_IGNORE_STATE, true);
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +09001497 ft.setCustomAnimations(R.animator.dir_enter, R.animator.dir_frozen);
Steve McKay9ed88a42016-01-21 18:46:15 -08001498 break;
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +09001499 case ANIM_LEAVE:
1500 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_leave);
Steve McKay9ed88a42016-01-21 18:46:15 -08001501 break;
1502 }
1503
1504 final DirectoryFragment fragment = new DirectoryFragment();
1505 fragment.setArguments(args);
1506
1507 ft.replace(R.id.container_directory, fragment);
1508 ft.commitAllowingStateLoss();
1509 }
1510
1511 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
1512 final StringBuilder builder = new StringBuilder();
1513 builder.append(root != null ? root.authority : "null").append(';');
1514 builder.append(root != null ? root.rootId : "null").append(';');
1515 builder.append(doc != null ? doc.documentId : "null");
1516 return builder.toString();
1517 }
1518
1519 public static @Nullable DirectoryFragment get(FragmentManager fm) {
1520 // TODO: deal with multiple directories shown at once
1521 Fragment fragment = fm.findFragmentById(R.id.container_directory);
1522 return fragment instanceof DirectoryFragment
1523 ? (DirectoryFragment) fragment
1524 : null;
1525 }
Jeff Sharkey9e0036e2013-04-26 16:54:55 -07001526}