blob: 7e6ec8bc66f81d30f9c13a161dc1b196a3de06da [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 McKay351a7492015-08-04 10:11:01 -070092import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070093import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070094import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070095import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070096import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070097import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090098import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070099import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -0700100import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -0700101
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700102import com.google.android.collect.Lists;
103
104import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700105import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700106import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700107
108/**
109 * Display the documents inside a single directory.
110 */
111public class DirectoryFragment extends Fragment {
112
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700113 public static final int TYPE_NORMAL = 1;
114 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700115 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700116
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700117 public static final int ANIM_NONE = 1;
118 public static final int ANIM_SIDE = 2;
119 public static final int ANIM_DOWN = 3;
120 public static final int ANIM_UP = 4;
121
Ben Kwaf5858932015-04-07 15:43:39 -0700122 public static final int REQUEST_COPY_DESTINATION = 1;
123
Steve McKayef280152015-06-11 10:10:49 -0700124 private static final int LOADER_ID = 42;
125 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700126 private static final boolean DEBUG_ENABLE_DND = false;
127
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700128 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700129 private static final String EXTRA_ROOT = "root";
130 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700131 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700132 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133
Steve McKayef280152015-06-11 10:10:49 -0700134 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700135
Steve McKayef280152015-06-11 10:10:49 -0700136 private View mEmptyView;
137 private RecyclerView mRecView;
138
139 private int mType = TYPE_NORMAL;
140 private String mStateKey;
141
142 private int mLastMode = MODE_UNKNOWN;
143 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
144 private boolean mLastShowSize;
145 private boolean mHideGridTitles;
146 private boolean mSvelteRecents;
147 private Point mThumbSize;
148 private DocumentsAdapter mAdapter;
149 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700150 private FragmentTuner mFragmentTuner;
151 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700152 private MultiSelectManager mSelectionManager;
153 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700154 private LinearLayoutManager mListLayout;
155 private GridLayoutManager mGridLayout;
156 private OnLayoutChangeListener mRecyclerLayoutListener;
157 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700158
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700159 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
160 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700161 }
162
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700163 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
164 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700165 }
166
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700167 public static void showRecentsOpen(FragmentManager fm, int anim) {
168 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700169 }
170
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700171 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
172 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700173 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700174 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700175 args.putParcelable(EXTRA_ROOT, root);
176 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700177 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700178
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700179 final FragmentTransaction ft = fm.beginTransaction();
180 switch (anim) {
181 case ANIM_SIDE:
182 args.putBoolean(EXTRA_IGNORE_STATE, true);
183 break;
184 case ANIM_DOWN:
185 args.putBoolean(EXTRA_IGNORE_STATE, true);
186 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
187 break;
188 case ANIM_UP:
189 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
190 break;
191 }
192
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700193 final DirectoryFragment fragment = new DirectoryFragment();
194 fragment.setArguments(args);
195
Jeff Sharkey76112212013-08-06 11:26:10 -0700196 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700197 ft.commitAllowingStateLoss();
198 }
199
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700200 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
201 final StringBuilder builder = new StringBuilder();
202 builder.append(root != null ? root.authority : "null").append(';');
203 builder.append(root != null ? root.rootId : "null").append(';');
204 builder.append(doc != null ? doc.documentId : "null");
205 return builder.toString();
206 }
207
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700208 public static DirectoryFragment get(FragmentManager fm) {
209 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700210 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700211 }
212
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700213 @Override
214 public View onCreateView(
215 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
216 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700217 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700218 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
219
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700220 mEmptyView = view.findViewById(android.R.id.empty);
221
Steve McKayef280152015-06-11 10:10:49 -0700222 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
223 mRecView.setRecyclerListener(
224 new RecyclerListener() {
225 @Override
226 public void onViewRecycled(ViewHolder holder) {
227 cancelThumbnailTask(holder.itemView);
228 }
229 });
Steve McKay8e258c62015-05-06 14:27:57 -0700230
Steve McKayd57f5fa2015-07-23 16:33:41 -0700231 // TODO: Rather than update columns on layout changes, push this
232 // code (or something like it) into GridLayoutManager.
233 mRecView.addOnLayoutChangeListener(
234 new OnLayoutChangeListener() {
235
236 @Override
237 public void onLayoutChange(
238 View v, int left, int top, int right, int bottom, int oldLeft,
239 int oldTop, int oldRight, int oldBottom) {
240 int thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
241 mColumnCount = pickColumnCount(thumbSize);
242 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
268 mSelectionManager.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
297 mSelectionManager = new MultiSelectManager(mRecView, listener);
298 mSelectionManager.addCallback(new SelectionModeListener());
299
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700300 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700301 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700302
Steve McKay1f199482015-05-20 15:58:42 -0700303 mFragmentTuner = pickFragmentTuner(state);
304 mClipper = new DocumentClipper(context);
305
Jeff Sharkey9656a532013-09-13 13:42:19 -0700306 if (mType == TYPE_RECENT_OPEN) {
307 // Hide titles when showing recents for picking images/videos
308 mHideGridTitles = MimePredicate.mimeMatches(
309 MimePredicate.VISUAL_MIMES, state.acceptMimes);
310 } else {
311 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
312 }
313
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700314 final ActivityManager am = (ActivityManager) context.getSystemService(
315 Context.ACTIVITY_SERVICE);
316 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
317
Jeff Sharkey46899c82013-08-18 22:26:48 -0700318 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700319 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700320 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700321 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700322
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700323 Uri contentsUri;
324 switch (mType) {
325 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700326 contentsUri = DocumentsContract.buildChildDocumentsUri(
327 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700328 if (state.action == ACTION_MANAGE) {
329 contentsUri = DocumentsContract.setManageMode(contentsUri);
330 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700331 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700332 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700333 case TYPE_SEARCH:
334 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700335 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700336 if (state.action == ACTION_MANAGE) {
337 contentsUri = DocumentsContract.setManageMode(contentsUri);
338 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700339 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700340 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700341 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700342 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700343 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700344 default:
345 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700346 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700347 }
348
349 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700350 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700351 if (result == null || result.exception != null) {
352 // onBackPressed does a fragment transaction, which can't be done inside
353 // onLoadFinished
354 mHandler.post(new Runnable() {
355 @Override
356 public void run() {
357 final Activity activity = getActivity();
358 if (activity != null) {
359 activity.onBackPressed();
360 }
361 }
362 });
363 return;
364 }
365
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700366 if (!isAdded()) return;
367
Steve McKayef280152015-06-11 10:10:49 -0700368 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700369
370 // Push latest state up to UI
371 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700372 if (result.mode != MODE_UNKNOWN) {
373 state.derivedMode = result.mode;
374 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700375 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700376 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700377
378 updateDisplayState();
379
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700380 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700381 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
382 context instanceof DocumentsActivity) {
383 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700384 }
385
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700386 // Restore any previous instance state
387 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
388 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
389 getView().restoreHierarchyState(container);
390 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700391 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700392 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700393
394 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700395 }
396
397 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700398 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700399 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700400 }
401 };
402
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700403 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700404 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700405
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700406 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700407 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700408 }
409
Jeff Sharkey42d26792013-09-06 13:22:09 -0700410 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700411 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700412 // There's only one request code right now. Replace this with a switch statement or
413 // something more scalable when more codes are added.
414 if (requestCode != REQUEST_COPY_DESTINATION) {
415 return;
416 }
417 if (resultCode == Activity.RESULT_CANCELED || data == null) {
418 // User pressed the back button or otherwise cancelled the destination pick. Don't
419 // proceed with the copy.
420 return;
421 }
422
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900423 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700424 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
425 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700426 }
427
Steve McKayef280152015-06-11 10:10:49 -0700428 private int getEventAdapterPosition(MotionEvent e) {
429 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
430 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
431 }
432
433 private boolean onSingleTapUp(MotionEvent e) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700434 if (!Events.isMouseEvent(e)) {
435 int position = getEventAdapterPosition(e);
436 if (position != RecyclerView.NO_POSITION) {
437 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700438 }
439 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700440 return false;
441 }
Steve McKayef280152015-06-11 10:10:49 -0700442
Steve McKay93d8ef42015-07-30 12:27:44 -0700443 protected boolean onDoubleTap(MotionEvent e) {
444 if (Events.isMouseEvent(e)) {
445 Log.d(TAG, "Handling double tap from mouse.");
446 int position = getEventAdapterPosition(e);
447 if (position != RecyclerView.NO_POSITION) {
448 return handleViewItem(position);
449 }
450 }
451 return false;
452 }
453
454 private boolean handleViewItem(int position) {
455 final Cursor cursor = mAdapter.getItem(position);
456 checkNotNull(cursor, "Cursor cannot be null.");
457 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
458 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
459 if (isDocumentEnabled(docMimeType, docFlags)) {
460 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKay351a7492015-08-04 10:11:01 -0700461 ((BaseActivity) getActivity()).onDocumentPicked(doc, mAdapter);
Steve McKay93d8ef42015-07-30 12:27:44 -0700462 mSelectionManager.clearSelection();
463 return true;
464 }
Steve McKayef280152015-06-11 10:10:49 -0700465 return false;
466 }
467
Ben Kwaf5858932015-04-07 15:43:39 -0700468 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700469 public void onStop() {
470 super.onStop();
471
472 // Remember last scroll location
473 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
474 getView().saveHierarchyState(container);
475 final State state = getDisplayState(this);
476 state.dirState.put(mStateKey, container);
477 }
478
479 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700480 public void onResume() {
481 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700482 updateDisplayState();
483 }
484
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700485 public void onDisplayStateChanged() {
486 updateDisplayState();
487 }
488
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700489 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700490 // Sort order change always triggers reload; we'll trigger state change
491 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700492 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700493 }
494
495 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700496 final ContentResolver resolver = getActivity().getContentResolver();
497 final State state = getDisplayState(this);
498
499 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
500 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
501
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700502 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700503 final Uri stateUri = RecentsProvider.buildState(
504 root.authority, root.rootId, doc.documentId);
505 final ContentValues values = new ContentValues();
506 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700507
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700508 new AsyncTask<Void, Void, Void>() {
509 @Override
510 protected Void doInBackground(Void... params) {
511 resolver.insert(stateUri, values);
512 return null;
513 }
514 }.execute();
515 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700516
517 // Mode change is just visual change; no need to kick loader, and
518 // deliver change event immediately.
519 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700520 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700521
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700522 updateDisplayState();
523 }
524
525 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700526 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700527
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700528 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700529 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700530 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700531
Steve McKayef280152015-06-11 10:10:49 -0700532 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700533
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700534 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700535 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700536 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
537 } else {
538 choiceMode = ListView.CHOICE_MODE_NONE;
539 }
540
Steve McKayef280152015-06-11 10:10:49 -0700541 final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
542 mThumbSize = new Point(thumbSize, thumbSize);
543 mRecView.setAdapter(mAdapter);
544 }
545
546 /**
547 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
548 * classes as needed.
549 */
550 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700551 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700552
553 final LayoutManager layout;
554 switch (mode) {
555 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700556 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700557 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700558 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700559 }
Steve McKayef280152015-06-11 10:10:49 -0700560 layout = mGridLayout;
561 break;
562 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700563 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700564 if (mListLayout == null) {
565 mListLayout = new LinearLayoutManager(getContext());
566 }
Steve McKayef280152015-06-11 10:10:49 -0700567 layout = mListLayout;
568 break;
569 case MODE_UNKNOWN:
570 default:
571 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700572 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700573
Steve McKayef280152015-06-11 10:10:49 -0700574 mRecView.setLayoutManager(layout);
575 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700576 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700577 }
578
Steve McKayd57f5fa2015-07-23 16:33:41 -0700579 private int pickColumnCount(final int thumbSize) {
580 int itemPadding =
581 getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
582 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
583 checkState(mRecView.getWidth() > 0);
584 int columnCount = Math.max(1,
585 (mRecView.getWidth() - viewPadding) / (thumbSize + itemPadding));
586 return columnCount;
587 }
588
Steve McKayef280152015-06-11 10:10:49 -0700589 /**
590 * Manages the integration between our ActionMode and MultiSelectManager, initiating
591 * ActionMode when there is a selection, canceling it when there is no selection,
592 * and clearing selection when action mode is explicitly exited by the user.
593 */
594 private final class SelectionModeListener
595 implements MultiSelectManager.Callback, ActionMode.Callback {
596
597 private Selection mSelected = new Selection();
598 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700599 private int mNoDeleteCount = 0;
600 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700601
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700602 @Override
Steve McKayef280152015-06-11 10:10:49 -0700603 public boolean onBeforeItemStateChange(int position, boolean selected) {
604 // Directories and footer items cannot be checked
605 if (selected) {
606 final Cursor cursor = mAdapter.getItem(position);
607 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700608 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
609 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700610 return isDocumentEnabled(docMimeType, docFlags);
611 }
612 return true;
613 }
614
615 @Override
616 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700617
618 final Cursor cursor = mAdapter.getItem(position);
619 checkNotNull(cursor, "Cursor cannot be null.");
620
621 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
622 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
623 mNoDeleteCount += selected ? 1 : -1;
624 }
625
Steve McKayef280152015-06-11 10:10:49 -0700626 mSelectionManager.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700627 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700628 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
629 if (mActionMode == null) {
630 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
631 mActionMode = getActivity().startActionMode(this);
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900632 getActivity().getWindow().setStatusBarColor(
633 getResources().getColor(R.color.action_mode_status_bar_background));
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700634 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700635 updateActionMenu();
636 } else {
637 if (DEBUG) Log.d(TAG, "Finishing action mode.");
638 if (mActionMode != null) {
639 mActionMode.finish();
640 }
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900641 getActivity().getWindow().setStatusBarColor(
642 getResources().getColor(R.color.status_bar_background));
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700643 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700644
Steve McKayef280152015-06-11 10:10:49 -0700645 if (mActionMode != null) {
646 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
647 }
648 }
649
650 // Called when the user exits the action mode
651 @Override
652 public void onDestroyActionMode(ActionMode mode) {
653 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
654 mActionMode = null;
655 // clear selection
656 mSelectionManager.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700657 mSelected.clear();
658 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700659 }
660
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700661 @Override
662 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
663 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Steve McKayef280152015-06-11 10:10:49 -0700664 mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
665 return mSelectionManager.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700666 }
667
668 @Override
669 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700670 mMenu = menu;
671 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700672 return true;
673 }
674
Steve McKay4f4232d2015-07-22 12:13:46 -0700675 private void updateActionMenu() {
676 checkNotNull(mMenu);
677 // Delegate update logic to our owning action, since specialized logic is desired.
678 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
679 }
680
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700681 @Override
Steve McKayef280152015-06-11 10:10:49 -0700682 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700683
Steve McKayef280152015-06-11 10:10:49 -0700684 Selection selection = new Selection();
685 mSelectionManager.getSelection(selection);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700686
Jeff Sharkey873daa32013-08-18 17:38:20 -0700687 final int id = item.getItemId();
688 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700689 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700690 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700691 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700692
693 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700694 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700695 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700696 return true;
697
698 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700699 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700700 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700701 return true;
702
Steve McKay1f199482015-05-20 15:58:42 -0700703 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700704 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700705 mode.finish();
706 return true;
707
Steve McKay1f199482015-05-20 15:58:42 -0700708 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700709 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700710 mode.finish();
711 return true;
712
Steve McKay1f199482015-05-20 15:58:42 -0700713 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700714 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700715 mode.finish();
716 return true;
717
Ben Kwa512a6ba2015-03-31 08:15:21 -0700718 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700719 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700720 return true;
721
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700722 } else {
723 return false;
724 }
725 }
Steve McKayef280152015-06-11 10:10:49 -0700726 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700727
Steve McKayef280152015-06-11 10:10:49 -0700728 private static void cancelThumbnailTask(View view) {
729 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
730 if (iconThumb != null) {
731 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
732 if (oldTask != null) {
733 oldTask.preempt();
734 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700735 }
736 }
Steve McKayef280152015-06-11 10:10:49 -0700737 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700738
Steve McKayef280152015-06-11 10:10:49 -0700739 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700740 new GetDocumentsTask() {
741 @Override
742 void onDocumentsReady(List<DocumentInfo> docs) {
743 // TODO: Implement support in standalone for opening multiple docs.
744 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700745 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700746 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700747 }
748
Steve McKayef280152015-06-11 10:10:49 -0700749 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700750 new GetDocumentsTask() {
751 @Override
752 void onDocumentsReady(List<DocumentInfo> docs) {
753 Intent intent;
754
755 // Filter out directories - those can't be shared.
756 List<DocumentInfo> docsForSend = Lists.newArrayList();
757 for (DocumentInfo doc: docs) {
758 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
759 docsForSend.add(doc);
760 }
761 }
762
763 if (docsForSend.size() == 1) {
764 final DocumentInfo doc = docsForSend.get(0);
765
766 intent = new Intent(Intent.ACTION_SEND);
767 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
768 intent.addCategory(Intent.CATEGORY_DEFAULT);
769 intent.setType(doc.mimeType);
770 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
771
772 } else if (docsForSend.size() > 1) {
773 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
774 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
775 intent.addCategory(Intent.CATEGORY_DEFAULT);
776
777 final ArrayList<String> mimeTypes = Lists.newArrayList();
778 final ArrayList<Uri> uris = Lists.newArrayList();
779 for (DocumentInfo doc : docsForSend) {
780 mimeTypes.add(doc.mimeType);
781 uris.add(doc.derivedUri);
782 }
783
784 intent.setType(findCommonMimeType(mimeTypes));
785 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
786
787 } else {
788 return;
789 }
790
791 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
792 startActivity(intent);
793 }
794 }.execute(selected);
795 }
796
Steve McKayef280152015-06-11 10:10:49 -0700797 private void deleteDocuments(final Selection selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700798 final Context context = getActivity();
799 final ContentResolver resolver = context.getContentResolver();
800
Steve McKay9276f3b2015-05-27 16:11:42 -0700801 new GetDocumentsTask() {
802 @Override
803 void onDocumentsReady(List<DocumentInfo> docs) {
804 boolean hadTrouble = false;
805 for (DocumentInfo doc : docs) {
806 if (!doc.isDeleteSupported()) {
807 Log.w(TAG, "Skipping " + doc);
808 hadTrouble = true;
809 continue;
810 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700811
Steve McKay9276f3b2015-05-27 16:11:42 -0700812 ContentProviderClient client = null;
813 try {
814 client = DocumentsApplication.acquireUnstableProviderOrThrow(
815 resolver, doc.derivedUri.getAuthority());
816 DocumentsContract.deleteDocument(client, doc.derivedUri);
817 } catch (Exception e) {
818 Log.w(TAG, "Failed to delete " + doc);
819 hadTrouble = true;
820 } finally {
821 ContentProviderClient.releaseQuietly(client);
822 }
823 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700824
Steve McKay9276f3b2015-05-27 16:11:42 -0700825 if (hadTrouble) {
826 Toast.makeText(
827 context,
828 R.string.toast_failed_delete,
829 Toast.LENGTH_SHORT).show();
830 }
831 }
832 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700833 }
834
Steve McKayef280152015-06-11 10:10:49 -0700835 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700836 // Pop up a dialog to pick a destination. This is inadequate but works for now.
837 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900838 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900839 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900840 Uri.EMPTY,
841 getActivity(),
842 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700843
844 new GetDocumentsTask() {
845 @Override
846 void onDocumentsReady(List<DocumentInfo> docs) {
847 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
848
849 boolean directoryCopy = false;
850 for (DocumentInfo info : docs) {
851 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
852 directoryCopy = true;
853 break;
854 }
855 }
856 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
857 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
858 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900859 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700860 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700861 }
862
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700863 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700864 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700865 }
866
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700867 private static abstract class Footer {
868 private final int mItemViewType;
869
870 public Footer(int itemViewType) {
871 mItemViewType = itemViewType;
872 }
873
874 public abstract View getView(View convertView, ViewGroup parent);
875
876 public int getItemViewType() {
877 return mItemViewType;
878 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700879 }
880
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700881 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700882 public LoadingFooter() {
883 super(1);
884 }
885
Jeff Sharkey20b32272013-09-03 15:25:52 -0700886 @Override
887 public View getView(View convertView, ViewGroup parent) {
888 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700889 final State state = getDisplayState(DirectoryFragment.this);
890
Jeff Sharkey20b32272013-09-03 15:25:52 -0700891 if (convertView == null) {
892 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700893 if (state.derivedMode == MODE_LIST) {
894 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
895 } else if (state.derivedMode == MODE_GRID) {
896 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
897 } else {
898 throw new IllegalStateException();
899 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700900 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700901
Jeff Sharkey20b32272013-09-03 15:25:52 -0700902 return convertView;
903 }
904 }
905
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700906 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700907 private final int mIcon;
908 private final String mMessage;
909
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700910 public MessageFooter(int itemViewType, int icon, String message) {
911 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700912 mIcon = icon;
913 mMessage = message;
914 }
915
916 @Override
917 public View getView(View convertView, ViewGroup parent) {
918 final Context context = parent.getContext();
919 final State state = getDisplayState(DirectoryFragment.this);
920
921 if (convertView == null) {
922 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700923 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700924 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700925 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700926 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
927 } else {
928 throw new IllegalStateException();
929 }
930 }
931
932 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
933 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
934 icon.setImageResource(mIcon);
935 title.setText(mMessage);
936 return convertView;
937 }
938 }
939
Steve McKayef280152015-06-11 10:10:49 -0700940 // Provide a reference to the views for each data item
941 // Complex data items may need more than one view per item, and
942 // you provide access to all the views for a data item in a view holder
943 private static final class DocumentHolder extends RecyclerView.ViewHolder {
944 // each data item is just a string in this case
945 public View view;
946 public String docId; // The stable document id.
947 public DocumentHolder(View view) {
948 super(view);
949 this.view = view;
950 }
951 }
952
Steve McKay351a7492015-08-04 10:11:01 -0700953 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder>
954 implements DocumentContext {
Steve McKayef280152015-06-11 10:10:49 -0700955
956 private final Context mContext;
957 private final LayoutInflater mInflater;
958 // TODO: Bring back support for footers.
959 private final List<Footer> mFooters = Lists.newArrayList();
960
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700961 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700962 private int mCursorCount;
963
Steve McKayef280152015-06-11 10:10:49 -0700964 public DocumentsAdapter(Context context) {
965 mContext = context;
966 mInflater = LayoutInflater.from(context);
967 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700968
Steve McKayef280152015-06-11 10:10:49 -0700969 public void replaceResult(DirectoryResult result) {
970 if (DEBUG) Log.i(TAG, "Updating adapter with new result set.");
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700971 mCursor = result != null ? result.cursor : null;
972 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700973
974 mFooters.clear();
975
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700976 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700977 if (extras != null) {
978 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
979 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700980 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700981 }
982 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
983 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700984 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700985 }
986 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
987 mFooters.add(new LoadingFooter());
988 }
989 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700990
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700991 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700992 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700993 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700994 }
995
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700996 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700997 mEmptyView.setVisibility(View.VISIBLE);
998 } else {
999 mEmptyView.setVisibility(View.GONE);
1000 }
1001
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001002 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001003 }
1004
1005 @Override
Steve McKayef280152015-06-11 10:10:49 -07001006 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
1007 final State state = getDisplayState(DirectoryFragment.this);
1008 final LayoutInflater inflater = LayoutInflater.from(getContext());
1009 switch (state.derivedMode) {
1010 case MODE_GRID:
1011 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
1012 case MODE_LIST:
1013 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
1014 case MODE_UNKNOWN:
1015 default:
1016 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -07001017 }
1018 }
1019
Steve McKayef280152015-06-11 10:10:49 -07001020 @Override
1021 public void onBindViewHolder(DocumentHolder holder, int position) {
1022
1023 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001024 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001025 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -07001026 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1027 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1028 context, mThumbSize);
1029
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001030 final Cursor cursor = getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001031 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001032
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001033 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1034 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001035 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1036 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1037 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1038 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1039 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1040 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1041 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1042 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001043
Steve McKayef280152015-06-11 10:10:49 -07001044 holder.docId = docId;
1045 final View itemView = holder.view;
1046 itemView.setActivated(mSelectionManager.getSelection().contains(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001047
Steve McKayef280152015-06-11 10:10:49 -07001048 final View line1 = itemView.findViewById(R.id.line1);
1049 final View line2 = itemView.findViewById(R.id.line2);
1050
1051 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1052 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1053 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1054 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1055 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1056 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1057 final TextView date = (TextView) itemView.findViewById(R.id.date);
1058 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001059
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001060 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001061 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001062 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001063 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001064 }
1065
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001066 iconMime.animate().cancel();
1067 iconThumb.animate().cancel();
1068
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001069 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001070 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001071 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001072 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001073
Jeff Sharkey7e544612014-08-29 15:38:27 -07001074 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1075 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1076
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001077 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001078 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001079 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001080 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001081 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001082 iconThumb.setImageBitmap(cachedResult);
1083 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001084 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001085 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001086 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001087 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001088 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001089 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001090 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001091 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001092 }
1093
1094 // Always throw MIME icon into place, even when a thumbnail is being
1095 // loaded in background.
1096 if (cacheHit) {
1097 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001098 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001099 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001100 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001101 iconMime.setAlpha(1f);
1102 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001103 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001104 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001105 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001106 }
1107
Jeff Sharkey9656a532013-09-13 13:42:19 -07001108 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001109 boolean hasLine2 = false;
1110
Jeff Sharkey9656a532013-09-13 13:42:19 -07001111 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1112 if (!hideTitle) {
1113 title.setText(docDisplayName);
1114 hasLine1 = true;
1115 }
1116
1117 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001118 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001119 // We've already had to enumerate roots before any results can
1120 // be shown, so this will never block.
1121 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001122 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001123 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001124 } else {
Steve McKayef280152015-06-11 10:10:49 -07001125 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001126 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001127
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001128 if (summary != null) {
1129 final boolean alwaysShowSummary = getResources()
1130 .getBoolean(R.bool.always_show_summary);
1131 if (alwaysShowSummary) {
1132 summary.setText(root.getDirectoryString());
1133 summary.setVisibility(View.VISIBLE);
1134 hasLine2 = true;
1135 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001136 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001137 // No summary needed if icon speaks for itself
1138 summary.setVisibility(View.INVISIBLE);
1139 } else {
1140 summary.setText(root.getDirectoryString());
1141 summary.setVisibility(View.VISIBLE);
1142 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1143 hasLine2 = true;
1144 }
1145 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001146 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001147 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001148 // Directories showing thumbnails in grid mode get a little icon
1149 // hint to remind user they're a directory.
1150 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1151 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001152 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001153 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001154 }
1155
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001156 if (summary != null) {
1157 if (docSummary != null) {
1158 summary.setText(docSummary);
1159 summary.setVisibility(View.VISIBLE);
1160 hasLine2 = true;
1161 } else {
1162 summary.setVisibility(View.INVISIBLE);
1163 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001164 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001165 }
1166
Jeff Sharkey9656a532013-09-13 13:42:19 -07001167 if (icon1 != null) icon1.setVisibility(View.GONE);
1168 if (icon2 != null) icon2.setVisibility(View.GONE);
1169
1170 if (iconDrawable != null) {
1171 if (hasLine1) {
1172 icon1.setVisibility(View.VISIBLE);
1173 icon1.setImageDrawable(iconDrawable);
1174 } else {
1175 icon2.setVisibility(View.VISIBLE);
1176 icon2.setImageDrawable(iconDrawable);
1177 }
1178 }
1179
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001180 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001181 date.setText(null);
1182 } else {
Steve McKayef280152015-06-11 10:10:49 -07001183 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001184 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001185 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001186
1187 if (state.showSize) {
1188 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001189 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001190 size.setText(null);
1191 } else {
Steve McKayef280152015-06-11 10:10:49 -07001192 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001193 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001194 }
1195 } else {
1196 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001197 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001198
Jeff Sharkey9656a532013-09-13 13:42:19 -07001199 if (line1 != null) {
1200 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1201 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001202 if (line2 != null) {
1203 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1204 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001205
Steve McKayef280152015-06-11 10:10:49 -07001206 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001207
1208 iconMime.setAlpha(iconAlpha);
1209 iconThumb.setAlpha(iconAlpha);
1210 if (icon1 != null) icon1.setAlpha(iconAlpha);
1211 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001212
Steve McKay8e258c62015-05-06 14:27:57 -07001213 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001214 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001215 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001216 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001217
Steve McKay351a7492015-08-04 10:11:01 -07001218 @Override
1219 public Cursor getCursor() {
1220 if (Looper.myLooper() != Looper.getMainLooper()) {
1221 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1222 }
1223 return mCursor;
1224 }
1225
Steve McKayef280152015-06-11 10:10:49 -07001226 private Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001227 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001228 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001229 return mCursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001230 }
Steve McKayef280152015-06-11 10:10:49 -07001231
1232 Log.w(TAG, "Returning null cursor for position: " + position);
1233 if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
1234 if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
1235 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001236 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001237
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001238 @Override
Steve McKayef280152015-06-11 10:10:49 -07001239 public int getItemCount() {
1240 return mCursorCount;
1241 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001242 }
1243
1244 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001245 public int getItemViewType(int position) {
1246 if (position < mCursorCount) {
1247 return 0;
1248 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001249 position -= mCursorCount;
1250 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001251 }
1252 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001253
Steve McKayef280152015-06-11 10:10:49 -07001254 private boolean isEmpty() {
1255 return getItemCount() > 0;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001256 }
1257 }
1258
1259 private static String formatTime(Context context, long when) {
1260 // TODO: DateUtils should make this easier
1261 Time then = new Time();
1262 then.set(when);
1263 Time now = new Time();
1264 now.setToNow();
1265
1266 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1267 | DateUtils.FORMAT_ABBREV_ALL;
1268
1269 if (then.year != now.year) {
1270 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1271 } else if (then.yearDay != now.yearDay) {
1272 flags |= DateUtils.FORMAT_SHOW_DATE;
1273 } else {
1274 flags |= DateUtils.FORMAT_SHOW_TIME;
1275 }
1276
1277 return DateUtils.formatDateTime(context, when, flags);
1278 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001279
1280 private String findCommonMimeType(List<String> mimeTypes) {
1281 String[] commonType = mimeTypes.get(0).split("/");
1282 if (commonType.length != 2) {
1283 return "*/*";
1284 }
1285
1286 for (int i = 1; i < mimeTypes.size(); i++) {
1287 String[] type = mimeTypes.get(i).split("/");
1288 if (type.length != 2) continue;
1289
1290 if (!commonType[1].equals(type[1])) {
1291 commonType[1] = "*";
1292 }
1293
1294 if (!commonType[0].equals(type[0])) {
1295 commonType[0] = "*";
1296 commonType[1] = "*";
1297 break;
1298 }
1299 }
1300
1301 return commonType[0] + "/" + commonType[1];
1302 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001303
1304 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001305 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001306 if (v.isEnabled() == enabled) return;
1307 v.setEnabled(enabled);
1308
1309 if (v instanceof ViewGroup) {
1310 final ViewGroup vg = (ViewGroup) v;
1311 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1312 setEnabledRecursive(vg.getChildAt(i), enabled);
1313 }
1314 }
1315 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001316
1317 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1318 final State state = getDisplayState(DirectoryFragment.this);
1319
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001320 // Directories are always enabled
1321 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1322 return true;
1323 }
1324
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001325 // Read-only files are disabled when creating
1326 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1327 return false;
1328 }
1329
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001330 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1331 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001332
Steve McKay1f199482015-05-20 15:58:42 -07001333 private @NonNull List<DocumentInfo> getSelectedDocuments() {
Steve McKayef280152015-06-11 10:10:49 -07001334 Selection sel = mSelectionManager.getSelection(new Selection());
1335 return getItemsAsDocuments(sel);
Steve McKay1f199482015-05-20 15:58:42 -07001336 }
1337
Steve McKayef280152015-06-11 10:10:49 -07001338 private List<DocumentInfo> getItemsAsDocuments(Selection items) {
Steve McKay1f199482015-05-20 15:58:42 -07001339 if (items == null || items.size() == 0) {
1340 return new ArrayList<>(0);
1341 }
1342
1343 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1344 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001345 for (int i = 0; i < size; i++) {
Steve McKayef280152015-06-11 10:10:49 -07001346 final Cursor cursor = mAdapter.getItem(items.get(i));
1347 checkNotNull(cursor, "Cursor cannot be null.");
1348 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1349 docs.add(doc);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001350 }
1351 return docs;
1352 }
1353
Steve McKay1f199482015-05-20 15:58:42 -07001354 private void copyFromClipboard() {
1355 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1356
1357 @Override
1358 protected List<DocumentInfo> doInBackground(Void... params) {
1359 return mClipper.getClippedDocuments();
1360 }
1361
1362 @Override
1363 protected void onPostExecute(List<DocumentInfo> docs) {
1364 DocumentInfo destination =
1365 ((BaseActivity) getActivity()).getCurrentDirectory();
1366 copyDocuments(docs, destination);
1367 }
1368 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001369 }
1370
Steve McKay1f199482015-05-20 15:58:42 -07001371 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001372 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001373 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001374
Steve McKay1f199482015-05-20 15:58:42 -07001375 @Override
1376 protected List<DocumentInfo> doInBackground(Void... params) {
1377 return mClipper.getDocumentsFromClipData(clipData);
1378 }
1379
1380 @Override
1381 protected void onPostExecute(List<DocumentInfo> docs) {
1382 copyDocuments(docs, destination);
1383 }
1384 }.execute();
1385 }
1386
1387 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1388 if (!canCopy(docs, destination)) {
1389 Toast.makeText(
1390 getActivity(),
1391 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001392 return;
1393 }
1394
Steve McKay1f199482015-05-20 15:58:42 -07001395 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001396 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001397 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001398
Steve McKay1f199482015-05-20 15:58:42 -07001399 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001400 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001401 if (destination != null) {
1402 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001403 tmpStack.addAll(curStack);
1404 } else {
1405 tmpStack = curStack;
1406 }
1407
Steve McKay1f199482015-05-20 15:58:42 -07001408 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001409 }
1410
1411 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1412 Context context = getActivity();
1413 final ContentResolver resolver = context.getContentResolver();
1414 ClipData clipData = null;
1415 for (DocumentInfo doc : docs) {
1416 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1417 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001418 // TODO: figure out what this string should be.
1419 // Currently it is not displayed anywhere in the UI, but this might change.
1420 final String label = "";
1421 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001422 } else {
1423 // TODO: update list of mime types in ClipData.
1424 clipData.addItem(new ClipData.Item(uri));
1425 }
1426 }
1427 return clipData;
1428 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001429
Steve McKay1f199482015-05-20 15:58:42 -07001430 void copySelectedToClipboard() {
Steve McKayef280152015-06-11 10:10:49 -07001431 Selection sel = mSelectionManager.getSelection(new Selection());
1432 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001433 }
Steve McKay0599a442015-05-05 14:50:00 -07001434
Steve McKayef280152015-06-11 10:10:49 -07001435 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001436 new GetDocumentsTask() {
1437 @Override
1438 void onDocumentsReady(List<DocumentInfo> docs) {
1439 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001440 Activity activity = getActivity();
1441 Toast.makeText(activity,
1442 activity.getResources().getQuantityString(
1443 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1444 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001445 }
Steve McKayef280152015-06-11 10:10:49 -07001446 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001447 }
1448
1449 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001450 copyFromClipboard();
1451 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001452 }
1453
Steve McKay0599a442015-05-05 14:50:00 -07001454 /**
1455 * Returns true if the list of files can be copied to destination. Note that this
1456 * is a policy check only. Currently the method does not attempt to verify
1457 * available space or any other environmental aspects possibly resulting in
1458 * failure to copy.
1459 *
1460 * @return true if the list of files can be copied to destination.
1461 */
1462 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1463 BaseActivity activity = (BaseActivity)getActivity();
1464
1465 final RootInfo root = activity.getCurrentRoot();
1466
1467 // Can't copy folders to Downloads.
1468 if (root.isDownloads()) {
1469 for (DocumentInfo docs : files) {
1470 if (docs.isDirectory()) {
1471 return false;
1472 }
1473 }
1474 }
1475
1476 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1477 }
1478
1479 void selectAllFiles() {
Steve McKay9459a7c2015-07-24 13:14:20 -07001480 boolean changed = mSelectionManager.setItemsSelected(0, mAdapter.getItemCount(), true);
1481 if (changed) {
1482 updateDisplayState();
1483 }
Steve McKay0599a442015-05-05 14:50:00 -07001484 }
1485
Steve McKayef280152015-06-11 10:10:49 -07001486 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001487 // Listen for drops on non-directory items and empty space.
1488 view.setOnDragListener(mOnDragListener);
1489 }
1490
1491 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1492 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1493 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1494 // Make a directory item a drop target. Drop on non-directories and empty space
1495 // is handled at the list/grid view level.
1496 view.setOnDragListener(mOnDragListener);
1497 }
1498
1499 // Temporary: attaching the listener to the title only.
1500 // Attaching to the entire item conflicts with the item long click handler responsible
1501 // for item selection.
1502 final View title = view.findViewById(android.R.id.title);
1503 title.setOnLongClickListener(mLongClickListener);
1504 }
1505
1506 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1507 @Override
1508 public boolean onDrag(View v, DragEvent event) {
1509 switch (event.getAction()) {
1510 case DragEvent.ACTION_DRAG_STARTED:
1511 // TODO: Check if the event contains droppable data.
1512 return true;
1513
1514 // TODO: Highlight potential drop target directory?
1515 // TODO: Expand drop target directory on hover?
1516 case DragEvent.ACTION_DRAG_ENTERED:
1517 case DragEvent.ACTION_DRAG_LOCATION:
1518 case DragEvent.ACTION_DRAG_EXITED:
1519 case DragEvent.ACTION_DRAG_ENDED:
1520 return true;
1521
1522 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001523 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001524 DocumentInfo dstDir = null;
1525 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1526 Cursor dstCursor = mAdapter.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001527 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001528 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1529 // TODO: Do not drop into the directory where the documents came from.
1530 }
1531 copyFromClipData(event.getClipData(), dstDir);
1532 return true;
1533 }
1534 return false;
1535 }
1536 };
1537
1538 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1539 @Override
1540 public boolean onLongClick(View v) {
1541 final List<DocumentInfo> docs = getDraggableDocuments(v);
1542 if (docs.isEmpty()) {
1543 return false;
1544 }
1545 v.startDrag(
1546 getClipDataFromDocuments(docs),
1547 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1548 null,
1549 View.DRAG_FLAG_GLOBAL
1550 );
1551 return true;
1552 }
1553 };
1554
1555 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001556 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001557 if (position == android.widget.AdapterView.INVALID_POSITION) {
1558 return Collections.EMPTY_LIST;
1559 }
1560
1561 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1562 if (!selectedDocs.isEmpty()) {
Steve McKayef280152015-06-11 10:10:49 -07001563 if (!mSelectionManager.getSelection().contains(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001564 // There is a selection that does not include the current item, drag nothing.
1565 return Collections.EMPTY_LIST;
1566 }
1567 return selectedDocs;
1568 }
1569
1570 final Cursor cursor = mAdapter.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001571 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001572 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1573 return Lists.newArrayList(doc);
1574 }
1575
1576 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1577 if (docs.size() == 1) {
1578 final DocumentInfo doc = docs.get(0);
1579 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1580 doc.mimeType, doc.icon, getDisplayState(this));
1581 }
1582 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1583 }
1584
1585 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1586 String docMimeType, int docIcon, State state) {
1587 if (docIcon != 0) {
1588 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1589 } else {
1590 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1591 state.derivedMode);
1592 }
1593 }
1594
Steve McKayef280152015-06-11 10:10:49 -07001595 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1596 implements Preemptable {
1597 private final Uri mUri;
1598 private final ImageView mIconMime;
1599 private final ImageView mIconThumb;
1600 private final Point mThumbSize;
1601 private final float mTargetAlpha;
1602 private final CancellationSignal mSignal;
1603
1604 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1605 float targetAlpha) {
1606 mUri = uri;
1607 mIconMime = iconMime;
1608 mIconThumb = iconThumb;
1609 mThumbSize = thumbSize;
1610 mTargetAlpha = targetAlpha;
1611 mSignal = new CancellationSignal();
1612 }
1613
1614 @Override
1615 public void preempt() {
1616 cancel(false);
1617 mSignal.cancel();
1618 }
1619
1620 @Override
1621 protected Bitmap doInBackground(Uri... params) {
1622 if (isCancelled()) return null;
1623
1624 final Context context = mIconThumb.getContext();
1625 final ContentResolver resolver = context.getContentResolver();
1626
1627 ContentProviderClient client = null;
1628 Bitmap result = null;
1629 try {
1630 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1631 resolver, mUri.getAuthority());
1632 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1633 if (result != null) {
1634 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1635 context, mThumbSize);
1636 thumbs.put(mUri, result);
1637 }
1638 } catch (Exception e) {
1639 if (!(e instanceof OperationCanceledException)) {
1640 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1641 }
1642 } finally {
1643 ContentProviderClient.releaseQuietly(client);
1644 }
1645 return result;
1646 }
1647
1648 @Override
1649 protected void onPostExecute(Bitmap result) {
1650 if (mIconThumb.getTag() == this && result != null) {
1651 mIconThumb.setTag(null);
1652 mIconThumb.setImageBitmap(result);
1653
1654 mIconMime.setAlpha(mTargetAlpha);
1655 mIconMime.animate().alpha(0f).start();
1656 mIconThumb.setAlpha(0f);
1657 mIconThumb.animate().alpha(mTargetAlpha).start();
1658 }
1659 }
1660 }
1661
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001662 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1663
1664 private final Drawable mShadow;
1665
1666 private final int mShadowDimension;
1667
1668 public DrawableShadowBuilder(Drawable shadow) {
1669 mShadow = shadow;
1670 mShadowDimension = getResources().getDimensionPixelSize(
1671 R.dimen.drag_shadow_size);
1672 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1673 }
1674
1675 public void onProvideShadowMetrics(
1676 Point shadowSize, Point shadowTouchPoint) {
1677 shadowSize.set(mShadowDimension, mShadowDimension);
1678 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1679 }
1680
1681 public void onDrawShadow(Canvas canvas) {
1682 mShadow.draw(canvas);
1683 }
1684 }
Steve McKay1f199482015-05-20 15:58:42 -07001685
1686 private FragmentTuner pickFragmentTuner(final State state) {
1687 return state.action == ACTION_BROWSE_ALL
1688 ? new StandaloneTuner()
1689 : new DefaultTuner(state);
1690 }
1691
1692 /**
1693 * Interface for specializing the Fragment for the "host" Activity.
1694 * Feel free to expand the role of this class to handle other specializations.
1695 */
1696 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001697 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001698 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001699 }
1700
1701 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001702 * Abstract task providing support for loading documents *off*
1703 * the main thread. And if it isn't obvious, creating a list
1704 * of documents (especially large lists) can be pretty expensive.
1705 */
1706 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001707 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001708 @Override
Steve McKayef280152015-06-11 10:10:49 -07001709 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001710 return getItemsAsDocuments(selected[0]);
1711 }
1712
1713 @Override
1714 protected final void onPostExecute(List<DocumentInfo> docs) {
1715 onDocumentsReady(docs);
1716 }
1717
1718 abstract void onDocumentsReady(List<DocumentInfo> docs);
1719 }
1720
1721 /**
Steve McKay1f199482015-05-20 15:58:42 -07001722 * Provides support for Platform specific specializations of DirectoryFragment.
1723 */
1724 private static final class DefaultTuner implements FragmentTuner {
1725
1726 private final State mState;
1727
1728 public DefaultTuner(State state) {
1729 mState = state;
1730 }
1731
1732 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001733 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001734 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1735
1736 final MenuItem open = menu.findItem(R.id.menu_open);
1737 final MenuItem share = menu.findItem(R.id.menu_share);
1738 final MenuItem delete = menu.findItem(R.id.menu_delete);
1739 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1740 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1741 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1742
1743 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1744 || mState.action == ACTION_BROWSE);
1745
1746 open.setVisible(!manageOrBrowse);
1747 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001748 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001749 // Disable copying from the Recents view.
1750 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1751 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1752
1753 // Only shown in standalone mode.
1754 copyToClipboard.setVisible(false);
1755 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001756
1757 @Override
1758 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001759 }
1760
1761 /**
1762 * Provides support for Standalone specific specializations of DirectoryFragment.
1763 */
1764 private static final class StandaloneTuner implements FragmentTuner {
1765 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001766 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001767 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001768 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001769 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1770
1771 menu.findItem(R.id.menu_open).setVisible(false);
1772 menu.findItem(R.id.menu_copy_to).setVisible(false);
1773 menu.findItem(R.id.menu_move_to).setVisible(false);
1774 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001775
1776 @Override
1777 public void afterActivityCreated(DirectoryFragment fragment) {
1778 new BandSelectManager(fragment.mRecView, fragment.mSelectionManager);
1779 }
Steve McKay1f199482015-05-20 15:58:42 -07001780 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001781}