blob: 9468cfdc04f13c0114bddb5e4ce0fcbfb631b116 [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKayef280152015-06-11 10:10:49 -070031import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKayd57f5fa2015-07-23 16:33:41 -070032import static com.android.internal.util.Preconditions.checkState;
Steve McKay0599a442015-05-05 14:50:00 -070033
Steve McKay1f199482015-05-20 15:58:42 -070034import android.annotation.NonNull;
Ben Kwaf5858932015-04-07 15:43:39 -070035import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070036import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070037import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.app.FragmentManager;
39import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070041import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070042import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070043import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070044import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070045import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070046import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070048import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070049import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070051import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070052import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070053import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070055import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070056import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070057import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070058import android.os.Handler;
59import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070060import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070061import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070062import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070063import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070064import android.provider.DocumentsContract.Document;
Steve McKayef280152015-06-11 10:10:49 -070065import android.support.v7.widget.GridLayoutManager;
66import android.support.v7.widget.LinearLayoutManager;
67import android.support.v7.widget.RecyclerView;
68import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070069import android.support.v7.widget.RecyclerView.RecyclerListener;
70import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070071import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070073import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070074import android.text.format.Time;
75import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070076import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070077import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070078import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070079import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070082import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070085import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070086import android.view.ViewGroup;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087import android.widget.ImageView;
88import android.widget.ListView;
89import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070090import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070091
Steve McKayd0a2a2c2015-03-25 14:35:33 -070092import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070093import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070094import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070095import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070096import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090097import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070098import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -070099import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -0700100
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101import com.google.android.collect.Lists;
102
103import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700104import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700105import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700106
107/**
108 * Display the documents inside a single directory.
109 */
110public class DirectoryFragment extends Fragment {
111
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700112 public static final int TYPE_NORMAL = 1;
113 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700114 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700115
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700116 public static final int ANIM_NONE = 1;
117 public static final int ANIM_SIDE = 2;
118 public static final int ANIM_DOWN = 3;
119 public static final int ANIM_UP = 4;
120
Ben Kwaf5858932015-04-07 15:43:39 -0700121 public static final int REQUEST_COPY_DESTINATION = 1;
122
Steve McKayef280152015-06-11 10:10:49 -0700123 private static final int LOADER_ID = 42;
124 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700125 private static final boolean DEBUG_ENABLE_DND = false;
126
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 private static final String EXTRA_ROOT = "root";
129 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700130 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700131 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700132
Steve McKayef280152015-06-11 10:10:49 -0700133 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134
Steve McKayef280152015-06-11 10:10:49 -0700135 private View mEmptyView;
136 private RecyclerView mRecView;
137
138 private int mType = TYPE_NORMAL;
139 private String mStateKey;
140
141 private int mLastMode = MODE_UNKNOWN;
142 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
143 private boolean mLastShowSize;
144 private boolean mHideGridTitles;
145 private boolean mSvelteRecents;
146 private Point mThumbSize;
147 private DocumentsAdapter mAdapter;
148 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700149 private FragmentTuner mFragmentTuner;
150 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700151 private MultiSelectManager mSelectionManager;
152 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700153 private LinearLayoutManager mListLayout;
154 private GridLayoutManager mGridLayout;
155 private OnLayoutChangeListener mRecyclerLayoutListener;
156 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700157
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700158 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
159 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700160 }
161
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700162 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
163 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700164 }
165
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700166 public static void showRecentsOpen(FragmentManager fm, int anim) {
167 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700168 }
169
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700170 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
171 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700172 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700173 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700174 args.putParcelable(EXTRA_ROOT, root);
175 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700176 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700178 final FragmentTransaction ft = fm.beginTransaction();
179 switch (anim) {
180 case ANIM_SIDE:
181 args.putBoolean(EXTRA_IGNORE_STATE, true);
182 break;
183 case ANIM_DOWN:
184 args.putBoolean(EXTRA_IGNORE_STATE, true);
185 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
186 break;
187 case ANIM_UP:
188 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
189 break;
190 }
191
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700192 final DirectoryFragment fragment = new DirectoryFragment();
193 fragment.setArguments(args);
194
Jeff Sharkey76112212013-08-06 11:26:10 -0700195 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700196 ft.commitAllowingStateLoss();
197 }
198
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700199 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
200 final StringBuilder builder = new StringBuilder();
201 builder.append(root != null ? root.authority : "null").append(';');
202 builder.append(root != null ? root.rootId : "null").append(';');
203 builder.append(doc != null ? doc.documentId : "null");
204 return builder.toString();
205 }
206
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700207 public static DirectoryFragment get(FragmentManager fm) {
208 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700209 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700210 }
211
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700212 @Override
213 public View onCreateView(
214 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
215 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700216 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
218
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700219 mEmptyView = view.findViewById(android.R.id.empty);
220
Steve McKayef280152015-06-11 10:10:49 -0700221 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
222 mRecView.setRecyclerListener(
223 new RecyclerListener() {
224 @Override
225 public void onViewRecycled(ViewHolder holder) {
226 cancelThumbnailTask(holder.itemView);
227 }
228 });
Steve McKay8e258c62015-05-06 14:27:57 -0700229
Steve McKayd57f5fa2015-07-23 16:33:41 -0700230 // TODO: Rather than update columns on layout changes, push this
231 // code (or something like it) into GridLayoutManager.
232 mRecView.addOnLayoutChangeListener(
233 new OnLayoutChangeListener() {
234
235 @Override
236 public void onLayoutChange(
237 View v, int left, int top, int right, int bottom, int oldLeft,
238 int oldTop, int oldRight, int oldBottom) {
239 int thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
240 mColumnCount = pickColumnCount(thumbSize);
241 if (mGridLayout != null) {
242 mGridLayout.setSpanCount(mColumnCount);
243 }
244 }
245 });
246
247 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700248 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700249 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700250 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700251
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252 return view;
253 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700254
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700256 public void onDestroyView() {
257 super.onDestroyView();
258
259 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700260 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700261 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700262 final View view = mRecView.getChildAt(i);
263 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700265
Steve McKayef280152015-06-11 10:10:49 -0700266 // Clear any outstanding selection
267 mSelectionManager.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700268 }
269
270 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700271 public void onActivityCreated(Bundle savedInstanceState) {
272 super.onActivityCreated(savedInstanceState);
273
274 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700275 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700276
Jeff Sharkey9656a532013-09-13 13:42:19 -0700277 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
278 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
279
Steve McKayef280152015-06-11 10:10:49 -0700280 mAdapter = new DocumentsAdapter(context);
281 mRecView.setAdapter(mAdapter);
282
283 GestureDetector.SimpleOnGestureListener listener =
284 new GestureDetector.SimpleOnGestureListener() {
285 @Override
286 public boolean onSingleTapUp(MotionEvent e) {
287 return DirectoryFragment.this.onSingleTapUp(e);
288 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700289 @Override
290 public boolean onDoubleTap(MotionEvent e) {
291 Log.d(TAG, "Handling double tap.");
292 return DirectoryFragment.this.onDoubleTap(e);
293 }
Steve McKayef280152015-06-11 10:10:49 -0700294 };
295
296 mSelectionManager = new MultiSelectManager(mRecView, listener);
297 mSelectionManager.addCallback(new SelectionModeListener());
298
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700299 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700300 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700301
Steve McKay1f199482015-05-20 15:58:42 -0700302 mFragmentTuner = pickFragmentTuner(state);
303 mClipper = new DocumentClipper(context);
304
Jeff Sharkey9656a532013-09-13 13:42:19 -0700305 if (mType == TYPE_RECENT_OPEN) {
306 // Hide titles when showing recents for picking images/videos
307 mHideGridTitles = MimePredicate.mimeMatches(
308 MimePredicate.VISUAL_MIMES, state.acceptMimes);
309 } else {
310 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
311 }
312
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700313 final ActivityManager am = (ActivityManager) context.getSystemService(
314 Context.ACTIVITY_SERVICE);
315 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
316
Jeff Sharkey46899c82013-08-18 22:26:48 -0700317 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700318 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700319 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700320 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700321
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700322 Uri contentsUri;
323 switch (mType) {
324 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700325 contentsUri = DocumentsContract.buildChildDocumentsUri(
326 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700327 if (state.action == ACTION_MANAGE) {
328 contentsUri = DocumentsContract.setManageMode(contentsUri);
329 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700330 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700331 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700332 case TYPE_SEARCH:
333 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700334 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700335 if (state.action == ACTION_MANAGE) {
336 contentsUri = DocumentsContract.setManageMode(contentsUri);
337 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700338 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700339 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700340 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700341 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700342 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700343 default:
344 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700345 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700346 }
347
348 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700349 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700350 if (result == null || result.exception != null) {
351 // onBackPressed does a fragment transaction, which can't be done inside
352 // onLoadFinished
353 mHandler.post(new Runnable() {
354 @Override
355 public void run() {
356 final Activity activity = getActivity();
357 if (activity != null) {
358 activity.onBackPressed();
359 }
360 }
361 });
362 return;
363 }
364
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700365 if (!isAdded()) return;
366
Steve McKayef280152015-06-11 10:10:49 -0700367 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700368
369 // Push latest state up to UI
370 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700371 if (result.mode != MODE_UNKNOWN) {
372 state.derivedMode = result.mode;
373 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700374 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700375 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700376
377 updateDisplayState();
378
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700379 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700380 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
381 context instanceof DocumentsActivity) {
382 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700383 }
384
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700385 // Restore any previous instance state
386 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
387 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
388 getView().restoreHierarchyState(container);
389 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700390 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700391 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700392
393 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700394 }
395
396 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700397 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700398 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700399 }
400 };
401
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700402 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700403 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700404
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700405 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700406 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700407 }
408
Jeff Sharkey42d26792013-09-06 13:22:09 -0700409 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700410 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700411 // There's only one request code right now. Replace this with a switch statement or
412 // something more scalable when more codes are added.
413 if (requestCode != REQUEST_COPY_DESTINATION) {
414 return;
415 }
416 if (resultCode == Activity.RESULT_CANCELED || data == null) {
417 // User pressed the back button or otherwise cancelled the destination pick. Don't
418 // proceed with the copy.
419 return;
420 }
421
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900422 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700423 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
424 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700425 }
426
Steve McKayef280152015-06-11 10:10:49 -0700427 private int getEventAdapterPosition(MotionEvent e) {
428 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
429 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
430 }
431
432 private boolean onSingleTapUp(MotionEvent e) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700433 if (!Events.isMouseEvent(e)) {
434 int position = getEventAdapterPosition(e);
435 if (position != RecyclerView.NO_POSITION) {
436 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700437 }
438 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700439 return false;
440 }
Steve McKayef280152015-06-11 10:10:49 -0700441
Steve McKay93d8ef42015-07-30 12:27:44 -0700442 protected boolean onDoubleTap(MotionEvent e) {
443 if (Events.isMouseEvent(e)) {
444 Log.d(TAG, "Handling double tap from mouse.");
445 int position = getEventAdapterPosition(e);
446 if (position != RecyclerView.NO_POSITION) {
447 return handleViewItem(position);
448 }
449 }
450 return false;
451 }
452
453 private boolean handleViewItem(int position) {
454 final Cursor cursor = mAdapter.getItem(position);
455 checkNotNull(cursor, "Cursor cannot be null.");
456 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
457 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
458 if (isDocumentEnabled(docMimeType, docFlags)) {
459 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
460 ((BaseActivity) getActivity()).onDocumentPicked(doc);
461 mSelectionManager.clearSelection();
462 return true;
463 }
Steve McKayef280152015-06-11 10:10:49 -0700464 return false;
465 }
466
Ben Kwaf5858932015-04-07 15:43:39 -0700467 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700468 public void onStop() {
469 super.onStop();
470
471 // Remember last scroll location
472 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
473 getView().saveHierarchyState(container);
474 final State state = getDisplayState(this);
475 state.dirState.put(mStateKey, container);
476 }
477
478 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700479 public void onResume() {
480 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700481 updateDisplayState();
482 }
483
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700484 public void onDisplayStateChanged() {
485 updateDisplayState();
486 }
487
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700488 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700489 // Sort order change always triggers reload; we'll trigger state change
490 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700491 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700492 }
493
494 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700495 final ContentResolver resolver = getActivity().getContentResolver();
496 final State state = getDisplayState(this);
497
498 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
499 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
500
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700501 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700502 final Uri stateUri = RecentsProvider.buildState(
503 root.authority, root.rootId, doc.documentId);
504 final ContentValues values = new ContentValues();
505 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700506
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700507 new AsyncTask<Void, Void, Void>() {
508 @Override
509 protected Void doInBackground(Void... params) {
510 resolver.insert(stateUri, values);
511 return null;
512 }
513 }.execute();
514 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700515
516 // Mode change is just visual change; no need to kick loader, and
517 // deliver change event immediately.
518 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700519 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700520
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700521 updateDisplayState();
522 }
523
524 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700525 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700526
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700527 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700528 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700529 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700530
Steve McKayef280152015-06-11 10:10:49 -0700531 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700532
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700533 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700534 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700535 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
536 } else {
537 choiceMode = ListView.CHOICE_MODE_NONE;
538 }
539
Steve McKayef280152015-06-11 10:10:49 -0700540 final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
541 mThumbSize = new Point(thumbSize, thumbSize);
542 mRecView.setAdapter(mAdapter);
543 }
544
545 /**
546 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
547 * classes as needed.
548 */
549 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700550 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700551
552 final LayoutManager layout;
553 switch (mode) {
554 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700555 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700556 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700557 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700558 }
Steve McKayef280152015-06-11 10:10:49 -0700559 layout = mGridLayout;
560 break;
561 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700562 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700563 if (mListLayout == null) {
564 mListLayout = new LinearLayoutManager(getContext());
565 }
Steve McKayef280152015-06-11 10:10:49 -0700566 layout = mListLayout;
567 break;
568 case MODE_UNKNOWN:
569 default:
570 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700571 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700572
Steve McKayef280152015-06-11 10:10:49 -0700573 mRecView.setLayoutManager(layout);
574 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700575 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700576 }
577
Steve McKayd57f5fa2015-07-23 16:33:41 -0700578 private int pickColumnCount(final int thumbSize) {
579 int itemPadding =
580 getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
581 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
582 checkState(mRecView.getWidth() > 0);
583 int columnCount = Math.max(1,
584 (mRecView.getWidth() - viewPadding) / (thumbSize + itemPadding));
585 return columnCount;
586 }
587
Steve McKayef280152015-06-11 10:10:49 -0700588 /**
589 * Manages the integration between our ActionMode and MultiSelectManager, initiating
590 * ActionMode when there is a selection, canceling it when there is no selection,
591 * and clearing selection when action mode is explicitly exited by the user.
592 */
593 private final class SelectionModeListener
594 implements MultiSelectManager.Callback, ActionMode.Callback {
595
596 private Selection mSelected = new Selection();
597 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700598 private int mNoDeleteCount = 0;
599 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700600
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700601 @Override
Steve McKayef280152015-06-11 10:10:49 -0700602 public boolean onBeforeItemStateChange(int position, boolean selected) {
603 // Directories and footer items cannot be checked
604 if (selected) {
605 final Cursor cursor = mAdapter.getItem(position);
606 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700607 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
608 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700609 return isDocumentEnabled(docMimeType, docFlags);
610 }
611 return true;
612 }
613
614 @Override
615 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700616
617 final Cursor cursor = mAdapter.getItem(position);
618 checkNotNull(cursor, "Cursor cannot be null.");
619
620 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
621 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
622 mNoDeleteCount += selected ? 1 : -1;
623 }
624
Steve McKayef280152015-06-11 10:10:49 -0700625 mSelectionManager.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700626 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700627 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
628 if (mActionMode == null) {
629 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
630 mActionMode = getActivity().startActionMode(this);
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900631 getActivity().getWindow().setStatusBarColor(
632 getResources().getColor(R.color.action_mode_status_bar_background));
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700633 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700634 updateActionMenu();
635 } else {
636 if (DEBUG) Log.d(TAG, "Finishing action mode.");
637 if (mActionMode != null) {
638 mActionMode.finish();
639 }
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900640 getActivity().getWindow().setStatusBarColor(
641 getResources().getColor(R.color.status_bar_background));
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700642 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700643
Steve McKayef280152015-06-11 10:10:49 -0700644 if (mActionMode != null) {
645 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
646 }
647 }
648
649 // Called when the user exits the action mode
650 @Override
651 public void onDestroyActionMode(ActionMode mode) {
652 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
653 mActionMode = null;
654 // clear selection
655 mSelectionManager.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700656 mSelected.clear();
657 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700658 }
659
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700660 @Override
661 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
662 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Steve McKayef280152015-06-11 10:10:49 -0700663 mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
664 return mSelectionManager.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700665 }
666
667 @Override
668 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700669 mMenu = menu;
670 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700671 return true;
672 }
673
Steve McKay4f4232d2015-07-22 12:13:46 -0700674 private void updateActionMenu() {
675 checkNotNull(mMenu);
676 // Delegate update logic to our owning action, since specialized logic is desired.
677 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
678 }
679
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700680 @Override
Steve McKayef280152015-06-11 10:10:49 -0700681 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700682
Steve McKayef280152015-06-11 10:10:49 -0700683 Selection selection = new Selection();
684 mSelectionManager.getSelection(selection);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700685
Jeff Sharkey873daa32013-08-18 17:38:20 -0700686 final int id = item.getItemId();
687 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700688 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700689 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700690 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700691
692 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700693 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700694 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700695 return true;
696
697 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700698 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700699 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700700 return true;
701
Steve McKay1f199482015-05-20 15:58:42 -0700702 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700703 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700704 mode.finish();
705 return true;
706
Steve McKay1f199482015-05-20 15:58:42 -0700707 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700708 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700709 mode.finish();
710 return true;
711
Steve McKay1f199482015-05-20 15:58:42 -0700712 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700713 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700714 mode.finish();
715 return true;
716
Ben Kwa512a6ba2015-03-31 08:15:21 -0700717 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700718 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700719 return true;
720
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700721 } else {
722 return false;
723 }
724 }
Steve McKayef280152015-06-11 10:10:49 -0700725 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700726
Steve McKayef280152015-06-11 10:10:49 -0700727 private static void cancelThumbnailTask(View view) {
728 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
729 if (iconThumb != null) {
730 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
731 if (oldTask != null) {
732 oldTask.preempt();
733 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700734 }
735 }
Steve McKayef280152015-06-11 10:10:49 -0700736 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700737
Steve McKayef280152015-06-11 10:10:49 -0700738 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700739 new GetDocumentsTask() {
740 @Override
741 void onDocumentsReady(List<DocumentInfo> docs) {
742 // TODO: Implement support in standalone for opening multiple docs.
743 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700744 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700745 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700746 }
747
Steve McKayef280152015-06-11 10:10:49 -0700748 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700749 new GetDocumentsTask() {
750 @Override
751 void onDocumentsReady(List<DocumentInfo> docs) {
752 Intent intent;
753
754 // Filter out directories - those can't be shared.
755 List<DocumentInfo> docsForSend = Lists.newArrayList();
756 for (DocumentInfo doc: docs) {
757 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
758 docsForSend.add(doc);
759 }
760 }
761
762 if (docsForSend.size() == 1) {
763 final DocumentInfo doc = docsForSend.get(0);
764
765 intent = new Intent(Intent.ACTION_SEND);
766 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
767 intent.addCategory(Intent.CATEGORY_DEFAULT);
768 intent.setType(doc.mimeType);
769 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
770
771 } else if (docsForSend.size() > 1) {
772 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
773 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
774 intent.addCategory(Intent.CATEGORY_DEFAULT);
775
776 final ArrayList<String> mimeTypes = Lists.newArrayList();
777 final ArrayList<Uri> uris = Lists.newArrayList();
778 for (DocumentInfo doc : docsForSend) {
779 mimeTypes.add(doc.mimeType);
780 uris.add(doc.derivedUri);
781 }
782
783 intent.setType(findCommonMimeType(mimeTypes));
784 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
785
786 } else {
787 return;
788 }
789
790 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
791 startActivity(intent);
792 }
793 }.execute(selected);
794 }
795
Steve McKayef280152015-06-11 10:10:49 -0700796 private void deleteDocuments(final Selection selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700797 final Context context = getActivity();
798 final ContentResolver resolver = context.getContentResolver();
799
Steve McKay9276f3b2015-05-27 16:11:42 -0700800 new GetDocumentsTask() {
801 @Override
802 void onDocumentsReady(List<DocumentInfo> docs) {
803 boolean hadTrouble = false;
804 for (DocumentInfo doc : docs) {
805 if (!doc.isDeleteSupported()) {
806 Log.w(TAG, "Skipping " + doc);
807 hadTrouble = true;
808 continue;
809 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700810
Steve McKay9276f3b2015-05-27 16:11:42 -0700811 ContentProviderClient client = null;
812 try {
813 client = DocumentsApplication.acquireUnstableProviderOrThrow(
814 resolver, doc.derivedUri.getAuthority());
815 DocumentsContract.deleteDocument(client, doc.derivedUri);
816 } catch (Exception e) {
817 Log.w(TAG, "Failed to delete " + doc);
818 hadTrouble = true;
819 } finally {
820 ContentProviderClient.releaseQuietly(client);
821 }
822 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700823
Steve McKay9276f3b2015-05-27 16:11:42 -0700824 if (hadTrouble) {
825 Toast.makeText(
826 context,
827 R.string.toast_failed_delete,
828 Toast.LENGTH_SHORT).show();
829 }
830 }
831 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700832 }
833
Steve McKayef280152015-06-11 10:10:49 -0700834 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700835 // Pop up a dialog to pick a destination. This is inadequate but works for now.
836 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900837 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900838 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900839 Uri.EMPTY,
840 getActivity(),
841 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700842
843 new GetDocumentsTask() {
844 @Override
845 void onDocumentsReady(List<DocumentInfo> docs) {
846 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
847
848 boolean directoryCopy = false;
849 for (DocumentInfo info : docs) {
850 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
851 directoryCopy = true;
852 break;
853 }
854 }
855 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
856 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
857 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900858 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700859 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700860 }
861
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700862 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700863 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700864 }
865
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700866 private static abstract class Footer {
867 private final int mItemViewType;
868
869 public Footer(int itemViewType) {
870 mItemViewType = itemViewType;
871 }
872
873 public abstract View getView(View convertView, ViewGroup parent);
874
875 public int getItemViewType() {
876 return mItemViewType;
877 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700878 }
879
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700880 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700881 public LoadingFooter() {
882 super(1);
883 }
884
Jeff Sharkey20b32272013-09-03 15:25:52 -0700885 @Override
886 public View getView(View convertView, ViewGroup parent) {
887 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700888 final State state = getDisplayState(DirectoryFragment.this);
889
Jeff Sharkey20b32272013-09-03 15:25:52 -0700890 if (convertView == null) {
891 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700892 if (state.derivedMode == MODE_LIST) {
893 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
894 } else if (state.derivedMode == MODE_GRID) {
895 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
896 } else {
897 throw new IllegalStateException();
898 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700899 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700900
Jeff Sharkey20b32272013-09-03 15:25:52 -0700901 return convertView;
902 }
903 }
904
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700905 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700906 private final int mIcon;
907 private final String mMessage;
908
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700909 public MessageFooter(int itemViewType, int icon, String message) {
910 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700911 mIcon = icon;
912 mMessage = message;
913 }
914
915 @Override
916 public View getView(View convertView, ViewGroup parent) {
917 final Context context = parent.getContext();
918 final State state = getDisplayState(DirectoryFragment.this);
919
920 if (convertView == null) {
921 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700922 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700923 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700924 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700925 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
926 } else {
927 throw new IllegalStateException();
928 }
929 }
930
931 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
932 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
933 icon.setImageResource(mIcon);
934 title.setText(mMessage);
935 return convertView;
936 }
937 }
938
Steve McKayef280152015-06-11 10:10:49 -0700939 // Provide a reference to the views for each data item
940 // Complex data items may need more than one view per item, and
941 // you provide access to all the views for a data item in a view holder
942 private static final class DocumentHolder extends RecyclerView.ViewHolder {
943 // each data item is just a string in this case
944 public View view;
945 public String docId; // The stable document id.
946 public DocumentHolder(View view) {
947 super(view);
948 this.view = view;
949 }
950 }
951
952 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
953
954 private final Context mContext;
955 private final LayoutInflater mInflater;
956 // TODO: Bring back support for footers.
957 private final List<Footer> mFooters = Lists.newArrayList();
958
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700959 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700960 private int mCursorCount;
961
Steve McKayef280152015-06-11 10:10:49 -0700962 public DocumentsAdapter(Context context) {
963 mContext = context;
964 mInflater = LayoutInflater.from(context);
965 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700966
Steve McKayef280152015-06-11 10:10:49 -0700967 public void replaceResult(DirectoryResult result) {
968 if (DEBUG) Log.i(TAG, "Updating adapter with new result set.");
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700969 mCursor = result != null ? result.cursor : null;
970 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700971
972 mFooters.clear();
973
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700974 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700975 if (extras != null) {
976 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
977 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700978 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700979 }
980 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
981 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700982 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700983 }
984 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
985 mFooters.add(new LoadingFooter());
986 }
987 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700988
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700989 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700990 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700991 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700992 }
993
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700994 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700995 mEmptyView.setVisibility(View.VISIBLE);
996 } else {
997 mEmptyView.setVisibility(View.GONE);
998 }
999
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001000 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001001 }
1002
1003 @Override
Steve McKayef280152015-06-11 10:10:49 -07001004 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
1005 final State state = getDisplayState(DirectoryFragment.this);
1006 final LayoutInflater inflater = LayoutInflater.from(getContext());
1007 switch (state.derivedMode) {
1008 case MODE_GRID:
1009 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
1010 case MODE_LIST:
1011 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
1012 case MODE_UNKNOWN:
1013 default:
1014 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -07001015 }
1016 }
1017
Steve McKayef280152015-06-11 10:10:49 -07001018 @Override
1019 public void onBindViewHolder(DocumentHolder holder, int position) {
1020
1021 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001022 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001023 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -07001024 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1025 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1026 context, mThumbSize);
1027
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001028 final Cursor cursor = getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001029 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001030
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001031 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1032 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001033 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1034 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1035 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1036 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1037 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1038 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1039 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1040 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001041
Steve McKayef280152015-06-11 10:10:49 -07001042 holder.docId = docId;
1043 final View itemView = holder.view;
1044 itemView.setActivated(mSelectionManager.getSelection().contains(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001045
Steve McKayef280152015-06-11 10:10:49 -07001046 final View line1 = itemView.findViewById(R.id.line1);
1047 final View line2 = itemView.findViewById(R.id.line2);
1048
1049 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1050 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1051 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1052 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1053 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1054 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1055 final TextView date = (TextView) itemView.findViewById(R.id.date);
1056 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001057
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001058 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001059 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001060 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001061 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001062 }
1063
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001064 iconMime.animate().cancel();
1065 iconThumb.animate().cancel();
1066
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001067 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001068 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001069 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001070 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001071
Jeff Sharkey7e544612014-08-29 15:38:27 -07001072 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1073 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1074
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001075 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001076 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001077 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001078 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001079 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001080 iconThumb.setImageBitmap(cachedResult);
1081 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001082 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001083 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001084 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001085 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001086 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001087 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001088 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001089 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001090 }
1091
1092 // Always throw MIME icon into place, even when a thumbnail is being
1093 // loaded in background.
1094 if (cacheHit) {
1095 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001096 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001097 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001098 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001099 iconMime.setAlpha(1f);
1100 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001101 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001102 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001103 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001104 }
1105
Jeff Sharkey9656a532013-09-13 13:42:19 -07001106 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001107 boolean hasLine2 = false;
1108
Jeff Sharkey9656a532013-09-13 13:42:19 -07001109 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1110 if (!hideTitle) {
1111 title.setText(docDisplayName);
1112 hasLine1 = true;
1113 }
1114
1115 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001116 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001117 // We've already had to enumerate roots before any results can
1118 // be shown, so this will never block.
1119 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001120 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001121 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001122 } else {
Steve McKayef280152015-06-11 10:10:49 -07001123 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001124 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001125
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001126 if (summary != null) {
1127 final boolean alwaysShowSummary = getResources()
1128 .getBoolean(R.bool.always_show_summary);
1129 if (alwaysShowSummary) {
1130 summary.setText(root.getDirectoryString());
1131 summary.setVisibility(View.VISIBLE);
1132 hasLine2 = true;
1133 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001134 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001135 // No summary needed if icon speaks for itself
1136 summary.setVisibility(View.INVISIBLE);
1137 } else {
1138 summary.setText(root.getDirectoryString());
1139 summary.setVisibility(View.VISIBLE);
1140 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1141 hasLine2 = true;
1142 }
1143 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001144 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001145 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001146 // Directories showing thumbnails in grid mode get a little icon
1147 // hint to remind user they're a directory.
1148 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1149 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001150 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001151 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001152 }
1153
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001154 if (summary != null) {
1155 if (docSummary != null) {
1156 summary.setText(docSummary);
1157 summary.setVisibility(View.VISIBLE);
1158 hasLine2 = true;
1159 } else {
1160 summary.setVisibility(View.INVISIBLE);
1161 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001162 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001163 }
1164
Jeff Sharkey9656a532013-09-13 13:42:19 -07001165 if (icon1 != null) icon1.setVisibility(View.GONE);
1166 if (icon2 != null) icon2.setVisibility(View.GONE);
1167
1168 if (iconDrawable != null) {
1169 if (hasLine1) {
1170 icon1.setVisibility(View.VISIBLE);
1171 icon1.setImageDrawable(iconDrawable);
1172 } else {
1173 icon2.setVisibility(View.VISIBLE);
1174 icon2.setImageDrawable(iconDrawable);
1175 }
1176 }
1177
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001178 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001179 date.setText(null);
1180 } else {
Steve McKayef280152015-06-11 10:10:49 -07001181 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001182 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001183 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001184
1185 if (state.showSize) {
1186 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001187 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001188 size.setText(null);
1189 } else {
Steve McKayef280152015-06-11 10:10:49 -07001190 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001191 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001192 }
1193 } else {
1194 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001195 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001196
Jeff Sharkey9656a532013-09-13 13:42:19 -07001197 if (line1 != null) {
1198 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1199 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001200 if (line2 != null) {
1201 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1202 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001203
Steve McKayef280152015-06-11 10:10:49 -07001204 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001205
1206 iconMime.setAlpha(iconAlpha);
1207 iconThumb.setAlpha(iconAlpha);
1208 if (icon1 != null) icon1.setAlpha(iconAlpha);
1209 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001210
Steve McKay8e258c62015-05-06 14:27:57 -07001211 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001212 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001213 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001214 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001215
Steve McKayef280152015-06-11 10:10:49 -07001216 private Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001217 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001218 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001219 return mCursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001220 }
Steve McKayef280152015-06-11 10:10:49 -07001221
1222 Log.w(TAG, "Returning null cursor for position: " + position);
1223 if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
1224 if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
1225 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001226 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001227
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001228 @Override
Steve McKayef280152015-06-11 10:10:49 -07001229 public int getItemCount() {
1230 return mCursorCount;
1231 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001232 }
1233
1234 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001235 public int getItemViewType(int position) {
1236 if (position < mCursorCount) {
1237 return 0;
1238 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001239 position -= mCursorCount;
1240 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001241 }
1242 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001243
Steve McKayef280152015-06-11 10:10:49 -07001244 private boolean isEmpty() {
1245 return getItemCount() > 0;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001246 }
1247 }
1248
1249 private static String formatTime(Context context, long when) {
1250 // TODO: DateUtils should make this easier
1251 Time then = new Time();
1252 then.set(when);
1253 Time now = new Time();
1254 now.setToNow();
1255
1256 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1257 | DateUtils.FORMAT_ABBREV_ALL;
1258
1259 if (then.year != now.year) {
1260 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1261 } else if (then.yearDay != now.yearDay) {
1262 flags |= DateUtils.FORMAT_SHOW_DATE;
1263 } else {
1264 flags |= DateUtils.FORMAT_SHOW_TIME;
1265 }
1266
1267 return DateUtils.formatDateTime(context, when, flags);
1268 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001269
1270 private String findCommonMimeType(List<String> mimeTypes) {
1271 String[] commonType = mimeTypes.get(0).split("/");
1272 if (commonType.length != 2) {
1273 return "*/*";
1274 }
1275
1276 for (int i = 1; i < mimeTypes.size(); i++) {
1277 String[] type = mimeTypes.get(i).split("/");
1278 if (type.length != 2) continue;
1279
1280 if (!commonType[1].equals(type[1])) {
1281 commonType[1] = "*";
1282 }
1283
1284 if (!commonType[0].equals(type[0])) {
1285 commonType[0] = "*";
1286 commonType[1] = "*";
1287 break;
1288 }
1289 }
1290
1291 return commonType[0] + "/" + commonType[1];
1292 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001293
1294 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001295 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001296 if (v.isEnabled() == enabled) return;
1297 v.setEnabled(enabled);
1298
1299 if (v instanceof ViewGroup) {
1300 final ViewGroup vg = (ViewGroup) v;
1301 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1302 setEnabledRecursive(vg.getChildAt(i), enabled);
1303 }
1304 }
1305 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001306
1307 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1308 final State state = getDisplayState(DirectoryFragment.this);
1309
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001310 // Directories are always enabled
1311 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1312 return true;
1313 }
1314
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001315 // Read-only files are disabled when creating
1316 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1317 return false;
1318 }
1319
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001320 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1321 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001322
Steve McKay1f199482015-05-20 15:58:42 -07001323 private @NonNull List<DocumentInfo> getSelectedDocuments() {
Steve McKayef280152015-06-11 10:10:49 -07001324 Selection sel = mSelectionManager.getSelection(new Selection());
1325 return getItemsAsDocuments(sel);
Steve McKay1f199482015-05-20 15:58:42 -07001326 }
1327
Steve McKayef280152015-06-11 10:10:49 -07001328 private List<DocumentInfo> getItemsAsDocuments(Selection items) {
Steve McKay1f199482015-05-20 15:58:42 -07001329 if (items == null || items.size() == 0) {
1330 return new ArrayList<>(0);
1331 }
1332
1333 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1334 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001335 for (int i = 0; i < size; i++) {
Steve McKayef280152015-06-11 10:10:49 -07001336 final Cursor cursor = mAdapter.getItem(items.get(i));
1337 checkNotNull(cursor, "Cursor cannot be null.");
1338 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1339 docs.add(doc);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001340 }
1341 return docs;
1342 }
1343
Steve McKay1f199482015-05-20 15:58:42 -07001344 private void copyFromClipboard() {
1345 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1346
1347 @Override
1348 protected List<DocumentInfo> doInBackground(Void... params) {
1349 return mClipper.getClippedDocuments();
1350 }
1351
1352 @Override
1353 protected void onPostExecute(List<DocumentInfo> docs) {
1354 DocumentInfo destination =
1355 ((BaseActivity) getActivity()).getCurrentDirectory();
1356 copyDocuments(docs, destination);
1357 }
1358 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001359 }
1360
Steve McKay1f199482015-05-20 15:58:42 -07001361 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001362 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001363 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001364
Steve McKay1f199482015-05-20 15:58:42 -07001365 @Override
1366 protected List<DocumentInfo> doInBackground(Void... params) {
1367 return mClipper.getDocumentsFromClipData(clipData);
1368 }
1369
1370 @Override
1371 protected void onPostExecute(List<DocumentInfo> docs) {
1372 copyDocuments(docs, destination);
1373 }
1374 }.execute();
1375 }
1376
1377 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1378 if (!canCopy(docs, destination)) {
1379 Toast.makeText(
1380 getActivity(),
1381 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001382 return;
1383 }
1384
Steve McKay1f199482015-05-20 15:58:42 -07001385 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001386 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001387 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001388
Steve McKay1f199482015-05-20 15:58:42 -07001389 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001390 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001391 if (destination != null) {
1392 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001393 tmpStack.addAll(curStack);
1394 } else {
1395 tmpStack = curStack;
1396 }
1397
Steve McKay1f199482015-05-20 15:58:42 -07001398 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001399 }
1400
1401 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1402 Context context = getActivity();
1403 final ContentResolver resolver = context.getContentResolver();
1404 ClipData clipData = null;
1405 for (DocumentInfo doc : docs) {
1406 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1407 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001408 // TODO: figure out what this string should be.
1409 // Currently it is not displayed anywhere in the UI, but this might change.
1410 final String label = "";
1411 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001412 } else {
1413 // TODO: update list of mime types in ClipData.
1414 clipData.addItem(new ClipData.Item(uri));
1415 }
1416 }
1417 return clipData;
1418 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001419
Steve McKay1f199482015-05-20 15:58:42 -07001420 void copySelectedToClipboard() {
Steve McKayef280152015-06-11 10:10:49 -07001421 Selection sel = mSelectionManager.getSelection(new Selection());
1422 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001423 }
Steve McKay0599a442015-05-05 14:50:00 -07001424
Steve McKayef280152015-06-11 10:10:49 -07001425 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001426 new GetDocumentsTask() {
1427 @Override
1428 void onDocumentsReady(List<DocumentInfo> docs) {
1429 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001430 Activity activity = getActivity();
1431 Toast.makeText(activity,
1432 activity.getResources().getQuantityString(
1433 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1434 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001435 }
Steve McKayef280152015-06-11 10:10:49 -07001436 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001437 }
1438
1439 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001440 copyFromClipboard();
1441 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001442 }
1443
Steve McKay0599a442015-05-05 14:50:00 -07001444 /**
1445 * Returns true if the list of files can be copied to destination. Note that this
1446 * is a policy check only. Currently the method does not attempt to verify
1447 * available space or any other environmental aspects possibly resulting in
1448 * failure to copy.
1449 *
1450 * @return true if the list of files can be copied to destination.
1451 */
1452 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1453 BaseActivity activity = (BaseActivity)getActivity();
1454
1455 final RootInfo root = activity.getCurrentRoot();
1456
1457 // Can't copy folders to Downloads.
1458 if (root.isDownloads()) {
1459 for (DocumentInfo docs : files) {
1460 if (docs.isDirectory()) {
1461 return false;
1462 }
1463 }
1464 }
1465
1466 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1467 }
1468
1469 void selectAllFiles() {
Steve McKay9459a7c2015-07-24 13:14:20 -07001470 boolean changed = mSelectionManager.setItemsSelected(0, mAdapter.getItemCount(), true);
1471 if (changed) {
1472 updateDisplayState();
1473 }
Steve McKay0599a442015-05-05 14:50:00 -07001474 }
1475
Steve McKayef280152015-06-11 10:10:49 -07001476 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001477 // Listen for drops on non-directory items and empty space.
1478 view.setOnDragListener(mOnDragListener);
1479 }
1480
1481 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1482 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1483 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1484 // Make a directory item a drop target. Drop on non-directories and empty space
1485 // is handled at the list/grid view level.
1486 view.setOnDragListener(mOnDragListener);
1487 }
1488
1489 // Temporary: attaching the listener to the title only.
1490 // Attaching to the entire item conflicts with the item long click handler responsible
1491 // for item selection.
1492 final View title = view.findViewById(android.R.id.title);
1493 title.setOnLongClickListener(mLongClickListener);
1494 }
1495
1496 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1497 @Override
1498 public boolean onDrag(View v, DragEvent event) {
1499 switch (event.getAction()) {
1500 case DragEvent.ACTION_DRAG_STARTED:
1501 // TODO: Check if the event contains droppable data.
1502 return true;
1503
1504 // TODO: Highlight potential drop target directory?
1505 // TODO: Expand drop target directory on hover?
1506 case DragEvent.ACTION_DRAG_ENTERED:
1507 case DragEvent.ACTION_DRAG_LOCATION:
1508 case DragEvent.ACTION_DRAG_EXITED:
1509 case DragEvent.ACTION_DRAG_ENDED:
1510 return true;
1511
1512 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001513 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001514 DocumentInfo dstDir = null;
1515 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1516 Cursor dstCursor = mAdapter.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001517 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001518 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1519 // TODO: Do not drop into the directory where the documents came from.
1520 }
1521 copyFromClipData(event.getClipData(), dstDir);
1522 return true;
1523 }
1524 return false;
1525 }
1526 };
1527
1528 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1529 @Override
1530 public boolean onLongClick(View v) {
1531 final List<DocumentInfo> docs = getDraggableDocuments(v);
1532 if (docs.isEmpty()) {
1533 return false;
1534 }
1535 v.startDrag(
1536 getClipDataFromDocuments(docs),
1537 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1538 null,
1539 View.DRAG_FLAG_GLOBAL
1540 );
1541 return true;
1542 }
1543 };
1544
1545 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001546 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001547 if (position == android.widget.AdapterView.INVALID_POSITION) {
1548 return Collections.EMPTY_LIST;
1549 }
1550
1551 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1552 if (!selectedDocs.isEmpty()) {
Steve McKayef280152015-06-11 10:10:49 -07001553 if (!mSelectionManager.getSelection().contains(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001554 // There is a selection that does not include the current item, drag nothing.
1555 return Collections.EMPTY_LIST;
1556 }
1557 return selectedDocs;
1558 }
1559
1560 final Cursor cursor = mAdapter.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001561 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001562 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1563 return Lists.newArrayList(doc);
1564 }
1565
1566 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1567 if (docs.size() == 1) {
1568 final DocumentInfo doc = docs.get(0);
1569 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1570 doc.mimeType, doc.icon, getDisplayState(this));
1571 }
1572 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1573 }
1574
1575 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1576 String docMimeType, int docIcon, State state) {
1577 if (docIcon != 0) {
1578 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1579 } else {
1580 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1581 state.derivedMode);
1582 }
1583 }
1584
Steve McKayef280152015-06-11 10:10:49 -07001585 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1586 implements Preemptable {
1587 private final Uri mUri;
1588 private final ImageView mIconMime;
1589 private final ImageView mIconThumb;
1590 private final Point mThumbSize;
1591 private final float mTargetAlpha;
1592 private final CancellationSignal mSignal;
1593
1594 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1595 float targetAlpha) {
1596 mUri = uri;
1597 mIconMime = iconMime;
1598 mIconThumb = iconThumb;
1599 mThumbSize = thumbSize;
1600 mTargetAlpha = targetAlpha;
1601 mSignal = new CancellationSignal();
1602 }
1603
1604 @Override
1605 public void preempt() {
1606 cancel(false);
1607 mSignal.cancel();
1608 }
1609
1610 @Override
1611 protected Bitmap doInBackground(Uri... params) {
1612 if (isCancelled()) return null;
1613
1614 final Context context = mIconThumb.getContext();
1615 final ContentResolver resolver = context.getContentResolver();
1616
1617 ContentProviderClient client = null;
1618 Bitmap result = null;
1619 try {
1620 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1621 resolver, mUri.getAuthority());
1622 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1623 if (result != null) {
1624 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1625 context, mThumbSize);
1626 thumbs.put(mUri, result);
1627 }
1628 } catch (Exception e) {
1629 if (!(e instanceof OperationCanceledException)) {
1630 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1631 }
1632 } finally {
1633 ContentProviderClient.releaseQuietly(client);
1634 }
1635 return result;
1636 }
1637
1638 @Override
1639 protected void onPostExecute(Bitmap result) {
1640 if (mIconThumb.getTag() == this && result != null) {
1641 mIconThumb.setTag(null);
1642 mIconThumb.setImageBitmap(result);
1643
1644 mIconMime.setAlpha(mTargetAlpha);
1645 mIconMime.animate().alpha(0f).start();
1646 mIconThumb.setAlpha(0f);
1647 mIconThumb.animate().alpha(mTargetAlpha).start();
1648 }
1649 }
1650 }
1651
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001652 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1653
1654 private final Drawable mShadow;
1655
1656 private final int mShadowDimension;
1657
1658 public DrawableShadowBuilder(Drawable shadow) {
1659 mShadow = shadow;
1660 mShadowDimension = getResources().getDimensionPixelSize(
1661 R.dimen.drag_shadow_size);
1662 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1663 }
1664
1665 public void onProvideShadowMetrics(
1666 Point shadowSize, Point shadowTouchPoint) {
1667 shadowSize.set(mShadowDimension, mShadowDimension);
1668 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1669 }
1670
1671 public void onDrawShadow(Canvas canvas) {
1672 mShadow.draw(canvas);
1673 }
1674 }
Steve McKay1f199482015-05-20 15:58:42 -07001675
1676 private FragmentTuner pickFragmentTuner(final State state) {
1677 return state.action == ACTION_BROWSE_ALL
1678 ? new StandaloneTuner()
1679 : new DefaultTuner(state);
1680 }
1681
1682 /**
1683 * Interface for specializing the Fragment for the "host" Activity.
1684 * Feel free to expand the role of this class to handle other specializations.
1685 */
1686 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001687 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001688 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001689 }
1690
1691 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001692 * Abstract task providing support for loading documents *off*
1693 * the main thread. And if it isn't obvious, creating a list
1694 * of documents (especially large lists) can be pretty expensive.
1695 */
1696 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001697 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001698 @Override
Steve McKayef280152015-06-11 10:10:49 -07001699 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001700 return getItemsAsDocuments(selected[0]);
1701 }
1702
1703 @Override
1704 protected final void onPostExecute(List<DocumentInfo> docs) {
1705 onDocumentsReady(docs);
1706 }
1707
1708 abstract void onDocumentsReady(List<DocumentInfo> docs);
1709 }
1710
1711 /**
Steve McKay1f199482015-05-20 15:58:42 -07001712 * Provides support for Platform specific specializations of DirectoryFragment.
1713 */
1714 private static final class DefaultTuner implements FragmentTuner {
1715
1716 private final State mState;
1717
1718 public DefaultTuner(State state) {
1719 mState = state;
1720 }
1721
1722 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001723 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001724 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1725
1726 final MenuItem open = menu.findItem(R.id.menu_open);
1727 final MenuItem share = menu.findItem(R.id.menu_share);
1728 final MenuItem delete = menu.findItem(R.id.menu_delete);
1729 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1730 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1731 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1732
1733 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1734 || mState.action == ACTION_BROWSE);
1735
1736 open.setVisible(!manageOrBrowse);
1737 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001738 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001739 // Disable copying from the Recents view.
1740 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1741 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1742
1743 // Only shown in standalone mode.
1744 copyToClipboard.setVisible(false);
1745 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001746
1747 @Override
1748 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001749 }
1750
1751 /**
1752 * Provides support for Standalone specific specializations of DirectoryFragment.
1753 */
1754 private static final class StandaloneTuner implements FragmentTuner {
1755 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001756 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001757 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001758 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001759 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1760
1761 menu.findItem(R.id.menu_open).setVisible(false);
1762 menu.findItem(R.id.menu_copy_to).setVisible(false);
1763 menu.findItem(R.id.menu_move_to).setVisible(false);
1764 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001765
1766 @Override
1767 public void afterActivityCreated(DirectoryFragment fragment) {
1768 new BandSelectManager(fragment.mRecView, fragment.mSelectionManager);
1769 }
Steve McKay1f199482015-05-20 15:58:42 -07001770 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001771}