blob: ea8ecf54f31cb2535c1a0e935bfc096eb1a0f97a [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070020import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
21import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
22import static com.android.documentsui.BaseActivity.State.MODE_GRID;
23import static com.android.documentsui.BaseActivity.State.MODE_LIST;
24import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
25import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Steve McKayfefcd702015-08-20 16:19:38 +000026import static com.android.documentsui.Shared.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070027import static com.android.documentsui.model.DocumentInfo.getCursorInt;
28import static com.android.documentsui.model.DocumentInfo.getCursorLong;
29import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKayef280152015-06-11 10:10:49 -070030import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKayd57f5fa2015-07-23 16:33:41 -070031import static com.android.internal.util.Preconditions.checkState;
Steve McKay0599a442015-05-05 14:50:00 -070032
Ben Kwaf5858932015-04-07 15:43:39 -070033import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070034import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070035import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.app.FragmentManager;
37import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070039import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070040import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070041import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070042import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070043import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070044import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070045import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070046import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070047import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070048import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070049import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070051import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070052import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070053import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070055import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070056import android.os.Handler;
57import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070058import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070059import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070060import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070061import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070062import android.provider.DocumentsContract.Document;
Ben Kwa24be5d32015-08-27 16:04:46 -070063import android.support.annotation.Nullable;
Ben Kwa7461a952015-09-01 11:03:01 -070064import android.support.annotation.VisibleForTesting;
Ben Kwa91923182015-08-27 16:06:33 -070065import android.support.design.widget.Snackbar;
Steve McKayef280152015-06-11 10:10:49 -070066import android.support.v7.widget.GridLayoutManager;
67import android.support.v7.widget.LinearLayoutManager;
68import android.support.v7.widget.RecyclerView;
69import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070070import android.support.v7.widget.RecyclerView.RecyclerListener;
71import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070072import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070073import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070074import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070075import android.text.format.Time;
76import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070077import android.util.SparseArray;
Ben Kwa91923182015-08-27 16:06:33 -070078import android.util.SparseBooleanArray;
Ben Kwa0574b182015-09-08 07:31:19 -070079import android.util.TypedValue;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070080import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070081import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070082import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070083import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070084import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070085import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070086import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070088import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070089import android.view.ViewGroup;
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -070090import android.view.ViewParent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070091import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070092import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070093import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070094
Steve McKay351a7492015-08-04 10:11:01 -070095import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070096import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070097import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070098import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070099import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700100import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900101import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700102import com.android.documentsui.model.RootInfo;
Steve McKayfad3d4a2015-09-22 15:09:21 -0700103
Steve McKayfefcd702015-08-20 16:19:38 +0000104import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700105
106import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700107import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700108import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700109
110/**
111 * Display the documents inside a single directory.
112 */
113public class DirectoryFragment extends Fragment {
114
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700115 public static final int TYPE_NORMAL = 1;
116 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700117 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700118
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700119 public static final int ANIM_NONE = 1;
120 public static final int ANIM_SIDE = 2;
121 public static final int ANIM_DOWN = 3;
122 public static final int ANIM_UP = 4;
123
Ben Kwaf5858932015-04-07 15:43:39 -0700124 public static final int REQUEST_COPY_DESTINATION = 1;
125
Steve McKayef280152015-06-11 10:10:49 -0700126 private static final int LOADER_ID = 42;
127 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700128 private static final boolean DEBUG_ENABLE_DND = false;
129
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700130 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700131 private static final String EXTRA_ROOT = "root";
132 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700133 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700134 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700135
Ben Kwa7461a952015-09-01 11:03:01 -0700136 private Model mModel;
Ben Kwa379e1762015-09-21 10:49:52 -0700137 private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
Ben Kwa24be5d32015-08-27 16:04:46 -0700138
Steve McKayef280152015-06-11 10:10:49 -0700139 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140
Steve McKayef280152015-06-11 10:10:49 -0700141 private View mEmptyView;
142 private RecyclerView mRecView;
143
144 private int mType = TYPE_NORMAL;
145 private String mStateKey;
146
147 private int mLastMode = MODE_UNKNOWN;
148 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
149 private boolean mLastShowSize;
150 private boolean mHideGridTitles;
151 private boolean mSvelteRecents;
152 private Point mThumbSize;
153 private DocumentsAdapter mAdapter;
154 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700155 private FragmentTuner mFragmentTuner;
156 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700157 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700158 private LinearLayoutManager mListLayout;
159 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700160 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700161
Ben Kwac42fa402015-09-16 08:04:37 -0700162 private MessageBar mMessageBar;
Ben Kwa379e1762015-09-21 10:49:52 -0700163 private View mProgressBar;
Ben Kwac42fa402015-09-16 08:04:37 -0700164
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700165 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
166 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700167 }
168
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700169 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
170 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700171 }
172
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700173 public static void showRecentsOpen(FragmentManager fm, int anim) {
174 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700175 }
176
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700177 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
178 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700179 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700180 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700181 args.putParcelable(EXTRA_ROOT, root);
182 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700183 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700184
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700185 final FragmentTransaction ft = fm.beginTransaction();
186 switch (anim) {
187 case ANIM_SIDE:
188 args.putBoolean(EXTRA_IGNORE_STATE, true);
189 break;
190 case ANIM_DOWN:
191 args.putBoolean(EXTRA_IGNORE_STATE, true);
192 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
193 break;
194 case ANIM_UP:
195 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
196 break;
197 }
198
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700199 final DirectoryFragment fragment = new DirectoryFragment();
200 fragment.setArguments(args);
201
Jeff Sharkey76112212013-08-06 11:26:10 -0700202 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700203 ft.commitAllowingStateLoss();
204 }
205
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700206 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
207 final StringBuilder builder = new StringBuilder();
208 builder.append(root != null ? root.authority : "null").append(';');
209 builder.append(root != null ? root.rootId : "null").append(';');
210 builder.append(doc != null ? doc.documentId : "null");
211 return builder.toString();
212 }
213
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700214 public static DirectoryFragment get(FragmentManager fm) {
215 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700216 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700217 }
218
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700219 @Override
220 public View onCreateView(
221 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
222 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700223 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700224 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
225
Ben Kwac42fa402015-09-16 08:04:37 -0700226 mMessageBar = MessageBar.create(getChildFragmentManager());
Ben Kwa379e1762015-09-21 10:49:52 -0700227 mProgressBar = view.findViewById(R.id.progressbar);
Ben Kwac42fa402015-09-16 08:04:37 -0700228
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700229 mEmptyView = view.findViewById(android.R.id.empty);
230
Steve McKayef280152015-06-11 10:10:49 -0700231 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
232 mRecView.setRecyclerListener(
233 new RecyclerListener() {
234 @Override
235 public void onViewRecycled(ViewHolder holder) {
236 cancelThumbnailTask(holder.itemView);
237 }
238 });
Steve McKay8e258c62015-05-06 14:27:57 -0700239
Steve McKayd57f5fa2015-07-23 16:33:41 -0700240 // TODO: Rather than update columns on layout changes, push this
241 // code (or something like it) into GridLayoutManager.
242 mRecView.addOnLayoutChangeListener(
243 new OnLayoutChangeListener() {
244
245 @Override
246 public void onLayoutChange(
247 View v, int left, int top, int right, int bottom, int oldLeft,
248 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000249 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700250 if (mGridLayout != null) {
251 mGridLayout.setSpanCount(mColumnCount);
252 }
253 }
254 });
255
256 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700257 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700258 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700259 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700260
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700261 return view;
262 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700263
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700264 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700265 public void onDestroyView() {
266 super.onDestroyView();
267
268 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700269 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700270 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700271 final View view = mRecView.getChildAt(i);
272 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700273 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700274
Steve McKayef280152015-06-11 10:10:49 -0700275 // Clear any outstanding selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700276 mModel.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700277 }
278
279 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700280 public void onActivityCreated(Bundle savedInstanceState) {
281 super.onActivityCreated(savedInstanceState);
282
283 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700284 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700285
Jeff Sharkey9656a532013-09-13 13:42:19 -0700286 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
287 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
288
Steve McKayef280152015-06-11 10:10:49 -0700289 mAdapter = new DocumentsAdapter(context);
290 mRecView.setAdapter(mAdapter);
291
292 GestureDetector.SimpleOnGestureListener listener =
293 new GestureDetector.SimpleOnGestureListener() {
294 @Override
295 public boolean onSingleTapUp(MotionEvent e) {
296 return DirectoryFragment.this.onSingleTapUp(e);
297 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700298 @Override
299 public boolean onDoubleTap(MotionEvent e) {
300 Log.d(TAG, "Handling double tap.");
301 return DirectoryFragment.this.onDoubleTap(e);
302 }
Steve McKayef280152015-06-11 10:10:49 -0700303 };
304
Ben Kwa24be5d32015-08-27 16:04:46 -0700305 // TODO: instead of inserting the view into the constructor, extract listener-creation code
306 // and set the listener on the view after the fact. Then the view doesn't need to be passed
307 // into the selection manager which is passed into the model.
308 MultiSelectManager selMgr= new MultiSelectManager(
Steve McKay57394872015-08-12 14:48:34 -0700309 mRecView,
310 listener,
311 state.allowMultiple
312 ? MultiSelectManager.MODE_MULTIPLE
313 : MultiSelectManager.MODE_SINGLE);
Ben Kwa24be5d32015-08-27 16:04:46 -0700314 selMgr.addCallback(new SelectionModeListener());
Ben Kwa7461a952015-09-01 11:03:01 -0700315
Ben Kwa379e1762015-09-21 10:49:52 -0700316 mModel = new Model(context, selMgr, mAdapter);
317 mModel.addUpdateListener(mModelUpdateListener);
Steve McKayef280152015-06-11 10:10:49 -0700318
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700319 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700320 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700321
Steve McKay1f199482015-05-20 15:58:42 -0700322 mFragmentTuner = pickFragmentTuner(state);
323 mClipper = new DocumentClipper(context);
324
Jeff Sharkey9656a532013-09-13 13:42:19 -0700325 if (mType == TYPE_RECENT_OPEN) {
326 // Hide titles when showing recents for picking images/videos
327 mHideGridTitles = MimePredicate.mimeMatches(
328 MimePredicate.VISUAL_MIMES, state.acceptMimes);
329 } else {
330 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
331 }
332
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700333 final ActivityManager am = (ActivityManager) context.getSystemService(
334 Context.ACTIVITY_SERVICE);
335 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
336
Jeff Sharkey46899c82013-08-18 22:26:48 -0700337 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700338 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700339 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700340 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700341
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700342 Uri contentsUri;
343 switch (mType) {
344 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700345 contentsUri = DocumentsContract.buildChildDocumentsUri(
346 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700347 if (state.action == ACTION_MANAGE) {
348 contentsUri = DocumentsContract.setManageMode(contentsUri);
349 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700350 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700351 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700352 case TYPE_SEARCH:
353 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700354 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700355 if (state.action == ACTION_MANAGE) {
356 contentsUri = DocumentsContract.setManageMode(contentsUri);
357 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700358 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700359 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700360 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700361 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700362 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700363 default:
364 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700365 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700366 }
367
368 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700369 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700370 if (result == null || result.exception != null) {
371 // onBackPressed does a fragment transaction, which can't be done inside
372 // onLoadFinished
373 mHandler.post(new Runnable() {
374 @Override
375 public void run() {
376 final Activity activity = getActivity();
377 if (activity != null) {
378 activity.onBackPressed();
379 }
380 }
381 });
382 return;
383 }
384
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700385 if (!isAdded()) return;
386
Ben Kwa24be5d32015-08-27 16:04:46 -0700387 mModel.update(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700388
389 // Push latest state up to UI
390 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700391 if (result.mode != MODE_UNKNOWN) {
392 state.derivedMode = result.mode;
393 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700394 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700395 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700396
397 updateDisplayState();
398
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700399 // When launched into empty recents, show drawer
Ben Kwa24be5d32015-08-27 16:04:46 -0700400 if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
Steve McKayb68dd222015-04-20 17:18:15 -0700401 context instanceof DocumentsActivity) {
402 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700403 }
404
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700405 // Restore any previous instance state
406 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
407 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
408 getView().restoreHierarchyState(container);
409 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700410 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700411 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700412
413 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700414 }
415
416 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700417 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700418 mModel.update(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700419 }
420 };
421
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700422 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700423 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700424
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700425 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700426 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700427 }
428
Jeff Sharkey42d26792013-09-06 13:22:09 -0700429 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700430 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700431 // There's only one request code right now. Replace this with a switch statement or
432 // something more scalable when more codes are added.
433 if (requestCode != REQUEST_COPY_DESTINATION) {
434 return;
435 }
436 if (resultCode == Activity.RESULT_CANCELED || data == null) {
437 // User pressed the back button or otherwise cancelled the destination pick. Don't
438 // proceed with the copy.
439 return;
440 }
441
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900442 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700443 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
444 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700445 }
446
Steve McKayef280152015-06-11 10:10:49 -0700447 private int getEventAdapterPosition(MotionEvent e) {
448 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
449 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
450 }
451
452 private boolean onSingleTapUp(MotionEvent e) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700453 if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700454 int position = getEventAdapterPosition(e);
455 if (position != RecyclerView.NO_POSITION) {
456 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700457 }
458 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700459 return false;
460 }
Steve McKayef280152015-06-11 10:10:49 -0700461
Steve McKay93d8ef42015-07-30 12:27:44 -0700462 protected boolean onDoubleTap(MotionEvent e) {
463 if (Events.isMouseEvent(e)) {
464 Log.d(TAG, "Handling double tap from mouse.");
465 int position = getEventAdapterPosition(e);
466 if (position != RecyclerView.NO_POSITION) {
467 return handleViewItem(position);
468 }
469 }
470 return false;
471 }
472
473 private boolean handleViewItem(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700474 final Cursor cursor = mModel.getItem(position);
Steve McKay93d8ef42015-07-30 12:27:44 -0700475 checkNotNull(cursor, "Cursor cannot be null.");
476 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
477 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
478 if (isDocumentEnabled(docMimeType, docFlags)) {
479 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwa24be5d32015-08-27 16:04:46 -0700480 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
481 mModel.clearSelection();
Steve McKay93d8ef42015-07-30 12:27:44 -0700482 return true;
483 }
Steve McKayef280152015-06-11 10:10:49 -0700484 return false;
485 }
486
Ben Kwaf5858932015-04-07 15:43:39 -0700487 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700488 public void onStop() {
489 super.onStop();
490
491 // Remember last scroll location
492 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
493 getView().saveHierarchyState(container);
494 final State state = getDisplayState(this);
495 state.dirState.put(mStateKey, container);
496 }
497
498 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700499 public void onResume() {
500 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700501 updateDisplayState();
502 }
503
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700504 public void onDisplayStateChanged() {
505 updateDisplayState();
506 }
507
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700508 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700509 // Sort order change always triggers reload; we'll trigger state change
510 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700511 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700512 }
513
514 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700515 final ContentResolver resolver = getActivity().getContentResolver();
516 final State state = getDisplayState(this);
517
518 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
519 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
520
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700521 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700522 final Uri stateUri = RecentsProvider.buildState(
523 root.authority, root.rootId, doc.documentId);
524 final ContentValues values = new ContentValues();
525 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700526
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700527 new AsyncTask<Void, Void, Void>() {
528 @Override
529 protected Void doInBackground(Void... params) {
530 resolver.insert(stateUri, values);
531 return null;
532 }
533 }.execute();
534 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700535
536 // Mode change is just visual change; no need to kick loader, and
537 // deliver change event immediately.
538 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700539 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700540
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700541 updateDisplayState();
542 }
543
544 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700545 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700546
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700547 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700548 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700549 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700550
Steve McKayef280152015-06-11 10:10:49 -0700551 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700552
Steve McKayef280152015-06-11 10:10:49 -0700553 mRecView.setAdapter(mAdapter);
554 }
555
556 /**
557 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
558 * classes as needed.
559 */
560 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700561 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700562
563 final LayoutManager layout;
564 switch (mode) {
565 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700566 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700567 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700568 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700569 }
Steve McKayef280152015-06-11 10:10:49 -0700570 layout = mGridLayout;
571 break;
572 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700573 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700574 if (mListLayout == null) {
575 mListLayout = new LinearLayoutManager(getContext());
576 }
Steve McKayef280152015-06-11 10:10:49 -0700577 layout = mListLayout;
578 break;
579 case MODE_UNKNOWN:
580 default:
581 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700582 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700583
Steve McKayef280152015-06-11 10:10:49 -0700584 mRecView.setLayoutManager(layout);
Kyle Horimoto2da6e4a2015-08-27 16:44:00 -0700585 // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
586 // imperatively calling this function.
Steve McKay9058e042015-09-01 12:31:24 -0700587 mModel.mSelectionManager.handleLayoutChanged();
Steve McKayef280152015-06-11 10:10:49 -0700588 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700589 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700590 }
591
Steve McKayfefcd702015-08-20 16:19:38 +0000592 private int calculateColumnCount() {
593 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
594 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700595 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000596
Steve McKayd57f5fa2015-07-23 16:33:41 -0700597 checkState(mRecView.getWidth() > 0);
598 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000599 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
600
Steve McKayd57f5fa2015-07-23 16:33:41 -0700601 return columnCount;
602 }
603
Steve McKayef280152015-06-11 10:10:49 -0700604 /**
605 * Manages the integration between our ActionMode and MultiSelectManager, initiating
606 * ActionMode when there is a selection, canceling it when there is no selection,
607 * and clearing selection when action mode is explicitly exited by the user.
608 */
609 private final class SelectionModeListener
610 implements MultiSelectManager.Callback, ActionMode.Callback {
611
612 private Selection mSelected = new Selection();
613 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700614 private int mNoDeleteCount = 0;
615 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700616
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700617 @Override
Steve McKayef280152015-06-11 10:10:49 -0700618 public boolean onBeforeItemStateChange(int position, boolean selected) {
Ben Kwac42fa402015-09-16 08:04:37 -0700619 // Directories cannot be checked
Steve McKayef280152015-06-11 10:10:49 -0700620 if (selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700621 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700622 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700623 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
624 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700625 return isDocumentEnabled(docMimeType, docFlags);
626 }
627 return true;
628 }
629
630 @Override
631 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700632
Ben Kwa24be5d32015-08-27 16:04:46 -0700633 final Cursor cursor = mModel.getItem(position);
Steve McKay4f4232d2015-07-22 12:13:46 -0700634 checkNotNull(cursor, "Cursor cannot be null.");
635
636 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
637 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
638 mNoDeleteCount += selected ? 1 : -1;
639 }
Steve McKay57394872015-08-12 14:48:34 -0700640 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700641
Steve McKay57394872015-08-12 14:48:34 -0700642 @Override
643 public void onSelectionChanged() {
Ben Kwa24be5d32015-08-27 16:04:46 -0700644 mModel.getSelection(mSelected);
Ben Kwafe18c1b2015-09-11 15:40:18 -0700645 TypedValue color = new TypedValue();
Steve McKay4f4232d2015-07-22 12:13:46 -0700646 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700647 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
648 if (mActionMode == null) {
649 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
650 mActionMode = getActivity().startActionMode(this);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700651 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700652 getActivity().getTheme().resolveAttribute(
653 R.attr.colorActionMode, color, true);
Steve McKay4f4232d2015-07-22 12:13:46 -0700654 updateActionMenu();
655 } else {
656 if (DEBUG) Log.d(TAG, "Finishing action mode.");
657 if (mActionMode != null) {
658 mActionMode.finish();
659 }
Ben Kwa0574b182015-09-08 07:31:19 -0700660 getActivity().getTheme().resolveAttribute(
661 android.R.attr.colorPrimaryDark, color, true);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700662 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700663 getActivity().getWindow().setStatusBarColor(color.data);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700664
Steve McKayef280152015-06-11 10:10:49 -0700665 if (mActionMode != null) {
666 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
667 }
668 }
669
670 // Called when the user exits the action mode
671 @Override
672 public void onDestroyActionMode(ActionMode mode) {
673 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
674 mActionMode = null;
675 // clear selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700676 mModel.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700677 mSelected.clear();
678 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700679 }
680
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700681 @Override
682 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
683 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa24be5d32015-08-27 16:04:46 -0700684 mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
685 return mModel.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700686 }
687
688 @Override
689 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700690 mMenu = menu;
691 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700692 return true;
693 }
694
Steve McKay4f4232d2015-07-22 12:13:46 -0700695 private void updateActionMenu() {
696 checkNotNull(mMenu);
697 // Delegate update logic to our owning action, since specialized logic is desired.
698 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
699 }
700
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700701 @Override
Steve McKayef280152015-06-11 10:10:49 -0700702 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700703
Ben Kwa24be5d32015-08-27 16:04:46 -0700704 Selection selection = mModel.getSelection(new Selection());
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700705
Jeff Sharkey873daa32013-08-18 17:38:20 -0700706 final int id = item.getItemId();
707 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700708 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700709 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700710 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700711
712 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700713 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700714 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700715 return true;
716
717 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700718 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700719 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700720 return true;
721
Steve McKay1f199482015-05-20 15:58:42 -0700722 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700723 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700724 mode.finish();
725 return true;
726
Steve McKay1f199482015-05-20 15:58:42 -0700727 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700728 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700729 mode.finish();
730 return true;
731
Steve McKay1f199482015-05-20 15:58:42 -0700732 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700733 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700734 mode.finish();
735 return true;
736
Ben Kwa512a6ba2015-03-31 08:15:21 -0700737 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700738 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700739 return true;
740
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700741 } else {
742 return false;
743 }
744 }
Steve McKayef280152015-06-11 10:10:49 -0700745 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700746
Steve McKayef280152015-06-11 10:10:49 -0700747 private static void cancelThumbnailTask(View view) {
748 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
749 if (iconThumb != null) {
750 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
751 if (oldTask != null) {
752 oldTask.preempt();
753 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700754 }
755 }
Steve McKayef280152015-06-11 10:10:49 -0700756 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700757
Steve McKayef280152015-06-11 10:10:49 -0700758 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700759 new GetDocumentsTask() {
760 @Override
761 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700762 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700763 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700764 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700765 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700766 }
767
Steve McKayef280152015-06-11 10:10:49 -0700768 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700769 new GetDocumentsTask() {
770 @Override
771 void onDocumentsReady(List<DocumentInfo> docs) {
772 Intent intent;
773
774 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000775 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700776 for (DocumentInfo doc: docs) {
777 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
778 docsForSend.add(doc);
779 }
780 }
781
782 if (docsForSend.size() == 1) {
783 final DocumentInfo doc = docsForSend.get(0);
784
785 intent = new Intent(Intent.ACTION_SEND);
786 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
787 intent.addCategory(Intent.CATEGORY_DEFAULT);
788 intent.setType(doc.mimeType);
789 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
790
791 } else if (docsForSend.size() > 1) {
792 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
793 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
794 intent.addCategory(Intent.CATEGORY_DEFAULT);
795
Steve McKayfefcd702015-08-20 16:19:38 +0000796 final ArrayList<String> mimeTypes = new ArrayList<>();
797 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700798 for (DocumentInfo doc : docsForSend) {
799 mimeTypes.add(doc.mimeType);
800 uris.add(doc.derivedUri);
801 }
802
803 intent.setType(findCommonMimeType(mimeTypes));
804 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
805
806 } else {
807 return;
808 }
809
810 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
811 startActivity(intent);
812 }
813 }.execute(selected);
814 }
815
Steve McKayef280152015-06-11 10:10:49 -0700816 private void deleteDocuments(final Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -0700817 Context context = getActivity();
818 ContentResolver resolver = context.getContentResolver();
819 String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700820
Ben Kwa91923182015-08-27 16:06:33 -0700821 mModel.markForDeletion(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700822
Ben Kwa91923182015-08-27 16:06:33 -0700823 Activity activity = getActivity();
824 Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG)
825 .setAction(
826 R.string.undo,
827 new android.view.View.OnClickListener() {
828 @Override
829 public void onClick(View view) {}
830 })
831 .setCallback(
832 new Snackbar.Callback() {
833 @Override
834 public void onDismissed(Snackbar snackbar, int event) {
835 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
836 mModel.undoDeletion();
837 } else {
Ben Kwa83cedf22015-09-11 15:15:45 -0700838 mModel.finalizeDeletion(
839 new Runnable() {
840 @Override
841 public void run() {
842 Snackbar.make(
843 DirectoryFragment.this.getView(),
844 R.string.toast_failed_delete,
845 Snackbar.LENGTH_LONG)
846 .show();
847
848 }
849 });
Ben Kwa91923182015-08-27 16:06:33 -0700850 }
Ben Kwa91923182015-08-27 16:06:33 -0700851 }
852 })
853 .show();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700854 }
855
Steve McKayef280152015-06-11 10:10:49 -0700856 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700857 // Pop up a dialog to pick a destination. This is inadequate but works for now.
858 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900859 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900860 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900861 Uri.EMPTY,
862 getActivity(),
863 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700864
865 new GetDocumentsTask() {
866 @Override
867 void onDocumentsReady(List<DocumentInfo> docs) {
868 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
869
870 boolean directoryCopy = false;
871 for (DocumentInfo info : docs) {
872 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
873 directoryCopy = true;
874 break;
875 }
876 }
877 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
878 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
879 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900880 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700881 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700882 }
883
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700884 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700885 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700886 }
887
Steve McKayef280152015-06-11 10:10:49 -0700888 // Provide a reference to the views for each data item
889 // Complex data items may need more than one view per item, and
890 // you provide access to all the views for a data item in a view holder
891 private static final class DocumentHolder extends RecyclerView.ViewHolder {
892 // each data item is just a string in this case
893 public View view;
894 public String docId; // The stable document id.
895 public DocumentHolder(View view) {
896 super(view);
897 this.view = view;
898 }
899 }
900
Ben Kwa379e1762015-09-21 10:49:52 -0700901 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
Steve McKayef280152015-06-11 10:10:49 -0700902
903 private final Context mContext;
904 private final LayoutInflater mInflater;
Steve McKayef280152015-06-11 10:10:49 -0700905
Steve McKayef280152015-06-11 10:10:49 -0700906 public DocumentsAdapter(Context context) {
907 mContext = context;
908 mInflater = LayoutInflater.from(context);
909 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700910
Ben Kwac42fa402015-09-16 08:04:37 -0700911 @Override
Steve McKayef280152015-06-11 10:10:49 -0700912 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
913 final State state = getDisplayState(DirectoryFragment.this);
914 final LayoutInflater inflater = LayoutInflater.from(getContext());
915 switch (state.derivedMode) {
916 case MODE_GRID:
917 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
918 case MODE_LIST:
919 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
920 case MODE_UNKNOWN:
921 default:
922 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -0700923 }
924 }
925
Steve McKayef280152015-06-11 10:10:49 -0700926 @Override
927 public void onBindViewHolder(DocumentHolder holder, int position) {
928
929 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700930 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700931 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700932 final RootsCache roots = DocumentsApplication.getRootsCache(context);
933 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
934 context, mThumbSize);
935
Ben Kwa24be5d32015-08-27 16:04:46 -0700936 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700937 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700938
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700939 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
940 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700941 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
942 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
943 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
944 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
945 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
946 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
947 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
948 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700949
Steve McKayef280152015-06-11 10:10:49 -0700950 holder.docId = docId;
951 final View itemView = holder.view;
Ben Kwa24be5d32015-08-27 16:04:46 -0700952 itemView.setActivated(mModel.isSelected(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -0700953
Steve McKayef280152015-06-11 10:10:49 -0700954 final View line1 = itemView.findViewById(R.id.line1);
955 final View line2 = itemView.findViewById(R.id.line2);
956
957 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
958 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
959 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
960 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
961 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
962 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
963 final TextView date = (TextView) itemView.findViewById(R.id.date);
964 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700965
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700966 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700967 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700968 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700969 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700970 }
971
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700972 iconMime.animate().cancel();
973 iconThumb.animate().cancel();
974
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700975 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700976 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700977 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700978 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700979
Jeff Sharkey7e544612014-08-29 15:38:27 -0700980 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
981 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
982
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700983 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700984 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700985 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700986 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700987 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700988 iconThumb.setImageBitmap(cachedResult);
989 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700990 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700991 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -0700992 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700993 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700994 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700995 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700996 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700997 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700998 }
999
1000 // Always throw MIME icon into place, even when a thumbnail is being
1001 // loaded in background.
1002 if (cacheHit) {
1003 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001004 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001005 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001006 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001007 iconMime.setAlpha(1f);
1008 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001009 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001010 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001011 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001012 }
1013
Jeff Sharkey9656a532013-09-13 13:42:19 -07001014 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001015 boolean hasLine2 = false;
1016
Jeff Sharkey9656a532013-09-13 13:42:19 -07001017 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1018 if (!hideTitle) {
1019 title.setText(docDisplayName);
1020 hasLine1 = true;
1021 }
1022
1023 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001024 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001025 // We've already had to enumerate roots before any results can
1026 // be shown, so this will never block.
1027 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001028 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001029 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001030 } else {
Steve McKayef280152015-06-11 10:10:49 -07001031 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001032 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001033
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001034 if (summary != null) {
1035 final boolean alwaysShowSummary = getResources()
1036 .getBoolean(R.bool.always_show_summary);
1037 if (alwaysShowSummary) {
1038 summary.setText(root.getDirectoryString());
1039 summary.setVisibility(View.VISIBLE);
1040 hasLine2 = true;
1041 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001042 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001043 // No summary needed if icon speaks for itself
1044 summary.setVisibility(View.INVISIBLE);
1045 } else {
1046 summary.setText(root.getDirectoryString());
1047 summary.setVisibility(View.VISIBLE);
1048 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1049 hasLine2 = true;
1050 }
1051 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001052 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001053 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001054 // Directories showing thumbnails in grid mode get a little icon
1055 // hint to remind user they're a directory.
1056 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1057 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001058 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001059 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001060 }
1061
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001062 if (summary != null) {
1063 if (docSummary != null) {
1064 summary.setText(docSummary);
1065 summary.setVisibility(View.VISIBLE);
1066 hasLine2 = true;
1067 } else {
1068 summary.setVisibility(View.INVISIBLE);
1069 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001070 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001071 }
1072
Jeff Sharkey9656a532013-09-13 13:42:19 -07001073 if (icon1 != null) icon1.setVisibility(View.GONE);
1074 if (icon2 != null) icon2.setVisibility(View.GONE);
1075
1076 if (iconDrawable != null) {
1077 if (hasLine1) {
1078 icon1.setVisibility(View.VISIBLE);
1079 icon1.setImageDrawable(iconDrawable);
1080 } else {
1081 icon2.setVisibility(View.VISIBLE);
1082 icon2.setImageDrawable(iconDrawable);
1083 }
1084 }
1085
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001086 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001087 date.setText(null);
1088 } else {
Steve McKayef280152015-06-11 10:10:49 -07001089 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001090 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001091 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001092
1093 if (state.showSize) {
1094 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001095 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001096 size.setText(null);
1097 } else {
Steve McKayef280152015-06-11 10:10:49 -07001098 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001099 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001100 }
1101 } else {
1102 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001103 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001104
Jeff Sharkey9656a532013-09-13 13:42:19 -07001105 if (line1 != null) {
1106 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1107 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001108 if (line2 != null) {
1109 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1110 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001111
Steve McKayef280152015-06-11 10:10:49 -07001112 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001113
1114 iconMime.setAlpha(iconAlpha);
1115 iconThumb.setAlpha(iconAlpha);
1116 if (icon1 != null) icon1.setAlpha(iconAlpha);
1117 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001118
Steve McKay8e258c62015-05-06 14:27:57 -07001119 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001120 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001121 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001122 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001123
Steve McKay351a7492015-08-04 10:11:01 -07001124 @Override
Steve McKayef280152015-06-11 10:10:49 -07001125 public int getItemCount() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001126 return mModel.getItemCount();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001127 }
1128
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001129 }
1130
1131 private static String formatTime(Context context, long when) {
1132 // TODO: DateUtils should make this easier
1133 Time then = new Time();
1134 then.set(when);
1135 Time now = new Time();
1136 now.setToNow();
1137
1138 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1139 | DateUtils.FORMAT_ABBREV_ALL;
1140
1141 if (then.year != now.year) {
1142 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1143 } else if (then.yearDay != now.yearDay) {
1144 flags |= DateUtils.FORMAT_SHOW_DATE;
1145 } else {
1146 flags |= DateUtils.FORMAT_SHOW_TIME;
1147 }
1148
1149 return DateUtils.formatDateTime(context, when, flags);
1150 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001151
1152 private String findCommonMimeType(List<String> mimeTypes) {
1153 String[] commonType = mimeTypes.get(0).split("/");
1154 if (commonType.length != 2) {
1155 return "*/*";
1156 }
1157
1158 for (int i = 1; i < mimeTypes.size(); i++) {
1159 String[] type = mimeTypes.get(i).split("/");
1160 if (type.length != 2) continue;
1161
1162 if (!commonType[1].equals(type[1])) {
1163 commonType[1] = "*";
1164 }
1165
1166 if (!commonType[0].equals(type[0])) {
1167 commonType[0] = "*";
1168 commonType[1] = "*";
1169 break;
1170 }
1171 }
1172
1173 return commonType[0] + "/" + commonType[1];
1174 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001175
1176 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001177 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001178 if (v.isEnabled() == enabled) return;
1179 v.setEnabled(enabled);
1180
1181 if (v instanceof ViewGroup) {
1182 final ViewGroup vg = (ViewGroup) v;
1183 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1184 setEnabledRecursive(vg.getChildAt(i), enabled);
1185 }
1186 }
1187 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001188
1189 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1190 final State state = getDisplayState(DirectoryFragment.this);
1191
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001192 // Directories are always enabled
1193 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1194 return true;
1195 }
1196
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001197 // Read-only files are disabled when creating
1198 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1199 return false;
1200 }
1201
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001202 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1203 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001204
Steve McKay1f199482015-05-20 15:58:42 -07001205 private void copyFromClipboard() {
1206 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1207
1208 @Override
1209 protected List<DocumentInfo> doInBackground(Void... params) {
1210 return mClipper.getClippedDocuments();
1211 }
1212
1213 @Override
1214 protected void onPostExecute(List<DocumentInfo> docs) {
1215 DocumentInfo destination =
1216 ((BaseActivity) getActivity()).getCurrentDirectory();
1217 copyDocuments(docs, destination);
1218 }
1219 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001220 }
1221
Steve McKay1f199482015-05-20 15:58:42 -07001222 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001223 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001224 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001225
Steve McKay1f199482015-05-20 15:58:42 -07001226 @Override
1227 protected List<DocumentInfo> doInBackground(Void... params) {
1228 return mClipper.getDocumentsFromClipData(clipData);
1229 }
1230
1231 @Override
1232 protected void onPostExecute(List<DocumentInfo> docs) {
1233 copyDocuments(docs, destination);
1234 }
1235 }.execute();
1236 }
1237
1238 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1239 if (!canCopy(docs, destination)) {
1240 Toast.makeText(
1241 getActivity(),
1242 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001243 return;
1244 }
1245
Steve McKay1f199482015-05-20 15:58:42 -07001246 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001247 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001248 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001249
Steve McKay1f199482015-05-20 15:58:42 -07001250 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001251 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001252 if (destination != null) {
1253 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001254 tmpStack.addAll(curStack);
1255 } else {
1256 tmpStack = curStack;
1257 }
1258
Steve McKay1f199482015-05-20 15:58:42 -07001259 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001260 }
1261
1262 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1263 Context context = getActivity();
1264 final ContentResolver resolver = context.getContentResolver();
1265 ClipData clipData = null;
1266 for (DocumentInfo doc : docs) {
1267 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1268 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001269 // TODO: figure out what this string should be.
1270 // Currently it is not displayed anywhere in the UI, but this might change.
1271 final String label = "";
1272 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001273 } else {
1274 // TODO: update list of mime types in ClipData.
1275 clipData.addItem(new ClipData.Item(uri));
1276 }
1277 }
1278 return clipData;
1279 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001280
Steve McKay1f199482015-05-20 15:58:42 -07001281 void copySelectedToClipboard() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001282 Selection sel = mModel.getSelection(new Selection());
Steve McKayef280152015-06-11 10:10:49 -07001283 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001284 }
Steve McKay0599a442015-05-05 14:50:00 -07001285
Steve McKayef280152015-06-11 10:10:49 -07001286 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001287 new GetDocumentsTask() {
1288 @Override
1289 void onDocumentsReady(List<DocumentInfo> docs) {
1290 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001291 Activity activity = getActivity();
1292 Toast.makeText(activity,
1293 activity.getResources().getQuantityString(
1294 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1295 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001296 }
Steve McKayef280152015-06-11 10:10:49 -07001297 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001298 }
1299
1300 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001301 copyFromClipboard();
1302 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001303 }
1304
Steve McKay0599a442015-05-05 14:50:00 -07001305 /**
1306 * Returns true if the list of files can be copied to destination. Note that this
1307 * is a policy check only. Currently the method does not attempt to verify
1308 * available space or any other environmental aspects possibly resulting in
1309 * failure to copy.
1310 *
1311 * @return true if the list of files can be copied to destination.
1312 */
1313 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
Ben Kwa91923182015-08-27 16:06:33 -07001314 BaseActivity activity = (BaseActivity) getActivity();
Steve McKay0599a442015-05-05 14:50:00 -07001315
1316 final RootInfo root = activity.getCurrentRoot();
1317
1318 // Can't copy folders to Downloads.
1319 if (root.isDownloads()) {
1320 for (DocumentInfo docs : files) {
1321 if (docs.isDirectory()) {
1322 return false;
1323 }
1324 }
1325 }
1326
1327 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1328 }
1329
1330 void selectAllFiles() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001331 boolean changed = mModel.selectAll();
Steve McKay9459a7c2015-07-24 13:14:20 -07001332 if (changed) {
1333 updateDisplayState();
1334 }
Steve McKay0599a442015-05-05 14:50:00 -07001335 }
1336
Steve McKayef280152015-06-11 10:10:49 -07001337 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001338 // Listen for drops on non-directory items and empty space.
1339 view.setOnDragListener(mOnDragListener);
1340 }
1341
1342 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1343 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1344 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1345 // Make a directory item a drop target. Drop on non-directories and empty space
1346 // is handled at the list/grid view level.
1347 view.setOnDragListener(mOnDragListener);
1348 }
1349
1350 // Temporary: attaching the listener to the title only.
1351 // Attaching to the entire item conflicts with the item long click handler responsible
1352 // for item selection.
1353 final View title = view.findViewById(android.R.id.title);
1354 title.setOnLongClickListener(mLongClickListener);
1355 }
1356
1357 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1358 @Override
1359 public boolean onDrag(View v, DragEvent event) {
1360 switch (event.getAction()) {
1361 case DragEvent.ACTION_DRAG_STARTED:
1362 // TODO: Check if the event contains droppable data.
1363 return true;
1364
1365 // TODO: Highlight potential drop target directory?
1366 // TODO: Expand drop target directory on hover?
1367 case DragEvent.ACTION_DRAG_ENTERED:
1368 case DragEvent.ACTION_DRAG_LOCATION:
1369 case DragEvent.ACTION_DRAG_EXITED:
1370 case DragEvent.ACTION_DRAG_ENDED:
1371 return true;
1372
1373 case DragEvent.ACTION_DROP:
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001374 int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001375 DocumentInfo dstDir = null;
1376 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001377 Cursor dstCursor = mModel.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001378 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001379 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1380 // TODO: Do not drop into the directory where the documents came from.
1381 }
1382 copyFromClipData(event.getClipData(), dstDir);
1383 return true;
1384 }
1385 return false;
1386 }
1387 };
1388
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001389 private View getContainingItemView(View view) {
1390 while (true) {
1391 if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
1392 return view;
1393 }
1394 ViewParent parent = view.getParent();
1395 if (parent == null || !(parent instanceof View)) {
1396 return null;
1397 }
1398 view = (View) parent;
1399 }
1400 }
1401
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001402 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1403 @Override
1404 public boolean onLongClick(View v) {
1405 final List<DocumentInfo> docs = getDraggableDocuments(v);
1406 if (docs.isEmpty()) {
1407 return false;
1408 }
1409 v.startDrag(
1410 getClipDataFromDocuments(docs),
1411 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1412 null,
Vladislav Kaznacheeve3ce8a92015-07-15 18:04:04 -07001413 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
1414 View.DRAG_FLAG_GLOBAL_URI_WRITE
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001415 );
1416 return true;
1417 }
1418 };
1419
1420 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001421 int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001422 if (position == android.widget.AdapterView.INVALID_POSITION) {
1423 return Collections.EMPTY_LIST;
1424 }
1425
Ben Kwa24be5d32015-08-27 16:04:46 -07001426 final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001427 if (!selectedDocs.isEmpty()) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001428 if (!mModel.isSelected(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001429 // There is a selection that does not include the current item, drag nothing.
1430 return Collections.EMPTY_LIST;
1431 }
1432 return selectedDocs;
1433 }
1434
Ben Kwa24be5d32015-08-27 16:04:46 -07001435 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001436 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001437 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001438
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001439 return Lists.newArrayList(doc);
1440 }
1441
1442 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1443 if (docs.size() == 1) {
1444 final DocumentInfo doc = docs.get(0);
1445 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1446 doc.mimeType, doc.icon, getDisplayState(this));
1447 }
1448 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1449 }
1450
1451 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1452 String docMimeType, int docIcon, State state) {
1453 if (docIcon != 0) {
1454 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1455 } else {
1456 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1457 state.derivedMode);
1458 }
1459 }
1460
Steve McKayef280152015-06-11 10:10:49 -07001461 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1462 implements Preemptable {
1463 private final Uri mUri;
1464 private final ImageView mIconMime;
1465 private final ImageView mIconThumb;
1466 private final Point mThumbSize;
1467 private final float mTargetAlpha;
1468 private final CancellationSignal mSignal;
1469
1470 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1471 float targetAlpha) {
1472 mUri = uri;
1473 mIconMime = iconMime;
1474 mIconThumb = iconThumb;
1475 mThumbSize = thumbSize;
1476 mTargetAlpha = targetAlpha;
1477 mSignal = new CancellationSignal();
1478 }
1479
1480 @Override
1481 public void preempt() {
1482 cancel(false);
1483 mSignal.cancel();
1484 }
1485
1486 @Override
1487 protected Bitmap doInBackground(Uri... params) {
1488 if (isCancelled()) return null;
1489
1490 final Context context = mIconThumb.getContext();
1491 final ContentResolver resolver = context.getContentResolver();
1492
1493 ContentProviderClient client = null;
1494 Bitmap result = null;
1495 try {
1496 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1497 resolver, mUri.getAuthority());
1498 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1499 if (result != null) {
1500 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1501 context, mThumbSize);
1502 thumbs.put(mUri, result);
1503 }
1504 } catch (Exception e) {
1505 if (!(e instanceof OperationCanceledException)) {
1506 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1507 }
1508 } finally {
1509 ContentProviderClient.releaseQuietly(client);
1510 }
1511 return result;
1512 }
1513
1514 @Override
1515 protected void onPostExecute(Bitmap result) {
1516 if (mIconThumb.getTag() == this && result != null) {
1517 mIconThumb.setTag(null);
1518 mIconThumb.setImageBitmap(result);
1519
1520 mIconMime.setAlpha(mTargetAlpha);
1521 mIconMime.animate().alpha(0f).start();
1522 mIconThumb.setAlpha(0f);
1523 mIconThumb.animate().alpha(mTargetAlpha).start();
1524 }
1525 }
1526 }
1527
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001528 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1529
1530 private final Drawable mShadow;
1531
1532 private final int mShadowDimension;
1533
1534 public DrawableShadowBuilder(Drawable shadow) {
1535 mShadow = shadow;
1536 mShadowDimension = getResources().getDimensionPixelSize(
1537 R.dimen.drag_shadow_size);
1538 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1539 }
1540
Ben Kwa24be5d32015-08-27 16:04:46 -07001541 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001542 public void onProvideShadowMetrics(
1543 Point shadowSize, Point shadowTouchPoint) {
1544 shadowSize.set(mShadowDimension, mShadowDimension);
1545 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1546 }
1547
Ben Kwa24be5d32015-08-27 16:04:46 -07001548 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001549 public void onDrawShadow(Canvas canvas) {
1550 mShadow.draw(canvas);
1551 }
1552 }
Steve McKay1f199482015-05-20 15:58:42 -07001553
1554 private FragmentTuner pickFragmentTuner(final State state) {
Steve McKay459bc2b2015-09-16 15:07:31 -07001555 return state.action == ACTION_BROWSE
Steve McKay0fbfc652015-08-20 16:48:49 -07001556 ? new FilesTuner()
Steve McKay459bc2b2015-09-16 15:07:31 -07001557 : new DefaultTuner(state.action);
Steve McKay1f199482015-05-20 15:58:42 -07001558 }
1559
1560 /**
1561 * Interface for specializing the Fragment for the "host" Activity.
1562 * Feel free to expand the role of this class to handle other specializations.
1563 */
1564 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001565 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001566 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001567 }
1568
1569 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001570 * Abstract task providing support for loading documents *off*
1571 * the main thread. And if it isn't obvious, creating a list
1572 * of documents (especially large lists) can be pretty expensive.
1573 */
1574 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001575 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001576 @Override
Steve McKayef280152015-06-11 10:10:49 -07001577 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001578 return mModel.getDocuments(selected[0]);
Steve McKay9276f3b2015-05-27 16:11:42 -07001579 }
1580
1581 @Override
1582 protected final void onPostExecute(List<DocumentInfo> docs) {
1583 onDocumentsReady(docs);
1584 }
1585
1586 abstract void onDocumentsReady(List<DocumentInfo> docs);
1587 }
1588
1589 /**
Steve McKay1f199482015-05-20 15:58:42 -07001590 * Provides support for Platform specific specializations of DirectoryFragment.
1591 */
1592 private static final class DefaultTuner implements FragmentTuner {
1593
Steve McKay459bc2b2015-09-16 15:07:31 -07001594 private final boolean mManaging;
Steve McKay1f199482015-05-20 15:58:42 -07001595
Steve McKay459bc2b2015-09-16 15:07:31 -07001596 public DefaultTuner(int action) {
1597 mManaging = (action == ACTION_MANAGE);
Steve McKay1f199482015-05-20 15:58:42 -07001598 }
1599
1600 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001601 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001602
1603 final MenuItem open = menu.findItem(R.id.menu_open);
1604 final MenuItem share = menu.findItem(R.id.menu_share);
1605 final MenuItem delete = menu.findItem(R.id.menu_delete);
1606 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1607 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1608 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1609
Steve McKay459bc2b2015-09-16 15:07:31 -07001610 open.setVisible(!mManaging);
1611 share.setVisible(mManaging);
1612 delete.setVisible(mManaging && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001613 // Disable copying from the Recents view.
Steve McKay459bc2b2015-09-16 15:07:31 -07001614 copyTo.setVisible(mManaging && dirType != TYPE_RECENT_OPEN);
Steve McKay1f199482015-05-20 15:58:42 -07001615 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1616
Steve McKay0fbfc652015-08-20 16:48:49 -07001617 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001618 copyToClipboard.setVisible(false);
1619 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001620
1621 @Override
1622 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001623 }
1624
1625 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001626 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001627 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001628 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001629 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001630 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay459bc2b2015-09-16 15:07:31 -07001631
Steve McKay1f199482015-05-20 15:58:42 -07001632 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001633 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001634 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1635
1636 menu.findItem(R.id.menu_open).setVisible(false);
1637 menu.findItem(R.id.menu_copy_to).setVisible(false);
1638 menu.findItem(R.id.menu_move_to).setVisible(false);
1639 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001640
1641 @Override
Kyle Horimoto62a7fd02015-08-18 13:25:29 -07001642 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001643 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001644
1645 /**
1646 * The data model for the current loaded directory.
1647 */
Ben Kwa7461a952015-09-01 11:03:01 -07001648 @VisibleForTesting
1649 public static final class Model implements DocumentContext {
Ben Kwa24be5d32015-08-27 16:04:46 -07001650 private MultiSelectManager mSelectionManager;
Ben Kwa379e1762015-09-21 10:49:52 -07001651 private RecyclerView.Adapter<?> mViewAdapter;
Ben Kwa7461a952015-09-01 11:03:01 -07001652 private Context mContext;
Ben Kwa24be5d32015-08-27 16:04:46 -07001653 private int mCursorCount;
1654 private boolean mIsLoading;
Ben Kwa7461a952015-09-01 11:03:01 -07001655 private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
1656 private UpdateListener mUpdateListener;
Ben Kwa24be5d32015-08-27 16:04:46 -07001657 @Nullable private Cursor mCursor;
1658 @Nullable private String info;
1659 @Nullable private String error;
Ben Kwa7461a952015-09-01 11:03:01 -07001660
Ben Kwa379e1762015-09-21 10:49:52 -07001661 Model(Context context, MultiSelectManager selectionManager,
1662 RecyclerView.Adapter<?> viewAdapter) {
Ben Kwa7461a952015-09-01 11:03:01 -07001663 mContext = context;
1664 mSelectionManager = selectionManager;
Ben Kwa379e1762015-09-21 10:49:52 -07001665 mViewAdapter = viewAdapter;
Ben Kwa24be5d32015-08-27 16:04:46 -07001666 }
1667
1668 /**
1669 * Selects all files in the current directory.
1670 * @return true if the selection state changed for any files.
1671 */
1672 boolean selectAll() {
1673 return mSelectionManager.setItemsSelected(0, mCursorCount, true);
1674 }
1675
1676 /**
1677 * Clones the current selection into the given Selection object.
1678 * @param selection
1679 * @return The selection that was passed in, for convenience.
1680 */
1681 Selection getSelection(Selection selection) {
1682 return mSelectionManager.getSelection(selection);
1683 }
1684
1685 /**
1686 * @return The current selection (the live instance, not a copy).
1687 */
1688 Selection getSelection() {
1689 return mSelectionManager.getSelection();
1690 }
1691
1692 boolean isSelected(int position) {
1693 return mSelectionManager.getSelection().contains(position);
1694 }
1695
1696 void clearSelection() {
1697 mSelectionManager.clearSelection();
1698 }
1699
1700 void update(DirectoryResult result) {
1701 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
1702
1703 if (result == null) {
1704 mCursor = null;
1705 mCursorCount = 0;
1706 info = null;
1707 error = null;
1708 mIsLoading = false;
Ben Kwa83cedf22015-09-11 15:15:45 -07001709 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001710 return;
1711 }
1712
1713 if (result.exception != null) {
1714 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwa83cedf22015-09-11 15:15:45 -07001715 mUpdateListener.onModelUpdateFailed(result.exception);
Ben Kwa24be5d32015-08-27 16:04:46 -07001716 return;
1717 }
1718
1719 mCursor = result.cursor;
1720 mCursorCount = mCursor.getCount();
1721
1722 final Bundle extras = mCursor.getExtras();
1723 if (extras != null) {
1724 info = extras.getString(DocumentsContract.EXTRA_INFO);
1725 error = extras.getString(DocumentsContract.EXTRA_ERROR);
1726 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
1727 }
Ben Kwa7461a952015-09-01 11:03:01 -07001728
Ben Kwa83cedf22015-09-11 15:15:45 -07001729 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001730 }
1731
Ben Kwa7461a952015-09-01 11:03:01 -07001732 int getItemCount() {
Ben Kwa91923182015-08-27 16:06:33 -07001733 return mCursorCount - mMarkedForDeletion.size();
Ben Kwa24be5d32015-08-27 16:04:46 -07001734 }
1735
Ben Kwa7461a952015-09-01 11:03:01 -07001736 Cursor getItem(int position) {
Ben Kwa91923182015-08-27 16:06:33 -07001737 // Items marked for deletion are masked out of the UI. To do this, for every marked
1738 // item whose position is less than the requested item position, advance the requested
1739 // position by 1.
1740 final int originalPos = position;
1741 final int size = mMarkedForDeletion.size();
Ben Kwa0d0daff2015-09-09 13:49:07 -07001742 for (int i = 0; i < size; ++i) {
Ben Kwa91923182015-08-27 16:06:33 -07001743 // It'd be more concise, but less efficient, to iterate over positions while calling
1744 // mMarkedForDeletion.get. Instead, iterate over deleted entries.
1745 if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
1746 ++position;
1747 }
1748 }
1749
1750 if (DEBUG) {
1751 Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos
1752 + " Adjusted: " + position);
1753 }
1754
Ben Kwa24be5d32015-08-27 16:04:46 -07001755 if (position >= mCursorCount) {
1756 throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
1757 mCursorCount + " items");
1758 }
1759
1760 mCursor.moveToPosition(position);
1761 return mCursor;
1762 }
1763
1764 private boolean isEmpty() {
1765 return mCursorCount == 0;
1766 }
1767
1768 private boolean isLoading() {
1769 return mIsLoading;
1770 }
1771
1772 private List<DocumentInfo> getSelectedDocuments() {
1773 Selection sel = getSelection(new Selection());
1774 return getDocuments(sel);
1775 }
1776
Ben Kwa7461a952015-09-01 11:03:01 -07001777 List<DocumentInfo> getDocuments(Selection items) {
Ben Kwa91923182015-08-27 16:06:33 -07001778 final int size = (items != null) ? items.size() : 0;
Ben Kwa24be5d32015-08-27 16:04:46 -07001779
Ben Kwa91923182015-08-27 16:06:33 -07001780 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwa24be5d32015-08-27 16:04:46 -07001781 for (int i = 0; i < size; i++) {
1782 final Cursor cursor = getItem(items.get(i));
1783 checkNotNull(cursor, "Cursor cannot be null.");
1784 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1785 docs.add(doc);
1786 }
1787 return docs;
1788 }
1789
1790 @Override
1791 public Cursor getCursor() {
1792 if (Looper.myLooper() != Looper.getMainLooper()) {
1793 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1794 }
1795 return mCursor;
1796 }
Ben Kwa91923182015-08-27 16:06:33 -07001797
Ben Kwa7461a952015-09-01 11:03:01 -07001798 List<DocumentInfo> getDocumentsMarkedForDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001799 final int size = mMarkedForDeletion.size();
1800 List<DocumentInfo> docs = new ArrayList<>(size);
1801
1802 for (int i = 0; i < size; ++i) {
1803 final int position = mMarkedForDeletion.keyAt(i);
1804 checkState(position < mCursorCount);
1805 mCursor.moveToPosition(position);
1806 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
1807 docs.add(doc);
1808 }
1809 return docs;
1810 }
1811
1812 /**
1813 * Marks the given files for deletion. This will remove them from the UI. Clients must then
1814 * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
1815 * the deletion, respectively. Only one deletion operation is allowed at a time.
1816 *
1817 * @param selected A selection representing the files to delete.
1818 */
Ben Kwa7461a952015-09-01 11:03:01 -07001819 void markForDeletion(Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -07001820 // Only one deletion operation at a time.
1821 checkState(mMarkedForDeletion.size() == 0);
1822 // There should never be more to delete than what exists.
1823 checkState(mCursorCount >= selected.size());
1824
1825 final int size = selected.size();
1826 for (int i = 0; i < size; ++i) {
1827 int position = selected.get(i);
1828 if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
1829 mMarkedForDeletion.append(position, true);
Ben Kwa379e1762015-09-21 10:49:52 -07001830 mViewAdapter.notifyItemRemoved(position);
Ben Kwa91923182015-08-27 16:06:33 -07001831 }
1832 }
1833
1834 /**
1835 * Cancels an ongoing deletion operation. All files currently marked for deletion will be
1836 * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}.
1837 */
Ben Kwa7461a952015-09-01 11:03:01 -07001838 void undoDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001839 // Iterate over deleted items, temporarily marking them false in the deletion list, and
1840 // re-adding them to the UI.
1841 final int size = mMarkedForDeletion.size();
1842 for (int i = 0; i < size; ++i) {
1843 final int position = mMarkedForDeletion.keyAt(i);
1844 mMarkedForDeletion.put(position, false);
Ben Kwa379e1762015-09-21 10:49:52 -07001845 mViewAdapter.notifyItemInserted(position);
Ben Kwa91923182015-08-27 16:06:33 -07001846 }
1847
1848 // Then, clear the deletion list.
1849 mMarkedForDeletion.clear();
1850 }
1851
1852 /**
1853 * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
1854 * deleted. See {@link #markForDeletion(Selection)}.
Ben Kwa7461a952015-09-01 11:03:01 -07001855 *
1856 * @param view The view which will be used to interact with the user (e.g. surfacing
1857 * snackbars) for errors, info, etc.
Ben Kwa91923182015-08-27 16:06:33 -07001858 */
Ben Kwa83cedf22015-09-11 15:15:45 -07001859 void finalizeDeletion(Runnable errorCallback) {
Ben Kwa7461a952015-09-01 11:03:01 -07001860 final ContentResolver resolver = mContext.getContentResolver();
Ben Kwa83cedf22015-09-11 15:15:45 -07001861 DeleteFilesTask task = new DeleteFilesTask(resolver, errorCallback);
Ben Kwa7461a952015-09-01 11:03:01 -07001862 task.execute();
Ben Kwa91923182015-08-27 16:06:33 -07001863 }
1864
1865 /**
1866 * A Task which collects the DocumentInfo for documents that have been marked for deletion,
1867 * and actually deletes them.
1868 */
1869 private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
1870 private ContentResolver mResolver;
Ben Kwa7461a952015-09-01 11:03:01 -07001871 private Runnable mErrorCallback;
Ben Kwa91923182015-08-27 16:06:33 -07001872
Ben Kwa7461a952015-09-01 11:03:01 -07001873 /**
1874 * @param resolver A ContentResolver for performing the actual file deletions.
1875 * @param errorCallback A Runnable that is executed in the event that one or more errors
1876 * occured while copying files. Execution will occur on the UI thread.
1877 */
1878 public DeleteFilesTask(ContentResolver resolver, Runnable errorCallback) {
Ben Kwa91923182015-08-27 16:06:33 -07001879 mResolver = resolver;
Ben Kwa7461a952015-09-01 11:03:01 -07001880 mErrorCallback = errorCallback;
Ben Kwa91923182015-08-27 16:06:33 -07001881 }
1882
1883 @Override
1884 protected List<DocumentInfo> doInBackground(Void... params) {
1885 return getDocumentsMarkedForDeletion();
1886 }
1887
1888 @Override
1889 protected void onPostExecute(List<DocumentInfo> docs) {
1890 boolean hadTrouble = false;
1891 for (DocumentInfo doc : docs) {
1892 if (!doc.isDeleteSupported()) {
1893 Log.w(TAG, doc + " could not be deleted. Skipping...");
1894 hadTrouble = true;
1895 continue;
1896 }
1897
1898 ContentProviderClient client = null;
1899 try {
1900 if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
1901 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1902 mResolver, doc.derivedUri.getAuthority());
1903 DocumentsContract.deleteDocument(client, doc.derivedUri);
1904 } catch (Exception e) {
1905 Log.w(TAG, "Failed to delete " + doc);
1906 hadTrouble = true;
1907 } finally {
1908 ContentProviderClient.releaseQuietly(client);
1909 }
1910 }
1911
1912 if (hadTrouble) {
Ben Kwa7461a952015-09-01 11:03:01 -07001913 // TODO show which files failed? b/23720103
1914 mErrorCallback.run();
Ben Kwa91923182015-08-27 16:06:33 -07001915 if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed.");
1916 } else {
1917 if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
1918 }
1919 mMarkedForDeletion.clear();
1920 }
1921 }
Ben Kwa7461a952015-09-01 11:03:01 -07001922
1923 void addUpdateListener(UpdateListener listener) {
1924 checkState(mUpdateListener == null);
1925 mUpdateListener = listener;
1926 }
1927
Ben Kwa379e1762015-09-21 10:49:52 -07001928 static class UpdateListener {
Ben Kwa7461a952015-09-01 11:03:01 -07001929 /**
1930 * Called when a successful update has occurred.
1931 */
Ben Kwa379e1762015-09-21 10:49:52 -07001932 void onModelUpdate(Model model) {}
Ben Kwa7461a952015-09-01 11:03:01 -07001933
1934 /**
1935 * Called when an update has been attempted but failed.
1936 */
Ben Kwa379e1762015-09-21 10:49:52 -07001937 void onModelUpdateFailed(Exception e) {}
1938 }
1939 }
Ben Kwa7461a952015-09-01 11:03:01 -07001940
Ben Kwa379e1762015-09-21 10:49:52 -07001941 private class ModelUpdateListener extends Model.UpdateListener {
1942 @Override
1943 public void onModelUpdate(Model model) {
1944 if (model.info != null || model.error != null) {
1945 mMessageBar.setInfo(model.info);
1946 mMessageBar.setError(model.error);
1947 mMessageBar.show();
1948 }
Ben Kwa7461a952015-09-01 11:03:01 -07001949
Ben Kwa379e1762015-09-21 10:49:52 -07001950 mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE);
1951
1952 if (model.isEmpty()) {
1953 mEmptyView.setVisibility(View.VISIBLE);
1954 mRecView.setVisibility(View.GONE);
1955 } else {
1956 mEmptyView.setVisibility(View.GONE);
1957 mRecView.setVisibility(View.VISIBLE);
1958 }
1959
1960 mAdapter.notifyDataSetChanged();
1961 }
1962
1963 @Override
1964 public void onModelUpdateFailed(Exception e) {
1965 // TODO: deal with catastrophic update failures
1966 String error = getString(R.string.query_error);
1967 mAdapter.notifyDataSetChanged();
Ben Kwa7461a952015-09-01 11:03:01 -07001968 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001969 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001970}