blob: 0554601cd782c3627f4cb1816a84967e5a7d061a [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Steve McKayef280152015-06-11 10:10:49 -070031import static com.android.internal.util.Preconditions.checkNotNull;
Steve McKay0599a442015-05-05 14:50:00 -070032
Steve McKay1f199482015-05-20 15:58:42 -070033import android.annotation.NonNull;
Ben Kwaf5858932015-04-07 15:43:39 -070034import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070035import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070036import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.FragmentManager;
38import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070040import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070041import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070043import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070045import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070047import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.graphics.Bitmap;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070050import android.graphics.Canvas;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070054import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070056import android.os.CancellationSignal;
Makoto Onuki77778752015-07-01 14:55:14 -070057import android.os.Handler;
58import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070059import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070060import android.os.Parcelable;
Steve McKay8e258c62015-05-06 14:27:57 -070061import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070063import android.provider.DocumentsContract.Document;
Steve McKayef280152015-06-11 10:10:49 -070064import android.support.v7.widget.GridLayoutManager;
65import android.support.v7.widget.LinearLayoutManager;
66import android.support.v7.widget.RecyclerView;
67import android.support.v7.widget.RecyclerView.LayoutManager;
68import android.support.v7.widget.RecyclerView.OnItemTouchListener;
69import android.support.v7.widget.RecyclerView.RecyclerListener;
70import android.support.v7.widget.RecyclerView.ViewHolder;
Jeff Sharkey6d579272015-06-11 09:16:19 -070071import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070073import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070074import android.text.format.Time;
75import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070076import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070077import android.view.ActionMode;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -070078import android.view.DragEvent;
Steve McKayef280152015-06-11 10:10:49 -070079import android.view.GestureDetector;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070082import android.view.MenuItem;
Steve McKayef280152015-06-11 10:10:49 -070083import android.view.MotionEvent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070084import android.view.View;
85import android.view.ViewGroup;
Steve McKayef280152015-06-11 10:10:49 -070086import android.view.ViewParent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070087import android.widget.ImageView;
88import android.widget.ListView;
89import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070090import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070091
Steve McKayd0a2a2c2015-03-25 14:35:33 -070092import com.android.documentsui.BaseActivity.State;
Steve McKayef280152015-06-11 10:10:49 -070093import com.android.documentsui.MultiSelectManager.Selection;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070094import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070095import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070096import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090097import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070098import com.android.documentsui.model.RootInfo;
Steve McKay1f199482015-05-20 15:58:42 -070099import com.android.internal.util.Preconditions;
Steve McKay0599a442015-05-05 14:50:00 -0700100
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101import com.google.android.collect.Lists;
102
103import java.util.ArrayList;
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -0700104import java.util.Collections;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700105import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700106
107/**
108 * Display the documents inside a single directory.
109 */
110public class DirectoryFragment extends Fragment {
111
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700112 public static final int TYPE_NORMAL = 1;
113 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700114 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700115
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700116 public static final int ANIM_NONE = 1;
117 public static final int ANIM_SIDE = 2;
118 public static final int ANIM_DOWN = 3;
119 public static final int ANIM_UP = 4;
120
Ben Kwaf5858932015-04-07 15:43:39 -0700121 public static final int REQUEST_COPY_DESTINATION = 1;
122
Steve McKayef280152015-06-11 10:10:49 -0700123 private static final int LOADER_ID = 42;
124 private static final boolean DEBUG = false;
Steve McKay8e258c62015-05-06 14:27:57 -0700125 private static final boolean DEBUG_ENABLE_DND = false;
126
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 private static final String EXTRA_ROOT = "root";
129 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700130 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700131 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700132
Steve McKayef280152015-06-11 10:10:49 -0700133 private final Handler mHandler = new Handler(Looper.getMainLooper());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134
Steve McKayef280152015-06-11 10:10:49 -0700135 private View mEmptyView;
136 private RecyclerView mRecView;
137
138 private int mType = TYPE_NORMAL;
139 private String mStateKey;
140
141 private int mLastMode = MODE_UNKNOWN;
142 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
143 private boolean mLastShowSize;
144 private boolean mHideGridTitles;
145 private boolean mSvelteRecents;
146 private Point mThumbSize;
147 private DocumentsAdapter mAdapter;
148 private LoaderCallbacks<DirectoryResult> mCallbacks;
Steve McKay1f199482015-05-20 15:58:42 -0700149 private FragmentTuner mFragmentTuner;
150 private DocumentClipper mClipper;
Steve McKayef280152015-06-11 10:10:49 -0700151 private MultiSelectManager mSelectionManager;
152 // These are lazily initialized.
153 private LayoutManager mListLayout;
154 private LayoutManager mGridLayout;
Steve McKay1f199482015-05-20 15:58:42 -0700155
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700156 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
157 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700158 }
159
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700160 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
161 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700162 }
163
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700164 public static void showRecentsOpen(FragmentManager fm, int anim) {
165 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700166 }
167
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700168 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
169 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700170 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700171 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700172 args.putParcelable(EXTRA_ROOT, root);
173 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700174 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700175
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700176 final FragmentTransaction ft = fm.beginTransaction();
177 switch (anim) {
178 case ANIM_SIDE:
179 args.putBoolean(EXTRA_IGNORE_STATE, true);
180 break;
181 case ANIM_DOWN:
182 args.putBoolean(EXTRA_IGNORE_STATE, true);
183 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
184 break;
185 case ANIM_UP:
186 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
187 break;
188 }
189
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700190 final DirectoryFragment fragment = new DirectoryFragment();
191 fragment.setArguments(args);
192
Jeff Sharkey76112212013-08-06 11:26:10 -0700193 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700194 ft.commitAllowingStateLoss();
195 }
196
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700197 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
198 final StringBuilder builder = new StringBuilder();
199 builder.append(root != null ? root.authority : "null").append(';');
200 builder.append(root != null ? root.rootId : "null").append(';');
201 builder.append(doc != null ? doc.documentId : "null");
202 return builder.toString();
203 }
204
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700205 public static DirectoryFragment get(FragmentManager fm) {
206 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700207 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700208 }
209
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700210 @Override
211 public View onCreateView(
212 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
213 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700214 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700215 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
216
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700217 mEmptyView = view.findViewById(android.R.id.empty);
218
Steve McKayef280152015-06-11 10:10:49 -0700219 mRecView = (RecyclerView) view.findViewById(R.id.recyclerView);
220 mRecView.setRecyclerListener(
221 new RecyclerListener() {
222 @Override
223 public void onViewRecycled(ViewHolder holder) {
224 cancelThumbnailTask(holder.itemView);
225 }
226 });
227 // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration).
Steve McKay8e258c62015-05-06 14:27:57 -0700228
229 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -0700230 setupDragAndDropOnDirectoryView(mRecView);
Steve McKay8e258c62015-05-06 14:27:57 -0700231 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700232
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700233 return view;
234 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700235
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700236 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700237 public void onDestroyView() {
238 super.onDestroyView();
239
240 // Cancel any outstanding thumbnail requests
Steve McKayef280152015-06-11 10:10:49 -0700241 final int count = mRecView.getChildCount();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700242 for (int i = 0; i < count; i++) {
Steve McKayef280152015-06-11 10:10:49 -0700243 final View view = mRecView.getChildAt(i);
244 cancelThumbnailTask(view);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700245 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700246
Steve McKayef280152015-06-11 10:10:49 -0700247 // Clear any outstanding selection
248 mSelectionManager.clearSelection();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700249 }
250
251 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700252 public void onActivityCreated(Bundle savedInstanceState) {
253 super.onActivityCreated(savedInstanceState);
254
255 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700256 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700257
Jeff Sharkey9656a532013-09-13 13:42:19 -0700258 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
259 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
260
Steve McKayef280152015-06-11 10:10:49 -0700261 mAdapter = new DocumentsAdapter(context);
262 mRecView.setAdapter(mAdapter);
263
264 GestureDetector.SimpleOnGestureListener listener =
265 new GestureDetector.SimpleOnGestureListener() {
266 @Override
267 public boolean onSingleTapUp(MotionEvent e) {
268 return DirectoryFragment.this.onSingleTapUp(e);
269 }
270 };
271
272 mSelectionManager = new MultiSelectManager(mRecView, listener);
273 mSelectionManager.addCallback(new SelectionModeListener());
274
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700275 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700276 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700277
Steve McKay1f199482015-05-20 15:58:42 -0700278 mFragmentTuner = pickFragmentTuner(state);
279 mClipper = new DocumentClipper(context);
280
Jeff Sharkey9656a532013-09-13 13:42:19 -0700281 if (mType == TYPE_RECENT_OPEN) {
282 // Hide titles when showing recents for picking images/videos
283 mHideGridTitles = MimePredicate.mimeMatches(
284 MimePredicate.VISUAL_MIMES, state.acceptMimes);
285 } else {
286 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
287 }
288
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700289 final ActivityManager am = (ActivityManager) context.getSystemService(
290 Context.ACTIVITY_SERVICE);
291 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
292
Jeff Sharkey46899c82013-08-18 22:26:48 -0700293 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700294 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700295 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700296 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700297
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700298 Uri contentsUri;
299 switch (mType) {
300 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700301 contentsUri = DocumentsContract.buildChildDocumentsUri(
302 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700303 if (state.action == ACTION_MANAGE) {
304 contentsUri = DocumentsContract.setManageMode(contentsUri);
305 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700306 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700307 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700308 case TYPE_SEARCH:
309 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700310 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700311 if (state.action == ACTION_MANAGE) {
312 contentsUri = DocumentsContract.setManageMode(contentsUri);
313 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700314 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700315 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700316 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700317 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700318 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700319 default:
320 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700321 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700322 }
323
324 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700325 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700326 if (result == null || result.exception != null) {
327 // onBackPressed does a fragment transaction, which can't be done inside
328 // onLoadFinished
329 mHandler.post(new Runnable() {
330 @Override
331 public void run() {
332 final Activity activity = getActivity();
333 if (activity != null) {
334 activity.onBackPressed();
335 }
336 }
337 });
338 return;
339 }
340
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700341 if (!isAdded()) return;
342
Steve McKayef280152015-06-11 10:10:49 -0700343 mAdapter.replaceResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700344
345 // Push latest state up to UI
346 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700347 if (result.mode != MODE_UNKNOWN) {
348 state.derivedMode = result.mode;
349 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700350 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700351 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700352
353 updateDisplayState();
354
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700355 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700356 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
357 context instanceof DocumentsActivity) {
358 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700359 }
360
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700361 // Restore any previous instance state
362 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
363 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
364 getView().restoreHierarchyState(container);
365 } else if (mLastSortOrder != state.derivedSortOrder) {
Steve McKayef280152015-06-11 10:10:49 -0700366 mRecView.smoothScrollToPosition(0);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700367 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700368
369 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700370 }
371
372 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700373 public void onLoaderReset(Loader<DirectoryResult> loader) {
Steve McKayef280152015-06-11 10:10:49 -0700374 mAdapter.replaceResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700375 }
376 };
377
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700378 // Kick off loader at least once
Steve McKayef280152015-06-11 10:10:49 -0700379 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700380
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700381 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700382 }
383
Jeff Sharkey42d26792013-09-06 13:22:09 -0700384 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700385 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700386 // There's only one request code right now. Replace this with a switch statement or
387 // something more scalable when more codes are added.
388 if (requestCode != REQUEST_COPY_DESTINATION) {
389 return;
390 }
391 if (resultCode == Activity.RESULT_CANCELED || data == null) {
392 // User pressed the back button or otherwise cancelled the destination pick. Don't
393 // proceed with the copy.
394 return;
395 }
396
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900397 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700398 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
399 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700400 }
401
Steve McKayef280152015-06-11 10:10:49 -0700402 private int getEventAdapterPosition(MotionEvent e) {
403 View view = mRecView.findChildViewUnder(e.getX(), e.getY());
404 return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION;
405 }
406
407 private boolean onSingleTapUp(MotionEvent e) {
408 int position = getEventAdapterPosition(e);
409
410 if (position != RecyclerView.NO_POSITION) {
411 final Cursor cursor = mAdapter.getItem(position);
412 checkNotNull(cursor, "Cursor cannot be null.");
413 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
414 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
415 if (isDocumentEnabled(docMimeType, docFlags)) {
416 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
417 ((BaseActivity) getActivity()).onDocumentPicked(doc);
418 return true;
419 }
420 }
421
422 return false;
423 }
424
Ben Kwaf5858932015-04-07 15:43:39 -0700425 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700426 public void onStop() {
427 super.onStop();
428
429 // Remember last scroll location
430 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
431 getView().saveHierarchyState(container);
432 final State state = getDisplayState(this);
433 state.dirState.put(mStateKey, container);
434 }
435
436 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700437 public void onResume() {
438 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700439 updateDisplayState();
440 }
441
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700442 public void onDisplayStateChanged() {
443 updateDisplayState();
444 }
445
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700446 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700447 // Sort order change always triggers reload; we'll trigger state change
448 // on the flip side.
Steve McKayef280152015-06-11 10:10:49 -0700449 getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700450 }
451
452 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700453 final ContentResolver resolver = getActivity().getContentResolver();
454 final State state = getDisplayState(this);
455
456 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
457 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
458
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700459 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700460 final Uri stateUri = RecentsProvider.buildState(
461 root.authority, root.rootId, doc.documentId);
462 final ContentValues values = new ContentValues();
463 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700464
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700465 new AsyncTask<Void, Void, Void>() {
466 @Override
467 protected Void doInBackground(Void... params) {
468 resolver.insert(stateUri, values);
469 return null;
470 }
471 }.execute();
472 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700473
474 // Mode change is just visual change; no need to kick loader, and
475 // deliver change event immediately.
476 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700477 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700478
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700479 updateDisplayState();
480 }
481
482 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700483 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700484
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700485 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700486 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700487 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700488
Steve McKayef280152015-06-11 10:10:49 -0700489 updateLayout(state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700490
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700491 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700492 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700493 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
494 } else {
495 choiceMode = ListView.CHOICE_MODE_NONE;
496 }
497
Steve McKayef280152015-06-11 10:10:49 -0700498 final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
499 mThumbSize = new Point(thumbSize, thumbSize);
500 mRecView.setAdapter(mAdapter);
501 }
502
503 /**
504 * Returns a {@code LayoutManager} for {@code mode}, lazily initializing
505 * classes as needed.
506 */
507 private void updateLayout(int mode) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700508 final int thumbSize;
Steve McKayef280152015-06-11 10:10:49 -0700509
510 final LayoutManager layout;
511 switch (mode) {
512 case MODE_GRID:
513 if (mGridLayout == null) {
514 // TODO: Determine appropriate column count.
515 mGridLayout = new GridLayoutManager(getContext(), 4);
516 }
517 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
518 layout = mGridLayout;
519 break;
520 case MODE_LIST:
521 if (mListLayout == null) {
522 mListLayout = new LinearLayoutManager(getContext());
523 }
524 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
525 layout = mListLayout;
526 break;
527 case MODE_UNKNOWN:
528 default:
529 throw new IllegalArgumentException("Unsupported layout mode: " + mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700530 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700531
Steve McKayef280152015-06-11 10:10:49 -0700532 mRecView.setLayoutManager(layout);
533 // setting layout manager automatically invalidates existing ViewHolders.
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700534 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700535 }
536
Steve McKayef280152015-06-11 10:10:49 -0700537 /**
538 * Manages the integration between our ActionMode and MultiSelectManager, initiating
539 * ActionMode when there is a selection, canceling it when there is no selection,
540 * and clearing selection when action mode is explicitly exited by the user.
541 */
542 private final class SelectionModeListener
543 implements MultiSelectManager.Callback, ActionMode.Callback {
544
545 private Selection mSelected = new Selection();
546 private ActionMode mActionMode;
547
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700548 @Override
Steve McKayef280152015-06-11 10:10:49 -0700549 public boolean onBeforeItemStateChange(int position, boolean selected) {
550 // Directories and footer items cannot be checked
551 if (selected) {
552 final Cursor cursor = mAdapter.getItem(position);
553 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700554 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
555 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Steve McKayef280152015-06-11 10:10:49 -0700556 return isDocumentEnabled(docMimeType, docFlags);
557 }
558 return true;
559 }
560
561 @Override
562 public void onItemStateChanged(int position, boolean selected) {
563 mSelectionManager.getSelection(mSelected);
564 if (mSelected.size() == 0) {
565 if (DEBUG) Log.d(TAG, "Finishing action mode.");
566 if (mActionMode != null) {
567 mActionMode.finish();
568 }
569 } else {
570 if (DEBUG) Log.d(TAG, "Maybe starting action mode.");
571 if (mActionMode == null) {
572 if (DEBUG) Log.d(TAG, "Yeah. Starting action mode.");
573 mActionMode = getActivity().startActionMode(this);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700574 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700575 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700576
Steve McKayef280152015-06-11 10:10:49 -0700577 if (mActionMode != null) {
578 mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size()));
579 }
580 }
581
582 // Called when the user exits the action mode
583 @Override
584 public void onDestroyActionMode(ActionMode mode) {
585 if (DEBUG) Log.d(TAG, "Handling action mode destroyed.");
586 mActionMode = null;
587 // clear selection
588 mSelectionManager.clearSelection();
589 }
590
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700591 @Override
592 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
593 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Steve McKayef280152015-06-11 10:10:49 -0700594 mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size()));
595 return mSelectionManager.getSelection().size() > 0;
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700596 }
597
598 @Override
599 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Steve McKay1f199482015-05-20 15:58:42 -0700600 // Delegate update logic to our owning action, since specialized
601 // logic is desired.
602 mFragmentTuner.updateActionMenu(menu, mType);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700603 return true;
604 }
605
606 @Override
Steve McKayef280152015-06-11 10:10:49 -0700607 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Steve McKay1f199482015-05-20 15:58:42 -0700608
Steve McKayef280152015-06-11 10:10:49 -0700609 Selection selection = new Selection();
610 mSelectionManager.getSelection(selection);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700611
Jeff Sharkey873daa32013-08-18 17:38:20 -0700612 final int id = item.getItemId();
613 if (id == R.id.menu_open) {
Steve McKayef280152015-06-11 10:10:49 -0700614 openDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700615 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700616 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700617
618 } else if (id == R.id.menu_share) {
Steve McKayef280152015-06-11 10:10:49 -0700619 shareDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700620 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700621 return true;
622
623 } else if (id == R.id.menu_delete) {
Steve McKayef280152015-06-11 10:10:49 -0700624 deleteDocuments(selection);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700625 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700626 return true;
627
Steve McKay1f199482015-05-20 15:58:42 -0700628 } else if (id == R.id.menu_copy_to) {
Steve McKayef280152015-06-11 10:10:49 -0700629 transferDocuments(selection, CopyService.TRANSFER_MODE_COPY);
Ben Kwacb4461f2015-05-05 11:50:11 -0700630 mode.finish();
631 return true;
632
Steve McKay1f199482015-05-20 15:58:42 -0700633 } else if (id == R.id.menu_move_to) {
Steve McKayef280152015-06-11 10:10:49 -0700634 transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700635 mode.finish();
636 return true;
637
Steve McKay1f199482015-05-20 15:58:42 -0700638 } else if (id == R.id.menu_copy_to_clipboard) {
Steve McKayef280152015-06-11 10:10:49 -0700639 copySelectionToClipboard(selection);
Steve McKay1f199482015-05-20 15:58:42 -0700640 mode.finish();
641 return true;
642
Ben Kwa512a6ba2015-03-31 08:15:21 -0700643 } else if (id == R.id.menu_select_all) {
Steve McKay0599a442015-05-05 14:50:00 -0700644 selectAllFiles();
Ben Kwa512a6ba2015-03-31 08:15:21 -0700645 return true;
646
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700647 } else {
648 return false;
649 }
650 }
Steve McKayef280152015-06-11 10:10:49 -0700651 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700652
Steve McKayef280152015-06-11 10:10:49 -0700653 private static void cancelThumbnailTask(View view) {
654 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
655 if (iconThumb != null) {
656 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
657 if (oldTask != null) {
658 oldTask.preempt();
659 iconThumb.setTag(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700660 }
661 }
Steve McKayef280152015-06-11 10:10:49 -0700662 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700663
Steve McKayef280152015-06-11 10:10:49 -0700664 private void openDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700665 new GetDocumentsTask() {
666 @Override
667 void onDocumentsReady(List<DocumentInfo> docs) {
668 // TODO: Implement support in standalone for opening multiple docs.
669 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Ben Kwaf527c632015-04-08 15:03:35 -0700670 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700671 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700672 }
673
Steve McKayef280152015-06-11 10:10:49 -0700674 private void shareDocuments(final Selection selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -0700675 new GetDocumentsTask() {
676 @Override
677 void onDocumentsReady(List<DocumentInfo> docs) {
678 Intent intent;
679
680 // Filter out directories - those can't be shared.
681 List<DocumentInfo> docsForSend = Lists.newArrayList();
682 for (DocumentInfo doc: docs) {
683 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
684 docsForSend.add(doc);
685 }
686 }
687
688 if (docsForSend.size() == 1) {
689 final DocumentInfo doc = docsForSend.get(0);
690
691 intent = new Intent(Intent.ACTION_SEND);
692 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
693 intent.addCategory(Intent.CATEGORY_DEFAULT);
694 intent.setType(doc.mimeType);
695 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
696
697 } else if (docsForSend.size() > 1) {
698 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
699 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
700 intent.addCategory(Intent.CATEGORY_DEFAULT);
701
702 final ArrayList<String> mimeTypes = Lists.newArrayList();
703 final ArrayList<Uri> uris = Lists.newArrayList();
704 for (DocumentInfo doc : docsForSend) {
705 mimeTypes.add(doc.mimeType);
706 uris.add(doc.derivedUri);
707 }
708
709 intent.setType(findCommonMimeType(mimeTypes));
710 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
711
712 } else {
713 return;
714 }
715
716 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
717 startActivity(intent);
718 }
719 }.execute(selected);
720 }
721
Steve McKayef280152015-06-11 10:10:49 -0700722 private void deleteDocuments(final Selection selected) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700723 final Context context = getActivity();
724 final ContentResolver resolver = context.getContentResolver();
725
Steve McKay9276f3b2015-05-27 16:11:42 -0700726 new GetDocumentsTask() {
727 @Override
728 void onDocumentsReady(List<DocumentInfo> docs) {
729 boolean hadTrouble = false;
730 for (DocumentInfo doc : docs) {
731 if (!doc.isDeleteSupported()) {
732 Log.w(TAG, "Skipping " + doc);
733 hadTrouble = true;
734 continue;
735 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700736
Steve McKay9276f3b2015-05-27 16:11:42 -0700737 ContentProviderClient client = null;
738 try {
739 client = DocumentsApplication.acquireUnstableProviderOrThrow(
740 resolver, doc.derivedUri.getAuthority());
741 DocumentsContract.deleteDocument(client, doc.derivedUri);
742 } catch (Exception e) {
743 Log.w(TAG, "Failed to delete " + doc);
744 hadTrouble = true;
745 } finally {
746 ContentProviderClient.releaseQuietly(client);
747 }
748 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700749
Steve McKay9276f3b2015-05-27 16:11:42 -0700750 if (hadTrouble) {
751 Toast.makeText(
752 context,
753 R.string.toast_failed_delete,
754 Toast.LENGTH_SHORT).show();
755 }
756 }
757 }.execute(selected);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700758 }
759
Steve McKayef280152015-06-11 10:10:49 -0700760 private void transferDocuments(final Selection selected, final int mode) {
Ben Kwaf5858932015-04-07 15:43:39 -0700761 // Pop up a dialog to pick a destination. This is inadequate but works for now.
762 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900763 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900764 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900765 Uri.EMPTY,
766 getActivity(),
767 DocumentsActivity.class);
Steve McKay9276f3b2015-05-27 16:11:42 -0700768
769 new GetDocumentsTask() {
770 @Override
771 void onDocumentsReady(List<DocumentInfo> docs) {
772 getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs;
773
774 boolean directoryCopy = false;
775 for (DocumentInfo info : docs) {
776 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
777 directoryCopy = true;
778 break;
779 }
780 }
781 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
782 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
783 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900784 }
Steve McKay9276f3b2015-05-27 16:11:42 -0700785 }.execute(selected);
Ben Kwa41b26c12015-03-31 10:11:43 -0700786 }
787
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700788 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700789 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700790 }
791
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700792 private static abstract class Footer {
793 private final int mItemViewType;
794
795 public Footer(int itemViewType) {
796 mItemViewType = itemViewType;
797 }
798
799 public abstract View getView(View convertView, ViewGroup parent);
800
801 public int getItemViewType() {
802 return mItemViewType;
803 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700804 }
805
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700806 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700807 public LoadingFooter() {
808 super(1);
809 }
810
Jeff Sharkey20b32272013-09-03 15:25:52 -0700811 @Override
812 public View getView(View convertView, ViewGroup parent) {
813 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700814 final State state = getDisplayState(DirectoryFragment.this);
815
Jeff Sharkey20b32272013-09-03 15:25:52 -0700816 if (convertView == null) {
817 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700818 if (state.derivedMode == MODE_LIST) {
819 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
820 } else if (state.derivedMode == MODE_GRID) {
821 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
822 } else {
823 throw new IllegalStateException();
824 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700825 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700826
Jeff Sharkey20b32272013-09-03 15:25:52 -0700827 return convertView;
828 }
829 }
830
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700831 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700832 private final int mIcon;
833 private final String mMessage;
834
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700835 public MessageFooter(int itemViewType, int icon, String message) {
836 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700837 mIcon = icon;
838 mMessage = message;
839 }
840
841 @Override
842 public View getView(View convertView, ViewGroup parent) {
843 final Context context = parent.getContext();
844 final State state = getDisplayState(DirectoryFragment.this);
845
846 if (convertView == null) {
847 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700848 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700849 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700850 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700851 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
852 } else {
853 throw new IllegalStateException();
854 }
855 }
856
857 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
858 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
859 icon.setImageResource(mIcon);
860 title.setText(mMessage);
861 return convertView;
862 }
863 }
864
Steve McKayef280152015-06-11 10:10:49 -0700865 // Provide a reference to the views for each data item
866 // Complex data items may need more than one view per item, and
867 // you provide access to all the views for a data item in a view holder
868 private static final class DocumentHolder extends RecyclerView.ViewHolder {
869 // each data item is just a string in this case
870 public View view;
871 public String docId; // The stable document id.
872 public DocumentHolder(View view) {
873 super(view);
874 this.view = view;
875 }
876 }
877
878 private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> {
879
880 private final Context mContext;
881 private final LayoutInflater mInflater;
882 // TODO: Bring back support for footers.
883 private final List<Footer> mFooters = Lists.newArrayList();
884
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700885 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700886 private int mCursorCount;
887
Steve McKayef280152015-06-11 10:10:49 -0700888 public DocumentsAdapter(Context context) {
889 mContext = context;
890 mInflater = LayoutInflater.from(context);
891 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700892
Steve McKayef280152015-06-11 10:10:49 -0700893 public void replaceResult(DirectoryResult result) {
894 if (DEBUG) Log.i(TAG, "Updating adapter with new result set.");
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700895 mCursor = result != null ? result.cursor : null;
896 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700897
898 mFooters.clear();
899
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700900 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700901 if (extras != null) {
902 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
903 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700904 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700905 }
906 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
907 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700908 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700909 }
910 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
911 mFooters.add(new LoadingFooter());
912 }
913 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700914
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700915 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700916 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700917 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700918 }
919
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700920 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700921 mEmptyView.setVisibility(View.VISIBLE);
922 } else {
923 mEmptyView.setVisibility(View.GONE);
924 }
925
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700926 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700927 }
928
929 @Override
Steve McKayef280152015-06-11 10:10:49 -0700930 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
931 final State state = getDisplayState(DirectoryFragment.this);
932 final LayoutInflater inflater = LayoutInflater.from(getContext());
933 switch (state.derivedMode) {
934 case MODE_GRID:
935 return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false));
936 case MODE_LIST:
937 return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false));
938 case MODE_UNKNOWN:
939 default:
940 throw new IllegalStateException("Unsupported layout mode.");
Jeff Sharkey20b32272013-09-03 15:25:52 -0700941 }
942 }
943
Steve McKayef280152015-06-11 10:10:49 -0700944 @Override
945 public void onBindViewHolder(DocumentHolder holder, int position) {
946
947 final Context context = getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700948 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700949 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700950 final RootsCache roots = DocumentsApplication.getRootsCache(context);
951 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
952 context, mThumbSize);
953
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700954 final Cursor cursor = getItem(position);
Steve McKayef280152015-06-11 10:10:49 -0700955 checkNotNull(cursor, "Cursor cannot be null.");
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700956
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700957 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
958 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700959 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
960 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
961 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
962 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
963 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
964 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
965 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
966 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700967
Steve McKayef280152015-06-11 10:10:49 -0700968 holder.docId = docId;
969 final View itemView = holder.view;
970 itemView.setActivated(mSelectionManager.getSelection().contains(position));
Jeff Sharkey9656a532013-09-13 13:42:19 -0700971
Steve McKayef280152015-06-11 10:10:49 -0700972 final View line1 = itemView.findViewById(R.id.line1);
973 final View line2 = itemView.findViewById(R.id.line2);
974
975 final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
976 final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
977 final TextView title = (TextView) itemView.findViewById(android.R.id.title);
978 final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1);
979 final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2);
980 final TextView summary = (TextView) itemView.findViewById(android.R.id.summary);
981 final TextView date = (TextView) itemView.findViewById(R.id.date);
982 final TextView size = (TextView) itemView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700983
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700984 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700985 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700986 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700987 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700988 }
989
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700990 iconMime.animate().cancel();
991 iconThumb.animate().cancel();
992
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700993 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700994 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700995 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700996 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700997
Jeff Sharkey7e544612014-08-29 15:38:27 -0700998 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
999 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
1000
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001001 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -07001002 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001003 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001004 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001005 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001006 iconThumb.setImageBitmap(cachedResult);
1007 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001008 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001009 iconThumb.setImageDrawable(null);
Steve McKayef280152015-06-11 10:10:49 -07001010 // TODO: Hang this off DocumentHolder?
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001011 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -07001012 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001013 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001014 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001015 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001016 }
1017
1018 // Always throw MIME icon into place, even when a thumbnail is being
1019 // loaded in background.
1020 if (cacheHit) {
1021 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001022 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001023 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001024 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001025 iconMime.setAlpha(1f);
1026 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001027 iconThumb.setImageDrawable(null);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001028 iconMime.setImageDrawable(
Steve McKayef280152015-06-11 10:10:49 -07001029 getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001030 }
1031
Jeff Sharkey9656a532013-09-13 13:42:19 -07001032 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -07001033 boolean hasLine2 = false;
1034
Jeff Sharkey9656a532013-09-13 13:42:19 -07001035 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
1036 if (!hideTitle) {
1037 title.setText(docDisplayName);
1038 hasLine1 = true;
1039 }
1040
1041 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001042 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001043 // We've already had to enumerate roots before any results can
1044 // be shown, so this will never block.
1045 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001046 if (state.derivedMode == MODE_GRID) {
Steve McKayef280152015-06-11 10:10:49 -07001047 iconDrawable = root.loadGridIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001048 } else {
Steve McKayef280152015-06-11 10:10:49 -07001049 iconDrawable = root.loadIcon(mContext);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -07001050 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001051
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001052 if (summary != null) {
1053 final boolean alwaysShowSummary = getResources()
1054 .getBoolean(R.bool.always_show_summary);
1055 if (alwaysShowSummary) {
1056 summary.setText(root.getDirectoryString());
1057 summary.setVisibility(View.VISIBLE);
1058 hasLine2 = true;
1059 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -07001060 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001061 // No summary needed if icon speaks for itself
1062 summary.setVisibility(View.INVISIBLE);
1063 } else {
1064 summary.setText(root.getDirectoryString());
1065 summary.setVisibility(View.VISIBLE);
1066 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
1067 hasLine2 = true;
1068 }
1069 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001071 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001072 // Directories showing thumbnails in grid mode get a little icon
1073 // hint to remind user they're a directory.
1074 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
1075 && showThumbnail) {
Steve McKayef280152015-06-11 10:10:49 -07001076 iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -07001077 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -07001078 }
1079
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001080 if (summary != null) {
1081 if (docSummary != null) {
1082 summary.setText(docSummary);
1083 summary.setVisibility(View.VISIBLE);
1084 hasLine2 = true;
1085 } else {
1086 summary.setVisibility(View.INVISIBLE);
1087 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -07001088 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001089 }
1090
Jeff Sharkey9656a532013-09-13 13:42:19 -07001091 if (icon1 != null) icon1.setVisibility(View.GONE);
1092 if (icon2 != null) icon2.setVisibility(View.GONE);
1093
1094 if (iconDrawable != null) {
1095 if (hasLine1) {
1096 icon1.setVisibility(View.VISIBLE);
1097 icon1.setImageDrawable(iconDrawable);
1098 } else {
1099 icon2.setVisibility(View.VISIBLE);
1100 icon2.setImageDrawable(iconDrawable);
1101 }
1102 }
1103
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001104 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001105 date.setText(null);
1106 } else {
Steve McKayef280152015-06-11 10:10:49 -07001107 date.setText(formatTime(mContext, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001108 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001109 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001110
1111 if (state.showSize) {
1112 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001113 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001114 size.setText(null);
1115 } else {
Steve McKayef280152015-06-11 10:10:49 -07001116 size.setText(Formatter.formatFileSize(mContext, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001117 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001118 }
1119 } else {
1120 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001121 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001122
Jeff Sharkey9656a532013-09-13 13:42:19 -07001123 if (line1 != null) {
1124 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1125 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001126 if (line2 != null) {
1127 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1128 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001129
Steve McKayef280152015-06-11 10:10:49 -07001130 setEnabledRecursive(itemView, enabled);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001131
1132 iconMime.setAlpha(iconAlpha);
1133 iconThumb.setAlpha(iconAlpha);
1134 if (icon1 != null) icon1.setAlpha(iconAlpha);
1135 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001136
Steve McKay8e258c62015-05-06 14:27:57 -07001137 if (DEBUG_ENABLE_DND) {
Steve McKayef280152015-06-11 10:10:49 -07001138 setupDragAndDropOnDocumentView(itemView, cursor);
Steve McKay8e258c62015-05-06 14:27:57 -07001139 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001140 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001141
Steve McKayef280152015-06-11 10:10:49 -07001142 private Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001143 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001144 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001145 return mCursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001146 }
Steve McKayef280152015-06-11 10:10:49 -07001147
1148 Log.w(TAG, "Returning null cursor for position: " + position);
1149 if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount);
1150 if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size());
1151 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001152 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001153
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001154 @Override
Steve McKayef280152015-06-11 10:10:49 -07001155 public int getItemCount() {
1156 return mCursorCount;
1157 // return mCursorCount + mFooters.size();
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001158 }
1159
1160 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001161 public int getItemViewType(int position) {
1162 if (position < mCursorCount) {
1163 return 0;
1164 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001165 position -= mCursorCount;
1166 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001167 }
1168 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001169
Steve McKayef280152015-06-11 10:10:49 -07001170 private boolean isEmpty() {
1171 return getItemCount() > 0;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001172 }
1173 }
1174
1175 private static String formatTime(Context context, long when) {
1176 // TODO: DateUtils should make this easier
1177 Time then = new Time();
1178 then.set(when);
1179 Time now = new Time();
1180 now.setToNow();
1181
1182 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1183 | DateUtils.FORMAT_ABBREV_ALL;
1184
1185 if (then.year != now.year) {
1186 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1187 } else if (then.yearDay != now.yearDay) {
1188 flags |= DateUtils.FORMAT_SHOW_DATE;
1189 } else {
1190 flags |= DateUtils.FORMAT_SHOW_TIME;
1191 }
1192
1193 return DateUtils.formatDateTime(context, when, flags);
1194 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001195
1196 private String findCommonMimeType(List<String> mimeTypes) {
1197 String[] commonType = mimeTypes.get(0).split("/");
1198 if (commonType.length != 2) {
1199 return "*/*";
1200 }
1201
1202 for (int i = 1; i < mimeTypes.size(); i++) {
1203 String[] type = mimeTypes.get(i).split("/");
1204 if (type.length != 2) continue;
1205
1206 if (!commonType[1].equals(type[1])) {
1207 commonType[1] = "*";
1208 }
1209
1210 if (!commonType[0].equals(type[0])) {
1211 commonType[0] = "*";
1212 commonType[1] = "*";
1213 break;
1214 }
1215 }
1216
1217 return commonType[0] + "/" + commonType[1];
1218 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001219
1220 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001221 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001222 if (v.isEnabled() == enabled) return;
1223 v.setEnabled(enabled);
1224
1225 if (v instanceof ViewGroup) {
1226 final ViewGroup vg = (ViewGroup) v;
1227 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1228 setEnabledRecursive(vg.getChildAt(i), enabled);
1229 }
1230 }
1231 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001232
1233 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1234 final State state = getDisplayState(DirectoryFragment.this);
1235
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001236 // Directories are always enabled
1237 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1238 return true;
1239 }
1240
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001241 // Read-only files are disabled when creating
1242 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1243 return false;
1244 }
1245
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001246 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1247 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001248
Steve McKay1f199482015-05-20 15:58:42 -07001249 private @NonNull List<DocumentInfo> getSelectedDocuments() {
Steve McKayef280152015-06-11 10:10:49 -07001250 Selection sel = mSelectionManager.getSelection(new Selection());
1251 return getItemsAsDocuments(sel);
Steve McKay1f199482015-05-20 15:58:42 -07001252 }
1253
Steve McKayef280152015-06-11 10:10:49 -07001254 private List<DocumentInfo> getItemsAsDocuments(Selection items) {
Steve McKay1f199482015-05-20 15:58:42 -07001255 if (items == null || items.size() == 0) {
1256 return new ArrayList<>(0);
1257 }
1258
1259 final List<DocumentInfo> docs = new ArrayList<>(items.size());
1260 final int size = items.size();
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001261 for (int i = 0; i < size; i++) {
Steve McKayef280152015-06-11 10:10:49 -07001262 final Cursor cursor = mAdapter.getItem(items.get(i));
1263 checkNotNull(cursor, "Cursor cannot be null.");
1264 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1265 docs.add(doc);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001266 }
1267 return docs;
1268 }
1269
Steve McKay1f199482015-05-20 15:58:42 -07001270 private void copyFromClipboard() {
1271 new AsyncTask<Void, Void, List<DocumentInfo>>() {
1272
1273 @Override
1274 protected List<DocumentInfo> doInBackground(Void... params) {
1275 return mClipper.getClippedDocuments();
1276 }
1277
1278 @Override
1279 protected void onPostExecute(List<DocumentInfo> docs) {
1280 DocumentInfo destination =
1281 ((BaseActivity) getActivity()).getCurrentDirectory();
1282 copyDocuments(docs, destination);
1283 }
1284 }.execute();
Steve McKay0599a442015-05-05 14:50:00 -07001285 }
1286
Steve McKay1f199482015-05-20 15:58:42 -07001287 private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) {
Steve McKayef280152015-06-11 10:10:49 -07001288 checkNotNull(clipData);
Steve McKay1f199482015-05-20 15:58:42 -07001289 new AsyncTask<Void, Void, List<DocumentInfo>>() {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001290
Steve McKay1f199482015-05-20 15:58:42 -07001291 @Override
1292 protected List<DocumentInfo> doInBackground(Void... params) {
1293 return mClipper.getDocumentsFromClipData(clipData);
1294 }
1295
1296 @Override
1297 protected void onPostExecute(List<DocumentInfo> docs) {
1298 copyDocuments(docs, destination);
1299 }
1300 }.execute();
1301 }
1302
1303 private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) {
1304 if (!canCopy(docs, destination)) {
1305 Toast.makeText(
1306 getActivity(),
1307 R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show();
Steve McKay0599a442015-05-05 14:50:00 -07001308 return;
1309 }
1310
Steve McKay1f199482015-05-20 15:58:42 -07001311 if (docs.isEmpty()) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001312 return;
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001313 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001314
Steve McKay1f199482015-05-20 15:58:42 -07001315 final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001316 DocumentStack tmpStack = new DocumentStack();
Steve McKay1f199482015-05-20 15:58:42 -07001317 if (destination != null) {
1318 tmpStack.push(destination);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001319 tmpStack.addAll(curStack);
1320 } else {
1321 tmpStack = curStack;
1322 }
1323
Steve McKay1f199482015-05-20 15:58:42 -07001324 CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001325 }
1326
1327 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1328 Context context = getActivity();
1329 final ContentResolver resolver = context.getContentResolver();
1330 ClipData clipData = null;
1331 for (DocumentInfo doc : docs) {
1332 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1333 if (clipData == null) {
Vladislav Kaznacheeve16887a2015-05-05 12:09:47 -07001334 // TODO: figure out what this string should be.
1335 // Currently it is not displayed anywhere in the UI, but this might change.
1336 final String label = "";
1337 clipData = ClipData.newUri(resolver, label, uri);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001338 } else {
1339 // TODO: update list of mime types in ClipData.
1340 clipData.addItem(new ClipData.Item(uri));
1341 }
1342 }
1343 return clipData;
1344 }
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001345
Steve McKay1f199482015-05-20 15:58:42 -07001346 void copySelectedToClipboard() {
Steve McKayef280152015-06-11 10:10:49 -07001347 Selection sel = mSelectionManager.getSelection(new Selection());
1348 copySelectionToClipboard(sel);
Steve McKay9276f3b2015-05-27 16:11:42 -07001349 }
Steve McKay0599a442015-05-05 14:50:00 -07001350
Steve McKayef280152015-06-11 10:10:49 -07001351 void copySelectionToClipboard(Selection items) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001352 new GetDocumentsTask() {
1353 @Override
1354 void onDocumentsReady(List<DocumentInfo> docs) {
1355 mClipper.clipDocuments(docs);
Steve McKay1f199482015-05-20 15:58:42 -07001356 Activity activity = getActivity();
1357 Toast.makeText(activity,
1358 activity.getResources().getQuantityString(
1359 R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
1360 Toast.LENGTH_SHORT).show();
Steve McKay9276f3b2015-05-27 16:11:42 -07001361 }
Steve McKayef280152015-06-11 10:10:49 -07001362 }.execute(items);
Steve McKay0599a442015-05-05 14:50:00 -07001363 }
1364
1365 void pasteFromClipboard() {
Steve McKay1f199482015-05-20 15:58:42 -07001366 copyFromClipboard();
1367 getActivity().invalidateOptionsMenu();
Steve McKay0599a442015-05-05 14:50:00 -07001368 }
1369
Steve McKay0599a442015-05-05 14:50:00 -07001370 /**
1371 * Returns true if the list of files can be copied to destination. Note that this
1372 * is a policy check only. Currently the method does not attempt to verify
1373 * available space or any other environmental aspects possibly resulting in
1374 * failure to copy.
1375 *
1376 * @return true if the list of files can be copied to destination.
1377 */
1378 boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) {
1379 BaseActivity activity = (BaseActivity)getActivity();
1380
1381 final RootInfo root = activity.getCurrentRoot();
1382
1383 // Can't copy folders to Downloads.
1384 if (root.isDownloads()) {
1385 for (DocumentInfo docs : files) {
1386 if (docs.isDirectory()) {
1387 return false;
1388 }
1389 }
1390 }
1391
1392 return dest != null && dest.isDirectory() && dest.isCreateSupported();
1393 }
1394
1395 void selectAllFiles() {
Steve McKayef280152015-06-11 10:10:49 -07001396 mSelectionManager.selectItems(0, mAdapter.getItemCount());
Steve McKay0599a442015-05-05 14:50:00 -07001397 updateDisplayState();
1398 }
1399
Steve McKayef280152015-06-11 10:10:49 -07001400 private void setupDragAndDropOnDirectoryView(View view) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001401 // Listen for drops on non-directory items and empty space.
1402 view.setOnDragListener(mOnDragListener);
1403 }
1404
1405 private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
1406 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
1407 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1408 // Make a directory item a drop target. Drop on non-directories and empty space
1409 // is handled at the list/grid view level.
1410 view.setOnDragListener(mOnDragListener);
1411 }
1412
1413 // Temporary: attaching the listener to the title only.
1414 // Attaching to the entire item conflicts with the item long click handler responsible
1415 // for item selection.
1416 final View title = view.findViewById(android.R.id.title);
1417 title.setOnLongClickListener(mLongClickListener);
1418 }
1419
1420 private View.OnDragListener mOnDragListener = new View.OnDragListener() {
1421 @Override
1422 public boolean onDrag(View v, DragEvent event) {
1423 switch (event.getAction()) {
1424 case DragEvent.ACTION_DRAG_STARTED:
1425 // TODO: Check if the event contains droppable data.
1426 return true;
1427
1428 // TODO: Highlight potential drop target directory?
1429 // TODO: Expand drop target directory on hover?
1430 case DragEvent.ACTION_DRAG_ENTERED:
1431 case DragEvent.ACTION_DRAG_LOCATION:
1432 case DragEvent.ACTION_DRAG_EXITED:
1433 case DragEvent.ACTION_DRAG_ENDED:
1434 return true;
1435
1436 case DragEvent.ACTION_DROP:
Steve McKayef280152015-06-11 10:10:49 -07001437 int dstPosition = mRecView.getChildAdapterPosition(v);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001438 DocumentInfo dstDir = null;
1439 if (dstPosition != android.widget.AdapterView.INVALID_POSITION) {
1440 Cursor dstCursor = mAdapter.getItem(dstPosition);
Steve McKayef280152015-06-11 10:10:49 -07001441 checkNotNull(dstCursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001442 dstDir = DocumentInfo.fromDirectoryCursor(dstCursor);
1443 // TODO: Do not drop into the directory where the documents came from.
1444 }
1445 copyFromClipData(event.getClipData(), dstDir);
1446 return true;
1447 }
1448 return false;
1449 }
1450 };
1451
1452 private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
1453 @Override
1454 public boolean onLongClick(View v) {
1455 final List<DocumentInfo> docs = getDraggableDocuments(v);
1456 if (docs.isEmpty()) {
1457 return false;
1458 }
1459 v.startDrag(
1460 getClipDataFromDocuments(docs),
1461 new DrawableShadowBuilder(getDragShadowIcon(docs)),
1462 null,
1463 View.DRAG_FLAG_GLOBAL
1464 );
1465 return true;
1466 }
1467 };
1468
1469 private List<DocumentInfo> getDraggableDocuments(View currentItemView) {
Steve McKayef280152015-06-11 10:10:49 -07001470 int position = mRecView.getChildAdapterPosition(currentItemView);
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001471 if (position == android.widget.AdapterView.INVALID_POSITION) {
1472 return Collections.EMPTY_LIST;
1473 }
1474
1475 final List<DocumentInfo> selectedDocs = getSelectedDocuments();
1476 if (!selectedDocs.isEmpty()) {
Steve McKayef280152015-06-11 10:10:49 -07001477 if (!mSelectionManager.getSelection().contains(position)) {
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001478 // There is a selection that does not include the current item, drag nothing.
1479 return Collections.EMPTY_LIST;
1480 }
1481 return selectedDocs;
1482 }
1483
1484 final Cursor cursor = mAdapter.getItem(position);
Steve McKayef280152015-06-11 10:10:49 -07001485 checkNotNull(cursor, "Cursor cannot be null.");
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001486 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1487 return Lists.newArrayList(doc);
1488 }
1489
1490 private Drawable getDragShadowIcon(List<DocumentInfo> docs) {
1491 if (docs.size() == 1) {
1492 final DocumentInfo doc = docs.get(0);
1493 return getDocumentIcon(getActivity(), doc.authority, doc.documentId,
1494 doc.mimeType, doc.icon, getDisplayState(this));
1495 }
1496 return getActivity().getDrawable(R.drawable.ic_doc_generic);
1497 }
1498
1499 public static Drawable getDocumentIcon(Context context, String docAuthority, String docId,
1500 String docMimeType, int docIcon, State state) {
1501 if (docIcon != 0) {
1502 return IconUtils.loadPackageIcon(context, docAuthority, docIcon);
1503 } else {
1504 return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId,
1505 state.derivedMode);
1506 }
1507 }
1508
Steve McKayef280152015-06-11 10:10:49 -07001509 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1510 implements Preemptable {
1511 private final Uri mUri;
1512 private final ImageView mIconMime;
1513 private final ImageView mIconThumb;
1514 private final Point mThumbSize;
1515 private final float mTargetAlpha;
1516 private final CancellationSignal mSignal;
1517
1518 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1519 float targetAlpha) {
1520 mUri = uri;
1521 mIconMime = iconMime;
1522 mIconThumb = iconThumb;
1523 mThumbSize = thumbSize;
1524 mTargetAlpha = targetAlpha;
1525 mSignal = new CancellationSignal();
1526 }
1527
1528 @Override
1529 public void preempt() {
1530 cancel(false);
1531 mSignal.cancel();
1532 }
1533
1534 @Override
1535 protected Bitmap doInBackground(Uri... params) {
1536 if (isCancelled()) return null;
1537
1538 final Context context = mIconThumb.getContext();
1539 final ContentResolver resolver = context.getContentResolver();
1540
1541 ContentProviderClient client = null;
1542 Bitmap result = null;
1543 try {
1544 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1545 resolver, mUri.getAuthority());
1546 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
1547 if (result != null) {
1548 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1549 context, mThumbSize);
1550 thumbs.put(mUri, result);
1551 }
1552 } catch (Exception e) {
1553 if (!(e instanceof OperationCanceledException)) {
1554 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1555 }
1556 } finally {
1557 ContentProviderClient.releaseQuietly(client);
1558 }
1559 return result;
1560 }
1561
1562 @Override
1563 protected void onPostExecute(Bitmap result) {
1564 if (mIconThumb.getTag() == this && result != null) {
1565 mIconThumb.setTag(null);
1566 mIconThumb.setImageBitmap(result);
1567
1568 mIconMime.setAlpha(mTargetAlpha);
1569 mIconMime.animate().alpha(0f).start();
1570 mIconThumb.setAlpha(0f);
1571 mIconThumb.animate().alpha(mTargetAlpha).start();
1572 }
1573 }
1574 }
1575
Vladislav Kaznacheevb6da7222015-05-01 14:18:57 -07001576 private class DrawableShadowBuilder extends View.DragShadowBuilder {
1577
1578 private final Drawable mShadow;
1579
1580 private final int mShadowDimension;
1581
1582 public DrawableShadowBuilder(Drawable shadow) {
1583 mShadow = shadow;
1584 mShadowDimension = getResources().getDimensionPixelSize(
1585 R.dimen.drag_shadow_size);
1586 mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension);
1587 }
1588
1589 public void onProvideShadowMetrics(
1590 Point shadowSize, Point shadowTouchPoint) {
1591 shadowSize.set(mShadowDimension, mShadowDimension);
1592 shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2);
1593 }
1594
1595 public void onDrawShadow(Canvas canvas) {
1596 mShadow.draw(canvas);
1597 }
1598 }
Steve McKay1f199482015-05-20 15:58:42 -07001599
1600 private FragmentTuner pickFragmentTuner(final State state) {
1601 return state.action == ACTION_BROWSE_ALL
1602 ? new StandaloneTuner()
1603 : new DefaultTuner(state);
1604 }
1605
1606 /**
1607 * Interface for specializing the Fragment for the "host" Activity.
1608 * Feel free to expand the role of this class to handle other specializations.
1609 */
1610 private interface FragmentTuner {
1611 void updateActionMenu(Menu menu, int dirType);
1612 }
1613
1614 /**
Steve McKay9276f3b2015-05-27 16:11:42 -07001615 * Abstract task providing support for loading documents *off*
1616 * the main thread. And if it isn't obvious, creating a list
1617 * of documents (especially large lists) can be pretty expensive.
1618 */
1619 private abstract class GetDocumentsTask
Steve McKayef280152015-06-11 10:10:49 -07001620 extends AsyncTask<Selection, Void, List<DocumentInfo>> {
Steve McKay9276f3b2015-05-27 16:11:42 -07001621 @Override
Steve McKayef280152015-06-11 10:10:49 -07001622 protected final List<DocumentInfo> doInBackground(Selection... selected) {
Steve McKay9276f3b2015-05-27 16:11:42 -07001623 return getItemsAsDocuments(selected[0]);
1624 }
1625
1626 @Override
1627 protected final void onPostExecute(List<DocumentInfo> docs) {
1628 onDocumentsReady(docs);
1629 }
1630
1631 abstract void onDocumentsReady(List<DocumentInfo> docs);
1632 }
1633
1634 /**
Steve McKay1f199482015-05-20 15:58:42 -07001635 * Provides support for Platform specific specializations of DirectoryFragment.
1636 */
1637 private static final class DefaultTuner implements FragmentTuner {
1638
1639 private final State mState;
1640
1641 public DefaultTuner(State state) {
1642 mState = state;
1643 }
1644
1645 @Override
1646 public void updateActionMenu(Menu menu, int dirType) {
1647 Preconditions.checkState(mState.action != ACTION_BROWSE_ALL);
1648
1649 final MenuItem open = menu.findItem(R.id.menu_open);
1650 final MenuItem share = menu.findItem(R.id.menu_share);
1651 final MenuItem delete = menu.findItem(R.id.menu_delete);
1652 final MenuItem copyTo = menu.findItem(R.id.menu_copy_to);
1653 final MenuItem moveTo = menu.findItem(R.id.menu_move_to);
1654 final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard);
1655
1656 final boolean manageOrBrowse = (mState.action == ACTION_MANAGE
1657 || mState.action == ACTION_BROWSE);
1658
1659 open.setVisible(!manageOrBrowse);
1660 share.setVisible(manageOrBrowse);
1661 delete.setVisible(manageOrBrowse);
1662 // Disable copying from the Recents view.
1663 copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN);
1664 moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
1665
1666 // Only shown in standalone mode.
1667 copyToClipboard.setVisible(false);
1668 }
1669 }
1670
1671 /**
1672 * Provides support for Standalone specific specializations of DirectoryFragment.
1673 */
1674 private static final class StandaloneTuner implements FragmentTuner {
1675 @Override
1676 public void updateActionMenu(Menu menu, int dirType) {
1677 menu.findItem(R.id.menu_share).setVisible(true);
1678 menu.findItem(R.id.menu_delete).setVisible(true);
1679 menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true);
1680
1681 menu.findItem(R.id.menu_open).setVisible(false);
1682 menu.findItem(R.id.menu_copy_to).setVisible(false);
1683 menu.findItem(R.id.menu_move_to).setVisible(false);
1684 }
1685 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001686}