blob: a789da86badd902a0599e2a2cde102de26403e7f [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;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070037import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070038import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070039import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070041import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070043import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070044import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070045import android.graphics.Bitmap;
46import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070047import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070048import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070049import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070053import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070054import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070056import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070057import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070058import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070059import android.text.format.Time;
60import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070061import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070062import android.util.SparseBooleanArray;
63import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070064import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070065import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070066import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070067import android.view.View;
68import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070069import android.widget.AbsListView;
70import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070071import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.widget.AdapterView;
73import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070074import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070075import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070076import android.widget.ImageView;
77import android.widget.ListView;
78import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070079import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080
Steve McKayd0a2a2c2015-03-25 14:35:33 -070081import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070082import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070083import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070084import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090085import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070086import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070087import com.google.android.collect.Lists;
88
89import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070090import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070091
92/**
93 * Display the documents inside a single directory.
94 */
95public class DirectoryFragment extends Fragment {
96
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070097 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070098 private ListView mListView;
99 private GridView mGridView;
100
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700101 private AbsListView mCurrentView;
102
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700103 public static final int TYPE_NORMAL = 1;
104 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700105 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700106
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700107 public static final int ANIM_NONE = 1;
108 public static final int ANIM_SIDE = 2;
109 public static final int ANIM_DOWN = 3;
110 public static final int ANIM_UP = 4;
111
Ben Kwaf5858932015-04-07 15:43:39 -0700112 public static final int REQUEST_COPY_DESTINATION = 1;
113
Jeff Sharkey5b535922013-08-02 15:55:26 -0700114 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700115 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700116
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700117 private int mLastMode = MODE_UNKNOWN;
118 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700119 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700120
Jeff Sharkey9656a532013-09-13 13:42:19 -0700121 private boolean mHideGridTitles = false;
122
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700123 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700124 private Point mThumbSize;
125
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700126 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700127 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700129 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700130 private static final String EXTRA_ROOT = "root";
131 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700132 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700133 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700134
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700135 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700136
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700137 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
138 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700139 }
140
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700141 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
142 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700143 }
144
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700145 public static void showRecentsOpen(FragmentManager fm, int anim) {
146 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700147 }
148
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700149 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
150 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700151 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700152 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700153 args.putParcelable(EXTRA_ROOT, root);
154 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700155 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700156
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700157 final FragmentTransaction ft = fm.beginTransaction();
158 switch (anim) {
159 case ANIM_SIDE:
160 args.putBoolean(EXTRA_IGNORE_STATE, true);
161 break;
162 case ANIM_DOWN:
163 args.putBoolean(EXTRA_IGNORE_STATE, true);
164 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
165 break;
166 case ANIM_UP:
167 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
168 break;
169 }
170
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700171 final DirectoryFragment fragment = new DirectoryFragment();
172 fragment.setArguments(args);
173
Jeff Sharkey76112212013-08-06 11:26:10 -0700174 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700175 ft.commitAllowingStateLoss();
176 }
177
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700178 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
179 final StringBuilder builder = new StringBuilder();
180 builder.append(root != null ? root.authority : "null").append(';');
181 builder.append(root != null ? root.rootId : "null").append(';');
182 builder.append(doc != null ? doc.documentId : "null");
183 return builder.toString();
184 }
185
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700186 public static DirectoryFragment get(FragmentManager fm) {
187 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700188 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700189 }
190
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700191 @Override
192 public View onCreateView(
193 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
194 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700195 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700196 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
197
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700198 mEmptyView = view.findViewById(android.R.id.empty);
199
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700200 mListView = (ListView) view.findViewById(R.id.list);
201 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700202 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700203 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700204
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700205 // Indent our list divider to align with text
206 final Drawable divider = mListView.getDivider();
207 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
208 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
209 if (insetLeft) {
210 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
211 } else {
212 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
213 }
214
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700215 mGridView = (GridView) view.findViewById(R.id.grid);
216 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700217 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700218 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700219
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700220 return view;
221 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700222
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700223 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700224 public void onDestroyView() {
225 super.onDestroyView();
226
227 // Cancel any outstanding thumbnail requests
228 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
229 final int count = target.getChildCount();
230 for (int i = 0; i < count; i++) {
231 final View view = target.getChildAt(i);
232 mRecycleListener.onMovedToScrapHeap(view);
233 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700234
235 // Tear down any selection in progress
236 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
237 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700238 }
239
240 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700241 public void onActivityCreated(Bundle savedInstanceState) {
242 super.onActivityCreated(savedInstanceState);
243
244 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700245 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700246
Jeff Sharkey9656a532013-09-13 13:42:19 -0700247 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
248 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
249
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700250 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700251 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700252 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700253
Jeff Sharkey9656a532013-09-13 13:42:19 -0700254 if (mType == TYPE_RECENT_OPEN) {
255 // Hide titles when showing recents for picking images/videos
256 mHideGridTitles = MimePredicate.mimeMatches(
257 MimePredicate.VISUAL_MIMES, state.acceptMimes);
258 } else {
259 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
260 }
261
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700262 final ActivityManager am = (ActivityManager) context.getSystemService(
263 Context.ACTIVITY_SERVICE);
264 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
265
Jeff Sharkey46899c82013-08-18 22:26:48 -0700266 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700267 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700268 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700269 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700270
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700271 Uri contentsUri;
272 switch (mType) {
273 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700274 contentsUri = DocumentsContract.buildChildDocumentsUri(
275 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700276 if (state.action == ACTION_MANAGE) {
277 contentsUri = DocumentsContract.setManageMode(contentsUri);
278 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700279 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700280 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700281 case TYPE_SEARCH:
282 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700283 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700284 if (state.action == ACTION_MANAGE) {
285 contentsUri = DocumentsContract.setManageMode(contentsUri);
286 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700287 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700288 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700289 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700290 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700291 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700292 default:
293 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700294 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700295 }
296
297 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700298 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700299 if (!isAdded()) return;
300
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700301 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700302
303 // Push latest state up to UI
304 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700305 if (result.mode != MODE_UNKNOWN) {
306 state.derivedMode = result.mode;
307 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700308 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700309 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700310
311 updateDisplayState();
312
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700313 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700314 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
315 context instanceof DocumentsActivity) {
316 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700317 }
318
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700319 // Restore any previous instance state
320 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
321 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
322 getView().restoreHierarchyState(container);
323 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700324 mListView.smoothScrollToPosition(0);
325 mGridView.smoothScrollToPosition(0);
326 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700327
328 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700329 }
330
331 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700332 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700333 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700334 }
335 };
336
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700337 // Kick off loader at least once
338 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
339
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700340 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700341 }
342
Jeff Sharkey42d26792013-09-06 13:22:09 -0700343 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700344 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700345 // There's only one request code right now. Replace this with a switch statement or
346 // something more scalable when more codes are added.
347 if (requestCode != REQUEST_COPY_DESTINATION) {
348 return;
349 }
350 if (resultCode == Activity.RESULT_CANCELED || data == null) {
351 // User pressed the back button or otherwise cancelled the destination pick. Don't
352 // proceed with the copy.
353 return;
354 }
355
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900356 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
357 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK));
Ben Kwaf5858932015-04-07 15:43:39 -0700358 }
359
360 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700361 public void onStop() {
362 super.onStop();
363
364 // Remember last scroll location
365 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
366 getView().saveHierarchyState(container);
367 final State state = getDisplayState(this);
368 state.dirState.put(mStateKey, container);
369 }
370
371 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700372 public void onResume() {
373 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700374 updateDisplayState();
375 }
376
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700377 public void onDisplayStateChanged() {
378 updateDisplayState();
379 }
380
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700381 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700382 // Sort order change always triggers reload; we'll trigger state change
383 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700384 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
385 }
386
387 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700388 final ContentResolver resolver = getActivity().getContentResolver();
389 final State state = getDisplayState(this);
390
391 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
392 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
393
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700394 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700395 final Uri stateUri = RecentsProvider.buildState(
396 root.authority, root.rootId, doc.documentId);
397 final ContentValues values = new ContentValues();
398 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700399
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700400 new AsyncTask<Void, Void, Void>() {
401 @Override
402 protected Void doInBackground(Void... params) {
403 resolver.insert(stateUri, values);
404 return null;
405 }
406 }.execute();
407 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700408
409 // Mode change is just visual change; no need to kick loader, and
410 // deliver change event immediately.
411 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700412 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700413
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700414 updateDisplayState();
415 }
416
417 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700418 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700419
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700420 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700421 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700422 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700423
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700424 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
425 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700426
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700427 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700428 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700429 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
430 } else {
431 choiceMode = ListView.CHOICE_MODE_NONE;
432 }
433
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700434 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700435 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700436 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700437 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700438 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700439 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700440 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700441 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700442 mGridView.setChoiceMode(choiceMode);
443 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700444 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700445 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700446 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700448 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700449 mListView.setChoiceMode(choiceMode);
450 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700451 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700452 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700453 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700454
455 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700456 }
457
458 private OnItemClickListener mItemListener = new OnItemClickListener() {
459 @Override
460 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700461 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700462 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700463 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
464 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
465 if (isDocumentEnabled(docMimeType, docFlags)) {
466 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700467 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700468 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700469 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700470 }
471 };
472
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700473 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
474 @Override
475 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
476 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100477 mode.setTitle(getResources()
478 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700479 return true;
480 }
481
482 @Override
483 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700484 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700485
486 final MenuItem open = menu.findItem(R.id.menu_open);
487 final MenuItem share = menu.findItem(R.id.menu_share);
488 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700489 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700490
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700491 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
492 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
493
494 open.setVisible(!manageOrBrowse);
495 share.setVisible(manageOrBrowse);
496 delete.setVisible(manageOrBrowse);
Ben Kwada1cea52015-04-22 10:33:53 -0700497 // Disable copying from the Recents view.
498 copy.setVisible(manageOrBrowse && mType != TYPE_RECENT_OPEN);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700500 return true;
501 }
502
503 @Override
504 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700505 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700506 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700507 final int size = checked.size();
508 for (int i = 0; i < size; i++) {
509 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700510 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700511 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700512 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700513 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700514 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700515
Jeff Sharkey873daa32013-08-18 17:38:20 -0700516 final int id = item.getItemId();
517 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700518 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700519 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700520 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700521
522 } else if (id == R.id.menu_share) {
523 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700524 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700525 return true;
526
527 } else if (id == R.id.menu_delete) {
528 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700529 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700530 return true;
531
Ben Kwa41b26c12015-03-31 10:11:43 -0700532 } else if (id == R.id.menu_copy) {
533 onCopyDocuments(docs);
534 mode.finish();
535 return true;
536
Ben Kwa512a6ba2015-03-31 08:15:21 -0700537 } else if (id == R.id.menu_select_all) {
538 int count = mCurrentView.getCount();
539 for (int i = 0; i < count; i++) {
540 mCurrentView.setItemChecked(i, true);
541 }
542 updateDisplayState();
543 return true;
544
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700545 } else {
546 return false;
547 }
548 }
549
550 @Override
551 public void onDestroyActionMode(ActionMode mode) {
552 // ignored
553 }
554
555 @Override
556 public void onItemCheckedStateChanged(
557 ActionMode mode, int position, long id, boolean checked) {
558 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700559 // Directories and footer items cannot be checked
560 boolean valid = false;
561
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700562 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700563 if (cursor != null) {
564 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700565 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700566 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700567 }
568
569 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700570 mCurrentView.setItemChecked(position, false);
571 }
572 }
573
574 mode.setTitle(getResources()
575 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
576 }
577 };
578
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700579 private RecyclerListener mRecycleListener = new RecyclerListener() {
580 @Override
581 public void onMovedToScrapHeap(View view) {
582 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
583 if (iconThumb != null) {
584 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
585 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700586 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700587 iconThumb.setTag(null);
588 }
589 }
590 }
591 };
592
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700593 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700594 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700595
596 // Filter out directories - those can't be shared.
597 List<DocumentInfo> docsForSend = Lists.newArrayList();
598 for (DocumentInfo doc: docs) {
599 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
600 docsForSend.add(doc);
601 }
602 }
603
604 if (docsForSend.size() == 1) {
605 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700606
Jeff Sharkey873daa32013-08-18 17:38:20 -0700607 intent = new Intent(Intent.ACTION_SEND);
608 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
609 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700610 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700611 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700612
Ben Kwaf527c632015-04-08 15:03:35 -0700613 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700614 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
615 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
616 intent.addCategory(Intent.CATEGORY_DEFAULT);
617
618 final ArrayList<String> mimeTypes = Lists.newArrayList();
619 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700620 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700621 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700622 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700623 }
624
625 intent.setType(findCommonMimeType(mimeTypes));
626 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
627
628 } else {
629 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700630 }
631
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700632 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700633 startActivity(intent);
634 }
635
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700636 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700637 final Context context = getActivity();
638 final ContentResolver resolver = context.getContentResolver();
639
640 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700641 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700642 if (!doc.isDeleteSupported()) {
643 Log.w(TAG, "Skipping " + doc);
644 hadTrouble = true;
645 continue;
646 }
647
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700648 ContentProviderClient client = null;
649 try {
650 client = DocumentsApplication.acquireUnstableProviderOrThrow(
651 resolver, doc.derivedUri.getAuthority());
652 DocumentsContract.deleteDocument(client, doc.derivedUri);
653 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700654 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700655 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700656 } finally {
657 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700658 }
659 }
660
661 if (hadTrouble) {
662 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
663 }
664 }
665
Ben Kwa41b26c12015-03-31 10:11:43 -0700666 private void onCopyDocuments(List<DocumentInfo> docs) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900667 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700668
Ben Kwaf5858932015-04-07 15:43:39 -0700669 // Pop up a dialog to pick a destination. This is inadequate but works for now.
670 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900671 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900672 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900673 Uri.EMPTY,
674 getActivity(),
675 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900676 boolean directoryCopy = false;
677 for (DocumentInfo info : docs) {
678 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
679 directoryCopy = true;
680 break;
681 }
682 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900683 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwaf5858932015-04-07 15:43:39 -0700684 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700685 }
686
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700687 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700688 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700689 }
690
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700691 private static abstract class Footer {
692 private final int mItemViewType;
693
694 public Footer(int itemViewType) {
695 mItemViewType = itemViewType;
696 }
697
698 public abstract View getView(View convertView, ViewGroup parent);
699
700 public int getItemViewType() {
701 return mItemViewType;
702 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700703 }
704
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700705 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700706 public LoadingFooter() {
707 super(1);
708 }
709
Jeff Sharkey20b32272013-09-03 15:25:52 -0700710 @Override
711 public View getView(View convertView, ViewGroup parent) {
712 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700713 final State state = getDisplayState(DirectoryFragment.this);
714
Jeff Sharkey20b32272013-09-03 15:25:52 -0700715 if (convertView == null) {
716 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700717 if (state.derivedMode == MODE_LIST) {
718 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
719 } else if (state.derivedMode == MODE_GRID) {
720 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
721 } else {
722 throw new IllegalStateException();
723 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700724 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700725
Jeff Sharkey20b32272013-09-03 15:25:52 -0700726 return convertView;
727 }
728 }
729
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700730 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700731 private final int mIcon;
732 private final String mMessage;
733
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700734 public MessageFooter(int itemViewType, int icon, String message) {
735 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700736 mIcon = icon;
737 mMessage = message;
738 }
739
740 @Override
741 public View getView(View convertView, ViewGroup parent) {
742 final Context context = parent.getContext();
743 final State state = getDisplayState(DirectoryFragment.this);
744
745 if (convertView == null) {
746 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700747 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700748 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700749 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700750 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
751 } else {
752 throw new IllegalStateException();
753 }
754 }
755
756 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
757 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
758 icon.setImageResource(mIcon);
759 title.setText(mMessage);
760 return convertView;
761 }
762 }
763
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700764 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700765 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700766 private int mCursorCount;
767
768 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700769
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700770 public void swapResult(DirectoryResult result) {
771 mCursor = result != null ? result.cursor : null;
772 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700773
774 mFooters.clear();
775
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700776 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700777 if (extras != null) {
778 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
779 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700780 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700781 }
782 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
783 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700784 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700785 }
786 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
787 mFooters.add(new LoadingFooter());
788 }
789 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700790
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700791 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700792 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700793 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700794 }
795
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700796 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700797 mEmptyView.setVisibility(View.VISIBLE);
798 } else {
799 mEmptyView.setVisibility(View.GONE);
800 }
801
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700802 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700803 }
804
805 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700806 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700807 if (position < mCursorCount) {
808 return getDocumentView(position, convertView, parent);
809 } else {
810 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700811 convertView = mFooters.get(position).getView(convertView, parent);
812 // Only the view itself is disabled; contents inside shouldn't
813 // be dimmed.
814 convertView.setEnabled(false);
815 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700816 }
817 }
818
819 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700820 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700821 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700822
Jeff Sharkey9656a532013-09-13 13:42:19 -0700823 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
824
Jeff Sharkey873daa32013-08-18 17:38:20 -0700825 final RootsCache roots = DocumentsApplication.getRootsCache(context);
826 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
827 context, mThumbSize);
828
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700829 if (convertView == null) {
830 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700831 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700832 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700833 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700834 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
835 } else {
836 throw new IllegalStateException();
837 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700838 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700839
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700840 final Cursor cursor = getItem(position);
841
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700842 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
843 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700844 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
845 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
846 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
847 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
848 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
849 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
850 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
851 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700852
Jeff Sharkey9656a532013-09-13 13:42:19 -0700853 final View line1 = convertView.findViewById(R.id.line1);
854 final View line2 = convertView.findViewById(R.id.line2);
855
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700856 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
857 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700858 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
859 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700860 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700861 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
862 final TextView date = (TextView) convertView.findViewById(R.id.date);
863 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700864
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700865 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700866 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700867 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700868 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700869 }
870
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700871 iconMime.animate().cancel();
872 iconThumb.animate().cancel();
873
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700874 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700875 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700876 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700877 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700878
Jeff Sharkey7e544612014-08-29 15:38:27 -0700879 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
880 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
881
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700882 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700883 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700884 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700885 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700886 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700887 iconThumb.setImageBitmap(cachedResult);
888 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700889 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700890 iconThumb.setImageDrawable(null);
891 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700892 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700893 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700894 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700895 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700896 }
897
898 // Always throw MIME icon into place, even when a thumbnail is being
899 // loaded in background.
900 if (cacheHit) {
901 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700902 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700903 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700904 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700905 iconMime.setAlpha(1f);
906 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700907 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700908 if (docIcon != 0) {
909 iconMime.setImageDrawable(
910 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
911 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700912 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
913 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700914 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700915 }
916
Jeff Sharkey9656a532013-09-13 13:42:19 -0700917 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700918 boolean hasLine2 = false;
919
Jeff Sharkey9656a532013-09-13 13:42:19 -0700920 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
921 if (!hideTitle) {
922 title.setText(docDisplayName);
923 hasLine1 = true;
924 }
925
926 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700927 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700928 // We've already had to enumerate roots before any results can
929 // be shown, so this will never block.
930 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700931 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700932 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700933 } else {
934 iconDrawable = root.loadIcon(context);
935 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700936
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700937 if (summary != null) {
938 final boolean alwaysShowSummary = getResources()
939 .getBoolean(R.bool.always_show_summary);
940 if (alwaysShowSummary) {
941 summary.setText(root.getDirectoryString());
942 summary.setVisibility(View.VISIBLE);
943 hasLine2 = true;
944 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700945 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700946 // No summary needed if icon speaks for itself
947 summary.setVisibility(View.INVISIBLE);
948 } else {
949 summary.setText(root.getDirectoryString());
950 summary.setVisibility(View.VISIBLE);
951 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
952 hasLine2 = true;
953 }
954 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700955 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700956 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700957 // Directories showing thumbnails in grid mode get a little icon
958 // hint to remind user they're a directory.
959 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
960 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700961 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700962 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700963 }
964
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700965 if (summary != null) {
966 if (docSummary != null) {
967 summary.setText(docSummary);
968 summary.setVisibility(View.VISIBLE);
969 hasLine2 = true;
970 } else {
971 summary.setVisibility(View.INVISIBLE);
972 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700973 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700974 }
975
Jeff Sharkey9656a532013-09-13 13:42:19 -0700976 if (icon1 != null) icon1.setVisibility(View.GONE);
977 if (icon2 != null) icon2.setVisibility(View.GONE);
978
979 if (iconDrawable != null) {
980 if (hasLine1) {
981 icon1.setVisibility(View.VISIBLE);
982 icon1.setImageDrawable(iconDrawable);
983 } else {
984 icon2.setVisibility(View.VISIBLE);
985 icon2.setImageDrawable(iconDrawable);
986 }
987 }
988
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700989 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700990 date.setText(null);
991 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700992 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700993 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700994 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700995
996 if (state.showSize) {
997 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700998 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700999 size.setText(null);
1000 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001001 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001002 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001003 }
1004 } else {
1005 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001006 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001007
Jeff Sharkey9656a532013-09-13 13:42:19 -07001008 if (line1 != null) {
1009 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1010 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001011 if (line2 != null) {
1012 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1013 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001014
Jeff Sharkey7e544612014-08-29 15:38:27 -07001015 setEnabledRecursive(convertView, enabled);
1016
1017 iconMime.setAlpha(iconAlpha);
1018 iconThumb.setAlpha(iconAlpha);
1019 if (icon1 != null) icon1.setAlpha(iconAlpha);
1020 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001021
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001022 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001023 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001024
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001025 @Override
1026 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001027 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001028 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001029
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001030 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001031 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001032 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001033 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001034 return mCursor;
1035 } else {
1036 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001037 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001038 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001039
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001040 @Override
1041 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001042 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001043 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001044
1045 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001046 public int getViewTypeCount() {
1047 return 4;
1048 }
1049
1050 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001051 public int getItemViewType(int position) {
1052 if (position < mCursorCount) {
1053 return 0;
1054 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001055 position -= mCursorCount;
1056 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001057 }
1058 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001059 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001060
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001061 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1062 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001063 private final Uri mUri;
1064 private final ImageView mIconMime;
1065 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001066 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001067 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001068 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001069
Jeff Sharkey7e544612014-08-29 15:38:27 -07001070 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1071 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001072 mUri = uri;
1073 mIconMime = iconMime;
1074 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001075 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001076 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001077 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001078 }
1079
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001080 @Override
1081 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001082 cancel(false);
1083 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001084 }
1085
1086 @Override
1087 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001088 if (isCancelled()) return null;
1089
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001090 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001091 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001092
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001093 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001094 Bitmap result = null;
1095 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001096 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1097 resolver, mUri.getAuthority());
1098 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001099 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001100 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1101 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001102 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001103 }
1104 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001105 if (!(e instanceof OperationCanceledException)) {
1106 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1107 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001108 } finally {
1109 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001110 }
1111 return result;
1112 }
1113
1114 @Override
1115 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001116 if (mIconThumb.getTag() == this && result != null) {
1117 mIconThumb.setTag(null);
1118 mIconThumb.setImageBitmap(result);
1119
Jeff Sharkey7e544612014-08-29 15:38:27 -07001120 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001121 mIconMime.animate().alpha(0f).start();
1122 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001123 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001124 }
1125 }
1126 }
1127
1128 private static String formatTime(Context context, long when) {
1129 // TODO: DateUtils should make this easier
1130 Time then = new Time();
1131 then.set(when);
1132 Time now = new Time();
1133 now.setToNow();
1134
1135 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1136 | DateUtils.FORMAT_ABBREV_ALL;
1137
1138 if (then.year != now.year) {
1139 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1140 } else if (then.yearDay != now.yearDay) {
1141 flags |= DateUtils.FORMAT_SHOW_DATE;
1142 } else {
1143 flags |= DateUtils.FORMAT_SHOW_TIME;
1144 }
1145
1146 return DateUtils.formatDateTime(context, when, flags);
1147 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001148
1149 private String findCommonMimeType(List<String> mimeTypes) {
1150 String[] commonType = mimeTypes.get(0).split("/");
1151 if (commonType.length != 2) {
1152 return "*/*";
1153 }
1154
1155 for (int i = 1; i < mimeTypes.size(); i++) {
1156 String[] type = mimeTypes.get(i).split("/");
1157 if (type.length != 2) continue;
1158
1159 if (!commonType[1].equals(type[1])) {
1160 commonType[1] = "*";
1161 }
1162
1163 if (!commonType[0].equals(type[0])) {
1164 commonType[0] = "*";
1165 commonType[1] = "*";
1166 break;
1167 }
1168 }
1169
1170 return commonType[0] + "/" + commonType[1];
1171 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001172
1173 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001174 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001175 if (v.isEnabled() == enabled) return;
1176 v.setEnabled(enabled);
1177
1178 if (v instanceof ViewGroup) {
1179 final ViewGroup vg = (ViewGroup) v;
1180 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1181 setEnabledRecursive(vg.getChildAt(i), enabled);
1182 }
1183 }
1184 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001185
1186 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1187 final State state = getDisplayState(DirectoryFragment.this);
1188
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001189 // Directories are always enabled
1190 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1191 return true;
1192 }
1193
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001194 // Read-only files are disabled when creating
1195 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1196 return false;
1197 }
1198
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001199 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1200 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001201}