blob: 240cdda8dc8db7eb1945e87e0b380f04bfc04f7e [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;
Steve McKayfefcd702015-08-20 16:19:38 +000027import static com.android.documentsui.Shared.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
Ben Kwaf5858932015-04-07 15:43:39 -070034import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070035import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070036import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.FragmentManager;
38import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070040import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070041import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070043import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070045import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070047import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070050import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070054import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070056import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070057import android.os.Handler;
58import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070059import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070060import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070061import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070063import android.provider.DocumentsContract.Document;
Steve McKayef280152015-06-11 10:10:49 -070064import android.support.v7.widget.GridLayoutManager;
65import android.support.v7.widget.LinearLayoutManager;
66import android.support.v7.widget.RecyclerView;
67import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070068import android.support.v7.widget.RecyclerView.RecyclerListener;
69import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070070import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070071import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070072import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070073import android.text.format.Time;
74import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070075import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070076import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070077import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070078import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070079import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070080import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070082import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070083import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070084import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070085import android.view.ViewGroup;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070086import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070088import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070089
Steve McKay351a7492015-08-04 10:11:01 -070090import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070091import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070092import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070093import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070094import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070095import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090096import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070097import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -070098import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -070099
Steve McKayfefcd702015-08-20 16:19:38 +0000100import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101
102import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700103import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700104import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700105
106/**
107 * Display the documents inside a single directory.
108 */
109public class DirectoryFragment extends Fragment {
110
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700111 public static final int TYPE_NORMAL = 1;
112 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700113 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700114
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700115 public static final int ANIM_NONE = 1;
116 public static final int ANIM_SIDE = 2;
117 public static final int ANIM_DOWN = 3;
118 public static final int ANIM_UP = 4;
119
Ben Kwaf5858932015-04-07 15:43:39 -0700120 public static final int REQUEST_COPY_DESTINATION = 1;
121
Steve McKayef280152015-06-11 10:10:49 -0700122 private static final int LOADER_ID = 42;
123 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700124 private static final boolean DEBUG_ENABLE_DND = false;
125
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700126 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700127 private static final String EXTRA_ROOT = "root";
128 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700129 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700130 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700131
Steve McKayef280152015-06-11 10:10:49 -0700132 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700133
Steve McKayef280152015-06-11 10:10:49 -0700134 private View mEmptyView;
135 private RecyclerView mRecView;
136
137 private int mType = TYPE_NORMAL;
138 private String mStateKey;
139
140 private int mLastMode = MODE_UNKNOWN;
141 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
142 private boolean mLastShowSize;
143 private boolean mHideGridTitles;
144 private boolean mSvelteRecents;
145 private Point mThumbSize;
146 private DocumentsAdapter mAdapter;
147 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700148 private FragmentTuner mFragmentTuner;
149 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700150 private MultiSelectManager mSelectionManager;
151 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700152 private LinearLayoutManager mListLayout;
153 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700154 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700155
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700156 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
157 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700158 }
159
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700160 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
161 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700162 }
163
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700164 public static void showRecentsOpen(FragmentManager fm, int anim) {
165 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700166 }
167
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700168 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
169 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700170 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700171 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700172 args.putParcelable(EXTRA_ROOT, root);
173 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700174 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700175
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700176 final FragmentTransaction ft = fm.beginTransaction();
177 switch (anim) {
178 case ANIM_SIDE:
179 args.putBoolean(EXTRA_IGNORE_STATE, true);
180 break;
181 case ANIM_DOWN:
182 args.putBoolean(EXTRA_IGNORE_STATE, true);
183 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
184 break;
185 case ANIM_UP:
186 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
187 break;
188 }
189
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700190 final DirectoryFragment fragment = new DirectoryFragment();
191 fragment.setArguments(args);
192
Jeff Sharkey76112212013-08-06 11:26:10 -0700193 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700194 ft.commitAllowingStateLoss();
195 }
196
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700197 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
198 final StringBuilder builder = new StringBuilder();
199 builder.append(root != null ? root.authority : "null").append(';');
200 builder.append(root != null ? root.rootId : "null").append(';');
201 builder.append(doc != null ? doc.documentId : "null");
202 return builder.toString();
203 }
204
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700205 public static DirectoryFragment get(FragmentManager fm) {
206 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700207 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700208 }
209
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700210 @Override
211 public View onCreateView(
212 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
213 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700214 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700215 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
216
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700217 mEmptyView = view.findViewById(android.R.id.empty);
218
Steve McKayef280152015-06-11 10:10:49 -0700219 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
220 mRecView.setRecyclerListener(
221 new RecyclerListener() {
222 @Override
223 public void onViewRecycled(ViewHolder holder) {
224 cancelThumbnailTask(holder.itemView);
225 }
226 });
Steve McKay8e258c62015-05-06 14:27:57 -0700227
Steve McKayd57f5fa2015-07-23 16:33:41 -0700228 // TODO: Rather than update columns on layout changes, push this
229 // code (or something like it) into GridLayoutManager.
230 mRecView.addOnLayoutChangeListener(
231 new OnLayoutChangeListener() {
232
233 @Override
234 public void onLayoutChange(
235 View v, int left, int top, int right, int bottom, int oldLeft,
236 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000237 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700238 if (mGridLayout != null) {
239 mGridLayout.setSpanCount(mColumnCount);
240 }
241 }
242 });
243
244 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700245 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700246 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700247 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700248
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700249 return view;
250 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700251
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700253 public void onDestroyView() {
254 super.onDestroyView();
255
256 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700257 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700258 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700259 final View view = mRecView.getChildAt(i);
260 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700261 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700262
Steve McKayef280152015-06-11 10:10:49 -0700263 // Clear any outstanding selection
264 mSelectionManager.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700265 }
266
267 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700268 public void onActivityCreated(Bundle savedInstanceState) {
269 super.onActivityCreated(savedInstanceState);
270
271 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700272 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700273
Jeff Sharkey9656a532013-09-13 13:42:19 -0700274 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
275 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
276
Steve McKayef280152015-06-11 10:10:49 -0700277 mAdapter = new DocumentsAdapter(context);
278 mRecView.setAdapter(mAdapter);
279
280 GestureDetector.SimpleOnGestureListener listener =
281 new GestureDetector.SimpleOnGestureListener() {
282 @Override
283 public boolean onSingleTapUp(MotionEvent e) {
284 return DirectoryFragment.this.onSingleTapUp(e);
285 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700286 @Override
287 public boolean onDoubleTap(MotionEvent e) {
288 Log.d(TAG, "Handling double tap.");
289 return DirectoryFragment.this.onDoubleTap(e);
290 }
Steve McKayef280152015-06-11 10:10:49 -0700291 };
292
Steve McKay57394872015-08-12 14:48:34 -0700293 mSelectionManager = new MultiSelectManager(
294 mRecView,
295 listener,
296 state.allowMultiple
297 ? MultiSelectManager.MODE_MULTIPLE
298 : MultiSelectManager.MODE_SINGLE);
299
Steve McKayef280152015-06-11 10:10:49 -0700300 mSelectionManager.addCallback(new SelectionModeListener());
301
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700302 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700303 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700304
Steve McKay1f199482015-05-20 15:58:42 -0700305 mFragmentTuner = pickFragmentTuner(state);
306 mClipper = new DocumentClipper(context);
307
Jeff Sharkey9656a532013-09-13 13:42:19 -0700308 if (mType == TYPE_RECENT_OPEN) {
309 // Hide titles when showing recents for picking images/videos
310 mHideGridTitles = MimePredicate.mimeMatches(
311 MimePredicate.VISUAL_MIMES, state.acceptMimes);
312 } else {
313 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
314 }
315
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700316 final ActivityManager am = (ActivityManager) context.getSystemService(
317 Context.ACTIVITY_SERVICE);
318 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
319
Jeff Sharkey46899c82013-08-18 22:26:48 -0700320 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700321 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700322 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700323 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700324
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700325 Uri contentsUri;
326 switch (mType) {
327 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700328 contentsUri = DocumentsContract.buildChildDocumentsUri(
329 doc.authority, doc.documentId);
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_SEARCH:
336 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700337 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700338 if (state.action == ACTION_MANAGE) {
339 contentsUri = DocumentsContract.setManageMode(contentsUri);
340 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700341 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700342 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700343 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700344 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700345 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700346 default:
347 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700348 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700349 }
350
351 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700352 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700353 if (result == null || result.exception != null) {
354 // onBackPressed does a fragment transaction, which can't be done inside
355 // onLoadFinished
356 mHandler.post(new Runnable() {
357 @Override
358 public void run() {
359 final Activity activity = getActivity();
360 if (activity != null) {
361 activity.onBackPressed();
362 }
363 }
364 });
365 return;
366 }
367
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700368 if (!isAdded()) return;
369
Steve McKayef280152015-06-11 10:10:49 -0700370 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700371
372 // Push latest state up to UI
373 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700374 if (result.mode != MODE_UNKNOWN) {
375 state.derivedMode = result.mode;
376 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700377 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700378 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700379
380 updateDisplayState();
381
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700382 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700383 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
384 context instanceof DocumentsActivity) {
385 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700386 }
387
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700388 // Restore any previous instance state
389 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
390 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
391 getView().restoreHierarchyState(container);
392 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700393 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700394 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700395
396 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700397 }
398
399 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700400 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700401 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700402 }
403 };
404
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700405 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700406 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700407
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700408 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700409 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700410 }
411
Jeff Sharkey42d26792013-09-06 13:22:09 -0700412 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700413 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700414 // There's only one request code right now. Replace this with a switch statement or
415 // something more scalable when more codes are added.
416 if (requestCode != REQUEST_COPY_DESTINATION) {
417 return;
418 }
419 if (resultCode == Activity.RESULT_CANCELED || data == null) {
420 // User pressed the back button or otherwise cancelled the destination pick. Don't
421 // proceed with the copy.
422 return;
423 }
424
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900425 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700426 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
427 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700428 }
429
Steve McKayef280152015-06-11 10:10:49 -0700430 private int getEventAdapterPosition(MotionEvent e) {
431 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
432 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
433 }
434
435 private boolean onSingleTapUp(MotionEvent e) {
Steve McKay57394872015-08-12 14:48:34 -0700436 if (Events.isTouchEvent(e) && mSelectionManager.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700437 int position = getEventAdapterPosition(e);
438 if (position != RecyclerView.NO_POSITION) {
439 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700440 }
441 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700442 return false;
443 }
Steve McKayef280152015-06-11 10:10:49 -0700444
Steve McKay93d8ef42015-07-30 12:27:44 -0700445 protected boolean onDoubleTap(MotionEvent e) {
446 if (Events.isMouseEvent(e)) {
447 Log.d(TAG, "Handling double tap from mouse.");
448 int position = getEventAdapterPosition(e);
449 if (position != RecyclerView.NO_POSITION) {
450 return handleViewItem(position);
451 }
452 }
453 return false;
454 }
455
456 private boolean handleViewItem(int position) {
457 final Cursor cursor = mAdapter.getItem(position);
458 checkNotNull(cursor, "Cursor cannot be null.");
459 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
460 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
461 if (isDocumentEnabled(docMimeType, docFlags)) {
462 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKay351a7492015-08-04 10:11:01 -0700463 ((BaseActivity) getActivity()).onDocumentPicked(doc, mAdapter);
Steve McKay93d8ef42015-07-30 12:27:44 -0700464 mSelectionManager.clearSelection();
465 return true;
466 }
Steve McKayef280152015-06-11 10:10:49 -0700467 return false;
468 }
469
Ben Kwaf5858932015-04-07 15:43:39 -0700470 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700471 public void onStop() {
472 super.onStop();
473
474 // Remember last scroll location
475 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
476 getView().saveHierarchyState(container);
477 final State state = getDisplayState(this);
478 state.dirState.put(mStateKey, container);
479 }
480
481 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700482 public void onResume() {
483 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700484 updateDisplayState();
485 }
486
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700487 public void onDisplayStateChanged() {
488 updateDisplayState();
489 }
490
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700491 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700492 // Sort order change always triggers reload; we'll trigger state change
493 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700494 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700495 }
496
497 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700498 final ContentResolver resolver = getActivity().getContentResolver();
499 final State state = getDisplayState(this);
500
501 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
502 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
503
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700504 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700505 final Uri stateUri = RecentsProvider.buildState(
506 root.authority, root.rootId, doc.documentId);
507 final ContentValues values = new ContentValues();
508 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700509
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700510 new AsyncTask<Void, Void, Void>() {
511 @Override
512 protected Void doInBackground(Void... params) {
513 resolver.insert(stateUri, values);
514 return null;
515 }
516 }.execute();
517 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700518
519 // Mode change is just visual change; no need to kick loader, and
520 // deliver change event immediately.
521 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700522 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700523
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700524 updateDisplayState();
525 }
526
527 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700528 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700529
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700530 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700531 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700532 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700533
Steve McKayef280152015-06-11 10:10:49 -0700534 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700535
Steve McKayef280152015-06-11 10:10:49 -0700536 mRecView.setAdapter(mAdapter);
537 }
538
539 /**
540 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
541 * classes as needed.
542 */
543 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700544 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700545
546 final LayoutManager layout;
547 switch (mode) {
548 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700549 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700550 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700551 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700552 }
Steve McKayef280152015-06-11 10:10:49 -0700553 layout = mGridLayout;
554 break;
555 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700556 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700557 if (mListLayout == null) {
558 mListLayout = new LinearLayoutManager(getContext());
559 }
Steve McKayef280152015-06-11 10:10:49 -0700560 layout = mListLayout;
561 break;
562 case MODE_UNKNOWN:
563 default:
564 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700565 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700566
Steve McKayef280152015-06-11 10:10:49 -0700567 mRecView.setLayoutManager(layout);
568 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700569 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700570 }
571
Steve McKayfefcd702015-08-20 16:19:38 +0000572 private int calculateColumnCount() {
573 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
574 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700575 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000576
Steve McKayd57f5fa2015-07-23 16:33:41 -0700577 checkState(mRecView.getWidth() > 0);
578 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000579 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
580
Steve McKayd57f5fa2015-07-23 16:33:41 -0700581 return columnCount;
582 }
583
Steve McKayef280152015-06-11 10:10:49 -0700584 /**
585 * Manages the integration between our ActionMode and MultiSelectManager, initiating
586 * ActionMode when there is a selection, canceling it when there is no selection,
587 * and clearing selection when action mode is explicitly exited by the user.
588 */
589 private final class SelectionModeListener
590 implements MultiSelectManager.Callback, ActionMode.Callback {
591
592 private Selection mSelected = new Selection();
593 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700594 private int mNoDeleteCount = 0;
595 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700596
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700597 @Override
Steve McKayef280152015-06-11 10:10:49 -0700598 public boolean onBeforeItemStateChange(int position, boolean selected) {
599 // Directories and footer items cannot be checked
600 if (selected) {
601 final Cursor cursor = mAdapter.getItem(position);
602 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700603 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
604 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700605 return isDocumentEnabled(docMimeType, docFlags);
606 }
607 return true;
608 }
609
610 @Override
611 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700612
613 final Cursor cursor = mAdapter.getItem(position);
614 checkNotNull(cursor, "Cursor cannot be null.");
615
616 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
617 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
618 mNoDeleteCount += selected ? 1 : -1;
619 }
Steve McKay57394872015-08-12 14:48:34 -0700620 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700621
Steve McKay57394872015-08-12 14:48:34 -0700622 @Override
623 public void onSelectionChanged() {
Steve McKayef280152015-06-11 10:10:49 -0700624 mSelectionManager.getSelection(mSelected);
Steve McKay4f4232d2015-07-22 12:13:46 -0700625 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700626 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
627 if (mActionMode == null) {
628 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
629 mActionMode = getActivity().startActionMode(this);
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900630 getActivity().getWindow().setStatusBarColor(
631 getResources().getColor(R.color.action_mode_status_bar_background));
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700632 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700633 updateActionMenu();
634 } else {
635 if (DEBUG) Log.d(TAG, "Finishing action mode.");
636 if (mActionMode != null) {
637 mActionMode.finish();
638 }
Tomasz Mikolajewski2b6b0662015-07-28 14:59:00 +0900639 getActivity().getWindow().setStatusBarColor(
640 getResources().getColor(R.color.status_bar_background));
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700641 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700642
Steve McKayef280152015-06-11 10:10:49 -0700643 if (mActionMode != null) {
644 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
645 }
646 }
647
648 // Called when the user exits the action mode
649 @Override
650 public void onDestroyActionMode(ActionMode mode) {
651 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
652 mActionMode = null;
653 // clear selection
654 mSelectionManager.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700655 mSelected.clear();
656 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700657 }
658
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700659 @Override
660 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
661 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Steve McKayef280152015-06-11 10:10:49 -0700662 mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
663 return mSelectionManager.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700664 }
665
666 @Override
667 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700668 mMenu = menu;
669 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700670 return true;
671 }
672
Steve McKay4f4232d2015-07-22 12:13:46 -0700673 private void updateActionMenu() {
674 checkNotNull(mMenu);
675 // Delegate update logic to our owning action, since specialized logic is desired.
676 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
677 }
678
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700679 @Override
Steve McKayef280152015-06-11 10:10:49 -0700680 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700681
Steve McKayef280152015-06-11 10:10:49 -0700682 Selection selection = new Selection();
683 mSelectionManager.getSelection(selection);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700684
Jeff Sharkey873daa32013-08-18 17:38:20 -0700685 final int id = item.getItemId();
686 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700687 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700688 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700689 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700690
691 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700692 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700693 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700694 return true;
695
696 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700697 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700698 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700699 return true;
700
Steve McKay1f199482015-05-20 15:58:42 -0700701 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700702 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700703 mode.finish();
704 return true;
705
Steve McKay1f199482015-05-20 15:58:42 -0700706 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700707 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700708 mode.finish();
709 return true;
710
Steve McKay1f199482015-05-20 15:58:42 -0700711 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700712 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700713 mode.finish();
714 return true;
715
Ben Kwa512a6ba2015-03-31 08:15:21 -0700716 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700717 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700718 return true;
719
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700720 } else {
721 return false;
722 }
723 }
Steve McKayef280152015-06-11 10:10:49 -0700724 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700725
Steve McKayef280152015-06-11 10:10:49 -0700726 private static void cancelThumbnailTask(View view) {
727 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
728 if (iconThumb != null) {
729 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
730 if (oldTask != null) {
731 oldTask.preempt();
732 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700733 }
734 }
Steve McKayef280152015-06-11 10:10:49 -0700735 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700736
Steve McKayef280152015-06-11 10:10:49 -0700737 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700738 new GetDocumentsTask() {
739 @Override
740 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700741 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700742 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700743 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700744 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700745 }
746
Steve McKayef280152015-06-11 10:10:49 -0700747 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700748 new GetDocumentsTask() {
749 @Override
750 void onDocumentsReady(List<DocumentInfo> docs) {
751 Intent intent;
752
753 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000754 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700755 for (DocumentInfo doc: docs) {
756 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
757 docsForSend.add(doc);
758 }
759 }
760
761 if (docsForSend.size() == 1) {
762 final DocumentInfo doc = docsForSend.get(0);
763
764 intent = new Intent(Intent.ACTION_SEND);
765 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
766 intent.addCategory(Intent.CATEGORY_DEFAULT);
767 intent.setType(doc.mimeType);
768 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
769
770 } else if (docsForSend.size() > 1) {
771 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
772 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
773 intent.addCategory(Intent.CATEGORY_DEFAULT);
774
Steve McKayfefcd702015-08-20 16:19:38 +0000775 final ArrayList<String> mimeTypes = new ArrayList<>();
776 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700777 for (DocumentInfo doc : docsForSend) {
778 mimeTypes.add(doc.mimeType);
779 uris.add(doc.derivedUri);
780 }
781
782 intent.setType(findCommonMimeType(mimeTypes));
783 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
784
785 } else {
786 return;
787 }
788
789 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
790 startActivity(intent);
791 }
792 }.execute(selected);
793 }
794
Steve McKayef280152015-06-11 10:10:49 -0700795 private void deleteDocuments(final Selection selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700796 final Context context = getActivity();
797 final ContentResolver resolver = context.getContentResolver();
798
Steve McKay9276f3b2015-05-27 16:11:42 -0700799 new GetDocumentsTask() {
800 @Override
801 void onDocumentsReady(List<DocumentInfo> docs) {
802 boolean hadTrouble = false;
803 for (DocumentInfo doc : docs) {
804 if (!doc.isDeleteSupported()) {
805 Log.w(TAG, "Skipping " + doc);
806 hadTrouble = true;
807 continue;
808 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700809
Steve McKay9276f3b2015-05-27 16:11:42 -0700810 ContentProviderClient client = null;
811 try {
812 client = DocumentsApplication.acquireUnstableProviderOrThrow(
813 resolver, doc.derivedUri.getAuthority());
814 DocumentsContract.deleteDocument(client, doc.derivedUri);
815 } catch (Exception e) {
816 Log.w(TAG, "Failed to delete " + doc);
817 hadTrouble = true;
818 } finally {
819 ContentProviderClient.releaseQuietly(client);
820 }
821 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700822
Steve McKay9276f3b2015-05-27 16:11:42 -0700823 if (hadTrouble) {
824 Toast.makeText(
825 context,
826 R.string.toast_failed_delete,
827 Toast.LENGTH_SHORT).show();
828 }
829 }
830 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700831 }
832
Steve McKayef280152015-06-11 10:10:49 -0700833 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700834 // Pop up a dialog to pick a destination. This is inadequate but works for now.
835 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900836 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900837 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900838 Uri.EMPTY,
839 getActivity(),
840 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700841
842 new GetDocumentsTask() {
843 @Override
844 void onDocumentsReady(List<DocumentInfo> docs) {
845 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
846
847 boolean directoryCopy = false;
848 for (DocumentInfo info : docs) {
849 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
850 directoryCopy = true;
851 break;
852 }
853 }
854 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
855 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
856 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900857 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700858 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700859 }
860
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700861 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700862 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700863 }
864
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700865 private static abstract class Footer {
866 private final int mItemViewType;
867
868 public Footer(int itemViewType) {
869 mItemViewType = itemViewType;
870 }
871
872 public abstract View getView(View convertView, ViewGroup parent);
873
874 public int getItemViewType() {
875 return mItemViewType;
876 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700877 }
878
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700879 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700880 public LoadingFooter() {
881 super(1);
882 }
883
Jeff Sharkey20b32272013-09-03 15:25:52 -0700884 @Override
885 public View getView(View convertView, ViewGroup parent) {
886 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700887 final State state = getDisplayState(DirectoryFragment.this);
888
Jeff Sharkey20b32272013-09-03 15:25:52 -0700889 if (convertView == null) {
890 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700891 if (state.derivedMode == MODE_LIST) {
892 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
893 } else if (state.derivedMode == MODE_GRID) {
894 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
895 } else {
896 throw new IllegalStateException();
897 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700898 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700899
Jeff Sharkey20b32272013-09-03 15:25:52 -0700900 return convertView;
901 }
902 }
903
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700904 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700905 private final int mIcon;
906 private final String mMessage;
907
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700908 public MessageFooter(int itemViewType, int icon, String message) {
909 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700910 mIcon = icon;
911 mMessage = message;
912 }
913
914 @Override
915 public View getView(View convertView, ViewGroup parent) {
916 final Context context = parent.getContext();
917 final State state = getDisplayState(DirectoryFragment.this);
918
919 if (convertView == null) {
920 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700921 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700922 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700923 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700924 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
925 } else {
926 throw new IllegalStateException();
927 }
928 }
929
930 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
931 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
932 icon.setImageResource(mIcon);
933 title.setText(mMessage);
934 return convertView;
935 }
936 }
937
Steve McKayef280152015-06-11 10:10:49 -0700938 // Provide a reference to the views for each data item
939 // Complex data items may need more than one view per item, and
940 // you provide access to all the views for a data item in a view holder
941 private static final class DocumentHolder extends RecyclerView.ViewHolder {
942 // each data item is just a string in this case
943 public View view;
944 public String docId; // The stable document id.
945 public DocumentHolder(View view) {
946 super(view);
947 this.view = view;
948 }
949 }
950
Steve McKay351a7492015-08-04 10:11:01 -0700951 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder>
952 implements DocumentContext {
Steve McKayef280152015-06-11 10:10:49 -0700953
954 private final Context mContext;
955 private final LayoutInflater mInflater;
956 // TODO: Bring back support for footers.
Steve McKayfefcd702015-08-20 16:19:38 +0000957 private final List<Footer> mFooters = new ArrayList<>();
Steve McKayef280152015-06-11 10:10:49 -0700958
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 McKay351a7492015-08-04 10:11:01 -07001216 @Override
1217 public Cursor getCursor() {
1218 if (Looper.myLooper() != Looper.getMainLooper()) {
1219 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1220 }
1221 return mCursor;
1222 }
1223
Steve McKayef280152015-06-11 10:10:49 -07001224 private Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001225 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001226 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001227 return mCursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001228 }
Steve McKayef280152015-06-11 10:10:49 -07001229
1230 Log.w(TAG, "Returning null cursor for position: " + position);
1231 if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
1232 if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
1233 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001234 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001235
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001236 @Override
Steve McKayef280152015-06-11 10:10:49 -07001237 public int getItemCount() {
1238 return mCursorCount;
1239 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001240 }
1241
1242 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001243 public int getItemViewType(int position) {
1244 if (position < mCursorCount) {
1245 return 0;
1246 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001247 position -= mCursorCount;
1248 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001249 }
1250 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001251
Steve McKayef280152015-06-11 10:10:49 -07001252 private boolean isEmpty() {
1253 return getItemCount() > 0;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001254 }
1255 }
1256
1257 private static String formatTime(Context context, long when) {
1258 // TODO: DateUtils should make this easier
1259 Time then = new Time();
1260 then.set(when);
1261 Time now = new Time();
1262 now.setToNow();
1263
1264 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1265 | DateUtils.FORMAT_ABBREV_ALL;
1266
1267 if (then.year != now.year) {
1268 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1269 } else if (then.yearDay != now.yearDay) {
1270 flags |= DateUtils.FORMAT_SHOW_DATE;
1271 } else {
1272 flags |= DateUtils.FORMAT_SHOW_TIME;
1273 }
1274
1275 return DateUtils.formatDateTime(context, when, flags);
1276 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001277
1278 private String findCommonMimeType(List<String> mimeTypes) {
1279 String[] commonType = mimeTypes.get(0).split("/");
1280 if (commonType.length != 2) {
1281 return "*/*";
1282 }
1283
1284 for (int i = 1; i < mimeTypes.size(); i++) {
1285 String[] type = mimeTypes.get(i).split("/");
1286 if (type.length != 2) continue;
1287
1288 if (!commonType[1].equals(type[1])) {
1289 commonType[1] = "*";
1290 }
1291
1292 if (!commonType[0].equals(type[0])) {
1293 commonType[0] = "*";
1294 commonType[1] = "*";
1295 break;
1296 }
1297 }
1298
1299 return commonType[0] + "/" + commonType[1];
1300 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001301
1302 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001303 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001304 if (v.isEnabled() == enabled) return;
1305 v.setEnabled(enabled);
1306
1307 if (v instanceof ViewGroup) {
1308 final ViewGroup vg = (ViewGroup) v;
1309 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1310 setEnabledRecursive(vg.getChildAt(i), enabled);
1311 }
1312 }
1313 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001314
1315 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1316 final State state = getDisplayState(DirectoryFragment.this);
1317
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001318 // Directories are always enabled
1319 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1320 return true;
1321 }
1322
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001323 // Read-only files are disabled when creating
1324 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1325 return false;
1326 }
1327
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001328 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1329 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001330
Steve McKayfefcd702015-08-20 16:19:38 +00001331 private List<DocumentInfo> getSelectedDocuments() {
Steve McKayef280152015-06-11 10:10:49 -07001332 Selection sel = mSelectionManager.getSelection(new Selection());
1333 return getItemsAsDocuments(sel);
Steve McKay1f199482015-05-20 15:58:42 -07001334 }
1335
Steve McKayef280152015-06-11 10:10:49 -07001336 private List<DocumentInfo> getItemsAsDocuments(Selection items) {
Steve McKay1f199482015-05-20 15:58:42 -07001337 if (items == null || items.size() == 0) {
1338 return new ArrayList<>(0);
1339 }
1340
1341 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1342 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001343 for (int i = 0; i < size; i++) {
Steve McKayef280152015-06-11 10:10:49 -07001344 final Cursor cursor = mAdapter.getItem(items.get(i));
1345 checkNotNull(cursor, "Cursor cannot be null.");
1346 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1347 docs.add(doc);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001348 }
1349 return docs;
1350 }
1351
Steve McKay1f199482015-05-20 15:58:42 -07001352 private void copyFromClipboard() {
1353 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1354
1355 @Override
1356 protected List<DocumentInfo> doInBackground(Void... params) {
1357 return mClipper.getClippedDocuments();
1358 }
1359
1360 @Override
1361 protected void onPostExecute(List<DocumentInfo> docs) {
1362 DocumentInfo destination =
1363 ((BaseActivity) getActivity()).getCurrentDirectory();
1364 copyDocuments(docs, destination);
1365 }
1366 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001367 }
1368
Steve McKay1f199482015-05-20 15:58:42 -07001369 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001370 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001371 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001372
Steve McKay1f199482015-05-20 15:58:42 -07001373 @Override
1374 protected List<DocumentInfo> doInBackground(Void... params) {
1375 return mClipper.getDocumentsFromClipData(clipData);
1376 }
1377
1378 @Override
1379 protected void onPostExecute(List<DocumentInfo> docs) {
1380 copyDocuments(docs, destination);
1381 }
1382 }.execute();
1383 }
1384
1385 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1386 if (!canCopy(docs, destination)) {
1387 Toast.makeText(
1388 getActivity(),
1389 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001390 return;
1391 }
1392
Steve McKay1f199482015-05-20 15:58:42 -07001393 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001394 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001395 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001396
Steve McKay1f199482015-05-20 15:58:42 -07001397 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001398 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001399 if (destination != null) {
1400 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001401 tmpStack.addAll(curStack);
1402 } else {
1403 tmpStack = curStack;
1404 }
1405
Steve McKay1f199482015-05-20 15:58:42 -07001406 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001407 }
1408
1409 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1410 Context context = getActivity();
1411 final ContentResolver resolver = context.getContentResolver();
1412 ClipData clipData = null;
1413 for (DocumentInfo doc : docs) {
1414 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1415 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001416 // TODO: figure out what this string should be.
1417 // Currently it is not displayed anywhere in the UI, but this might change.
1418 final String label = "";
1419 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001420 } else {
1421 // TODO: update list of mime types in ClipData.
1422 clipData.addItem(new ClipData.Item(uri));
1423 }
1424 }
1425 return clipData;
1426 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001427
Steve McKay1f199482015-05-20 15:58:42 -07001428 void copySelectedToClipboard() {
Steve McKayef280152015-06-11 10:10:49 -07001429 Selection sel = mSelectionManager.getSelection(new Selection());
1430 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001431 }
Steve McKay0599a442015-05-05 14:50:00 -07001432
Steve McKayef280152015-06-11 10:10:49 -07001433 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001434 new GetDocumentsTask() {
1435 @Override
1436 void onDocumentsReady(List<DocumentInfo> docs) {
1437 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001438 Activity activity = getActivity();
1439 Toast.makeText(activity,
1440 activity.getResources().getQuantityString(
1441 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1442 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001443 }
Steve McKayef280152015-06-11 10:10:49 -07001444 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001445 }
1446
1447 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001448 copyFromClipboard();
1449 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001450 }
1451
Steve McKay0599a442015-05-05 14:50:00 -07001452 /**
1453 * Returns true if the list of files can be copied to destination. Note that this
1454 * is a policy check only. Currently the method does not attempt to verify
1455 * available space or any other environmental aspects possibly resulting in
1456 * failure to copy.
1457 *
1458 * @return true if the list of files can be copied to destination.
1459 */
1460 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1461 BaseActivity activity = (BaseActivity)getActivity();
1462
1463 final RootInfo root = activity.getCurrentRoot();
1464
1465 // Can't copy folders to Downloads.
1466 if (root.isDownloads()) {
1467 for (DocumentInfo docs : files) {
1468 if (docs.isDirectory()) {
1469 return false;
1470 }
1471 }
1472 }
1473
1474 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1475 }
1476
1477 void selectAllFiles() {
Steve McKay9459a7c2015-07-24 13:14:20 -07001478 boolean changed = mSelectionManager.setItemsSelected(0, mAdapter.getItemCount(), true);
1479 if (changed) {
1480 updateDisplayState();
1481 }
Steve McKay0599a442015-05-05 14:50:00 -07001482 }
1483
Steve McKayef280152015-06-11 10:10:49 -07001484 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001485 // Listen for drops on non-directory items and empty space.
1486 view.setOnDragListener(mOnDragListener);
1487 }
1488
1489 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1490 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1491 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1492 // Make a directory item a drop target. Drop on non-directories and empty space
1493 // is handled at the list/grid view level.
1494 view.setOnDragListener(mOnDragListener);
1495 }
1496
1497 // Temporary: attaching the listener to the title only.
1498 // Attaching to the entire item conflicts with the item long click handler responsible
1499 // for item selection.
1500 final View title = view.findViewById(android.R.id.title);
1501 title.setOnLongClickListener(mLongClickListener);
1502 }
1503
1504 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1505 @Override
1506 public boolean onDrag(View v, DragEvent event) {
1507 switch (event.getAction()) {
1508 case DragEvent.ACTION_DRAG_STARTED:
1509 // TODO: Check if the event contains droppable data.
1510 return true;
1511
1512 // TODO: Highlight potential drop target directory?
1513 // TODO: Expand drop target directory on hover?
1514 case DragEvent.ACTION_DRAG_ENTERED:
1515 case DragEvent.ACTION_DRAG_LOCATION:
1516 case DragEvent.ACTION_DRAG_EXITED:
1517 case DragEvent.ACTION_DRAG_ENDED:
1518 return true;
1519
1520 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001521 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001522 DocumentInfo dstDir = null;
1523 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1524 Cursor dstCursor = mAdapter.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001525 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001526 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1527 // TODO: Do not drop into the directory where the documents came from.
1528 }
1529 copyFromClipData(event.getClipData(), dstDir);
1530 return true;
1531 }
1532 return false;
1533 }
1534 };
1535
1536 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1537 @Override
1538 public boolean onLongClick(View v) {
1539 final List<DocumentInfo> docs = getDraggableDocuments(v);
1540 if (docs.isEmpty()) {
1541 return false;
1542 }
1543 v.startDrag(
1544 getClipDataFromDocuments(docs),
1545 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1546 null,
1547 View.DRAG_FLAG_GLOBAL
1548 );
1549 return true;
1550 }
1551 };
1552
1553 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001554 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001555 if (position == android.widget.AdapterView.INVALID_POSITION) {
1556 return Collections.EMPTY_LIST;
1557 }
1558
1559 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1560 if (!selectedDocs.isEmpty()) {
Steve McKayef280152015-06-11 10:10:49 -07001561 if (!mSelectionManager.getSelection().contains(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001562 // There is a selection that does not include the current item, drag nothing.
1563 return Collections.EMPTY_LIST;
1564 }
1565 return selectedDocs;
1566 }
1567
1568 final Cursor cursor = mAdapter.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001569 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001570 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001571
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001572 return Lists.newArrayList(doc);
1573 }
1574
1575 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1576 if (docs.size() == 1) {
1577 final DocumentInfo doc = docs.get(0);
1578 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1579 doc.mimeType, doc.icon, getDisplayState(this));
1580 }
1581 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1582 }
1583
1584 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1585 String docMimeType, int docIcon, State state) {
1586 if (docIcon != 0) {
1587 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1588 } else {
1589 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1590 state.derivedMode);
1591 }
1592 }
1593
Steve McKayef280152015-06-11 10:10:49 -07001594 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1595 implements Preemptable {
1596 private final Uri mUri;
1597 private final ImageView mIconMime;
1598 private final ImageView mIconThumb;
1599 private final Point mThumbSize;
1600 private final float mTargetAlpha;
1601 private final CancellationSignal mSignal;
1602
1603 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1604 float targetAlpha) {
1605 mUri = uri;
1606 mIconMime = iconMime;
1607 mIconThumb = iconThumb;
1608 mThumbSize = thumbSize;
1609 mTargetAlpha = targetAlpha;
1610 mSignal = new CancellationSignal();
1611 }
1612
1613 @Override
1614 public void preempt() {
1615 cancel(false);
1616 mSignal.cancel();
1617 }
1618
1619 @Override
1620 protected Bitmap doInBackground(Uri... params) {
1621 if (isCancelled()) return null;
1622
1623 final Context context = mIconThumb.getContext();
1624 final ContentResolver resolver = context.getContentResolver();
1625
1626 ContentProviderClient client = null;
1627 Bitmap result = null;
1628 try {
1629 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1630 resolver, mUri.getAuthority());
1631 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1632 if (result != null) {
1633 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1634 context, mThumbSize);
1635 thumbs.put(mUri, result);
1636 }
1637 } catch (Exception e) {
1638 if (!(e instanceof OperationCanceledException)) {
1639 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1640 }
1641 } finally {
1642 ContentProviderClient.releaseQuietly(client);
1643 }
1644 return result;
1645 }
1646
1647 @Override
1648 protected void onPostExecute(Bitmap result) {
1649 if (mIconThumb.getTag() == this && result != null) {
1650 mIconThumb.setTag(null);
1651 mIconThumb.setImageBitmap(result);
1652
1653 mIconMime.setAlpha(mTargetAlpha);
1654 mIconMime.animate().alpha(0f).start();
1655 mIconThumb.setAlpha(0f);
1656 mIconThumb.animate().alpha(mTargetAlpha).start();
1657 }
1658 }
1659 }
1660
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001661 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1662
1663 private final Drawable mShadow;
1664
1665 private final int mShadowDimension;
1666
1667 public DrawableShadowBuilder(Drawable shadow) {
1668 mShadow = shadow;
1669 mShadowDimension = getResources().getDimensionPixelSize(
1670 R.dimen.drag_shadow_size);
1671 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1672 }
1673
1674 public void onProvideShadowMetrics(
1675 Point shadowSize, Point shadowTouchPoint) {
1676 shadowSize.set(mShadowDimension, mShadowDimension);
1677 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1678 }
1679
1680 public void onDrawShadow(Canvas canvas) {
1681 mShadow.draw(canvas);
1682 }
1683 }
Steve McKay1f199482015-05-20 15:58:42 -07001684
1685 private FragmentTuner pickFragmentTuner(final State state) {
1686 return state.action == ACTION_BROWSE_ALL
Steve McKay0fbfc652015-08-20 16:48:49 -07001687 ? new FilesTuner()
Steve McKay1f199482015-05-20 15:58:42 -07001688 : new DefaultTuner(state);
1689 }
1690
1691 /**
1692 * Interface for specializing the Fragment for the "host" Activity.
1693 * Feel free to expand the role of this class to handle other specializations.
1694 */
1695 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001696 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001697 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001698 }
1699
1700 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001701 * Abstract task providing support for loading documents *off*
1702 * the main thread. And if it isn't obvious, creating a list
1703 * of documents (especially large lists) can be pretty expensive.
1704 */
1705 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001706 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001707 @Override
Steve McKayef280152015-06-11 10:10:49 -07001708 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001709 return getItemsAsDocuments(selected[0]);
1710 }
1711
1712 @Override
1713 protected final void onPostExecute(List<DocumentInfo> docs) {
1714 onDocumentsReady(docs);
1715 }
1716
1717 abstract void onDocumentsReady(List<DocumentInfo> docs);
1718 }
1719
1720 /**
Steve McKay1f199482015-05-20 15:58:42 -07001721 * Provides support for Platform specific specializations of DirectoryFragment.
1722 */
1723 private static final class DefaultTuner implements FragmentTuner {
1724
1725 private final State mState;
1726
1727 public DefaultTuner(State state) {
1728 mState = state;
1729 }
1730
1731 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001732 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001733 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1734
1735 final MenuItem open = menu.findItem(R.id.menu_open);
1736 final MenuItem share = menu.findItem(R.id.menu_share);
1737 final MenuItem delete = menu.findItem(R.id.menu_delete);
1738 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1739 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1740 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1741
1742 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1743 || mState.action == ACTION_BROWSE);
1744
1745 open.setVisible(!manageOrBrowse);
1746 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001747 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001748 // Disable copying from the Recents view.
1749 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1750 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1751
Steve McKay0fbfc652015-08-20 16:48:49 -07001752 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001753 copyToClipboard.setVisible(false);
1754 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001755
1756 @Override
1757 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001758 }
1759
1760 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001761 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001762 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001763 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001764 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001765 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001766 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001767 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001768 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1769
1770 menu.findItem(R.id.menu_open).setVisible(false);
1771 menu.findItem(R.id.menu_copy_to).setVisible(false);
1772 menu.findItem(R.id.menu_move_to).setVisible(false);
1773 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001774
1775 @Override
1776 public void afterActivityCreated(DirectoryFragment fragment) {
1777 new BandSelectManager(fragment.mRecView, fragment.mSelectionManager);
1778 }
Steve McKay1f199482015-05-20 15:58:42 -07001779 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001780}