blob: 308375e739173b2d2f9776e6a6c248bab3b5090a [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKayef280152015-06-11 10:10:49 -070031import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKayd57f5fa2015-07-23 16:33:41 -070032import static com.android.internal.util.Preconditions.checkState;
Steve McKay0599a442015-05-05 14:50:00 -070033
Steve McKay1f199482015-05-20 15:58:42 -070034import android.annotation.NonNull;
Ben Kwaf5858932015-04-07 15:43:39 -070035import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070036import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070037import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.app.FragmentManager;
39import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070041import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070042import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070043import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070044import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070045import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070046import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070048import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070049import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070051import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070052import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070053import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070055import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070056import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070057import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070058import android.os.Handler;
59import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070060import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070061import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070062import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070063import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070064import android.provider.DocumentsContract.Document;
Steve McKayef280152015-06-11 10:10:49 -070065import android.support.v7.widget.GridLayoutManager;
66import android.support.v7.widget.LinearLayoutManager;
67import android.support.v7.widget.RecyclerView;
68import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070069import android.support.v7.widget.RecyclerView.RecyclerListener;
70import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070071import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070073import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070074import android.text.format.Time;
75import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070076import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070077import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070078import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070079import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070082import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070085import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070086import android.view.ViewGroup;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087import android.widget.ImageView;
88import android.widget.ListView;
89import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070090import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070091
Steve McKayd0a2a2c2015-03-25 14:35:33 -070092import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070093import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070094import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070095import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070096import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090097import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070098import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -070099import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -0700100
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101import com.google.android.collect.Lists;
102
103import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700104import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700105import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700106
107/**
108 * Display the documents inside a single directory.
109 */
110public class DirectoryFragment extends Fragment {
111
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700112 public static final int TYPE_NORMAL = 1;
113 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700114 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700115
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700116 public static final int ANIM_NONE = 1;
117 public static final int ANIM_SIDE = 2;
118 public static final int ANIM_DOWN = 3;
119 public static final int ANIM_UP = 4;
120
Ben Kwaf5858932015-04-07 15:43:39 -0700121 public static final int REQUEST_COPY_DESTINATION = 1;
122
Steve McKayef280152015-06-11 10:10:49 -0700123 private static final int LOADER_ID = 42;
124 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700125 private static final boolean DEBUG_ENABLE_DND = false;
126
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 private static final String EXTRA_ROOT = "root";
129 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700130 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700131 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700132
Steve McKayef280152015-06-11 10:10:49 -0700133 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134
Steve McKayef280152015-06-11 10:10:49 -0700135 private View mEmptyView;
136 private RecyclerView mRecView;
137
138 private int mType = TYPE_NORMAL;
139 private String mStateKey;
140
141 private int mLastMode = MODE_UNKNOWN;
142 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
143 private boolean mLastShowSize;
144 private boolean mHideGridTitles;
145 private boolean mSvelteRecents;
146 private Point mThumbSize;
147 private DocumentsAdapter mAdapter;
148 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700149 private FragmentTuner mFragmentTuner;
150 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700151 private MultiSelectManager mSelectionManager;
152 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700153 private LinearLayoutManager mListLayout;
154 private GridLayoutManager mGridLayout;
155 private OnLayoutChangeListener mRecyclerLayoutListener;
156 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700157
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700158 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
159 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700160 }
161
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700162 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
163 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700164 }
165
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700166 public static void showRecentsOpen(FragmentManager fm, int anim) {
167 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700168 }
169
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700170 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
171 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700172 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700173 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700174 args.putParcelable(EXTRA_ROOT, root);
175 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700176 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700178 final FragmentTransaction ft = fm.beginTransaction();
179 switch (anim) {
180 case ANIM_SIDE:
181 args.putBoolean(EXTRA_IGNORE_STATE, true);
182 break;
183 case ANIM_DOWN:
184 args.putBoolean(EXTRA_IGNORE_STATE, true);
185 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
186 break;
187 case ANIM_UP:
188 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
189 break;
190 }
191
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700192 final DirectoryFragment fragment = new DirectoryFragment();
193 fragment.setArguments(args);
194
Jeff Sharkey76112212013-08-06 11:26:10 -0700195 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700196 ft.commitAllowingStateLoss();
197 }
198
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700199 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
200 final StringBuilder builder = new StringBuilder();
201 builder.append(root != null ? root.authority : "null").append(';');
202 builder.append(root != null ? root.rootId : "null").append(';');
203 builder.append(doc != null ? doc.documentId : "null");
204 return builder.toString();
205 }
206
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700207 public static DirectoryFragment get(FragmentManager fm) {
208 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700209 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700210 }
211
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700212 @Override
213 public View onCreateView(
214 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
215 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700216 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
218
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700219 mEmptyView = view.findViewById(android.R.id.empty);
220
Steve McKayef280152015-06-11 10:10:49 -0700221 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
222 mRecView.setRecyclerListener(
223 new RecyclerListener() {
224 @Override
225 public void onViewRecycled(ViewHolder holder) {
226 cancelThumbnailTask(holder.itemView);
227 }
228 });
Steve McKay8e258c62015-05-06 14:27:57 -0700229
Steve McKayd57f5fa2015-07-23 16:33:41 -0700230 // TODO: Rather than update columns on layout changes, push this
231 // code (or something like it) into GridLayoutManager.
232 mRecView.addOnLayoutChangeListener(
233 new OnLayoutChangeListener() {
234
235 @Override
236 public void onLayoutChange(
237 View v, int left, int top, int right, int bottom, int oldLeft,
238 int oldTop, int oldRight, int oldBottom) {
239 int thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
240 mColumnCount = pickColumnCount(thumbSize);
241 if (mGridLayout != null) {
242 mGridLayout.setSpanCount(mColumnCount);
243 }
244 }
245 });
246
247 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700248 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700249 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700250 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700251
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252 return view;
253 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700254
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700256 public void onDestroyView() {
257 super.onDestroyView();
258
259 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700260 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700261 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700262 final View view = mRecView.getChildAt(i);
263 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700265
Steve McKayef280152015-06-11 10:10:49 -0700266 // Clear any outstanding selection
267 mSelectionManager.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700268 }
269
270 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700271 public void onActivityCreated(Bundle savedInstanceState) {
272 super.onActivityCreated(savedInstanceState);
273
274 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700275 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700276
Jeff Sharkey9656a532013-09-13 13:42:19 -0700277 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
278 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
279
Steve McKayef280152015-06-11 10:10:49 -0700280 mAdapter = new DocumentsAdapter(context);
281 mRecView.setAdapter(mAdapter);
282
283 GestureDetector.SimpleOnGestureListener listener =
284 new GestureDetector.SimpleOnGestureListener() {
285 @Override
286 public boolean onSingleTapUp(MotionEvent e) {
287 return DirectoryFragment.this.onSingleTapUp(e);
288 }
289 };
290
291 mSelectionManager = new MultiSelectManager(mRecView, listener);
292 mSelectionManager.addCallback(new SelectionModeListener());
293
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700294 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700295 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700296
Steve McKay1f199482015-05-20 15:58:42 -0700297 mFragmentTuner = pickFragmentTuner(state);
298 mClipper = new DocumentClipper(context);
299
Jeff Sharkey9656a532013-09-13 13:42:19 -0700300 if (mType == TYPE_RECENT_OPEN) {
301 // Hide titles when showing recents for picking images/videos
302 mHideGridTitles = MimePredicate.mimeMatches(
303 MimePredicate.VISUAL_MIMES, state.acceptMimes);
304 } else {
305 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
306 }
307
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700308 final ActivityManager am = (ActivityManager) context.getSystemService(
309 Context.ACTIVITY_SERVICE);
310 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
311
Jeff Sharkey46899c82013-08-18 22:26:48 -0700312 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700313 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700314 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700315 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700316
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700317 Uri contentsUri;
318 switch (mType) {
319 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700320 contentsUri = DocumentsContract.buildChildDocumentsUri(
321 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700322 if (state.action == ACTION_MANAGE) {
323 contentsUri = DocumentsContract.setManageMode(contentsUri);
324 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700325 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700326 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700327 case TYPE_SEARCH:
328 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700329 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700330 if (state.action == ACTION_MANAGE) {
331 contentsUri = DocumentsContract.setManageMode(contentsUri);
332 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700333 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700334 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700335 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700336 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700337 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700338 default:
339 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700340 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700341 }
342
343 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700344 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700345 if (result == null || result.exception != null) {
346 // onBackPressed does a fragment transaction, which can't be done inside
347 // onLoadFinished
348 mHandler.post(new Runnable() {
349 @Override
350 public void run() {
351 final Activity activity = getActivity();
352 if (activity != null) {
353 activity.onBackPressed();
354 }
355 }
356 });
357 return;
358 }
359
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700360 if (!isAdded()) return;
361
Steve McKayef280152015-06-11 10:10:49 -0700362 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700363
364 // Push latest state up to UI
365 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700366 if (result.mode != MODE_UNKNOWN) {
367 state.derivedMode = result.mode;
368 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700369 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700370 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700371
372 updateDisplayState();
373
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700374 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700375 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
376 context instanceof DocumentsActivity) {
377 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700378 }
379
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700380 // Restore any previous instance state
381 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
382 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
383 getView().restoreHierarchyState(container);
384 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700385 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700386 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700387
388 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700389 }
390
391 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700392 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700393 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700394 }
395 };
396
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700397 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700398 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700399
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700400 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700401 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700402 }
403
Jeff Sharkey42d26792013-09-06 13:22:09 -0700404 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700405 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700406 // There's only one request code right now. Replace this with a switch statement or
407 // something more scalable when more codes are added.
408 if (requestCode != REQUEST_COPY_DESTINATION) {
409 return;
410 }
411 if (resultCode == Activity.RESULT_CANCELED || data == null) {
412 // User pressed the back button or otherwise cancelled the destination pick. Don't
413 // proceed with the copy.
414 return;
415 }
416
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900417 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700418 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
419 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700420 }
421
Steve McKayef280152015-06-11 10:10:49 -0700422 private int getEventAdapterPosition(MotionEvent e) {
423 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
424 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
425 }
426
427 private boolean onSingleTapUp(MotionEvent e) {
428 int position = getEventAdapterPosition(e);
429
430 if (position != RecyclerView.NO_POSITION) {
431 final Cursor cursor = mAdapter.getItem(position);
432 checkNotNull(cursor, "Cursor cannot be null.");
433 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
434 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
435 if (isDocumentEnabled(docMimeType, docFlags)) {
436 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
437 ((BaseActivity) getActivity()).onDocumentPicked(doc);
438 return true;
439 }
440 }
441
442 return false;
443 }
444
Ben Kwaf5858932015-04-07 15:43:39 -0700445 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700446 public void onStop() {
447 super.onStop();
448
449 // Remember last scroll location
450 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
451 getView().saveHierarchyState(container);
452 final State state = getDisplayState(this);
453 state.dirState.put(mStateKey, container);
454 }
455
456 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700457 public void onResume() {
458 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700459 updateDisplayState();
460 }
461
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700462 public void onDisplayStateChanged() {
463 updateDisplayState();
464 }
465
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700466 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700467 // Sort order change always triggers reload; we'll trigger state change
468 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700469 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700470 }
471
472 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700473 final ContentResolver resolver = getActivity().getContentResolver();
474 final State state = getDisplayState(this);
475
476 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
477 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
478
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700479 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700480 final Uri stateUri = RecentsProvider.buildState(
481 root.authority, root.rootId, doc.documentId);
482 final ContentValues values = new ContentValues();
483 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700484
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700485 new AsyncTask<Void, Void, Void>() {
486 @Override
487 protected Void doInBackground(Void... params) {
488 resolver.insert(stateUri, values);
489 return null;
490 }
491 }.execute();
492 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700493
494 // Mode change is just visual change; no need to kick loader, and
495 // deliver change event immediately.
496 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700497 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700498
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700499 updateDisplayState();
500 }
501
502 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700503 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700504
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700505 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700506 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700507 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700508
Steve McKayef280152015-06-11 10:10:49 -0700509 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700510
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700511 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700512 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700513 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
514 } else {
515 choiceMode = ListView.CHOICE_MODE_NONE;
516 }
517
Steve McKayef280152015-06-11 10:10:49 -0700518 final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
519 mThumbSize = new Point(thumbSize, thumbSize);
520 mRecView.setAdapter(mAdapter);
521 }
522
523 /**
524 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
525 * classes as needed.
526 */
527 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700528 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700529
530 final LayoutManager layout;
531 switch (mode) {
532 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700533 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700534 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700535 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700536 }
Steve McKayef280152015-06-11 10:10:49 -0700537 layout = mGridLayout;
538 break;
539 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700540 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700541 if (mListLayout == null) {
542 mListLayout = new LinearLayoutManager(getContext());
543 }
Steve McKayef280152015-06-11 10:10:49 -0700544 layout = mListLayout;
545 break;
546 case MODE_UNKNOWN:
547 default:
548 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700549 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700550
Steve McKayef280152015-06-11 10:10:49 -0700551 mRecView.setLayoutManager(layout);
552 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700553 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700554 }
555
Steve McKayd57f5fa2015-07-23 16:33:41 -0700556 private int pickColumnCount(final int thumbSize) {
557 int itemPadding =
558 getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
559 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
560 checkState(mRecView.getWidth() > 0);
561 int columnCount = Math.max(1,
562 (mRecView.getWidth() - viewPadding) / (thumbSize + itemPadding));
563 return columnCount;
564 }
565
Steve McKayef280152015-06-11 10:10:49 -0700566 /**
567 * Manages the integration between our ActionMode and MultiSelectManager, initiating
568 * ActionMode when there is a selection, canceling it when there is no selection,
569 * and clearing selection when action mode is explicitly exited by the user.
570 */
571 private final class SelectionModeListener
572 implements MultiSelectManager.Callback, ActionMode.Callback {
573
574 private Selection mSelected = new Selection();
575 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700576 private int mNoDeleteCount = 0;
577 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700578
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700579 @Override
Steve McKayef280152015-06-11 10:10:49 -0700580 public boolean onBeforeItemStateChange(int position, boolean selected) {
581 // Directories and footer items cannot be checked
582 if (selected) {
583 final Cursor cursor = mAdapter.getItem(position);
584 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700585 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
586 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700587 return isDocumentEnabled(docMimeType, docFlags);
588 }
589 return true;
590 }
591
592 @Override
593 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700594
595 final Cursor cursor = mAdapter.getItem(position);
596 checkNotNull(cursor, "Cursor cannot be null.");
597
598 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
599 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
600 mNoDeleteCount += selected ? 1 : -1;
601 }
602
Steve McKayef280152015-06-11 10:10:49 -0700603 mSelectionManager.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700604 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700605 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
606 if (mActionMode == null) {
607 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
608 mActionMode = getActivity().startActionMode(this);
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900609 getActivity().getWindow().setStatusBarColor(
610 getResources().getColor(R.color.action_mode_status_bar_background));
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700611 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700612 updateActionMenu();
613 } else {
614 if (DEBUG) Log.d(TAG, "Finishing action mode.");
615 if (mActionMode != null) {
616 mActionMode.finish();
617 }
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900618 getActivity().getWindow().setStatusBarColor(
619 getResources().getColor(R.color.status_bar_background));
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700620 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700621
Steve McKayef280152015-06-11 10:10:49 -0700622 if (mActionMode != null) {
623 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
624 }
625 }
626
627 // Called when the user exits the action mode
628 @Override
629 public void onDestroyActionMode(ActionMode mode) {
630 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
631 mActionMode = null;
632 // clear selection
633 mSelectionManager.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700634 mSelected.clear();
635 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700636 }
637
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700638 @Override
639 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
640 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Steve McKayef280152015-06-11 10:10:49 -0700641 mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
642 return mSelectionManager.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700643 }
644
645 @Override
646 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700647 mMenu = menu;
648 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700649 return true;
650 }
651
Steve McKay4f4232d2015-07-22 12:13:46 -0700652 private void updateActionMenu() {
653 checkNotNull(mMenu);
654 // Delegate update logic to our owning action, since specialized logic is desired.
655 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
656 }
657
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700658 @Override
Steve McKayef280152015-06-11 10:10:49 -0700659 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700660
Steve McKayef280152015-06-11 10:10:49 -0700661 Selection selection = new Selection();
662 mSelectionManager.getSelection(selection);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700663
Jeff Sharkey873daa32013-08-18 17:38:20 -0700664 final int id = item.getItemId();
665 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700666 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700667 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700668 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700669
670 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700671 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700672 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700673 return true;
674
675 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700676 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700677 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700678 return true;
679
Steve McKay1f199482015-05-20 15:58:42 -0700680 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700681 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700682 mode.finish();
683 return true;
684
Steve McKay1f199482015-05-20 15:58:42 -0700685 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700686 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700687 mode.finish();
688 return true;
689
Steve McKay1f199482015-05-20 15:58:42 -0700690 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700691 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700692 mode.finish();
693 return true;
694
Ben Kwa512a6ba2015-03-31 08:15:21 -0700695 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700696 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700697 return true;
698
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700699 } else {
700 return false;
701 }
702 }
Steve McKayef280152015-06-11 10:10:49 -0700703 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700704
Steve McKayef280152015-06-11 10:10:49 -0700705 private static void cancelThumbnailTask(View view) {
706 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
707 if (iconThumb != null) {
708 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
709 if (oldTask != null) {
710 oldTask.preempt();
711 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700712 }
713 }
Steve McKayef280152015-06-11 10:10:49 -0700714 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700715
Steve McKayef280152015-06-11 10:10:49 -0700716 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700717 new GetDocumentsTask() {
718 @Override
719 void onDocumentsReady(List<DocumentInfo> docs) {
720 // TODO: Implement support in standalone for opening multiple docs.
721 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700722 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700723 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700724 }
725
Steve McKayef280152015-06-11 10:10:49 -0700726 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700727 new GetDocumentsTask() {
728 @Override
729 void onDocumentsReady(List<DocumentInfo> docs) {
730 Intent intent;
731
732 // Filter out directories - those can't be shared.
733 List<DocumentInfo> docsForSend = Lists.newArrayList();
734 for (DocumentInfo doc: docs) {
735 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
736 docsForSend.add(doc);
737 }
738 }
739
740 if (docsForSend.size() == 1) {
741 final DocumentInfo doc = docsForSend.get(0);
742
743 intent = new Intent(Intent.ACTION_SEND);
744 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
745 intent.addCategory(Intent.CATEGORY_DEFAULT);
746 intent.setType(doc.mimeType);
747 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
748
749 } else if (docsForSend.size() > 1) {
750 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
751 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
752 intent.addCategory(Intent.CATEGORY_DEFAULT);
753
754 final ArrayList<String> mimeTypes = Lists.newArrayList();
755 final ArrayList<Uri> uris = Lists.newArrayList();
756 for (DocumentInfo doc : docsForSend) {
757 mimeTypes.add(doc.mimeType);
758 uris.add(doc.derivedUri);
759 }
760
761 intent.setType(findCommonMimeType(mimeTypes));
762 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
763
764 } else {
765 return;
766 }
767
768 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
769 startActivity(intent);
770 }
771 }.execute(selected);
772 }
773
Steve McKayef280152015-06-11 10:10:49 -0700774 private void deleteDocuments(final Selection selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700775 final Context context = getActivity();
776 final ContentResolver resolver = context.getContentResolver();
777
Steve McKay9276f3b2015-05-27 16:11:42 -0700778 new GetDocumentsTask() {
779 @Override
780 void onDocumentsReady(List<DocumentInfo> docs) {
781 boolean hadTrouble = false;
782 for (DocumentInfo doc : docs) {
783 if (!doc.isDeleteSupported()) {
784 Log.w(TAG, "Skipping " + doc);
785 hadTrouble = true;
786 continue;
787 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700788
Steve McKay9276f3b2015-05-27 16:11:42 -0700789 ContentProviderClient client = null;
790 try {
791 client = DocumentsApplication.acquireUnstableProviderOrThrow(
792 resolver, doc.derivedUri.getAuthority());
793 DocumentsContract.deleteDocument(client, doc.derivedUri);
794 } catch (Exception e) {
795 Log.w(TAG, "Failed to delete " + doc);
796 hadTrouble = true;
797 } finally {
798 ContentProviderClient.releaseQuietly(client);
799 }
800 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700801
Steve McKay9276f3b2015-05-27 16:11:42 -0700802 if (hadTrouble) {
803 Toast.makeText(
804 context,
805 R.string.toast_failed_delete,
806 Toast.LENGTH_SHORT).show();
807 }
808 }
809 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700810 }
811
Steve McKayef280152015-06-11 10:10:49 -0700812 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700813 // Pop up a dialog to pick a destination. This is inadequate but works for now.
814 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900815 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900816 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900817 Uri.EMPTY,
818 getActivity(),
819 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700820
821 new GetDocumentsTask() {
822 @Override
823 void onDocumentsReady(List<DocumentInfo> docs) {
824 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
825
826 boolean directoryCopy = false;
827 for (DocumentInfo info : docs) {
828 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
829 directoryCopy = true;
830 break;
831 }
832 }
833 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
834 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
835 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900836 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700837 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700838 }
839
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700840 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700841 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700842 }
843
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700844 private static abstract class Footer {
845 private final int mItemViewType;
846
847 public Footer(int itemViewType) {
848 mItemViewType = itemViewType;
849 }
850
851 public abstract View getView(View convertView, ViewGroup parent);
852
853 public int getItemViewType() {
854 return mItemViewType;
855 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700856 }
857
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700858 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700859 public LoadingFooter() {
860 super(1);
861 }
862
Jeff Sharkey20b32272013-09-03 15:25:52 -0700863 @Override
864 public View getView(View convertView, ViewGroup parent) {
865 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700866 final State state = getDisplayState(DirectoryFragment.this);
867
Jeff Sharkey20b32272013-09-03 15:25:52 -0700868 if (convertView == null) {
869 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700870 if (state.derivedMode == MODE_LIST) {
871 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
872 } else if (state.derivedMode == MODE_GRID) {
873 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
874 } else {
875 throw new IllegalStateException();
876 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700877 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700878
Jeff Sharkey20b32272013-09-03 15:25:52 -0700879 return convertView;
880 }
881 }
882
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700883 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700884 private final int mIcon;
885 private final String mMessage;
886
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700887 public MessageFooter(int itemViewType, int icon, String message) {
888 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700889 mIcon = icon;
890 mMessage = message;
891 }
892
893 @Override
894 public View getView(View convertView, ViewGroup parent) {
895 final Context context = parent.getContext();
896 final State state = getDisplayState(DirectoryFragment.this);
897
898 if (convertView == null) {
899 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700900 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700901 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700902 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700903 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
904 } else {
905 throw new IllegalStateException();
906 }
907 }
908
909 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
910 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
911 icon.setImageResource(mIcon);
912 title.setText(mMessage);
913 return convertView;
914 }
915 }
916
Steve McKayef280152015-06-11 10:10:49 -0700917 // Provide a reference to the views for each data item
918 // Complex data items may need more than one view per item, and
919 // you provide access to all the views for a data item in a view holder
920 private static final class DocumentHolder extends RecyclerView.ViewHolder {
921 // each data item is just a string in this case
922 public View view;
923 public String docId; // The stable document id.
924 public DocumentHolder(View view) {
925 super(view);
926 this.view = view;
927 }
928 }
929
930 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
931
932 private final Context mContext;
933 private final LayoutInflater mInflater;
934 // TODO: Bring back support for footers.
935 private final List<Footer> mFooters = Lists.newArrayList();
936
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700937 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700938 private int mCursorCount;
939
Steve McKayef280152015-06-11 10:10:49 -0700940 public DocumentsAdapter(Context context) {
941 mContext = context;
942 mInflater = LayoutInflater.from(context);
943 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700944
Steve McKayef280152015-06-11 10:10:49 -0700945 public void replaceResult(DirectoryResult result) {
946 if (DEBUG) Log.i(TAG, "Updating adapter with new result set.");
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700947 mCursor = result != null ? result.cursor : null;
948 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700949
950 mFooters.clear();
951
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700952 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700953 if (extras != null) {
954 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
955 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700956 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700957 }
958 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
959 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700960 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700961 }
962 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
963 mFooters.add(new LoadingFooter());
964 }
965 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700966
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700967 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700968 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700969 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700970 }
971
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700972 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700973 mEmptyView.setVisibility(View.VISIBLE);
974 } else {
975 mEmptyView.setVisibility(View.GONE);
976 }
977
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700978 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700979 }
980
981 @Override
Steve McKayef280152015-06-11 10:10:49 -0700982 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
983 final State state = getDisplayState(DirectoryFragment.this);
984 final LayoutInflater inflater = LayoutInflater.from(getContext());
985 switch (state.derivedMode) {
986 case MODE_GRID:
987 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
988 case MODE_LIST:
989 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
990 case MODE_UNKNOWN:
991 default:
992 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -0700993 }
994 }
995
Steve McKayef280152015-06-11 10:10:49 -0700996 @Override
997 public void onBindViewHolder(DocumentHolder holder, int position) {
998
999 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001000 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001001 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -07001002 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1003 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1004 context, mThumbSize);
1005
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001006 final Cursor cursor = getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001007 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001008
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001009 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1010 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001011 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1012 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1013 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1014 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1015 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1016 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1017 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1018 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001019
Steve McKayef280152015-06-11 10:10:49 -07001020 holder.docId = docId;
1021 final View itemView = holder.view;
1022 itemView.setActivated(mSelectionManager.getSelection().contains(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001023
Steve McKayef280152015-06-11 10:10:49 -07001024 final View line1 = itemView.findViewById(R.id.line1);
1025 final View line2 = itemView.findViewById(R.id.line2);
1026
1027 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1028 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1029 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1030 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1031 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1032 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1033 final TextView date = (TextView) itemView.findViewById(R.id.date);
1034 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001035
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001036 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001037 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001038 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001039 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001040 }
1041
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001042 iconMime.animate().cancel();
1043 iconThumb.animate().cancel();
1044
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001045 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001046 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001047 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001048 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001049
Jeff Sharkey7e544612014-08-29 15:38:27 -07001050 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1051 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1052
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001053 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001054 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001055 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001056 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001057 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001058 iconThumb.setImageBitmap(cachedResult);
1059 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001060 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001061 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001062 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001063 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001064 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001065 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001066 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001067 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001068 }
1069
1070 // Always throw MIME icon into place, even when a thumbnail is being
1071 // loaded in background.
1072 if (cacheHit) {
1073 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001074 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001075 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001076 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001077 iconMime.setAlpha(1f);
1078 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001079 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001080 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001081 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001082 }
1083
Jeff Sharkey9656a532013-09-13 13:42:19 -07001084 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001085 boolean hasLine2 = false;
1086
Jeff Sharkey9656a532013-09-13 13:42:19 -07001087 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1088 if (!hideTitle) {
1089 title.setText(docDisplayName);
1090 hasLine1 = true;
1091 }
1092
1093 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001094 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001095 // We've already had to enumerate roots before any results can
1096 // be shown, so this will never block.
1097 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001098 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001099 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001100 } else {
Steve McKayef280152015-06-11 10:10:49 -07001101 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001102 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001103
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001104 if (summary != null) {
1105 final boolean alwaysShowSummary = getResources()
1106 .getBoolean(R.bool.always_show_summary);
1107 if (alwaysShowSummary) {
1108 summary.setText(root.getDirectoryString());
1109 summary.setVisibility(View.VISIBLE);
1110 hasLine2 = true;
1111 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001112 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001113 // No summary needed if icon speaks for itself
1114 summary.setVisibility(View.INVISIBLE);
1115 } else {
1116 summary.setText(root.getDirectoryString());
1117 summary.setVisibility(View.VISIBLE);
1118 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1119 hasLine2 = true;
1120 }
1121 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001122 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001123 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001124 // Directories showing thumbnails in grid mode get a little icon
1125 // hint to remind user they're a directory.
1126 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1127 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001128 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001129 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001130 }
1131
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001132 if (summary != null) {
1133 if (docSummary != null) {
1134 summary.setText(docSummary);
1135 summary.setVisibility(View.VISIBLE);
1136 hasLine2 = true;
1137 } else {
1138 summary.setVisibility(View.INVISIBLE);
1139 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001140 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001141 }
1142
Jeff Sharkey9656a532013-09-13 13:42:19 -07001143 if (icon1 != null) icon1.setVisibility(View.GONE);
1144 if (icon2 != null) icon2.setVisibility(View.GONE);
1145
1146 if (iconDrawable != null) {
1147 if (hasLine1) {
1148 icon1.setVisibility(View.VISIBLE);
1149 icon1.setImageDrawable(iconDrawable);
1150 } else {
1151 icon2.setVisibility(View.VISIBLE);
1152 icon2.setImageDrawable(iconDrawable);
1153 }
1154 }
1155
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001156 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001157 date.setText(null);
1158 } else {
Steve McKayef280152015-06-11 10:10:49 -07001159 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001160 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001161 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001162
1163 if (state.showSize) {
1164 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001165 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001166 size.setText(null);
1167 } else {
Steve McKayef280152015-06-11 10:10:49 -07001168 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001169 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001170 }
1171 } else {
1172 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001173 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001174
Jeff Sharkey9656a532013-09-13 13:42:19 -07001175 if (line1 != null) {
1176 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1177 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001178 if (line2 != null) {
1179 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1180 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001181
Steve McKayef280152015-06-11 10:10:49 -07001182 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001183
1184 iconMime.setAlpha(iconAlpha);
1185 iconThumb.setAlpha(iconAlpha);
1186 if (icon1 != null) icon1.setAlpha(iconAlpha);
1187 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001188
Steve McKay8e258c62015-05-06 14:27:57 -07001189 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001190 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001191 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001192 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001193
Steve McKayef280152015-06-11 10:10:49 -07001194 private Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001195 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001196 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001197 return mCursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001198 }
Steve McKayef280152015-06-11 10:10:49 -07001199
1200 Log.w(TAG, "Returning null cursor for position: " + position);
1201 if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
1202 if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
1203 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001204 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001205
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001206 @Override
Steve McKayef280152015-06-11 10:10:49 -07001207 public int getItemCount() {
1208 return mCursorCount;
1209 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001210 }
1211
1212 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001213 public int getItemViewType(int position) {
1214 if (position < mCursorCount) {
1215 return 0;
1216 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001217 position -= mCursorCount;
1218 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001219 }
1220 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001221
Steve McKayef280152015-06-11 10:10:49 -07001222 private boolean isEmpty() {
1223 return getItemCount() > 0;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001224 }
1225 }
1226
1227 private static String formatTime(Context context, long when) {
1228 // TODO: DateUtils should make this easier
1229 Time then = new Time();
1230 then.set(when);
1231 Time now = new Time();
1232 now.setToNow();
1233
1234 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1235 | DateUtils.FORMAT_ABBREV_ALL;
1236
1237 if (then.year != now.year) {
1238 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1239 } else if (then.yearDay != now.yearDay) {
1240 flags |= DateUtils.FORMAT_SHOW_DATE;
1241 } else {
1242 flags |= DateUtils.FORMAT_SHOW_TIME;
1243 }
1244
1245 return DateUtils.formatDateTime(context, when, flags);
1246 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001247
1248 private String findCommonMimeType(List<String> mimeTypes) {
1249 String[] commonType = mimeTypes.get(0).split("/");
1250 if (commonType.length != 2) {
1251 return "*/*";
1252 }
1253
1254 for (int i = 1; i < mimeTypes.size(); i++) {
1255 String[] type = mimeTypes.get(i).split("/");
1256 if (type.length != 2) continue;
1257
1258 if (!commonType[1].equals(type[1])) {
1259 commonType[1] = "*";
1260 }
1261
1262 if (!commonType[0].equals(type[0])) {
1263 commonType[0] = "*";
1264 commonType[1] = "*";
1265 break;
1266 }
1267 }
1268
1269 return commonType[0] + "/" + commonType[1];
1270 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001271
1272 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001273 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001274 if (v.isEnabled() == enabled) return;
1275 v.setEnabled(enabled);
1276
1277 if (v instanceof ViewGroup) {
1278 final ViewGroup vg = (ViewGroup) v;
1279 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1280 setEnabledRecursive(vg.getChildAt(i), enabled);
1281 }
1282 }
1283 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001284
1285 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1286 final State state = getDisplayState(DirectoryFragment.this);
1287
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001288 // Directories are always enabled
1289 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1290 return true;
1291 }
1292
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001293 // Read-only files are disabled when creating
1294 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1295 return false;
1296 }
1297
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001298 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1299 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001300
Steve McKay1f199482015-05-20 15:58:42 -07001301 private @NonNull List<DocumentInfo> getSelectedDocuments() {
Steve McKayef280152015-06-11 10:10:49 -07001302 Selection sel = mSelectionManager.getSelection(new Selection());
1303 return getItemsAsDocuments(sel);
Steve McKay1f199482015-05-20 15:58:42 -07001304 }
1305
Steve McKayef280152015-06-11 10:10:49 -07001306 private List<DocumentInfo> getItemsAsDocuments(Selection items) {
Steve McKay1f199482015-05-20 15:58:42 -07001307 if (items == null || items.size() == 0) {
1308 return new ArrayList<>(0);
1309 }
1310
1311 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1312 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001313 for (int i = 0; i < size; i++) {
Steve McKayef280152015-06-11 10:10:49 -07001314 final Cursor cursor = mAdapter.getItem(items.get(i));
1315 checkNotNull(cursor, "Cursor cannot be null.");
1316 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1317 docs.add(doc);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001318 }
1319 return docs;
1320 }
1321
Steve McKay1f199482015-05-20 15:58:42 -07001322 private void copyFromClipboard() {
1323 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1324
1325 @Override
1326 protected List<DocumentInfo> doInBackground(Void... params) {
1327 return mClipper.getClippedDocuments();
1328 }
1329
1330 @Override
1331 protected void onPostExecute(List<DocumentInfo> docs) {
1332 DocumentInfo destination =
1333 ((BaseActivity) getActivity()).getCurrentDirectory();
1334 copyDocuments(docs, destination);
1335 }
1336 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001337 }
1338
Steve McKay1f199482015-05-20 15:58:42 -07001339 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001340 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001341 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001342
Steve McKay1f199482015-05-20 15:58:42 -07001343 @Override
1344 protected List<DocumentInfo> doInBackground(Void... params) {
1345 return mClipper.getDocumentsFromClipData(clipData);
1346 }
1347
1348 @Override
1349 protected void onPostExecute(List<DocumentInfo> docs) {
1350 copyDocuments(docs, destination);
1351 }
1352 }.execute();
1353 }
1354
1355 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1356 if (!canCopy(docs, destination)) {
1357 Toast.makeText(
1358 getActivity(),
1359 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001360 return;
1361 }
1362
Steve McKay1f199482015-05-20 15:58:42 -07001363 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001364 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001365 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001366
Steve McKay1f199482015-05-20 15:58:42 -07001367 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001368 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001369 if (destination != null) {
1370 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001371 tmpStack.addAll(curStack);
1372 } else {
1373 tmpStack = curStack;
1374 }
1375
Steve McKay1f199482015-05-20 15:58:42 -07001376 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001377 }
1378
1379 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1380 Context context = getActivity();
1381 final ContentResolver resolver = context.getContentResolver();
1382 ClipData clipData = null;
1383 for (DocumentInfo doc : docs) {
1384 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1385 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001386 // TODO: figure out what this string should be.
1387 // Currently it is not displayed anywhere in the UI, but this might change.
1388 final String label = "";
1389 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001390 } else {
1391 // TODO: update list of mime types in ClipData.
1392 clipData.addItem(new ClipData.Item(uri));
1393 }
1394 }
1395 return clipData;
1396 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001397
Steve McKay1f199482015-05-20 15:58:42 -07001398 void copySelectedToClipboard() {
Steve McKayef280152015-06-11 10:10:49 -07001399 Selection sel = mSelectionManager.getSelection(new Selection());
1400 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001401 }
Steve McKay0599a442015-05-05 14:50:00 -07001402
Steve McKayef280152015-06-11 10:10:49 -07001403 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001404 new GetDocumentsTask() {
1405 @Override
1406 void onDocumentsReady(List<DocumentInfo> docs) {
1407 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001408 Activity activity = getActivity();
1409 Toast.makeText(activity,
1410 activity.getResources().getQuantityString(
1411 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1412 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001413 }
Steve McKayef280152015-06-11 10:10:49 -07001414 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001415 }
1416
1417 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001418 copyFromClipboard();
1419 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001420 }
1421
Steve McKay0599a442015-05-05 14:50:00 -07001422 /**
1423 * Returns true if the list of files can be copied to destination. Note that this
1424 * is a policy check only. Currently the method does not attempt to verify
1425 * available space or any other environmental aspects possibly resulting in
1426 * failure to copy.
1427 *
1428 * @return true if the list of files can be copied to destination.
1429 */
1430 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1431 BaseActivity activity = (BaseActivity)getActivity();
1432
1433 final RootInfo root = activity.getCurrentRoot();
1434
1435 // Can't copy folders to Downloads.
1436 if (root.isDownloads()) {
1437 for (DocumentInfo docs : files) {
1438 if (docs.isDirectory()) {
1439 return false;
1440 }
1441 }
1442 }
1443
1444 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1445 }
1446
1447 void selectAllFiles() {
Steve McKay9459a7c2015-07-24 13:14:20 -07001448 boolean changed = mSelectionManager.setItemsSelected(0, mAdapter.getItemCount(), true);
1449 if (changed) {
1450 updateDisplayState();
1451 }
Steve McKay0599a442015-05-05 14:50:00 -07001452 }
1453
Steve McKayef280152015-06-11 10:10:49 -07001454 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001455 // Listen for drops on non-directory items and empty space.
1456 view.setOnDragListener(mOnDragListener);
1457 }
1458
1459 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1460 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1461 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1462 // Make a directory item a drop target. Drop on non-directories and empty space
1463 // is handled at the list/grid view level.
1464 view.setOnDragListener(mOnDragListener);
1465 }
1466
1467 // Temporary: attaching the listener to the title only.
1468 // Attaching to the entire item conflicts with the item long click handler responsible
1469 // for item selection.
1470 final View title = view.findViewById(android.R.id.title);
1471 title.setOnLongClickListener(mLongClickListener);
1472 }
1473
1474 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1475 @Override
1476 public boolean onDrag(View v, DragEvent event) {
1477 switch (event.getAction()) {
1478 case DragEvent.ACTION_DRAG_STARTED:
1479 // TODO: Check if the event contains droppable data.
1480 return true;
1481
1482 // TODO: Highlight potential drop target directory?
1483 // TODO: Expand drop target directory on hover?
1484 case DragEvent.ACTION_DRAG_ENTERED:
1485 case DragEvent.ACTION_DRAG_LOCATION:
1486 case DragEvent.ACTION_DRAG_EXITED:
1487 case DragEvent.ACTION_DRAG_ENDED:
1488 return true;
1489
1490 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001491 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001492 DocumentInfo dstDir = null;
1493 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1494 Cursor dstCursor = mAdapter.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001495 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001496 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1497 // TODO: Do not drop into the directory where the documents came from.
1498 }
1499 copyFromClipData(event.getClipData(), dstDir);
1500 return true;
1501 }
1502 return false;
1503 }
1504 };
1505
1506 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1507 @Override
1508 public boolean onLongClick(View v) {
1509 final List<DocumentInfo> docs = getDraggableDocuments(v);
1510 if (docs.isEmpty()) {
1511 return false;
1512 }
1513 v.startDrag(
1514 getClipDataFromDocuments(docs),
1515 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1516 null,
1517 View.DRAG_FLAG_GLOBAL
1518 );
1519 return true;
1520 }
1521 };
1522
1523 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001524 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001525 if (position == android.widget.AdapterView.INVALID_POSITION) {
1526 return Collections.EMPTY_LIST;
1527 }
1528
1529 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1530 if (!selectedDocs.isEmpty()) {
Steve McKayef280152015-06-11 10:10:49 -07001531 if (!mSelectionManager.getSelection().contains(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001532 // There is a selection that does not include the current item, drag nothing.
1533 return Collections.EMPTY_LIST;
1534 }
1535 return selectedDocs;
1536 }
1537
1538 final Cursor cursor = mAdapter.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001539 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001540 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1541 return Lists.newArrayList(doc);
1542 }
1543
1544 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1545 if (docs.size() == 1) {
1546 final DocumentInfo doc = docs.get(0);
1547 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1548 doc.mimeType, doc.icon, getDisplayState(this));
1549 }
1550 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1551 }
1552
1553 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1554 String docMimeType, int docIcon, State state) {
1555 if (docIcon != 0) {
1556 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1557 } else {
1558 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1559 state.derivedMode);
1560 }
1561 }
1562
Steve McKayef280152015-06-11 10:10:49 -07001563 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1564 implements Preemptable {
1565 private final Uri mUri;
1566 private final ImageView mIconMime;
1567 private final ImageView mIconThumb;
1568 private final Point mThumbSize;
1569 private final float mTargetAlpha;
1570 private final CancellationSignal mSignal;
1571
1572 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1573 float targetAlpha) {
1574 mUri = uri;
1575 mIconMime = iconMime;
1576 mIconThumb = iconThumb;
1577 mThumbSize = thumbSize;
1578 mTargetAlpha = targetAlpha;
1579 mSignal = new CancellationSignal();
1580 }
1581
1582 @Override
1583 public void preempt() {
1584 cancel(false);
1585 mSignal.cancel();
1586 }
1587
1588 @Override
1589 protected Bitmap doInBackground(Uri... params) {
1590 if (isCancelled()) return null;
1591
1592 final Context context = mIconThumb.getContext();
1593 final ContentResolver resolver = context.getContentResolver();
1594
1595 ContentProviderClient client = null;
1596 Bitmap result = null;
1597 try {
1598 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1599 resolver, mUri.getAuthority());
1600 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1601 if (result != null) {
1602 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1603 context, mThumbSize);
1604 thumbs.put(mUri, result);
1605 }
1606 } catch (Exception e) {
1607 if (!(e instanceof OperationCanceledException)) {
1608 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1609 }
1610 } finally {
1611 ContentProviderClient.releaseQuietly(client);
1612 }
1613 return result;
1614 }
1615
1616 @Override
1617 protected void onPostExecute(Bitmap result) {
1618 if (mIconThumb.getTag() == this && result != null) {
1619 mIconThumb.setTag(null);
1620 mIconThumb.setImageBitmap(result);
1621
1622 mIconMime.setAlpha(mTargetAlpha);
1623 mIconMime.animate().alpha(0f).start();
1624 mIconThumb.setAlpha(0f);
1625 mIconThumb.animate().alpha(mTargetAlpha).start();
1626 }
1627 }
1628 }
1629
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001630 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1631
1632 private final Drawable mShadow;
1633
1634 private final int mShadowDimension;
1635
1636 public DrawableShadowBuilder(Drawable shadow) {
1637 mShadow = shadow;
1638 mShadowDimension = getResources().getDimensionPixelSize(
1639 R.dimen.drag_shadow_size);
1640 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1641 }
1642
1643 public void onProvideShadowMetrics(
1644 Point shadowSize, Point shadowTouchPoint) {
1645 shadowSize.set(mShadowDimension, mShadowDimension);
1646 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1647 }
1648
1649 public void onDrawShadow(Canvas canvas) {
1650 mShadow.draw(canvas);
1651 }
1652 }
Steve McKay1f199482015-05-20 15:58:42 -07001653
1654 private FragmentTuner pickFragmentTuner(final State state) {
1655 return state.action == ACTION_BROWSE_ALL
1656 ? new StandaloneTuner()
1657 : new DefaultTuner(state);
1658 }
1659
1660 /**
1661 * Interface for specializing the Fragment for the "host" Activity.
1662 * Feel free to expand the role of this class to handle other specializations.
1663 */
1664 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001665 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001666 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001667 }
1668
1669 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001670 * Abstract task providing support for loading documents *off*
1671 * the main thread. And if it isn't obvious, creating a list
1672 * of documents (especially large lists) can be pretty expensive.
1673 */
1674 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001675 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001676 @Override
Steve McKayef280152015-06-11 10:10:49 -07001677 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001678 return getItemsAsDocuments(selected[0]);
1679 }
1680
1681 @Override
1682 protected final void onPostExecute(List<DocumentInfo> docs) {
1683 onDocumentsReady(docs);
1684 }
1685
1686 abstract void onDocumentsReady(List<DocumentInfo> docs);
1687 }
1688
1689 /**
Steve McKay1f199482015-05-20 15:58:42 -07001690 * Provides support for Platform specific specializations of DirectoryFragment.
1691 */
1692 private static final class DefaultTuner implements FragmentTuner {
1693
1694 private final State mState;
1695
1696 public DefaultTuner(State state) {
1697 mState = state;
1698 }
1699
1700 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001701 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001702 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1703
1704 final MenuItem open = menu.findItem(R.id.menu_open);
1705 final MenuItem share = menu.findItem(R.id.menu_share);
1706 final MenuItem delete = menu.findItem(R.id.menu_delete);
1707 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1708 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1709 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1710
1711 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1712 || mState.action == ACTION_BROWSE);
1713
1714 open.setVisible(!manageOrBrowse);
1715 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001716 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001717 // Disable copying from the Recents view.
1718 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1719 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1720
1721 // Only shown in standalone mode.
1722 copyToClipboard.setVisible(false);
1723 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001724
1725 @Override
1726 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001727 }
1728
1729 /**
1730 * Provides support for Standalone specific specializations of DirectoryFragment.
1731 */
1732 private static final class StandaloneTuner implements FragmentTuner {
1733 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001734 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001735 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001736 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001737 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1738
1739 menu.findItem(R.id.menu_open).setVisible(false);
1740 menu.findItem(R.id.menu_copy_to).setVisible(false);
1741 menu.findItem(R.id.menu_move_to).setVisible(false);
1742 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001743
1744 @Override
1745 public void afterActivityCreated(DirectoryFragment fragment) {
1746 new BandSelectManager(fragment.mRecView, fragment.mSelectionManager);
1747 }
Steve McKay1f199482015-05-20 15:58:42 -07001748 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001749}