blob: edf829d178c9e52ed438d3e66847fc7f879e4c4c [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Steve McKayfefcd702015-08-20 16:19:38 +000027import static com.android.documentsui.Shared.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKayef280152015-06-11 10:10:49 -070031import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKayd57f5fa2015-07-23 16:33:41 -070032import static com.android.internal.util.Preconditions.checkState;
Steve McKay0599a442015-05-05 14:50:00 -070033
Ben Kwaf5858932015-04-07 15:43:39 -070034import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070035import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070036import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.FragmentManager;
38import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070040import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070041import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070043import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070045import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070047import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070050import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070054import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070056import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070057import android.os.Handler;
58import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070059import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070060import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070061import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070063import android.provider.DocumentsContract.Document;
Ben Kwa24be5d32015-08-27 16:04:46 -070064import android.support.annotation.Nullable;
Ben 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;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070079import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070080import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070081import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070082import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070083import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070084import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070085import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070086import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070087import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070088import android.view.ViewGroup;
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -070089import android.view.ViewParent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070090import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070091import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070092import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070093
Steve McKay351a7492015-08-04 10:11:01 -070094import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070095import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070096import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070097import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070098import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070099import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900100import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700101import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -0700102import com.android.internal.util.Preconditions;
Steve McKayfefcd702015-08-20 16:19:38 +0000103import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700104
105import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700106import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700107import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700108
109/**
110 * Display the documents inside a single directory.
111 */
112public class DirectoryFragment extends Fragment {
113
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700114 public static final int TYPE_NORMAL = 1;
115 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700116 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700117
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700118 public static final int ANIM_NONE = 1;
119 public static final int ANIM_SIDE = 2;
120 public static final int ANIM_DOWN = 3;
121 public static final int ANIM_UP = 4;
122
Ben Kwaf5858932015-04-07 15:43:39 -0700123 public static final int REQUEST_COPY_DESTINATION = 1;
124
Steve McKayef280152015-06-11 10:10:49 -0700125 private static final int LOADER_ID = 42;
126 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700127 private static final boolean DEBUG_ENABLE_DND = false;
128
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700129 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700130 private static final String EXTRA_ROOT = "root";
131 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700132 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700133 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700134
Ben Kwa24be5d32015-08-27 16:04:46 -0700135 private final Model mModel = new Model();
136
Steve McKayef280152015-06-11 10:10:49 -0700137 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700138
Steve McKayef280152015-06-11 10:10:49 -0700139 private View mEmptyView;
140 private RecyclerView mRecView;
141
142 private int mType = TYPE_NORMAL;
143 private String mStateKey;
144
145 private int mLastMode = MODE_UNKNOWN;
146 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
147 private boolean mLastShowSize;
148 private boolean mHideGridTitles;
149 private boolean mSvelteRecents;
150 private Point mThumbSize;
151 private DocumentsAdapter mAdapter;
152 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700153 private FragmentTuner mFragmentTuner;
154 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700155 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700156 private LinearLayoutManager mListLayout;
157 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700158 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700159
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700160 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
161 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700162 }
163
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700164 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
165 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700166 }
167
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700168 public static void showRecentsOpen(FragmentManager fm, int anim) {
169 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700170 }
171
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700172 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
173 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700174 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700175 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700176 args.putParcelable(EXTRA_ROOT, root);
177 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700178 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700179
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700180 final FragmentTransaction ft = fm.beginTransaction();
181 switch (anim) {
182 case ANIM_SIDE:
183 args.putBoolean(EXTRA_IGNORE_STATE, true);
184 break;
185 case ANIM_DOWN:
186 args.putBoolean(EXTRA_IGNORE_STATE, true);
187 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
188 break;
189 case ANIM_UP:
190 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
191 break;
192 }
193
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700194 final DirectoryFragment fragment = new DirectoryFragment();
195 fragment.setArguments(args);
196
Jeff Sharkey76112212013-08-06 11:26:10 -0700197 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700198 ft.commitAllowingStateLoss();
199 }
200
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700201 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
202 final StringBuilder builder = new StringBuilder();
203 builder.append(root != null ? root.authority : "null").append(';');
204 builder.append(root != null ? root.rootId : "null").append(';');
205 builder.append(doc != null ? doc.documentId : "null");
206 return builder.toString();
207 }
208
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700209 public static DirectoryFragment get(FragmentManager fm) {
210 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700211 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700212 }
213
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700214 @Override
215 public View onCreateView(
216 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
217 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700218 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700219 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
220
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700221 mEmptyView = view.findViewById(android.R.id.empty);
222
Steve McKayef280152015-06-11 10:10:49 -0700223 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
224 mRecView.setRecyclerListener(
225 new RecyclerListener() {
226 @Override
227 public void onViewRecycled(ViewHolder holder) {
228 cancelThumbnailTask(holder.itemView);
229 }
230 });
Steve McKay8e258c62015-05-06 14:27:57 -0700231
Steve McKayd57f5fa2015-07-23 16:33:41 -0700232 // TODO: Rather than update columns on layout changes, push this
233 // code (or something like it) into GridLayoutManager.
234 mRecView.addOnLayoutChangeListener(
235 new OnLayoutChangeListener() {
236
237 @Override
238 public void onLayoutChange(
239 View v, int left, int top, int right, int bottom, int oldLeft,
240 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000241 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700242 if (mGridLayout != null) {
243 mGridLayout.setSpanCount(mColumnCount);
244 }
245 }
246 });
247
248 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700249 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700250 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700251 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700252
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700253 return view;
254 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700255
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700256 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700257 public void onDestroyView() {
258 super.onDestroyView();
259
260 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700261 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700262 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700263 final View view = mRecView.getChildAt(i);
264 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700265 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700266
Steve McKayef280152015-06-11 10:10:49 -0700267 // Clear any outstanding selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700268 mModel.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700269 }
270
271 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700272 public void onActivityCreated(Bundle savedInstanceState) {
273 super.onActivityCreated(savedInstanceState);
274
275 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700276 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700277
Jeff Sharkey9656a532013-09-13 13:42:19 -0700278 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
279 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
280
Steve McKayef280152015-06-11 10:10:49 -0700281 mAdapter = new DocumentsAdapter(context);
282 mRecView.setAdapter(mAdapter);
283
284 GestureDetector.SimpleOnGestureListener listener =
285 new GestureDetector.SimpleOnGestureListener() {
286 @Override
287 public boolean onSingleTapUp(MotionEvent e) {
288 return DirectoryFragment.this.onSingleTapUp(e);
289 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700290 @Override
291 public boolean onDoubleTap(MotionEvent e) {
292 Log.d(TAG, "Handling double tap.");
293 return DirectoryFragment.this.onDoubleTap(e);
294 }
Steve McKayef280152015-06-11 10:10:49 -0700295 };
296
Ben Kwa24be5d32015-08-27 16:04:46 -0700297 // TODO: instead of inserting the view into the constructor, extract listener-creation code
298 // and set the listener on the view after the fact. Then the view doesn't need to be passed
299 // into the selection manager which is passed into the model.
300 MultiSelectManager selMgr= new MultiSelectManager(
Steve McKay57394872015-08-12 14:48:34 -0700301 mRecView,
302 listener,
303 state.allowMultiple
304 ? MultiSelectManager.MODE_MULTIPLE
305 : MultiSelectManager.MODE_SINGLE);
Ben Kwa24be5d32015-08-27 16:04:46 -0700306 selMgr.addCallback(new SelectionModeListener());
307 mModel.setSelectionManager(selMgr);
Steve McKayef280152015-06-11 10:10:49 -0700308
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700309 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700310 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700311
Steve McKay1f199482015-05-20 15:58:42 -0700312 mFragmentTuner = pickFragmentTuner(state);
313 mClipper = new DocumentClipper(context);
314
Jeff Sharkey9656a532013-09-13 13:42:19 -0700315 if (mType == TYPE_RECENT_OPEN) {
316 // Hide titles when showing recents for picking images/videos
317 mHideGridTitles = MimePredicate.mimeMatches(
318 MimePredicate.VISUAL_MIMES, state.acceptMimes);
319 } else {
320 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
321 }
322
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700323 final ActivityManager am = (ActivityManager) context.getSystemService(
324 Context.ACTIVITY_SERVICE);
325 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
326
Jeff Sharkey46899c82013-08-18 22:26:48 -0700327 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700328 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700329 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700330 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700331
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700332 Uri contentsUri;
333 switch (mType) {
334 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700335 contentsUri = DocumentsContract.buildChildDocumentsUri(
336 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700337 if (state.action == ACTION_MANAGE) {
338 contentsUri = DocumentsContract.setManageMode(contentsUri);
339 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700340 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700341 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700342 case TYPE_SEARCH:
343 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700344 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700345 if (state.action == ACTION_MANAGE) {
346 contentsUri = DocumentsContract.setManageMode(contentsUri);
347 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700348 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700349 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700350 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700351 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700352 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700353 default:
354 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700355 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700356 }
357
358 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700359 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700360 if (result == null || result.exception != null) {
361 // onBackPressed does a fragment transaction, which can't be done inside
362 // onLoadFinished
363 mHandler.post(new Runnable() {
364 @Override
365 public void run() {
366 final Activity activity = getActivity();
367 if (activity != null) {
368 activity.onBackPressed();
369 }
370 }
371 });
372 return;
373 }
374
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700375 if (!isAdded()) return;
376
Ben Kwa24be5d32015-08-27 16:04:46 -0700377 // TODO: make the adapter listen to the model
378 mModel.update(result);
379 mAdapter.update();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700380
381 // Push latest state up to UI
382 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700383 if (result.mode != MODE_UNKNOWN) {
384 state.derivedMode = result.mode;
385 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700386 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700387 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700388
389 updateDisplayState();
390
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700391 // When launched into empty recents, show drawer
Ben Kwa24be5d32015-08-27 16:04:46 -0700392 if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
Steve McKayb68dd222015-04-20 17:18:15 -0700393 context instanceof DocumentsActivity) {
394 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700395 }
396
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700397 // Restore any previous instance state
398 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
399 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
400 getView().restoreHierarchyState(container);
401 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700402 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700403 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700404
405 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700406 }
407
408 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700409 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700410 // TODO: make the adapter listen to the model.
411 mModel.update(null);
412 mAdapter.update();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700413 }
414 };
415
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700416 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700417 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700418
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700419 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700420 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700421 }
422
Jeff Sharkey42d26792013-09-06 13:22:09 -0700423 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700424 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700425 // There's only one request code right now. Replace this with a switch statement or
426 // something more scalable when more codes are added.
427 if (requestCode != REQUEST_COPY_DESTINATION) {
428 return;
429 }
430 if (resultCode == Activity.RESULT_CANCELED || data == null) {
431 // User pressed the back button or otherwise cancelled the destination pick. Don't
432 // proceed with the copy.
433 return;
434 }
435
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900436 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700437 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
438 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700439 }
440
Steve McKayef280152015-06-11 10:10:49 -0700441 private int getEventAdapterPosition(MotionEvent e) {
442 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
443 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
444 }
445
446 private boolean onSingleTapUp(MotionEvent e) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700447 if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700448 int position = getEventAdapterPosition(e);
449 if (position != RecyclerView.NO_POSITION) {
450 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700451 }
452 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700453 return false;
454 }
Steve McKayef280152015-06-11 10:10:49 -0700455
Steve McKay93d8ef42015-07-30 12:27:44 -0700456 protected boolean onDoubleTap(MotionEvent e) {
457 if (Events.isMouseEvent(e)) {
458 Log.d(TAG, "Handling double tap from mouse.");
459 int position = getEventAdapterPosition(e);
460 if (position != RecyclerView.NO_POSITION) {
461 return handleViewItem(position);
462 }
463 }
464 return false;
465 }
466
467 private boolean handleViewItem(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700468 final Cursor cursor = mModel.getItem(position);
Steve McKay93d8ef42015-07-30 12:27:44 -0700469 checkNotNull(cursor, "Cursor cannot be null.");
470 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
471 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
472 if (isDocumentEnabled(docMimeType, docFlags)) {
473 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwa24be5d32015-08-27 16:04:46 -0700474 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
475 mModel.clearSelection();
Steve McKay93d8ef42015-07-30 12:27:44 -0700476 return true;
477 }
Steve McKayef280152015-06-11 10:10:49 -0700478 return false;
479 }
480
Ben Kwaf5858932015-04-07 15:43:39 -0700481 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700482 public void onStop() {
483 super.onStop();
484
485 // Remember last scroll location
486 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
487 getView().saveHierarchyState(container);
488 final State state = getDisplayState(this);
489 state.dirState.put(mStateKey, container);
490 }
491
492 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700493 public void onResume() {
494 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700495 updateDisplayState();
496 }
497
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700498 public void onDisplayStateChanged() {
499 updateDisplayState();
500 }
501
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700502 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700503 // Sort order change always triggers reload; we'll trigger state change
504 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700505 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700506 }
507
508 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700509 final ContentResolver resolver = getActivity().getContentResolver();
510 final State state = getDisplayState(this);
511
512 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
513 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
514
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700515 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700516 final Uri stateUri = RecentsProvider.buildState(
517 root.authority, root.rootId, doc.documentId);
518 final ContentValues values = new ContentValues();
519 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700520
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700521 new AsyncTask<Void, Void, Void>() {
522 @Override
523 protected Void doInBackground(Void... params) {
524 resolver.insert(stateUri, values);
525 return null;
526 }
527 }.execute();
528 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700529
530 // Mode change is just visual change; no need to kick loader, and
531 // deliver change event immediately.
532 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700533 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700534
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700535 updateDisplayState();
536 }
537
538 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700539 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700540
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700541 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700542 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700543 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700544
Steve McKayef280152015-06-11 10:10:49 -0700545 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700546
Steve McKayef280152015-06-11 10:10:49 -0700547 mRecView.setAdapter(mAdapter);
548 }
549
550 /**
551 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
552 * classes as needed.
553 */
554 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700555 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700556
557 final LayoutManager layout;
558 switch (mode) {
559 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700560 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700561 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700562 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700563 }
Steve McKayef280152015-06-11 10:10:49 -0700564 layout = mGridLayout;
565 break;
566 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700567 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700568 if (mListLayout == null) {
569 mListLayout = new LinearLayoutManager(getContext());
570 }
Steve McKayef280152015-06-11 10:10:49 -0700571 layout = mListLayout;
572 break;
573 case MODE_UNKNOWN:
574 default:
575 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700576 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700577
Steve McKayef280152015-06-11 10:10:49 -0700578 mRecView.setLayoutManager(layout);
Kyle Horimoto2da6e4a2015-08-27 16:44:00 -0700579 // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
580 // imperatively calling this function.
Steve McKay9058e042015-09-01 12:31:24 -0700581 mModel.mSelectionManager.handleLayoutChanged();
Steve McKayef280152015-06-11 10:10:49 -0700582 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700583 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700584 }
585
Steve McKayfefcd702015-08-20 16:19:38 +0000586 private int calculateColumnCount() {
587 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
588 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700589 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000590
Steve McKayd57f5fa2015-07-23 16:33:41 -0700591 checkState(mRecView.getWidth() > 0);
592 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000593 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
594
Steve McKayd57f5fa2015-07-23 16:33:41 -0700595 return columnCount;
596 }
597
Steve McKayef280152015-06-11 10:10:49 -0700598 /**
599 * Manages the integration between our ActionMode and MultiSelectManager, initiating
600 * ActionMode when there is a selection, canceling it when there is no selection,
601 * and clearing selection when action mode is explicitly exited by the user.
602 */
603 private final class SelectionModeListener
604 implements MultiSelectManager.Callback, ActionMode.Callback {
605
606 private Selection mSelected = new Selection();
607 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700608 private int mNoDeleteCount = 0;
609 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700610
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700611 @Override
Steve McKayef280152015-06-11 10:10:49 -0700612 public boolean onBeforeItemStateChange(int position, boolean selected) {
613 // Directories and footer items cannot be checked
614 if (selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700615 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700616 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700617 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
618 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700619 return isDocumentEnabled(docMimeType, docFlags);
620 }
621 return true;
622 }
623
624 @Override
625 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700626
Ben Kwa24be5d32015-08-27 16:04:46 -0700627 final Cursor cursor = mModel.getItem(position);
Steve McKay4f4232d2015-07-22 12:13:46 -0700628 checkNotNull(cursor, "Cursor cannot be null.");
629
630 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
631 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
632 mNoDeleteCount += selected ? 1 : -1;
633 }
Steve McKay57394872015-08-12 14:48:34 -0700634 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700635
Steve McKay57394872015-08-12 14:48:34 -0700636 @Override
637 public void onSelectionChanged() {
Ben Kwa24be5d32015-08-27 16:04:46 -0700638 mModel.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700639 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700640 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
641 if (mActionMode == null) {
642 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
643 mActionMode = getActivity().startActionMode(this);
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900644 getActivity().getWindow().setStatusBarColor(
645 getResources().getColor(R.color.action_mode_status_bar_background));
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700646 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700647 updateActionMenu();
648 } else {
649 if (DEBUG) Log.d(TAG, "Finishing action mode.");
650 if (mActionMode != null) {
651 mActionMode.finish();
652 }
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900653 getActivity().getWindow().setStatusBarColor(
654 getResources().getColor(R.color.status_bar_background));
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700655 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700656
Steve McKayef280152015-06-11 10:10:49 -0700657 if (mActionMode != null) {
658 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
659 }
660 }
661
662 // Called when the user exits the action mode
663 @Override
664 public void onDestroyActionMode(ActionMode mode) {
665 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
666 mActionMode = null;
667 // clear selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700668 mModel.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700669 mSelected.clear();
670 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700671 }
672
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700673 @Override
674 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
675 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa24be5d32015-08-27 16:04:46 -0700676 mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
677 return mModel.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700678 }
679
680 @Override
681 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700682 mMenu = menu;
683 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700684 return true;
685 }
686
Steve McKay4f4232d2015-07-22 12:13:46 -0700687 private void updateActionMenu() {
688 checkNotNull(mMenu);
689 // Delegate update logic to our owning action, since specialized logic is desired.
690 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
691 }
692
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700693 @Override
Steve McKayef280152015-06-11 10:10:49 -0700694 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700695
Ben Kwa24be5d32015-08-27 16:04:46 -0700696 Selection selection = mModel.getSelection(new Selection());
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700697
Jeff Sharkey873daa32013-08-18 17:38:20 -0700698 final int id = item.getItemId();
699 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700700 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700701 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700702 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700703
704 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700705 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700706 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700707 return true;
708
709 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700710 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700711 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700712 return true;
713
Steve McKay1f199482015-05-20 15:58:42 -0700714 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700715 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700716 mode.finish();
717 return true;
718
Steve McKay1f199482015-05-20 15:58:42 -0700719 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700720 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700721 mode.finish();
722 return true;
723
Steve McKay1f199482015-05-20 15:58:42 -0700724 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700725 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700726 mode.finish();
727 return true;
728
Ben Kwa512a6ba2015-03-31 08:15:21 -0700729 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700730 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700731 return true;
732
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700733 } else {
734 return false;
735 }
736 }
Steve McKayef280152015-06-11 10:10:49 -0700737 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700738
Steve McKayef280152015-06-11 10:10:49 -0700739 private static void cancelThumbnailTask(View view) {
740 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
741 if (iconThumb != null) {
742 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
743 if (oldTask != null) {
744 oldTask.preempt();
745 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700746 }
747 }
Steve McKayef280152015-06-11 10:10:49 -0700748 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700749
Steve McKayef280152015-06-11 10:10:49 -0700750 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700751 new GetDocumentsTask() {
752 @Override
753 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700754 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700755 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700756 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700757 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700758 }
759
Steve McKayef280152015-06-11 10:10:49 -0700760 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700761 new GetDocumentsTask() {
762 @Override
763 void onDocumentsReady(List<DocumentInfo> docs) {
764 Intent intent;
765
766 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000767 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700768 for (DocumentInfo doc: docs) {
769 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
770 docsForSend.add(doc);
771 }
772 }
773
774 if (docsForSend.size() == 1) {
775 final DocumentInfo doc = docsForSend.get(0);
776
777 intent = new Intent(Intent.ACTION_SEND);
778 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
779 intent.addCategory(Intent.CATEGORY_DEFAULT);
780 intent.setType(doc.mimeType);
781 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
782
783 } else if (docsForSend.size() > 1) {
784 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
785 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
786 intent.addCategory(Intent.CATEGORY_DEFAULT);
787
Steve McKayfefcd702015-08-20 16:19:38 +0000788 final ArrayList<String> mimeTypes = new ArrayList<>();
789 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700790 for (DocumentInfo doc : docsForSend) {
791 mimeTypes.add(doc.mimeType);
792 uris.add(doc.derivedUri);
793 }
794
795 intent.setType(findCommonMimeType(mimeTypes));
796 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
797
798 } else {
799 return;
800 }
801
802 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
803 startActivity(intent);
804 }
805 }.execute(selected);
806 }
807
Steve McKayef280152015-06-11 10:10:49 -0700808 private void deleteDocuments(final Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -0700809 Context context = getActivity();
810 ContentResolver resolver = context.getContentResolver();
811 String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700812
Ben Kwa91923182015-08-27 16:06:33 -0700813 mModel.markForDeletion(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700814
Ben Kwa91923182015-08-27 16:06:33 -0700815 Activity activity = getActivity();
816 Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG)
817 .setAction(
818 R.string.undo,
819 new android.view.View.OnClickListener() {
820 @Override
821 public void onClick(View view) {}
822 })
823 .setCallback(
824 new Snackbar.Callback() {
825 @Override
826 public void onDismissed(Snackbar snackbar, int event) {
827 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
828 mModel.undoDeletion();
829 } else {
830 mModel.finalizeDeletion();
831 }
832 ;
833 }
834 })
835 .show();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700836 }
837
Steve McKayef280152015-06-11 10:10:49 -0700838 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700839 // Pop up a dialog to pick a destination. This is inadequate but works for now.
840 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900841 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900842 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900843 Uri.EMPTY,
844 getActivity(),
845 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700846
847 new GetDocumentsTask() {
848 @Override
849 void onDocumentsReady(List<DocumentInfo> docs) {
850 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
851
852 boolean directoryCopy = false;
853 for (DocumentInfo info : docs) {
854 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
855 directoryCopy = true;
856 break;
857 }
858 }
859 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
860 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
861 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900862 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700863 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700864 }
865
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700866 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700867 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700868 }
869
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700870 private static abstract class Footer {
871 private final int mItemViewType;
872
873 public Footer(int itemViewType) {
874 mItemViewType = itemViewType;
875 }
876
877 public abstract View getView(View convertView, ViewGroup parent);
878
879 public int getItemViewType() {
880 return mItemViewType;
881 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700882 }
883
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700884 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700885 public LoadingFooter() {
886 super(1);
887 }
888
Jeff Sharkey20b32272013-09-03 15:25:52 -0700889 @Override
890 public View getView(View convertView, ViewGroup parent) {
891 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700892 final State state = getDisplayState(DirectoryFragment.this);
893
Jeff Sharkey20b32272013-09-03 15:25:52 -0700894 if (convertView == null) {
895 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700896 if (state.derivedMode == MODE_LIST) {
897 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
898 } else if (state.derivedMode == MODE_GRID) {
899 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
900 } else {
901 throw new IllegalStateException();
902 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700903 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700904
Jeff Sharkey20b32272013-09-03 15:25:52 -0700905 return convertView;
906 }
907 }
908
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700909 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700910 private final int mIcon;
911 private final String mMessage;
912
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700913 public MessageFooter(int itemViewType, int icon, String message) {
914 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700915 mIcon = icon;
916 mMessage = message;
917 }
918
919 @Override
920 public View getView(View convertView, ViewGroup parent) {
921 final Context context = parent.getContext();
922 final State state = getDisplayState(DirectoryFragment.this);
923
924 if (convertView == null) {
925 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700926 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700927 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700928 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700929 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
930 } else {
931 throw new IllegalStateException();
932 }
933 }
934
935 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
936 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
937 icon.setImageResource(mIcon);
938 title.setText(mMessage);
939 return convertView;
940 }
941 }
942
Steve McKayef280152015-06-11 10:10:49 -0700943 // Provide a reference to the views for each data item
944 // Complex data items may need more than one view per item, and
945 // you provide access to all the views for a data item in a view holder
946 private static final class DocumentHolder extends RecyclerView.ViewHolder {
947 // each data item is just a string in this case
948 public View view;
949 public String docId; // The stable document id.
950 public DocumentHolder(View view) {
951 super(view);
952 this.view = view;
953 }
954 }
955
Ben Kwa24be5d32015-08-27 16:04:46 -0700956 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
Steve McKayef280152015-06-11 10:10:49 -0700957
958 private final Context mContext;
959 private final LayoutInflater mInflater;
960 // TODO: Bring back support for footers.
Steve McKayfefcd702015-08-20 16:19:38 +0000961 private final List<Footer> mFooters = new ArrayList<>();
Steve McKayef280152015-06-11 10:10:49 -0700962
Steve McKayef280152015-06-11 10:10:49 -0700963 public DocumentsAdapter(Context context) {
964 mContext = context;
965 mInflater = LayoutInflater.from(context);
966 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700967
Ben Kwa24be5d32015-08-27 16:04:46 -0700968 public void update() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700969 mFooters.clear();
Ben Kwa24be5d32015-08-27 16:04:46 -0700970 if (mModel.info != null) {
971 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, mModel.info));
972 }
973 if (mModel.error != null) {
974 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, mModel.error));
975 }
976 if (mModel.isLoading()) {
977 mFooters.add(new LoadingFooter());
Jeff Sharkey20b32272013-09-03 15:25:52 -0700978 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700979
Ben Kwa24be5d32015-08-27 16:04:46 -0700980 if (mModel.isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700981 mEmptyView.setVisibility(View.VISIBLE);
982 } else {
983 mEmptyView.setVisibility(View.GONE);
984 }
985
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700986 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700987 }
988
989 @Override
Steve McKayef280152015-06-11 10:10:49 -0700990 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
991 final State state = getDisplayState(DirectoryFragment.this);
992 final LayoutInflater inflater = LayoutInflater.from(getContext());
993 switch (state.derivedMode) {
994 case MODE_GRID:
995 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
996 case MODE_LIST:
997 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
998 case MODE_UNKNOWN:
999 default:
1000 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -07001001 }
1002 }
1003
Steve McKayef280152015-06-11 10:10:49 -07001004 @Override
1005 public void onBindViewHolder(DocumentHolder holder, int position) {
1006
1007 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001008 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001009 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -07001010 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1011 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1012 context, mThumbSize);
1013
Ben Kwa24be5d32015-08-27 16:04:46 -07001014 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001015 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001016
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001017 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1018 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001019 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1020 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1021 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1022 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1023 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1024 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1025 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1026 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001027
Steve McKayef280152015-06-11 10:10:49 -07001028 holder.docId = docId;
1029 final View itemView = holder.view;
Ben Kwa24be5d32015-08-27 16:04:46 -07001030 itemView.setActivated(mModel.isSelected(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001031
Steve McKayef280152015-06-11 10:10:49 -07001032 final View line1 = itemView.findViewById(R.id.line1);
1033 final View line2 = itemView.findViewById(R.id.line2);
1034
1035 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1036 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1037 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1038 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1039 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1040 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1041 final TextView date = (TextView) itemView.findViewById(R.id.date);
1042 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001043
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001044 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001045 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001046 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001047 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001048 }
1049
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001050 iconMime.animate().cancel();
1051 iconThumb.animate().cancel();
1052
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001053 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001054 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001055 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001056 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001057
Jeff Sharkey7e544612014-08-29 15:38:27 -07001058 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1059 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1060
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001061 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001062 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001063 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001064 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001065 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001066 iconThumb.setImageBitmap(cachedResult);
1067 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001068 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001069 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001070 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001071 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001072 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001073 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001074 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001075 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001076 }
1077
1078 // Always throw MIME icon into place, even when a thumbnail is being
1079 // loaded in background.
1080 if (cacheHit) {
1081 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001082 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001083 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001084 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001085 iconMime.setAlpha(1f);
1086 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001087 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001088 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001089 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001090 }
1091
Jeff Sharkey9656a532013-09-13 13:42:19 -07001092 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001093 boolean hasLine2 = false;
1094
Jeff Sharkey9656a532013-09-13 13:42:19 -07001095 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1096 if (!hideTitle) {
1097 title.setText(docDisplayName);
1098 hasLine1 = true;
1099 }
1100
1101 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001102 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001103 // We've already had to enumerate roots before any results can
1104 // be shown, so this will never block.
1105 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001106 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001107 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001108 } else {
Steve McKayef280152015-06-11 10:10:49 -07001109 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001110 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001111
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001112 if (summary != null) {
1113 final boolean alwaysShowSummary = getResources()
1114 .getBoolean(R.bool.always_show_summary);
1115 if (alwaysShowSummary) {
1116 summary.setText(root.getDirectoryString());
1117 summary.setVisibility(View.VISIBLE);
1118 hasLine2 = true;
1119 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001120 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001121 // No summary needed if icon speaks for itself
1122 summary.setVisibility(View.INVISIBLE);
1123 } else {
1124 summary.setText(root.getDirectoryString());
1125 summary.setVisibility(View.VISIBLE);
1126 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1127 hasLine2 = true;
1128 }
1129 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001130 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001131 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001132 // Directories showing thumbnails in grid mode get a little icon
1133 // hint to remind user they're a directory.
1134 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1135 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001136 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001137 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001138 }
1139
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001140 if (summary != null) {
1141 if (docSummary != null) {
1142 summary.setText(docSummary);
1143 summary.setVisibility(View.VISIBLE);
1144 hasLine2 = true;
1145 } else {
1146 summary.setVisibility(View.INVISIBLE);
1147 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001148 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001149 }
1150
Jeff Sharkey9656a532013-09-13 13:42:19 -07001151 if (icon1 != null) icon1.setVisibility(View.GONE);
1152 if (icon2 != null) icon2.setVisibility(View.GONE);
1153
1154 if (iconDrawable != null) {
1155 if (hasLine1) {
1156 icon1.setVisibility(View.VISIBLE);
1157 icon1.setImageDrawable(iconDrawable);
1158 } else {
1159 icon2.setVisibility(View.VISIBLE);
1160 icon2.setImageDrawable(iconDrawable);
1161 }
1162 }
1163
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001164 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001165 date.setText(null);
1166 } else {
Steve McKayef280152015-06-11 10:10:49 -07001167 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001168 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001169 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001170
1171 if (state.showSize) {
1172 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001173 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001174 size.setText(null);
1175 } else {
Steve McKayef280152015-06-11 10:10:49 -07001176 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001177 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001178 }
1179 } else {
1180 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001181 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001182
Jeff Sharkey9656a532013-09-13 13:42:19 -07001183 if (line1 != null) {
1184 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1185 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001186 if (line2 != null) {
1187 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1188 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001189
Steve McKayef280152015-06-11 10:10:49 -07001190 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001191
1192 iconMime.setAlpha(iconAlpha);
1193 iconThumb.setAlpha(iconAlpha);
1194 if (icon1 != null) icon1.setAlpha(iconAlpha);
1195 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001196
Steve McKay8e258c62015-05-06 14:27:57 -07001197 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001198 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001199 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001200 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001201
Steve McKay351a7492015-08-04 10:11:01 -07001202 @Override
Steve McKayef280152015-06-11 10:10:49 -07001203 public int getItemCount() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001204 return mModel.getItemCount();
Steve McKayef280152015-06-11 10:10:49 -07001205 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001206 }
1207
1208 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001209 public int getItemViewType(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001210 final int itemCount = mModel.getItemCount();
1211 if (position < itemCount) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001212 return 0;
1213 } else {
Ben Kwa24be5d32015-08-27 16:04:46 -07001214 position -= itemCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001215 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001216 }
1217 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001218 }
1219
1220 private static String formatTime(Context context, long when) {
1221 // TODO: DateUtils should make this easier
1222 Time then = new Time();
1223 then.set(when);
1224 Time now = new Time();
1225 now.setToNow();
1226
1227 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1228 | DateUtils.FORMAT_ABBREV_ALL;
1229
1230 if (then.year != now.year) {
1231 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1232 } else if (then.yearDay != now.yearDay) {
1233 flags |= DateUtils.FORMAT_SHOW_DATE;
1234 } else {
1235 flags |= DateUtils.FORMAT_SHOW_TIME;
1236 }
1237
1238 return DateUtils.formatDateTime(context, when, flags);
1239 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001240
1241 private String findCommonMimeType(List<String> mimeTypes) {
1242 String[] commonType = mimeTypes.get(0).split("/");
1243 if (commonType.length != 2) {
1244 return "*/*";
1245 }
1246
1247 for (int i = 1; i < mimeTypes.size(); i++) {
1248 String[] type = mimeTypes.get(i).split("/");
1249 if (type.length != 2) continue;
1250
1251 if (!commonType[1].equals(type[1])) {
1252 commonType[1] = "*";
1253 }
1254
1255 if (!commonType[0].equals(type[0])) {
1256 commonType[0] = "*";
1257 commonType[1] = "*";
1258 break;
1259 }
1260 }
1261
1262 return commonType[0] + "/" + commonType[1];
1263 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001264
1265 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001266 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001267 if (v.isEnabled() == enabled) return;
1268 v.setEnabled(enabled);
1269
1270 if (v instanceof ViewGroup) {
1271 final ViewGroup vg = (ViewGroup) v;
1272 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1273 setEnabledRecursive(vg.getChildAt(i), enabled);
1274 }
1275 }
1276 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001277
1278 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1279 final State state = getDisplayState(DirectoryFragment.this);
1280
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001281 // Directories are always enabled
1282 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1283 return true;
1284 }
1285
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001286 // Read-only files are disabled when creating
1287 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1288 return false;
1289 }
1290
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001291 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1292 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001293
Steve McKay1f199482015-05-20 15:58:42 -07001294 private void copyFromClipboard() {
1295 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1296
1297 @Override
1298 protected List<DocumentInfo> doInBackground(Void... params) {
1299 return mClipper.getClippedDocuments();
1300 }
1301
1302 @Override
1303 protected void onPostExecute(List<DocumentInfo> docs) {
1304 DocumentInfo destination =
1305 ((BaseActivity) getActivity()).getCurrentDirectory();
1306 copyDocuments(docs, destination);
1307 }
1308 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001309 }
1310
Steve McKay1f199482015-05-20 15:58:42 -07001311 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001312 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001313 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001314
Steve McKay1f199482015-05-20 15:58:42 -07001315 @Override
1316 protected List<DocumentInfo> doInBackground(Void... params) {
1317 return mClipper.getDocumentsFromClipData(clipData);
1318 }
1319
1320 @Override
1321 protected void onPostExecute(List<DocumentInfo> docs) {
1322 copyDocuments(docs, destination);
1323 }
1324 }.execute();
1325 }
1326
1327 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1328 if (!canCopy(docs, destination)) {
1329 Toast.makeText(
1330 getActivity(),
1331 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001332 return;
1333 }
1334
Steve McKay1f199482015-05-20 15:58:42 -07001335 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001336 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001337 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001338
Steve McKay1f199482015-05-20 15:58:42 -07001339 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001340 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001341 if (destination != null) {
1342 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001343 tmpStack.addAll(curStack);
1344 } else {
1345 tmpStack = curStack;
1346 }
1347
Steve McKay1f199482015-05-20 15:58:42 -07001348 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001349 }
1350
1351 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1352 Context context = getActivity();
1353 final ContentResolver resolver = context.getContentResolver();
1354 ClipData clipData = null;
1355 for (DocumentInfo doc : docs) {
1356 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1357 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001358 // TODO: figure out what this string should be.
1359 // Currently it is not displayed anywhere in the UI, but this might change.
1360 final String label = "";
1361 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001362 } else {
1363 // TODO: update list of mime types in ClipData.
1364 clipData.addItem(new ClipData.Item(uri));
1365 }
1366 }
1367 return clipData;
1368 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001369
Steve McKay1f199482015-05-20 15:58:42 -07001370 void copySelectedToClipboard() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001371 Selection sel = mModel.getSelection(new Selection());
Steve McKayef280152015-06-11 10:10:49 -07001372 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001373 }
Steve McKay0599a442015-05-05 14:50:00 -07001374
Steve McKayef280152015-06-11 10:10:49 -07001375 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001376 new GetDocumentsTask() {
1377 @Override
1378 void onDocumentsReady(List<DocumentInfo> docs) {
1379 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001380 Activity activity = getActivity();
1381 Toast.makeText(activity,
1382 activity.getResources().getQuantityString(
1383 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1384 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001385 }
Steve McKayef280152015-06-11 10:10:49 -07001386 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001387 }
1388
1389 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001390 copyFromClipboard();
1391 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001392 }
1393
Steve McKay0599a442015-05-05 14:50:00 -07001394 /**
1395 * Returns true if the list of files can be copied to destination. Note that this
1396 * is a policy check only. Currently the method does not attempt to verify
1397 * available space or any other environmental aspects possibly resulting in
1398 * failure to copy.
1399 *
1400 * @return true if the list of files can be copied to destination.
1401 */
1402 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
Ben Kwa91923182015-08-27 16:06:33 -07001403 BaseActivity activity = (BaseActivity) getActivity();
Steve McKay0599a442015-05-05 14:50:00 -07001404
1405 final RootInfo root = activity.getCurrentRoot();
1406
1407 // Can't copy folders to Downloads.
1408 if (root.isDownloads()) {
1409 for (DocumentInfo docs : files) {
1410 if (docs.isDirectory()) {
1411 return false;
1412 }
1413 }
1414 }
1415
1416 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1417 }
1418
1419 void selectAllFiles() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001420 boolean changed = mModel.selectAll();
Steve McKay9459a7c2015-07-24 13:14:20 -07001421 if (changed) {
1422 updateDisplayState();
1423 }
Steve McKay0599a442015-05-05 14:50:00 -07001424 }
1425
Steve McKayef280152015-06-11 10:10:49 -07001426 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001427 // Listen for drops on non-directory items and empty space.
1428 view.setOnDragListener(mOnDragListener);
1429 }
1430
1431 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1432 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1433 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1434 // Make a directory item a drop target. Drop on non-directories and empty space
1435 // is handled at the list/grid view level.
1436 view.setOnDragListener(mOnDragListener);
1437 }
1438
1439 // Temporary: attaching the listener to the title only.
1440 // Attaching to the entire item conflicts with the item long click handler responsible
1441 // for item selection.
1442 final View title = view.findViewById(android.R.id.title);
1443 title.setOnLongClickListener(mLongClickListener);
1444 }
1445
1446 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1447 @Override
1448 public boolean onDrag(View v, DragEvent event) {
1449 switch (event.getAction()) {
1450 case DragEvent.ACTION_DRAG_STARTED:
1451 // TODO: Check if the event contains droppable data.
1452 return true;
1453
1454 // TODO: Highlight potential drop target directory?
1455 // TODO: Expand drop target directory on hover?
1456 case DragEvent.ACTION_DRAG_ENTERED:
1457 case DragEvent.ACTION_DRAG_LOCATION:
1458 case DragEvent.ACTION_DRAG_EXITED:
1459 case DragEvent.ACTION_DRAG_ENDED:
1460 return true;
1461
1462 case DragEvent.ACTION_DROP:
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001463 int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001464 DocumentInfo dstDir = null;
1465 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001466 Cursor dstCursor = mModel.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001467 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001468 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1469 // TODO: Do not drop into the directory where the documents came from.
1470 }
1471 copyFromClipData(event.getClipData(), dstDir);
1472 return true;
1473 }
1474 return false;
1475 }
1476 };
1477
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001478 private View getContainingItemView(View view) {
1479 while (true) {
1480 if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
1481 return view;
1482 }
1483 ViewParent parent = view.getParent();
1484 if (parent == null || !(parent instanceof View)) {
1485 return null;
1486 }
1487 view = (View) parent;
1488 }
1489 }
1490
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001491 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1492 @Override
1493 public boolean onLongClick(View v) {
1494 final List<DocumentInfo> docs = getDraggableDocuments(v);
1495 if (docs.isEmpty()) {
1496 return false;
1497 }
1498 v.startDrag(
1499 getClipDataFromDocuments(docs),
1500 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1501 null,
1502 View.DRAG_FLAG_GLOBAL
1503 );
1504 return true;
1505 }
1506 };
1507
1508 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001509 int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001510 if (position == android.widget.AdapterView.INVALID_POSITION) {
1511 return Collections.EMPTY_LIST;
1512 }
1513
Ben Kwa24be5d32015-08-27 16:04:46 -07001514 final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001515 if (!selectedDocs.isEmpty()) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001516 if (!mModel.isSelected(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001517 // There is a selection that does not include the current item, drag nothing.
1518 return Collections.EMPTY_LIST;
1519 }
1520 return selectedDocs;
1521 }
1522
Ben Kwa24be5d32015-08-27 16:04:46 -07001523 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001524 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001525 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001526
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001527 return Lists.newArrayList(doc);
1528 }
1529
1530 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1531 if (docs.size() == 1) {
1532 final DocumentInfo doc = docs.get(0);
1533 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1534 doc.mimeType, doc.icon, getDisplayState(this));
1535 }
1536 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1537 }
1538
1539 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1540 String docMimeType, int docIcon, State state) {
1541 if (docIcon != 0) {
1542 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1543 } else {
1544 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1545 state.derivedMode);
1546 }
1547 }
1548
Steve McKayef280152015-06-11 10:10:49 -07001549 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1550 implements Preemptable {
1551 private final Uri mUri;
1552 private final ImageView mIconMime;
1553 private final ImageView mIconThumb;
1554 private final Point mThumbSize;
1555 private final float mTargetAlpha;
1556 private final CancellationSignal mSignal;
1557
1558 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1559 float targetAlpha) {
1560 mUri = uri;
1561 mIconMime = iconMime;
1562 mIconThumb = iconThumb;
1563 mThumbSize = thumbSize;
1564 mTargetAlpha = targetAlpha;
1565 mSignal = new CancellationSignal();
1566 }
1567
1568 @Override
1569 public void preempt() {
1570 cancel(false);
1571 mSignal.cancel();
1572 }
1573
1574 @Override
1575 protected Bitmap doInBackground(Uri... params) {
1576 if (isCancelled()) return null;
1577
1578 final Context context = mIconThumb.getContext();
1579 final ContentResolver resolver = context.getContentResolver();
1580
1581 ContentProviderClient client = null;
1582 Bitmap result = null;
1583 try {
1584 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1585 resolver, mUri.getAuthority());
1586 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1587 if (result != null) {
1588 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1589 context, mThumbSize);
1590 thumbs.put(mUri, result);
1591 }
1592 } catch (Exception e) {
1593 if (!(e instanceof OperationCanceledException)) {
1594 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1595 }
1596 } finally {
1597 ContentProviderClient.releaseQuietly(client);
1598 }
1599 return result;
1600 }
1601
1602 @Override
1603 protected void onPostExecute(Bitmap result) {
1604 if (mIconThumb.getTag() == this && result != null) {
1605 mIconThumb.setTag(null);
1606 mIconThumb.setImageBitmap(result);
1607
1608 mIconMime.setAlpha(mTargetAlpha);
1609 mIconMime.animate().alpha(0f).start();
1610 mIconThumb.setAlpha(0f);
1611 mIconThumb.animate().alpha(mTargetAlpha).start();
1612 }
1613 }
1614 }
1615
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001616 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1617
1618 private final Drawable mShadow;
1619
1620 private final int mShadowDimension;
1621
1622 public DrawableShadowBuilder(Drawable shadow) {
1623 mShadow = shadow;
1624 mShadowDimension = getResources().getDimensionPixelSize(
1625 R.dimen.drag_shadow_size);
1626 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1627 }
1628
Ben Kwa24be5d32015-08-27 16:04:46 -07001629 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001630 public void onProvideShadowMetrics(
1631 Point shadowSize, Point shadowTouchPoint) {
1632 shadowSize.set(mShadowDimension, mShadowDimension);
1633 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1634 }
1635
Ben Kwa24be5d32015-08-27 16:04:46 -07001636 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001637 public void onDrawShadow(Canvas canvas) {
1638 mShadow.draw(canvas);
1639 }
1640 }
Steve McKay1f199482015-05-20 15:58:42 -07001641
1642 private FragmentTuner pickFragmentTuner(final State state) {
1643 return state.action == ACTION_BROWSE_ALL
Steve McKay0fbfc652015-08-20 16:48:49 -07001644 ? new FilesTuner()
Steve McKay1f199482015-05-20 15:58:42 -07001645 : new DefaultTuner(state);
1646 }
1647
1648 /**
1649 * Interface for specializing the Fragment for the "host" Activity.
1650 * Feel free to expand the role of this class to handle other specializations.
1651 */
1652 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001653 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001654 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001655 }
1656
1657 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001658 * Abstract task providing support for loading documents *off*
1659 * the main thread. And if it isn't obvious, creating a list
1660 * of documents (especially large lists) can be pretty expensive.
1661 */
1662 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001663 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001664 @Override
Steve McKayef280152015-06-11 10:10:49 -07001665 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001666 return mModel.getDocuments(selected[0]);
Steve McKay9276f3b2015-05-27 16:11:42 -07001667 }
1668
1669 @Override
1670 protected final void onPostExecute(List<DocumentInfo> docs) {
1671 onDocumentsReady(docs);
1672 }
1673
1674 abstract void onDocumentsReady(List<DocumentInfo> docs);
1675 }
1676
1677 /**
Steve McKay1f199482015-05-20 15:58:42 -07001678 * Provides support for Platform specific specializations of DirectoryFragment.
1679 */
1680 private static final class DefaultTuner implements FragmentTuner {
1681
1682 private final State mState;
1683
1684 public DefaultTuner(State state) {
1685 mState = state;
1686 }
1687
1688 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001689 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001690 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1691
1692 final MenuItem open = menu.findItem(R.id.menu_open);
1693 final MenuItem share = menu.findItem(R.id.menu_share);
1694 final MenuItem delete = menu.findItem(R.id.menu_delete);
1695 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1696 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1697 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1698
1699 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1700 || mState.action == ACTION_BROWSE);
1701
1702 open.setVisible(!manageOrBrowse);
1703 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001704 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001705 // Disable copying from the Recents view.
1706 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1707 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1708
Steve McKay0fbfc652015-08-20 16:48:49 -07001709 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001710 copyToClipboard.setVisible(false);
1711 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001712
1713 @Override
1714 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001715 }
1716
1717 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001718 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001719 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001720 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001721 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001722 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001723 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001724 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001725 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1726
1727 menu.findItem(R.id.menu_open).setVisible(false);
1728 menu.findItem(R.id.menu_copy_to).setVisible(false);
1729 menu.findItem(R.id.menu_move_to).setVisible(false);
1730 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001731
1732 @Override
Kyle Horimoto62a7fd02015-08-18 13:25:29 -07001733 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001734 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001735
1736 /**
1737 * The data model for the current loaded directory.
1738 */
1739 private final class Model implements DocumentContext {
1740 private MultiSelectManager mSelectionManager;
1741 private int mCursorCount;
1742 private boolean mIsLoading;
1743 @Nullable private Cursor mCursor;
1744 @Nullable private String info;
1745 @Nullable private String error;
Ben Kwa91923182015-08-27 16:06:33 -07001746 private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
Ben Kwa24be5d32015-08-27 16:04:46 -07001747
1748 /**
1749 * Sets the selection manager used by the model.
1750 * TODO: the model should instantiate the selection manager. See onActivityCreated.
1751 */
1752 void setSelectionManager(MultiSelectManager mgr) {
1753 mSelectionManager = mgr;
1754 }
1755
1756 /**
1757 * Selects all files in the current directory.
1758 * @return true if the selection state changed for any files.
1759 */
1760 boolean selectAll() {
1761 return mSelectionManager.setItemsSelected(0, mCursorCount, true);
1762 }
1763
1764 /**
1765 * Clones the current selection into the given Selection object.
1766 * @param selection
1767 * @return The selection that was passed in, for convenience.
1768 */
1769 Selection getSelection(Selection selection) {
1770 return mSelectionManager.getSelection(selection);
1771 }
1772
1773 /**
1774 * @return The current selection (the live instance, not a copy).
1775 */
1776 Selection getSelection() {
1777 return mSelectionManager.getSelection();
1778 }
1779
1780 boolean isSelected(int position) {
1781 return mSelectionManager.getSelection().contains(position);
1782 }
1783
1784 void clearSelection() {
1785 mSelectionManager.clearSelection();
1786 }
1787
1788 void update(DirectoryResult result) {
1789 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
1790
1791 if (result == null) {
1792 mCursor = null;
1793 mCursorCount = 0;
1794 info = null;
1795 error = null;
1796 mIsLoading = false;
1797 return;
1798 }
1799
1800 if (result.exception != null) {
1801 Log.e(TAG, "Error while loading directory contents", result.exception);
1802 error = getString(R.string.query_error);
1803 return;
1804 }
1805
1806 mCursor = result.cursor;
1807 mCursorCount = mCursor.getCount();
1808
1809 final Bundle extras = mCursor.getExtras();
1810 if (extras != null) {
1811 info = extras.getString(DocumentsContract.EXTRA_INFO);
1812 error = extras.getString(DocumentsContract.EXTRA_ERROR);
1813 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
1814 }
1815 }
1816
1817 private int getItemCount() {
Ben Kwa91923182015-08-27 16:06:33 -07001818 return mCursorCount - mMarkedForDeletion.size();
Ben Kwa24be5d32015-08-27 16:04:46 -07001819 }
1820
1821 private Cursor getItem(int position) {
Ben Kwa91923182015-08-27 16:06:33 -07001822 // Items marked for deletion are masked out of the UI. To do this, for every marked
1823 // item whose position is less than the requested item position, advance the requested
1824 // position by 1.
1825 final int originalPos = position;
1826 final int size = mMarkedForDeletion.size();
1827 for (int i = 0; i <= size; ++i) {
1828 // It'd be more concise, but less efficient, to iterate over positions while calling
1829 // mMarkedForDeletion.get. Instead, iterate over deleted entries.
1830 if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
1831 ++position;
1832 }
1833 }
1834
1835 if (DEBUG) {
1836 Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos
1837 + " Adjusted: " + position);
1838 }
1839
Ben Kwa24be5d32015-08-27 16:04:46 -07001840 if (position >= mCursorCount) {
1841 throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
1842 mCursorCount + " items");
1843 }
1844
1845 mCursor.moveToPosition(position);
1846 return mCursor;
1847 }
1848
1849 private boolean isEmpty() {
1850 return mCursorCount == 0;
1851 }
1852
1853 private boolean isLoading() {
1854 return mIsLoading;
1855 }
1856
1857 private List<DocumentInfo> getSelectedDocuments() {
1858 Selection sel = getSelection(new Selection());
1859 return getDocuments(sel);
1860 }
1861
1862 private List<DocumentInfo> getDocuments(Selection items) {
Ben Kwa91923182015-08-27 16:06:33 -07001863 final int size = (items != null) ? items.size() : 0;
Ben Kwa24be5d32015-08-27 16:04:46 -07001864
Ben Kwa91923182015-08-27 16:06:33 -07001865 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwa24be5d32015-08-27 16:04:46 -07001866 for (int i = 0; i < size; i++) {
1867 final Cursor cursor = getItem(items.get(i));
1868 checkNotNull(cursor, "Cursor cannot be null.");
1869 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1870 docs.add(doc);
1871 }
1872 return docs;
1873 }
1874
1875 @Override
1876 public Cursor getCursor() {
1877 if (Looper.myLooper() != Looper.getMainLooper()) {
1878 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1879 }
1880 return mCursor;
1881 }
Ben Kwa91923182015-08-27 16:06:33 -07001882
1883 private List<DocumentInfo> getDocumentsMarkedForDeletion() {
1884 final int size = mMarkedForDeletion.size();
1885 List<DocumentInfo> docs = new ArrayList<>(size);
1886
1887 for (int i = 0; i < size; ++i) {
1888 final int position = mMarkedForDeletion.keyAt(i);
1889 checkState(position < mCursorCount);
1890 mCursor.moveToPosition(position);
1891 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
1892 docs.add(doc);
1893 }
1894 return docs;
1895 }
1896
1897 /**
1898 * Marks the given files for deletion. This will remove them from the UI. Clients must then
1899 * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
1900 * the deletion, respectively. Only one deletion operation is allowed at a time.
1901 *
1902 * @param selected A selection representing the files to delete.
1903 */
1904 public void markForDeletion(Selection selected) {
1905 // Only one deletion operation at a time.
1906 checkState(mMarkedForDeletion.size() == 0);
1907 // There should never be more to delete than what exists.
1908 checkState(mCursorCount >= selected.size());
1909
1910 final int size = selected.size();
1911 for (int i = 0; i < size; ++i) {
1912 int position = selected.get(i);
1913 if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
1914 mMarkedForDeletion.append(position, true);
1915 mAdapter.notifyItemRemoved(position);
1916 }
1917 }
1918
1919 /**
1920 * Cancels an ongoing deletion operation. All files currently marked for deletion will be
1921 * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}.
1922 */
1923 public void undoDeletion() {
1924 // Iterate over deleted items, temporarily marking them false in the deletion list, and
1925 // re-adding them to the UI.
1926 final int size = mMarkedForDeletion.size();
1927 for (int i = 0; i < size; ++i) {
1928 final int position = mMarkedForDeletion.keyAt(i);
1929 mMarkedForDeletion.put(position, false);
1930 mAdapter.notifyItemInserted(position);
1931 }
1932
1933 // Then, clear the deletion list.
1934 mMarkedForDeletion.clear();
1935 }
1936
1937 /**
1938 * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
1939 * deleted. See {@link #markForDeletion(Selection)}.
1940 */
1941 public void finalizeDeletion() {
1942 final Context context = getActivity();
1943 final ContentResolver resolver = context.getContentResolver();
1944 new DeleteFilesTask(resolver).execute();
1945 }
1946
1947 /**
1948 * A Task which collects the DocumentInfo for documents that have been marked for deletion,
1949 * and actually deletes them.
1950 */
1951 private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
1952 private ContentResolver mResolver;
1953
1954 public DeleteFilesTask(ContentResolver resolver) {
1955 mResolver = resolver;
1956 }
1957
1958 @Override
1959 protected List<DocumentInfo> doInBackground(Void... params) {
1960 return getDocumentsMarkedForDeletion();
1961 }
1962
1963 @Override
1964 protected void onPostExecute(List<DocumentInfo> docs) {
1965 boolean hadTrouble = false;
1966 for (DocumentInfo doc : docs) {
1967 if (!doc.isDeleteSupported()) {
1968 Log.w(TAG, doc + " could not be deleted. Skipping...");
1969 hadTrouble = true;
1970 continue;
1971 }
1972
1973 ContentProviderClient client = null;
1974 try {
1975 if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
1976 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1977 mResolver, doc.derivedUri.getAuthority());
1978 DocumentsContract.deleteDocument(client, doc.derivedUri);
1979 } catch (Exception e) {
1980 Log.w(TAG, "Failed to delete " + doc);
1981 hadTrouble = true;
1982 } finally {
1983 ContentProviderClient.releaseQuietly(client);
1984 }
1985 }
1986
1987 if (hadTrouble) {
1988 // TODO show which files failed?
1989 Snackbar.make(DirectoryFragment.this.getView(),
1990 R.string.toast_failed_delete,
1991 Snackbar.LENGTH_LONG).show();
1992 if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed.");
1993 } else {
1994 if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
1995 }
1996 mMarkedForDeletion.clear();
1997 }
1998 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001999 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07002000}