blob: ca1b444b3531ba096792140858d58cf3e18b86f0 [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.MODE_GRID;
21import static com.android.documentsui.State.MODE_LIST;
Steve McKay7a3b88c2015-09-23 17:21:40 -070022import static com.android.documentsui.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070023import static com.android.documentsui.model.DocumentInfo.getCursorInt;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070024import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKay3da8afc2015-05-05 14:50:00 -070025
Steve McKayaa15dae2016-02-09 16:17:24 -080026import android.annotation.IntDef;
Steve McKay9ed88a42016-01-21 18:46:15 -080027import android.annotation.StringRes;
Ben Kwaef3f2622015-04-07 15:43:39 -070028import android.app.Activity;
Jeff Sharkeyd01571e2013-10-01 17:57:41 -070029import android.app.ActivityManager;
Steve McKay7a3b8112016-02-23 10:06:50 -080030import android.app.AlertDialog;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070031import android.app.Fragment;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070032import android.app.FragmentManager;
33import android.app.FragmentTransaction;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070034import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -070035import android.content.ClipData;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070036import android.content.Context;
Steve McKay7a3b8112016-02-23 10:06:50 -080037import android.content.DialogInterface;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -070038import android.content.Intent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070039import android.content.Loader;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070040import android.database.Cursor;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -070041import android.graphics.Canvas;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070042import android.graphics.Point;
Jeff Sharkey4ec97392013-09-10 12:04:26 -070043import android.graphics.drawable.Drawable;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070044import android.net.Uri;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070045import android.os.AsyncTask;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070046import android.os.Bundle;
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -070047import android.os.Parcelable;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070048import android.provider.DocumentsContract;
Jeff Sharkey5dfb3452013-08-31 21:27:44 -070049import android.provider.DocumentsContract.Document;
Ben Kwac64cb252015-08-27 16:04:46 -070050import android.support.annotation.Nullable;
Ben Kwa304895a2015-08-27 16:06:33 -070051import android.support.design.widget.Snackbar;
Ben Kwae3dfebf2016-02-18 16:45:45 -080052import android.support.v13.view.DragStartHelper;
Steve McKay4b3a13c2015-06-11 10:10:49 -070053import android.support.v7.widget.GridLayoutManager;
Steve McKayef16f5f2015-12-22 18:15:31 -080054import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;
Steve McKay4b3a13c2015-06-11 10:10:49 -070055import android.support.v7.widget.RecyclerView;
Steve McKay669ebe72015-10-19 12:04:21 -070056import android.support.v7.widget.RecyclerView.OnItemTouchListener;
Steve McKay4b3a13c2015-06-11 10:10:49 -070057import android.support.v7.widget.RecyclerView.RecyclerListener;
58import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkeyf491c722015-06-11 09:16:19 -070059import android.text.TextUtils;
Jeff Sharkey9d0843d2013-05-07 12:41:33 -070060import android.util.Log;
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -070061import android.util.SparseArray;
Ben Kwa0f7078f02015-09-08 07:31:19 -070062import android.util.TypedValue;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -070063import android.view.ActionMode;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -070064import android.view.DragEvent;
Steve McKay4b3a13c2015-06-11 10:10:49 -070065import android.view.GestureDetector;
Aga Wronska7d5a2472016-03-08 17:56:28 -080066import android.view.HapticFeedbackConstants;
Ben Kwa67924892016-01-27 09:58:36 -080067import android.view.KeyEvent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070068import android.view.LayoutInflater;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070069import android.view.Menu;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070070import android.view.MenuItem;
Steve McKay4b3a13c2015-06-11 10:10:49 -070071import android.view.MotionEvent;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070072import android.view.View;
73import android.view.ViewGroup;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070074import android.widget.ImageView;
Jeff Sharkey9e0036e2013-04-26 16:54:55 -070075import android.widget.TextView;
76
Steve McKayf8621552015-11-03 15:23:16 -080077import com.android.documentsui.BaseActivity;
Steve McKayf8621552015-11-03 15:23:16 -080078import com.android.documentsui.DirectoryLoader;
79import com.android.documentsui.DirectoryResult;
80import com.android.documentsui.DocumentClipper;
81import com.android.documentsui.DocumentsActivity;
82import com.android.documentsui.DocumentsApplication;
83import com.android.documentsui.Events;
Ben Kwa0436a752016-01-15 10:43:24 -080084import com.android.documentsui.Events.MotionInputEvent;
Steve McKayf8621552015-11-03 15:23:16 -080085import com.android.documentsui.Menus;
86import com.android.documentsui.MessageBar;
87import com.android.documentsui.MimePredicate;
Steve McKayf8621552015-11-03 15:23:16 -080088import com.android.documentsui.R;
Steve McKaye2af0782016-02-18 09:48:39 -080089import com.android.documentsui.RecentsLoader;
Steve McKayf8621552015-11-03 15:23:16 -080090import com.android.documentsui.RootsCache;
91import com.android.documentsui.Shared;
92import com.android.documentsui.Snackbars;
93import com.android.documentsui.State;
Ben Kwa1e2fa5e2016-02-02 23:00:02 -080094import com.android.documentsui.State.ViewMode;
Steve McKayf8621552015-11-03 15:23:16 -080095import com.android.documentsui.dirlist.MultiSelectManager.Selection;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070096import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewskif8c3f322015-04-14 16:32:41 +090097import com.android.documentsui.model.DocumentStack;
Jeff Sharkey251097b2013-09-02 15:07:28 -070098import com.android.documentsui.model.RootInfo;
Steve McKay14e827a2016-01-06 18:32:13 -080099import com.android.documentsui.services.FileOperationService;
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800100import com.android.documentsui.services.FileOperationService.OpType;
Steve McKay14e827a2016-01-06 18:32:13 -0800101import com.android.documentsui.services.FileOperations;
Steve McKaya1f76802016-02-25 13:34:03 -0800102
Steve McKay58efce32015-08-20 16:19:38 +0000103import com.google.common.collect.Lists;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700104
Steve McKayaa15dae2016-02-09 16:17:24 -0800105import java.lang.annotation.Retention;
106import java.lang.annotation.RetentionPolicy;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700107import java.util.ArrayList;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -0700108import java.util.Collections;
Jeff Sharkeyef7184a2013-08-05 17:56:48 -0700109import java.util.List;
Ben Kwa990489d2016-02-25 14:10:40 -0800110import java.util.Objects;
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700111
112/**
113 * Display the documents inside a single directory.
114 */
Aga Wronska893390b2016-02-17 13:50:42 -0800115public class DirectoryFragment extends Fragment
116 implements DocumentsAdapter.Environment, LoaderCallbacks<DirectoryResult> {
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700117
Steve McKayaa15dae2016-02-09 16:17:24 -0800118 @IntDef(flag = true, value = {
119 TYPE_NORMAL,
Steve McKayaa15dae2016-02-09 16:17:24 -0800120 TYPE_RECENT_OPEN
121 })
122 @Retention(RetentionPolicy.SOURCE)
123 public @interface ResultType {}
Jeff Sharkeyef7184a2013-08-05 17:56:48 -0700124 public static final int TYPE_NORMAL = 1;
Aga Wronska893390b2016-02-17 13:50:42 -0800125 public static final int TYPE_RECENT_OPEN = 2;
Jeff Sharkeydc2963a2013-08-02 15:55:26 -0700126
Aga Wronska893390b2016-02-17 13:50:42 -0800127 @IntDef(flag = true, value = {
128 ANIM_NONE,
129 ANIM_SIDE,
130 ANIM_LEAVE,
131 ANIM_ENTER
132 })
133 @Retention(RetentionPolicy.SOURCE)
134 public @interface AnimationType {}
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700135 public static final int ANIM_NONE = 1;
136 public static final int ANIM_SIDE = 2;
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +0900137 public static final int ANIM_LEAVE = 3;
138 public static final int ANIM_ENTER = 4;
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700139
Steve McKaya6bbeab2016-02-17 15:02:01 -0800140 @IntDef(flag = true, value = {
141 REQUEST_COPY_DESTINATION
142 })
143 @Retention(RetentionPolicy.SOURCE)
144 public @interface RequestCode {}
Ben Kwaef3f2622015-04-07 15:43:39 -0700145 public static final int REQUEST_COPY_DESTINATION = 1;
146
Steve McKay35645432016-01-20 15:09:35 -0800147 private static final String TAG = "DirectoryFragment";
148 private static final int LOADER_ID = 42;
Steve McKay35645432016-01-20 15:09:35 -0800149
Ben Kwa18fce3c2015-09-01 11:03:01 -0700150 private Model mModel;
Ben Kwa8250db42015-10-07 14:15:12 -0700151 private MultiSelectManager mSelectionManager;
Ben Kwa91caed82015-09-21 10:49:52 -0700152 private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
Ben Kwa0436a752016-01-15 10:43:24 -0800153 private ItemEventListener mItemEventListener = new ItemEventListener();
Ben Kwab0bfe2d2016-02-09 11:27:45 -0800154 private FocusManager mFocusManager;
Ben Kwac64cb252015-08-27 16:04:46 -0700155
Ben Kwad8391492015-12-17 10:37:00 -0800156 private IconHelper mIconHelper;
157
Steve McKay4b3a13c2015-06-11 10:10:49 -0700158 private View mEmptyView;
159 private RecyclerView mRecView;
Steve McKay41585d52016-01-21 15:10:39 -0800160 private ListeningGestureDetector mGestureDetector;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700161
Steve McKay4b3a13c2015-06-11 10:10:49 -0700162 private String mStateKey;
163
Steve McKay4b3a13c2015-06-11 10:10:49 -0700164 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700165 private DocumentsAdapter mAdapter;
Steve McKayf69502c2015-11-09 17:38:35 +0900166 private FragmentTuner mTuner;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700167 private DocumentClipper mClipper;
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800168 private GridLayoutManager mLayout;
Steve McKay0ba3a142015-07-23 16:33:41 -0700169 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700170
Steve McKayc8d4e222016-03-01 08:28:02 -0800171 private LayoutInflater mInflater;
Ben Kwa3b19e312015-09-16 08:04:37 -0700172 private MessageBar mMessageBar;
Ben Kwa91caed82015-09-21 10:49:52 -0700173 private View mProgressBar;
Ben Kwa3b19e312015-09-16 08:04:37 -0700174
Aga Wronska893390b2016-02-17 13:50:42 -0800175 // Directory fragment state is defined by: root, document, query, type, selection
176 private @ResultType int mType = TYPE_NORMAL;
177 private RootInfo mRoot;
178 private DocumentInfo mDocument;
179 private String mQuery = null;
180 private Selection mSelection = null;
181 private boolean mSearchMode = false;
182
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700183 @Override
184 public View onCreateView(
185 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Steve McKayc8d4e222016-03-01 08:28:02 -0800186 mInflater = inflater;
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700187 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
188
Ben Kwa3b19e312015-09-16 08:04:37 -0700189 mMessageBar = MessageBar.create(getChildFragmentManager());
Ben Kwa91caed82015-09-21 10:49:52 -0700190 mProgressBar = view.findViewById(R.id.progressbar);
Jeff Sharkey9fb567b2013-08-07 16:22:02 -0700191 mEmptyView = view.findViewById(android.R.id.empty);
Ben Kwa2036dad2016-02-10 07:46:35 -0800192 mRecView = (RecyclerView) view.findViewById(R.id.dir_list);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700193 mRecView.setRecyclerListener(
194 new RecyclerListener() {
195 @Override
196 public void onViewRecycled(ViewHolder holder) {
197 cancelThumbnailTask(holder.itemView);
198 }
199 });
Steve McKayb46383b2015-05-06 14:27:57 -0700200
Ben Kwae48e4ca2015-10-20 15:02:33 -0700201 mRecView.setItemAnimator(new DirectoryItemAnimator(getActivity()));
Ben Kwabd964562015-10-14 08:00:27 -0700202
Ben Kwa990489d2016-02-25 14:10:40 -0800203 // Make the recycler and the empty views responsive to drop events.
204 mRecView.setOnDragListener(mOnDragListener);
205 mEmptyView.setOnDragListener(mOnDragListener);
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() {
Aga Wronskad5597432016-02-26 11:36:07 -0800212 mSelectionManager.clearSelection();
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700213
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 }
Aga Wronskad5597432016-02-26 11:36:07 -0800220
221 super.onDestroyView();
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700222 }
223
224 @Override
Jeff Sharkey5dfb3452013-08-31 21:27:44 -0700225 public void onActivityCreated(Bundle savedInstanceState) {
226 super.onActivityCreated(savedInstanceState);
227
228 final Context context = getActivity();
Steve McKayf69502c2015-11-09 17:38:35 +0900229 final State state = getDisplayState();
Jeff Sharkey5dfb3452013-08-31 21:27:44 -0700230
Aga Wronska893390b2016-02-17 13:50:42 -0800231 // Read arguments when object created for the first time.
232 // Restore state if fragment recreated.
233 Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
234 mRoot = args.getParcelable(Shared.EXTRA_ROOT);
235 mDocument = args.getParcelable(Shared.EXTRA_DOC);
236 mStateKey = buildStateKey(mRoot, mDocument);
237 mQuery = args.getString(Shared.EXTRA_QUERY);
238 mType = args.getInt(Shared.EXTRA_TYPE);
239 mSelection = args.getParcelable(Shared.EXTRA_SELECTION);
240 mSearchMode = args.getBoolean(Shared.EXTRA_SEARCH_MODE);
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700241
Steve McKay3eb2d072016-01-25 19:00:22 -0800242 mIconHelper = new IconHelper(context, MODE_GRID);
Steve McKayef16f5f2015-12-22 18:15:31 -0800243
244 mAdapter = new SectionBreakDocumentsAdapterWrapper(
245 this, new ModelBackedDocumentsAdapter(this, mIconHelper));
246
Steve McKay4b3a13c2015-06-11 10:10:49 -0700247 mRecView.setAdapter(mAdapter);
248
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800249 mLayout = new GridLayoutManager(getContext(), mColumnCount);
250 SpanSizeLookup lookup = mAdapter.createSpanSizeLookup();
251 if (lookup != null) {
252 mLayout.setSpanSizeLookup(lookup);
253 }
254 mRecView.setLayoutManager(mLayout);
255
Ben Kwae3dfebf2016-02-18 16:45:45 -0800256 mGestureDetector =
257 new ListeningGestureDetector(this.getContext(), mDragHelper, new GestureListener());
Steve McKay669ebe72015-10-19 12:04:21 -0700258
Steve McKay41585d52016-01-21 15:10:39 -0800259 mRecView.addOnItemTouchListener(mGestureDetector);
Steve McKay669ebe72015-10-19 12:04:21 -0700260
Ben Kwac64cb252015-08-27 16:04:46 -0700261 // TODO: instead of inserting the view into the constructor, extract listener-creation code
262 // 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 -0700263 // into the selection manager.
264 mSelectionManager = new MultiSelectManager(
Steve McKaydbec47a2015-08-12 14:48:34 -0700265 mRecView,
Steve McKay44408262016-01-05 15:27:17 -0800266 mAdapter,
Steve McKaydbec47a2015-08-12 14:48:34 -0700267 state.allowMultiple
268 ? MultiSelectManager.MODE_MULTIPLE
Steve McKaye852d932016-02-08 19:09:42 -0800269 : MultiSelectManager.MODE_SINGLE,
Aga Wronska893390b2016-02-17 13:50:42 -0800270 null);
Steve McKaye852d932016-02-08 19:09:42 -0800271
Aga Wronska893390b2016-02-17 13:50:42 -0800272 mSelectionManager.addCallback(new SelectionModeListener());
Ben Kwa18fce3c2015-09-01 11:03:01 -0700273
Steve McKay35645432016-01-20 15:09:35 -0800274 mModel = new Model();
Ben Kwa743c7c22015-12-01 19:56:57 -0800275 mModel.addUpdateListener(mAdapter);
Ben Kwa91caed82015-09-21 10:49:52 -0700276 mModel.addUpdateListener(mModelUpdateListener);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700277
Ben Kwaa4acc902016-02-10 15:48:25 -0800278 // Make sure this is done after the RecyclerView is set up.
279 mFocusManager = new FocusManager(context, mRecView, mModel);
280
Steve McKayaa15dae2016-02-09 16:17:24 -0800281 mTuner = FragmentTuner.pick(getContext(), state);
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700282 mClipper = new DocumentClipper(context);
283
Ben Kwad8391492015-12-17 10:37:00 -0800284 boolean hideGridTitles;
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700285 if (mType == TYPE_RECENT_OPEN) {
286 // Hide titles when showing recents for picking images/videos
Ben Kwad8391492015-12-17 10:37:00 -0800287 hideGridTitles = MimePredicate.mimeMatches(
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700288 MimePredicate.VISUAL_MIMES, state.acceptMimes);
289 } else {
Aga Wronska893390b2016-02-17 13:50:42 -0800290 hideGridTitles = (mDocument != null) && mDocument.isGridTitlesHidden();
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700291 }
Ben Kwad8391492015-12-17 10:37:00 -0800292 GridDocumentHolder.setHideTitles(hideGridTitles);
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700293
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700294 final ActivityManager am = (ActivityManager) context.getSystemService(
295 Context.ACTIVITY_SERVICE);
Ben Kwad8391492015-12-17 10:37:00 -0800296 boolean svelte = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
297 mIconHelper.setThumbnailsEnabled(!svelte);
Jeff Sharkeyd01571e2013-10-01 17:57:41 -0700298
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700299 // Kick off loader at least once
Aga Wronska893390b2016-02-17 13:50:42 -0800300 getLoaderManager().restartLoader(LOADER_ID, null, this);
Jeff Sharkey9e0036e2013-04-26 16:54:55 -0700301 }
302
Jeff Sharkey28c05ee2013-09-06 13:22:09 -0700303 @Override
Steve McKaye852d932016-02-08 19:09:42 -0800304 public void onSaveInstanceState(Bundle outState) {
Aga Wronska893390b2016-02-17 13:50:42 -0800305 super.onSaveInstanceState(outState);
306
Aga Wronskad5597432016-02-26 11:36:07 -0800307 mSelectionManager.getSelection(mSelection);
308
Aga Wronska893390b2016-02-17 13:50:42 -0800309 outState.putInt(Shared.EXTRA_TYPE, mType);
310 outState.putParcelable(Shared.EXTRA_ROOT, mRoot);
311 outState.putParcelable(Shared.EXTRA_DOC, mDocument);
312 outState.putString(Shared.EXTRA_QUERY, mQuery);
Aga Wronskad5597432016-02-26 11:36:07 -0800313 outState.putParcelable(Shared.EXTRA_SELECTION, mSelection);
Aga Wronska893390b2016-02-17 13:50:42 -0800314 outState.putBoolean(Shared.EXTRA_SEARCH_MODE, mSearchMode);
Aga Wronskad5597432016-02-26 11:36:07 -0800315
Steve McKaye852d932016-02-08 19:09:42 -0800316 }
317
318 @Override
Steve McKaya6bbeab2016-02-17 15:02:01 -0800319 public void onActivityResult(@RequestCode int requestCode, int resultCode, Intent data) {
320 switch(requestCode) {
321 case REQUEST_COPY_DESTINATION:
322 handleCopyResult(resultCode, data);
323 break;
324 default:
325 throw new UnsupportedOperationException("Unknown request code: " + requestCode);
Ben Kwaef3f2622015-04-07 15:43:39 -0700326 }
Steve McKaya6bbeab2016-02-17 15:02:01 -0800327 }
328
329 private void handleCopyResult(int resultCode, Intent data) {
Ben Kwaef3f2622015-04-07 15:43:39 -0700330 if (resultCode == Activity.RESULT_CANCELED || data == null) {
331 // User pressed the back button or otherwise cancelled the destination pick. Don't
332 // proceed with the copy.
333 return;
334 }
335
Steve McKaya6bbeab2016-02-17 15:02:01 -0800336 @OpType int operationType = data.getIntExtra(
Steve McKay14e827a2016-01-06 18:32:13 -0800337 FileOperationService.EXTRA_OPERATION,
338 FileOperationService.OPERATION_COPY);
339
340 FileOperations.start(
341 getActivity(),
342 getDisplayState().selectedDocumentsForCopy,
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900343 getDisplayState().stack.peek(),
Steve McKay323ee3e2015-09-25 16:02:56 -0700344 (DocumentStack) data.getParcelableExtra(Shared.EXTRA_STACK),
Steve McKay14e827a2016-01-06 18:32:13 -0800345 operationType);
Ben Kwaef3f2622015-04-07 15:43:39 -0700346 }
347
Steve McKay5353a1e2015-07-30 12:27:44 -0700348 protected boolean onDoubleTap(MotionEvent e) {
349 if (Events.isMouseEvent(e)) {
Ben Kwa003a9992015-11-30 23:00:02 -0800350 String id = getModelId(e);
351 if (id != null) {
352 return handleViewItem(id);
Steve McKay5353a1e2015-07-30 12:27:44 -0700353 }
354 }
355 return false;
356 }
357
Ben Kwa003a9992015-11-30 23:00:02 -0800358 private boolean handleViewItem(String id) {
359 final Cursor cursor = mModel.getItem(id);
Steve McKaya1f76802016-02-25 13:34:03 -0800360
361 assert(cursor != null);
362
Steve McKay5353a1e2015-07-30 12:27:44 -0700363 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
364 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayf69502c2015-11-09 17:38:35 +0900365 if (mTuner.isDocumentEnabled(docMimeType, docFlags)) {
Steve McKay5353a1e2015-07-30 12:27:44 -0700366 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwac64cb252015-08-27 16:04:46 -0700367 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
Ben Kwa8250db42015-10-07 14:15:12 -0700368 mSelectionManager.clearSelection();
Steve McKay5353a1e2015-07-30 12:27:44 -0700369 return true;
370 }
Steve McKay4b3a13c2015-06-11 10:10:49 -0700371 return false;
372 }
373
Ben Kwaef3f2622015-04-07 15:43:39 -0700374 @Override
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700375 public void onStop() {
376 super.onStop();
377
378 // Remember last scroll location
379 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
380 getView().saveHierarchyState(container);
Steve McKayf69502c2015-11-09 17:38:35 +0900381 final State state = getDisplayState();
Jeff Sharkeye20a3ac2013-09-18 16:26:49 -0700382 state.dirState.put(mStateKey, container);
383 }
384
Jeff Sharkey669f8e72014-08-08 15:10:03 -0700385 public void onDisplayStateChanged() {
386 updateDisplayState();
387 }
388
Steve McKay3eb2d072016-01-25 19:00:22 -0800389 public void onSortOrderChanged() {
390 // Sort order is implemented as a sorting wrapper around directory
391 // results. So when sort order changes, we force a reload of the directory.
Aga Wronska893390b2016-02-17 13:50:42 -0800392 getLoaderManager().restartLoader(LOADER_ID, null, this);
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700393 }
394
Steve McKay3eb2d072016-01-25 19:00:22 -0800395 public void onViewModeChanged() {
396 // Mode change is just visual change; no need to kick loader.
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700397 updateDisplayState();
398 }
399
400 private void updateDisplayState() {
Steve McKay3eb2d072016-01-25 19:00:22 -0800401 State state = getDisplayState();
Steve McKay4b3a13c2015-06-11 10:10:49 -0700402 updateLayout(state.derivedMode);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700403 mRecView.setAdapter(mAdapter);
404 }
405
406 /**
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800407 * Updates the layout after the view mode switches.
408 * @param mode The new view mode.
Steve McKay4b3a13c2015-06-11 10:10:49 -0700409 */
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800410 private void updateLayout(@ViewMode int mode) {
411 mColumnCount = calculateColumnCount(mode);
412 if (mLayout != null) {
413 mLayout.setSpanCount(mColumnCount);
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700414 }
Jeff Sharkey9d0843d2013-05-07 12:41:33 -0700415
Steve McKay76be6202016-01-12 11:14:33 -0800416 int pad = getDirectoryPadding(mode);
417 mRecView.setPadding(pad, pad, pad, pad);
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800418 mRecView.requestLayout();
Steve McKay76be6202016-01-12 11:14:33 -0800419 mSelectionManager.handleLayoutChanged(); // RecyclerView doesn't do this for us
Steve McKay3eb2d072016-01-25 19:00:22 -0800420 mIconHelper.setViewMode(mode);
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700421 }
422
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800423 private int calculateColumnCount(@ViewMode int mode) {
424 if (mode == MODE_LIST) {
425 // List mode is a "grid" with 1 column.
426 return 1;
427 }
428
Steve McKay58efce32015-08-20 16:19:38 +0000429 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
430 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKay0ba3a142015-07-23 16:33:41 -0700431 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKay58efce32015-08-20 16:19:38 +0000432
Steve McKaya1f76802016-02-25 13:34:03 -0800433 assert(mRecView.getWidth() > 0);
434
Steve McKay0ba3a142015-07-23 16:33:41 -0700435 int columnCount = Math.max(1,
Steve McKay58efce32015-08-20 16:19:38 +0000436 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
437
Steve McKay0ba3a142015-07-23 16:33:41 -0700438 return columnCount;
439 }
440
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800441 private int getDirectoryPadding(@ViewMode int mode) {
Steve McKay76be6202016-01-12 11:14:33 -0800442 switch (mode) {
443 case MODE_GRID:
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800444 return getResources().getDimensionPixelSize(R.dimen.grid_container_padding);
Steve McKay76be6202016-01-12 11:14:33 -0800445 case MODE_LIST:
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800446 return getResources().getDimensionPixelSize(R.dimen.list_container_padding);
Steve McKay76be6202016-01-12 11:14:33 -0800447 default:
448 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
449 }
450 }
451
Steve McKayef16f5f2015-12-22 18:15:31 -0800452 @Override
453 public int getColumnCount() {
454 return mColumnCount;
455 }
456
Steve McKay4b3a13c2015-06-11 10:10:49 -0700457 /**
458 * Manages the integration between our ActionMode and MultiSelectManager, initiating
459 * ActionMode when there is a selection, canceling it when there is no selection,
460 * and clearing selection when action mode is explicitly exited by the user.
461 */
462 private final class SelectionModeListener
463 implements MultiSelectManager.Callback, ActionMode.Callback {
464
465 private Selection mSelected = new Selection();
466 private ActionMode mActionMode;
Steve McKay503648d2015-07-22 12:13:46 -0700467 private int mNoDeleteCount = 0;
Aga Wronska3b327ef2016-01-20 16:32:33 -0800468 private int mNoRenameCount = -1;
Steve McKay503648d2015-07-22 12:13:46 -0700469 private Menu mMenu;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700470
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700471 @Override
Ben Kwa743c7c22015-12-01 19:56:57 -0800472 public boolean onBeforeItemStateChange(String modelId, boolean selected) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700473 if (selected) {
Ben Kwa743c7c22015-12-01 19:56:57 -0800474 final Cursor cursor = mModel.getItem(modelId);
Steve McKaya1f76802016-02-25 13:34:03 -0800475
476 assert(cursor != null);
477
Jeff Sharkey2a030b02013-09-26 10:54:16 -0700478 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
479 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900480 return mTuner.canSelectType(docMimeType, docFlags);
Steve McKay4b3a13c2015-06-11 10:10:49 -0700481 }
482 return true;
483 }
484
485 @Override
Ben Kwa743c7c22015-12-01 19:56:57 -0800486 public void onItemStateChanged(String modelId, boolean selected) {
487 final Cursor cursor = mModel.getItem(modelId);
Steve McKay30551a22016-02-16 13:08:10 -0800488 if (cursor == null) {
489 Log.e(TAG, "Model returned null cursor for document: " + modelId
490 + ". Ignoring state changed event.");
491 return;
492 }
Steve McKay503648d2015-07-22 12:13:46 -0700493
Ben Kwa67924892016-01-27 09:58:36 -0800494 // TODO: Should this be happening in onSelectionChanged? Technically this callback is
495 // triggered on "silent" selection updates (i.e. we might be reacting to unfinalized
496 // selection changes here)
Steve McKay503648d2015-07-22 12:13:46 -0700497 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKay27d20a32016-02-22 18:38:09 -0800498 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0
499 && (docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
Steve McKay503648d2015-07-22 12:13:46 -0700500 mNoDeleteCount += selected ? 1 : -1;
501 }
Aga Wronska3b327ef2016-01-20 16:32:33 -0800502 if ((docFlags & Document.FLAG_SUPPORTS_RENAME) != 0) {
503 mNoRenameCount += selected ? 1 : -1;
504 }
Steve McKaydbec47a2015-08-12 14:48:34 -0700505 }
Steve McKay503648d2015-07-22 12:13:46 -0700506
Steve McKaydbec47a2015-08-12 14:48:34 -0700507 @Override
508 public void onSelectionChanged() {
Ben Kwa8250db42015-10-07 14:15:12 -0700509 mSelectionManager.getSelection(mSelected);
Ben Kwa7cc80012015-09-11 15:40:18 -0700510 TypedValue color = new TypedValue();
Steve McKay503648d2015-07-22 12:13:46 -0700511 if (mSelected.size() > 0) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700512 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
513 if (mActionMode == null) {
514 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
515 mActionMode = getActivity().startActionMode(this);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700516 }
Ben Kwa8250db42015-10-07 14:15:12 -0700517 getActivity().getTheme().resolveAttribute(R.attr.colorActionMode, color, true);
Steve McKay503648d2015-07-22 12:13:46 -0700518 updateActionMenu();
519 } else {
520 if (DEBUG) Log.d(TAG, "Finishing action mode.");
521 if (mActionMode != null) {
522 mActionMode.finish();
523 }
Ben Kwa0f7078f02015-09-08 07:31:19 -0700524 getActivity().getTheme().resolveAttribute(
525 android.R.attr.colorPrimaryDark, color, true);
Jeff Sharkeya5599ef2013-08-15 16:17:41 -0700526 }
Ben Kwa7cc80012015-09-11 15:40:18 -0700527 getActivity().getWindow().setStatusBarColor(color.data);
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700528
Steve McKay4b3a13c2015-06-11 10:10:49 -0700529 if (mActionMode != null) {
Aga Wronska91c1a432016-03-08 15:45:29 -0800530 mActionMode.setTitle(Shared.getQuantityString(getActivity(),
531 R.plurals.elements_selected, mSelected.size()));
Steve McKay4b3a13c2015-06-11 10:10:49 -0700532 }
533 }
534
535 // Called when the user exits the action mode
536 @Override
537 public void onDestroyActionMode(ActionMode mode) {
538 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
539 mActionMode = null;
540 // clear selection
Ben Kwa8250db42015-10-07 14:15:12 -0700541 mSelectionManager.clearSelection();
Steve McKay503648d2015-07-22 12:13:46 -0700542 mSelected.clear();
543 mNoDeleteCount = 0;
Aga Wronska3b327ef2016-01-20 16:32:33 -0800544 mNoRenameCount = -1;
Steve McKay4b3a13c2015-06-11 10:10:49 -0700545 }
546
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700547 @Override
548 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Aga Wronska7d5a2472016-03-08 17:56:28 -0800549 mRecView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
550
Ben Kwa8250db42015-10-07 14:15:12 -0700551 int size = mSelectionManager.getSelection().size();
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700552 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa8250db42015-10-07 14:15:12 -0700553 mode.setTitle(TextUtils.formatSelectedCount(size));
554 return (size > 0);
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700555 }
556
557 @Override
558 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay503648d2015-07-22 12:13:46 -0700559 mMenu = menu;
560 updateActionMenu();
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700561 return true;
562 }
563
Aga Wronska3b327ef2016-01-20 16:32:33 -0800564 boolean canRenameSelection() {
565 return mNoRenameCount == 0 && mSelectionManager.getSelection().size() == 1;
566 }
567
568 boolean canDeleteSelection() {
569 return mNoDeleteCount == 0;
570 }
571
Steve McKay503648d2015-07-22 12:13:46 -0700572 private void updateActionMenu() {
Steve McKaya1f76802016-02-25 13:34:03 -0800573 assert(mMenu != null);
Aga Wronska3b327ef2016-01-20 16:32:33 -0800574
Steve McKay503648d2015-07-22 12:13:46 -0700575 // Delegate update logic to our owning action, since specialized logic is desired.
Aga Wronska3b327ef2016-01-20 16:32:33 -0800576 mTuner.updateActionMenu(mMenu, mType, canDeleteSelection(), canRenameSelection());
Steve McKay5bbae102015-10-01 11:39:24 -0700577 Menus.disableHiddenItems(mMenu);
Steve McKay503648d2015-07-22 12:13:46 -0700578 }
579
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700580 @Override
Steve McKay4b3a13c2015-06-11 10:10:49 -0700581 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700582
Ben Kwa8250db42015-10-07 14:15:12 -0700583 Selection selection = mSelectionManager.getSelection(new Selection());
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700584
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800585 switch (item.getItemId()) {
586 case R.id.menu_open:
587 openDocuments(selection);
588 mode.finish();
589 return true;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700590
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800591 case R.id.menu_share:
592 shareDocuments(selection);
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800593 return true;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700594
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800595 case R.id.menu_delete:
Steve McKayda4b8aa2016-03-08 14:49:38 -0800596 // Pass mode along to the delete function so it can
597 // end action mode when documents are deleted.
598 // It won't end action mode if user cancels the delete.
599 deleteDocuments(selection, mode);
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800600 return true;
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700601
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800602 case R.id.menu_copy_to:
Steve McKay14e827a2016-01-06 18:32:13 -0800603 transferDocuments(selection, FileOperationService.OPERATION_COPY);
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800604 return true;
Ben Kwa0b4a3c42015-05-05 11:50:11 -0700605
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800606 case R.id.menu_move_to:
607 // Exit selection mode first, so we avoid deselecting deleted documents.
608 mode.finish();
Steve McKay14e827a2016-01-06 18:32:13 -0800609 transferDocuments(selection, FileOperationService.OPERATION_MOVE);
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800610 return true;
Ben Kwad99109f2015-03-31 10:11:43 -0700611
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800612 case R.id.menu_copy_to_clipboard:
Steve McKaycbee5442016-01-28 15:30:10 -0800613 copySelectedToClipboard();
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800614 return true;
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700615
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800616 case R.id.menu_select_all:
617 selectAllFiles();
618 return true;
Ben Kwa3bcc94882015-03-31 08:15:21 -0700619
Aga Wronska3b327ef2016-01-20 16:32:33 -0800620 case R.id.menu_rename:
Steve McKayda4b8aa2016-03-08 14:49:38 -0800621 // Exit selection mode first, so we avoid deselecting deleted
622 // (renamed) documents.
Aga Wronska3b327ef2016-01-20 16:32:33 -0800623 mode.finish();
Steve McKayda4b8aa2016-03-08 14:49:38 -0800624 renameDocuments(selection);
Aga Wronska3b327ef2016-01-20 16:32:33 -0800625 return true;
626
Steve McKayc2b4b3d2015-12-01 17:02:42 -0800627 default:
628 if (DEBUG) Log.d(TAG, "Unhandled menu item selected: " + item);
629 return false;
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700630 }
631 }
Steve McKay4b3a13c2015-06-11 10:10:49 -0700632 }
Jeff Sharkeybe8b12e2013-07-01 16:56:54 -0700633
Steve McKaycbee5442016-01-28 15:30:10 -0800634 public final boolean onBackPressed() {
635 if (mSelectionManager.hasSelection()) {
636 if (DEBUG) Log.d(TAG, "Clearing selection on back pressed.");
637 mSelectionManager.clearSelection();
638 return true;
639 }
640 return false;
641 }
642
Ben Kwad8391492015-12-17 10:37:00 -0800643 private void cancelThumbnailTask(View view) {
Steve McKay4b3a13c2015-06-11 10:10:49 -0700644 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
645 if (iconThumb != null) {
Ben Kwad8391492015-12-17 10:37:00 -0800646 mIconHelper.stopLoading(iconThumb);
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700647 }
Steve McKay4b3a13c2015-06-11 10:10:49 -0700648 }
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700649
Steve McKay4b3a13c2015-06-11 10:10:49 -0700650 private void openDocuments(final Selection selected) {
Steve McKayab7865c2015-05-27 16:11:42 -0700651 new GetDocumentsTask() {
652 @Override
653 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay12055472015-08-20 16:48:49 -0700654 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKayab7865c2015-05-27 16:11:42 -0700655 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwa726cf702015-04-08 15:03:35 -0700656 }
Steve McKayab7865c2015-05-27 16:11:42 -0700657 }.execute(selected);
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700658 }
659
Steve McKay4b3a13c2015-06-11 10:10:49 -0700660 private void shareDocuments(final Selection selected) {
Steve McKayab7865c2015-05-27 16:11:42 -0700661 new GetDocumentsTask() {
662 @Override
663 void onDocumentsReady(List<DocumentInfo> docs) {
664 Intent intent;
665
666 // Filter out directories - those can't be shared.
Steve McKay58efce32015-08-20 16:19:38 +0000667 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKayab7865c2015-05-27 16:11:42 -0700668 for (DocumentInfo doc: docs) {
669 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
670 docsForSend.add(doc);
671 }
672 }
673
674 if (docsForSend.size() == 1) {
675 final DocumentInfo doc = docsForSend.get(0);
676
677 intent = new Intent(Intent.ACTION_SEND);
678 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
679 intent.addCategory(Intent.CATEGORY_DEFAULT);
680 intent.setType(doc.mimeType);
681 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
682
683 } else if (docsForSend.size() > 1) {
684 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
685 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
686 intent.addCategory(Intent.CATEGORY_DEFAULT);
687
Steve McKay58efce32015-08-20 16:19:38 +0000688 final ArrayList<String> mimeTypes = new ArrayList<>();
689 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKayab7865c2015-05-27 16:11:42 -0700690 for (DocumentInfo doc : docsForSend) {
691 mimeTypes.add(doc.mimeType);
692 uris.add(doc.derivedUri);
693 }
694
695 intent.setType(findCommonMimeType(mimeTypes));
696 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
697
698 } else {
699 return;
700 }
701
702 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
703 startActivity(intent);
704 }
705 }.execute(selected);
706 }
707
Steve McKayda4b8aa2016-03-08 14:49:38 -0800708 private void deleteDocuments(final Selection selected, final ActionMode mode) {
Steve McKaya1f76802016-02-25 13:34:03 -0800709 assert(!selected.isEmpty());
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700710
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900711 final DocumentInfo srcParent = getDisplayState().stack.peek();
712 new GetDocumentsTask() {
713 @Override
Steve McKay7a3b8112016-02-23 10:06:50 -0800714 void onDocumentsReady(final List<DocumentInfo> docs) {
Steve McKayc8d4e222016-03-01 08:28:02 -0800715
716 TextView message =
717 (TextView) mInflater.inflate(R.layout.dialog_delete_confirmation, null);
718 message.setText(
719 Shared.getQuantityString(
720 getActivity(),
721 R.plurals.delete_confirmation_message,
722 docs.size()));
723
724 // This "insta-hides" files that are being deleted, because
725 // the delete operation may be not execute immediately (it
726 // may be queued up on the FileOperationService.)
727 // To hide the files locally, we call the hide method on the adapter
728 // ...which a live object...cannot be parceled.
729 // For that reason, for now, we implement this dialog NOT
730 // as a fragment (which can survive rotation and have its own state),
731 // but as a simple runtime dialog. So rotating a device with an
732 // active delete dialog...results in that dialog disappearing.
733 // We can do better, but don't have cycles for it now.
Steve McKay7a3b8112016-02-23 10:06:50 -0800734 new AlertDialog.Builder(getActivity())
Steve McKayc8d4e222016-03-01 08:28:02 -0800735 .setView(message)
Steve McKay7a3b8112016-02-23 10:06:50 -0800736 .setPositiveButton(
737 android.R.string.yes,
738 new DialogInterface.OnClickListener() {
739 public void onClick(DialogInterface dialog, int id) {
Steve McKayda4b8aa2016-03-08 14:49:38 -0800740 // Finish selection mode first which clears selection so we
741 // don't end up trying to deselect deleted documents.
742 // This is done here, rather in the onActionItemClicked
743 // so we can avoid de-selecting items in the case where
744 // the user cancels the delete.
745 mode.finish();
746 // Hide the files in the UI...since the operation
747 // might be queued up on FileOperationService.
748 // We're walking a line here.
Steve McKay7a3b8112016-02-23 10:06:50 -0800749 mAdapter.hide(selected.getAll());
750 FileOperations.delete(
751 getActivity(), docs, srcParent, getDisplayState().stack);
Ben Kwa304895a2015-08-27 16:06:33 -0700752 }
753 })
Steve McKay7a3b8112016-02-23 10:06:50 -0800754 .setNegativeButton(android.R.string.no, null)
755 .show();
756 }
757 }.execute(selected);
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700758 }
759
Ben Kwa1e2fa5e2016-02-02 23:00:02 -0800760 private void transferDocuments(final Selection selected, final @OpType int mode) {
Ben Kwaef3f2622015-04-07 15:43:39 -0700761 // Pop up a dialog to pick a destination. This is inadequate but works for now.
762 // TODO: Implement a picker that is to spec.
Daichi Hironobbe22922015-04-10 15:50:38 +0900763 final Intent intent = new Intent(
Ben Kwa84cebbe2015-09-25 14:48:29 -0700764 Shared.ACTION_PICK_COPY_DESTINATION,
Daichi Hironobbe22922015-04-10 15:50:38 +0900765 Uri.EMPTY,
766 getActivity(),
767 DocumentsActivity.class);
Steve McKayab7865c2015-05-27 16:11:42 -0700768
Steve McKaya6bbeab2016-02-17 15:02:01 -0800769 // Set an appropriate title on the drawer when it is shown in the picker.
770 // Coupled with the fact that we auto-open the drawer for copy/move operations
771 // it should basically be the thing people see first.
772 int drawerTitleId = mode == FileOperationService.OPERATION_MOVE
773 ? R.string.menu_move : R.string.menu_copy;
774 intent.putExtra(DocumentsContract.EXTRA_PROMPT, getResources().getString(drawerTitleId));
775
Steve McKayab7865c2015-05-27 16:11:42 -0700776 new GetDocumentsTask() {
777 @Override
778 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKaya6bbeab2016-02-17 15:02:01 -0800779 // TODO: Can this move to Fragment bundle state?
Steve McKayf69502c2015-11-09 17:38:35 +0900780 getDisplayState().selectedDocumentsForCopy = docs;
Steve McKayab7865c2015-05-27 16:11:42 -0700781
Steve McKaya6bbeab2016-02-17 15:02:01 -0800782 // Determine if there is a directory in the set of documents
783 // to be copied? Why? Directory creation isn't supported by some roots
784 // (like Downloads). This informs DocumentsActivity (the "picker")
785 // to restrict available roots to just those with support.
786 intent.putExtra(Shared.EXTRA_DIRECTORY_COPY, hasDirectory(docs));
Steve McKay14e827a2016-01-06 18:32:13 -0800787 intent.putExtra(FileOperationService.EXTRA_OPERATION, mode);
Steve McKaya6bbeab2016-02-17 15:02:01 -0800788
789 // This just identifies the type of request...we'll check it
790 // when we reveive a response.
Steve McKayab7865c2015-05-27 16:11:42 -0700791 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hirono9be34292015-04-14 17:12:54 +0900792 }
Steve McKaya6bbeab2016-02-17 15:02:01 -0800793
Steve McKayab7865c2015-05-27 16:11:42 -0700794 }.execute(selected);
Ben Kwad99109f2015-03-31 10:11:43 -0700795 }
796
Steve McKaya6bbeab2016-02-17 15:02:01 -0800797 private static boolean hasDirectory(List<DocumentInfo> docs) {
798 for (DocumentInfo info : docs) {
799 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
800 return true;
801 }
802 }
803 return false;
804 }
805
Aga Wronska3b327ef2016-01-20 16:32:33 -0800806 private void renameDocuments(Selection selected) {
807 // Batch renaming not supported
808 // Rename option is only available in menu when 1 document selected
Steve McKaya1f76802016-02-25 13:34:03 -0800809 assert(selected.size() == 1);
Aga Wronska3b327ef2016-01-20 16:32:33 -0800810
811 new GetDocumentsTask() {
812 @Override
813 void onDocumentsReady(List<DocumentInfo> docs) {
814 RenameDocumentFragment.show(getFragmentManager(), docs.get(0));
815 }
816 }.execute(selected);
817 }
818
Steve McKayef16f5f2015-12-22 18:15:31 -0800819 @Override
820 public void initDocumentHolder(DocumentHolder holder) {
Ben Kwa0436a752016-01-15 10:43:24 -0800821 holder.addEventListener(mItemEventListener);
Ben Kwa2036dad2016-02-10 07:46:35 -0800822 holder.itemView.setOnFocusChangeListener(mFocusManager);
Steve McKayef16f5f2015-12-22 18:15:31 -0800823 }
824
825 @Override
826 public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {
Ben Kwa990489d2016-02-25 14:10:40 -0800827 setupDragAndDropOnDocumentView(holder.itemView, cursor);
Steve McKayef16f5f2015-12-22 18:15:31 -0800828 }
829
830 @Override
831 public State getDisplayState() {
Steve McKayf69502c2015-11-09 17:38:35 +0900832 return ((BaseActivity) getActivity()).getDisplayState();
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700833 }
834
Steve McKayef16f5f2015-12-22 18:15:31 -0800835 @Override
836 public Model getModel() {
837 return mModel;
838 }
839
840 @Override
841 public boolean isDocumentEnabled(String docMimeType, int docFlags) {
842 return mTuner.isDocumentEnabled(docMimeType, docFlags);
843 }
844
Steve McKay9ed88a42016-01-21 18:46:15 -0800845 private void showEmptyDirectory() {
Ben Kwa0e9aae42016-02-04 16:35:27 -0800846 showEmptyView(R.string.empty, R.drawable.cabinet);
Ben Kwa91bec532015-09-16 13:15:38 -0700847 }
848
Steve McKay9ed88a42016-01-21 18:46:15 -0800849 private void showNoResults(RootInfo root) {
850 CharSequence msg = getContext().getResources().getText(R.string.no_results);
Ben Kwa0e9aae42016-02-04 16:35:27 -0800851 showEmptyView(String.format(String.valueOf(msg), root.title), R.drawable.cabinet);
Ben Kwa91bec532015-09-16 13:15:38 -0700852 }
853
Steve McKay9ed88a42016-01-21 18:46:15 -0800854 private void showQueryError() {
Ben Kwa0e9aae42016-02-04 16:35:27 -0800855 showEmptyView(R.string.query_error, R.drawable.hourglass);
Steve McKay9ed88a42016-01-21 18:46:15 -0800856 }
857
Ben Kwa0e9aae42016-02-04 16:35:27 -0800858 private void showEmptyView(@StringRes int id, int drawable) {
859 showEmptyView(getContext().getResources().getText(id), drawable);
Steve McKay9ed88a42016-01-21 18:46:15 -0800860 }
861
Ben Kwa0e9aae42016-02-04 16:35:27 -0800862 private void showEmptyView(CharSequence msg, int drawable) {
Steve McKay9ed88a42016-01-21 18:46:15 -0800863 View content = mEmptyView.findViewById(R.id.content);
864 TextView msgView = (TextView) mEmptyView.findViewById(R.id.message);
Ben Kwa0e9aae42016-02-04 16:35:27 -0800865 ImageView imageView = (ImageView) mEmptyView.findViewById(R.id.artwork);
Steve McKay9ed88a42016-01-21 18:46:15 -0800866 msgView.setText(msg);
Ben Kwa0e9aae42016-02-04 16:35:27 -0800867 imageView.setImageResource(drawable);
Steve McKay9ed88a42016-01-21 18:46:15 -0800868
Steve McKay9ed88a42016-01-21 18:46:15 -0800869 mEmptyView.setVisibility(View.VISIBLE);
Aga Wronskad4b17532016-02-12 09:15:32 -0800870 mEmptyView.requestFocus();
Steve McKay9ed88a42016-01-21 18:46:15 -0800871 mRecView.setVisibility(View.GONE);
Steve McKay9ed88a42016-01-21 18:46:15 -0800872 }
873
874 private void showDirectory() {
Ben Kwa91bec532015-09-16 13:15:38 -0700875 mEmptyView.setVisibility(View.GONE);
876 mRecView.setVisibility(View.VISIBLE);
Aga Wronskad4b17532016-02-12 09:15:32 -0800877 mRecView.requestFocus();
Ben Kwa91bec532015-09-16 13:15:38 -0700878 }
879
Jeff Sharkeyb5133112013-09-01 18:41:04 -0700880 private String findCommonMimeType(List<String> mimeTypes) {
881 String[] commonType = mimeTypes.get(0).split("/");
882 if (commonType.length != 2) {
883 return "*/*";
884 }
885
886 for (int i = 1; i < mimeTypes.size(); i++) {
887 String[] type = mimeTypes.get(i).split("/");
888 if (type.length != 2) continue;
889
890 if (!commonType[1].equals(type[1])) {
891 commonType[1] = "*";
892 }
893
894 if (!commonType[0].equals(type[0])) {
895 commonType[0] = "*";
896 commonType[1] = "*";
897 break;
898 }
899 }
900
901 return commonType[0] + "/" + commonType[1];
902 }
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700903
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700904 private void copyFromClipboard() {
905 new AsyncTask<Void, Void, List<DocumentInfo>>() {
906
907 @Override
908 protected List<DocumentInfo> doInBackground(Void... params) {
909 return mClipper.getClippedDocuments();
910 }
911
912 @Override
913 protected void onPostExecute(List<DocumentInfo> docs) {
914 DocumentInfo destination =
915 ((BaseActivity) getActivity()).getCurrentDirectory();
916 copyDocuments(docs, destination);
917 }
918 }.execute();
Steve McKay3da8afc2015-05-05 14:50:00 -0700919 }
920
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700921 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKaya1f76802016-02-25 13:34:03 -0800922 assert(clipData != null);
923
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700924 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700925
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700926 @Override
927 protected List<DocumentInfo> doInBackground(Void... params) {
928 return mClipper.getDocumentsFromClipData(clipData);
929 }
930
931 @Override
932 protected void onPostExecute(List<DocumentInfo> docs) {
933 copyDocuments(docs, destination);
934 }
935 }.execute();
936 }
937
938 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
Steve McKay4a1ca862016-02-17 18:25:47 -0800939 BaseActivity activity = (BaseActivity) getActivity();
940 if (!canCopy(docs, activity.getCurrentRoot(), destination)) {
Steve McKay5bbae102015-10-01 11:39:24 -0700941 Snackbars.makeSnackbar(
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700942 getActivity(),
Ben Kwa94b486d2015-09-30 10:00:10 -0700943 R.string.clipboard_files_cannot_paste,
944 Snackbar.LENGTH_SHORT)
945 .show();
Steve McKay3da8afc2015-05-05 14:50:00 -0700946 return;
947 }
948
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700949 if (docs.isEmpty()) {
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700950 return;
Vladislav Kaznacheevd69d3c42015-05-05 12:09:47 -0700951 }
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700952
Steve McKayf69502c2015-11-09 17:38:35 +0900953 final DocumentStack curStack = getDisplayState().stack;
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700954 DocumentStack tmpStack = new DocumentStack();
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700955 if (destination != null) {
956 tmpStack.push(destination);
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700957 tmpStack.addAll(curStack);
958 } else {
959 tmpStack = curStack;
960 }
961
Steve McKay14e827a2016-01-06 18:32:13 -0800962 FileOperations.copy(getActivity(), docs, tmpStack);
Vladislav Kaznacheev335dba52015-05-01 13:46:57 -0700963 }
964
Steve McKayf8621552015-11-03 15:23:16 -0800965 public void copySelectedToClipboard() {
Steve McKayd28c5c32015-12-07 16:31:42 -0800966 Selection selection = mSelectionManager.getSelection(new Selection());
967 if (!selection.isEmpty()) {
968 copySelectionToClipboard(selection);
Steve McKaycbee5442016-01-28 15:30:10 -0800969 mSelectionManager.clearSelection();
Steve McKayd28c5c32015-12-07 16:31:42 -0800970 }
Steve McKayab7865c2015-05-27 16:11:42 -0700971 }
Steve McKay3da8afc2015-05-05 14:50:00 -0700972
Steve McKayd28c5c32015-12-07 16:31:42 -0800973 void copySelectionToClipboard(Selection selection) {
Steve McKaya1f76802016-02-25 13:34:03 -0800974 assert(!selection.isEmpty());
Steve McKayab7865c2015-05-27 16:11:42 -0700975 new GetDocumentsTask() {
976 @Override
977 void onDocumentsReady(List<DocumentInfo> docs) {
978 mClipper.clipDocuments(docs);
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700979 Activity activity = getActivity();
Steve McKay5bbae102015-10-01 11:39:24 -0700980 Snackbars.makeSnackbar(activity,
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700981 activity.getResources().getQuantityString(
982 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
Ben Kwa862b96412015-12-07 13:25:27 -0800983 Snackbar.LENGTH_SHORT).show();
Steve McKayab7865c2015-05-27 16:11:42 -0700984 }
Steve McKayd28c5c32015-12-07 16:31:42 -0800985 }.execute(selection);
Steve McKay3da8afc2015-05-05 14:50:00 -0700986 }
987
Steve McKayf8621552015-11-03 15:23:16 -0800988 public void pasteFromClipboard() {
Steve McKaybdbd0ff2015-05-20 15:58:42 -0700989 copyFromClipboard();
990 getActivity().invalidateOptionsMenu();
Steve McKay3da8afc2015-05-05 14:50:00 -0700991 }
992
Steve McKay3da8afc2015-05-05 14:50:00 -0700993 /**
994 * Returns true if the list of files can be copied to destination. Note that this
995 * is a policy check only. Currently the method does not attempt to verify
996 * available space or any other environmental aspects possibly resulting in
997 * failure to copy.
998 *
999 * @return true if the list of files can be copied to destination.
1000 */
Steve McKay4a1ca862016-02-17 18:25:47 -08001001 private boolean canCopy(List<DocumentInfo> files, RootInfo root, DocumentInfo dest) {
1002 if (dest == null || !dest.isDirectory() || !dest.isCreateSupported()) {
1003 return false;
1004 }
Steve McKay3da8afc2015-05-05 14:50:00 -07001005
Steve McKay008e9482016-02-18 15:32:16 -08001006 // Can't copy folders to downloads, because we don't show folders there.
1007 if (!root.isDownloads()) {
Steve McKay3da8afc2015-05-05 14:50:00 -07001008 for (DocumentInfo docs : files) {
1009 if (docs.isDirectory()) {
1010 return false;
1011 }
1012 }
1013 }
1014
Steve McKay4a1ca862016-02-17 18:25:47 -08001015 return true;
Steve McKay3da8afc2015-05-05 14:50:00 -07001016 }
1017
Steve McKayf8621552015-11-03 15:23:16 -08001018 public void selectAllFiles() {
Ben Kwa862b96412015-12-07 13:25:27 -08001019 // Only select things currently visible in the adapter.
1020 boolean changed = mSelectionManager.setItemsSelected(mAdapter.getModelIds(), true);
Steve McKayb04b1642015-07-24 13:14:20 -07001021 if (changed) {
1022 updateDisplayState();
1023 }
Steve McKay3da8afc2015-05-05 14:50:00 -07001024 }
1025
Ben Kwa2036dad2016-02-10 07:46:35 -08001026 /**
1027 * Attempts to restore focus on the directory listing.
1028 */
1029 public void requestFocus() {
1030 mFocusManager.restoreLastFocus();
1031 }
1032
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001033 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1034 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1035 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1036 // Make a directory item a drop target. Drop on non-directories and empty space
1037 // is handled at the list/grid view level.
1038 view.setOnDragListener(mOnDragListener);
1039 }
1040
Ben Kwa990489d2016-02-25 14:10:40 -08001041 // Make all items draggable.
Ben Kwae3dfebf2016-02-18 16:45:45 -08001042 view.setOnLongClickListener(mDragHelper);
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001043 }
1044
1045 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1046 @Override
1047 public boolean onDrag(View v, DragEvent event) {
1048 switch (event.getAction()) {
1049 case DragEvent.ACTION_DRAG_STARTED:
1050 // TODO: Check if the event contains droppable data.
1051 return true;
1052
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001053 // TODO: Expand drop target directory on hover?
1054 case DragEvent.ACTION_DRAG_ENTERED:
Ben Kwa1b685e52016-02-24 10:05:53 -08001055 setDropTargetHighlight(v, true);
Ben Kwae3dfebf2016-02-18 16:45:45 -08001056 return true;
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001057 case DragEvent.ACTION_DRAG_EXITED:
Ben Kwa1b685e52016-02-24 10:05:53 -08001058 setDropTargetHighlight(v, false);
Ben Kwae3dfebf2016-02-18 16:45:45 -08001059 return true;
1060
1061 case DragEvent.ACTION_DRAG_LOCATION:
Ben Kwa990489d2016-02-25 14:10:40 -08001062 return true;
1063
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001064 case DragEvent.ACTION_DRAG_ENDED:
Ben Kwa990489d2016-02-25 14:10:40 -08001065 if (event.getResult()) {
1066 // Exit selection mode if the drop was handled.
1067 mSelectionManager.clearSelection();
1068 }
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001069 return true;
1070
1071 case DragEvent.ACTION_DROP:
Ben Kwa990489d2016-02-25 14:10:40 -08001072 // After a drop event, always stop highlighting the target.
Ben Kwa1b685e52016-02-24 10:05:53 -08001073 setDropTargetHighlight(v, false);
Ben Kwa990489d2016-02-25 14:10:40 -08001074 // Don't copy from the cwd into the cwd. Note: this currently doesn't work for
1075 // multi-window drag, because localState isn't carried over from one process to
1076 // another.
1077 Object src = event.getLocalState();
1078 DocumentInfo dst = getDestination(v);
1079 if (Objects.equals(src, dst)) {
1080 return false;
1081 }
1082 copyFromClipData(event.getClipData(), dst);
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001083 return true;
1084 }
1085 return false;
1086 }
Ben Kwae3dfebf2016-02-18 16:45:45 -08001087
Ben Kwa990489d2016-02-25 14:10:40 -08001088 private DocumentInfo getDestination(View v) {
1089 String id = getModelId(v);
1090 if (id != null) {
1091 Cursor dstCursor = mModel.getItem(id);
Steve McKaya1f76802016-02-25 13:34:03 -08001092 assert(dstCursor != null);
Ben Kwa990489d2016-02-25 14:10:40 -08001093 return DocumentInfo.fromDirectoryCursor(dstCursor);
1094 }
1095
1096 if (v == mRecView || v == mEmptyView) {
1097 return getDisplayState().stack.peek();
1098 }
1099
1100 return null;
1101 }
1102
Ben Kwa1b685e52016-02-24 10:05:53 -08001103 private void setDropTargetHighlight(View v, boolean highlight) {
Ben Kwae3dfebf2016-02-18 16:45:45 -08001104 // Note: use exact comparison - this code is searching for views which are children of
1105 // the RecyclerView instance in the UI.
1106 if (v.getParent() == mRecView) {
1107 RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(v);
1108 if (vh instanceof DocumentHolder) {
1109 ((DocumentHolder) vh).setHighlighted(highlight);
1110 }
1111 }
1112 }
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001113 };
1114
Ben Kwa003a9992015-11-30 23:00:02 -08001115 /**
1116 * Gets the model ID for a given motion event (using the event position)
1117 */
1118 private String getModelId(MotionEvent e) {
1119 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
1120 if (view == null) {
1121 return null;
1122 }
1123 RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(view);
1124 if (vh instanceof DocumentHolder) {
1125 return ((DocumentHolder) vh).modelId;
1126 } else {
1127 return null;
1128 }
1129 }
1130
1131 /**
1132 * Gets the model ID for a given RecyclerView item.
1133 * @param view A View that is a document item view, or a child of a document item view.
1134 * @return The Model ID for the given document, or null if the given view is not associated with
1135 * a document item view.
1136 */
1137 private String getModelId(View view) {
Ben Kwae3dfebf2016-02-18 16:45:45 -08001138 View itemView = mRecView.findContainingItemView(view);
1139 if (itemView != null) {
1140 RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(itemView);
1141 if (vh instanceof DocumentHolder) {
1142 return ((DocumentHolder) vh).modelId;
Vladislav Kaznacheevb5999ef2015-09-04 09:17:37 -07001143 }
Vladislav Kaznacheevb5999ef2015-09-04 09:17:37 -07001144 }
Ben Kwae3dfebf2016-02-18 16:45:45 -08001145 return null;
Vladislav Kaznacheevb5999ef2015-09-04 09:17:37 -07001146 }
1147
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001148 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Ben Kwa003a9992015-11-30 23:00:02 -08001149 String modelId = getModelId(currentItemView);
1150 if (modelId == null) {
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001151 return Collections.EMPTY_LIST;
1152 }
1153
Ben Kwa8250db42015-10-07 14:15:12 -07001154 final List<DocumentInfo> selectedDocs =
1155 mModel.getDocuments(mSelectionManager.getSelection());
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001156 if (!selectedDocs.isEmpty()) {
Ben Kwa003a9992015-11-30 23:00:02 -08001157 if (!isSelected(modelId)) {
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001158 // There is a selection that does not include the current item, drag nothing.
1159 return Collections.EMPTY_LIST;
1160 }
1161 return selectedDocs;
1162 }
1163
Ben Kwa003a9992015-11-30 23:00:02 -08001164 final Cursor cursor = mModel.getItem(modelId);
Steve McKay58efce32015-08-20 16:19:38 +00001165
Steve McKaya1f76802016-02-25 13:34:03 -08001166 assert(cursor != null);
1167
1168 return Lists.newArrayList(
1169 DocumentInfo.fromDirectoryCursor(cursor));
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001170 }
1171
1172 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1173 if (docs.size() == 1) {
1174 final DocumentInfo doc = docs.get(0);
Ben Kwad8391492015-12-17 10:37:00 -08001175 return mIconHelper.getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1176 doc.mimeType, doc.icon);
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001177 }
1178 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1179 }
1180
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001181 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1182
1183 private final Drawable mShadow;
1184
1185 private final int mShadowDimension;
1186
1187 public DrawableShadowBuilder(Drawable shadow) {
1188 mShadow = shadow;
1189 mShadowDimension = getResources().getDimensionPixelSize(
1190 R.dimen.drag_shadow_size);
1191 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1192 }
1193
Ben Kwac64cb252015-08-27 16:04:46 -07001194 @Override
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001195 public void onProvideShadowMetrics(
1196 Point shadowSize, Point shadowTouchPoint) {
1197 shadowSize.set(mShadowDimension, mShadowDimension);
1198 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1199 }
1200
Ben Kwac64cb252015-08-27 16:04:46 -07001201 @Override
Vladislav Kaznacheev42a6bbc2015-05-01 14:18:57 -07001202 public void onDrawShadow(Canvas canvas) {
1203 mShadow.draw(canvas);
1204 }
1205 }
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001206
Steve McKaybdbd0ff2015-05-20 15:58:42 -07001207 /**
Steve McKayab7865c2015-05-27 16:11:42 -07001208 * Abstract task providing support for loading documents *off*
1209 * the main thread. And if it isn't obvious, creating a list
1210 * of documents (especially large lists) can be pretty expensive.
1211 */
1212 private abstract class GetDocumentsTask
Steve McKay4b3a13c2015-06-11 10:10:49 -07001213 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKayab7865c2015-05-27 16:11:42 -07001214 @Override
Steve McKay4b3a13c2015-06-11 10:10:49 -07001215 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwac64cb252015-08-27 16:04:46 -07001216 return mModel.getDocuments(selected[0]);
Steve McKayab7865c2015-05-27 16:11:42 -07001217 }
1218
1219 @Override
1220 protected final void onPostExecute(List<DocumentInfo> docs) {
1221 onDocumentsReady(docs);
1222 }
1223
1224 abstract void onDocumentsReady(List<DocumentInfo> docs);
1225 }
1226
Steve McKayef16f5f2015-12-22 18:15:31 -08001227 @Override
1228 public boolean isSelected(String modelId) {
Ben Kwa743c7c22015-12-01 19:56:57 -08001229 return mSelectionManager.getSelection().contains(modelId);
Ben Kwa91caed82015-09-21 10:49:52 -07001230 }
Ben Kwa18fce3c2015-09-01 11:03:01 -07001231
Ben Kwa0436a752016-01-15 10:43:24 -08001232 private class ItemEventListener implements DocumentHolder.EventListener {
Ben Kwabd964562015-10-14 08:00:27 -07001233 @Override
Ben Kwa0436a752016-01-15 10:43:24 -08001234 public boolean onActivate(DocumentHolder doc) {
Ben Kwa67924892016-01-27 09:58:36 -08001235 // Toggle selection if we're in selection mode, othewise, view item.
1236 if (mSelectionManager.hasSelection()) {
1237 mSelectionManager.toggleSelection(doc.modelId);
1238 } else {
1239 handleViewItem(doc.modelId);
1240 }
Ben Kwa0436a752016-01-15 10:43:24 -08001241 return true;
1242 }
1243
1244 @Override
1245 public boolean onSelect(DocumentHolder doc) {
1246 mSelectionManager.toggleSelection(doc.modelId);
1247 mSelectionManager.setSelectionRangeBegin(doc.getAdapterPosition());
1248 return true;
Ben Kwabd964562015-10-14 08:00:27 -07001249 }
Ben Kwa67924892016-01-27 09:58:36 -08001250
1251 @Override
1252 public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event) {
1253 // Only handle key-down events. This is simpler, consistent with most other UIs, and
1254 // enables the handling of repeated key events from holding down a key.
1255 if (event.getAction() != KeyEvent.ACTION_DOWN) {
1256 return false;
1257 }
1258
Ben Kwa22937c92016-02-23 23:00:01 -08001259 // Ignore tab key events. Those should be handled by the top-level key handler.
1260 if (keyCode == KeyEvent.KEYCODE_TAB) {
1261 return false;
1262 }
1263
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001264 if (mFocusManager.handleKey(doc, keyCode, event)) {
Ben Kwa83df50f2016-02-10 14:01:19 -08001265 // Handle range selection adjustments. Extending the selection will adjust the
1266 // bounds of the in-progress range selection. Each time an unshifted navigation
1267 // event is received, the range selection is restarted.
1268 if (shouldExtendSelection(event)) {
1269 if (!mSelectionManager.isRangeSelectionActive()) {
1270 // Start a range selection if one isn't active
1271 mSelectionManager.startRangeSelection(doc.getAdapterPosition());
1272 }
1273 mSelectionManager.snapRangeSelection(mFocusManager.getFocusPosition());
1274 } else {
1275 mSelectionManager.endRangeSelection();
1276 }
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001277 return true;
Ben Kwa67924892016-01-27 09:58:36 -08001278 }
1279
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001280 // Handle enter key events
1281 if (keyCode == KeyEvent.KEYCODE_ENTER) {
Ben Kwa09eb77b2016-02-10 08:57:25 -08001282 if (event.isShiftPressed()) {
1283 return onSelect(doc);
1284 } else {
1285 return onActivate(doc);
1286 }
Ben Kwab0bfe2d2016-02-09 11:27:45 -08001287 }
1288
1289 return false;
Ben Kwa67924892016-01-27 09:58:36 -08001290 }
Ben Kwa83df50f2016-02-10 14:01:19 -08001291
1292 private boolean shouldExtendSelection(KeyEvent event) {
1293 return Events.isNavigationKeyCode(event.getKeyCode()) &&
1294 event.isShiftPressed();
1295 }
Ben Kwabd964562015-10-14 08:00:27 -07001296 }
1297
Steve McKayef16f5f2015-12-22 18:15:31 -08001298 private final class ModelUpdateListener implements Model.UpdateListener {
Ben Kwa91caed82015-09-21 10:49:52 -07001299 @Override
1300 public void onModelUpdate(Model model) {
1301 if (model.info != null || model.error != null) {
1302 mMessageBar.setInfo(model.info);
1303 mMessageBar.setError(model.error);
1304 mMessageBar.show();
1305 }
Ben Kwa18fce3c2015-09-01 11:03:01 -07001306
Ben Kwa91caed82015-09-21 10:49:52 -07001307 mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE);
1308
1309 if (model.isEmpty()) {
Aga Wronska893390b2016-02-17 13:50:42 -08001310 if (mSearchMode) {
Steve McKay9ed88a42016-01-21 18:46:15 -08001311 showNoResults(getDisplayState().stack.root);
1312 } else {
1313 showEmptyDirectory();
1314 }
Ben Kwa91caed82015-09-21 10:49:52 -07001315 } else {
Steve McKay9ed88a42016-01-21 18:46:15 -08001316 showDirectory();
Ben Kwa91bec532015-09-16 13:15:38 -07001317 mAdapter.notifyDataSetChanged();
Ben Kwa91caed82015-09-21 10:49:52 -07001318 }
Tomasz Mikolajewskiae6d6b42016-02-24 12:53:44 +09001319
1320 if (!model.isLoading()) {
1321 ((BaseActivity) getActivity()).notifyDirectoryLoaded(
1322 model.doc != null ? model.doc.derivedUri : null);
1323 }
Ben Kwa91caed82015-09-21 10:49:52 -07001324 }
1325
1326 @Override
1327 public void onModelUpdateFailed(Exception e) {
Steve McKay9ed88a42016-01-21 18:46:15 -08001328 showQueryError();
Ben Kwa18fce3c2015-09-01 11:03:01 -07001329 }
Ben Kwac64cb252015-08-27 16:04:46 -07001330 }
Ben Kwa0436a752016-01-15 10:43:24 -08001331
Ben Kwae3dfebf2016-02-18 16:45:45 -08001332 private DragStartHelper mDragHelper = new DragStartHelper(null) {
Steve McKay41585d52016-01-21 15:10:39 -08001333 @Override
Ben Kwae3dfebf2016-02-18 16:45:45 -08001334 protected boolean onDragStart(View v) {
1335 if (isSelected(getModelId(v))) {
Steve McKay41585d52016-01-21 15:10:39 -08001336 List<DocumentInfo> docs = getDraggableDocuments(v);
1337 if (docs.isEmpty()) {
1338 return false;
1339 }
Ben Kwa990489d2016-02-25 14:10:40 -08001340 v.startDragAndDrop(
1341 mClipper.getClipDataForDocuments(docs),
Steve McKay41585d52016-01-21 15:10:39 -08001342 new DrawableShadowBuilder(getDragShadowIcon(docs)),
Ben Kwa990489d2016-02-25 14:10:40 -08001343 getDisplayState().stack.peek(),
Steve McKay41585d52016-01-21 15:10:39 -08001344 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
1345 View.DRAG_FLAG_GLOBAL_URI_WRITE
1346 );
1347 return true;
1348 }
1349
1350 return false;
1351 }
1352 };
1353
1354 // Previously we listened to events with one class, only to bounce them forward
1355 // to GestureDetector. We're still doing that here, but with a single class
1356 // that reduces overall complexity in our glue code.
1357 private static final class ListeningGestureDetector extends GestureDetector
1358 implements OnItemTouchListener {
1359
1360 private int mLastTool = -1;
Ben Kwae3dfebf2016-02-18 16:45:45 -08001361 private DragStartHelper mDragHelper;
Steve McKay41585d52016-01-21 15:10:39 -08001362
Ben Kwae3dfebf2016-02-18 16:45:45 -08001363 public ListeningGestureDetector(
1364 Context context, DragStartHelper dragHelper, GestureListener listener) {
Steve McKay41585d52016-01-21 15:10:39 -08001365 super(context, listener);
Ben Kwae3dfebf2016-02-18 16:45:45 -08001366 mDragHelper = dragHelper;
Steve McKay41585d52016-01-21 15:10:39 -08001367 setOnDoubleTapListener(listener);
1368 }
1369
1370 boolean mouseSpawnedLastEvent() {
1371 return Events.isMouseType(mLastTool);
1372 }
1373
1374 boolean touchSpawnedLastEvent() {
1375 return Events.isTouchType(mLastTool);
1376 }
1377
1378 @Override
1379 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
1380 mLastTool = e.getToolType(0);
Ben Kwae3dfebf2016-02-18 16:45:45 -08001381
1382 // Detect drag events. When a drag is detected, intercept the rest of the gesture.
1383 View itemView = rv.findChildViewUnder(e.getX(), e.getY());
1384 if (itemView != null && mDragHelper.handleTouch(itemView, e)) {
1385 return true;
1386 }
1387 // Forward unhandled events to the GestureDetector.
1388 onTouchEvent(e);
1389
Steve McKay41585d52016-01-21 15:10:39 -08001390 return false;
1391 }
1392
1393 @Override
Ben Kwae3dfebf2016-02-18 16:45:45 -08001394 public void onTouchEvent(RecyclerView rv, MotionEvent e) {
1395 View itemView = rv.findChildViewUnder(e.getX(), e.getY());
1396 mDragHelper.handleTouch(itemView, e);
1397 // Note: even though this event is being handled as part of a drag gesture, continue
1398 // forwarding to the GestureDetector. The detector needs to see the entire cluster of
1399 // events in order to properly interpret gestures.
1400 onTouchEvent(e);
1401 }
Steve McKay41585d52016-01-21 15:10:39 -08001402
1403 @Override
1404 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
1405 }
1406
Ben Kwa0436a752016-01-15 10:43:24 -08001407 /**
1408 * The gesture listener for items in the list/grid view. Interprets gestures and sends the
1409 * events to the target DocumentHolder, whence they are routed to the appropriate listener.
1410 */
1411 private class GestureListener extends GestureDetector.SimpleOnGestureListener {
1412 @Override
1413 public boolean onSingleTapUp(MotionEvent e) {
1414 // Single tap logic:
1415 // If the selection manager is active, it gets first whack at handling tap
1416 // events. Otherwise, tap events are routed to the target DocumentHolder.
1417 boolean handled = mSelectionManager.onSingleTapUp(
1418 new MotionInputEvent(e, mRecView));
1419
1420 if (handled) {
1421 return handled;
1422 }
1423
1424 // Give the DocumentHolder a crack at the event.
1425 DocumentHolder holder = getTarget(e);
1426 if (holder != null) {
1427 handled = holder.onSingleTapUp(e);
1428 }
1429
1430 return handled;
1431 }
1432
1433 @Override
1434 public void onLongPress(MotionEvent e) {
1435 // Long-press events get routed directly to the selection manager. They can be
1436 // changed to route through the DocumentHolder if necessary.
1437 mSelectionManager.onLongPress(new MotionInputEvent(e, mRecView));
1438 }
1439
1440 @Override
1441 public boolean onDoubleTap(MotionEvent e) {
1442 // Double-tap events are handled directly by the DirectoryFragment. They can be changed
1443 // to route through the DocumentHolder if necessary.
1444 return DirectoryFragment.this.onDoubleTap(e);
1445 }
1446
1447 private @Nullable DocumentHolder getTarget(MotionEvent e) {
1448 View childView = mRecView.findChildViewUnder(e.getX(), e.getY());
1449 if (childView != null) {
1450 return (DocumentHolder) mRecView.getChildViewHolder(childView);
1451 } else {
1452 return null;
1453 }
1454 }
1455 }
Steve McKay9ed88a42016-01-21 18:46:15 -08001456
1457 public static void showDirectory(
1458 FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
Aga Wronska893390b2016-02-17 13:50:42 -08001459 create(fm, TYPE_NORMAL, root, doc, null, anim);
Steve McKay9ed88a42016-01-21 18:46:15 -08001460 }
1461
1462 public static void showRecentsOpen(FragmentManager fm, int anim) {
Aga Wronska893390b2016-02-17 13:50:42 -08001463 create(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Steve McKay9ed88a42016-01-21 18:46:15 -08001464 }
1465
Aga Wronska893390b2016-02-17 13:50:42 -08001466 public static void reloadSearch(FragmentManager fm, RootInfo root, DocumentInfo doc,
1467 String query) {
1468 DirectoryFragment df = get(fm);
1469
1470 df.mQuery = query;
1471 df.mRoot = root;
1472 df.mDocument = doc;
1473 df.mSearchMode = query != null;
1474 df.getLoaderManager().restartLoader(LOADER_ID, null, df);
1475 }
1476
1477 public static void reload(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
1478 String query) {
1479 DirectoryFragment df = get(fm);
1480 df.mType = type;
1481 df.mQuery = query;
1482 df.mRoot = root;
1483 df.mDocument = doc;
1484 df.mSearchMode = query != null;
1485 df.getLoaderManager().restartLoader(LOADER_ID, null, df);
1486 }
1487
1488 public static void create(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
Steve McKay9ed88a42016-01-21 18:46:15 -08001489 String query, int anim) {
1490 final Bundle args = new Bundle();
Aga Wronska893390b2016-02-17 13:50:42 -08001491 args.putInt(Shared.EXTRA_TYPE, type);
1492 args.putParcelable(Shared.EXTRA_ROOT, root);
1493 args.putParcelable(Shared.EXTRA_DOC, doc);
1494 args.putString(Shared.EXTRA_QUERY, query);
Aga Wronskad5597432016-02-26 11:36:07 -08001495 args.putParcelable(Shared.EXTRA_SELECTION, new Selection());
Steve McKay9ed88a42016-01-21 18:46:15 -08001496
1497 final FragmentTransaction ft = fm.beginTransaction();
1498 switch (anim) {
1499 case ANIM_SIDE:
Aga Wronska893390b2016-02-17 13:50:42 -08001500 args.putBoolean(Shared.EXTRA_IGNORE_STATE, true);
Steve McKay9ed88a42016-01-21 18:46:15 -08001501 break;
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +09001502 case ANIM_ENTER:
Aga Wronska893390b2016-02-17 13:50:42 -08001503 args.putBoolean(Shared.EXTRA_IGNORE_STATE, true);
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +09001504 ft.setCustomAnimations(R.animator.dir_enter, R.animator.dir_frozen);
Steve McKay9ed88a42016-01-21 18:46:15 -08001505 break;
Tomasz Mikolajewskie3fe9d72016-02-03 16:53:21 +09001506 case ANIM_LEAVE:
1507 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_leave);
Steve McKay9ed88a42016-01-21 18:46:15 -08001508 break;
1509 }
1510
1511 final DirectoryFragment fragment = new DirectoryFragment();
1512 fragment.setArguments(args);
1513
Aga Wronska893390b2016-02-17 13:50:42 -08001514 ft.replace(getFragmentId(), fragment);
Steve McKay9ed88a42016-01-21 18:46:15 -08001515 ft.commitAllowingStateLoss();
1516 }
1517
1518 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
1519 final StringBuilder builder = new StringBuilder();
1520 builder.append(root != null ? root.authority : "null").append(';');
1521 builder.append(root != null ? root.rootId : "null").append(';');
1522 builder.append(doc != null ? doc.documentId : "null");
1523 return builder.toString();
1524 }
1525
1526 public static @Nullable DirectoryFragment get(FragmentManager fm) {
1527 // TODO: deal with multiple directories shown at once
Aga Wronska893390b2016-02-17 13:50:42 -08001528 Fragment fragment = fm.findFragmentById(getFragmentId());
Steve McKay9ed88a42016-01-21 18:46:15 -08001529 return fragment instanceof DirectoryFragment
1530 ? (DirectoryFragment) fragment
1531 : null;
1532 }
Aga Wronska893390b2016-02-17 13:50:42 -08001533
1534 private static int getFragmentId() {
1535 return R.id.container_directory;
1536 }
1537
1538 @Override
1539 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
1540 Context context = getActivity();
1541 State state = getDisplayState();
1542
1543 Uri contentsUri;
1544 switch (mType) {
1545 case TYPE_NORMAL:
1546 contentsUri = mSearchMode ? DocumentsContract.buildSearchDocumentsUri(
1547 mRoot.authority, mRoot.rootId, mQuery)
1548 : DocumentsContract.buildChildDocumentsUri(
1549 mDocument.authority, mDocument.documentId);
Steve McKay75fe6bc2016-03-07 11:51:31 -08001550 if (mTuner.enableManagedMode()) {
Aga Wronska893390b2016-02-17 13:50:42 -08001551 contentsUri = DocumentsContract.setManageMode(contentsUri);
1552 }
1553 return new DirectoryLoader(
Steve McKay27d20a32016-02-22 18:38:09 -08001554 context, mType, mRoot, mDocument, contentsUri, state.userSortOrder,
1555 mSearchMode);
Aga Wronska893390b2016-02-17 13:50:42 -08001556 case TYPE_RECENT_OPEN:
1557 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1558 return new RecentsLoader(context, roots, state);
Steve McKay75fe6bc2016-03-07 11:51:31 -08001559
Aga Wronska893390b2016-02-17 13:50:42 -08001560 default:
1561 throw new IllegalStateException("Unknown type " + mType);
1562 }
1563 }
1564
1565 @Override
1566 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
1567 if (!isAdded()) return;
1568
1569 State state = getDisplayState();
1570
1571 mAdapter.notifyDataSetChanged();
1572 mModel.update(result);
1573
1574 state.derivedSortOrder = result.sortOrder;
1575
1576 updateLayout(state.derivedMode);
1577
1578 if (mSelection != null) {
1579 mSelectionManager.setItemsSelected(mSelection.toList(), true);
1580 }
1581
1582 // Restore any previous instance state
1583 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
1584 if (container != null && !getArguments().getBoolean(Shared.EXTRA_IGNORE_STATE, false)) {
1585 getView().restoreHierarchyState(container);
1586 } else if (mLastSortOrder != state.derivedSortOrder) {
1587 // The derived sort order takes the user sort order into account, but applies
1588 // directory-specific defaults when the user doesn't explicitly set the sort
1589 // order. Scroll to the top if the sort order actually changed.
1590 mRecView.smoothScrollToPosition(0);
1591 }
1592
1593 mLastSortOrder = state.derivedSortOrder;
1594
1595 mTuner.onModelLoaded(mModel, mType, mSearchMode);
1596
1597 }
1598
1599 @Override
1600 public void onLoaderReset(Loader<DirectoryResult> loader) {
1601 mModel.update(null);
1602 }
1603 }