blob: 3202876569e0f10a8b6f3968bc3a0e4f76531a23 [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;
Ben Kwa24be5d32015-08-27 16:04:46 -070064import android.support.annotation.Nullable;
Ben Kwa7461a952015-09-01 11:03:01 -070065import android.support.annotation.VisibleForTesting;
Ben Kwa91923182015-08-27 16:06:33 -070066import android.support.design.widget.Snackbar;
Steve McKayef280152015-06-11 10:10:49 -070067import android.support.v7.widget.GridLayoutManager;
68import android.support.v7.widget.LinearLayoutManager;
69import android.support.v7.widget.RecyclerView;
70import android.support.v7.widget.RecyclerView.LayoutManager;
Steve McKayef280152015-06-11 10:10:49 -070071import android.support.v7.widget.RecyclerView.RecyclerListener;
72import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070073import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070074import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070075import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070076import android.text.format.Time;
77import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070078import android.util.SparseArray;
Ben Kwa91923182015-08-27 16:06:33 -070079import android.util.SparseBooleanArray;
Ben Kwa0574b182015-09-08 07:31:19 -070080import android.util.TypedValue;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070081import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070082import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070085import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070086import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070087import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070088import android.view.View;
Steve McKayd57f5fa2015-07-23 16:33:41 -070089import android.view.View.OnLayoutChangeListener;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070090import android.view.ViewGroup;
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -070091import android.view.ViewParent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070092import android.widget.ImageView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070093import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070094import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070095
Steve McKay351a7492015-08-04 10:11:01 -070096import com.android.documentsui.BaseActivity.DocumentContext;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070097import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070098import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070099import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700100import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700101import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900102import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700103import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -0700104import com.android.internal.util.Preconditions;
Steve McKayfefcd702015-08-20 16:19:38 +0000105import com.google.common.collect.Lists;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700106
107import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700108import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700109import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700110
111/**
112 * Display the documents inside a single directory.
113 */
114public class DirectoryFragment extends Fragment {
115
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700116 public static final int TYPE_NORMAL = 1;
117 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700118 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700119
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700120 public static final int ANIM_NONE = 1;
121 public static final int ANIM_SIDE = 2;
122 public static final int ANIM_DOWN = 3;
123 public static final int ANIM_UP = 4;
124
Ben Kwaf5858932015-04-07 15:43:39 -0700125 public static final int REQUEST_COPY_DESTINATION = 1;
126
Steve McKayef280152015-06-11 10:10:49 -0700127 private static final int LOADER_ID = 42;
128 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700129 private static final boolean DEBUG_ENABLE_DND = false;
130
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700132 private static final String EXTRA_ROOT = "root";
133 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700134 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700135 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700136
Ben Kwa7461a952015-09-01 11:03:01 -0700137 private Model mModel;
Ben Kwa24be5d32015-08-27 16:04:46 -0700138
Steve McKayef280152015-06-11 10:10:49 -0700139 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140
Steve McKayef280152015-06-11 10:10:49 -0700141 private View mEmptyView;
142 private RecyclerView mRecView;
143
144 private int mType = TYPE_NORMAL;
145 private String mStateKey;
146
147 private int mLastMode = MODE_UNKNOWN;
148 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
149 private boolean mLastShowSize;
150 private boolean mHideGridTitles;
151 private boolean mSvelteRecents;
152 private Point mThumbSize;
153 private DocumentsAdapter mAdapter;
154 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700155 private FragmentTuner mFragmentTuner;
156 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700157 // These are lazily initialized.
Steve McKayd57f5fa2015-07-23 16:33:41 -0700158 private LinearLayoutManager mListLayout;
159 private GridLayoutManager mGridLayout;
Steve McKayd57f5fa2015-07-23 16:33:41 -0700160 private int mColumnCount = 1; // This will get updated when layout changes.
Steve McKay1f199482015-05-20 15:58:42 -0700161
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700162 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
163 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700164 }
165
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700166 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
167 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700168 }
169
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700170 public static void showRecentsOpen(FragmentManager fm, int anim) {
171 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700172 }
173
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700174 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
175 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700176 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700177 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700178 args.putParcelable(EXTRA_ROOT, root);
179 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700180 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700181
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700182 final FragmentTransaction ft = fm.beginTransaction();
183 switch (anim) {
184 case ANIM_SIDE:
185 args.putBoolean(EXTRA_IGNORE_STATE, true);
186 break;
187 case ANIM_DOWN:
188 args.putBoolean(EXTRA_IGNORE_STATE, true);
189 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
190 break;
191 case ANIM_UP:
192 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
193 break;
194 }
195
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700196 final DirectoryFragment fragment = new DirectoryFragment();
197 fragment.setArguments(args);
198
Jeff Sharkey76112212013-08-06 11:26:10 -0700199 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700200 ft.commitAllowingStateLoss();
201 }
202
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700203 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
204 final StringBuilder builder = new StringBuilder();
205 builder.append(root != null ? root.authority : "null").append(';');
206 builder.append(root != null ? root.rootId : "null").append(';');
207 builder.append(doc != null ? doc.documentId : "null");
208 return builder.toString();
209 }
210
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700211 public static DirectoryFragment get(FragmentManager fm) {
212 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700213 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700214 }
215
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700216 @Override
217 public View onCreateView(
218 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
219 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700220 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700221 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
222
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700223 mEmptyView = view.findViewById(android.R.id.empty);
224
Steve McKayef280152015-06-11 10:10:49 -0700225 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
226 mRecView.setRecyclerListener(
227 new RecyclerListener() {
228 @Override
229 public void onViewRecycled(ViewHolder holder) {
230 cancelThumbnailTask(holder.itemView);
231 }
232 });
Steve McKay8e258c62015-05-06 14:27:57 -0700233
Steve McKayd57f5fa2015-07-23 16:33:41 -0700234 // TODO: Rather than update columns on layout changes, push this
235 // code (or something like it) into GridLayoutManager.
236 mRecView.addOnLayoutChangeListener(
237 new OnLayoutChangeListener() {
238
239 @Override
240 public void onLayoutChange(
241 View v, int left, int top, int right, int bottom, int oldLeft,
242 int oldTop, int oldRight, int oldBottom) {
Steve McKayfefcd702015-08-20 16:19:38 +0000243 mColumnCount = calculateColumnCount();
Steve McKayd57f5fa2015-07-23 16:33:41 -0700244 if (mGridLayout != null) {
245 mGridLayout.setSpanCount(mColumnCount);
246 }
247 }
248 });
249
250 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700251 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700252 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700253 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700254
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255 return view;
256 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700257
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700258 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700259 public void onDestroyView() {
260 super.onDestroyView();
261
262 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700263 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700265 final View view = mRecView.getChildAt(i);
266 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700267 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700268
Steve McKayef280152015-06-11 10:10:49 -0700269 // Clear any outstanding selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700270 mModel.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700271 }
272
273 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700274 public void onActivityCreated(Bundle savedInstanceState) {
275 super.onActivityCreated(savedInstanceState);
276
277 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700278 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700279
Jeff Sharkey9656a532013-09-13 13:42:19 -0700280 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
281 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
282
Steve McKayef280152015-06-11 10:10:49 -0700283 mAdapter = new DocumentsAdapter(context);
284 mRecView.setAdapter(mAdapter);
285
286 GestureDetector.SimpleOnGestureListener listener =
287 new GestureDetector.SimpleOnGestureListener() {
288 @Override
289 public boolean onSingleTapUp(MotionEvent e) {
290 return DirectoryFragment.this.onSingleTapUp(e);
291 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700292 @Override
293 public boolean onDoubleTap(MotionEvent e) {
294 Log.d(TAG, "Handling double tap.");
295 return DirectoryFragment.this.onDoubleTap(e);
296 }
Steve McKayef280152015-06-11 10:10:49 -0700297 };
298
Ben Kwa24be5d32015-08-27 16:04:46 -0700299 // TODO: instead of inserting the view into the constructor, extract listener-creation code
300 // and set the listener on the view after the fact. Then the view doesn't need to be passed
301 // into the selection manager which is passed into the model.
302 MultiSelectManager selMgr= new MultiSelectManager(
Steve McKay57394872015-08-12 14:48:34 -0700303 mRecView,
304 listener,
305 state.allowMultiple
306 ? MultiSelectManager.MODE_MULTIPLE
307 : MultiSelectManager.MODE_SINGLE);
Ben Kwa24be5d32015-08-27 16:04:46 -0700308 selMgr.addCallback(new SelectionModeListener());
Ben Kwa7461a952015-09-01 11:03:01 -0700309
310 mModel = new Model(context, selMgr);
Ben Kwa24be5d32015-08-27 16:04:46 -0700311 mModel.setSelectionManager(selMgr);
Ben Kwa7461a952015-09-01 11:03:01 -0700312 mModel.addUpdateListener(mAdapter);
Steve McKayef280152015-06-11 10:10:49 -0700313
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700314 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700315 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700316
Steve McKay1f199482015-05-20 15:58:42 -0700317 mFragmentTuner = pickFragmentTuner(state);
318 mClipper = new DocumentClipper(context);
319
Jeff Sharkey9656a532013-09-13 13:42:19 -0700320 if (mType == TYPE_RECENT_OPEN) {
321 // Hide titles when showing recents for picking images/videos
322 mHideGridTitles = MimePredicate.mimeMatches(
323 MimePredicate.VISUAL_MIMES, state.acceptMimes);
324 } else {
325 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
326 }
327
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700328 final ActivityManager am = (ActivityManager) context.getSystemService(
329 Context.ACTIVITY_SERVICE);
330 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
331
Jeff Sharkey46899c82013-08-18 22:26:48 -0700332 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700333 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700334 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700335 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700336
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700337 Uri contentsUri;
338 switch (mType) {
339 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700340 contentsUri = DocumentsContract.buildChildDocumentsUri(
341 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700342 if (state.action == ACTION_MANAGE) {
343 contentsUri = DocumentsContract.setManageMode(contentsUri);
344 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700345 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700346 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700347 case TYPE_SEARCH:
348 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700349 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700350 if (state.action == ACTION_MANAGE) {
351 contentsUri = DocumentsContract.setManageMode(contentsUri);
352 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700353 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700354 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700355 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700356 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700357 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700358 default:
359 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700360 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700361 }
362
363 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700364 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700365 if (result == null || result.exception != null) {
366 // onBackPressed does a fragment transaction, which can't be done inside
367 // onLoadFinished
368 mHandler.post(new Runnable() {
369 @Override
370 public void run() {
371 final Activity activity = getActivity();
372 if (activity != null) {
373 activity.onBackPressed();
374 }
375 }
376 });
377 return;
378 }
379
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700380 if (!isAdded()) return;
381
Ben Kwa24be5d32015-08-27 16:04:46 -0700382 mModel.update(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700383
384 // Push latest state up to UI
385 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700386 if (result.mode != MODE_UNKNOWN) {
387 state.derivedMode = result.mode;
388 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700389 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700390 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700391
392 updateDisplayState();
393
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700394 // When launched into empty recents, show drawer
Ben Kwa24be5d32015-08-27 16:04:46 -0700395 if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
Steve McKayb68dd222015-04-20 17:18:15 -0700396 context instanceof DocumentsActivity) {
397 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700398 }
399
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700400 // Restore any previous instance state
401 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
402 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
403 getView().restoreHierarchyState(container);
404 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700405 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700406 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700407
408 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700409 }
410
411 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700412 public void onLoaderReset(Loader<DirectoryResult> loader) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700413 mModel.update(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700414 }
415 };
416
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700417 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700418 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700419
Kyle Horimoto426bd0d2015-07-29 15:33:49 -0700420 mFragmentTuner.afterActivityCreated(this);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700421 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700422 }
423
Jeff Sharkey42d26792013-09-06 13:22:09 -0700424 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700425 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700426 // There's only one request code right now. Replace this with a switch statement or
427 // something more scalable when more codes are added.
428 if (requestCode != REQUEST_COPY_DESTINATION) {
429 return;
430 }
431 if (resultCode == Activity.RESULT_CANCELED || data == null) {
432 // User pressed the back button or otherwise cancelled the destination pick. Don't
433 // proceed with the copy.
434 return;
435 }
436
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900437 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700438 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
439 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700440 }
441
Steve McKayef280152015-06-11 10:10:49 -0700442 private int getEventAdapterPosition(MotionEvent e) {
443 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
444 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
445 }
446
447 private boolean onSingleTapUp(MotionEvent e) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700448 if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) {
Steve McKay93d8ef42015-07-30 12:27:44 -0700449 int position = getEventAdapterPosition(e);
450 if (position != RecyclerView.NO_POSITION) {
451 return handleViewItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700452 }
453 }
Steve McKay93d8ef42015-07-30 12:27:44 -0700454 return false;
455 }
Steve McKayef280152015-06-11 10:10:49 -0700456
Steve McKay93d8ef42015-07-30 12:27:44 -0700457 protected boolean onDoubleTap(MotionEvent e) {
458 if (Events.isMouseEvent(e)) {
459 Log.d(TAG, "Handling double tap from mouse.");
460 int position = getEventAdapterPosition(e);
461 if (position != RecyclerView.NO_POSITION) {
462 return handleViewItem(position);
463 }
464 }
465 return false;
466 }
467
468 private boolean handleViewItem(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700469 final Cursor cursor = mModel.getItem(position);
Steve McKay93d8ef42015-07-30 12:27:44 -0700470 checkNotNull(cursor, "Cursor cannot be null.");
471 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
472 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
473 if (isDocumentEnabled(docMimeType, docFlags)) {
474 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Ben Kwa24be5d32015-08-27 16:04:46 -0700475 ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel);
476 mModel.clearSelection();
Steve McKay93d8ef42015-07-30 12:27:44 -0700477 return true;
478 }
Steve McKayef280152015-06-11 10:10:49 -0700479 return false;
480 }
481
Ben Kwaf5858932015-04-07 15:43:39 -0700482 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700483 public void onStop() {
484 super.onStop();
485
486 // Remember last scroll location
487 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
488 getView().saveHierarchyState(container);
489 final State state = getDisplayState(this);
490 state.dirState.put(mStateKey, container);
491 }
492
493 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700494 public void onResume() {
495 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700496 updateDisplayState();
497 }
498
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700499 public void onDisplayStateChanged() {
500 updateDisplayState();
501 }
502
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700503 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700504 // Sort order change always triggers reload; we'll trigger state change
505 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700506 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700507 }
508
509 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700510 final ContentResolver resolver = getActivity().getContentResolver();
511 final State state = getDisplayState(this);
512
513 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
514 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
515
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700516 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700517 final Uri stateUri = RecentsProvider.buildState(
518 root.authority, root.rootId, doc.documentId);
519 final ContentValues values = new ContentValues();
520 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700521
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700522 new AsyncTask<Void, Void, Void>() {
523 @Override
524 protected Void doInBackground(Void... params) {
525 resolver.insert(stateUri, values);
526 return null;
527 }
528 }.execute();
529 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700530
531 // Mode change is just visual change; no need to kick loader, and
532 // deliver change event immediately.
533 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700534 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700535
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700536 updateDisplayState();
537 }
538
539 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700540 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700541
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700542 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700543 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700544 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700545
Steve McKayef280152015-06-11 10:10:49 -0700546 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700547
Steve McKayef280152015-06-11 10:10:49 -0700548 mRecView.setAdapter(mAdapter);
549 }
550
551 /**
552 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
553 * classes as needed.
554 */
555 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700556 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700557
558 final LayoutManager layout;
559 switch (mode) {
560 case MODE_GRID:
Steve McKayef280152015-06-11 10:10:49 -0700561 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Steve McKaya9be7182015-07-22 16:03:35 -0700562 if (mGridLayout == null) {
Steve McKayd57f5fa2015-07-23 16:33:41 -0700563 mGridLayout = new GridLayoutManager(getContext(), mColumnCount );
Steve McKaya9be7182015-07-22 16:03:35 -0700564 }
Steve McKayef280152015-06-11 10:10:49 -0700565 layout = mGridLayout;
566 break;
567 case MODE_LIST:
Steve McKaya9be7182015-07-22 16:03:35 -0700568 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Steve McKayef280152015-06-11 10:10:49 -0700569 if (mListLayout == null) {
570 mListLayout = new LinearLayoutManager(getContext());
571 }
Steve McKayef280152015-06-11 10:10:49 -0700572 layout = mListLayout;
573 break;
574 case MODE_UNKNOWN:
575 default:
576 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700577 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700578
Steve McKayef280152015-06-11 10:10:49 -0700579 mRecView.setLayoutManager(layout);
Kyle Horimoto2da6e4a2015-08-27 16:44:00 -0700580 // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of
581 // imperatively calling this function.
Steve McKay9058e042015-09-01 12:31:24 -0700582 mModel.mSelectionManager.handleLayoutChanged();
Steve McKayef280152015-06-11 10:10:49 -0700583 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700584 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700585 }
586
Steve McKayfefcd702015-08-20 16:19:38 +0000587 private int calculateColumnCount() {
588 int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width);
589 int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin);
Steve McKayd57f5fa2015-07-23 16:33:41 -0700590 int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
Steve McKayfefcd702015-08-20 16:19:38 +0000591
Steve McKayd57f5fa2015-07-23 16:33:41 -0700592 checkState(mRecView.getWidth() > 0);
593 int columnCount = Math.max(1,
Steve McKayfefcd702015-08-20 16:19:38 +0000594 (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin));
595
Steve McKayd57f5fa2015-07-23 16:33:41 -0700596 return columnCount;
597 }
598
Steve McKayef280152015-06-11 10:10:49 -0700599 /**
600 * Manages the integration between our ActionMode and MultiSelectManager, initiating
601 * ActionMode when there is a selection, canceling it when there is no selection,
602 * and clearing selection when action mode is explicitly exited by the user.
603 */
604 private final class SelectionModeListener
605 implements MultiSelectManager.Callback, ActionMode.Callback {
606
607 private Selection mSelected = new Selection();
608 private ActionMode mActionMode;
Steve McKay4f4232d2015-07-22 12:13:46 -0700609 private int mNoDeleteCount = 0;
610 private Menu mMenu;
Steve McKayef280152015-06-11 10:10:49 -0700611
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700612 @Override
Steve McKayef280152015-06-11 10:10:49 -0700613 public boolean onBeforeItemStateChange(int position, boolean selected) {
614 // Directories and footer items cannot be checked
615 if (selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700616 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700617 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700618 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
619 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700620 return isDocumentEnabled(docMimeType, docFlags);
621 }
622 return true;
623 }
624
625 @Override
626 public void onItemStateChanged(int position, boolean selected) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700627
Ben Kwa24be5d32015-08-27 16:04:46 -0700628 final Cursor cursor = mModel.getItem(position);
Steve McKay4f4232d2015-07-22 12:13:46 -0700629 checkNotNull(cursor, "Cursor cannot be null.");
630
631 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
632 if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) {
633 mNoDeleteCount += selected ? 1 : -1;
634 }
Steve McKay57394872015-08-12 14:48:34 -0700635 }
Steve McKay4f4232d2015-07-22 12:13:46 -0700636
Steve McKay57394872015-08-12 14:48:34 -0700637 @Override
638 public void onSelectionChanged() {
Ben Kwa24be5d32015-08-27 16:04:46 -0700639 mModel.getSelection(mSelected);
Ben Kwafe18c1b2015-09-11 15:40:18 -0700640 TypedValue color = new TypedValue();
Steve McKay4f4232d2015-07-22 12:13:46 -0700641 if (mSelected.size() > 0) {
Steve McKayef280152015-06-11 10:10:49 -0700642 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
643 if (mActionMode == null) {
644 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
645 mActionMode = getActivity().startActionMode(this);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700646 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700647 getActivity().getTheme().resolveAttribute(
648 R.attr.colorActionMode, color, true);
Steve McKay4f4232d2015-07-22 12:13:46 -0700649 updateActionMenu();
650 } else {
651 if (DEBUG) Log.d(TAG, "Finishing action mode.");
652 if (mActionMode != null) {
653 mActionMode.finish();
654 }
Ben Kwa0574b182015-09-08 07:31:19 -0700655 getActivity().getTheme().resolveAttribute(
656 android.R.attr.colorPrimaryDark, color, true);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700657 }
Ben Kwafe18c1b2015-09-11 15:40:18 -0700658 getActivity().getWindow().setStatusBarColor(color.data);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700659
Steve McKayef280152015-06-11 10:10:49 -0700660 if (mActionMode != null) {
661 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
662 }
663 }
664
665 // Called when the user exits the action mode
666 @Override
667 public void onDestroyActionMode(ActionMode mode) {
668 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
669 mActionMode = null;
670 // clear selection
Ben Kwa24be5d32015-08-27 16:04:46 -0700671 mModel.clearSelection();
Steve McKay4f4232d2015-07-22 12:13:46 -0700672 mSelected.clear();
673 mNoDeleteCount = 0;
Steve McKayef280152015-06-11 10:10:49 -0700674 }
675
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700676 @Override
677 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
678 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Ben Kwa24be5d32015-08-27 16:04:46 -0700679 mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size()));
680 return mModel.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700681 }
682
683 @Override
684 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay4f4232d2015-07-22 12:13:46 -0700685 mMenu = menu;
686 updateActionMenu();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700687 return true;
688 }
689
Steve McKay4f4232d2015-07-22 12:13:46 -0700690 private void updateActionMenu() {
691 checkNotNull(mMenu);
692 // Delegate update logic to our owning action, since specialized logic is desired.
693 mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0);
694 }
695
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700696 @Override
Steve McKayef280152015-06-11 10:10:49 -0700697 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700698
Ben Kwa24be5d32015-08-27 16:04:46 -0700699 Selection selection = mModel.getSelection(new Selection());
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700700
Jeff Sharkey873daa32013-08-18 17:38:20 -0700701 final int id = item.getItemId();
702 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700703 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700704 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700705 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700706
707 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700708 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700709 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700710 return true;
711
712 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700713 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700714 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700715 return true;
716
Steve McKay1f199482015-05-20 15:58:42 -0700717 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700718 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700719 mode.finish();
720 return true;
721
Steve McKay1f199482015-05-20 15:58:42 -0700722 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700723 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700724 mode.finish();
725 return true;
726
Steve McKay1f199482015-05-20 15:58:42 -0700727 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700728 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700729 mode.finish();
730 return true;
731
Ben Kwa512a6ba2015-03-31 08:15:21 -0700732 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700733 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700734 return true;
735
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700736 } else {
737 return false;
738 }
739 }
Steve McKayef280152015-06-11 10:10:49 -0700740 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700741
Steve McKayef280152015-06-11 10:10:49 -0700742 private static void cancelThumbnailTask(View view) {
743 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
744 if (iconThumb != null) {
745 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
746 if (oldTask != null) {
747 oldTask.preempt();
748 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700749 }
750 }
Steve McKayef280152015-06-11 10:10:49 -0700751 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700752
Steve McKayef280152015-06-11 10:10:49 -0700753 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700754 new GetDocumentsTask() {
755 @Override
756 void onDocumentsReady(List<DocumentInfo> docs) {
Steve McKay0fbfc652015-08-20 16:48:49 -0700757 // TODO: Implement support in Files activity for opening multiple docs.
Steve McKay9276f3b2015-05-27 16:11:42 -0700758 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700759 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700760 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700761 }
762
Steve McKayef280152015-06-11 10:10:49 -0700763 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700764 new GetDocumentsTask() {
765 @Override
766 void onDocumentsReady(List<DocumentInfo> docs) {
767 Intent intent;
768
769 // Filter out directories - those can't be shared.
Steve McKayfefcd702015-08-20 16:19:38 +0000770 List<DocumentInfo> docsForSend = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700771 for (DocumentInfo doc: docs) {
772 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
773 docsForSend.add(doc);
774 }
775 }
776
777 if (docsForSend.size() == 1) {
778 final DocumentInfo doc = docsForSend.get(0);
779
780 intent = new Intent(Intent.ACTION_SEND);
781 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
782 intent.addCategory(Intent.CATEGORY_DEFAULT);
783 intent.setType(doc.mimeType);
784 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
785
786 } else if (docsForSend.size() > 1) {
787 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
788 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
789 intent.addCategory(Intent.CATEGORY_DEFAULT);
790
Steve McKayfefcd702015-08-20 16:19:38 +0000791 final ArrayList<String> mimeTypes = new ArrayList<>();
792 final ArrayList<Uri> uris = new ArrayList<>();
Steve McKay9276f3b2015-05-27 16:11:42 -0700793 for (DocumentInfo doc : docsForSend) {
794 mimeTypes.add(doc.mimeType);
795 uris.add(doc.derivedUri);
796 }
797
798 intent.setType(findCommonMimeType(mimeTypes));
799 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
800
801 } else {
802 return;
803 }
804
805 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
806 startActivity(intent);
807 }
808 }.execute(selected);
809 }
810
Steve McKayef280152015-06-11 10:10:49 -0700811 private void deleteDocuments(final Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -0700812 Context context = getActivity();
813 ContentResolver resolver = context.getContentResolver();
814 String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700815
Ben Kwa91923182015-08-27 16:06:33 -0700816 mModel.markForDeletion(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700817
Ben Kwa91923182015-08-27 16:06:33 -0700818 Activity activity = getActivity();
819 Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG)
820 .setAction(
821 R.string.undo,
822 new android.view.View.OnClickListener() {
823 @Override
824 public void onClick(View view) {}
825 })
826 .setCallback(
827 new Snackbar.Callback() {
828 @Override
829 public void onDismissed(Snackbar snackbar, int event) {
830 if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {
831 mModel.undoDeletion();
832 } else {
Ben Kwa83cedf22015-09-11 15:15:45 -0700833 mModel.finalizeDeletion(
834 new Runnable() {
835 @Override
836 public void run() {
837 Snackbar.make(
838 DirectoryFragment.this.getView(),
839 R.string.toast_failed_delete,
840 Snackbar.LENGTH_LONG)
841 .show();
842
843 }
844 });
Ben Kwa91923182015-08-27 16:06:33 -0700845 }
Ben Kwa91923182015-08-27 16:06:33 -0700846 }
847 })
848 .show();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700849 }
850
Steve McKayef280152015-06-11 10:10:49 -0700851 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700852 // Pop up a dialog to pick a destination. This is inadequate but works for now.
853 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900854 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900855 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900856 Uri.EMPTY,
857 getActivity(),
858 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700859
860 new GetDocumentsTask() {
861 @Override
862 void onDocumentsReady(List<DocumentInfo> docs) {
863 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
864
865 boolean directoryCopy = false;
866 for (DocumentInfo info : docs) {
867 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
868 directoryCopy = true;
869 break;
870 }
871 }
872 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
873 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
874 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900875 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700876 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700877 }
878
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700879 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700880 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700881 }
882
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700883 private static abstract class Footer {
884 private final int mItemViewType;
885
886 public Footer(int itemViewType) {
887 mItemViewType = itemViewType;
888 }
889
890 public abstract View getView(View convertView, ViewGroup parent);
891
892 public int getItemViewType() {
893 return mItemViewType;
894 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700895 }
896
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700897 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700898 public LoadingFooter() {
899 super(1);
900 }
901
Jeff Sharkey20b32272013-09-03 15:25:52 -0700902 @Override
903 public View getView(View convertView, ViewGroup parent) {
904 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700905 final State state = getDisplayState(DirectoryFragment.this);
906
Jeff Sharkey20b32272013-09-03 15:25:52 -0700907 if (convertView == null) {
908 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700909 if (state.derivedMode == MODE_LIST) {
910 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
911 } else if (state.derivedMode == MODE_GRID) {
912 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
913 } else {
914 throw new IllegalStateException();
915 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700916 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700917
Jeff Sharkey20b32272013-09-03 15:25:52 -0700918 return convertView;
919 }
920 }
921
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700922 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700923 private final int mIcon;
924 private final String mMessage;
925
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700926 public MessageFooter(int itemViewType, int icon, String message) {
927 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700928 mIcon = icon;
929 mMessage = message;
930 }
931
932 @Override
933 public View getView(View convertView, ViewGroup parent) {
934 final Context context = parent.getContext();
935 final State state = getDisplayState(DirectoryFragment.this);
936
937 if (convertView == null) {
938 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700939 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700940 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700941 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700942 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
943 } else {
944 throw new IllegalStateException();
945 }
946 }
947
948 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
949 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
950 icon.setImageResource(mIcon);
951 title.setText(mMessage);
952 return convertView;
953 }
954 }
955
Steve McKayef280152015-06-11 10:10:49 -0700956 // Provide a reference to the views for each data item
957 // Complex data items may need more than one view per item, and
958 // you provide access to all the views for a data item in a view holder
959 private static final class DocumentHolder extends RecyclerView.ViewHolder {
960 // each data item is just a string in this case
961 public View view;
962 public String docId; // The stable document id.
963 public DocumentHolder(View view) {
964 super(view);
965 this.view = view;
966 }
967 }
968
Ben Kwa7461a952015-09-01 11:03:01 -0700969 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder>
970 implements Model.UpdateListener {
Steve McKayef280152015-06-11 10:10:49 -0700971
972 private final Context mContext;
973 private final LayoutInflater mInflater;
974 // TODO: Bring back support for footers.
Steve McKayfefcd702015-08-20 16:19:38 +0000975 private final List<Footer> mFooters = new ArrayList<>();
Steve McKayef280152015-06-11 10:10:49 -0700976
Steve McKayef280152015-06-11 10:10:49 -0700977 public DocumentsAdapter(Context context) {
978 mContext = context;
979 mInflater = LayoutInflater.from(context);
980 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700981
Ben Kwa7461a952015-09-01 11:03:01 -0700982 public void onModelUpdate(Model model) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700983 mFooters.clear();
Ben Kwa7461a952015-09-01 11:03:01 -0700984 if (model.info != null) {
985 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, model.info));
Ben Kwa24be5d32015-08-27 16:04:46 -0700986 }
Ben Kwa7461a952015-09-01 11:03:01 -0700987 if (model.error != null) {
988 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, model.error));
Ben Kwa24be5d32015-08-27 16:04:46 -0700989 }
Ben Kwa7461a952015-09-01 11:03:01 -0700990 if (model.isLoading()) {
Ben Kwa24be5d32015-08-27 16:04:46 -0700991 mFooters.add(new LoadingFooter());
Jeff Sharkey20b32272013-09-03 15:25:52 -0700992 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700993
Ben Kwa7461a952015-09-01 11:03:01 -0700994 if (model.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
Ben Kwa7461a952015-09-01 11:03:01 -07001003 public void onModelUpdateFailed(Exception e) {
1004 String error = getString(R.string.query_error);
1005 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
1006 notifyDataSetChanged();
1007 }
1008
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001009 @Override
Steve McKayef280152015-06-11 10:10:49 -07001010 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
1011 final State state = getDisplayState(DirectoryFragment.this);
1012 final LayoutInflater inflater = LayoutInflater.from(getContext());
1013 switch (state.derivedMode) {
1014 case MODE_GRID:
1015 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
1016 case MODE_LIST:
1017 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
1018 case MODE_UNKNOWN:
1019 default:
1020 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -07001021 }
1022 }
1023
Steve McKayef280152015-06-11 10:10:49 -07001024 @Override
1025 public void onBindViewHolder(DocumentHolder holder, int position) {
1026
1027 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001028 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001029 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -07001030 final RootsCache roots = DocumentsApplication.getRootsCache(context);
1031 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1032 context, mThumbSize);
1033
Ben Kwa24be5d32015-08-27 16:04:46 -07001034 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001035 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001036
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001037 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
1038 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001039 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
1040 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1041 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
1042 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
1043 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
1044 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
1045 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
1046 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001047
Steve McKayef280152015-06-11 10:10:49 -07001048 holder.docId = docId;
1049 final View itemView = holder.view;
Ben Kwa24be5d32015-08-27 16:04:46 -07001050 itemView.setActivated(mModel.isSelected(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -07001051
Steve McKayef280152015-06-11 10:10:49 -07001052 final View line1 = itemView.findViewById(R.id.line1);
1053 final View line2 = itemView.findViewById(R.id.line2);
1054
1055 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
1056 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
1057 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
1058 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
1059 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
1060 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
1061 final TextView date = (TextView) itemView.findViewById(R.id.date);
1062 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001063
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001064 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001065 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001066 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001067 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001068 }
1069
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 iconMime.animate().cancel();
1071 iconThumb.animate().cancel();
1072
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001073 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -07001074 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -07001075 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001076 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001077
Jeff Sharkey7e544612014-08-29 15:38:27 -07001078 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
1079 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1080
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001081 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001082 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001083 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001084 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001085 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001086 iconThumb.setImageBitmap(cachedResult);
1087 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001088 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001089 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001090 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001091 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001092 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001093 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001094 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001095 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001096 }
1097
1098 // Always throw MIME icon into place, even when a thumbnail is being
1099 // loaded in background.
1100 if (cacheHit) {
1101 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001102 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001103 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001104 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001105 iconMime.setAlpha(1f);
1106 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001107 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001108 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001109 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001110 }
1111
Jeff Sharkey9656a532013-09-13 13:42:19 -07001112 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001113 boolean hasLine2 = false;
1114
Jeff Sharkey9656a532013-09-13 13:42:19 -07001115 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1116 if (!hideTitle) {
1117 title.setText(docDisplayName);
1118 hasLine1 = true;
1119 }
1120
1121 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001122 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001123 // We've already had to enumerate roots before any results can
1124 // be shown, so this will never block.
1125 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001126 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001127 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001128 } else {
Steve McKayef280152015-06-11 10:10:49 -07001129 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001130 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001131
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001132 if (summary != null) {
1133 final boolean alwaysShowSummary = getResources()
1134 .getBoolean(R.bool.always_show_summary);
1135 if (alwaysShowSummary) {
1136 summary.setText(root.getDirectoryString());
1137 summary.setVisibility(View.VISIBLE);
1138 hasLine2 = true;
1139 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001140 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001141 // No summary needed if icon speaks for itself
1142 summary.setVisibility(View.INVISIBLE);
1143 } else {
1144 summary.setText(root.getDirectoryString());
1145 summary.setVisibility(View.VISIBLE);
1146 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1147 hasLine2 = true;
1148 }
1149 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001150 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001151 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001152 // Directories showing thumbnails in grid mode get a little icon
1153 // hint to remind user they're a directory.
1154 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1155 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001156 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001157 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001158 }
1159
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001160 if (summary != null) {
1161 if (docSummary != null) {
1162 summary.setText(docSummary);
1163 summary.setVisibility(View.VISIBLE);
1164 hasLine2 = true;
1165 } else {
1166 summary.setVisibility(View.INVISIBLE);
1167 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001168 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001169 }
1170
Jeff Sharkey9656a532013-09-13 13:42:19 -07001171 if (icon1 != null) icon1.setVisibility(View.GONE);
1172 if (icon2 != null) icon2.setVisibility(View.GONE);
1173
1174 if (iconDrawable != null) {
1175 if (hasLine1) {
1176 icon1.setVisibility(View.VISIBLE);
1177 icon1.setImageDrawable(iconDrawable);
1178 } else {
1179 icon2.setVisibility(View.VISIBLE);
1180 icon2.setImageDrawable(iconDrawable);
1181 }
1182 }
1183
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001184 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001185 date.setText(null);
1186 } else {
Steve McKayef280152015-06-11 10:10:49 -07001187 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001188 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001189 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001190
1191 if (state.showSize) {
1192 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001193 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001194 size.setText(null);
1195 } else {
Steve McKayef280152015-06-11 10:10:49 -07001196 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001197 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001198 }
1199 } else {
1200 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001201 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001202
Jeff Sharkey9656a532013-09-13 13:42:19 -07001203 if (line1 != null) {
1204 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1205 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001206 if (line2 != null) {
1207 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1208 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001209
Steve McKayef280152015-06-11 10:10:49 -07001210 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001211
1212 iconMime.setAlpha(iconAlpha);
1213 iconThumb.setAlpha(iconAlpha);
1214 if (icon1 != null) icon1.setAlpha(iconAlpha);
1215 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001216
Steve McKay8e258c62015-05-06 14:27:57 -07001217 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001218 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001219 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001220 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001221
Steve McKay351a7492015-08-04 10:11:01 -07001222 @Override
Steve McKayef280152015-06-11 10:10:49 -07001223 public int getItemCount() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001224 return mModel.getItemCount();
Steve McKayef280152015-06-11 10:10:49 -07001225 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001226 }
1227
1228 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001229 public int getItemViewType(int position) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001230 final int itemCount = mModel.getItemCount();
1231 if (position < itemCount) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001232 return 0;
1233 } else {
Ben Kwa24be5d32015-08-27 16:04:46 -07001234 position -= itemCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001235 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001236 }
1237 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001238 }
1239
1240 private static String formatTime(Context context, long when) {
1241 // TODO: DateUtils should make this easier
1242 Time then = new Time();
1243 then.set(when);
1244 Time now = new Time();
1245 now.setToNow();
1246
1247 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1248 | DateUtils.FORMAT_ABBREV_ALL;
1249
1250 if (then.year != now.year) {
1251 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1252 } else if (then.yearDay != now.yearDay) {
1253 flags |= DateUtils.FORMAT_SHOW_DATE;
1254 } else {
1255 flags |= DateUtils.FORMAT_SHOW_TIME;
1256 }
1257
1258 return DateUtils.formatDateTime(context, when, flags);
1259 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001260
1261 private String findCommonMimeType(List<String> mimeTypes) {
1262 String[] commonType = mimeTypes.get(0).split("/");
1263 if (commonType.length != 2) {
1264 return "*/*";
1265 }
1266
1267 for (int i = 1; i < mimeTypes.size(); i++) {
1268 String[] type = mimeTypes.get(i).split("/");
1269 if (type.length != 2) continue;
1270
1271 if (!commonType[1].equals(type[1])) {
1272 commonType[1] = "*";
1273 }
1274
1275 if (!commonType[0].equals(type[0])) {
1276 commonType[0] = "*";
1277 commonType[1] = "*";
1278 break;
1279 }
1280 }
1281
1282 return commonType[0] + "/" + commonType[1];
1283 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001284
1285 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001286 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001287 if (v.isEnabled() == enabled) return;
1288 v.setEnabled(enabled);
1289
1290 if (v instanceof ViewGroup) {
1291 final ViewGroup vg = (ViewGroup) v;
1292 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1293 setEnabledRecursive(vg.getChildAt(i), enabled);
1294 }
1295 }
1296 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001297
1298 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1299 final State state = getDisplayState(DirectoryFragment.this);
1300
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001301 // Directories are always enabled
1302 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1303 return true;
1304 }
1305
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001306 // Read-only files are disabled when creating
1307 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1308 return false;
1309 }
1310
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001311 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1312 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001313
Steve McKay1f199482015-05-20 15:58:42 -07001314 private void copyFromClipboard() {
1315 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1316
1317 @Override
1318 protected List<DocumentInfo> doInBackground(Void... params) {
1319 return mClipper.getClippedDocuments();
1320 }
1321
1322 @Override
1323 protected void onPostExecute(List<DocumentInfo> docs) {
1324 DocumentInfo destination =
1325 ((BaseActivity) getActivity()).getCurrentDirectory();
1326 copyDocuments(docs, destination);
1327 }
1328 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001329 }
1330
Steve McKay1f199482015-05-20 15:58:42 -07001331 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001332 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001333 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001334
Steve McKay1f199482015-05-20 15:58:42 -07001335 @Override
1336 protected List<DocumentInfo> doInBackground(Void... params) {
1337 return mClipper.getDocumentsFromClipData(clipData);
1338 }
1339
1340 @Override
1341 protected void onPostExecute(List<DocumentInfo> docs) {
1342 copyDocuments(docs, destination);
1343 }
1344 }.execute();
1345 }
1346
1347 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1348 if (!canCopy(docs, destination)) {
1349 Toast.makeText(
1350 getActivity(),
1351 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001352 return;
1353 }
1354
Steve McKay1f199482015-05-20 15:58:42 -07001355 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001356 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001357 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001358
Steve McKay1f199482015-05-20 15:58:42 -07001359 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001360 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001361 if (destination != null) {
1362 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001363 tmpStack.addAll(curStack);
1364 } else {
1365 tmpStack = curStack;
1366 }
1367
Steve McKay1f199482015-05-20 15:58:42 -07001368 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001369 }
1370
1371 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1372 Context context = getActivity();
1373 final ContentResolver resolver = context.getContentResolver();
1374 ClipData clipData = null;
1375 for (DocumentInfo doc : docs) {
1376 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1377 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001378 // TODO: figure out what this string should be.
1379 // Currently it is not displayed anywhere in the UI, but this might change.
1380 final String label = "";
1381 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001382 } else {
1383 // TODO: update list of mime types in ClipData.
1384 clipData.addItem(new ClipData.Item(uri));
1385 }
1386 }
1387 return clipData;
1388 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001389
Steve McKay1f199482015-05-20 15:58:42 -07001390 void copySelectedToClipboard() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001391 Selection sel = mModel.getSelection(new Selection());
Steve McKayef280152015-06-11 10:10:49 -07001392 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001393 }
Steve McKay0599a442015-05-05 14:50:00 -07001394
Steve McKayef280152015-06-11 10:10:49 -07001395 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001396 new GetDocumentsTask() {
1397 @Override
1398 void onDocumentsReady(List<DocumentInfo> docs) {
1399 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001400 Activity activity = getActivity();
1401 Toast.makeText(activity,
1402 activity.getResources().getQuantityString(
1403 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1404 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001405 }
Steve McKayef280152015-06-11 10:10:49 -07001406 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001407 }
1408
1409 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001410 copyFromClipboard();
1411 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001412 }
1413
Steve McKay0599a442015-05-05 14:50:00 -07001414 /**
1415 * Returns true if the list of files can be copied to destination. Note that this
1416 * is a policy check only. Currently the method does not attempt to verify
1417 * available space or any other environmental aspects possibly resulting in
1418 * failure to copy.
1419 *
1420 * @return true if the list of files can be copied to destination.
1421 */
1422 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
Ben Kwa91923182015-08-27 16:06:33 -07001423 BaseActivity activity = (BaseActivity) getActivity();
Steve McKay0599a442015-05-05 14:50:00 -07001424
1425 final RootInfo root = activity.getCurrentRoot();
1426
1427 // Can't copy folders to Downloads.
1428 if (root.isDownloads()) {
1429 for (DocumentInfo docs : files) {
1430 if (docs.isDirectory()) {
1431 return false;
1432 }
1433 }
1434 }
1435
1436 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1437 }
1438
1439 void selectAllFiles() {
Ben Kwa24be5d32015-08-27 16:04:46 -07001440 boolean changed = mModel.selectAll();
Steve McKay9459a7c2015-07-24 13:14:20 -07001441 if (changed) {
1442 updateDisplayState();
1443 }
Steve McKay0599a442015-05-05 14:50:00 -07001444 }
1445
Steve McKayef280152015-06-11 10:10:49 -07001446 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001447 // Listen for drops on non-directory items and empty space.
1448 view.setOnDragListener(mOnDragListener);
1449 }
1450
1451 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1452 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1453 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1454 // Make a directory item a drop target. Drop on non-directories and empty space
1455 // is handled at the list/grid view level.
1456 view.setOnDragListener(mOnDragListener);
1457 }
1458
1459 // Temporary: attaching the listener to the title only.
1460 // Attaching to the entire item conflicts with the item long click handler responsible
1461 // for item selection.
1462 final View title = view.findViewById(android.R.id.title);
1463 title.setOnLongClickListener(mLongClickListener);
1464 }
1465
1466 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1467 @Override
1468 public boolean onDrag(View v, DragEvent event) {
1469 switch (event.getAction()) {
1470 case DragEvent.ACTION_DRAG_STARTED:
1471 // TODO: Check if the event contains droppable data.
1472 return true;
1473
1474 // TODO: Highlight potential drop target directory?
1475 // TODO: Expand drop target directory on hover?
1476 case DragEvent.ACTION_DRAG_ENTERED:
1477 case DragEvent.ACTION_DRAG_LOCATION:
1478 case DragEvent.ACTION_DRAG_EXITED:
1479 case DragEvent.ACTION_DRAG_ENDED:
1480 return true;
1481
1482 case DragEvent.ACTION_DROP:
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001483 int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001484 DocumentInfo dstDir = null;
1485 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001486 Cursor dstCursor = mModel.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001487 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001488 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1489 // TODO: Do not drop into the directory where the documents came from.
1490 }
1491 copyFromClipData(event.getClipData(), dstDir);
1492 return true;
1493 }
1494 return false;
1495 }
1496 };
1497
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001498 private View getContainingItemView(View view) {
1499 while (true) {
1500 if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
1501 return view;
1502 }
1503 ViewParent parent = view.getParent();
1504 if (parent == null || !(parent instanceof View)) {
1505 return null;
1506 }
1507 view = (View) parent;
1508 }
1509 }
1510
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001511 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1512 @Override
1513 public boolean onLongClick(View v) {
1514 final List<DocumentInfo> docs = getDraggableDocuments(v);
1515 if (docs.isEmpty()) {
1516 return false;
1517 }
1518 v.startDrag(
1519 getClipDataFromDocuments(docs),
1520 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1521 null,
Vladislav Kaznacheeve3ce8a92015-07-15 18:04:04 -07001522 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
1523 View.DRAG_FLAG_GLOBAL_URI_WRITE
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001524 );
1525 return true;
1526 }
1527 };
1528
1529 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Vladislav Kaznacheev9400b892015-09-04 09:17:37 -07001530 int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView));
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001531 if (position == android.widget.AdapterView.INVALID_POSITION) {
1532 return Collections.EMPTY_LIST;
1533 }
1534
Ben Kwa24be5d32015-08-27 16:04:46 -07001535 final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments();
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001536 if (!selectedDocs.isEmpty()) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001537 if (!mModel.isSelected(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001538 // There is a selection that does not include the current item, drag nothing.
1539 return Collections.EMPTY_LIST;
1540 }
1541 return selectedDocs;
1542 }
1543
Ben Kwa24be5d32015-08-27 16:04:46 -07001544 final Cursor cursor = mModel.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001545 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001546 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayfefcd702015-08-20 16:19:38 +00001547
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001548 return Lists.newArrayList(doc);
1549 }
1550
1551 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1552 if (docs.size() == 1) {
1553 final DocumentInfo doc = docs.get(0);
1554 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1555 doc.mimeType, doc.icon, getDisplayState(this));
1556 }
1557 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1558 }
1559
1560 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1561 String docMimeType, int docIcon, State state) {
1562 if (docIcon != 0) {
1563 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1564 } else {
1565 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1566 state.derivedMode);
1567 }
1568 }
1569
Steve McKayef280152015-06-11 10:10:49 -07001570 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1571 implements Preemptable {
1572 private final Uri mUri;
1573 private final ImageView mIconMime;
1574 private final ImageView mIconThumb;
1575 private final Point mThumbSize;
1576 private final float mTargetAlpha;
1577 private final CancellationSignal mSignal;
1578
1579 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1580 float targetAlpha) {
1581 mUri = uri;
1582 mIconMime = iconMime;
1583 mIconThumb = iconThumb;
1584 mThumbSize = thumbSize;
1585 mTargetAlpha = targetAlpha;
1586 mSignal = new CancellationSignal();
1587 }
1588
1589 @Override
1590 public void preempt() {
1591 cancel(false);
1592 mSignal.cancel();
1593 }
1594
1595 @Override
1596 protected Bitmap doInBackground(Uri... params) {
1597 if (isCancelled()) return null;
1598
1599 final Context context = mIconThumb.getContext();
1600 final ContentResolver resolver = context.getContentResolver();
1601
1602 ContentProviderClient client = null;
1603 Bitmap result = null;
1604 try {
1605 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1606 resolver, mUri.getAuthority());
1607 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1608 if (result != null) {
1609 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1610 context, mThumbSize);
1611 thumbs.put(mUri, result);
1612 }
1613 } catch (Exception e) {
1614 if (!(e instanceof OperationCanceledException)) {
1615 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1616 }
1617 } finally {
1618 ContentProviderClient.releaseQuietly(client);
1619 }
1620 return result;
1621 }
1622
1623 @Override
1624 protected void onPostExecute(Bitmap result) {
1625 if (mIconThumb.getTag() == this && result != null) {
1626 mIconThumb.setTag(null);
1627 mIconThumb.setImageBitmap(result);
1628
1629 mIconMime.setAlpha(mTargetAlpha);
1630 mIconMime.animate().alpha(0f).start();
1631 mIconThumb.setAlpha(0f);
1632 mIconThumb.animate().alpha(mTargetAlpha).start();
1633 }
1634 }
1635 }
1636
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001637 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1638
1639 private final Drawable mShadow;
1640
1641 private final int mShadowDimension;
1642
1643 public DrawableShadowBuilder(Drawable shadow) {
1644 mShadow = shadow;
1645 mShadowDimension = getResources().getDimensionPixelSize(
1646 R.dimen.drag_shadow_size);
1647 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1648 }
1649
Ben Kwa24be5d32015-08-27 16:04:46 -07001650 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001651 public void onProvideShadowMetrics(
1652 Point shadowSize, Point shadowTouchPoint) {
1653 shadowSize.set(mShadowDimension, mShadowDimension);
1654 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1655 }
1656
Ben Kwa24be5d32015-08-27 16:04:46 -07001657 @Override
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001658 public void onDrawShadow(Canvas canvas) {
1659 mShadow.draw(canvas);
1660 }
1661 }
Steve McKay1f199482015-05-20 15:58:42 -07001662
1663 private FragmentTuner pickFragmentTuner(final State state) {
1664 return state.action == ACTION_BROWSE_ALL
Steve McKay0fbfc652015-08-20 16:48:49 -07001665 ? new FilesTuner()
Steve McKay1f199482015-05-20 15:58:42 -07001666 : new DefaultTuner(state);
1667 }
1668
1669 /**
1670 * Interface for specializing the Fragment for the "host" Activity.
1671 * Feel free to expand the role of this class to handle other specializations.
1672 */
1673 private interface FragmentTuner {
Steve McKay4f4232d2015-07-22 12:13:46 -07001674 void updateActionMenu(Menu menu, int dirType, boolean canDelete);
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001675 void afterActivityCreated(DirectoryFragment fragment);
Steve McKay1f199482015-05-20 15:58:42 -07001676 }
1677
1678 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001679 * Abstract task providing support for loading documents *off*
1680 * the main thread. And if it isn't obvious, creating a list
1681 * of documents (especially large lists) can be pretty expensive.
1682 */
1683 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001684 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001685 @Override
Steve McKayef280152015-06-11 10:10:49 -07001686 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Ben Kwa24be5d32015-08-27 16:04:46 -07001687 return mModel.getDocuments(selected[0]);
Steve McKay9276f3b2015-05-27 16:11:42 -07001688 }
1689
1690 @Override
1691 protected final void onPostExecute(List<DocumentInfo> docs) {
1692 onDocumentsReady(docs);
1693 }
1694
1695 abstract void onDocumentsReady(List<DocumentInfo> docs);
1696 }
1697
1698 /**
Steve McKay1f199482015-05-20 15:58:42 -07001699 * Provides support for Platform specific specializations of DirectoryFragment.
1700 */
1701 private static final class DefaultTuner implements FragmentTuner {
1702
1703 private final State mState;
1704
1705 public DefaultTuner(State state) {
1706 mState = state;
1707 }
1708
1709 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001710 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001711 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1712
1713 final MenuItem open = menu.findItem(R.id.menu_open);
1714 final MenuItem share = menu.findItem(R.id.menu_share);
1715 final MenuItem delete = menu.findItem(R.id.menu_delete);
1716 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1717 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1718 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1719
1720 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1721 || mState.action == ACTION_BROWSE);
1722
1723 open.setVisible(!manageOrBrowse);
1724 share.setVisible(manageOrBrowse);
Steve McKay4f4232d2015-07-22 12:13:46 -07001725 delete.setVisible(manageOrBrowse && canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001726 // Disable copying from the Recents view.
1727 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1728 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1729
Steve McKay0fbfc652015-08-20 16:48:49 -07001730 // Only shown in files mode.
Steve McKay1f199482015-05-20 15:58:42 -07001731 copyToClipboard.setVisible(false);
1732 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001733
1734 @Override
1735 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001736 }
1737
1738 /**
Steve McKay0fbfc652015-08-20 16:48:49 -07001739 * Provides support for Files activity specific specializations of DirectoryFragment.
Steve McKay1f199482015-05-20 15:58:42 -07001740 */
Steve McKay0fbfc652015-08-20 16:48:49 -07001741 private static final class FilesTuner implements FragmentTuner {
Steve McKay1f199482015-05-20 15:58:42 -07001742 @Override
Steve McKay4f4232d2015-07-22 12:13:46 -07001743 public void updateActionMenu(Menu menu, int dirType, boolean canDelete) {
Steve McKay1f199482015-05-20 15:58:42 -07001744 menu.findItem(R.id.menu_share).setVisible(true);
Steve McKay4f4232d2015-07-22 12:13:46 -07001745 menu.findItem(R.id.menu_delete).setVisible(canDelete);
Steve McKay1f199482015-05-20 15:58:42 -07001746 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1747
1748 menu.findItem(R.id.menu_open).setVisible(false);
1749 menu.findItem(R.id.menu_copy_to).setVisible(false);
1750 menu.findItem(R.id.menu_move_to).setVisible(false);
1751 }
Kyle Horimoto426bd0d2015-07-29 15:33:49 -07001752
1753 @Override
Kyle Horimoto62a7fd02015-08-18 13:25:29 -07001754 public void afterActivityCreated(DirectoryFragment fragment) {}
Steve McKay1f199482015-05-20 15:58:42 -07001755 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001756
1757 /**
1758 * The data model for the current loaded directory.
1759 */
Ben Kwa7461a952015-09-01 11:03:01 -07001760 @VisibleForTesting
1761 public static final class Model implements DocumentContext {
Ben Kwa24be5d32015-08-27 16:04:46 -07001762 private MultiSelectManager mSelectionManager;
Ben Kwa7461a952015-09-01 11:03:01 -07001763 private Context mContext;
Ben Kwa24be5d32015-08-27 16:04:46 -07001764 private int mCursorCount;
1765 private boolean mIsLoading;
Ben Kwa7461a952015-09-01 11:03:01 -07001766 private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray();
1767 private UpdateListener mUpdateListener;
Ben Kwa24be5d32015-08-27 16:04:46 -07001768 @Nullable private Cursor mCursor;
1769 @Nullable private String info;
1770 @Nullable private String error;
Ben Kwa7461a952015-09-01 11:03:01 -07001771
1772 Model(Context context, MultiSelectManager selectionManager) {
1773 mContext = context;
1774 mSelectionManager = selectionManager;
1775 }
Ben Kwa24be5d32015-08-27 16:04:46 -07001776
1777 /**
1778 * Sets the selection manager used by the model.
1779 * TODO: the model should instantiate the selection manager. See onActivityCreated.
1780 */
1781 void setSelectionManager(MultiSelectManager mgr) {
1782 mSelectionManager = mgr;
1783 }
1784
1785 /**
1786 * Selects all files in the current directory.
1787 * @return true if the selection state changed for any files.
1788 */
1789 boolean selectAll() {
1790 return mSelectionManager.setItemsSelected(0, mCursorCount, true);
1791 }
1792
1793 /**
1794 * Clones the current selection into the given Selection object.
1795 * @param selection
1796 * @return The selection that was passed in, for convenience.
1797 */
1798 Selection getSelection(Selection selection) {
1799 return mSelectionManager.getSelection(selection);
1800 }
1801
1802 /**
1803 * @return The current selection (the live instance, not a copy).
1804 */
1805 Selection getSelection() {
1806 return mSelectionManager.getSelection();
1807 }
1808
1809 boolean isSelected(int position) {
1810 return mSelectionManager.getSelection().contains(position);
1811 }
1812
1813 void clearSelection() {
1814 mSelectionManager.clearSelection();
1815 }
1816
1817 void update(DirectoryResult result) {
1818 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
1819
1820 if (result == null) {
1821 mCursor = null;
1822 mCursorCount = 0;
1823 info = null;
1824 error = null;
1825 mIsLoading = false;
Ben Kwa83cedf22015-09-11 15:15:45 -07001826 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001827 return;
1828 }
1829
1830 if (result.exception != null) {
1831 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwa83cedf22015-09-11 15:15:45 -07001832 mUpdateListener.onModelUpdateFailed(result.exception);
Ben Kwa24be5d32015-08-27 16:04:46 -07001833 return;
1834 }
1835
1836 mCursor = result.cursor;
1837 mCursorCount = mCursor.getCount();
1838
1839 final Bundle extras = mCursor.getExtras();
1840 if (extras != null) {
1841 info = extras.getString(DocumentsContract.EXTRA_INFO);
1842 error = extras.getString(DocumentsContract.EXTRA_ERROR);
1843 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
1844 }
Ben Kwa7461a952015-09-01 11:03:01 -07001845
Ben Kwa83cedf22015-09-11 15:15:45 -07001846 mUpdateListener.onModelUpdate(this);
Ben Kwa24be5d32015-08-27 16:04:46 -07001847 }
1848
Ben Kwa7461a952015-09-01 11:03:01 -07001849 int getItemCount() {
Ben Kwa91923182015-08-27 16:06:33 -07001850 return mCursorCount - mMarkedForDeletion.size();
Ben Kwa24be5d32015-08-27 16:04:46 -07001851 }
1852
Ben Kwa7461a952015-09-01 11:03:01 -07001853 Cursor getItem(int position) {
Ben Kwa91923182015-08-27 16:06:33 -07001854 // Items marked for deletion are masked out of the UI. To do this, for every marked
1855 // item whose position is less than the requested item position, advance the requested
1856 // position by 1.
1857 final int originalPos = position;
1858 final int size = mMarkedForDeletion.size();
Ben Kwa0d0daff2015-09-09 13:49:07 -07001859 for (int i = 0; i < size; ++i) {
Ben Kwa91923182015-08-27 16:06:33 -07001860 // It'd be more concise, but less efficient, to iterate over positions while calling
1861 // mMarkedForDeletion.get. Instead, iterate over deleted entries.
1862 if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) {
1863 ++position;
1864 }
1865 }
1866
1867 if (DEBUG) {
1868 Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos
1869 + " Adjusted: " + position);
1870 }
1871
Ben Kwa24be5d32015-08-27 16:04:46 -07001872 if (position >= mCursorCount) {
1873 throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " +
1874 mCursorCount + " items");
1875 }
1876
1877 mCursor.moveToPosition(position);
1878 return mCursor;
1879 }
1880
1881 private boolean isEmpty() {
1882 return mCursorCount == 0;
1883 }
1884
1885 private boolean isLoading() {
1886 return mIsLoading;
1887 }
1888
1889 private List<DocumentInfo> getSelectedDocuments() {
1890 Selection sel = getSelection(new Selection());
1891 return getDocuments(sel);
1892 }
1893
Ben Kwa7461a952015-09-01 11:03:01 -07001894 List<DocumentInfo> getDocuments(Selection items) {
Ben Kwa91923182015-08-27 16:06:33 -07001895 final int size = (items != null) ? items.size() : 0;
Ben Kwa24be5d32015-08-27 16:04:46 -07001896
Ben Kwa91923182015-08-27 16:06:33 -07001897 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwa24be5d32015-08-27 16:04:46 -07001898 for (int i = 0; i < size; i++) {
1899 final Cursor cursor = getItem(items.get(i));
1900 checkNotNull(cursor, "Cursor cannot be null.");
1901 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1902 docs.add(doc);
1903 }
1904 return docs;
1905 }
1906
1907 @Override
1908 public Cursor getCursor() {
1909 if (Looper.myLooper() != Looper.getMainLooper()) {
1910 throw new IllegalStateException("Can't call getCursor from non-main thread.");
1911 }
1912 return mCursor;
1913 }
Ben Kwa91923182015-08-27 16:06:33 -07001914
Ben Kwa7461a952015-09-01 11:03:01 -07001915 List<DocumentInfo> getDocumentsMarkedForDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001916 final int size = mMarkedForDeletion.size();
1917 List<DocumentInfo> docs = new ArrayList<>(size);
1918
1919 for (int i = 0; i < size; ++i) {
1920 final int position = mMarkedForDeletion.keyAt(i);
1921 checkState(position < mCursorCount);
1922 mCursor.moveToPosition(position);
1923 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor);
1924 docs.add(doc);
1925 }
1926 return docs;
1927 }
1928
1929 /**
1930 * Marks the given files for deletion. This will remove them from the UI. Clients must then
1931 * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm
1932 * the deletion, respectively. Only one deletion operation is allowed at a time.
1933 *
1934 * @param selected A selection representing the files to delete.
1935 */
Ben Kwa7461a952015-09-01 11:03:01 -07001936 void markForDeletion(Selection selected) {
Ben Kwa91923182015-08-27 16:06:33 -07001937 // Only one deletion operation at a time.
1938 checkState(mMarkedForDeletion.size() == 0);
1939 // There should never be more to delete than what exists.
1940 checkState(mCursorCount >= selected.size());
1941
1942 final int size = selected.size();
1943 for (int i = 0; i < size; ++i) {
1944 int position = selected.get(i);
1945 if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion");
1946 mMarkedForDeletion.append(position, true);
Ben Kwa83cedf22015-09-11 15:15:45 -07001947 mUpdateListener.notifyItemRemoved(position);
Ben Kwa91923182015-08-27 16:06:33 -07001948 }
1949 }
1950
1951 /**
1952 * Cancels an ongoing deletion operation. All files currently marked for deletion will be
1953 * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}.
1954 */
Ben Kwa7461a952015-09-01 11:03:01 -07001955 void undoDeletion() {
Ben Kwa91923182015-08-27 16:06:33 -07001956 // Iterate over deleted items, temporarily marking them false in the deletion list, and
1957 // re-adding them to the UI.
1958 final int size = mMarkedForDeletion.size();
1959 for (int i = 0; i < size; ++i) {
1960 final int position = mMarkedForDeletion.keyAt(i);
1961 mMarkedForDeletion.put(position, false);
Ben Kwa83cedf22015-09-11 15:15:45 -07001962 mUpdateListener.notifyItemInserted(position);
Ben Kwa91923182015-08-27 16:06:33 -07001963 }
1964
1965 // Then, clear the deletion list.
1966 mMarkedForDeletion.clear();
1967 }
1968
1969 /**
1970 * Finalizes an ongoing deletion operation. All files currently marked for deletion will be
1971 * deleted. See {@link #markForDeletion(Selection)}.
Ben Kwa7461a952015-09-01 11:03:01 -07001972 *
1973 * @param view The view which will be used to interact with the user (e.g. surfacing
1974 * snackbars) for errors, info, etc.
Ben Kwa91923182015-08-27 16:06:33 -07001975 */
Ben Kwa83cedf22015-09-11 15:15:45 -07001976 void finalizeDeletion(Runnable errorCallback) {
Ben Kwa7461a952015-09-01 11:03:01 -07001977 final ContentResolver resolver = mContext.getContentResolver();
Ben Kwa83cedf22015-09-11 15:15:45 -07001978 DeleteFilesTask task = new DeleteFilesTask(resolver, errorCallback);
Ben Kwa7461a952015-09-01 11:03:01 -07001979 task.execute();
Ben Kwa91923182015-08-27 16:06:33 -07001980 }
1981
1982 /**
1983 * A Task which collects the DocumentInfo for documents that have been marked for deletion,
1984 * and actually deletes them.
1985 */
1986 private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> {
1987 private ContentResolver mResolver;
Ben Kwa7461a952015-09-01 11:03:01 -07001988 private Runnable mErrorCallback;
Ben Kwa91923182015-08-27 16:06:33 -07001989
Ben Kwa7461a952015-09-01 11:03:01 -07001990 /**
1991 * @param resolver A ContentResolver for performing the actual file deletions.
1992 * @param errorCallback A Runnable that is executed in the event that one or more errors
1993 * occured while copying files. Execution will occur on the UI thread.
1994 */
1995 public DeleteFilesTask(ContentResolver resolver, Runnable errorCallback) {
Ben Kwa91923182015-08-27 16:06:33 -07001996 mResolver = resolver;
Ben Kwa7461a952015-09-01 11:03:01 -07001997 mErrorCallback = errorCallback;
Ben Kwa91923182015-08-27 16:06:33 -07001998 }
1999
2000 @Override
2001 protected List<DocumentInfo> doInBackground(Void... params) {
2002 return getDocumentsMarkedForDeletion();
2003 }
2004
2005 @Override
2006 protected void onPostExecute(List<DocumentInfo> docs) {
2007 boolean hadTrouble = false;
2008 for (DocumentInfo doc : docs) {
2009 if (!doc.isDeleteSupported()) {
2010 Log.w(TAG, doc + " could not be deleted. Skipping...");
2011 hadTrouble = true;
2012 continue;
2013 }
2014
2015 ContentProviderClient client = null;
2016 try {
2017 if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName);
2018 client = DocumentsApplication.acquireUnstableProviderOrThrow(
2019 mResolver, doc.derivedUri.getAuthority());
2020 DocumentsContract.deleteDocument(client, doc.derivedUri);
2021 } catch (Exception e) {
2022 Log.w(TAG, "Failed to delete " + doc);
2023 hadTrouble = true;
2024 } finally {
2025 ContentProviderClient.releaseQuietly(client);
2026 }
2027 }
2028
2029 if (hadTrouble) {
Ben Kwa7461a952015-09-01 11:03:01 -07002030 // TODO show which files failed? b/23720103
2031 mErrorCallback.run();
Ben Kwa91923182015-08-27 16:06:33 -07002032 if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed.");
2033 } else {
2034 if (DEBUG) Log.d(TAG, "Deletion task completed successfully.");
2035 }
2036 mMarkedForDeletion.clear();
2037 }
2038 }
Ben Kwa7461a952015-09-01 11:03:01 -07002039
2040 void addUpdateListener(UpdateListener listener) {
2041 checkState(mUpdateListener == null);
2042 mUpdateListener = listener;
2043 }
2044
2045 interface UpdateListener {
2046 /**
2047 * Called when a successful update has occurred.
2048 */
2049 void onModelUpdate(Model model);
2050
2051 /**
2052 * Called when an update has been attempted but failed.
2053 */
2054 void onModelUpdateFailed(Exception e);
2055
2056 /**
2057 * Called when an item has been removed from the model.
2058 */
2059 void notifyItemRemoved(int position);
2060
2061 /**
2062 * Called when an item has been added to the model.
2063 */
2064 void notifyItemInserted(int position);
2065 }
Ben Kwa24be5d32015-08-27 16:04:46 -07002066 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07002067}