blob: 5223d760e96deb223510218272b538959d7370d0 [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;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070088import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070089import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070090
Steve McKay351a7492015-08-04 10:11:01 -070091import com.android.documentsui.BaseActivity.DocumentContext;
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;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700155 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700156
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700157 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
158 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700159 }
160
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700161 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
162 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700163 }
164
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700165 public static void showRecentsOpen(FragmentManager fm, int anim) {
166 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700167 }
168
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700169 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
170 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700171 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700172 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700173 args.putParcelable(EXTRA_ROOT, root);
174 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700175 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700176
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700177 final FragmentTransaction ft = fm.beginTransaction();
178 switch (anim) {
179 case ANIM_SIDE:
180 args.putBoolean(EXTRA_IGNORE_STATE, true);
181 break;
182 case ANIM_DOWN:
183 args.putBoolean(EXTRA_IGNORE_STATE, true);
184 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
185 break;
186 case ANIM_UP:
187 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
188 break;
189 }
190
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700191 final DirectoryFragment fragment = new DirectoryFragment();
192 fragment.setArguments(args);
193
Jeff Sharkey76112212013-08-06 11:26:10 -0700194 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700195 ft.commitAllowingStateLoss();
196 }
197
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700198 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
199 final StringBuilder builder = new StringBuilder();
200 builder.append(root != null ? root.authority : "null").append(';');
201 builder.append(root != null ? root.rootId : "null").append(';');
202 builder.append(doc != null ? doc.documentId : "null");
203 return builder.toString();
204 }
205
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700206 public static DirectoryFragment get(FragmentManager fm) {
207 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700208 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700209 }
210
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700211 @Override
212 public View onCreateView(
213 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
214 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700215 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700216 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
217
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700218 mEmptyView = view.findViewById(android.R.id.empty);
219
Steve McKayef280152015-06-11 10:10:49 -0700220 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
221 mRecView.setRecyclerListener(
222 new RecyclerListener() {
223 @Override
224 public void onViewRecycled(ViewHolder holder) {
225 cancelThumbnailTask(holder.itemView);
226 }
227 });
Steve McKay8e258c62015-05-06 14:27:57 -0700228
Steve McKayd57f5fa2015-07-23 16:33:41 -0700229 // TODO: Rather than update columns on layout changes, push this
230 // code (or something like it) into GridLayoutManager.
231 mRecView.addOnLayoutChangeListener(
232 new OnLayoutChangeListener() {
233
234 @Override
235 public void onLayoutChange(
236 View v, int left, int top, int right, int bottom, int oldLeft,
237 int oldTop, int oldRight, int oldBottom) {
238 int thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
239 mColumnCount = pickColumnCount(thumbSize);
240 if (mGridLayout != null) {
241 mGridLayout.setSpanCount(mColumnCount);
242 }
243 }
244 });
245
246 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700247 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700248 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700249 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700250
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700251 return view;
252 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700253
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700254 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700255 public void onDestroyView() {
256 super.onDestroyView();
257
258 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700259 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700260 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700261 final View view = mRecView.getChildAt(i);
262 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700263 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700264
Steve McKayef280152015-06-11 10:10:49 -0700265 // Clear any outstanding selection
266 mSelectionManager.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700267 }
268
269 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700270 public void onActivityCreated(Bundle savedInstanceState) {
271 super.onActivityCreated(savedInstanceState);
272
273 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700274 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700275
Jeff Sharkey9656a532013-09-13 13:42:19 -0700276 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
277 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
278
Steve McKayef280152015-06-11 10:10:49 -0700279 mAdapter = new DocumentsAdapter(context);
280 mRecView.setAdapter(mAdapter);
281
282 GestureDetector.SimpleOnGestureListener listener =
283 new GestureDetector.SimpleOnGestureListener() {
284 @Override
285 public boolean onSingleTapUp(MotionEvent e) {
286 return DirectoryFragment.this.onSingleTapUp(e);
287 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700288 @Override
289 public boolean onDoubleTap(MotionEvent e) {
290 Log.d(TAG, "Handling double tap.");
291 return DirectoryFragment.this.onDoubleTap(e);
292 }
Steve McKayef280152015-06-11 10:10:49 -0700293 };
294
Steve McKay57394872015-08-12 14:48:34 -0700295 mSelectionManager = new MultiSelectManager(
296 mRecView,
297 listener,
298 state.allowMultiple
299 ? MultiSelectManager.MODE_MULTIPLE
300 : MultiSelectManager.MODE_SINGLE);
301
Steve McKayef280152015-06-11 10:10:49 -0700302 mSelectionManager.addCallback(new SelectionModeListener());
303
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700304 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700305 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700306
Steve McKay1f199482015-05-20 15:58:42 -0700307 mFragmentTuner = pickFragmentTuner(state);
308 mClipper = new DocumentClipper(context);
309
Jeff Sharkey9656a532013-09-13 13:42:19 -0700310 if (mType == TYPE_RECENT_OPEN) {
311 // Hide titles when showing recents for picking images/videos
312 mHideGridTitles = MimePredicate.mimeMatches(
313 MimePredicate.VISUAL_MIMES, state.acceptMimes);
314 } else {
315 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
316 }
317
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700318 final ActivityManager am = (ActivityManager) context.getSystemService(
319 Context.ACTIVITY_SERVICE);
320 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
321
Jeff Sharkey46899c82013-08-18 22:26:48 -0700322 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700323 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700324 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700325 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700326
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700327 Uri contentsUri;
328 switch (mType) {
329 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700330 contentsUri = DocumentsContract.buildChildDocumentsUri(
331 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700332 if (state.action == ACTION_MANAGE) {
333 contentsUri = DocumentsContract.setManageMode(contentsUri);
334 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700335 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700336 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700337 case TYPE_SEARCH:
338 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700339 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700340 if (state.action == ACTION_MANAGE) {
341 contentsUri = DocumentsContract.setManageMode(contentsUri);
342 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700343 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700344 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700345 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700346 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700347 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700348 default:
349 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700350 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700351 }
352
353 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700354 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700355 if (result == null || result.exception != null) {
356 // onBackPressed does a fragment transaction, which can't be done inside
357 // onLoadFinished
358 mHandler.post(new Runnable() {
359 @Override
360 public void run() {
361 final Activity activity = getActivity();
362 if (activity != null) {
363 activity.onBackPressed();
364 }
365 }
366 });
367 return;
368 }
369
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700370 if (!isAdded()) return;
371
Steve McKayef280152015-06-11 10:10:49 -0700372 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700373
374 // Push latest state up to UI
375 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700376 if (result.mode != MODE_UNKNOWN) {
377 state.derivedMode = result.mode;
378 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700379 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700380 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700381
382 updateDisplayState();
383
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700384 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700385 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
386 context instanceof DocumentsActivity) {
387 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700388 }
389
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700390 // Restore any previous instance state
391 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
392 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
393 getView().restoreHierarchyState(container);
394 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700395 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700396 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700397
398 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700399 }
400
401 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700402 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700403 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700404 }
405 };
406
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700407 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700408 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700409
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700410 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700411 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700412 }
413
Jeff Sharkey42d26792013-09-06 13:22:09 -0700414 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700415 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700416 // There's only one request code right now. Replace this with a switch statement or
417 // something more scalable when more codes are added.
418 if (requestCode != REQUEST_COPY_DESTINATION) {
419 return;
420 }
421 if (resultCode == Activity.RESULT_CANCELED || data == null) {
422 // User pressed the back button or otherwise cancelled the destination pick. Don't
423 // proceed with the copy.
424 return;
425 }
426
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900427 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700428 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
429 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700430 }
431
Steve McKayef280152015-06-11 10:10:49 -0700432 private int getEventAdapterPosition(MotionEvent e) {
433 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
434 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
435 }
436
437 private boolean onSingleTapUp(MotionEvent e) {
Steve McKay57394872015-08-12 14:48:34 -0700438 if (Events.isTouchEvent(e) && mSelectionManager.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700439 int position = getEventAdapterPosition(e);
440 if (position != RecyclerView.NO_POSITION) {
441 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700442 }
443 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700444 return false;
445 }
Steve McKayef280152015-06-11 10:10:49 -0700446
Steve McKay93d8ef42015-07-30 12:27:44 -0700447 protected boolean onDoubleTap(MotionEvent e) {
448 if (Events.isMouseEvent(e)) {
449 Log.d(TAG, "Handling double tap from mouse.");
450 int position = getEventAdapterPosition(e);
451 if (position != RecyclerView.NO_POSITION) {
452 return handleViewItem(position);
453 }
454 }
455 return false;
456 }
457
458 private boolean handleViewItem(int position) {
459 final Cursor cursor = mAdapter.getItem(position);
460 checkNotNull(cursor, "Cursor cannot be null.");
461 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
462 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
463 if (isDocumentEnabled(docMimeType, docFlags)) {
464 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKay351a7492015-08-04 10:11:01 -0700465 ((BaseActivity) getActivity()).onDocumentPicked(doc, mAdapter);
Steve McKay93d8ef42015-07-30 12:27:44 -0700466 mSelectionManager.clearSelection();
467 return true;
468 }
Steve McKayef280152015-06-11 10:10:49 -0700469 return false;
470 }
471
Ben Kwaf5858932015-04-07 15:43:39 -0700472 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700473 public void onStop() {
474 super.onStop();
475
476 // Remember last scroll location
477 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
478 getView().saveHierarchyState(container);
479 final State state = getDisplayState(this);
480 state.dirState.put(mStateKey, container);
481 }
482
483 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700484 public void onResume() {
485 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700486 updateDisplayState();
487 }
488
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700489 public void onDisplayStateChanged() {
490 updateDisplayState();
491 }
492
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700493 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700494 // Sort order change always triggers reload; we'll trigger state change
495 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700496 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700497 }
498
499 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700500 final ContentResolver resolver = getActivity().getContentResolver();
501 final State state = getDisplayState(this);
502
503 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
504 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
505
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700506 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700507 final Uri stateUri = RecentsProvider.buildState(
508 root.authority, root.rootId, doc.documentId);
509 final ContentValues values = new ContentValues();
510 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700511
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700512 new AsyncTask<Void, Void, Void>() {
513 @Override
514 protected Void doInBackground(Void... params) {
515 resolver.insert(stateUri, values);
516 return null;
517 }
518 }.execute();
519 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700520
521 // Mode change is just visual change; no need to kick loader, and
522 // deliver change event immediately.
523 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700524 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700525
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700526 updateDisplayState();
527 }
528
529 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700530 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700531
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700532 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700533 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700534 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700535
Steve McKayef280152015-06-11 10:10:49 -0700536 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700537
Steve McKayef280152015-06-11 10:10:49 -0700538 final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
539 mThumbSize = new Point(thumbSize, thumbSize);
540 mRecView.setAdapter(mAdapter);
541 }
542
543 /**
544 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
545 * classes as needed.
546 */
547 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700548 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700549
550 final LayoutManager layout;
551 switch (mode) {
552 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700553 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700554 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700555 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700556 }
Steve McKayef280152015-06-11 10:10:49 -0700557 layout = mGridLayout;
558 break;
559 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700560 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700561 if (mListLayout == null) {
562 mListLayout = new LinearLayoutManager(getContext());
563 }
Steve McKayef280152015-06-11 10:10:49 -0700564 layout = mListLayout;
565 break;
566 case MODE_UNKNOWN:
567 default:
568 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700569 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700570
Steve McKayef280152015-06-11 10:10:49 -0700571 mRecView.setLayoutManager(layout);
572 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700573 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700574 }
575
Steve McKayd57f5fa2015-07-23 16:33:41 -0700576 private int pickColumnCount(final int thumbSize) {
577 int itemPadding =
578 getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
579 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
580 checkState(mRecView.getWidth() > 0);
581 int columnCount = Math.max(1,
582 (mRecView.getWidth() - viewPadding) / (thumbSize + itemPadding));
583 return columnCount;
584 }
585
Steve McKayef280152015-06-11 10:10:49 -0700586 /**
587 * Manages the integration between our ActionMode and MultiSelectManager, initiating
588 * ActionMode when there is a selection, canceling it when there is no selection,
589 * and clearing selection when action mode is explicitly exited by the user.
590 */
591 private final class SelectionModeListener
592 implements MultiSelectManager.Callback, ActionMode.Callback {
593
594 private Selection mSelected = new Selection();
595 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700596 private int mNoDeleteCount = 0;
597 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700598
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700599 @Override
Steve McKayef280152015-06-11 10:10:49 -0700600 public boolean onBeforeItemStateChange(int position, boolean selected) {
601 // Directories and footer items cannot be checked
602 if (selected) {
603 final Cursor cursor = mAdapter.getItem(position);
604 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700605 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
606 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700607 return isDocumentEnabled(docMimeType, docFlags);
608 }
609 return true;
610 }
611
612 @Override
613 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700614
615 final Cursor cursor = mAdapter.getItem(position);
616 checkNotNull(cursor, "Cursor cannot be null.");
617
618 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
619 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
620 mNoDeleteCount += selected ? 1 : -1;
621 }
Steve McKay57394872015-08-12 14:48:34 -0700622 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700623
Steve McKay57394872015-08-12 14:48:34 -0700624 @Override
625 public void onSelectionChanged() {
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}