blob: 83071bd02143d25fb978f08b08e963736a08b620 [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 Sharkey8a8fb672013-05-07 12:41:33 -070019import static com.android.documentsui.DocumentsActivity.TAG;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070020import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
21import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
22import static com.android.documentsui.BaseActivity.State.MODE_GRID;
23import static com.android.documentsui.BaseActivity.State.MODE_LIST;
24import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
25import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070026import static com.android.documentsui.model.DocumentInfo.getCursorInt;
27import static com.android.documentsui.model.DocumentInfo.getCursorLong;
28import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070029
Ben Kwaf5858932015-04-07 15:43:39 -070030import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070031import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070032import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070033import android.app.FragmentManager;
34import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070035import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070036import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070037import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070038import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070040import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070041import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070042import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070043import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070044import android.graphics.Bitmap;
45import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070046import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070047import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070048import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070049import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070050import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070051import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070052import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070053import android.os.Parcelable;
Ben Kwa41b26c12015-03-31 10:11:43 -070054import android.os.SystemProperties;
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;
Ben Kwa41b26c12015-03-31 10:11:43 -070082import com.android.documentsui.CopyService;
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;
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
Ben Kwaf5858932015-04-07 15:43:39 -0700103 private List<DocumentInfo> mSelectedDocumentsForCopy;
104
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700105 public static final int TYPE_NORMAL = 1;
106 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700107 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700108
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700109 public static final int ANIM_NONE = 1;
110 public static final int ANIM_SIDE = 2;
111 public static final int ANIM_DOWN = 3;
112 public static final int ANIM_UP = 4;
113
Ben Kwaf5858932015-04-07 15:43:39 -0700114 public static final int REQUEST_COPY_DESTINATION = 1;
115
Jeff Sharkey5b535922013-08-02 15:55:26 -0700116 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700117 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700118
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700119 private int mLastMode = MODE_UNKNOWN;
120 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700121 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700122
Jeff Sharkey9656a532013-09-13 13:42:19 -0700123 private boolean mHideGridTitles = false;
124
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700125 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700126 private Point mThumbSize;
127
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700129 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700130
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700132 private static final String EXTRA_ROOT = "root";
133 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700134 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700135 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700136
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700137 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700138
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700139 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
140 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700141 }
142
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700143 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
144 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700145 }
146
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700147 public static void showRecentsOpen(FragmentManager fm, int anim) {
148 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700149 }
150
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700151 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
152 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700153 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700154 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700155 args.putParcelable(EXTRA_ROOT, root);
156 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700157 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700158
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700159 final FragmentTransaction ft = fm.beginTransaction();
160 switch (anim) {
161 case ANIM_SIDE:
162 args.putBoolean(EXTRA_IGNORE_STATE, true);
163 break;
164 case ANIM_DOWN:
165 args.putBoolean(EXTRA_IGNORE_STATE, true);
166 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
167 break;
168 case ANIM_UP:
169 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
170 break;
171 }
172
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700173 final DirectoryFragment fragment = new DirectoryFragment();
174 fragment.setArguments(args);
175
Jeff Sharkey76112212013-08-06 11:26:10 -0700176 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177 ft.commitAllowingStateLoss();
178 }
179
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700180 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
181 final StringBuilder builder = new StringBuilder();
182 builder.append(root != null ? root.authority : "null").append(';');
183 builder.append(root != null ? root.rootId : "null").append(';');
184 builder.append(doc != null ? doc.documentId : "null");
185 return builder.toString();
186 }
187
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700188 public static DirectoryFragment get(FragmentManager fm) {
189 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700190 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700191 }
192
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700193 @Override
194 public View onCreateView(
195 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
196 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700197 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700198 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
199
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700200 mEmptyView = view.findViewById(android.R.id.empty);
201
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700202 mListView = (ListView) view.findViewById(R.id.list);
203 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700204 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700205 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700206
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700207 // Indent our list divider to align with text
208 final Drawable divider = mListView.getDivider();
209 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
210 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
211 if (insetLeft) {
212 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
213 } else {
214 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
215 }
216
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700217 mGridView = (GridView) view.findViewById(R.id.grid);
218 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700219 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700220 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700221
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700222 return view;
223 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700224
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700225 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700226 public void onDestroyView() {
227 super.onDestroyView();
228
229 // Cancel any outstanding thumbnail requests
230 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
231 final int count = target.getChildCount();
232 for (int i = 0; i < count; i++) {
233 final View view = target.getChildAt(i);
234 mRecycleListener.onMovedToScrapHeap(view);
235 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700236
237 // Tear down any selection in progress
238 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
239 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700240 }
241
242 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700243 public void onActivityCreated(Bundle savedInstanceState) {
244 super.onActivityCreated(savedInstanceState);
245
246 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700247 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700248
Jeff Sharkey9656a532013-09-13 13:42:19 -0700249 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
250 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
251
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700252 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700253 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700254 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700255
Jeff Sharkey9656a532013-09-13 13:42:19 -0700256 if (mType == TYPE_RECENT_OPEN) {
257 // Hide titles when showing recents for picking images/videos
258 mHideGridTitles = MimePredicate.mimeMatches(
259 MimePredicate.VISUAL_MIMES, state.acceptMimes);
260 } else {
261 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
262 }
263
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700264 final ActivityManager am = (ActivityManager) context.getSystemService(
265 Context.ACTIVITY_SERVICE);
266 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
267
Jeff Sharkey46899c82013-08-18 22:26:48 -0700268 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700269 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700270 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700271 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700272
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700273 Uri contentsUri;
274 switch (mType) {
275 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700276 contentsUri = DocumentsContract.buildChildDocumentsUri(
277 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700278 if (state.action == ACTION_MANAGE) {
279 contentsUri = DocumentsContract.setManageMode(contentsUri);
280 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700281 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700282 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700283 case TYPE_SEARCH:
284 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700285 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700286 if (state.action == ACTION_MANAGE) {
287 contentsUri = DocumentsContract.setManageMode(contentsUri);
288 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700289 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700290 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700291 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700292 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700293 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700294 default:
295 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700296 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700297 }
298
299 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700300 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700301 if (!isAdded()) return;
302
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700303 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700304
305 // Push latest state up to UI
306 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700307 if (result.mode != MODE_UNKNOWN) {
308 state.derivedMode = result.mode;
309 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700310 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700311 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700312
313 updateDisplayState();
314
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700315 // When launched into empty recents, show drawer
316 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700317 ((BaseActivity) 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) {
346 final Context context = getActivity();
347 final Resources res = context.getResources();
348
349 // There's only one request code right now. Replace this with a switch statement or
350 // something more scalable when more codes are added.
351 if (requestCode != REQUEST_COPY_DESTINATION) {
352 return;
353 }
354 if (resultCode == Activity.RESULT_CANCELED || data == null) {
355 // User pressed the back button or otherwise cancelled the destination pick. Don't
356 // proceed with the copy.
357 return;
358 }
359
360 Uri destination = data.getData();
361
362 List<DocumentInfo> docs = mSelectedDocumentsForCopy;
363 Intent copyIntent = new Intent(context, CopyService.class);
364 copyIntent.putParcelableArrayListExtra(CopyService.EXTRA_SRC_LIST,
365 new ArrayList<DocumentInfo>(docs));
366 copyIntent.setData(destination);
367
368 Toast.makeText(context,
369 res.getQuantityString(R.plurals.copy_begin, docs.size(), docs.size()),
370 Toast.LENGTH_SHORT).show();
371 context.startService(copyIntent);
372 }
373
374 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700375 public void onStop() {
376 super.onStop();
377
378 // Remember last scroll location
379 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
380 getView().saveHierarchyState(container);
381 final State state = getDisplayState(this);
382 state.dirState.put(mStateKey, container);
383 }
384
385 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700386 public void onResume() {
387 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700388 updateDisplayState();
389 }
390
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700391 public void onDisplayStateChanged() {
392 updateDisplayState();
393 }
394
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700395 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700396 // Sort order change always triggers reload; we'll trigger state change
397 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700398 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
399 }
400
401 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700402 final ContentResolver resolver = getActivity().getContentResolver();
403 final State state = getDisplayState(this);
404
405 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
406 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
407
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700408 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700409 final Uri stateUri = RecentsProvider.buildState(
410 root.authority, root.rootId, doc.documentId);
411 final ContentValues values = new ContentValues();
412 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700413
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700414 new AsyncTask<Void, Void, Void>() {
415 @Override
416 protected Void doInBackground(Void... params) {
417 resolver.insert(stateUri, values);
418 return null;
419 }
420 }.execute();
421 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700422
423 // Mode change is just visual change; no need to kick loader, and
424 // deliver change event immediately.
425 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700426 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700427
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700428 updateDisplayState();
429 }
430
431 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700432 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700433
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700434 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700435 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700436 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700437
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700438 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
439 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700440
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700441 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700442 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700443 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
444 } else {
445 choiceMode = ListView.CHOICE_MODE_NONE;
446 }
447
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700448 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700449 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700450 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700451 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700452 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700453 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700454 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700455 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700456 mGridView.setChoiceMode(choiceMode);
457 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700458 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700459 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700460 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700461 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700462 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700463 mListView.setChoiceMode(choiceMode);
464 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700465 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700466 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700467 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700468
469 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700470 }
471
472 private OnItemClickListener mItemListener = new OnItemClickListener() {
473 @Override
474 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700475 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700476 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700477 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
478 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
479 if (isDocumentEnabled(docMimeType, docFlags)) {
480 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700481 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700482 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700483 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700484 }
485 };
486
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700487 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
488 @Override
489 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
490 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100491 mode.setTitle(getResources()
492 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700493 return true;
494 }
495
496 @Override
497 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700498 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499
500 final MenuItem open = menu.findItem(R.id.menu_open);
501 final MenuItem share = menu.findItem(R.id.menu_share);
502 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700503 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700504
505 final boolean manageMode = state.action == ACTION_MANAGE;
506 open.setVisible(!manageMode);
507 share.setVisible(manageMode);
508 delete.setVisible(manageMode);
Ben Kwa41b26c12015-03-31 10:11:43 -0700509 // Hide the copy feature by default.
510 copy.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_copy", false));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700511
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700512 return true;
513 }
514
515 @Override
516 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700517 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700518 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700519 final int size = checked.size();
520 for (int i = 0; i < size; i++) {
521 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700522 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700523 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700525 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700526 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700527
Jeff Sharkey873daa32013-08-18 17:38:20 -0700528 final int id = item.getItemId();
529 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700530 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700531 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700532 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700533
534 } else if (id == R.id.menu_share) {
535 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700536 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700537 return true;
538
539 } else if (id == R.id.menu_delete) {
540 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700541 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700542 return true;
543
Ben Kwa41b26c12015-03-31 10:11:43 -0700544 } else if (id == R.id.menu_copy) {
545 onCopyDocuments(docs);
546 mode.finish();
547 return true;
548
Ben Kwa512a6ba2015-03-31 08:15:21 -0700549 } else if (id == R.id.menu_select_all) {
550 int count = mCurrentView.getCount();
551 for (int i = 0; i < count; i++) {
552 mCurrentView.setItemChecked(i, true);
553 }
554 updateDisplayState();
555 return true;
556
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700557 } else {
558 return false;
559 }
560 }
561
562 @Override
563 public void onDestroyActionMode(ActionMode mode) {
564 // ignored
565 }
566
567 @Override
568 public void onItemCheckedStateChanged(
569 ActionMode mode, int position, long id, boolean checked) {
570 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700571 // Directories and footer items cannot be checked
572 boolean valid = false;
573
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700574 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700575 if (cursor != null) {
576 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700577 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
578 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
579 valid = isDocumentEnabled(docMimeType, docFlags);
580 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700581 }
582
583 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700584 mCurrentView.setItemChecked(position, false);
585 }
586 }
587
588 mode.setTitle(getResources()
589 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
590 }
591 };
592
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700593 private RecyclerListener mRecycleListener = new RecyclerListener() {
594 @Override
595 public void onMovedToScrapHeap(View view) {
596 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
597 if (iconThumb != null) {
598 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
599 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700600 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700601 iconThumb.setTag(null);
602 }
603 }
604 }
605 };
606
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700607 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700608 Intent intent;
609 if (docs.size() == 1) {
610 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700611
Jeff Sharkey873daa32013-08-18 17:38:20 -0700612 intent = new Intent(Intent.ACTION_SEND);
613 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
614 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700615 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700616 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700617
618 } else if (docs.size() > 1) {
619 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
620 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
621 intent.addCategory(Intent.CATEGORY_DEFAULT);
622
623 final ArrayList<String> mimeTypes = Lists.newArrayList();
624 final ArrayList<Uri> uris = Lists.newArrayList();
625 for (DocumentInfo doc : docs) {
626 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700627 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700628 }
629
630 intent.setType(findCommonMimeType(mimeTypes));
631 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
632
633 } else {
634 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700635 }
636
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700637 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700638 startActivity(intent);
639 }
640
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700641 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700642 final Context context = getActivity();
643 final ContentResolver resolver = context.getContentResolver();
644
645 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700646 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700647 if (!doc.isDeleteSupported()) {
648 Log.w(TAG, "Skipping " + doc);
649 hadTrouble = true;
650 continue;
651 }
652
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700653 ContentProviderClient client = null;
654 try {
655 client = DocumentsApplication.acquireUnstableProviderOrThrow(
656 resolver, doc.derivedUri.getAuthority());
657 DocumentsContract.deleteDocument(client, doc.derivedUri);
658 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700659 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700660 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700661 } finally {
662 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700663 }
664 }
665
666 if (hadTrouble) {
667 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
668 }
669 }
670
Ben Kwa41b26c12015-03-31 10:11:43 -0700671 private void onCopyDocuments(List<DocumentInfo> docs) {
Ben Kwaf5858932015-04-07 15:43:39 -0700672 mSelectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700673
Ben Kwaf5858932015-04-07 15:43:39 -0700674 // Pop up a dialog to pick a destination. This is inadequate but works for now.
675 // TODO: Implement a picker that is to spec.
676 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
677 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700678 }
679
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700680 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700681 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700682 }
683
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700684 private static abstract class Footer {
685 private final int mItemViewType;
686
687 public Footer(int itemViewType) {
688 mItemViewType = itemViewType;
689 }
690
691 public abstract View getView(View convertView, ViewGroup parent);
692
693 public int getItemViewType() {
694 return mItemViewType;
695 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700696 }
697
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700698 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700699 public LoadingFooter() {
700 super(1);
701 }
702
Jeff Sharkey20b32272013-09-03 15:25:52 -0700703 @Override
704 public View getView(View convertView, ViewGroup parent) {
705 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700706 final State state = getDisplayState(DirectoryFragment.this);
707
Jeff Sharkey20b32272013-09-03 15:25:52 -0700708 if (convertView == null) {
709 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700710 if (state.derivedMode == MODE_LIST) {
711 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
712 } else if (state.derivedMode == MODE_GRID) {
713 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
714 } else {
715 throw new IllegalStateException();
716 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700717 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700718
Jeff Sharkey20b32272013-09-03 15:25:52 -0700719 return convertView;
720 }
721 }
722
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700723 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700724 private final int mIcon;
725 private final String mMessage;
726
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700727 public MessageFooter(int itemViewType, int icon, String message) {
728 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700729 mIcon = icon;
730 mMessage = message;
731 }
732
733 @Override
734 public View getView(View convertView, ViewGroup parent) {
735 final Context context = parent.getContext();
736 final State state = getDisplayState(DirectoryFragment.this);
737
738 if (convertView == null) {
739 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700740 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700741 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700742 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700743 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
744 } else {
745 throw new IllegalStateException();
746 }
747 }
748
749 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
750 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
751 icon.setImageResource(mIcon);
752 title.setText(mMessage);
753 return convertView;
754 }
755 }
756
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700757 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700758 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700759 private int mCursorCount;
760
761 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700762
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700763 public void swapResult(DirectoryResult result) {
764 mCursor = result != null ? result.cursor : null;
765 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700766
767 mFooters.clear();
768
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700769 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700770 if (extras != null) {
771 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
772 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700773 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700774 }
775 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
776 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700777 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700778 }
779 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
780 mFooters.add(new LoadingFooter());
781 }
782 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700783
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700784 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700785 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700786 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700787 }
788
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700789 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700790 mEmptyView.setVisibility(View.VISIBLE);
791 } else {
792 mEmptyView.setVisibility(View.GONE);
793 }
794
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700795 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700796 }
797
798 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700799 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700800 if (position < mCursorCount) {
801 return getDocumentView(position, convertView, parent);
802 } else {
803 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700804 convertView = mFooters.get(position).getView(convertView, parent);
805 // Only the view itself is disabled; contents inside shouldn't
806 // be dimmed.
807 convertView.setEnabled(false);
808 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700809 }
810 }
811
812 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700813 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700814 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700815
Jeff Sharkey9656a532013-09-13 13:42:19 -0700816 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
817
Jeff Sharkey873daa32013-08-18 17:38:20 -0700818 final RootsCache roots = DocumentsApplication.getRootsCache(context);
819 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
820 context, mThumbSize);
821
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700822 if (convertView == null) {
823 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700824 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700825 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700826 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700827 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
828 } else {
829 throw new IllegalStateException();
830 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700831 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700832
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700833 final Cursor cursor = getItem(position);
834
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700835 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
836 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700837 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
838 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
839 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
840 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
841 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
842 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
843 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
844 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700845
Jeff Sharkey9656a532013-09-13 13:42:19 -0700846 final View line1 = convertView.findViewById(R.id.line1);
847 final View line2 = convertView.findViewById(R.id.line2);
848
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700849 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
850 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700851 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
852 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700853 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700854 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
855 final TextView date = (TextView) convertView.findViewById(R.id.date);
856 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700857
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700858 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700859 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700860 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700861 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700862 }
863
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700864 iconMime.animate().cancel();
865 iconThumb.animate().cancel();
866
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700867 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700868 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700869 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700870 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700871
Jeff Sharkey7e544612014-08-29 15:38:27 -0700872 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
873 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
874
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700875 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700876 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700877 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700878 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700879 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700880 iconThumb.setImageBitmap(cachedResult);
881 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700882 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700883 iconThumb.setImageDrawable(null);
884 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700885 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700886 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700887 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700888 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700889 }
890
891 // Always throw MIME icon into place, even when a thumbnail is being
892 // loaded in background.
893 if (cacheHit) {
894 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700895 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700896 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700897 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700898 iconMime.setAlpha(1f);
899 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700900 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700901 if (docIcon != 0) {
902 iconMime.setImageDrawable(
903 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
904 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700905 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
906 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700907 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700908 }
909
Jeff Sharkey9656a532013-09-13 13:42:19 -0700910 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700911 boolean hasLine2 = false;
912
Jeff Sharkey9656a532013-09-13 13:42:19 -0700913 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
914 if (!hideTitle) {
915 title.setText(docDisplayName);
916 hasLine1 = true;
917 }
918
919 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700920 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700921 // We've already had to enumerate roots before any results can
922 // be shown, so this will never block.
923 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700924 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700925 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700926 } else {
927 iconDrawable = root.loadIcon(context);
928 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700929
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700930 if (summary != null) {
931 final boolean alwaysShowSummary = getResources()
932 .getBoolean(R.bool.always_show_summary);
933 if (alwaysShowSummary) {
934 summary.setText(root.getDirectoryString());
935 summary.setVisibility(View.VISIBLE);
936 hasLine2 = true;
937 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700938 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700939 // No summary needed if icon speaks for itself
940 summary.setVisibility(View.INVISIBLE);
941 } else {
942 summary.setText(root.getDirectoryString());
943 summary.setVisibility(View.VISIBLE);
944 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
945 hasLine2 = true;
946 }
947 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700948 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700949 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700950 // Directories showing thumbnails in grid mode get a little icon
951 // hint to remind user they're a directory.
952 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
953 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700954 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700955 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700956 }
957
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700958 if (summary != null) {
959 if (docSummary != null) {
960 summary.setText(docSummary);
961 summary.setVisibility(View.VISIBLE);
962 hasLine2 = true;
963 } else {
964 summary.setVisibility(View.INVISIBLE);
965 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700966 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700967 }
968
Jeff Sharkey9656a532013-09-13 13:42:19 -0700969 if (icon1 != null) icon1.setVisibility(View.GONE);
970 if (icon2 != null) icon2.setVisibility(View.GONE);
971
972 if (iconDrawable != null) {
973 if (hasLine1) {
974 icon1.setVisibility(View.VISIBLE);
975 icon1.setImageDrawable(iconDrawable);
976 } else {
977 icon2.setVisibility(View.VISIBLE);
978 icon2.setImageDrawable(iconDrawable);
979 }
980 }
981
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700982 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700983 date.setText(null);
984 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700985 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700986 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700987 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700988
989 if (state.showSize) {
990 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700991 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700992 size.setText(null);
993 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700994 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700995 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700996 }
997 } else {
998 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700999 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001000
Jeff Sharkey9656a532013-09-13 13:42:19 -07001001 if (line1 != null) {
1002 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1003 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001004 if (line2 != null) {
1005 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1006 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001007
Jeff Sharkey7e544612014-08-29 15:38:27 -07001008 setEnabledRecursive(convertView, enabled);
1009
1010 iconMime.setAlpha(iconAlpha);
1011 iconThumb.setAlpha(iconAlpha);
1012 if (icon1 != null) icon1.setAlpha(iconAlpha);
1013 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001014
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001015 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001016 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001017
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001018 @Override
1019 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001020 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001021 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001022
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001023 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001024 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001025 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001026 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001027 return mCursor;
1028 } else {
1029 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001030 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001031 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001032
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001033 @Override
1034 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001035 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001036 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001037
1038 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001039 public int getViewTypeCount() {
1040 return 4;
1041 }
1042
1043 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001044 public int getItemViewType(int position) {
1045 if (position < mCursorCount) {
1046 return 0;
1047 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001048 position -= mCursorCount;
1049 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001050 }
1051 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001052 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001053
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001054 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1055 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001056 private final Uri mUri;
1057 private final ImageView mIconMime;
1058 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001059 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001060 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001061 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001062
Jeff Sharkey7e544612014-08-29 15:38:27 -07001063 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1064 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001065 mUri = uri;
1066 mIconMime = iconMime;
1067 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001068 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001069 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001071 }
1072
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001073 @Override
1074 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001075 cancel(false);
1076 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001077 }
1078
1079 @Override
1080 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001081 if (isCancelled()) return null;
1082
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001083 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001084 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001085
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001086 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001087 Bitmap result = null;
1088 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001089 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1090 resolver, mUri.getAuthority());
1091 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001092 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001093 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1094 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001095 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001096 }
1097 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001098 if (!(e instanceof OperationCanceledException)) {
1099 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1100 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001101 } finally {
1102 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001103 }
1104 return result;
1105 }
1106
1107 @Override
1108 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001109 if (mIconThumb.getTag() == this && result != null) {
1110 mIconThumb.setTag(null);
1111 mIconThumb.setImageBitmap(result);
1112
Jeff Sharkey7e544612014-08-29 15:38:27 -07001113 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001114 mIconMime.animate().alpha(0f).start();
1115 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001116 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001117 }
1118 }
1119 }
1120
1121 private static String formatTime(Context context, long when) {
1122 // TODO: DateUtils should make this easier
1123 Time then = new Time();
1124 then.set(when);
1125 Time now = new Time();
1126 now.setToNow();
1127
1128 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1129 | DateUtils.FORMAT_ABBREV_ALL;
1130
1131 if (then.year != now.year) {
1132 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1133 } else if (then.yearDay != now.yearDay) {
1134 flags |= DateUtils.FORMAT_SHOW_DATE;
1135 } else {
1136 flags |= DateUtils.FORMAT_SHOW_TIME;
1137 }
1138
1139 return DateUtils.formatDateTime(context, when, flags);
1140 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001141
1142 private String findCommonMimeType(List<String> mimeTypes) {
1143 String[] commonType = mimeTypes.get(0).split("/");
1144 if (commonType.length != 2) {
1145 return "*/*";
1146 }
1147
1148 for (int i = 1; i < mimeTypes.size(); i++) {
1149 String[] type = mimeTypes.get(i).split("/");
1150 if (type.length != 2) continue;
1151
1152 if (!commonType[1].equals(type[1])) {
1153 commonType[1] = "*";
1154 }
1155
1156 if (!commonType[0].equals(type[0])) {
1157 commonType[0] = "*";
1158 commonType[1] = "*";
1159 break;
1160 }
1161 }
1162
1163 return commonType[0] + "/" + commonType[1];
1164 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001165
1166 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001167 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001168 if (v.isEnabled() == enabled) return;
1169 v.setEnabled(enabled);
1170
1171 if (v instanceof ViewGroup) {
1172 final ViewGroup vg = (ViewGroup) v;
1173 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1174 setEnabledRecursive(vg.getChildAt(i), enabled);
1175 }
1176 }
1177 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001178
1179 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1180 final State state = getDisplayState(DirectoryFragment.this);
1181
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001182 // Directories are always enabled
1183 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1184 return true;
1185 }
1186
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001187 // Read-only files are disabled when creating
1188 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1189 return false;
1190 }
1191
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001192 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1193 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001194}