blob: 8519bef7f2efc5686dc44dac5ed28a404edacd08 [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKay0599a442015-05-05 14:50:00 -070031
Steve McKay1f199482015-05-20 15:58:42 -070032import android.annotation.NonNull;
Ben Kwaf5858932015-04-07 15:43:39 -070033import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070034import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070035import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.app.FragmentManager;
37import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070039import android.content.ClipData;
Steve McKay0599a442015-05-05 14:50:00 -070040import android.content.ClipboardManager;
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 Sharkeydb884f52013-09-21 15:11:19 -070053import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070055import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070056import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070057import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070058import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070059import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070060import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070061import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070062import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070063import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070064import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070065import android.text.format.Time;
66import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070067import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070068import android.util.SparseBooleanArray;
69import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070070import android.view.DragEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070071import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070073import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070074import android.view.View;
75import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070076import android.widget.AbsListView;
77import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070078import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070079import android.widget.AdapterView;
80import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070081import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070082import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070083import android.widget.ImageView;
84import android.widget.ListView;
85import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070086import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087
Steve McKayd0a2a2c2015-03-25 14:35:33 -070088import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070089import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070090import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070091import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090092import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070093import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -070094import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -070095
Jeff Sharkeyc317af82013-07-01 16:56:54 -070096import com.google.android.collect.Lists;
97
Steve McKayab4746b2015-05-20 13:31:35 -070098import libcore.io.IoUtils;
99
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700100import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700101import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700102import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700103
104/**
105 * Display the documents inside a single directory.
106 */
107public class DirectoryFragment extends Fragment {
108
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700109 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700110 private ListView mListView;
111 private GridView mGridView;
112
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700113 private AbsListView mCurrentView;
114
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700115 public static final int TYPE_NORMAL = 1;
116 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700117 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700118
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700119 public static final int ANIM_NONE = 1;
120 public static final int ANIM_SIDE = 2;
121 public static final int ANIM_DOWN = 3;
122 public static final int ANIM_UP = 4;
123
Ben Kwaf5858932015-04-07 15:43:39 -0700124 public static final int REQUEST_COPY_DESTINATION = 1;
125
Jeff Sharkey5b535922013-08-02 15:55:26 -0700126 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700127 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700128
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700129 private int mLastMode = MODE_UNKNOWN;
130 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700131 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700132
Jeff Sharkey9656a532013-09-13 13:42:19 -0700133 private boolean mHideGridTitles = false;
134
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700135 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700136 private Point mThumbSize;
137
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700138 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700139 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700140
Steve McKay8e258c62015-05-06 14:27:57 -0700141 private static final boolean DEBUG_ENABLE_DND = false;
142
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700143 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700144 private static final String EXTRA_ROOT = "root";
145 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700146 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700147 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700148
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700149 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700150
Steve McKay1f199482015-05-20 15:58:42 -0700151 private FragmentTuner mFragmentTuner;
152 private DocumentClipper mClipper;
153
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700154 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
155 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700156 }
157
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700158 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
159 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700160 }
161
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700162 public static void showRecentsOpen(FragmentManager fm, int anim) {
163 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700164 }
165
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700166 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
167 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700168 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700169 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700170 args.putParcelable(EXTRA_ROOT, root);
171 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700172 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700173
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700174 final FragmentTransaction ft = fm.beginTransaction();
175 switch (anim) {
176 case ANIM_SIDE:
177 args.putBoolean(EXTRA_IGNORE_STATE, true);
178 break;
179 case ANIM_DOWN:
180 args.putBoolean(EXTRA_IGNORE_STATE, true);
181 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
182 break;
183 case ANIM_UP:
184 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
185 break;
186 }
187
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700188 final DirectoryFragment fragment = new DirectoryFragment();
189 fragment.setArguments(args);
190
Jeff Sharkey76112212013-08-06 11:26:10 -0700191 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700192 ft.commitAllowingStateLoss();
193 }
194
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700195 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
196 final StringBuilder builder = new StringBuilder();
197 builder.append(root != null ? root.authority : "null").append(';');
198 builder.append(root != null ? root.rootId : "null").append(';');
199 builder.append(doc != null ? doc.documentId : "null");
200 return builder.toString();
201 }
202
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700203 public static DirectoryFragment get(FragmentManager fm) {
204 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700205 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700206 }
207
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700208 @Override
209 public View onCreateView(
210 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
211 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700212 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700213 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
214
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700215 mEmptyView = view.findViewById(android.R.id.empty);
216
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217 mListView = (ListView) view.findViewById(R.id.list);
218 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700219 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700220 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700221
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700222 // Indent our list divider to align with text
223 final Drawable divider = mListView.getDivider();
224 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
225 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
226 if (insetLeft) {
227 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
228 } else {
229 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
230 }
231
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700232 mGridView = (GridView) view.findViewById(R.id.grid);
233 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700234 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700235 mGridView.setRecyclerListener(mRecycleListener);
Steve McKay8e258c62015-05-06 14:27:57 -0700236
237 if (DEBUG_ENABLE_DND) {
238 setupDragAndDropOnDirectoryView(mListView);
239 setupDragAndDropOnDirectoryView(mGridView);
240 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700241
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700242 return view;
243 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700244
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700245 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700246 public void onDestroyView() {
247 super.onDestroyView();
248
249 // Cancel any outstanding thumbnail requests
250 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
251 final int count = target.getChildCount();
252 for (int i = 0; i < count; i++) {
253 final View view = target.getChildAt(i);
254 mRecycleListener.onMovedToScrapHeap(view);
255 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700256
257 // Tear down any selection in progress
258 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
259 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700260 }
261
262 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700263 public void onActivityCreated(Bundle savedInstanceState) {
264 super.onActivityCreated(savedInstanceState);
265
266 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700267 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700268
Jeff Sharkey9656a532013-09-13 13:42:19 -0700269 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
270 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
271
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700272 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700273 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700274 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700275
Steve McKay1f199482015-05-20 15:58:42 -0700276 mFragmentTuner = pickFragmentTuner(state);
277 mClipper = new DocumentClipper(context);
278
Jeff Sharkey9656a532013-09-13 13:42:19 -0700279 if (mType == TYPE_RECENT_OPEN) {
280 // Hide titles when showing recents for picking images/videos
281 mHideGridTitles = MimePredicate.mimeMatches(
282 MimePredicate.VISUAL_MIMES, state.acceptMimes);
283 } else {
284 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
285 }
286
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700287 final ActivityManager am = (ActivityManager) context.getSystemService(
288 Context.ACTIVITY_SERVICE);
289 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
290
Jeff Sharkey46899c82013-08-18 22:26:48 -0700291 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700292 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700293 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700294 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700295
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700296 Uri contentsUri;
297 switch (mType) {
298 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700299 contentsUri = DocumentsContract.buildChildDocumentsUri(
300 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700301 if (state.action == ACTION_MANAGE) {
302 contentsUri = DocumentsContract.setManageMode(contentsUri);
303 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700304 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700305 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700306 case TYPE_SEARCH:
307 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700308 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700309 if (state.action == ACTION_MANAGE) {
310 contentsUri = DocumentsContract.setManageMode(contentsUri);
311 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700312 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700313 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700314 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700315 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700316 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700317 default:
318 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700319 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700320 }
321
322 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700323 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700324 if (!isAdded()) return;
325
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700326 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700327
328 // Push latest state up to UI
329 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700330 if (result.mode != MODE_UNKNOWN) {
331 state.derivedMode = result.mode;
332 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700333 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700334 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700335
336 updateDisplayState();
337
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700338 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700339 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
340 context instanceof DocumentsActivity) {
341 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700342 }
343
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700344 // Restore any previous instance state
345 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
346 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
347 getView().restoreHierarchyState(container);
348 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700349 mListView.smoothScrollToPosition(0);
350 mGridView.smoothScrollToPosition(0);
351 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700352
353 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700354 }
355
356 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700357 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700358 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700359 }
360 };
361
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700362 // Kick off loader at least once
363 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
364
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700365 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700366 }
367
Jeff Sharkey42d26792013-09-06 13:22:09 -0700368 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700369 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700370 // There's only one request code right now. Replace this with a switch statement or
371 // something more scalable when more codes are added.
372 if (requestCode != REQUEST_COPY_DESTINATION) {
373 return;
374 }
375 if (resultCode == Activity.RESULT_CANCELED || data == null) {
376 // User pressed the back button or otherwise cancelled the destination pick. Don't
377 // proceed with the copy.
378 return;
379 }
380
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900381 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700382 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
383 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700384 }
385
386 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700387 public void onStop() {
388 super.onStop();
389
390 // Remember last scroll location
391 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
392 getView().saveHierarchyState(container);
393 final State state = getDisplayState(this);
394 state.dirState.put(mStateKey, container);
395 }
396
397 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700398 public void onResume() {
399 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700400 updateDisplayState();
401 }
402
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700403 public void onDisplayStateChanged() {
404 updateDisplayState();
405 }
406
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700407 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700408 // Sort order change always triggers reload; we'll trigger state change
409 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700410 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
411 }
412
413 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700414 final ContentResolver resolver = getActivity().getContentResolver();
415 final State state = getDisplayState(this);
416
417 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
418 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
419
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700420 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700421 final Uri stateUri = RecentsProvider.buildState(
422 root.authority, root.rootId, doc.documentId);
423 final ContentValues values = new ContentValues();
424 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700425
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700426 new AsyncTask<Void, Void, Void>() {
427 @Override
428 protected Void doInBackground(Void... params) {
429 resolver.insert(stateUri, values);
430 return null;
431 }
432 }.execute();
433 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700434
435 // Mode change is just visual change; no need to kick loader, and
436 // deliver change event immediately.
437 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700438 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700439
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700440 updateDisplayState();
441 }
442
443 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700444 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700445
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700446 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700447 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700448 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700449
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700450 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
451 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700452
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700453 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700454 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700455 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
456 } else {
457 choiceMode = ListView.CHOICE_MODE_NONE;
458 }
459
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700460 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700461 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700462 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700463 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700464 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700465 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700466 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700467 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700468 mGridView.setChoiceMode(choiceMode);
469 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700470 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700471 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700472 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700473 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700474 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700475 mListView.setChoiceMode(choiceMode);
476 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700477 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700478 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700479 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700480
481 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700482 }
483
484 private OnItemClickListener mItemListener = new OnItemClickListener() {
485 @Override
486 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700487 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700488 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700489 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
490 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
491 if (isDocumentEnabled(docMimeType, docFlags)) {
492 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700493 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700494 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700495 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700496 }
497 };
498
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700499 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
500 @Override
501 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
502 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100503 mode.setTitle(getResources()
504 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700505 return true;
506 }
507
508 @Override
509 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay1f199482015-05-20 15:58:42 -0700510 // Delegate update logic to our owning action, since specialized
511 // logic is desired.
512 mFragmentTuner.updateActionMenu(menu, mType);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700513 return true;
514 }
515
516 @Override
Steve McKay1f199482015-05-20 15:58:42 -0700517 public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
518
Steve McKay9276f3b2015-05-27 16:11:42 -0700519 // ListView returns a reference to its internal selection container,
520 // which will get cleared when we cancel action mode. So we
521 // make a defensive clone here.
522 final SparseBooleanArray selected = mCurrentView.getCheckedItemPositions().clone();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700523
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 final int id = item.getItemId();
525 if (id == R.id.menu_open) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700526 openDocuments(selected);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700527 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700528 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700529
530 } else if (id == R.id.menu_share) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700531 shareDocuments(selected);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700532 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700533 return true;
534
535 } else if (id == R.id.menu_delete) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700536 deleteDocuments(selected);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700537 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700538 return true;
539
Steve McKay1f199482015-05-20 15:58:42 -0700540 } else if (id == R.id.menu_copy_to) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700541 transferDocuments(selected, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700542 mode.finish();
543 return true;
544
Steve McKay1f199482015-05-20 15:58:42 -0700545 } else if (id == R.id.menu_move_to) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700546 transferDocuments(selected, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700547 mode.finish();
548 return true;
549
Steve McKay1f199482015-05-20 15:58:42 -0700550 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700551 copySelectionToClipboard(selected);
Steve McKay1f199482015-05-20 15:58:42 -0700552 mode.finish();
553 return true;
554
Ben Kwa512a6ba2015-03-31 08:15:21 -0700555 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700556 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700557 return true;
558
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700559 } else {
560 return false;
561 }
562 }
563
564 @Override
565 public void onDestroyActionMode(ActionMode mode) {
566 // ignored
567 }
568
569 @Override
570 public void onItemCheckedStateChanged(
571 ActionMode mode, int position, long id, boolean checked) {
572 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700573 // Directories and footer items cannot be checked
574 boolean valid = false;
575
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700576 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700577 if (cursor != null) {
578 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700579 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700580 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700581 }
582
583 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700584 mCurrentView.setItemChecked(position, false);
585 }
586 }
587
588 mode.setTitle(getResources()
589 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
590 }
591 };
592
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700593 private RecyclerListener mRecycleListener = new RecyclerListener() {
594 @Override
595 public void onMovedToScrapHeap(View view) {
596 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
597 if (iconThumb != null) {
598 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
599 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700600 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700601 iconThumb.setTag(null);
602 }
603 }
604 }
605 };
606
Steve McKay9276f3b2015-05-27 16:11:42 -0700607 private void openDocuments(final SparseBooleanArray selected) {
608 new GetDocumentsTask() {
609 @Override
610 void onDocumentsReady(List<DocumentInfo> docs) {
611 // TODO: Implement support in standalone for opening multiple docs.
612 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700613 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700614 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700615 }
616
Steve McKay9276f3b2015-05-27 16:11:42 -0700617 private void shareDocuments(final SparseBooleanArray selected) {
618 new GetDocumentsTask() {
619 @Override
620 void onDocumentsReady(List<DocumentInfo> docs) {
621 Intent intent;
622
623 // Filter out directories - those can't be shared.
624 List<DocumentInfo> docsForSend = Lists.newArrayList();
625 for (DocumentInfo doc: docs) {
626 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
627 docsForSend.add(doc);
628 }
629 }
630
631 if (docsForSend.size() == 1) {
632 final DocumentInfo doc = docsForSend.get(0);
633
634 intent = new Intent(Intent.ACTION_SEND);
635 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
636 intent.addCategory(Intent.CATEGORY_DEFAULT);
637 intent.setType(doc.mimeType);
638 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
639
640 } else if (docsForSend.size() > 1) {
641 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
642 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
643 intent.addCategory(Intent.CATEGORY_DEFAULT);
644
645 final ArrayList<String> mimeTypes = Lists.newArrayList();
646 final ArrayList<Uri> uris = Lists.newArrayList();
647 for (DocumentInfo doc : docsForSend) {
648 mimeTypes.add(doc.mimeType);
649 uris.add(doc.derivedUri);
650 }
651
652 intent.setType(findCommonMimeType(mimeTypes));
653 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
654
655 } else {
656 return;
657 }
658
659 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
660 startActivity(intent);
661 }
662 }.execute(selected);
663 }
664
665 private void deleteDocuments(final SparseBooleanArray selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700666 final Context context = getActivity();
667 final ContentResolver resolver = context.getContentResolver();
668
Steve McKay9276f3b2015-05-27 16:11:42 -0700669 new GetDocumentsTask() {
670 @Override
671 void onDocumentsReady(List<DocumentInfo> docs) {
672 boolean hadTrouble = false;
673 for (DocumentInfo doc : docs) {
674 if (!doc.isDeleteSupported()) {
675 Log.w(TAG, "Skipping " + doc);
676 hadTrouble = true;
677 continue;
678 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700679
Steve McKay9276f3b2015-05-27 16:11:42 -0700680 ContentProviderClient client = null;
681 try {
682 client = DocumentsApplication.acquireUnstableProviderOrThrow(
683 resolver, doc.derivedUri.getAuthority());
684 DocumentsContract.deleteDocument(client, doc.derivedUri);
685 } catch (Exception e) {
686 Log.w(TAG, "Failed to delete " + doc);
687 hadTrouble = true;
688 } finally {
689 ContentProviderClient.releaseQuietly(client);
690 }
691 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700692
Steve McKay9276f3b2015-05-27 16:11:42 -0700693 if (hadTrouble) {
694 Toast.makeText(
695 context,
696 R.string.toast_failed_delete,
697 Toast.LENGTH_SHORT).show();
698 }
699 }
700 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700701 }
702
Steve McKay9276f3b2015-05-27 16:11:42 -0700703 private void transferDocuments(final SparseBooleanArray selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700704 // Pop up a dialog to pick a destination. This is inadequate but works for now.
705 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900706 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900707 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900708 Uri.EMPTY,
709 getActivity(),
710 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700711
712 new GetDocumentsTask() {
713 @Override
714 void onDocumentsReady(List<DocumentInfo> docs) {
715 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
716
717 boolean directoryCopy = false;
718 for (DocumentInfo info : docs) {
719 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
720 directoryCopy = true;
721 break;
722 }
723 }
724 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
725 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
726 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900727 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700728 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700729 }
730
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700731 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700732 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700733 }
734
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700735 private static abstract class Footer {
736 private final int mItemViewType;
737
738 public Footer(int itemViewType) {
739 mItemViewType = itemViewType;
740 }
741
742 public abstract View getView(View convertView, ViewGroup parent);
743
744 public int getItemViewType() {
745 return mItemViewType;
746 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700747 }
748
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700749 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700750 public LoadingFooter() {
751 super(1);
752 }
753
Jeff Sharkey20b32272013-09-03 15:25:52 -0700754 @Override
755 public View getView(View convertView, ViewGroup parent) {
756 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700757 final State state = getDisplayState(DirectoryFragment.this);
758
Jeff Sharkey20b32272013-09-03 15:25:52 -0700759 if (convertView == null) {
760 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700761 if (state.derivedMode == MODE_LIST) {
762 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
763 } else if (state.derivedMode == MODE_GRID) {
764 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
765 } else {
766 throw new IllegalStateException();
767 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700768 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700769
Jeff Sharkey20b32272013-09-03 15:25:52 -0700770 return convertView;
771 }
772 }
773
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700774 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700775 private final int mIcon;
776 private final String mMessage;
777
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700778 public MessageFooter(int itemViewType, int icon, String message) {
779 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700780 mIcon = icon;
781 mMessage = message;
782 }
783
784 @Override
785 public View getView(View convertView, ViewGroup parent) {
786 final Context context = parent.getContext();
787 final State state = getDisplayState(DirectoryFragment.this);
788
789 if (convertView == null) {
790 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700791 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700792 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700793 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700794 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
795 } else {
796 throw new IllegalStateException();
797 }
798 }
799
800 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
801 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
802 icon.setImageResource(mIcon);
803 title.setText(mMessage);
804 return convertView;
805 }
806 }
807
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700808 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700809 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700810 private int mCursorCount;
811
812 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700813
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700814 public void swapResult(DirectoryResult result) {
815 mCursor = result != null ? result.cursor : null;
816 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700817
818 mFooters.clear();
819
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700820 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700821 if (extras != null) {
822 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
823 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700824 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700825 }
826 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
827 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700828 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700829 }
830 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
831 mFooters.add(new LoadingFooter());
832 }
833 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700834
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700835 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700836 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700837 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700838 }
839
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700840 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700841 mEmptyView.setVisibility(View.VISIBLE);
842 } else {
843 mEmptyView.setVisibility(View.GONE);
844 }
845
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700846 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700847 }
848
849 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700850 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700851 if (position < mCursorCount) {
852 return getDocumentView(position, convertView, parent);
853 } else {
854 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700855 convertView = mFooters.get(position).getView(convertView, parent);
856 // Only the view itself is disabled; contents inside shouldn't
857 // be dimmed.
858 convertView.setEnabled(false);
859 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700860 }
861 }
862
863 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700864 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700865 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700866
Jeff Sharkey9656a532013-09-13 13:42:19 -0700867 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
868
Jeff Sharkey873daa32013-08-18 17:38:20 -0700869 final RootsCache roots = DocumentsApplication.getRootsCache(context);
870 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
871 context, mThumbSize);
872
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700873 if (convertView == null) {
874 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700875 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700876 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700877 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700878 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
879 } else {
880 throw new IllegalStateException();
881 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700882 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700883
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700884 final Cursor cursor = getItem(position);
885
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700886 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
887 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700888 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
889 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
890 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
891 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
892 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
893 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
894 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
895 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700896
Jeff Sharkey9656a532013-09-13 13:42:19 -0700897 final View line1 = convertView.findViewById(R.id.line1);
898 final View line2 = convertView.findViewById(R.id.line2);
899
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700900 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
901 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700902 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
903 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700904 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700905 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
906 final TextView date = (TextView) convertView.findViewById(R.id.date);
907 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700908
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700909 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700910 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700911 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700912 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700913 }
914
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700915 iconMime.animate().cancel();
916 iconThumb.animate().cancel();
917
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700918 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700919 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700920 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700921 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700922
Jeff Sharkey7e544612014-08-29 15:38:27 -0700923 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
924 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
925
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700926 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700927 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700928 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700929 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700930 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700931 iconThumb.setImageBitmap(cachedResult);
932 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700933 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700934 iconThumb.setImageDrawable(null);
935 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700936 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700937 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700938 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700939 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700940 }
941
942 // Always throw MIME icon into place, even when a thumbnail is being
943 // loaded in background.
944 if (cacheHit) {
945 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700946 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700947 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700948 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700949 iconMime.setAlpha(1f);
950 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700951 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700952 iconMime.setImageDrawable(
953 getDocumentIcon(context, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700954 }
955
Jeff Sharkey9656a532013-09-13 13:42:19 -0700956 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700957 boolean hasLine2 = false;
958
Jeff Sharkey9656a532013-09-13 13:42:19 -0700959 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
960 if (!hideTitle) {
961 title.setText(docDisplayName);
962 hasLine1 = true;
963 }
964
965 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700966 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700967 // We've already had to enumerate roots before any results can
968 // be shown, so this will never block.
969 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700970 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700971 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700972 } else {
973 iconDrawable = root.loadIcon(context);
974 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700975
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700976 if (summary != null) {
977 final boolean alwaysShowSummary = getResources()
978 .getBoolean(R.bool.always_show_summary);
979 if (alwaysShowSummary) {
980 summary.setText(root.getDirectoryString());
981 summary.setVisibility(View.VISIBLE);
982 hasLine2 = true;
983 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700984 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700985 // No summary needed if icon speaks for itself
986 summary.setVisibility(View.INVISIBLE);
987 } else {
988 summary.setText(root.getDirectoryString());
989 summary.setVisibility(View.VISIBLE);
990 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
991 hasLine2 = true;
992 }
993 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700994 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700995 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700996 // Directories showing thumbnails in grid mode get a little icon
997 // hint to remind user they're a directory.
998 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
999 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -07001000 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001001 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001002 }
1003
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001004 if (summary != null) {
1005 if (docSummary != null) {
1006 summary.setText(docSummary);
1007 summary.setVisibility(View.VISIBLE);
1008 hasLine2 = true;
1009 } else {
1010 summary.setVisibility(View.INVISIBLE);
1011 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001012 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001013 }
1014
Jeff Sharkey9656a532013-09-13 13:42:19 -07001015 if (icon1 != null) icon1.setVisibility(View.GONE);
1016 if (icon2 != null) icon2.setVisibility(View.GONE);
1017
1018 if (iconDrawable != null) {
1019 if (hasLine1) {
1020 icon1.setVisibility(View.VISIBLE);
1021 icon1.setImageDrawable(iconDrawable);
1022 } else {
1023 icon2.setVisibility(View.VISIBLE);
1024 icon2.setImageDrawable(iconDrawable);
1025 }
1026 }
1027
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001028 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001029 date.setText(null);
1030 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001031 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001032 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001033 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001034
1035 if (state.showSize) {
1036 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001037 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001038 size.setText(null);
1039 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001040 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001041 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001042 }
1043 } else {
1044 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001045 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001046
Jeff Sharkey9656a532013-09-13 13:42:19 -07001047 if (line1 != null) {
1048 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1049 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001050 if (line2 != null) {
1051 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1052 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001053
Jeff Sharkey7e544612014-08-29 15:38:27 -07001054 setEnabledRecursive(convertView, enabled);
1055
1056 iconMime.setAlpha(iconAlpha);
1057 iconThumb.setAlpha(iconAlpha);
1058 if (icon1 != null) icon1.setAlpha(iconAlpha);
1059 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001060
Steve McKay8e258c62015-05-06 14:27:57 -07001061 if (DEBUG_ENABLE_DND) {
1062 setupDragAndDropOnDocumentView(convertView, cursor);
1063 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001064
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001065 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001066 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001067
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001068 @Override
1069 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001070 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001071 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001072
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001073 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001074 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001075 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001076 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001077 return mCursor;
1078 } else {
1079 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001080 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001081 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001082
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001083 @Override
1084 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001085 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001086 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001087
1088 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001089 public int getViewTypeCount() {
1090 return 4;
1091 }
1092
1093 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001094 public int getItemViewType(int position) {
1095 if (position < mCursorCount) {
1096 return 0;
1097 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001098 position -= mCursorCount;
1099 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001100 }
1101 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001102 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001103
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001104 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1105 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001106 private final Uri mUri;
1107 private final ImageView mIconMime;
1108 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001109 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001110 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001111 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001112
Jeff Sharkey7e544612014-08-29 15:38:27 -07001113 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1114 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001115 mUri = uri;
1116 mIconMime = iconMime;
1117 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001118 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001119 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001120 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001121 }
1122
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001123 @Override
1124 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001125 cancel(false);
1126 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001127 }
1128
1129 @Override
1130 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001131 if (isCancelled()) return null;
1132
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001133 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001134 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001135
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001136 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001137 Bitmap result = null;
1138 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001139 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1140 resolver, mUri.getAuthority());
1141 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001142 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001143 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1144 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001145 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001146 }
1147 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001148 if (!(e instanceof OperationCanceledException)) {
1149 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1150 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001151 } finally {
1152 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001153 }
1154 return result;
1155 }
1156
1157 @Override
1158 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001159 if (mIconThumb.getTag() == this && result != null) {
1160 mIconThumb.setTag(null);
1161 mIconThumb.setImageBitmap(result);
1162
Jeff Sharkey7e544612014-08-29 15:38:27 -07001163 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001164 mIconMime.animate().alpha(0f).start();
1165 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001166 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001167 }
1168 }
1169 }
1170
1171 private static String formatTime(Context context, long when) {
1172 // TODO: DateUtils should make this easier
1173 Time then = new Time();
1174 then.set(when);
1175 Time now = new Time();
1176 now.setToNow();
1177
1178 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1179 | DateUtils.FORMAT_ABBREV_ALL;
1180
1181 if (then.year != now.year) {
1182 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1183 } else if (then.yearDay != now.yearDay) {
1184 flags |= DateUtils.FORMAT_SHOW_DATE;
1185 } else {
1186 flags |= DateUtils.FORMAT_SHOW_TIME;
1187 }
1188
1189 return DateUtils.formatDateTime(context, when, flags);
1190 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001191
1192 private String findCommonMimeType(List<String> mimeTypes) {
1193 String[] commonType = mimeTypes.get(0).split("/");
1194 if (commonType.length != 2) {
1195 return "*/*";
1196 }
1197
1198 for (int i = 1; i < mimeTypes.size(); i++) {
1199 String[] type = mimeTypes.get(i).split("/");
1200 if (type.length != 2) continue;
1201
1202 if (!commonType[1].equals(type[1])) {
1203 commonType[1] = "*";
1204 }
1205
1206 if (!commonType[0].equals(type[0])) {
1207 commonType[0] = "*";
1208 commonType[1] = "*";
1209 break;
1210 }
1211 }
1212
1213 return commonType[0] + "/" + commonType[1];
1214 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001215
1216 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001217 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001218 if (v.isEnabled() == enabled) return;
1219 v.setEnabled(enabled);
1220
1221 if (v instanceof ViewGroup) {
1222 final ViewGroup vg = (ViewGroup) v;
1223 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1224 setEnabledRecursive(vg.getChildAt(i), enabled);
1225 }
1226 }
1227 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001228
1229 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1230 final State state = getDisplayState(DirectoryFragment.this);
1231
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001232 // Directories are always enabled
1233 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1234 return true;
1235 }
1236
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001237 // Read-only files are disabled when creating
1238 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1239 return false;
1240 }
1241
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001242 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1243 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001244
Steve McKay1f199482015-05-20 15:58:42 -07001245 private @NonNull List<DocumentInfo> getSelectedDocuments() {
1246 return getItemsAsDocuments(mCurrentView.getCheckedItemPositions().clone());
1247 }
1248
1249 private List<DocumentInfo> getItemsAsDocuments(SparseBooleanArray items) {
1250 if (items == null || items.size() == 0) {
1251 return new ArrayList<>(0);
1252 }
1253
1254 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1255 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001256 for (int i = 0; i < size; i++) {
Steve McKay1f199482015-05-20 15:58:42 -07001257 if (items.valueAt(i)) {
1258 final Cursor cursor = mAdapter.getItem(items.keyAt(i));
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001259 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1260 docs.add(doc);
1261 }
1262 }
1263 return docs;
1264 }
1265
Steve McKay1f199482015-05-20 15:58:42 -07001266 private void copyFromClipboard() {
1267 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1268
1269 @Override
1270 protected List<DocumentInfo> doInBackground(Void... params) {
1271 return mClipper.getClippedDocuments();
1272 }
1273
1274 @Override
1275 protected void onPostExecute(List<DocumentInfo> docs) {
1276 DocumentInfo destination =
1277 ((BaseActivity) getActivity()).getCurrentDirectory();
1278 copyDocuments(docs, destination);
1279 }
1280 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001281 }
1282
Steve McKay1f199482015-05-20 15:58:42 -07001283 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
1284 Preconditions.checkNotNull(clipData);
1285 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001286
Steve McKay1f199482015-05-20 15:58:42 -07001287 @Override
1288 protected List<DocumentInfo> doInBackground(Void... params) {
1289 return mClipper.getDocumentsFromClipData(clipData);
1290 }
1291
1292 @Override
1293 protected void onPostExecute(List<DocumentInfo> docs) {
1294 copyDocuments(docs, destination);
1295 }
1296 }.execute();
1297 }
1298
1299 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1300 if (!canCopy(docs, destination)) {
1301 Toast.makeText(
1302 getActivity(),
1303 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001304 return;
1305 }
1306
Steve McKay1f199482015-05-20 15:58:42 -07001307 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001308 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001309 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001310
Steve McKay1f199482015-05-20 15:58:42 -07001311 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001312 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001313 if (destination != null) {
1314 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001315 tmpStack.addAll(curStack);
1316 } else {
1317 tmpStack = curStack;
1318 }
1319
Steve McKay1f199482015-05-20 15:58:42 -07001320 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001321 }
1322
1323 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1324 Context context = getActivity();
1325 final ContentResolver resolver = context.getContentResolver();
1326 ClipData clipData = null;
1327 for (DocumentInfo doc : docs) {
1328 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1329 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001330 // TODO: figure out what this string should be.
1331 // Currently it is not displayed anywhere in the UI, but this might change.
1332 final String label = "";
1333 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001334 } else {
1335 // TODO: update list of mime types in ClipData.
1336 clipData.addItem(new ClipData.Item(uri));
1337 }
1338 }
1339 return clipData;
1340 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001341
Steve McKay1f199482015-05-20 15:58:42 -07001342 void copySelectedToClipboard() {
Steve McKay9276f3b2015-05-27 16:11:42 -07001343 copySelectionToClipboard(mCurrentView.getCheckedItemPositions().clone());
1344 }
Steve McKay0599a442015-05-05 14:50:00 -07001345
Steve McKay9276f3b2015-05-27 16:11:42 -07001346 void copySelectionToClipboard(SparseBooleanArray selected) {
1347 new GetDocumentsTask() {
1348 @Override
1349 void onDocumentsReady(List<DocumentInfo> docs) {
1350 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001351 Activity activity = getActivity();
1352 Toast.makeText(activity,
1353 activity.getResources().getQuantityString(
1354 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1355 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001356 }
1357 }.execute(selected);
Steve McKay0599a442015-05-05 14:50:00 -07001358 }
1359
1360 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001361 copyFromClipboard();
1362 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001363 }
1364
Steve McKay0599a442015-05-05 14:50:00 -07001365 /**
1366 * Returns true if the list of files can be copied to destination. Note that this
1367 * is a policy check only. Currently the method does not attempt to verify
1368 * available space or any other environmental aspects possibly resulting in
1369 * failure to copy.
1370 *
1371 * @return true if the list of files can be copied to destination.
1372 */
1373 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1374 BaseActivity activity = (BaseActivity)getActivity();
1375
1376 final RootInfo root = activity.getCurrentRoot();
1377
1378 // Can't copy folders to Downloads.
1379 if (root.isDownloads()) {
1380 for (DocumentInfo docs : files) {
1381 if (docs.isDirectory()) {
1382 return false;
1383 }
1384 }
1385 }
1386
1387 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1388 }
1389
1390 void selectAllFiles() {
1391 int count = mCurrentView.getCount();
1392 for (int i = 0; i < count; i++) {
1393 mCurrentView.setItemChecked(i, true);
1394 }
1395 updateDisplayState();
1396 }
1397
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001398 private void setupDragAndDropOnDirectoryView(AbsListView view) {
1399 // Listen for drops on non-directory items and empty space.
1400 view.setOnDragListener(mOnDragListener);
1401 }
1402
1403 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1404 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1405 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1406 // Make a directory item a drop target. Drop on non-directories and empty space
1407 // is handled at the list/grid view level.
1408 view.setOnDragListener(mOnDragListener);
1409 }
1410
1411 // Temporary: attaching the listener to the title only.
1412 // Attaching to the entire item conflicts with the item long click handler responsible
1413 // for item selection.
1414 final View title = view.findViewById(android.R.id.title);
1415 title.setOnLongClickListener(mLongClickListener);
1416 }
1417
1418 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1419 @Override
1420 public boolean onDrag(View v, DragEvent event) {
1421 switch (event.getAction()) {
1422 case DragEvent.ACTION_DRAG_STARTED:
1423 // TODO: Check if the event contains droppable data.
1424 return true;
1425
1426 // TODO: Highlight potential drop target directory?
1427 // TODO: Expand drop target directory on hover?
1428 case DragEvent.ACTION_DRAG_ENTERED:
1429 case DragEvent.ACTION_DRAG_LOCATION:
1430 case DragEvent.ACTION_DRAG_EXITED:
1431 case DragEvent.ACTION_DRAG_ENDED:
1432 return true;
1433
1434 case DragEvent.ACTION_DROP:
1435 int dstPosition = mCurrentView.getPositionForView(v);
1436 DocumentInfo dstDir = null;
1437 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1438 Cursor dstCursor = mAdapter.getItem(dstPosition);
1439 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1440 // TODO: Do not drop into the directory where the documents came from.
1441 }
1442 copyFromClipData(event.getClipData(), dstDir);
1443 return true;
1444 }
1445 return false;
1446 }
1447 };
1448
1449 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1450 @Override
1451 public boolean onLongClick(View v) {
1452 final List<DocumentInfo> docs = getDraggableDocuments(v);
1453 if (docs.isEmpty()) {
1454 return false;
1455 }
1456 v.startDrag(
1457 getClipDataFromDocuments(docs),
1458 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1459 null,
1460 View.DRAG_FLAG_GLOBAL
1461 );
1462 return true;
1463 }
1464 };
1465
1466 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
1467 final int position = mCurrentView.getPositionForView(currentItemView);
1468 if (position == android.widget.AdapterView.INVALID_POSITION) {
1469 return Collections.EMPTY_LIST;
1470 }
1471
1472 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1473 if (!selectedDocs.isEmpty()) {
1474 if (!mCurrentView.isItemChecked(position)) {
1475 // There is a selection that does not include the current item, drag nothing.
1476 return Collections.EMPTY_LIST;
1477 }
1478 return selectedDocs;
1479 }
1480
1481 final Cursor cursor = mAdapter.getItem(position);
1482 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1483 return Lists.newArrayList(doc);
1484 }
1485
1486 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1487 if (docs.size() == 1) {
1488 final DocumentInfo doc = docs.get(0);
1489 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1490 doc.mimeType, doc.icon, getDisplayState(this));
1491 }
1492 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1493 }
1494
1495 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1496 String docMimeType, int docIcon, State state) {
1497 if (docIcon != 0) {
1498 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1499 } else {
1500 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1501 state.derivedMode);
1502 }
1503 }
1504
1505 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1506
1507 private final Drawable mShadow;
1508
1509 private final int mShadowDimension;
1510
1511 public DrawableShadowBuilder(Drawable shadow) {
1512 mShadow = shadow;
1513 mShadowDimension = getResources().getDimensionPixelSize(
1514 R.dimen.drag_shadow_size);
1515 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1516 }
1517
1518 public void onProvideShadowMetrics(
1519 Point shadowSize, Point shadowTouchPoint) {
1520 shadowSize.set(mShadowDimension, mShadowDimension);
1521 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1522 }
1523
1524 public void onDrawShadow(Canvas canvas) {
1525 mShadow.draw(canvas);
1526 }
1527 }
Steve McKay1f199482015-05-20 15:58:42 -07001528
1529 private FragmentTuner pickFragmentTuner(final State state) {
1530 return state.action == ACTION_BROWSE_ALL
1531 ? new StandaloneTuner()
1532 : new DefaultTuner(state);
1533 }
1534
1535 /**
1536 * Interface for specializing the Fragment for the "host" Activity.
1537 * Feel free to expand the role of this class to handle other specializations.
1538 */
1539 private interface FragmentTuner {
1540 void updateActionMenu(Menu menu, int dirType);
1541 }
1542
1543 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001544 * Abstract task providing support for loading documents *off*
1545 * the main thread. And if it isn't obvious, creating a list
1546 * of documents (especially large lists) can be pretty expensive.
1547 */
1548 private abstract class GetDocumentsTask
1549 extends AsyncTask<SparseBooleanArray, Void, List<DocumentInfo>> {
1550 @Override
1551 protected final List<DocumentInfo> doInBackground(SparseBooleanArray... selected) {
1552 return getItemsAsDocuments(selected[0]);
1553 }
1554
1555 @Override
1556 protected final void onPostExecute(List<DocumentInfo> docs) {
1557 onDocumentsReady(docs);
1558 }
1559
1560 abstract void onDocumentsReady(List<DocumentInfo> docs);
1561 }
1562
1563 /**
Steve McKay1f199482015-05-20 15:58:42 -07001564 * Provides support for Platform specific specializations of DirectoryFragment.
1565 */
1566 private static final class DefaultTuner implements FragmentTuner {
1567
1568 private final State mState;
1569
1570 public DefaultTuner(State state) {
1571 mState = state;
1572 }
1573
1574 @Override
1575 public void updateActionMenu(Menu menu, int dirType) {
1576 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1577
1578 final MenuItem open = menu.findItem(R.id.menu_open);
1579 final MenuItem share = menu.findItem(R.id.menu_share);
1580 final MenuItem delete = menu.findItem(R.id.menu_delete);
1581 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1582 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1583 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1584
1585 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1586 || mState.action == ACTION_BROWSE);
1587
1588 open.setVisible(!manageOrBrowse);
1589 share.setVisible(manageOrBrowse);
1590 delete.setVisible(manageOrBrowse);
1591 // Disable copying from the Recents view.
1592 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1593 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1594
1595 // Only shown in standalone mode.
1596 copyToClipboard.setVisible(false);
1597 }
1598 }
1599
1600 /**
1601 * Provides support for Standalone specific specializations of DirectoryFragment.
1602 */
1603 private static final class StandaloneTuner implements FragmentTuner {
1604 @Override
1605 public void updateActionMenu(Menu menu, int dirType) {
1606 menu.findItem(R.id.menu_share).setVisible(true);
1607 menu.findItem(R.id.menu_delete).setVisible(true);
1608 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1609
1610 menu.findItem(R.id.menu_open).setVisible(false);
1611 menu.findItem(R.id.menu_copy_to).setVisible(false);
1612 menu.findItem(R.id.menu_move_to).setVisible(false);
1613 }
1614 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001615}