blob: b3ce103b4c632783dec2151bac667e33fc0210b3 [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Steve McKayf8a5e082015-09-23 17:21:40 -070019import static com.android.documentsui.Shared.DEBUG;
Steve McKayfefcd702015-08-20 16:19:38 +000020import static com.android.documentsui.Shared.TAG;
Steve McKayf8a5e082015-09-23 17:21:40 -070021import static com.android.documentsui.State.ACTION_BROWSE;
22import static com.android.documentsui.State.ACTION_CREATE;
23import static com.android.documentsui.State.ACTION_MANAGE;
24import static com.android.documentsui.State.MODE_GRID;
25import static com.android.documentsui.State.MODE_LIST;
26import static com.android.documentsui.State.MODE_UNKNOWN;
27import static com.android.documentsui.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKayef280152015-06-11 10:10:49 -070031import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKayd57f5fa2015-07-23 16:33:41 -070032import static com.android.internal.util.Preconditions.checkState;
Steve McKay0599a442015-05-05 14:50:00 -070033
Ben Kwaf5858932015-04-07 15:43:39 -070034import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070035import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070036import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.FragmentManager;
38import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070040import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070041import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070043import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070045import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070047import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070050import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070054import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070056import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070057import android.os.Handler;
58import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070059import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070060import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070061import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070063import android.provider.DocumentsContract.Document;
Ben Kwa24be5d32015-08-27 16:04:46 -070064import android.support.annotation.Nullable;
Ben Kwa7461a952015-09-01 11:03:01 -070065import android.support.annotation.VisibleForTesting;
Ben Kwa91923182015-08-27 16:06:33 -070066import android.support.design.widget.Snackbar;
Steve McKayef280152015-06-11 10:10:49 -070067import android.support.v7.widget.GridLayoutManager;
68import android.support.v7.widget.LinearLayoutManager;
69import android.support.v7.widget.RecyclerView;
70import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070071import android.support.v7.widget.RecyclerView.RecyclerListener;
72import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070073import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070074import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070075import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070076import android.text.format.Time;
77import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070078import android.util.SparseArray;
Ben Kwa91923182015-08-27 16:06:33 -070079import android.util.SparseBooleanArray;
Ben Kwa0574b182015-09-08 07:31:19 -070080import android.util.TypedValue;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070081import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070082import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070085import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070086import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070087import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070088import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070089import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070090import android.view.ViewGroup;
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -070091import android.view.ViewParent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070092import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070093import android.widget.TextView;
94
Steve McKay351a7492015-08-04 10:11:01 -070095import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayef280152015-06-11 10:10:49 -070096import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070097import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070098import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070099import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900100import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700101import com.android.documentsui.model.RootInfo;
Steve McKayfad3d4a2015-09-22 15:09:21 -0700102
Steve McKayfefcd702015-08-20 16:19:38 +0000103import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700104
105import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700106import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700107import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700108
109/**
110 * Display the documents inside a single directory.
111 */
112public class DirectoryFragment extends Fragment {
113
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700114 public static final int TYPE_NORMAL = 1;
115 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700116 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700117
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700118 public static final int ANIM_NONE = 1;
119 public static final int ANIM_SIDE = 2;
120 public static final int ANIM_DOWN = 3;
121 public static final int ANIM_UP = 4;
122
Ben Kwaf5858932015-04-07 15:43:39 -0700123 public static final int REQUEST_COPY_DESTINATION = 1;
124
Steve McKayef280152015-06-11 10:10:49 -0700125 private static final int LOADER_ID = 42;
Steve McKay8e258c62015-05-06 14:27:57 -0700126 private static final boolean DEBUG_ENABLE_DND = false;
127
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700128 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700129 private static final String EXTRA_ROOT = "root";
130 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700131 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700132 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133
Ben Kwa7461a952015-09-01 11:03:01 -0700134 private Model mModel;
Ben Kwa379e1762015-09-21 10:49:52 -0700135 private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
Ben Kwa24be5d32015-08-27 16:04:46 -0700136
Steve McKayef280152015-06-11 10:10:49 -0700137 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700138
Steve McKayef280152015-06-11 10:10:49 -0700139 private View mEmptyView;
140 private RecyclerView mRecView;
141
142 private int mType = TYPE_NORMAL;
143 private String mStateKey;
144
145 private int mLastMode = MODE_UNKNOWN;
146 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
147 private boolean mLastShowSize;
148 private boolean mHideGridTitles;
149 private boolean mSvelteRecents;
150 private Point mThumbSize;
151 private DocumentsAdapter mAdapter;
152 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700153 private FragmentTuner mFragmentTuner;
154 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700155 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700156 private LinearLayoutManager mListLayout;
157 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700158 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700159
Ben Kwac42fa402015-09-16 08:04:37 -0700160 private MessageBar mMessageBar;
Ben Kwa379e1762015-09-21 10:49:52 -0700161 private View mProgressBar;
Ben Kwac42fa402015-09-16 08:04:37 -0700162
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700163 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
164 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700165 }
166
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700167 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
168 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700169 }
170
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700171 public static void showRecentsOpen(FragmentManager fm, int anim) {
172 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700173 }
174
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700175 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
176 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700178 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700179 args.putParcelable(EXTRA_ROOT, root);
180 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700181 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700182
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700183 final FragmentTransaction ft = fm.beginTransaction();
184 switch (anim) {
185 case ANIM_SIDE:
186 args.putBoolean(EXTRA_IGNORE_STATE, true);
187 break;
188 case ANIM_DOWN:
189 args.putBoolean(EXTRA_IGNORE_STATE, true);
190 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
191 break;
192 case ANIM_UP:
193 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
194 break;
195 }
196
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700197 final DirectoryFragment fragment = new DirectoryFragment();
198 fragment.setArguments(args);
199
Jeff Sharkey76112212013-08-06 11:26:10 -0700200 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700201 ft.commitAllowingStateLoss();
202 }
203
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700204 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
205 final StringBuilder builder = new StringBuilder();
206 builder.append(root != null ? root.authority : "null").append(';');
207 builder.append(root != null ? root.rootId : "null").append(';');
208 builder.append(doc != null ? doc.documentId : "null");
209 return builder.toString();
210 }
211
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700212 public static DirectoryFragment get(FragmentManager fm) {
213 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700214 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700215 }
216
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700217 @Override
218 public View onCreateView(
219 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
220 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700221 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700222 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
223
Ben Kwac42fa402015-09-16 08:04:37 -0700224 mMessageBar = MessageBar.create(getChildFragmentManager());
Ben Kwa379e1762015-09-21 10:49:52 -0700225 mProgressBar = view.findViewById(R.id.progressbar);
Ben Kwac42fa402015-09-16 08:04:37 -0700226
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700227 mEmptyView = view.findViewById(android.R.id.empty);
228
Steve McKayef280152015-06-11 10:10:49 -0700229 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
230 mRecView.setRecyclerListener(
231 new RecyclerListener() {
232 @Override
233 public void onViewRecycled(ViewHolder holder) {
234 cancelThumbnailTask(holder.itemView);
235 }
236 });
Steve McKay8e258c62015-05-06 14:27:57 -0700237
Steve McKayd57f5fa2015-07-23 16:33:41 -0700238 // TODO: Rather than update columns on layout changes, push this
239 // code (or something like it) into GridLayoutManager.
240 mRecView.addOnLayoutChangeListener(
241 new OnLayoutChangeListener() {
242
243 @Override
244 public void onLayoutChange(
245 View v, int left, int top, int right, int bottom, int oldLeft,
246 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000247 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700248 if (mGridLayout != null) {
249 mGridLayout.setSpanCount(mColumnCount);
250 }
251 }
252 });
253
254 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700255 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700256 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700257 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700258
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700259 return view;
260 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700261
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700262 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700263 public void onDestroyView() {
264 super.onDestroyView();
265
266 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700267 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700268 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700269 final View view = mRecView.getChildAt(i);
270 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700271 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700272
Steve McKayef280152015-06-11 10:10:49 -0700273 // Clear any outstanding selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700274 mModel.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700275 }
276
277 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700278 public void onActivityCreated(Bundle savedInstanceState) {
279 super.onActivityCreated(savedInstanceState);
280
281 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700282 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700283
Jeff Sharkey9656a532013-09-13 13:42:19 -0700284 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
285 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
286
Steve McKayef280152015-06-11 10:10:49 -0700287 mAdapter = new DocumentsAdapter(context);
288 mRecView.setAdapter(mAdapter);
289
290 GestureDetector.SimpleOnGestureListener listener =
291 new GestureDetector.SimpleOnGestureListener() {
292 @Override
293 public boolean onSingleTapUp(MotionEvent e) {
294 return DirectoryFragment.this.onSingleTapUp(e);
295 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700296 @Override
297 public boolean onDoubleTap(MotionEvent e) {
298 Log.d(TAG, "Handling double tap.");
299 return DirectoryFragment.this.onDoubleTap(e);
300 }
Steve McKayef280152015-06-11 10:10:49 -0700301 };
302
Ben Kwa24be5d32015-08-27 16:04:46 -0700303 // TODO: instead of inserting the view into the constructor, extract listener-creation code
304 // and set the listener on the view after the fact. Then the view doesn't need to be passed
305 // into the selection manager which is passed into the model.
306 MultiSelectManager selMgr= new MultiSelectManager(
Steve McKay57394872015-08-12 14:48:34 -0700307 mRecView,
308 listener,
309 state.allowMultiple
310 ? MultiSelectManager.MODE_MULTIPLE
311 : MultiSelectManager.MODE_SINGLE);
Ben Kwa24be5d32015-08-27 16:04:46 -0700312 selMgr.addCallback(new SelectionModeListener());
Ben Kwa7461a952015-09-01 11:03:01 -0700313
Ben Kwa379e1762015-09-21 10:49:52 -0700314 mModel = new Model(context, selMgr, mAdapter);
315 mModel.addUpdateListener(mModelUpdateListener);
Steve McKayef280152015-06-11 10:10:49 -0700316
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700317 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700318 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700319
Steve McKay1f199482015-05-20 15:58:42 -0700320 mFragmentTuner = pickFragmentTuner(state);
321 mClipper = new DocumentClipper(context);
322
Jeff Sharkey9656a532013-09-13 13:42:19 -0700323 if (mType == TYPE_RECENT_OPEN) {
324 // Hide titles when showing recents for picking images/videos
325 mHideGridTitles = MimePredicate.mimeMatches(
326 MimePredicate.VISUAL_MIMES, state.acceptMimes);
327 } else {
328 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
329 }
330
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700331 final ActivityManager am = (ActivityManager) context.getSystemService(
332 Context.ACTIVITY_SERVICE);
333 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
334
Jeff Sharkey46899c82013-08-18 22:26:48 -0700335 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700336 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700337 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700338 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700339
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700340 Uri contentsUri;
341 switch (mType) {
342 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700343 contentsUri = DocumentsContract.buildChildDocumentsUri(
344 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700345 if (state.action == ACTION_MANAGE) {
346 contentsUri = DocumentsContract.setManageMode(contentsUri);
347 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700348 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700349 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700350 case TYPE_SEARCH:
351 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700352 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700353 if (state.action == ACTION_MANAGE) {
354 contentsUri = DocumentsContract.setManageMode(contentsUri);
355 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700356 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700357 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700358 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700359 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700360 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700361 default:
362 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700363 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700364 }
365
366 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700367 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700368 if (!isAdded()) return;
369
Ben Kwa24be5d32015-08-27 16:04:46 -0700370 mModel.update(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700371
372 // Push latest state up to UI
373 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700374 if (result.mode != MODE_UNKNOWN) {
375 state.derivedMode = result.mode;
376 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700377 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700378 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700379
380 updateDisplayState();
381
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700382 // When launched into empty recents, show drawer
Ben Kwa24be5d32015-08-27 16:04:46 -0700383 if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
Steve McKayb68dd222015-04-20 17:18:15 -0700384 context instanceof DocumentsActivity) {
385 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700386 }
387
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700388 // Restore any previous instance state
389 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
390 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
391 getView().restoreHierarchyState(container);
392 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700393 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700394 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700395
396 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700397 }
398
399 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700400 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700401 mModel.update(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700402 }
403 };
404
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700405 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700406 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700407
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700408 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700409 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700410 }
411
Jeff Sharkey42d26792013-09-06 13:22:09 -0700412 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700413 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700414 // There's only one request code right now. Replace this with a switch statement or
415 // something more scalable when more codes are added.
416 if (requestCode != REQUEST_COPY_DESTINATION) {
417 return;
418 }
419 if (resultCode == Activity.RESULT_CANCELED || data == null) {
420 // User pressed the back button or otherwise cancelled the destination pick. Don't
421 // proceed with the copy.
422 return;
423 }
424
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900425 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Steve McKay4d0255f2015-09-25 16:02:56 -0700426 (DocumentStack) data.getParcelableExtra(Shared.EXTRA_STACK),
Ben Kwacb4461f2015-05-05 11:50:11 -0700427 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700428 }
429
Steve McKayef280152015-06-11 10:10:49 -0700430 private int getEventAdapterPosition(MotionEvent e) {
431 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
432 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
433 }
434
435 private boolean onSingleTapUp(MotionEvent e) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700436 if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700437 int position = getEventAdapterPosition(e);
438 if (position != RecyclerView.NO_POSITION) {
439 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700440 }
441 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700442 return false;
443 }
Steve McKayef280152015-06-11 10:10:49 -0700444
Steve McKay93d8ef42015-07-30 12:27:44 -0700445 protected boolean onDoubleTap(MotionEvent e) {
446 if (Events.isMouseEvent(e)) {
447 Log.d(TAG, "Handling double tap from mouse.");
448 int position = getEventAdapterPosition(e);
449 if (position != RecyclerView.NO_POSITION) {
450 return handleViewItem(position);
451 }
452 }
453 return false;
454 }
455
456 private boolean handleViewItem(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700457 final Cursor cursor = mModel.getItem(position);
Steve McKay93d8ef42015-07-30 12:27:44 -0700458 checkNotNull(cursor, "Cursor cannot be null.");
459 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
460 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
461 if (isDocumentEnabled(docMimeType, docFlags)) {
462 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwa24be5d32015-08-27 16:04:46 -0700463 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
464 mModel.clearSelection();
Steve McKay93d8ef42015-07-30 12:27:44 -0700465 return true;
466 }
Steve McKayef280152015-06-11 10:10:49 -0700467 return false;
468 }
469
Ben Kwaf5858932015-04-07 15:43:39 -0700470 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700471 public void onStop() {
472 super.onStop();
473
474 // Remember last scroll location
475 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
476 getView().saveHierarchyState(container);
477 final State state = getDisplayState(this);
478 state.dirState.put(mStateKey, container);
479 }
480
481 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700482 public void onResume() {
483 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700484 updateDisplayState();
485 }
486
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700487 public void onDisplayStateChanged() {
488 updateDisplayState();
489 }
490
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700491 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700492 // Sort order change always triggers reload; we'll trigger state change
493 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700494 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700495 }
496
497 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700498 final ContentResolver resolver = getActivity().getContentResolver();
499 final State state = getDisplayState(this);
500
501 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
502 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
503
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700504 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700505 final Uri stateUri = RecentsProvider.buildState(
506 root.authority, root.rootId, doc.documentId);
507 final ContentValues values = new ContentValues();
508 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700509
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700510 new AsyncTask<Void, Void, Void>() {
511 @Override
512 protected Void doInBackground(Void... params) {
513 resolver.insert(stateUri, values);
514 return null;
515 }
516 }.execute();
517 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700518
519 // Mode change is just visual change; no need to kick loader, and
520 // deliver change event immediately.
521 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700522 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700523
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700524 updateDisplayState();
525 }
526
527 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700528 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700529
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700530 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700531 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700532 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700533
Steve McKayef280152015-06-11 10:10:49 -0700534 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700535
Steve McKayef280152015-06-11 10:10:49 -0700536 mRecView.setAdapter(mAdapter);
537 }
538
539 /**
540 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
541 * classes as needed.
542 */
543 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700544 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700545
546 final LayoutManager layout;
547 switch (mode) {
548 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700549 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700550 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700551 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700552 }
Steve McKayef280152015-06-11 10:10:49 -0700553 layout = mGridLayout;
554 break;
555 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700556 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700557 if (mListLayout == null) {
558 mListLayout = new LinearLayoutManager(getContext());
559 }
Steve McKayef280152015-06-11 10:10:49 -0700560 layout = mListLayout;
561 break;
562 case MODE_UNKNOWN:
563 default:
564 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700565 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700566
Steve McKayef280152015-06-11 10:10:49 -0700567 mRecView.setLayoutManager(layout);
Kyle Horimoto2da6e4a2015-08-27 16:44:00 -0700568 // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
569 // imperatively calling this function.
Steve McKay9058e042015-09-01 12:31:24 -0700570 mModel.mSelectionManager.handleLayoutChanged();
Steve McKayef280152015-06-11 10:10:49 -0700571 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700572 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700573 }
574
Steve McKayfefcd702015-08-20 16:19:38 +0000575 private int calculateColumnCount() {
576 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
577 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700578 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000579
Steve McKayd57f5fa2015-07-23 16:33:41 -0700580 checkState(mRecView.getWidth() > 0);
581 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000582 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
583
Steve McKayd57f5fa2015-07-23 16:33:41 -0700584 return columnCount;
585 }
586
Steve McKayef280152015-06-11 10:10:49 -0700587 /**
588 * Manages the integration between our ActionMode and MultiSelectManager, initiating
589 * ActionMode when there is a selection, canceling it when there is no selection,
590 * and clearing selection when action mode is explicitly exited by the user.
591 */
592 private final class SelectionModeListener
593 implements MultiSelectManager.Callback, ActionMode.Callback {
594
595 private Selection mSelected = new Selection();
596 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700597 private int mNoDeleteCount = 0;
598 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700599
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700600 @Override
Steve McKayef280152015-06-11 10:10:49 -0700601 public boolean onBeforeItemStateChange(int position, boolean selected) {
Ben Kwac42fa402015-09-16 08:04:37 -0700602 // Directories cannot be checked
Steve McKayef280152015-06-11 10:10:49 -0700603 if (selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700604 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700605 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700606 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
607 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700608 return isDocumentEnabled(docMimeType, docFlags);
609 }
610 return true;
611 }
612
613 @Override
614 public void onItemStateChanged(int position, boolean selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700615 final Cursor cursor = mModel.getItem(position);
Steve McKay4f4232d2015-07-22 12:13:46 -0700616 checkNotNull(cursor, "Cursor cannot be null.");
617
618 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
619 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
620 mNoDeleteCount += selected ? 1 : -1;
621 }
Steve McKay57394872015-08-12 14:48:34 -0700622 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700623
Steve McKay57394872015-08-12 14:48:34 -0700624 @Override
625 public void onSelectionChanged() {
Ben Kwa24be5d32015-08-27 16:04:46 -0700626 mModel.getSelection(mSelected);
Ben Kwafe18c1b2015-09-11 15:40:18 -0700627 TypedValue color = new TypedValue();
Steve McKay4f4232d2015-07-22 12:13:46 -0700628 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700629 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
630 if (mActionMode == null) {
631 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
632 mActionMode = getActivity().startActionMode(this);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700633 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700634 getActivity().getTheme().resolveAttribute(
635 R.attr.colorActionMode, color, true);
Steve McKay4f4232d2015-07-22 12:13:46 -0700636 updateActionMenu();
637 } else {
638 if (DEBUG) Log.d(TAG, "Finishing action mode.");
639 if (mActionMode != null) {
640 mActionMode.finish();
641 }
Ben Kwa0574b182015-09-08 07:31:19 -0700642 getActivity().getTheme().resolveAttribute(
643 android.R.attr.colorPrimaryDark, color, true);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700644 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700645 getActivity().getWindow().setStatusBarColor(color.data);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700646
Steve McKayef280152015-06-11 10:10:49 -0700647 if (mActionMode != null) {
648 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
649 }
650 }
651
652 // Called when the user exits the action mode
653 @Override
654 public void onDestroyActionMode(ActionMode mode) {
655 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
656 mActionMode = null;
657 // clear selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700658 mModel.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700659 mSelected.clear();
660 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700661 }
662
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700663 @Override
664 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
665 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa24be5d32015-08-27 16:04:46 -0700666 mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
667 return mModel.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700668 }
669
670 @Override
671 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700672 mMenu = menu;
673 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700674 return true;
675 }
676
Steve McKay4f4232d2015-07-22 12:13:46 -0700677 private void updateActionMenu() {
678 checkNotNull(mMenu);
679 // Delegate update logic to our owning action, since specialized logic is desired.
680 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
681 }
682
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700683 @Override
Steve McKayef280152015-06-11 10:10:49 -0700684 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700685
Ben Kwa24be5d32015-08-27 16:04:46 -0700686 Selection selection = mModel.getSelection(new Selection());
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700687
Jeff Sharkey873daa32013-08-18 17:38:20 -0700688 final int id = item.getItemId();
689 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700690 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700691 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700692 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700693
694 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700695 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700696 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700697 return true;
698
699 } else if (id == R.id.menu_delete) {
Ben Kwa33ba3342015-09-24 15:03:51 -0700700 // Exit selection mode first, so we avoid deselecting deleted documents.
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700701 mode.finish();
Ben Kwa33ba3342015-09-24 15:03:51 -0700702 deleteDocuments(selection);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700703 return true;
704
Steve McKay1f199482015-05-20 15:58:42 -0700705 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700706 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700707 mode.finish();
708 return true;
709
Steve McKay1f199482015-05-20 15:58:42 -0700710 } else if (id == R.id.menu_move_to) {
Ben Kwa33ba3342015-09-24 15:03:51 -0700711 // Exit selection mode first, so we avoid deselecting deleted documents.
Ben Kwa41b26c12015-03-31 10:11:43 -0700712 mode.finish();
Ben Kwa33ba3342015-09-24 15:03:51 -0700713 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700714 return true;
715
Steve McKay1f199482015-05-20 15:58:42 -0700716 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700717 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700718 mode.finish();
719 return true;
720
Ben Kwa512a6ba2015-03-31 08:15:21 -0700721 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700722 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700723 return true;
724
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700725 } else {
726 return false;
727 }
728 }
Steve McKayef280152015-06-11 10:10:49 -0700729 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700730
Steve McKayef280152015-06-11 10:10:49 -0700731 private static void cancelThumbnailTask(View view) {
732 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
733 if (iconThumb != null) {
734 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
735 if (oldTask != null) {
736 oldTask.preempt();
737 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700738 }
739 }
Steve McKayef280152015-06-11 10:10:49 -0700740 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700741
Steve McKayef280152015-06-11 10:10:49 -0700742 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700743 new GetDocumentsTask() {
744 @Override
745 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700746 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700747 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700748 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700749 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700750 }
751
Steve McKayef280152015-06-11 10:10:49 -0700752 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700753 new GetDocumentsTask() {
754 @Override
755 void onDocumentsReady(List<DocumentInfo> docs) {
756 Intent intent;
757
758 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000759 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700760 for (DocumentInfo doc: docs) {
761 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
762 docsForSend.add(doc);
763 }
764 }
765
766 if (docsForSend.size() == 1) {
767 final DocumentInfo doc = docsForSend.get(0);
768
769 intent = new Intent(Intent.ACTION_SEND);
770 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
771 intent.addCategory(Intent.CATEGORY_DEFAULT);
772 intent.setType(doc.mimeType);
773 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
774
775 } else if (docsForSend.size() > 1) {
776 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
777 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
778 intent.addCategory(Intent.CATEGORY_DEFAULT);
779
Steve McKayfefcd702015-08-20 16:19:38 +0000780 final ArrayList<String> mimeTypes = new ArrayList<>();
781 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700782 for (DocumentInfo doc : docsForSend) {
783 mimeTypes.add(doc.mimeType);
784 uris.add(doc.derivedUri);
785 }
786
787 intent.setType(findCommonMimeType(mimeTypes));
788 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
789
790 } else {
791 return;
792 }
793
794 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
795 startActivity(intent);
796 }
797 }.execute(selected);
798 }
799
Steve McKayef280152015-06-11 10:10:49 -0700800 private void deleteDocuments(final Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -0700801 Context context = getActivity();
802 ContentResolver resolver = context.getContentResolver();
803 String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700804
Ben Kwa91923182015-08-27 16:06:33 -0700805 mModel.markForDeletion(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700806
Ben Kwac4693342015-09-30 10:00:10 -0700807 final Activity activity = getActivity();
808 Shared.makeSnackbar(activity, message, Snackbar.LENGTH_LONG)
Ben Kwa91923182015-08-27 16:06:33 -0700809 .setAction(
810 R.string.undo,
811 new android.view.View.OnClickListener() {
812 @Override
813 public void onClick(View view) {}
814 })
815 .setCallback(
816 new Snackbar.Callback() {
817 @Override
818 public void onDismissed(Snackbar snackbar, int event) {
819 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
820 mModel.undoDeletion();
821 } else {
Ben Kwa83cedf22015-09-11 15:15:45 -0700822 mModel.finalizeDeletion(
Ben Kwac21888e2015-09-30 14:14:16 -0700823 new Model.DeletionListener() {
Ben Kwa83cedf22015-09-11 15:15:45 -0700824 @Override
Ben Kwac21888e2015-09-30 14:14:16 -0700825 public void onError() {
Ben Kwac4693342015-09-30 10:00:10 -0700826 Shared.makeSnackbar(
827 activity,
Ben Kwa83cedf22015-09-11 15:15:45 -0700828 R.string.toast_failed_delete,
829 Snackbar.LENGTH_LONG)
830 .show();
831
832 }
833 });
Ben Kwa91923182015-08-27 16:06:33 -0700834 }
Ben Kwa91923182015-08-27 16:06:33 -0700835 }
836 })
837 .show();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700838 }
839
Steve McKayef280152015-06-11 10:10:49 -0700840 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700841 // Pop up a dialog to pick a destination. This is inadequate but works for now.
842 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900843 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900844 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900845 Uri.EMPTY,
846 getActivity(),
847 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700848
849 new GetDocumentsTask() {
850 @Override
851 void onDocumentsReady(List<DocumentInfo> docs) {
852 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
853
854 boolean directoryCopy = false;
855 for (DocumentInfo info : docs) {
856 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
857 directoryCopy = true;
858 break;
859 }
860 }
861 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
862 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
863 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900864 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700865 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700866 }
867
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700868 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700869 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700870 }
871
Steve McKayef280152015-06-11 10:10:49 -0700872 // Provide a reference to the views for each data item
873 // Complex data items may need more than one view per item, and
874 // you provide access to all the views for a data item in a view holder
875 private static final class DocumentHolder extends RecyclerView.ViewHolder {
876 // each data item is just a string in this case
877 public View view;
878 public String docId; // The stable document id.
879 public DocumentHolder(View view) {
880 super(view);
881 this.view = view;
882 }
883 }
884
Ben Kwa2f975262015-09-16 13:15:38 -0700885 void showEmptyView() {
886 mEmptyView.setVisibility(View.VISIBLE);
887 mRecView.setVisibility(View.GONE);
888 TextView msg = (TextView) mEmptyView.findViewById(R.id.message);
889 msg.setText(R.string.empty);
890 // No retry button for the empty view.
891 mEmptyView.findViewById(R.id.button_retry).setVisibility(View.GONE);
892 }
893
894 void showErrorView() {
895 mEmptyView.setVisibility(View.VISIBLE);
896 mRecView.setVisibility(View.GONE);
897 TextView msg = (TextView) mEmptyView.findViewById(R.id.message);
898 msg.setText(R.string.query_error);
899 // TODO: Enable this once the retry button does something.
900 mEmptyView.findViewById(R.id.button_retry).setVisibility(View.GONE);
901 }
902
903 void showRecyclerView() {
904 mEmptyView.setVisibility(View.GONE);
905 mRecView.setVisibility(View.VISIBLE);
906 }
907
Ben Kwa379e1762015-09-21 10:49:52 -0700908 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
Steve McKayef280152015-06-11 10:10:49 -0700909
910 private final Context mContext;
911 private final LayoutInflater mInflater;
Steve McKayef280152015-06-11 10:10:49 -0700912
Steve McKayef280152015-06-11 10:10:49 -0700913 public DocumentsAdapter(Context context) {
914 mContext = context;
915 mInflater = LayoutInflater.from(context);
916 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700917
Ben Kwac42fa402015-09-16 08:04:37 -0700918 @Override
Steve McKayef280152015-06-11 10:10:49 -0700919 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
920 final State state = getDisplayState(DirectoryFragment.this);
921 final LayoutInflater inflater = LayoutInflater.from(getContext());
922 switch (state.derivedMode) {
923 case MODE_GRID:
924 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
925 case MODE_LIST:
926 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
927 case MODE_UNKNOWN:
928 default:
929 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -0700930 }
931 }
932
Steve McKayef280152015-06-11 10:10:49 -0700933 @Override
934 public void onBindViewHolder(DocumentHolder holder, int position) {
935
936 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700937 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700938 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700939 final RootsCache roots = DocumentsApplication.getRootsCache(context);
940 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
941 context, mThumbSize);
942
Ben Kwa24be5d32015-08-27 16:04:46 -0700943 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700944 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700945
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700946 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
947 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700948 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
949 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
950 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
951 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
952 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
953 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
954 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
955 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700956
Steve McKayef280152015-06-11 10:10:49 -0700957 holder.docId = docId;
958 final View itemView = holder.view;
Ben Kwa24be5d32015-08-27 16:04:46 -0700959 itemView.setActivated(mModel.isSelected(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -0700960
Steve McKayef280152015-06-11 10:10:49 -0700961 final View line1 = itemView.findViewById(R.id.line1);
962 final View line2 = itemView.findViewById(R.id.line2);
963
964 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
965 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
966 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
967 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
968 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
969 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
970 final TextView date = (TextView) itemView.findViewById(R.id.date);
971 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700972
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700973 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700974 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700975 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700976 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700977 }
978
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700979 iconMime.animate().cancel();
980 iconThumb.animate().cancel();
981
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700982 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700983 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700984 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700985 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700986
Jeff Sharkey7e544612014-08-29 15:38:27 -0700987 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
988 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
989
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700990 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700991 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700992 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700993 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700994 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700995 iconThumb.setImageBitmap(cachedResult);
996 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700997 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700998 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -0700999 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001000 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001001 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001002 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001003 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001004 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001005 }
1006
1007 // Always throw MIME icon into place, even when a thumbnail is being
1008 // loaded in background.
1009 if (cacheHit) {
1010 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001011 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001012 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001013 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001014 iconMime.setAlpha(1f);
1015 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001016 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001017 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001018 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001019 }
1020
Jeff Sharkey9656a532013-09-13 13:42:19 -07001021 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001022 boolean hasLine2 = false;
1023
Jeff Sharkey9656a532013-09-13 13:42:19 -07001024 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1025 if (!hideTitle) {
1026 title.setText(docDisplayName);
1027 hasLine1 = true;
1028 }
1029
1030 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001031 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001032 // We've already had to enumerate roots before any results can
1033 // be shown, so this will never block.
1034 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001035 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001036 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001037 } else {
Steve McKayef280152015-06-11 10:10:49 -07001038 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001039 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001040
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001041 if (summary != null) {
1042 final boolean alwaysShowSummary = getResources()
1043 .getBoolean(R.bool.always_show_summary);
1044 if (alwaysShowSummary) {
1045 summary.setText(root.getDirectoryString());
1046 summary.setVisibility(View.VISIBLE);
1047 hasLine2 = true;
1048 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001049 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001050 // No summary needed if icon speaks for itself
1051 summary.setVisibility(View.INVISIBLE);
1052 } else {
1053 summary.setText(root.getDirectoryString());
1054 summary.setVisibility(View.VISIBLE);
1055 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1056 hasLine2 = true;
1057 }
1058 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001059 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001060 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001061 // Directories showing thumbnails in grid mode get a little icon
1062 // hint to remind user they're a directory.
1063 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1064 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001065 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001066 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001067 }
1068
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001069 if (summary != null) {
1070 if (docSummary != null) {
1071 summary.setText(docSummary);
1072 summary.setVisibility(View.VISIBLE);
1073 hasLine2 = true;
1074 } else {
1075 summary.setVisibility(View.INVISIBLE);
1076 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001077 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001078 }
1079
Jeff Sharkey9656a532013-09-13 13:42:19 -07001080 if (icon1 != null) icon1.setVisibility(View.GONE);
1081 if (icon2 != null) icon2.setVisibility(View.GONE);
1082
1083 if (iconDrawable != null) {
1084 if (hasLine1) {
1085 icon1.setVisibility(View.VISIBLE);
1086 icon1.setImageDrawable(iconDrawable);
1087 } else {
1088 icon2.setVisibility(View.VISIBLE);
1089 icon2.setImageDrawable(iconDrawable);
1090 }
1091 }
1092
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001093 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001094 date.setText(null);
1095 } else {
Steve McKayef280152015-06-11 10:10:49 -07001096 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001097 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001098 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001099
1100 if (state.showSize) {
1101 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001102 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001103 size.setText(null);
1104 } else {
Steve McKayef280152015-06-11 10:10:49 -07001105 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001106 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001107 }
1108 } else {
1109 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001110 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001111
Jeff Sharkey9656a532013-09-13 13:42:19 -07001112 if (line1 != null) {
1113 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1114 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001115 if (line2 != null) {
1116 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1117 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001118
Steve McKayef280152015-06-11 10:10:49 -07001119 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001120
1121 iconMime.setAlpha(iconAlpha);
1122 iconThumb.setAlpha(iconAlpha);
1123 if (icon1 != null) icon1.setAlpha(iconAlpha);
1124 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001125
Steve McKay8e258c62015-05-06 14:27:57 -07001126 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001127 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001128 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001129 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001130
Steve McKay351a7492015-08-04 10:11:01 -07001131 @Override
Steve McKayef280152015-06-11 10:10:49 -07001132 public int getItemCount() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001133 return mModel.getItemCount();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001134 }
1135
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001136 }
1137
1138 private static String formatTime(Context context, long when) {
1139 // TODO: DateUtils should make this easier
1140 Time then = new Time();
1141 then.set(when);
1142 Time now = new Time();
1143 now.setToNow();
1144
1145 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1146 | DateUtils.FORMAT_ABBREV_ALL;
1147
1148 if (then.year != now.year) {
1149 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1150 } else if (then.yearDay != now.yearDay) {
1151 flags |= DateUtils.FORMAT_SHOW_DATE;
1152 } else {
1153 flags |= DateUtils.FORMAT_SHOW_TIME;
1154 }
1155
1156 return DateUtils.formatDateTime(context, when, flags);
1157 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001158
1159 private String findCommonMimeType(List<String> mimeTypes) {
1160 String[] commonType = mimeTypes.get(0).split("/");
1161 if (commonType.length != 2) {
1162 return "*/*";
1163 }
1164
1165 for (int i = 1; i < mimeTypes.size(); i++) {
1166 String[] type = mimeTypes.get(i).split("/");
1167 if (type.length != 2) continue;
1168
1169 if (!commonType[1].equals(type[1])) {
1170 commonType[1] = "*";
1171 }
1172
1173 if (!commonType[0].equals(type[0])) {
1174 commonType[0] = "*";
1175 commonType[1] = "*";
1176 break;
1177 }
1178 }
1179
1180 return commonType[0] + "/" + commonType[1];
1181 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001182
1183 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001184 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001185 if (v.isEnabled() == enabled) return;
1186 v.setEnabled(enabled);
1187
1188 if (v instanceof ViewGroup) {
1189 final ViewGroup vg = (ViewGroup) v;
1190 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1191 setEnabledRecursive(vg.getChildAt(i), enabled);
1192 }
1193 }
1194 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001195
1196 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1197 final State state = getDisplayState(DirectoryFragment.this);
1198
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001199 // Directories are always enabled
1200 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1201 return true;
1202 }
1203
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001204 // Read-only files are disabled when creating
1205 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1206 return false;
1207 }
1208
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001209 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1210 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001211
Steve McKay1f199482015-05-20 15:58:42 -07001212 private void copyFromClipboard() {
1213 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1214
1215 @Override
1216 protected List<DocumentInfo> doInBackground(Void... params) {
1217 return mClipper.getClippedDocuments();
1218 }
1219
1220 @Override
1221 protected void onPostExecute(List<DocumentInfo> docs) {
1222 DocumentInfo destination =
1223 ((BaseActivity) getActivity()).getCurrentDirectory();
1224 copyDocuments(docs, destination);
1225 }
1226 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001227 }
1228
Steve McKay1f199482015-05-20 15:58:42 -07001229 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001230 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001231 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001232
Steve McKay1f199482015-05-20 15:58:42 -07001233 @Override
1234 protected List<DocumentInfo> doInBackground(Void... params) {
1235 return mClipper.getDocumentsFromClipData(clipData);
1236 }
1237
1238 @Override
1239 protected void onPostExecute(List<DocumentInfo> docs) {
1240 copyDocuments(docs, destination);
1241 }
1242 }.execute();
1243 }
1244
1245 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1246 if (!canCopy(docs, destination)) {
Ben Kwac4693342015-09-30 10:00:10 -07001247 Shared.makeSnackbar(
Steve McKay1f199482015-05-20 15:58:42 -07001248 getActivity(),
Ben Kwac4693342015-09-30 10:00:10 -07001249 R.string.clipboard_files_cannot_paste,
1250 Snackbar.LENGTH_SHORT)
1251 .show();
Steve McKay0599a442015-05-05 14:50:00 -07001252 return;
1253 }
1254
Steve McKay1f199482015-05-20 15:58:42 -07001255 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001256 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001257 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001258
Steve McKay1f199482015-05-20 15:58:42 -07001259 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001260 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001261 if (destination != null) {
1262 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001263 tmpStack.addAll(curStack);
1264 } else {
1265 tmpStack = curStack;
1266 }
1267
Steve McKay1f199482015-05-20 15:58:42 -07001268 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001269 }
1270
1271 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1272 Context context = getActivity();
1273 final ContentResolver resolver = context.getContentResolver();
1274 ClipData clipData = null;
1275 for (DocumentInfo doc : docs) {
1276 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1277 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001278 // TODO: figure out what this string should be.
1279 // Currently it is not displayed anywhere in the UI, but this might change.
1280 final String label = "";
1281 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001282 } else {
1283 // TODO: update list of mime types in ClipData.
1284 clipData.addItem(new ClipData.Item(uri));
1285 }
1286 }
1287 return clipData;
1288 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001289
Steve McKay1f199482015-05-20 15:58:42 -07001290 void copySelectedToClipboard() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001291 Selection sel = mModel.getSelection(new Selection());
Steve McKayef280152015-06-11 10:10:49 -07001292 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001293 }
Steve McKay0599a442015-05-05 14:50:00 -07001294
Steve McKayef280152015-06-11 10:10:49 -07001295 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001296 new GetDocumentsTask() {
1297 @Override
1298 void onDocumentsReady(List<DocumentInfo> docs) {
1299 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001300 Activity activity = getActivity();
Ben Kwac4693342015-09-30 10:00:10 -07001301 Shared.makeSnackbar(activity,
Steve McKay1f199482015-05-20 15:58:42 -07001302 activity.getResources().getQuantityString(
1303 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
Ben Kwac4693342015-09-30 10:00:10 -07001304 Snackbar.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001305 }
Steve McKayef280152015-06-11 10:10:49 -07001306 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001307 }
1308
1309 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001310 copyFromClipboard();
1311 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001312 }
1313
Steve McKay0599a442015-05-05 14:50:00 -07001314 /**
1315 * Returns true if the list of files can be copied to destination. Note that this
1316 * is a policy check only. Currently the method does not attempt to verify
1317 * available space or any other environmental aspects possibly resulting in
1318 * failure to copy.
1319 *
1320 * @return true if the list of files can be copied to destination.
1321 */
1322 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
Ben Kwa91923182015-08-27 16:06:33 -07001323 BaseActivity activity = (BaseActivity) getActivity();
Steve McKay0599a442015-05-05 14:50:00 -07001324
1325 final RootInfo root = activity.getCurrentRoot();
1326
1327 // Can't copy folders to Downloads.
1328 if (root.isDownloads()) {
1329 for (DocumentInfo docs : files) {
1330 if (docs.isDirectory()) {
1331 return false;
1332 }
1333 }
1334 }
1335
1336 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1337 }
1338
1339 void selectAllFiles() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001340 boolean changed = mModel.selectAll();
Steve McKay9459a7c2015-07-24 13:14:20 -07001341 if (changed) {
1342 updateDisplayState();
1343 }
Steve McKay0599a442015-05-05 14:50:00 -07001344 }
1345
Steve McKayef280152015-06-11 10:10:49 -07001346 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001347 // Listen for drops on non-directory items and empty space.
1348 view.setOnDragListener(mOnDragListener);
1349 }
1350
1351 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1352 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1353 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1354 // Make a directory item a drop target. Drop on non-directories and empty space
1355 // is handled at the list/grid view level.
1356 view.setOnDragListener(mOnDragListener);
1357 }
1358
1359 // Temporary: attaching the listener to the title only.
1360 // Attaching to the entire item conflicts with the item long click handler responsible
1361 // for item selection.
1362 final View title = view.findViewById(android.R.id.title);
1363 title.setOnLongClickListener(mLongClickListener);
1364 }
1365
1366 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1367 @Override
1368 public boolean onDrag(View v, DragEvent event) {
1369 switch (event.getAction()) {
1370 case DragEvent.ACTION_DRAG_STARTED:
1371 // TODO: Check if the event contains droppable data.
1372 return true;
1373
1374 // TODO: Highlight potential drop target directory?
1375 // TODO: Expand drop target directory on hover?
1376 case DragEvent.ACTION_DRAG_ENTERED:
1377 case DragEvent.ACTION_DRAG_LOCATION:
1378 case DragEvent.ACTION_DRAG_EXITED:
1379 case DragEvent.ACTION_DRAG_ENDED:
1380 return true;
1381
1382 case DragEvent.ACTION_DROP:
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001383 int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001384 DocumentInfo dstDir = null;
1385 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001386 Cursor dstCursor = mModel.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001387 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001388 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1389 // TODO: Do not drop into the directory where the documents came from.
1390 }
1391 copyFromClipData(event.getClipData(), dstDir);
1392 return true;
1393 }
1394 return false;
1395 }
1396 };
1397
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001398 private View getContainingItemView(View view) {
1399 while (true) {
1400 if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
1401 return view;
1402 }
1403 ViewParent parent = view.getParent();
1404 if (parent == null || !(parent instanceof View)) {
1405 return null;
1406 }
1407 view = (View) parent;
1408 }
1409 }
1410
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001411 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1412 @Override
1413 public boolean onLongClick(View v) {
1414 final List<DocumentInfo> docs = getDraggableDocuments(v);
1415 if (docs.isEmpty()) {
1416 return false;
1417 }
1418 v.startDrag(
1419 getClipDataFromDocuments(docs),
1420 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1421 null,
Vladislav Kaznacheeve3ce8a92015-07-15 18:04:04 -07001422 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
1423 View.DRAG_FLAG_GLOBAL_URI_WRITE
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001424 );
1425 return true;
1426 }
1427 };
1428
1429 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001430 int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001431 if (position == android.widget.AdapterView.INVALID_POSITION) {
1432 return Collections.EMPTY_LIST;
1433 }
1434
Ben Kwa24be5d32015-08-27 16:04:46 -07001435 final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001436 if (!selectedDocs.isEmpty()) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001437 if (!mModel.isSelected(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001438 // There is a selection that does not include the current item, drag nothing.
1439 return Collections.EMPTY_LIST;
1440 }
1441 return selectedDocs;
1442 }
1443
Ben Kwa24be5d32015-08-27 16:04:46 -07001444 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001445 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001446 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001447
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001448 return Lists.newArrayList(doc);
1449 }
1450
1451 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1452 if (docs.size() == 1) {
1453 final DocumentInfo doc = docs.get(0);
1454 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1455 doc.mimeType, doc.icon, getDisplayState(this));
1456 }
1457 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1458 }
1459
1460 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1461 String docMimeType, int docIcon, State state) {
1462 if (docIcon != 0) {
1463 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1464 } else {
1465 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1466 state.derivedMode);
1467 }
1468 }
1469
Steve McKayef280152015-06-11 10:10:49 -07001470 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1471 implements Preemptable {
1472 private final Uri mUri;
1473 private final ImageView mIconMime;
1474 private final ImageView mIconThumb;
1475 private final Point mThumbSize;
1476 private final float mTargetAlpha;
1477 private final CancellationSignal mSignal;
1478
1479 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1480 float targetAlpha) {
1481 mUri = uri;
1482 mIconMime = iconMime;
1483 mIconThumb = iconThumb;
1484 mThumbSize = thumbSize;
1485 mTargetAlpha = targetAlpha;
1486 mSignal = new CancellationSignal();
1487 }
1488
1489 @Override
1490 public void preempt() {
1491 cancel(false);
1492 mSignal.cancel();
1493 }
1494
1495 @Override
1496 protected Bitmap doInBackground(Uri... params) {
1497 if (isCancelled()) return null;
1498
1499 final Context context = mIconThumb.getContext();
1500 final ContentResolver resolver = context.getContentResolver();
1501
1502 ContentProviderClient client = null;
1503 Bitmap result = null;
1504 try {
1505 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1506 resolver, mUri.getAuthority());
1507 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1508 if (result != null) {
1509 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1510 context, mThumbSize);
1511 thumbs.put(mUri, result);
1512 }
1513 } catch (Exception e) {
1514 if (!(e instanceof OperationCanceledException)) {
1515 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1516 }
1517 } finally {
1518 ContentProviderClient.releaseQuietly(client);
1519 }
1520 return result;
1521 }
1522
1523 @Override
1524 protected void onPostExecute(Bitmap result) {
1525 if (mIconThumb.getTag() == this && result != null) {
1526 mIconThumb.setTag(null);
1527 mIconThumb.setImageBitmap(result);
1528
1529 mIconMime.setAlpha(mTargetAlpha);
1530 mIconMime.animate().alpha(0f).start();
1531 mIconThumb.setAlpha(0f);
1532 mIconThumb.animate().alpha(mTargetAlpha).start();
1533 }
1534 }
1535 }
1536
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001537 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1538
1539 private final Drawable mShadow;
1540
1541 private final int mShadowDimension;
1542
1543 public DrawableShadowBuilder(Drawable shadow) {
1544 mShadow = shadow;
1545 mShadowDimension = getResources().getDimensionPixelSize(
1546 R.dimen.drag_shadow_size);
1547 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1548 }
1549
Ben Kwa24be5d32015-08-27 16:04:46 -07001550 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001551 public void onProvideShadowMetrics(
1552 Point shadowSize, Point shadowTouchPoint) {
1553 shadowSize.set(mShadowDimension, mShadowDimension);
1554 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1555 }
1556
Ben Kwa24be5d32015-08-27 16:04:46 -07001557 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001558 public void onDrawShadow(Canvas canvas) {
1559 mShadow.draw(canvas);
1560 }
1561 }
Steve McKay1f199482015-05-20 15:58:42 -07001562
1563 private FragmentTuner pickFragmentTuner(final State state) {
Steve McKay459bc2b2015-09-16 15:07:31 -07001564 return state.action == ACTION_BROWSE
Steve McKay0fbfc652015-08-20 16:48:49 -07001565 ? new FilesTuner()
Steve McKay459bc2b2015-09-16 15:07:31 -07001566 : new DefaultTuner(state.action);
Steve McKay1f199482015-05-20 15:58:42 -07001567 }
1568
1569 /**
1570 * Interface for specializing the Fragment for the "host" Activity.
1571 * Feel free to expand the role of this class to handle other specializations.
1572 */
1573 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001574 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001575 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001576 }
1577
1578 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001579 * Abstract task providing support for loading documents *off*
1580 * the main thread. And if it isn't obvious, creating a list
1581 * of documents (especially large lists) can be pretty expensive.
1582 */
1583 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001584 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001585 @Override
Steve McKayef280152015-06-11 10:10:49 -07001586 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001587 return mModel.getDocuments(selected[0]);
Steve McKay9276f3b2015-05-27 16:11:42 -07001588 }
1589
1590 @Override
1591 protected final void onPostExecute(List<DocumentInfo> docs) {
1592 onDocumentsReady(docs);
1593 }
1594
1595 abstract void onDocumentsReady(List<DocumentInfo> docs);
1596 }
1597
1598 /**
Steve McKay1f199482015-05-20 15:58:42 -07001599 * Provides support for Platform specific specializations of DirectoryFragment.
1600 */
1601 private static final class DefaultTuner implements FragmentTuner {
1602
Steve McKay459bc2b2015-09-16 15:07:31 -07001603 private final boolean mManaging;
Steve McKay1f199482015-05-20 15:58:42 -07001604
Steve McKay459bc2b2015-09-16 15:07:31 -07001605 public DefaultTuner(int action) {
1606 mManaging = (action == ACTION_MANAGE);
Steve McKay1f199482015-05-20 15:58:42 -07001607 }
1608
1609 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001610 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001611
1612 final MenuItem open = menu.findItem(R.id.menu_open);
1613 final MenuItem share = menu.findItem(R.id.menu_share);
1614 final MenuItem delete = menu.findItem(R.id.menu_delete);
1615 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1616 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1617 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1618
Steve McKay459bc2b2015-09-16 15:07:31 -07001619 open.setVisible(!mManaging);
1620 share.setVisible(mManaging);
1621 delete.setVisible(mManaging && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001622 // Disable copying from the Recents view.
Steve McKay459bc2b2015-09-16 15:07:31 -07001623 copyTo.setVisible(mManaging && dirType != TYPE_RECENT_OPEN);
Steve McKay1f199482015-05-20 15:58:42 -07001624 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1625
Steve McKay0fbfc652015-08-20 16:48:49 -07001626 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001627 copyToClipboard.setVisible(false);
1628 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001629
1630 @Override
1631 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001632 }
1633
1634 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001635 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001636 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001637 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001638 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001639 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay459bc2b2015-09-16 15:07:31 -07001640
Steve McKay1f199482015-05-20 15:58:42 -07001641 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001642 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001643 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1644
1645 menu.findItem(R.id.menu_open).setVisible(false);
1646 menu.findItem(R.id.menu_copy_to).setVisible(false);
1647 menu.findItem(R.id.menu_move_to).setVisible(false);
1648 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001649
1650 @Override
Kyle Horimoto62a7fd02015-08-18 13:25:29 -07001651 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001652 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001653
1654 /**
1655 * The data model for the current loaded directory.
1656 */
Ben Kwa7461a952015-09-01 11:03:01 -07001657 @VisibleForTesting
1658 public static final class Model implements DocumentContext {
Ben Kwa24be5d32015-08-27 16:04:46 -07001659 private MultiSelectManager mSelectionManager;
Ben Kwa379e1762015-09-21 10:49:52 -07001660 private RecyclerView.Adapter<?> mViewAdapter;
Ben Kwa7461a952015-09-01 11:03:01 -07001661 private Context mContext;
Ben Kwa24be5d32015-08-27 16:04:46 -07001662 private int mCursorCount;
1663 private boolean mIsLoading;
Ben Kwa7461a952015-09-01 11:03:01 -07001664 private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
1665 private UpdateListener mUpdateListener;
Ben Kwa24be5d32015-08-27 16:04:46 -07001666 @Nullable private Cursor mCursor;
1667 @Nullable private String info;
1668 @Nullable private String error;
Ben Kwa7461a952015-09-01 11:03:01 -07001669
Ben Kwa379e1762015-09-21 10:49:52 -07001670 Model(Context context, MultiSelectManager selectionManager,
1671 RecyclerView.Adapter<?> viewAdapter) {
Ben Kwa7461a952015-09-01 11:03:01 -07001672 mContext = context;
1673 mSelectionManager = selectionManager;
Ben Kwa379e1762015-09-21 10:49:52 -07001674 mViewAdapter = viewAdapter;
Ben Kwa24be5d32015-08-27 16:04:46 -07001675 }
1676
1677 /**
1678 * Selects all files in the current directory.
1679 * @return true if the selection state changed for any files.
1680 */
1681 boolean selectAll() {
1682 return mSelectionManager.setItemsSelected(0, mCursorCount, true);
1683 }
1684
1685 /**
1686 * Clones the current selection into the given Selection object.
1687 * @param selection
1688 * @return The selection that was passed in, for convenience.
1689 */
1690 Selection getSelection(Selection selection) {
1691 return mSelectionManager.getSelection(selection);
1692 }
1693
1694 /**
1695 * @return The current selection (the live instance, not a copy).
1696 */
1697 Selection getSelection() {
1698 return mSelectionManager.getSelection();
1699 }
1700
1701 boolean isSelected(int position) {
1702 return mSelectionManager.getSelection().contains(position);
1703 }
1704
1705 void clearSelection() {
1706 mSelectionManager.clearSelection();
1707 }
1708
1709 void update(DirectoryResult result) {
1710 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
1711
1712 if (result == null) {
1713 mCursor = null;
1714 mCursorCount = 0;
1715 info = null;
1716 error = null;
1717 mIsLoading = false;
Ben Kwa83cedf22015-09-11 15:15:45 -07001718 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001719 return;
1720 }
1721
1722 if (result.exception != null) {
1723 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwa83cedf22015-09-11 15:15:45 -07001724 mUpdateListener.onModelUpdateFailed(result.exception);
Ben Kwa24be5d32015-08-27 16:04:46 -07001725 return;
1726 }
1727
1728 mCursor = result.cursor;
1729 mCursorCount = mCursor.getCount();
1730
1731 final Bundle extras = mCursor.getExtras();
1732 if (extras != null) {
1733 info = extras.getString(DocumentsContract.EXTRA_INFO);
1734 error = extras.getString(DocumentsContract.EXTRA_ERROR);
1735 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
1736 }
Ben Kwa7461a952015-09-01 11:03:01 -07001737
Ben Kwa83cedf22015-09-11 15:15:45 -07001738 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001739 }
1740
Ben Kwa7461a952015-09-01 11:03:01 -07001741 int getItemCount() {
Ben Kwa91923182015-08-27 16:06:33 -07001742 return mCursorCount - mMarkedForDeletion.size();
Ben Kwa24be5d32015-08-27 16:04:46 -07001743 }
1744
Ben Kwa7461a952015-09-01 11:03:01 -07001745 Cursor getItem(int position) {
Ben Kwa91923182015-08-27 16:06:33 -07001746 // Items marked for deletion are masked out of the UI. To do this, for every marked
1747 // item whose position is less than the requested item position, advance the requested
1748 // position by 1.
1749 final int originalPos = position;
1750 final int size = mMarkedForDeletion.size();
Ben Kwa0d0daff2015-09-09 13:49:07 -07001751 for (int i = 0; i < size; ++i) {
Ben Kwa91923182015-08-27 16:06:33 -07001752 // It'd be more concise, but less efficient, to iterate over positions while calling
1753 // mMarkedForDeletion.get. Instead, iterate over deleted entries.
1754 if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
1755 ++position;
1756 }
1757 }
1758
1759 if (DEBUG) {
1760 Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos
1761 + " Adjusted: " + position);
1762 }
1763
Ben Kwa24be5d32015-08-27 16:04:46 -07001764 if (position >= mCursorCount) {
1765 throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
1766 mCursorCount + " items");
1767 }
1768
1769 mCursor.moveToPosition(position);
1770 return mCursor;
1771 }
1772
1773 private boolean isEmpty() {
1774 return mCursorCount == 0;
1775 }
1776
1777 private boolean isLoading() {
1778 return mIsLoading;
1779 }
1780
1781 private List<DocumentInfo> getSelectedDocuments() {
1782 Selection sel = getSelection(new Selection());
1783 return getDocuments(sel);
1784 }
1785
Ben Kwa7461a952015-09-01 11:03:01 -07001786 List<DocumentInfo> getDocuments(Selection items) {
Ben Kwa91923182015-08-27 16:06:33 -07001787 final int size = (items != null) ? items.size() : 0;
Ben Kwa24be5d32015-08-27 16:04:46 -07001788
Ben Kwa91923182015-08-27 16:06:33 -07001789 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwa24be5d32015-08-27 16:04:46 -07001790 for (int i = 0; i < size; i++) {
1791 final Cursor cursor = getItem(items.get(i));
1792 checkNotNull(cursor, "Cursor cannot be null.");
1793 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1794 docs.add(doc);
1795 }
1796 return docs;
1797 }
1798
1799 @Override
1800 public Cursor getCursor() {
1801 if (Looper.myLooper() != Looper.getMainLooper()) {
1802 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1803 }
1804 return mCursor;
1805 }
Ben Kwa91923182015-08-27 16:06:33 -07001806
Ben Kwa7461a952015-09-01 11:03:01 -07001807 List<DocumentInfo> getDocumentsMarkedForDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001808 final int size = mMarkedForDeletion.size();
1809 List<DocumentInfo> docs = new ArrayList<>(size);
1810
1811 for (int i = 0; i < size; ++i) {
1812 final int position = mMarkedForDeletion.keyAt(i);
1813 checkState(position < mCursorCount);
1814 mCursor.moveToPosition(position);
1815 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
1816 docs.add(doc);
1817 }
1818 return docs;
1819 }
1820
1821 /**
1822 * Marks the given files for deletion. This will remove them from the UI. Clients must then
1823 * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
1824 * the deletion, respectively. Only one deletion operation is allowed at a time.
1825 *
1826 * @param selected A selection representing the files to delete.
1827 */
Ben Kwa7461a952015-09-01 11:03:01 -07001828 void markForDeletion(Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -07001829 // Only one deletion operation at a time.
1830 checkState(mMarkedForDeletion.size() == 0);
1831 // There should never be more to delete than what exists.
1832 checkState(mCursorCount >= selected.size());
1833
1834 final int size = selected.size();
1835 for (int i = 0; i < size; ++i) {
1836 int position = selected.get(i);
1837 if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
1838 mMarkedForDeletion.append(position, true);
Ben Kwa379e1762015-09-21 10:49:52 -07001839 mViewAdapter.notifyItemRemoved(position);
Ben Kwa91923182015-08-27 16:06:33 -07001840 }
1841 }
1842
1843 /**
1844 * Cancels an ongoing deletion operation. All files currently marked for deletion will be
1845 * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}.
1846 */
Ben Kwa7461a952015-09-01 11:03:01 -07001847 void undoDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001848 // Iterate over deleted items, temporarily marking them false in the deletion list, and
1849 // re-adding them to the UI.
1850 final int size = mMarkedForDeletion.size();
1851 for (int i = 0; i < size; ++i) {
1852 final int position = mMarkedForDeletion.keyAt(i);
1853 mMarkedForDeletion.put(position, false);
Ben Kwa379e1762015-09-21 10:49:52 -07001854 mViewAdapter.notifyItemInserted(position);
Ben Kwa91923182015-08-27 16:06:33 -07001855 }
1856
1857 // Then, clear the deletion list.
1858 mMarkedForDeletion.clear();
1859 }
1860
1861 /**
1862 * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
1863 * deleted. See {@link #markForDeletion(Selection)}.
Ben Kwa7461a952015-09-01 11:03:01 -07001864 *
1865 * @param view The view which will be used to interact with the user (e.g. surfacing
1866 * snackbars) for errors, info, etc.
Ben Kwa91923182015-08-27 16:06:33 -07001867 */
Ben Kwac21888e2015-09-30 14:14:16 -07001868 void finalizeDeletion(DeletionListener listener) {
Ben Kwa7461a952015-09-01 11:03:01 -07001869 final ContentResolver resolver = mContext.getContentResolver();
Ben Kwac21888e2015-09-30 14:14:16 -07001870 DeleteFilesTask task = new DeleteFilesTask(resolver, listener);
Ben Kwa7461a952015-09-01 11:03:01 -07001871 task.execute();
Ben Kwa91923182015-08-27 16:06:33 -07001872 }
1873
1874 /**
1875 * A Task which collects the DocumentInfo for documents that have been marked for deletion,
1876 * and actually deletes them.
1877 */
1878 private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
1879 private ContentResolver mResolver;
Ben Kwac21888e2015-09-30 14:14:16 -07001880 private DeletionListener mListener;
Ben Kwa91923182015-08-27 16:06:33 -07001881
Ben Kwa7461a952015-09-01 11:03:01 -07001882 /**
1883 * @param resolver A ContentResolver for performing the actual file deletions.
1884 * @param errorCallback A Runnable that is executed in the event that one or more errors
1885 * occured while copying files. Execution will occur on the UI thread.
1886 */
Ben Kwac21888e2015-09-30 14:14:16 -07001887 public DeleteFilesTask(ContentResolver resolver, DeletionListener listener) {
Ben Kwa91923182015-08-27 16:06:33 -07001888 mResolver = resolver;
Ben Kwac21888e2015-09-30 14:14:16 -07001889 mListener = listener;
Ben Kwa91923182015-08-27 16:06:33 -07001890 }
1891
1892 @Override
1893 protected List<DocumentInfo> doInBackground(Void... params) {
1894 return getDocumentsMarkedForDeletion();
1895 }
1896
1897 @Override
1898 protected void onPostExecute(List<DocumentInfo> docs) {
1899 boolean hadTrouble = false;
1900 for (DocumentInfo doc : docs) {
1901 if (!doc.isDeleteSupported()) {
1902 Log.w(TAG, doc + " could not be deleted. Skipping...");
1903 hadTrouble = true;
1904 continue;
1905 }
1906
1907 ContentProviderClient client = null;
1908 try {
1909 if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
1910 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1911 mResolver, doc.derivedUri.getAuthority());
1912 DocumentsContract.deleteDocument(client, doc.derivedUri);
1913 } catch (Exception e) {
1914 Log.w(TAG, "Failed to delete " + doc);
1915 hadTrouble = true;
1916 } finally {
1917 ContentProviderClient.releaseQuietly(client);
1918 }
1919 }
1920
1921 if (hadTrouble) {
Ben Kwa7461a952015-09-01 11:03:01 -07001922 // TODO show which files failed? b/23720103
Ben Kwac21888e2015-09-30 14:14:16 -07001923 mListener.onError();
Ben Kwa91923182015-08-27 16:06:33 -07001924 if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed.");
1925 } else {
1926 if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
1927 }
1928 mMarkedForDeletion.clear();
Ben Kwac21888e2015-09-30 14:14:16 -07001929
1930 mListener.onCompletion();
Ben Kwa91923182015-08-27 16:06:33 -07001931 }
1932 }
Ben Kwa7461a952015-09-01 11:03:01 -07001933
Ben Kwac21888e2015-09-30 14:14:16 -07001934 static class DeletionListener {
1935 /**
1936 * Called when deletion has completed (regardless of whether an error occurred).
1937 */
1938 void onCompletion() {}
1939
1940 /**
1941 * Called at the end of a deletion operation that produced one or more errors.
1942 */
1943 void onError() {}
1944 }
1945
Ben Kwa7461a952015-09-01 11:03:01 -07001946 void addUpdateListener(UpdateListener listener) {
1947 checkState(mUpdateListener == null);
1948 mUpdateListener = listener;
1949 }
1950
Ben Kwa379e1762015-09-21 10:49:52 -07001951 static class UpdateListener {
Ben Kwa7461a952015-09-01 11:03:01 -07001952 /**
1953 * Called when a successful update has occurred.
1954 */
Ben Kwa379e1762015-09-21 10:49:52 -07001955 void onModelUpdate(Model model) {}
Ben Kwa7461a952015-09-01 11:03:01 -07001956
1957 /**
1958 * Called when an update has been attempted but failed.
1959 */
Ben Kwa379e1762015-09-21 10:49:52 -07001960 void onModelUpdateFailed(Exception e) {}
1961 }
1962 }
Ben Kwa7461a952015-09-01 11:03:01 -07001963
Ben Kwa379e1762015-09-21 10:49:52 -07001964 private class ModelUpdateListener extends Model.UpdateListener {
1965 @Override
1966 public void onModelUpdate(Model model) {
1967 if (model.info != null || model.error != null) {
1968 mMessageBar.setInfo(model.info);
1969 mMessageBar.setError(model.error);
1970 mMessageBar.show();
1971 }
Ben Kwa7461a952015-09-01 11:03:01 -07001972
Ben Kwa379e1762015-09-21 10:49:52 -07001973 mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE);
1974
1975 if (model.isEmpty()) {
Ben Kwa2f975262015-09-16 13:15:38 -07001976 showEmptyView();
Ben Kwa379e1762015-09-21 10:49:52 -07001977 } else {
Ben Kwa2f975262015-09-16 13:15:38 -07001978 showRecyclerView();
1979 mAdapter.notifyDataSetChanged();
Ben Kwa379e1762015-09-21 10:49:52 -07001980 }
Ben Kwa379e1762015-09-21 10:49:52 -07001981 }
1982
1983 @Override
1984 public void onModelUpdateFailed(Exception e) {
Ben Kwa2f975262015-09-16 13:15:38 -07001985 showErrorView();
Ben Kwa7461a952015-09-01 11:03:01 -07001986 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001987 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001988}