blob: 7d737ca3b24ef456f8d50644f26517d24e7e196a [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;
Ben Kwaf5858932015-04-07 15:43:39 -070031import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070032import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070033import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070034import android.app.FragmentManager;
35import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070037import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070038import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070039import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070040import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070041import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070042import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070043import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070044import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070045import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070046import android.graphics.Bitmap;
47import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070048import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070049import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070050import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070052import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070053import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070054import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070055import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070056import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070057import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070058import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070059import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070060import android.text.format.Time;
61import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070062import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070063import android.util.SparseBooleanArray;
64import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070065import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070066import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070067import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070068import android.view.View;
69import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070070import android.widget.AbsListView;
71import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070072import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070073import android.widget.AdapterView;
74import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070075import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070076import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070077import android.widget.ImageView;
78import android.widget.ListView;
79import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070080import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070081
Steve McKayd0a2a2c2015-03-25 14:35:33 -070082import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070083import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070084import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070085import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090086import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070087import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070088import com.google.android.collect.Lists;
89
90import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070091import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070092
93/**
94 * Display the documents inside a single directory.
95 */
96public class DirectoryFragment extends Fragment {
97
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070098 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070099 private ListView mListView;
100 private GridView mGridView;
101
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700102 private AbsListView mCurrentView;
103
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700104 public static final int TYPE_NORMAL = 1;
105 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700106 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700107
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700108 public static final int ANIM_NONE = 1;
109 public static final int ANIM_SIDE = 2;
110 public static final int ANIM_DOWN = 3;
111 public static final int ANIM_UP = 4;
112
Ben Kwaf5858932015-04-07 15:43:39 -0700113 public static final int REQUEST_COPY_DESTINATION = 1;
114
Jeff Sharkey5b535922013-08-02 15:55:26 -0700115 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700116 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700117
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700118 private int mLastMode = MODE_UNKNOWN;
119 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700120 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700121
Jeff Sharkey9656a532013-09-13 13:42:19 -0700122 private boolean mHideGridTitles = false;
123
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700124 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700125 private Point mThumbSize;
126
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700127 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700128 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700129
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700130 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700131 private static final String EXTRA_ROOT = "root";
132 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700133 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700134 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700135
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700136 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700137
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700138 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
139 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140 }
141
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700142 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
143 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700144 }
145
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700146 public static void showRecentsOpen(FragmentManager fm, int anim) {
147 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700148 }
149
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700150 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
151 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700152 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700153 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700154 args.putParcelable(EXTRA_ROOT, root);
155 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700156 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700157
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700158 final FragmentTransaction ft = fm.beginTransaction();
159 switch (anim) {
160 case ANIM_SIDE:
161 args.putBoolean(EXTRA_IGNORE_STATE, true);
162 break;
163 case ANIM_DOWN:
164 args.putBoolean(EXTRA_IGNORE_STATE, true);
165 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
166 break;
167 case ANIM_UP:
168 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
169 break;
170 }
171
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700172 final DirectoryFragment fragment = new DirectoryFragment();
173 fragment.setArguments(args);
174
Jeff Sharkey76112212013-08-06 11:26:10 -0700175 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700176 ft.commitAllowingStateLoss();
177 }
178
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700179 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
180 final StringBuilder builder = new StringBuilder();
181 builder.append(root != null ? root.authority : "null").append(';');
182 builder.append(root != null ? root.rootId : "null").append(';');
183 builder.append(doc != null ? doc.documentId : "null");
184 return builder.toString();
185 }
186
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700187 public static DirectoryFragment get(FragmentManager fm) {
188 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700189 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700190 }
191
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700192 @Override
193 public View onCreateView(
194 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
195 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700196 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700197 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
198
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700199 mEmptyView = view.findViewById(android.R.id.empty);
200
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700201 mListView = (ListView) view.findViewById(R.id.list);
202 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700203 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700204 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700205
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700206 // Indent our list divider to align with text
207 final Drawable divider = mListView.getDivider();
208 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
209 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
210 if (insetLeft) {
211 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
212 } else {
213 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
214 }
215
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700216 mGridView = (GridView) view.findViewById(R.id.grid);
217 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700218 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700219 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700220
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700221 return view;
222 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700223
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700224 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700225 public void onDestroyView() {
226 super.onDestroyView();
227
228 // Cancel any outstanding thumbnail requests
229 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
230 final int count = target.getChildCount();
231 for (int i = 0; i < count; i++) {
232 final View view = target.getChildAt(i);
233 mRecycleListener.onMovedToScrapHeap(view);
234 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700235
236 // Tear down any selection in progress
237 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
238 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700239 }
240
241 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700242 public void onActivityCreated(Bundle savedInstanceState) {
243 super.onActivityCreated(savedInstanceState);
244
245 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700246 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700247
Jeff Sharkey9656a532013-09-13 13:42:19 -0700248 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
249 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
250
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700251 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700252 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700253 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700254
Jeff Sharkey9656a532013-09-13 13:42:19 -0700255 if (mType == TYPE_RECENT_OPEN) {
256 // Hide titles when showing recents for picking images/videos
257 mHideGridTitles = MimePredicate.mimeMatches(
258 MimePredicate.VISUAL_MIMES, state.acceptMimes);
259 } else {
260 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
261 }
262
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700263 final ActivityManager am = (ActivityManager) context.getSystemService(
264 Context.ACTIVITY_SERVICE);
265 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
266
Jeff Sharkey46899c82013-08-18 22:26:48 -0700267 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700268 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700269 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700270 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700271
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700272 Uri contentsUri;
273 switch (mType) {
274 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700275 contentsUri = DocumentsContract.buildChildDocumentsUri(
276 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700277 if (state.action == ACTION_MANAGE) {
278 contentsUri = DocumentsContract.setManageMode(contentsUri);
279 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700280 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700281 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700282 case TYPE_SEARCH:
283 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700284 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700285 if (state.action == ACTION_MANAGE) {
286 contentsUri = DocumentsContract.setManageMode(contentsUri);
287 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700288 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700289 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700290 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700291 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700292 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700293 default:
294 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700295 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700296 }
297
298 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700299 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700300 if (!isAdded()) return;
301
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700302 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700303
304 // Push latest state up to UI
305 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700306 if (result.mode != MODE_UNKNOWN) {
307 state.derivedMode = result.mode;
308 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700309 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700310 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700311
312 updateDisplayState();
313
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700314 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700315 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
316 context instanceof DocumentsActivity) {
317 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700318 }
319
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700320 // Restore any previous instance state
321 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
322 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
323 getView().restoreHierarchyState(container);
324 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700325 mListView.smoothScrollToPosition(0);
326 mGridView.smoothScrollToPosition(0);
327 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700328
329 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700330 }
331
332 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700333 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700334 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700335 }
336 };
337
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700338 // Kick off loader at least once
339 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
340
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700341 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700342 }
343
Jeff Sharkey42d26792013-09-06 13:22:09 -0700344 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700345 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700346 // There's only one request code right now. Replace this with a switch statement or
347 // something more scalable when more codes are added.
348 if (requestCode != REQUEST_COPY_DESTINATION) {
349 return;
350 }
351 if (resultCode == Activity.RESULT_CANCELED || data == null) {
352 // User pressed the back button or otherwise cancelled the destination pick. Don't
353 // proceed with the copy.
354 return;
355 }
356
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900357 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
358 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK));
Ben Kwaf5858932015-04-07 15:43:39 -0700359 }
360
361 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700362 public void onStop() {
363 super.onStop();
364
365 // Remember last scroll location
366 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
367 getView().saveHierarchyState(container);
368 final State state = getDisplayState(this);
369 state.dirState.put(mStateKey, container);
370 }
371
372 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700373 public void onResume() {
374 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700375 updateDisplayState();
376 }
377
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700378 public void onDisplayStateChanged() {
379 updateDisplayState();
380 }
381
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700382 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700383 // Sort order change always triggers reload; we'll trigger state change
384 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700385 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
386 }
387
388 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700389 final ContentResolver resolver = getActivity().getContentResolver();
390 final State state = getDisplayState(this);
391
392 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
393 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
394
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700395 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700396 final Uri stateUri = RecentsProvider.buildState(
397 root.authority, root.rootId, doc.documentId);
398 final ContentValues values = new ContentValues();
399 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700400
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700401 new AsyncTask<Void, Void, Void>() {
402 @Override
403 protected Void doInBackground(Void... params) {
404 resolver.insert(stateUri, values);
405 return null;
406 }
407 }.execute();
408 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700409
410 // Mode change is just visual change; no need to kick loader, and
411 // deliver change event immediately.
412 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700413 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700414
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700415 updateDisplayState();
416 }
417
418 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700419 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700420
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700421 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700422 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700423 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700424
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700425 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
426 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700427
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700428 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700429 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700430 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
431 } else {
432 choiceMode = ListView.CHOICE_MODE_NONE;
433 }
434
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700435 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700436 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700437 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700438 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700439 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700440 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700441 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700442 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700443 mGridView.setChoiceMode(choiceMode);
444 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700445 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700446 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700447 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700448 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700449 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700450 mListView.setChoiceMode(choiceMode);
451 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700452 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700453 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700454 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700455
456 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700457 }
458
459 private OnItemClickListener mItemListener = new OnItemClickListener() {
460 @Override
461 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700462 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700463 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700464 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
465 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
466 if (isDocumentEnabled(docMimeType, docFlags)) {
467 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700468 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700469 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700470 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700471 }
472 };
473
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700474 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
475 @Override
476 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
477 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100478 mode.setTitle(getResources()
479 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700480 return true;
481 }
482
483 @Override
484 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700485 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700486
487 final MenuItem open = menu.findItem(R.id.menu_open);
488 final MenuItem share = menu.findItem(R.id.menu_share);
489 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700490 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700491
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700492 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
493 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
494
495 open.setVisible(!manageOrBrowse);
496 share.setVisible(manageOrBrowse);
497 delete.setVisible(manageOrBrowse);
Ben Kwada1cea52015-04-22 10:33:53 -0700498 // Disable copying from the Recents view.
499 copy.setVisible(manageOrBrowse && mType != TYPE_RECENT_OPEN);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700500
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700501 return true;
502 }
503
504 @Override
505 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -0700506 final List<DocumentInfo> docs = getSelectedDocuments();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700507
Jeff Sharkey873daa32013-08-18 17:38:20 -0700508 final int id = item.getItemId();
509 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700510 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700511 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700512 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700513
514 } else if (id == R.id.menu_share) {
515 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700516 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700517 return true;
518
519 } else if (id == R.id.menu_delete) {
520 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700521 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700522 return true;
523
Ben Kwa41b26c12015-03-31 10:11:43 -0700524 } else if (id == R.id.menu_copy) {
525 onCopyDocuments(docs);
526 mode.finish();
527 return true;
528
Ben Kwa512a6ba2015-03-31 08:15:21 -0700529 } else if (id == R.id.menu_select_all) {
530 int count = mCurrentView.getCount();
531 for (int i = 0; i < count; i++) {
532 mCurrentView.setItemChecked(i, true);
533 }
534 updateDisplayState();
535 return true;
536
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700537 } else {
538 return false;
539 }
540 }
541
542 @Override
543 public void onDestroyActionMode(ActionMode mode) {
544 // ignored
545 }
546
547 @Override
548 public void onItemCheckedStateChanged(
549 ActionMode mode, int position, long id, boolean checked) {
550 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700551 // Directories and footer items cannot be checked
552 boolean valid = false;
553
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700554 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700555 if (cursor != null) {
556 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700557 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700558 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700559 }
560
561 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700562 mCurrentView.setItemChecked(position, false);
563 }
564 }
565
566 mode.setTitle(getResources()
567 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
568 }
569 };
570
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700571 private RecyclerListener mRecycleListener = new RecyclerListener() {
572 @Override
573 public void onMovedToScrapHeap(View view) {
574 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
575 if (iconThumb != null) {
576 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
577 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700578 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700579 iconThumb.setTag(null);
580 }
581 }
582 }
583 };
584
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700585 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700586 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700587
588 // Filter out directories - those can't be shared.
589 List<DocumentInfo> docsForSend = Lists.newArrayList();
590 for (DocumentInfo doc: docs) {
591 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
592 docsForSend.add(doc);
593 }
594 }
595
596 if (docsForSend.size() == 1) {
597 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700598
Jeff Sharkey873daa32013-08-18 17:38:20 -0700599 intent = new Intent(Intent.ACTION_SEND);
600 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
601 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700602 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700603 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700604
Ben Kwaf527c632015-04-08 15:03:35 -0700605 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700606 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
607 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
608 intent.addCategory(Intent.CATEGORY_DEFAULT);
609
610 final ArrayList<String> mimeTypes = Lists.newArrayList();
611 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700612 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700613 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700614 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700615 }
616
617 intent.setType(findCommonMimeType(mimeTypes));
618 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
619
620 } else {
621 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700622 }
623
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700624 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700625 startActivity(intent);
626 }
627
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700628 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700629 final Context context = getActivity();
630 final ContentResolver resolver = context.getContentResolver();
631
632 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700633 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700634 if (!doc.isDeleteSupported()) {
635 Log.w(TAG, "Skipping " + doc);
636 hadTrouble = true;
637 continue;
638 }
639
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700640 ContentProviderClient client = null;
641 try {
642 client = DocumentsApplication.acquireUnstableProviderOrThrow(
643 resolver, doc.derivedUri.getAuthority());
644 DocumentsContract.deleteDocument(client, doc.derivedUri);
645 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700646 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700647 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700648 } finally {
649 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700650 }
651 }
652
653 if (hadTrouble) {
654 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
655 }
656 }
657
Ben Kwa41b26c12015-03-31 10:11:43 -0700658 private void onCopyDocuments(List<DocumentInfo> docs) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900659 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700660
Ben Kwaf5858932015-04-07 15:43:39 -0700661 // Pop up a dialog to pick a destination. This is inadequate but works for now.
662 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900663 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900664 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900665 Uri.EMPTY,
666 getActivity(),
667 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900668 boolean directoryCopy = false;
669 for (DocumentInfo info : docs) {
670 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
671 directoryCopy = true;
672 break;
673 }
674 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900675 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwaf5858932015-04-07 15:43:39 -0700676 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700677 }
678
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700679 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700680 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700681 }
682
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700683 private static abstract class Footer {
684 private final int mItemViewType;
685
686 public Footer(int itemViewType) {
687 mItemViewType = itemViewType;
688 }
689
690 public abstract View getView(View convertView, ViewGroup parent);
691
692 public int getItemViewType() {
693 return mItemViewType;
694 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700695 }
696
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700697 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700698 public LoadingFooter() {
699 super(1);
700 }
701
Jeff Sharkey20b32272013-09-03 15:25:52 -0700702 @Override
703 public View getView(View convertView, ViewGroup parent) {
704 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700705 final State state = getDisplayState(DirectoryFragment.this);
706
Jeff Sharkey20b32272013-09-03 15:25:52 -0700707 if (convertView == null) {
708 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700709 if (state.derivedMode == MODE_LIST) {
710 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
711 } else if (state.derivedMode == MODE_GRID) {
712 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
713 } else {
714 throw new IllegalStateException();
715 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700716 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700717
Jeff Sharkey20b32272013-09-03 15:25:52 -0700718 return convertView;
719 }
720 }
721
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700722 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700723 private final int mIcon;
724 private final String mMessage;
725
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700726 public MessageFooter(int itemViewType, int icon, String message) {
727 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700728 mIcon = icon;
729 mMessage = message;
730 }
731
732 @Override
733 public View getView(View convertView, ViewGroup parent) {
734 final Context context = parent.getContext();
735 final State state = getDisplayState(DirectoryFragment.this);
736
737 if (convertView == null) {
738 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700739 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700740 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700741 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700742 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
743 } else {
744 throw new IllegalStateException();
745 }
746 }
747
748 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
749 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
750 icon.setImageResource(mIcon);
751 title.setText(mMessage);
752 return convertView;
753 }
754 }
755
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700756 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700757 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700758 private int mCursorCount;
759
760 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700761
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700762 public void swapResult(DirectoryResult result) {
763 mCursor = result != null ? result.cursor : null;
764 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700765
766 mFooters.clear();
767
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700768 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700769 if (extras != null) {
770 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
771 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700772 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700773 }
774 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
775 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700776 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700777 }
778 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
779 mFooters.add(new LoadingFooter());
780 }
781 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700782
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700783 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700784 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700785 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700786 }
787
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700788 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700789 mEmptyView.setVisibility(View.VISIBLE);
790 } else {
791 mEmptyView.setVisibility(View.GONE);
792 }
793
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700794 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700795 }
796
797 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700798 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700799 if (position < mCursorCount) {
800 return getDocumentView(position, convertView, parent);
801 } else {
802 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700803 convertView = mFooters.get(position).getView(convertView, parent);
804 // Only the view itself is disabled; contents inside shouldn't
805 // be dimmed.
806 convertView.setEnabled(false);
807 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700808 }
809 }
810
811 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700812 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700813 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700814
Jeff Sharkey9656a532013-09-13 13:42:19 -0700815 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
816
Jeff Sharkey873daa32013-08-18 17:38:20 -0700817 final RootsCache roots = DocumentsApplication.getRootsCache(context);
818 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
819 context, mThumbSize);
820
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700821 if (convertView == null) {
822 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700823 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700824 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700825 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700826 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
827 } else {
828 throw new IllegalStateException();
829 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700830 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700831
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700832 final Cursor cursor = getItem(position);
833
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700834 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
835 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700836 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
837 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
838 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
839 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
840 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
841 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
842 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
843 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700844
Jeff Sharkey9656a532013-09-13 13:42:19 -0700845 final View line1 = convertView.findViewById(R.id.line1);
846 final View line2 = convertView.findViewById(R.id.line2);
847
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700848 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
849 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700850 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
851 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700852 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700853 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
854 final TextView date = (TextView) convertView.findViewById(R.id.date);
855 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700856
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700857 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700858 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700859 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700860 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700861 }
862
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700863 iconMime.animate().cancel();
864 iconThumb.animate().cancel();
865
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700866 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700867 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700868 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700869 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700870
Jeff Sharkey7e544612014-08-29 15:38:27 -0700871 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
872 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
873
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700874 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700875 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700876 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700877 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700878 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700879 iconThumb.setImageBitmap(cachedResult);
880 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700881 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700882 iconThumb.setImageDrawable(null);
883 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700884 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700885 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700886 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700887 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700888 }
889
890 // Always throw MIME icon into place, even when a thumbnail is being
891 // loaded in background.
892 if (cacheHit) {
893 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700894 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700895 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700896 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700897 iconMime.setAlpha(1f);
898 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700899 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700900 if (docIcon != 0) {
901 iconMime.setImageDrawable(
902 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
903 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700904 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
905 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700906 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700907 }
908
Jeff Sharkey9656a532013-09-13 13:42:19 -0700909 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700910 boolean hasLine2 = false;
911
Jeff Sharkey9656a532013-09-13 13:42:19 -0700912 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
913 if (!hideTitle) {
914 title.setText(docDisplayName);
915 hasLine1 = true;
916 }
917
918 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700919 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700920 // We've already had to enumerate roots before any results can
921 // be shown, so this will never block.
922 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700923 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700924 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700925 } else {
926 iconDrawable = root.loadIcon(context);
927 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700928
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700929 if (summary != null) {
930 final boolean alwaysShowSummary = getResources()
931 .getBoolean(R.bool.always_show_summary);
932 if (alwaysShowSummary) {
933 summary.setText(root.getDirectoryString());
934 summary.setVisibility(View.VISIBLE);
935 hasLine2 = true;
936 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700937 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700938 // No summary needed if icon speaks for itself
939 summary.setVisibility(View.INVISIBLE);
940 } else {
941 summary.setText(root.getDirectoryString());
942 summary.setVisibility(View.VISIBLE);
943 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
944 hasLine2 = true;
945 }
946 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700947 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700948 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700949 // Directories showing thumbnails in grid mode get a little icon
950 // hint to remind user they're a directory.
951 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
952 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700953 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700954 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700955 }
956
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700957 if (summary != null) {
958 if (docSummary != null) {
959 summary.setText(docSummary);
960 summary.setVisibility(View.VISIBLE);
961 hasLine2 = true;
962 } else {
963 summary.setVisibility(View.INVISIBLE);
964 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700965 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700966 }
967
Jeff Sharkey9656a532013-09-13 13:42:19 -0700968 if (icon1 != null) icon1.setVisibility(View.GONE);
969 if (icon2 != null) icon2.setVisibility(View.GONE);
970
971 if (iconDrawable != null) {
972 if (hasLine1) {
973 icon1.setVisibility(View.VISIBLE);
974 icon1.setImageDrawable(iconDrawable);
975 } else {
976 icon2.setVisibility(View.VISIBLE);
977 icon2.setImageDrawable(iconDrawable);
978 }
979 }
980
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700981 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700982 date.setText(null);
983 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700984 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700985 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700986 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700987
988 if (state.showSize) {
989 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700990 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700991 size.setText(null);
992 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700993 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700994 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700995 }
996 } else {
997 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700998 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700999
Jeff Sharkey9656a532013-09-13 13:42:19 -07001000 if (line1 != null) {
1001 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1002 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001003 if (line2 != null) {
1004 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1005 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001006
Jeff Sharkey7e544612014-08-29 15:38:27 -07001007 setEnabledRecursive(convertView, enabled);
1008
1009 iconMime.setAlpha(iconAlpha);
1010 iconThumb.setAlpha(iconAlpha);
1011 if (icon1 != null) icon1.setAlpha(iconAlpha);
1012 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001013
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001014 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001015 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001016
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001017 @Override
1018 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001019 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001020 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001021
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001022 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001023 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001024 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001025 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001026 return mCursor;
1027 } else {
1028 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001029 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001030 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001031
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001032 @Override
1033 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001034 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001035 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001036
1037 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001038 public int getViewTypeCount() {
1039 return 4;
1040 }
1041
1042 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001043 public int getItemViewType(int position) {
1044 if (position < mCursorCount) {
1045 return 0;
1046 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001047 position -= mCursorCount;
1048 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001049 }
1050 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001051 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001052
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001053 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1054 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001055 private final Uri mUri;
1056 private final ImageView mIconMime;
1057 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001058 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001059 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001060 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001061
Jeff Sharkey7e544612014-08-29 15:38:27 -07001062 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1063 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001064 mUri = uri;
1065 mIconMime = iconMime;
1066 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001067 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001068 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001069 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001070 }
1071
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001072 @Override
1073 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001074 cancel(false);
1075 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001076 }
1077
1078 @Override
1079 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001080 if (isCancelled()) return null;
1081
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001082 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001083 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001084
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001085 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001086 Bitmap result = null;
1087 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001088 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1089 resolver, mUri.getAuthority());
1090 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001091 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001092 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1093 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001094 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001095 }
1096 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001097 if (!(e instanceof OperationCanceledException)) {
1098 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1099 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001100 } finally {
1101 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001102 }
1103 return result;
1104 }
1105
1106 @Override
1107 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001108 if (mIconThumb.getTag() == this && result != null) {
1109 mIconThumb.setTag(null);
1110 mIconThumb.setImageBitmap(result);
1111
Jeff Sharkey7e544612014-08-29 15:38:27 -07001112 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001113 mIconMime.animate().alpha(0f).start();
1114 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001115 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001116 }
1117 }
1118 }
1119
1120 private static String formatTime(Context context, long when) {
1121 // TODO: DateUtils should make this easier
1122 Time then = new Time();
1123 then.set(when);
1124 Time now = new Time();
1125 now.setToNow();
1126
1127 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1128 | DateUtils.FORMAT_ABBREV_ALL;
1129
1130 if (then.year != now.year) {
1131 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1132 } else if (then.yearDay != now.yearDay) {
1133 flags |= DateUtils.FORMAT_SHOW_DATE;
1134 } else {
1135 flags |= DateUtils.FORMAT_SHOW_TIME;
1136 }
1137
1138 return DateUtils.formatDateTime(context, when, flags);
1139 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001140
1141 private String findCommonMimeType(List<String> mimeTypes) {
1142 String[] commonType = mimeTypes.get(0).split("/");
1143 if (commonType.length != 2) {
1144 return "*/*";
1145 }
1146
1147 for (int i = 1; i < mimeTypes.size(); i++) {
1148 String[] type = mimeTypes.get(i).split("/");
1149 if (type.length != 2) continue;
1150
1151 if (!commonType[1].equals(type[1])) {
1152 commonType[1] = "*";
1153 }
1154
1155 if (!commonType[0].equals(type[0])) {
1156 commonType[0] = "*";
1157 commonType[1] = "*";
1158 break;
1159 }
1160 }
1161
1162 return commonType[0] + "/" + commonType[1];
1163 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001164
1165 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001166 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001167 if (v.isEnabled() == enabled) return;
1168 v.setEnabled(enabled);
1169
1170 if (v instanceof ViewGroup) {
1171 final ViewGroup vg = (ViewGroup) v;
1172 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1173 setEnabledRecursive(vg.getChildAt(i), enabled);
1174 }
1175 }
1176 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001177
1178 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1179 final State state = getDisplayState(DirectoryFragment.this);
1180
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001181 // Directories are always enabled
1182 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1183 return true;
1184 }
1185
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001186 // Read-only files are disabled when creating
1187 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1188 return false;
1189 }
1190
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001191 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1192 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001193
1194 public List<DocumentInfo> getSelectedDocuments() {
1195 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
1196 final List<DocumentInfo> docs = Lists.newArrayList();
1197 final int size = checked.size();
1198 for (int i = 0; i < size; i++) {
1199 if (checked.valueAt(i)) {
1200 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
1201 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1202 docs.add(doc);
1203 }
1204 }
1205 return docs;
1206 }
1207
1208 private void copyFromClipData(ClipData clipData, DocumentInfo dstDir) {
1209 final List<DocumentInfo> srcDocs = getDocumentsFromClipData(clipData);
1210
1211 if (srcDocs.isEmpty())
1212 return;
1213
1214 final DocumentStack curStack = getDisplayState(this).stack;
1215 DocumentStack tmpStack = new DocumentStack();
1216 if (dstDir != null) {
1217 tmpStack.push(dstDir);
1218 tmpStack.addAll(curStack);
1219 } else {
1220 tmpStack = curStack;
1221 }
1222
1223 CopyService.start(getActivity(), srcDocs, tmpStack);
1224 }
1225
1226 private List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
1227 final List<DocumentInfo> srcDocs = Lists.newArrayList();
1228
1229 Context context = getActivity();
1230 final ContentResolver resolver = context.getContentResolver();
1231
1232 int itemCount = clipData.getItemCount();
1233 for (int i = 0; i < itemCount; ++i) {
1234 ClipData.Item item = clipData.getItemAt(i);
1235 Uri itemUri = item.getUri();
1236 if (itemUri != null && DocumentsContract.isDocumentUri(context, itemUri)) {
1237 try {
1238 Cursor cursor = resolver.query(itemUri, null, null, null, null);
1239 cursor.moveToPosition(0);
1240 srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority()));
1241 } catch (Exception e) {
1242 Log.e(TAG, e.getMessage());
1243 }
1244 }
1245 }
1246
1247 return srcDocs;
1248 }
1249
1250 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1251 Context context = getActivity();
1252 final ContentResolver resolver = context.getContentResolver();
1253 ClipData clipData = null;
1254 for (DocumentInfo doc : docs) {
1255 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1256 if (clipData == null) {
1257 clipData = ClipData.newUri(resolver, "", uri);
1258 } else {
1259 // TODO: update list of mime types in ClipData.
1260 clipData.addItem(new ClipData.Item(uri));
1261 }
1262 }
1263 return clipData;
1264 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001265}