blob: 848ab93526c2528d29341b9f6a1099ac525bae6a [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;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.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
Steve McKay1f199482015-05-20 15:58:42 -070034import android.annotation.NonNull;
Ben Kwaf5858932015-04-07 15:43:39 -070035import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070036import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070037import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.app.FragmentManager;
39import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070041import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070042import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070043import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070044import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070045import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070046import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070048import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070049import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070051import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070052import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070053import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070055import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070056import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070057import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070058import android.os.Handler;
59import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070060import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070061import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070062import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070063import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070064import android.provider.DocumentsContract.Document;
Steve McKayef280152015-06-11 10:10:49 -070065import android.support.v7.widget.GridLayoutManager;
66import android.support.v7.widget.LinearLayoutManager;
67import android.support.v7.widget.RecyclerView;
68import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070069import android.support.v7.widget.RecyclerView.RecyclerListener;
70import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070071import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070073import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070074import android.text.format.Time;
75import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070076import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070077import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070078import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070079import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070082import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070085import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070086import android.view.ViewGroup;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087import android.widget.ImageView;
88import android.widget.ListView;
89import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070090import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070091
Steve McKayd0a2a2c2015-03-25 14:35:33 -070092import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070093import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070094import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070095import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070096import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090097import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070098import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -070099import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -0700100
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101import com.google.android.collect.Lists;
102
103import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700104import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700105import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700106
107/**
108 * Display the documents inside a single directory.
109 */
110public class DirectoryFragment extends Fragment {
111
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700112 public static final int TYPE_NORMAL = 1;
113 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700114 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700115
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700116 public static final int ANIM_NONE = 1;
117 public static final int ANIM_SIDE = 2;
118 public static final int ANIM_DOWN = 3;
119 public static final int ANIM_UP = 4;
120
Ben Kwaf5858932015-04-07 15:43:39 -0700121 public static final int REQUEST_COPY_DESTINATION = 1;
122
Steve McKayef280152015-06-11 10:10:49 -0700123 private static final int LOADER_ID = 42;
124 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700125 private static final boolean DEBUG_ENABLE_DND = false;
126
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 private static final String EXTRA_ROOT = "root";
129 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700130 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700131 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700132
Steve McKayef280152015-06-11 10:10:49 -0700133 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134
Steve McKayef280152015-06-11 10:10:49 -0700135 private View mEmptyView;
136 private RecyclerView mRecView;
137
138 private int mType = TYPE_NORMAL;
139 private String mStateKey;
140
141 private int mLastMode = MODE_UNKNOWN;
142 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
143 private boolean mLastShowSize;
144 private boolean mHideGridTitles;
145 private boolean mSvelteRecents;
146 private Point mThumbSize;
147 private DocumentsAdapter mAdapter;
148 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700149 private FragmentTuner mFragmentTuner;
150 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700151 private MultiSelectManager mSelectionManager;
152 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700153 private LinearLayoutManager mListLayout;
154 private GridLayoutManager mGridLayout;
155 private OnLayoutChangeListener mRecyclerLayoutListener;
156 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700157
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700158 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
159 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700160 }
161
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700162 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
163 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700164 }
165
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700166 public static void showRecentsOpen(FragmentManager fm, int anim) {
167 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700168 }
169
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700170 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
171 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700172 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700173 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700174 args.putParcelable(EXTRA_ROOT, root);
175 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700176 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700178 final FragmentTransaction ft = fm.beginTransaction();
179 switch (anim) {
180 case ANIM_SIDE:
181 args.putBoolean(EXTRA_IGNORE_STATE, true);
182 break;
183 case ANIM_DOWN:
184 args.putBoolean(EXTRA_IGNORE_STATE, true);
185 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
186 break;
187 case ANIM_UP:
188 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
189 break;
190 }
191
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700192 final DirectoryFragment fragment = new DirectoryFragment();
193 fragment.setArguments(args);
194
Jeff Sharkey76112212013-08-06 11:26:10 -0700195 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700196 ft.commitAllowingStateLoss();
197 }
198
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700199 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
200 final StringBuilder builder = new StringBuilder();
201 builder.append(root != null ? root.authority : "null").append(';');
202 builder.append(root != null ? root.rootId : "null").append(';');
203 builder.append(doc != null ? doc.documentId : "null");
204 return builder.toString();
205 }
206
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700207 public static DirectoryFragment get(FragmentManager fm) {
208 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700209 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700210 }
211
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700212 @Override
213 public View onCreateView(
214 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
215 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700216 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
218
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700219 mEmptyView = view.findViewById(android.R.id.empty);
220
Steve McKayef280152015-06-11 10:10:49 -0700221 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
222 mRecView.setRecyclerListener(
223 new RecyclerListener() {
224 @Override
225 public void onViewRecycled(ViewHolder holder) {
226 cancelThumbnailTask(holder.itemView);
227 }
228 });
Steve McKay8e258c62015-05-06 14:27:57 -0700229
Steve McKayd57f5fa2015-07-23 16:33:41 -0700230 // TODO: Rather than update columns on layout changes, push this
231 // code (or something like it) into GridLayoutManager.
232 mRecView.addOnLayoutChangeListener(
233 new OnLayoutChangeListener() {
234
235 @Override
236 public void onLayoutChange(
237 View v, int left, int top, int right, int bottom, int oldLeft,
238 int oldTop, int oldRight, int oldBottom) {
239 int thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
240 mColumnCount = pickColumnCount(thumbSize);
241 if (mGridLayout != null) {
242 mGridLayout.setSpanCount(mColumnCount);
243 }
244 }
245 });
246
247 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700248 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700249 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700250 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700251
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252 return view;
253 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700254
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700256 public void onDestroyView() {
257 super.onDestroyView();
258
259 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700260 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700261 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700262 final View view = mRecView.getChildAt(i);
263 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700265
Steve McKayef280152015-06-11 10:10:49 -0700266 // Clear any outstanding selection
267 mSelectionManager.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700268 }
269
270 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700271 public void onActivityCreated(Bundle savedInstanceState) {
272 super.onActivityCreated(savedInstanceState);
273
274 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700275 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700276
Jeff Sharkey9656a532013-09-13 13:42:19 -0700277 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
278 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
279
Steve McKayef280152015-06-11 10:10:49 -0700280 mAdapter = new DocumentsAdapter(context);
281 mRecView.setAdapter(mAdapter);
282
283 GestureDetector.SimpleOnGestureListener listener =
284 new GestureDetector.SimpleOnGestureListener() {
285 @Override
286 public boolean onSingleTapUp(MotionEvent e) {
287 return DirectoryFragment.this.onSingleTapUp(e);
288 }
289 };
290
291 mSelectionManager = new MultiSelectManager(mRecView, listener);
292 mSelectionManager.addCallback(new SelectionModeListener());
293
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700294 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700295 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700296
Steve McKay1f199482015-05-20 15:58:42 -0700297 mFragmentTuner = pickFragmentTuner(state);
298 mClipper = new DocumentClipper(context);
299
Jeff Sharkey9656a532013-09-13 13:42:19 -0700300 if (mType == TYPE_RECENT_OPEN) {
301 // Hide titles when showing recents for picking images/videos
302 mHideGridTitles = MimePredicate.mimeMatches(
303 MimePredicate.VISUAL_MIMES, state.acceptMimes);
304 } else {
305 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
306 }
307
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700308 final ActivityManager am = (ActivityManager) context.getSystemService(
309 Context.ACTIVITY_SERVICE);
310 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
311
Jeff Sharkey46899c82013-08-18 22:26:48 -0700312 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700313 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700314 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700315 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700316
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700317 Uri contentsUri;
318 switch (mType) {
319 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700320 contentsUri = DocumentsContract.buildChildDocumentsUri(
321 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700322 if (state.action == ACTION_MANAGE) {
323 contentsUri = DocumentsContract.setManageMode(contentsUri);
324 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700325 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700326 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700327 case TYPE_SEARCH:
328 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700329 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700330 if (state.action == ACTION_MANAGE) {
331 contentsUri = DocumentsContract.setManageMode(contentsUri);
332 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700333 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700334 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700335 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700336 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700337 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700338 default:
339 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700340 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700341 }
342
343 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700344 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700345 if (result == null || result.exception != null) {
346 // onBackPressed does a fragment transaction, which can't be done inside
347 // onLoadFinished
348 mHandler.post(new Runnable() {
349 @Override
350 public void run() {
351 final Activity activity = getActivity();
352 if (activity != null) {
353 activity.onBackPressed();
354 }
355 }
356 });
357 return;
358 }
359
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700360 if (!isAdded()) return;
361
Steve McKayef280152015-06-11 10:10:49 -0700362 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700363
364 // Push latest state up to UI
365 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700366 if (result.mode != MODE_UNKNOWN) {
367 state.derivedMode = result.mode;
368 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700369 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700370 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700371
372 updateDisplayState();
373
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700374 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700375 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
376 context instanceof DocumentsActivity) {
377 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700378 }
379
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700380 // Restore any previous instance state
381 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
382 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
383 getView().restoreHierarchyState(container);
384 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700385 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700386 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700387
388 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700389 }
390
391 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700392 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700393 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700394 }
395 };
396
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700397 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700398 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700399
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700400 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700401 }
402
Jeff Sharkey42d26792013-09-06 13:22:09 -0700403 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700404 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700405 // There's only one request code right now. Replace this with a switch statement or
406 // something more scalable when more codes are added.
407 if (requestCode != REQUEST_COPY_DESTINATION) {
408 return;
409 }
410 if (resultCode == Activity.RESULT_CANCELED || data == null) {
411 // User pressed the back button or otherwise cancelled the destination pick. Don't
412 // proceed with the copy.
413 return;
414 }
415
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900416 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700417 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
418 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700419 }
420
Steve McKayef280152015-06-11 10:10:49 -0700421 private int getEventAdapterPosition(MotionEvent e) {
422 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
423 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
424 }
425
426 private boolean onSingleTapUp(MotionEvent e) {
427 int position = getEventAdapterPosition(e);
428
429 if (position != RecyclerView.NO_POSITION) {
430 final Cursor cursor = mAdapter.getItem(position);
431 checkNotNull(cursor, "Cursor cannot be null.");
432 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
433 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
434 if (isDocumentEnabled(docMimeType, docFlags)) {
435 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
436 ((BaseActivity) getActivity()).onDocumentPicked(doc);
437 return true;
438 }
439 }
440
441 return false;
442 }
443
Ben Kwaf5858932015-04-07 15:43:39 -0700444 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700445 public void onStop() {
446 super.onStop();
447
448 // Remember last scroll location
449 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
450 getView().saveHierarchyState(container);
451 final State state = getDisplayState(this);
452 state.dirState.put(mStateKey, container);
453 }
454
455 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700456 public void onResume() {
457 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700458 updateDisplayState();
459 }
460
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700461 public void onDisplayStateChanged() {
462 updateDisplayState();
463 }
464
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700465 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700466 // Sort order change always triggers reload; we'll trigger state change
467 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700468 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700469 }
470
471 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700472 final ContentResolver resolver = getActivity().getContentResolver();
473 final State state = getDisplayState(this);
474
475 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
476 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
477
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700478 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700479 final Uri stateUri = RecentsProvider.buildState(
480 root.authority, root.rootId, doc.documentId);
481 final ContentValues values = new ContentValues();
482 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700483
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700484 new AsyncTask<Void, Void, Void>() {
485 @Override
486 protected Void doInBackground(Void... params) {
487 resolver.insert(stateUri, values);
488 return null;
489 }
490 }.execute();
491 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700492
493 // Mode change is just visual change; no need to kick loader, and
494 // deliver change event immediately.
495 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700496 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700497
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700498 updateDisplayState();
499 }
500
501 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700502 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700503
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700504 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700505 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700506 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700507
Steve McKayef280152015-06-11 10:10:49 -0700508 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700509
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700510 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700511 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700512 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
513 } else {
514 choiceMode = ListView.CHOICE_MODE_NONE;
515 }
516
Steve McKayef280152015-06-11 10:10:49 -0700517 final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
518 mThumbSize = new Point(thumbSize, thumbSize);
519 mRecView.setAdapter(mAdapter);
520 }
521
522 /**
523 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
524 * classes as needed.
525 */
526 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700527 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700528
529 final LayoutManager layout;
530 switch (mode) {
531 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700532 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700533 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700534 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700535 }
Steve McKayef280152015-06-11 10:10:49 -0700536 layout = mGridLayout;
537 break;
538 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700539 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700540 if (mListLayout == null) {
541 mListLayout = new LinearLayoutManager(getContext());
542 }
Steve McKayef280152015-06-11 10:10:49 -0700543 layout = mListLayout;
544 break;
545 case MODE_UNKNOWN:
546 default:
547 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700548 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700549
Steve McKayef280152015-06-11 10:10:49 -0700550 mRecView.setLayoutManager(layout);
551 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700552 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700553 }
554
Steve McKayd57f5fa2015-07-23 16:33:41 -0700555 private int pickColumnCount(final int thumbSize) {
556 int itemPadding =
557 getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
558 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
559 checkState(mRecView.getWidth() > 0);
560 int columnCount = Math.max(1,
561 (mRecView.getWidth() - viewPadding) / (thumbSize + itemPadding));
562 return columnCount;
563 }
564
Steve McKayef280152015-06-11 10:10:49 -0700565 /**
566 * Manages the integration between our ActionMode and MultiSelectManager, initiating
567 * ActionMode when there is a selection, canceling it when there is no selection,
568 * and clearing selection when action mode is explicitly exited by the user.
569 */
570 private final class SelectionModeListener
571 implements MultiSelectManager.Callback, ActionMode.Callback {
572
573 private Selection mSelected = new Selection();
574 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700575 private int mNoDeleteCount = 0;
576 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700577
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700578 @Override
Steve McKayef280152015-06-11 10:10:49 -0700579 public boolean onBeforeItemStateChange(int position, boolean selected) {
580 // Directories and footer items cannot be checked
581 if (selected) {
582 final Cursor cursor = mAdapter.getItem(position);
583 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700584 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
585 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700586 return isDocumentEnabled(docMimeType, docFlags);
587 }
588 return true;
589 }
590
591 @Override
592 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700593
594 final Cursor cursor = mAdapter.getItem(position);
595 checkNotNull(cursor, "Cursor cannot be null.");
596
597 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
598 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
599 mNoDeleteCount += selected ? 1 : -1;
600 }
601
Steve McKayef280152015-06-11 10:10:49 -0700602 mSelectionManager.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700603 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700604 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
605 if (mActionMode == null) {
606 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
607 mActionMode = getActivity().startActionMode(this);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700608 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700609 updateActionMenu();
610 } else {
611 if (DEBUG) Log.d(TAG, "Finishing action mode.");
612 if (mActionMode != null) {
613 mActionMode.finish();
614 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700615 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700616
Steve McKayef280152015-06-11 10:10:49 -0700617 if (mActionMode != null) {
618 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
619 }
620 }
621
622 // Called when the user exits the action mode
623 @Override
624 public void onDestroyActionMode(ActionMode mode) {
625 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
626 mActionMode = null;
627 // clear selection
628 mSelectionManager.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700629 mSelected.clear();
630 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700631 }
632
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700633 @Override
634 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
635 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Steve McKayef280152015-06-11 10:10:49 -0700636 mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
637 return mSelectionManager.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700638 }
639
640 @Override
641 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700642 mMenu = menu;
643 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700644 return true;
645 }
646
Steve McKay4f4232d2015-07-22 12:13:46 -0700647 private void updateActionMenu() {
648 checkNotNull(mMenu);
649 // Delegate update logic to our owning action, since specialized logic is desired.
650 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
651 }
652
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700653 @Override
Steve McKayef280152015-06-11 10:10:49 -0700654 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700655
Steve McKayef280152015-06-11 10:10:49 -0700656 Selection selection = new Selection();
657 mSelectionManager.getSelection(selection);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700658
Jeff Sharkey873daa32013-08-18 17:38:20 -0700659 final int id = item.getItemId();
660 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700661 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700662 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700663 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700664
665 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700666 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700667 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700668 return true;
669
670 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700671 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700672 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700673 return true;
674
Steve McKay1f199482015-05-20 15:58:42 -0700675 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700676 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700677 mode.finish();
678 return true;
679
Steve McKay1f199482015-05-20 15:58:42 -0700680 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700681 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700682 mode.finish();
683 return true;
684
Steve McKay1f199482015-05-20 15:58:42 -0700685 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700686 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700687 mode.finish();
688 return true;
689
Ben Kwa512a6ba2015-03-31 08:15:21 -0700690 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700691 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700692 return true;
693
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700694 } else {
695 return false;
696 }
697 }
Steve McKayef280152015-06-11 10:10:49 -0700698 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700699
Steve McKayef280152015-06-11 10:10:49 -0700700 private static void cancelThumbnailTask(View view) {
701 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
702 if (iconThumb != null) {
703 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
704 if (oldTask != null) {
705 oldTask.preempt();
706 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700707 }
708 }
Steve McKayef280152015-06-11 10:10:49 -0700709 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700710
Steve McKayef280152015-06-11 10:10:49 -0700711 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700712 new GetDocumentsTask() {
713 @Override
714 void onDocumentsReady(List<DocumentInfo> docs) {
715 // TODO: Implement support in standalone for opening multiple docs.
716 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700717 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700718 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700719 }
720
Steve McKayef280152015-06-11 10:10:49 -0700721 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700722 new GetDocumentsTask() {
723 @Override
724 void onDocumentsReady(List<DocumentInfo> docs) {
725 Intent intent;
726
727 // Filter out directories - those can't be shared.
728 List<DocumentInfo> docsForSend = Lists.newArrayList();
729 for (DocumentInfo doc: docs) {
730 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
731 docsForSend.add(doc);
732 }
733 }
734
735 if (docsForSend.size() == 1) {
736 final DocumentInfo doc = docsForSend.get(0);
737
738 intent = new Intent(Intent.ACTION_SEND);
739 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
740 intent.addCategory(Intent.CATEGORY_DEFAULT);
741 intent.setType(doc.mimeType);
742 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
743
744 } else if (docsForSend.size() > 1) {
745 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
746 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
747 intent.addCategory(Intent.CATEGORY_DEFAULT);
748
749 final ArrayList<String> mimeTypes = Lists.newArrayList();
750 final ArrayList<Uri> uris = Lists.newArrayList();
751 for (DocumentInfo doc : docsForSend) {
752 mimeTypes.add(doc.mimeType);
753 uris.add(doc.derivedUri);
754 }
755
756 intent.setType(findCommonMimeType(mimeTypes));
757 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
758
759 } else {
760 return;
761 }
762
763 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
764 startActivity(intent);
765 }
766 }.execute(selected);
767 }
768
Steve McKayef280152015-06-11 10:10:49 -0700769 private void deleteDocuments(final Selection selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700770 final Context context = getActivity();
771 final ContentResolver resolver = context.getContentResolver();
772
Steve McKay9276f3b2015-05-27 16:11:42 -0700773 new GetDocumentsTask() {
774 @Override
775 void onDocumentsReady(List<DocumentInfo> docs) {
776 boolean hadTrouble = false;
777 for (DocumentInfo doc : docs) {
778 if (!doc.isDeleteSupported()) {
779 Log.w(TAG, "Skipping " + doc);
780 hadTrouble = true;
781 continue;
782 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700783
Steve McKay9276f3b2015-05-27 16:11:42 -0700784 ContentProviderClient client = null;
785 try {
786 client = DocumentsApplication.acquireUnstableProviderOrThrow(
787 resolver, doc.derivedUri.getAuthority());
788 DocumentsContract.deleteDocument(client, doc.derivedUri);
789 } catch (Exception e) {
790 Log.w(TAG, "Failed to delete " + doc);
791 hadTrouble = true;
792 } finally {
793 ContentProviderClient.releaseQuietly(client);
794 }
795 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700796
Steve McKay9276f3b2015-05-27 16:11:42 -0700797 if (hadTrouble) {
798 Toast.makeText(
799 context,
800 R.string.toast_failed_delete,
801 Toast.LENGTH_SHORT).show();
802 }
803 }
804 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700805 }
806
Steve McKayef280152015-06-11 10:10:49 -0700807 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700808 // Pop up a dialog to pick a destination. This is inadequate but works for now.
809 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900810 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900811 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900812 Uri.EMPTY,
813 getActivity(),
814 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700815
816 new GetDocumentsTask() {
817 @Override
818 void onDocumentsReady(List<DocumentInfo> docs) {
819 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
820
821 boolean directoryCopy = false;
822 for (DocumentInfo info : docs) {
823 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
824 directoryCopy = true;
825 break;
826 }
827 }
828 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
829 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
830 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900831 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700832 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700833 }
834
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700835 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700836 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700837 }
838
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700839 private static abstract class Footer {
840 private final int mItemViewType;
841
842 public Footer(int itemViewType) {
843 mItemViewType = itemViewType;
844 }
845
846 public abstract View getView(View convertView, ViewGroup parent);
847
848 public int getItemViewType() {
849 return mItemViewType;
850 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700851 }
852
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700853 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700854 public LoadingFooter() {
855 super(1);
856 }
857
Jeff Sharkey20b32272013-09-03 15:25:52 -0700858 @Override
859 public View getView(View convertView, ViewGroup parent) {
860 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700861 final State state = getDisplayState(DirectoryFragment.this);
862
Jeff Sharkey20b32272013-09-03 15:25:52 -0700863 if (convertView == null) {
864 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700865 if (state.derivedMode == MODE_LIST) {
866 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
867 } else if (state.derivedMode == MODE_GRID) {
868 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
869 } else {
870 throw new IllegalStateException();
871 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700872 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700873
Jeff Sharkey20b32272013-09-03 15:25:52 -0700874 return convertView;
875 }
876 }
877
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700878 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700879 private final int mIcon;
880 private final String mMessage;
881
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700882 public MessageFooter(int itemViewType, int icon, String message) {
883 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700884 mIcon = icon;
885 mMessage = message;
886 }
887
888 @Override
889 public View getView(View convertView, ViewGroup parent) {
890 final Context context = parent.getContext();
891 final State state = getDisplayState(DirectoryFragment.this);
892
893 if (convertView == null) {
894 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700895 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700896 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700897 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700898 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
899 } else {
900 throw new IllegalStateException();
901 }
902 }
903
904 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
905 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
906 icon.setImageResource(mIcon);
907 title.setText(mMessage);
908 return convertView;
909 }
910 }
911
Steve McKayef280152015-06-11 10:10:49 -0700912 // Provide a reference to the views for each data item
913 // Complex data items may need more than one view per item, and
914 // you provide access to all the views for a data item in a view holder
915 private static final class DocumentHolder extends RecyclerView.ViewHolder {
916 // each data item is just a string in this case
917 public View view;
918 public String docId; // The stable document id.
919 public DocumentHolder(View view) {
920 super(view);
921 this.view = view;
922 }
923 }
924
925 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
926
927 private final Context mContext;
928 private final LayoutInflater mInflater;
929 // TODO: Bring back support for footers.
930 private final List<Footer> mFooters = Lists.newArrayList();
931
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700932 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700933 private int mCursorCount;
934
Steve McKayef280152015-06-11 10:10:49 -0700935 public DocumentsAdapter(Context context) {
936 mContext = context;
937 mInflater = LayoutInflater.from(context);
938 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700939
Steve McKayef280152015-06-11 10:10:49 -0700940 public void replaceResult(DirectoryResult result) {
941 if (DEBUG) Log.i(TAG, "Updating adapter with new result set.");
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700942 mCursor = result != null ? result.cursor : null;
943 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700944
945 mFooters.clear();
946
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700947 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700948 if (extras != null) {
949 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
950 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700951 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700952 }
953 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
954 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700955 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700956 }
957 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
958 mFooters.add(new LoadingFooter());
959 }
960 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700961
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700962 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700963 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700964 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700965 }
966
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700967 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700968 mEmptyView.setVisibility(View.VISIBLE);
969 } else {
970 mEmptyView.setVisibility(View.GONE);
971 }
972
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700973 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700974 }
975
976 @Override
Steve McKayef280152015-06-11 10:10:49 -0700977 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
978 final State state = getDisplayState(DirectoryFragment.this);
979 final LayoutInflater inflater = LayoutInflater.from(getContext());
980 switch (state.derivedMode) {
981 case MODE_GRID:
982 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
983 case MODE_LIST:
984 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
985 case MODE_UNKNOWN:
986 default:
987 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -0700988 }
989 }
990
Steve McKayef280152015-06-11 10:10:49 -0700991 @Override
992 public void onBindViewHolder(DocumentHolder holder, int position) {
993
994 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700995 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700996 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700997 final RootsCache roots = DocumentsApplication.getRootsCache(context);
998 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
999 context, mThumbSize);
1000
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001001 final Cursor cursor = getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001002 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001003
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001004 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1005 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001006 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1007 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1008 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1009 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1010 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1011 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1012 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1013 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001014
Steve McKayef280152015-06-11 10:10:49 -07001015 holder.docId = docId;
1016 final View itemView = holder.view;
1017 itemView.setActivated(mSelectionManager.getSelection().contains(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001018
Steve McKayef280152015-06-11 10:10:49 -07001019 final View line1 = itemView.findViewById(R.id.line1);
1020 final View line2 = itemView.findViewById(R.id.line2);
1021
1022 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1023 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1024 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1025 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1026 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1027 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1028 final TextView date = (TextView) itemView.findViewById(R.id.date);
1029 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001030
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001031 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001032 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001033 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001034 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001035 }
1036
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001037 iconMime.animate().cancel();
1038 iconThumb.animate().cancel();
1039
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001040 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001041 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001042 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001043 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001044
Jeff Sharkey7e544612014-08-29 15:38:27 -07001045 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1046 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1047
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001048 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001049 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001050 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001051 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001052 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001053 iconThumb.setImageBitmap(cachedResult);
1054 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001055 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001056 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001057 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001058 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001059 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001060 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001061 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001062 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001063 }
1064
1065 // Always throw MIME icon into place, even when a thumbnail is being
1066 // loaded in background.
1067 if (cacheHit) {
1068 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001069 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001071 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001072 iconMime.setAlpha(1f);
1073 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001074 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001075 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001076 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001077 }
1078
Jeff Sharkey9656a532013-09-13 13:42:19 -07001079 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001080 boolean hasLine2 = false;
1081
Jeff Sharkey9656a532013-09-13 13:42:19 -07001082 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1083 if (!hideTitle) {
1084 title.setText(docDisplayName);
1085 hasLine1 = true;
1086 }
1087
1088 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001089 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001090 // We've already had to enumerate roots before any results can
1091 // be shown, so this will never block.
1092 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001093 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001094 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001095 } else {
Steve McKayef280152015-06-11 10:10:49 -07001096 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001097 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001098
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001099 if (summary != null) {
1100 final boolean alwaysShowSummary = getResources()
1101 .getBoolean(R.bool.always_show_summary);
1102 if (alwaysShowSummary) {
1103 summary.setText(root.getDirectoryString());
1104 summary.setVisibility(View.VISIBLE);
1105 hasLine2 = true;
1106 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001107 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001108 // No summary needed if icon speaks for itself
1109 summary.setVisibility(View.INVISIBLE);
1110 } else {
1111 summary.setText(root.getDirectoryString());
1112 summary.setVisibility(View.VISIBLE);
1113 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1114 hasLine2 = true;
1115 }
1116 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001117 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001118 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001119 // Directories showing thumbnails in grid mode get a little icon
1120 // hint to remind user they're a directory.
1121 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1122 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001123 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001124 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001125 }
1126
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001127 if (summary != null) {
1128 if (docSummary != null) {
1129 summary.setText(docSummary);
1130 summary.setVisibility(View.VISIBLE);
1131 hasLine2 = true;
1132 } else {
1133 summary.setVisibility(View.INVISIBLE);
1134 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001135 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001136 }
1137
Jeff Sharkey9656a532013-09-13 13:42:19 -07001138 if (icon1 != null) icon1.setVisibility(View.GONE);
1139 if (icon2 != null) icon2.setVisibility(View.GONE);
1140
1141 if (iconDrawable != null) {
1142 if (hasLine1) {
1143 icon1.setVisibility(View.VISIBLE);
1144 icon1.setImageDrawable(iconDrawable);
1145 } else {
1146 icon2.setVisibility(View.VISIBLE);
1147 icon2.setImageDrawable(iconDrawable);
1148 }
1149 }
1150
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001151 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001152 date.setText(null);
1153 } else {
Steve McKayef280152015-06-11 10:10:49 -07001154 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001155 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001156 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001157
1158 if (state.showSize) {
1159 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001160 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001161 size.setText(null);
1162 } else {
Steve McKayef280152015-06-11 10:10:49 -07001163 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001164 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001165 }
1166 } else {
1167 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001168 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001169
Jeff Sharkey9656a532013-09-13 13:42:19 -07001170 if (line1 != null) {
1171 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1172 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001173 if (line2 != null) {
1174 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1175 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001176
Steve McKayef280152015-06-11 10:10:49 -07001177 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001178
1179 iconMime.setAlpha(iconAlpha);
1180 iconThumb.setAlpha(iconAlpha);
1181 if (icon1 != null) icon1.setAlpha(iconAlpha);
1182 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001183
Steve McKay8e258c62015-05-06 14:27:57 -07001184 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001185 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001186 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001187 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001188
Steve McKayef280152015-06-11 10:10:49 -07001189 private Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001190 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001191 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001192 return mCursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001193 }
Steve McKayef280152015-06-11 10:10:49 -07001194
1195 Log.w(TAG, "Returning null cursor for position: " + position);
1196 if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
1197 if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
1198 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001199 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001200
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001201 @Override
Steve McKayef280152015-06-11 10:10:49 -07001202 public int getItemCount() {
1203 return mCursorCount;
1204 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001205 }
1206
1207 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001208 public int getItemViewType(int position) {
1209 if (position < mCursorCount) {
1210 return 0;
1211 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001212 position -= mCursorCount;
1213 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001214 }
1215 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001216
Steve McKayef280152015-06-11 10:10:49 -07001217 private boolean isEmpty() {
1218 return getItemCount() > 0;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001219 }
1220 }
1221
1222 private static String formatTime(Context context, long when) {
1223 // TODO: DateUtils should make this easier
1224 Time then = new Time();
1225 then.set(when);
1226 Time now = new Time();
1227 now.setToNow();
1228
1229 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1230 | DateUtils.FORMAT_ABBREV_ALL;
1231
1232 if (then.year != now.year) {
1233 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1234 } else if (then.yearDay != now.yearDay) {
1235 flags |= DateUtils.FORMAT_SHOW_DATE;
1236 } else {
1237 flags |= DateUtils.FORMAT_SHOW_TIME;
1238 }
1239
1240 return DateUtils.formatDateTime(context, when, flags);
1241 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001242
1243 private String findCommonMimeType(List<String> mimeTypes) {
1244 String[] commonType = mimeTypes.get(0).split("/");
1245 if (commonType.length != 2) {
1246 return "*/*";
1247 }
1248
1249 for (int i = 1; i < mimeTypes.size(); i++) {
1250 String[] type = mimeTypes.get(i).split("/");
1251 if (type.length != 2) continue;
1252
1253 if (!commonType[1].equals(type[1])) {
1254 commonType[1] = "*";
1255 }
1256
1257 if (!commonType[0].equals(type[0])) {
1258 commonType[0] = "*";
1259 commonType[1] = "*";
1260 break;
1261 }
1262 }
1263
1264 return commonType[0] + "/" + commonType[1];
1265 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001266
1267 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001268 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001269 if (v.isEnabled() == enabled) return;
1270 v.setEnabled(enabled);
1271
1272 if (v instanceof ViewGroup) {
1273 final ViewGroup vg = (ViewGroup) v;
1274 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1275 setEnabledRecursive(vg.getChildAt(i), enabled);
1276 }
1277 }
1278 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001279
1280 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1281 final State state = getDisplayState(DirectoryFragment.this);
1282
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001283 // Directories are always enabled
1284 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1285 return true;
1286 }
1287
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001288 // Read-only files are disabled when creating
1289 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1290 return false;
1291 }
1292
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001293 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1294 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001295
Steve McKay1f199482015-05-20 15:58:42 -07001296 private @NonNull List<DocumentInfo> getSelectedDocuments() {
Steve McKayef280152015-06-11 10:10:49 -07001297 Selection sel = mSelectionManager.getSelection(new Selection());
1298 return getItemsAsDocuments(sel);
Steve McKay1f199482015-05-20 15:58:42 -07001299 }
1300
Steve McKayef280152015-06-11 10:10:49 -07001301 private List<DocumentInfo> getItemsAsDocuments(Selection items) {
Steve McKay1f199482015-05-20 15:58:42 -07001302 if (items == null || items.size() == 0) {
1303 return new ArrayList<>(0);
1304 }
1305
1306 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1307 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001308 for (int i = 0; i < size; i++) {
Steve McKayef280152015-06-11 10:10:49 -07001309 final Cursor cursor = mAdapter.getItem(items.get(i));
1310 checkNotNull(cursor, "Cursor cannot be null.");
1311 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1312 docs.add(doc);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001313 }
1314 return docs;
1315 }
1316
Steve McKay1f199482015-05-20 15:58:42 -07001317 private void copyFromClipboard() {
1318 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1319
1320 @Override
1321 protected List<DocumentInfo> doInBackground(Void... params) {
1322 return mClipper.getClippedDocuments();
1323 }
1324
1325 @Override
1326 protected void onPostExecute(List<DocumentInfo> docs) {
1327 DocumentInfo destination =
1328 ((BaseActivity) getActivity()).getCurrentDirectory();
1329 copyDocuments(docs, destination);
1330 }
1331 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001332 }
1333
Steve McKay1f199482015-05-20 15:58:42 -07001334 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001335 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001336 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001337
Steve McKay1f199482015-05-20 15:58:42 -07001338 @Override
1339 protected List<DocumentInfo> doInBackground(Void... params) {
1340 return mClipper.getDocumentsFromClipData(clipData);
1341 }
1342
1343 @Override
1344 protected void onPostExecute(List<DocumentInfo> docs) {
1345 copyDocuments(docs, destination);
1346 }
1347 }.execute();
1348 }
1349
1350 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1351 if (!canCopy(docs, destination)) {
1352 Toast.makeText(
1353 getActivity(),
1354 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001355 return;
1356 }
1357
Steve McKay1f199482015-05-20 15:58:42 -07001358 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001359 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001360 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001361
Steve McKay1f199482015-05-20 15:58:42 -07001362 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001363 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001364 if (destination != null) {
1365 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001366 tmpStack.addAll(curStack);
1367 } else {
1368 tmpStack = curStack;
1369 }
1370
Steve McKay1f199482015-05-20 15:58:42 -07001371 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001372 }
1373
1374 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1375 Context context = getActivity();
1376 final ContentResolver resolver = context.getContentResolver();
1377 ClipData clipData = null;
1378 for (DocumentInfo doc : docs) {
1379 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1380 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001381 // TODO: figure out what this string should be.
1382 // Currently it is not displayed anywhere in the UI, but this might change.
1383 final String label = "";
1384 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001385 } else {
1386 // TODO: update list of mime types in ClipData.
1387 clipData.addItem(new ClipData.Item(uri));
1388 }
1389 }
1390 return clipData;
1391 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001392
Steve McKay1f199482015-05-20 15:58:42 -07001393 void copySelectedToClipboard() {
Steve McKayef280152015-06-11 10:10:49 -07001394 Selection sel = mSelectionManager.getSelection(new Selection());
1395 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001396 }
Steve McKay0599a442015-05-05 14:50:00 -07001397
Steve McKayef280152015-06-11 10:10:49 -07001398 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001399 new GetDocumentsTask() {
1400 @Override
1401 void onDocumentsReady(List<DocumentInfo> docs) {
1402 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001403 Activity activity = getActivity();
1404 Toast.makeText(activity,
1405 activity.getResources().getQuantityString(
1406 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1407 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001408 }
Steve McKayef280152015-06-11 10:10:49 -07001409 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001410 }
1411
1412 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001413 copyFromClipboard();
1414 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001415 }
1416
Steve McKay0599a442015-05-05 14:50:00 -07001417 /**
1418 * Returns true if the list of files can be copied to destination. Note that this
1419 * is a policy check only. Currently the method does not attempt to verify
1420 * available space or any other environmental aspects possibly resulting in
1421 * failure to copy.
1422 *
1423 * @return true if the list of files can be copied to destination.
1424 */
1425 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1426 BaseActivity activity = (BaseActivity)getActivity();
1427
1428 final RootInfo root = activity.getCurrentRoot();
1429
1430 // Can't copy folders to Downloads.
1431 if (root.isDownloads()) {
1432 for (DocumentInfo docs : files) {
1433 if (docs.isDirectory()) {
1434 return false;
1435 }
1436 }
1437 }
1438
1439 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1440 }
1441
1442 void selectAllFiles() {
Steve McKayef280152015-06-11 10:10:49 -07001443 mSelectionManager.selectItems(0, mAdapter.getItemCount());
Steve McKay0599a442015-05-05 14:50:00 -07001444 updateDisplayState();
1445 }
1446
Steve McKayef280152015-06-11 10:10:49 -07001447 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001448 // Listen for drops on non-directory items and empty space.
1449 view.setOnDragListener(mOnDragListener);
1450 }
1451
1452 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1453 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1454 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1455 // Make a directory item a drop target. Drop on non-directories and empty space
1456 // is handled at the list/grid view level.
1457 view.setOnDragListener(mOnDragListener);
1458 }
1459
1460 // Temporary: attaching the listener to the title only.
1461 // Attaching to the entire item conflicts with the item long click handler responsible
1462 // for item selection.
1463 final View title = view.findViewById(android.R.id.title);
1464 title.setOnLongClickListener(mLongClickListener);
1465 }
1466
1467 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1468 @Override
1469 public boolean onDrag(View v, DragEvent event) {
1470 switch (event.getAction()) {
1471 case DragEvent.ACTION_DRAG_STARTED:
1472 // TODO: Check if the event contains droppable data.
1473 return true;
1474
1475 // TODO: Highlight potential drop target directory?
1476 // TODO: Expand drop target directory on hover?
1477 case DragEvent.ACTION_DRAG_ENTERED:
1478 case DragEvent.ACTION_DRAG_LOCATION:
1479 case DragEvent.ACTION_DRAG_EXITED:
1480 case DragEvent.ACTION_DRAG_ENDED:
1481 return true;
1482
1483 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001484 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001485 DocumentInfo dstDir = null;
1486 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1487 Cursor dstCursor = mAdapter.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001488 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001489 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1490 // TODO: Do not drop into the directory where the documents came from.
1491 }
1492 copyFromClipData(event.getClipData(), dstDir);
1493 return true;
1494 }
1495 return false;
1496 }
1497 };
1498
1499 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1500 @Override
1501 public boolean onLongClick(View v) {
1502 final List<DocumentInfo> docs = getDraggableDocuments(v);
1503 if (docs.isEmpty()) {
1504 return false;
1505 }
1506 v.startDrag(
1507 getClipDataFromDocuments(docs),
1508 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1509 null,
1510 View.DRAG_FLAG_GLOBAL
1511 );
1512 return true;
1513 }
1514 };
1515
1516 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001517 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001518 if (position == android.widget.AdapterView.INVALID_POSITION) {
1519 return Collections.EMPTY_LIST;
1520 }
1521
1522 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1523 if (!selectedDocs.isEmpty()) {
Steve McKayef280152015-06-11 10:10:49 -07001524 if (!mSelectionManager.getSelection().contains(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001525 // There is a selection that does not include the current item, drag nothing.
1526 return Collections.EMPTY_LIST;
1527 }
1528 return selectedDocs;
1529 }
1530
1531 final Cursor cursor = mAdapter.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001532 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001533 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1534 return Lists.newArrayList(doc);
1535 }
1536
1537 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1538 if (docs.size() == 1) {
1539 final DocumentInfo doc = docs.get(0);
1540 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1541 doc.mimeType, doc.icon, getDisplayState(this));
1542 }
1543 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1544 }
1545
1546 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1547 String docMimeType, int docIcon, State state) {
1548 if (docIcon != 0) {
1549 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1550 } else {
1551 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1552 state.derivedMode);
1553 }
1554 }
1555
Steve McKayef280152015-06-11 10:10:49 -07001556 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1557 implements Preemptable {
1558 private final Uri mUri;
1559 private final ImageView mIconMime;
1560 private final ImageView mIconThumb;
1561 private final Point mThumbSize;
1562 private final float mTargetAlpha;
1563 private final CancellationSignal mSignal;
1564
1565 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1566 float targetAlpha) {
1567 mUri = uri;
1568 mIconMime = iconMime;
1569 mIconThumb = iconThumb;
1570 mThumbSize = thumbSize;
1571 mTargetAlpha = targetAlpha;
1572 mSignal = new CancellationSignal();
1573 }
1574
1575 @Override
1576 public void preempt() {
1577 cancel(false);
1578 mSignal.cancel();
1579 }
1580
1581 @Override
1582 protected Bitmap doInBackground(Uri... params) {
1583 if (isCancelled()) return null;
1584
1585 final Context context = mIconThumb.getContext();
1586 final ContentResolver resolver = context.getContentResolver();
1587
1588 ContentProviderClient client = null;
1589 Bitmap result = null;
1590 try {
1591 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1592 resolver, mUri.getAuthority());
1593 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1594 if (result != null) {
1595 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1596 context, mThumbSize);
1597 thumbs.put(mUri, result);
1598 }
1599 } catch (Exception e) {
1600 if (!(e instanceof OperationCanceledException)) {
1601 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1602 }
1603 } finally {
1604 ContentProviderClient.releaseQuietly(client);
1605 }
1606 return result;
1607 }
1608
1609 @Override
1610 protected void onPostExecute(Bitmap result) {
1611 if (mIconThumb.getTag() == this && result != null) {
1612 mIconThumb.setTag(null);
1613 mIconThumb.setImageBitmap(result);
1614
1615 mIconMime.setAlpha(mTargetAlpha);
1616 mIconMime.animate().alpha(0f).start();
1617 mIconThumb.setAlpha(0f);
1618 mIconThumb.animate().alpha(mTargetAlpha).start();
1619 }
1620 }
1621 }
1622
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001623 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1624
1625 private final Drawable mShadow;
1626
1627 private final int mShadowDimension;
1628
1629 public DrawableShadowBuilder(Drawable shadow) {
1630 mShadow = shadow;
1631 mShadowDimension = getResources().getDimensionPixelSize(
1632 R.dimen.drag_shadow_size);
1633 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1634 }
1635
1636 public void onProvideShadowMetrics(
1637 Point shadowSize, Point shadowTouchPoint) {
1638 shadowSize.set(mShadowDimension, mShadowDimension);
1639 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1640 }
1641
1642 public void onDrawShadow(Canvas canvas) {
1643 mShadow.draw(canvas);
1644 }
1645 }
Steve McKay1f199482015-05-20 15:58:42 -07001646
1647 private FragmentTuner pickFragmentTuner(final State state) {
1648 return state.action == ACTION_BROWSE_ALL
1649 ? new StandaloneTuner()
1650 : new DefaultTuner(state);
1651 }
1652
1653 /**
1654 * Interface for specializing the Fragment for the "host" Activity.
1655 * Feel free to expand the role of this class to handle other specializations.
1656 */
1657 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001658 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001659 }
1660
1661 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001662 * Abstract task providing support for loading documents *off*
1663 * the main thread. And if it isn't obvious, creating a list
1664 * of documents (especially large lists) can be pretty expensive.
1665 */
1666 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001667 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001668 @Override
Steve McKayef280152015-06-11 10:10:49 -07001669 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001670 return getItemsAsDocuments(selected[0]);
1671 }
1672
1673 @Override
1674 protected final void onPostExecute(List<DocumentInfo> docs) {
1675 onDocumentsReady(docs);
1676 }
1677
1678 abstract void onDocumentsReady(List<DocumentInfo> docs);
1679 }
1680
1681 /**
Steve McKay1f199482015-05-20 15:58:42 -07001682 * Provides support for Platform specific specializations of DirectoryFragment.
1683 */
1684 private static final class DefaultTuner implements FragmentTuner {
1685
1686 private final State mState;
1687
1688 public DefaultTuner(State state) {
1689 mState = state;
1690 }
1691
1692 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001693 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001694 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1695
1696 final MenuItem open = menu.findItem(R.id.menu_open);
1697 final MenuItem share = menu.findItem(R.id.menu_share);
1698 final MenuItem delete = menu.findItem(R.id.menu_delete);
1699 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1700 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1701 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1702
1703 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1704 || mState.action == ACTION_BROWSE);
1705
1706 open.setVisible(!manageOrBrowse);
1707 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001708 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001709 // Disable copying from the Recents view.
1710 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1711 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1712
1713 // Only shown in standalone mode.
1714 copyToClipboard.setVisible(false);
1715 }
1716 }
1717
1718 /**
1719 * Provides support for Standalone specific specializations of DirectoryFragment.
1720 */
1721 private static final class StandaloneTuner implements FragmentTuner {
1722 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001723 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001724 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001725 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001726 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1727
1728 menu.findItem(R.id.menu_open).setVisible(false);
1729 menu.findItem(R.id.menu_copy_to).setVisible(false);
1730 menu.findItem(R.id.menu_move_to).setVisible(false);
1731 }
1732 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001733}