blob: 006f6e913b93ebf55649afddec92c41519c731fe [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;
Makoto Onuki77778752015-07-01 14:55:14 -070053import android.os.Handler;
54import android.os.Looper;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070055import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070056import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070057import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070058import android.provider.DocumentsContract.Document;
Jeff Sharkey6d579272015-06-11 09:16:19 -070059import android.text.TextUtils;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070060import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070061import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070062import android.text.format.Time;
63import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070064import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070065import android.util.SparseBooleanArray;
66import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070067import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070068import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070069import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070070import android.view.View;
71import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070072import android.widget.AbsListView;
73import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070074import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070075import android.widget.AdapterView;
76import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070077import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070078import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070079import android.widget.ImageView;
80import android.widget.ListView;
81import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070082import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070083
Steve McKayd0a2a2c2015-03-25 14:35:33 -070084import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070085import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070086import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070087import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090088import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070089import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070090import com.google.android.collect.Lists;
91
92import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070093import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070094
95/**
96 * Display the documents inside a single directory.
97 */
98public class DirectoryFragment extends Fragment {
99
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700100 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700101 private ListView mListView;
102 private GridView mGridView;
103
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700104 private AbsListView mCurrentView;
105
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700106 public static final int TYPE_NORMAL = 1;
107 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700108 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700109
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700110 public static final int ANIM_NONE = 1;
111 public static final int ANIM_SIDE = 2;
112 public static final int ANIM_DOWN = 3;
113 public static final int ANIM_UP = 4;
114
Ben Kwaf5858932015-04-07 15:43:39 -0700115 public static final int REQUEST_COPY_DESTINATION = 1;
116
Jeff Sharkey5b535922013-08-02 15:55:26 -0700117 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700118 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700119
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700120 private int mLastMode = MODE_UNKNOWN;
121 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700122 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700123
Jeff Sharkey9656a532013-09-13 13:42:19 -0700124 private boolean mHideGridTitles = false;
125
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700126 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700127 private Point mThumbSize;
128
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700129 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700130 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700131
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700132 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700133 private static final String EXTRA_ROOT = "root";
134 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700135 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700136 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700137
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700138 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700139
Makoto Onuki77778752015-07-01 14:55:14 -0700140 private final Handler mHandler = new Handler(Looper.getMainLooper());
141
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700142 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
143 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700144 }
145
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700146 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
147 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700148 }
149
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700150 public static void showRecentsOpen(FragmentManager fm, int anim) {
151 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700152 }
153
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700154 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
155 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700156 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700157 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700158 args.putParcelable(EXTRA_ROOT, root);
159 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700160 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700161
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700162 final FragmentTransaction ft = fm.beginTransaction();
163 switch (anim) {
164 case ANIM_SIDE:
165 args.putBoolean(EXTRA_IGNORE_STATE, true);
166 break;
167 case ANIM_DOWN:
168 args.putBoolean(EXTRA_IGNORE_STATE, true);
169 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
170 break;
171 case ANIM_UP:
172 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
173 break;
174 }
175
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700176 final DirectoryFragment fragment = new DirectoryFragment();
177 fragment.setArguments(args);
178
Jeff Sharkey76112212013-08-06 11:26:10 -0700179 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700180 ft.commitAllowingStateLoss();
181 }
182
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700183 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
184 final StringBuilder builder = new StringBuilder();
185 builder.append(root != null ? root.authority : "null").append(';');
186 builder.append(root != null ? root.rootId : "null").append(';');
187 builder.append(doc != null ? doc.documentId : "null");
188 return builder.toString();
189 }
190
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700191 public static DirectoryFragment get(FragmentManager fm) {
192 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700193 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700194 }
195
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700196 @Override
197 public View onCreateView(
198 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
199 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700200 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700201 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
202
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700203 mEmptyView = view.findViewById(android.R.id.empty);
204
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700205 mListView = (ListView) view.findViewById(R.id.list);
206 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700207 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700208 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700209
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700210 // Indent our list divider to align with text
211 final Drawable divider = mListView.getDivider();
212 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
213 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
214 if (insetLeft) {
215 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
216 } else {
217 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
218 }
219
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700220 mGridView = (GridView) view.findViewById(R.id.grid);
221 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700222 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700223 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700224
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700225 return view;
226 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700227
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700228 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700229 public void onDestroyView() {
230 super.onDestroyView();
231
232 // Cancel any outstanding thumbnail requests
233 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
234 final int count = target.getChildCount();
235 for (int i = 0; i < count; i++) {
236 final View view = target.getChildAt(i);
237 mRecycleListener.onMovedToScrapHeap(view);
238 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700239
240 // Tear down any selection in progress
241 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
242 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700243 }
244
245 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700246 public void onActivityCreated(Bundle savedInstanceState) {
247 super.onActivityCreated(savedInstanceState);
248
249 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700250 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700251
Jeff Sharkey9656a532013-09-13 13:42:19 -0700252 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
253 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
254
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700255 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700256 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700257 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700258
Jeff Sharkey9656a532013-09-13 13:42:19 -0700259 if (mType == TYPE_RECENT_OPEN) {
260 // Hide titles when showing recents for picking images/videos
261 mHideGridTitles = MimePredicate.mimeMatches(
262 MimePredicate.VISUAL_MIMES, state.acceptMimes);
263 } else {
264 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
265 }
266
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700267 final ActivityManager am = (ActivityManager) context.getSystemService(
268 Context.ACTIVITY_SERVICE);
269 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
270
Jeff Sharkey46899c82013-08-18 22:26:48 -0700271 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700272 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700273 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700274 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700275
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700276 Uri contentsUri;
277 switch (mType) {
278 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700279 contentsUri = DocumentsContract.buildChildDocumentsUri(
280 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700281 if (state.action == ACTION_MANAGE) {
282 contentsUri = DocumentsContract.setManageMode(contentsUri);
283 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700284 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700285 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700286 case TYPE_SEARCH:
287 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700288 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700289 if (state.action == ACTION_MANAGE) {
290 contentsUri = DocumentsContract.setManageMode(contentsUri);
291 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700292 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700293 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700294 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700295 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700296 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700297 default:
298 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700299 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700300 }
301
302 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700303 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Makoto Onuki77778752015-07-01 14:55:14 -0700304 if (result == null || result.exception != null) {
305 // onBackPressed does a fragment transaction, which can't be done inside
306 // onLoadFinished
307 mHandler.post(new Runnable() {
308 @Override
309 public void run() {
310 final Activity activity = getActivity();
311 if (activity != null) {
312 activity.onBackPressed();
313 }
314 }
315 });
316 return;
317 }
318
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700319 if (!isAdded()) return;
320
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700321 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700322
323 // Push latest state up to UI
324 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700325 if (result.mode != MODE_UNKNOWN) {
326 state.derivedMode = result.mode;
327 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700328 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700329 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700330
331 updateDisplayState();
332
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700333 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700334 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
335 context instanceof DocumentsActivity) {
336 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700337 }
338
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700339 // Restore any previous instance state
340 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
341 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
342 getView().restoreHierarchyState(container);
343 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700344 mListView.smoothScrollToPosition(0);
345 mGridView.smoothScrollToPosition(0);
346 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700347
348 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700349 }
350
351 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700352 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700353 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700354 }
355 };
356
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700357 // Kick off loader at least once
358 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
359
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700360 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700361 }
362
Jeff Sharkey42d26792013-09-06 13:22:09 -0700363 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700364 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700365 // There's only one request code right now. Replace this with a switch statement or
366 // something more scalable when more codes are added.
367 if (requestCode != REQUEST_COPY_DESTINATION) {
368 return;
369 }
370 if (resultCode == Activity.RESULT_CANCELED || data == null) {
371 // User pressed the back button or otherwise cancelled the destination pick. Don't
372 // proceed with the copy.
373 return;
374 }
375
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900376 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
377 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK));
Ben Kwaf5858932015-04-07 15:43:39 -0700378 }
379
380 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700381 public void onStop() {
382 super.onStop();
383
384 // Remember last scroll location
385 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
386 getView().saveHierarchyState(container);
387 final State state = getDisplayState(this);
388 state.dirState.put(mStateKey, container);
389 }
390
391 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700392 public void onResume() {
393 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700394 updateDisplayState();
395 }
396
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700397 public void onDisplayStateChanged() {
398 updateDisplayState();
399 }
400
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700401 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700402 // Sort order change always triggers reload; we'll trigger state change
403 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700404 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
405 }
406
407 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700408 final ContentResolver resolver = getActivity().getContentResolver();
409 final State state = getDisplayState(this);
410
411 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
412 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
413
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700414 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700415 final Uri stateUri = RecentsProvider.buildState(
416 root.authority, root.rootId, doc.documentId);
417 final ContentValues values = new ContentValues();
418 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700419
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700420 new AsyncTask<Void, Void, Void>() {
421 @Override
422 protected Void doInBackground(Void... params) {
423 resolver.insert(stateUri, values);
424 return null;
425 }
426 }.execute();
427 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700428
429 // Mode change is just visual change; no need to kick loader, and
430 // deliver change event immediately.
431 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700432 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700433
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700434 updateDisplayState();
435 }
436
437 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700438 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700439
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700440 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700441 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700442 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700443
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700444 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
445 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700446
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700448 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700449 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
450 } else {
451 choiceMode = ListView.CHOICE_MODE_NONE;
452 }
453
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700454 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700455 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700456 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700457 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700458 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700459 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700460 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700461 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700462 mGridView.setChoiceMode(choiceMode);
463 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700464 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700465 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700466 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700467 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700468 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700469 mListView.setChoiceMode(choiceMode);
470 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700471 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700472 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700473 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700474
475 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700476 }
477
478 private OnItemClickListener mItemListener = new OnItemClickListener() {
479 @Override
480 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700481 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700482 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700483 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
484 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
485 if (isDocumentEnabled(docMimeType, docFlags)) {
486 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700487 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700488 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700489 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700490 }
491 };
492
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700493 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
494 @Override
495 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
496 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Jeff Sharkey6d579272015-06-11 09:16:19 -0700497 mode.setTitle(TextUtils.formatSelectedCount(mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700498 return true;
499 }
500
501 @Override
502 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700503 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700504
505 final MenuItem open = menu.findItem(R.id.menu_open);
506 final MenuItem share = menu.findItem(R.id.menu_share);
507 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700508 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700509
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700510 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
511 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
512
513 open.setVisible(!manageOrBrowse);
514 share.setVisible(manageOrBrowse);
515 delete.setVisible(manageOrBrowse);
Ben Kwada1cea52015-04-22 10:33:53 -0700516 // Disable copying from the Recents view.
517 copy.setVisible(manageOrBrowse && mType != TYPE_RECENT_OPEN);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700518
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700519 return true;
520 }
521
522 @Override
523 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700525 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700526 final int size = checked.size();
527 for (int i = 0; i < size; i++) {
528 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700529 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700530 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700531 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700532 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700533 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700534
Jeff Sharkey873daa32013-08-18 17:38:20 -0700535 final int id = item.getItemId();
536 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700537 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700538 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700539 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700540
541 } else if (id == R.id.menu_share) {
542 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700543 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700544 return true;
545
546 } else if (id == R.id.menu_delete) {
547 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700548 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700549 return true;
550
Ben Kwa41b26c12015-03-31 10:11:43 -0700551 } else if (id == R.id.menu_copy) {
552 onCopyDocuments(docs);
553 mode.finish();
554 return true;
555
Ben Kwa512a6ba2015-03-31 08:15:21 -0700556 } else if (id == R.id.menu_select_all) {
557 int count = mCurrentView.getCount();
558 for (int i = 0; i < count; i++) {
559 mCurrentView.setItemChecked(i, true);
560 }
561 updateDisplayState();
562 return true;
563
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700564 } else {
565 return false;
566 }
567 }
568
569 @Override
570 public void onDestroyActionMode(ActionMode mode) {
571 // ignored
572 }
573
574 @Override
575 public void onItemCheckedStateChanged(
576 ActionMode mode, int position, long id, boolean checked) {
577 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700578 // Directories and footer items cannot be checked
579 boolean valid = false;
580
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700581 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700582 if (cursor != null) {
583 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700584 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700585 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700586 }
587
588 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700589 mCurrentView.setItemChecked(position, false);
590 }
591 }
592
Jeff Sharkey6d579272015-06-11 09:16:19 -0700593 mode.setTitle(TextUtils.formatSelectedCount(mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700594 }
595 };
596
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700597 private RecyclerListener mRecycleListener = new RecyclerListener() {
598 @Override
599 public void onMovedToScrapHeap(View view) {
600 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
601 if (iconThumb != null) {
602 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
603 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700604 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700605 iconThumb.setTag(null);
606 }
607 }
608 }
609 };
610
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700611 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700612 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700613
614 // Filter out directories - those can't be shared.
615 List<DocumentInfo> docsForSend = Lists.newArrayList();
616 for (DocumentInfo doc: docs) {
617 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
618 docsForSend.add(doc);
619 }
620 }
621
622 if (docsForSend.size() == 1) {
623 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700624
Jeff Sharkey873daa32013-08-18 17:38:20 -0700625 intent = new Intent(Intent.ACTION_SEND);
626 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
627 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700628 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700629 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700630
Ben Kwaf527c632015-04-08 15:03:35 -0700631 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700632 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
633 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
634 intent.addCategory(Intent.CATEGORY_DEFAULT);
635
636 final ArrayList<String> mimeTypes = Lists.newArrayList();
637 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700638 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700639 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700640 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700641 }
642
643 intent.setType(findCommonMimeType(mimeTypes));
644 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
645
646 } else {
647 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700648 }
649
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700650 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700651 startActivity(intent);
652 }
653
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700654 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700655 final Context context = getActivity();
656 final ContentResolver resolver = context.getContentResolver();
657
658 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700659 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700660 if (!doc.isDeleteSupported()) {
661 Log.w(TAG, "Skipping " + doc);
662 hadTrouble = true;
663 continue;
664 }
665
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700666 ContentProviderClient client = null;
667 try {
668 client = DocumentsApplication.acquireUnstableProviderOrThrow(
669 resolver, doc.derivedUri.getAuthority());
670 DocumentsContract.deleteDocument(client, doc.derivedUri);
671 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700672 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700673 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700674 } finally {
675 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700676 }
677 }
678
679 if (hadTrouble) {
680 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
681 }
682 }
683
Ben Kwa41b26c12015-03-31 10:11:43 -0700684 private void onCopyDocuments(List<DocumentInfo> docs) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900685 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700686
Ben Kwaf5858932015-04-07 15:43:39 -0700687 // Pop up a dialog to pick a destination. This is inadequate but works for now.
688 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900689 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900690 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900691 Uri.EMPTY,
692 getActivity(),
693 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900694 boolean directoryCopy = false;
695 for (DocumentInfo info : docs) {
696 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
697 directoryCopy = true;
698 break;
699 }
700 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900701 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwaf5858932015-04-07 15:43:39 -0700702 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700703 }
704
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700705 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700706 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700707 }
708
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700709 private static abstract class Footer {
710 private final int mItemViewType;
711
712 public Footer(int itemViewType) {
713 mItemViewType = itemViewType;
714 }
715
716 public abstract View getView(View convertView, ViewGroup parent);
717
718 public int getItemViewType() {
719 return mItemViewType;
720 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700721 }
722
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700723 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700724 public LoadingFooter() {
725 super(1);
726 }
727
Jeff Sharkey20b32272013-09-03 15:25:52 -0700728 @Override
729 public View getView(View convertView, ViewGroup parent) {
730 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700731 final State state = getDisplayState(DirectoryFragment.this);
732
Jeff Sharkey20b32272013-09-03 15:25:52 -0700733 if (convertView == null) {
734 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700735 if (state.derivedMode == MODE_LIST) {
736 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
737 } else if (state.derivedMode == MODE_GRID) {
738 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
739 } else {
740 throw new IllegalStateException();
741 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700742 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700743
Jeff Sharkey20b32272013-09-03 15:25:52 -0700744 return convertView;
745 }
746 }
747
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700748 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700749 private final int mIcon;
750 private final String mMessage;
751
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700752 public MessageFooter(int itemViewType, int icon, String message) {
753 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700754 mIcon = icon;
755 mMessage = message;
756 }
757
758 @Override
759 public View getView(View convertView, ViewGroup parent) {
760 final Context context = parent.getContext();
761 final State state = getDisplayState(DirectoryFragment.this);
762
763 if (convertView == null) {
764 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700765 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700766 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700767 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700768 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
769 } else {
770 throw new IllegalStateException();
771 }
772 }
773
774 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
775 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
776 icon.setImageResource(mIcon);
777 title.setText(mMessage);
778 return convertView;
779 }
780 }
781
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700782 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700783 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700784 private int mCursorCount;
785
786 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700787
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700788 public void swapResult(DirectoryResult result) {
789 mCursor = result != null ? result.cursor : null;
790 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700791
792 mFooters.clear();
793
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700794 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700795 if (extras != null) {
796 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
797 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700798 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700799 }
800 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
801 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700802 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700803 }
804 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
805 mFooters.add(new LoadingFooter());
806 }
807 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700808
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700809 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700810 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700811 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700812 }
813
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700814 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700815 mEmptyView.setVisibility(View.VISIBLE);
816 } else {
817 mEmptyView.setVisibility(View.GONE);
818 }
819
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700820 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700821 }
822
823 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700824 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700825 if (position < mCursorCount) {
826 return getDocumentView(position, convertView, parent);
827 } else {
828 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700829 convertView = mFooters.get(position).getView(convertView, parent);
830 // Only the view itself is disabled; contents inside shouldn't
831 // be dimmed.
832 convertView.setEnabled(false);
833 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700834 }
835 }
836
837 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700838 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700839 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700840
Jeff Sharkey9656a532013-09-13 13:42:19 -0700841 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
842
Jeff Sharkey873daa32013-08-18 17:38:20 -0700843 final RootsCache roots = DocumentsApplication.getRootsCache(context);
844 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
845 context, mThumbSize);
846
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700847 if (convertView == null) {
848 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700849 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700850 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700851 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700852 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
853 } else {
854 throw new IllegalStateException();
855 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700856 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700857
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700858 final Cursor cursor = getItem(position);
859
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700860 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
861 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700862 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
863 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
864 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
865 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
866 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
867 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
868 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
869 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700870
Jeff Sharkey9656a532013-09-13 13:42:19 -0700871 final View line1 = convertView.findViewById(R.id.line1);
872 final View line2 = convertView.findViewById(R.id.line2);
873
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700874 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
875 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700876 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
877 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700878 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700879 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
880 final TextView date = (TextView) convertView.findViewById(R.id.date);
881 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700882
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700883 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700884 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700885 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700886 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700887 }
888
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700889 iconMime.animate().cancel();
890 iconThumb.animate().cancel();
891
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700892 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700893 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700894 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700895 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700896
Jeff Sharkey7e544612014-08-29 15:38:27 -0700897 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
898 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
899
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700900 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700901 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700902 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700903 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700904 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700905 iconThumb.setImageBitmap(cachedResult);
906 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700907 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700908 iconThumb.setImageDrawable(null);
909 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700910 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700911 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700912 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700913 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700914 }
915
916 // Always throw MIME icon into place, even when a thumbnail is being
917 // loaded in background.
918 if (cacheHit) {
919 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700920 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700921 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700922 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700923 iconMime.setAlpha(1f);
924 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700925 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700926 if (docIcon != 0) {
927 iconMime.setImageDrawable(
928 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
929 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700930 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
931 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700932 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700933 }
934
Jeff Sharkey9656a532013-09-13 13:42:19 -0700935 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700936 boolean hasLine2 = false;
937
Jeff Sharkey9656a532013-09-13 13:42:19 -0700938 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
939 if (!hideTitle) {
940 title.setText(docDisplayName);
941 hasLine1 = true;
942 }
943
944 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700945 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700946 // We've already had to enumerate roots before any results can
947 // be shown, so this will never block.
948 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700949 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700950 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700951 } else {
952 iconDrawable = root.loadIcon(context);
953 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700954
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700955 if (summary != null) {
956 final boolean alwaysShowSummary = getResources()
957 .getBoolean(R.bool.always_show_summary);
958 if (alwaysShowSummary) {
959 summary.setText(root.getDirectoryString());
960 summary.setVisibility(View.VISIBLE);
961 hasLine2 = true;
962 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700963 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700964 // No summary needed if icon speaks for itself
965 summary.setVisibility(View.INVISIBLE);
966 } else {
967 summary.setText(root.getDirectoryString());
968 summary.setVisibility(View.VISIBLE);
969 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
970 hasLine2 = true;
971 }
972 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700973 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700974 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700975 // Directories showing thumbnails in grid mode get a little icon
976 // hint to remind user they're a directory.
977 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
978 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700979 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700980 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700981 }
982
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700983 if (summary != null) {
984 if (docSummary != null) {
985 summary.setText(docSummary);
986 summary.setVisibility(View.VISIBLE);
987 hasLine2 = true;
988 } else {
989 summary.setVisibility(View.INVISIBLE);
990 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700991 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700992 }
993
Jeff Sharkey9656a532013-09-13 13:42:19 -0700994 if (icon1 != null) icon1.setVisibility(View.GONE);
995 if (icon2 != null) icon2.setVisibility(View.GONE);
996
997 if (iconDrawable != null) {
998 if (hasLine1) {
999 icon1.setVisibility(View.VISIBLE);
1000 icon1.setImageDrawable(iconDrawable);
1001 } else {
1002 icon2.setVisibility(View.VISIBLE);
1003 icon2.setImageDrawable(iconDrawable);
1004 }
1005 }
1006
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001007 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001008 date.setText(null);
1009 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001010 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001011 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001012 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001013
1014 if (state.showSize) {
1015 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001016 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001017 size.setText(null);
1018 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001019 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001020 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001021 }
1022 } else {
1023 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001024 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001025
Jeff Sharkey9656a532013-09-13 13:42:19 -07001026 if (line1 != null) {
1027 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1028 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001029 if (line2 != null) {
1030 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1031 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001032
Jeff Sharkey7e544612014-08-29 15:38:27 -07001033 setEnabledRecursive(convertView, enabled);
1034
1035 iconMime.setAlpha(iconAlpha);
1036 iconThumb.setAlpha(iconAlpha);
1037 if (icon1 != null) icon1.setAlpha(iconAlpha);
1038 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001039
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001040 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001041 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001042
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001043 @Override
1044 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001045 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001046 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001047
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001048 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001049 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001050 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001051 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001052 return mCursor;
1053 } else {
1054 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001055 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001056 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001057
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001058 @Override
1059 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001060 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001061 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001062
1063 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001064 public int getViewTypeCount() {
1065 return 4;
1066 }
1067
1068 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001069 public int getItemViewType(int position) {
1070 if (position < mCursorCount) {
1071 return 0;
1072 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001073 position -= mCursorCount;
1074 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001075 }
1076 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001077 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001078
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001079 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1080 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001081 private final Uri mUri;
1082 private final ImageView mIconMime;
1083 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001084 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001085 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001086 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001087
Jeff Sharkey7e544612014-08-29 15:38:27 -07001088 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1089 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001090 mUri = uri;
1091 mIconMime = iconMime;
1092 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001093 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001094 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001095 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001096 }
1097
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001098 @Override
1099 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001100 cancel(false);
1101 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001102 }
1103
1104 @Override
1105 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001106 if (isCancelled()) return null;
1107
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001108 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001109 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001110
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001111 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001112 Bitmap result = null;
1113 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001114 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1115 resolver, mUri.getAuthority());
1116 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001117 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001118 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1119 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001120 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001121 }
1122 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001123 if (!(e instanceof OperationCanceledException)) {
1124 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1125 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001126 } finally {
1127 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001128 }
1129 return result;
1130 }
1131
1132 @Override
1133 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001134 if (mIconThumb.getTag() == this && result != null) {
1135 mIconThumb.setTag(null);
1136 mIconThumb.setImageBitmap(result);
1137
Jeff Sharkey7e544612014-08-29 15:38:27 -07001138 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001139 mIconMime.animate().alpha(0f).start();
1140 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001141 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001142 }
1143 }
1144 }
1145
1146 private static String formatTime(Context context, long when) {
1147 // TODO: DateUtils should make this easier
1148 Time then = new Time();
1149 then.set(when);
1150 Time now = new Time();
1151 now.setToNow();
1152
1153 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1154 | DateUtils.FORMAT_ABBREV_ALL;
1155
1156 if (then.year != now.year) {
1157 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1158 } else if (then.yearDay != now.yearDay) {
1159 flags |= DateUtils.FORMAT_SHOW_DATE;
1160 } else {
1161 flags |= DateUtils.FORMAT_SHOW_TIME;
1162 }
1163
1164 return DateUtils.formatDateTime(context, when, flags);
1165 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001166
1167 private String findCommonMimeType(List<String> mimeTypes) {
1168 String[] commonType = mimeTypes.get(0).split("/");
1169 if (commonType.length != 2) {
1170 return "*/*";
1171 }
1172
1173 for (int i = 1; i < mimeTypes.size(); i++) {
1174 String[] type = mimeTypes.get(i).split("/");
1175 if (type.length != 2) continue;
1176
1177 if (!commonType[1].equals(type[1])) {
1178 commonType[1] = "*";
1179 }
1180
1181 if (!commonType[0].equals(type[0])) {
1182 commonType[0] = "*";
1183 commonType[1] = "*";
1184 break;
1185 }
1186 }
1187
1188 return commonType[0] + "/" + commonType[1];
1189 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001190
1191 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001192 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001193 if (v.isEnabled() == enabled) return;
1194 v.setEnabled(enabled);
1195
1196 if (v instanceof ViewGroup) {
1197 final ViewGroup vg = (ViewGroup) v;
1198 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1199 setEnabledRecursive(vg.getChildAt(i), enabled);
1200 }
1201 }
1202 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001203
1204 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1205 final State state = getDisplayState(DirectoryFragment.this);
1206
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001207 // Directories are always enabled
1208 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1209 return true;
1210 }
1211
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001212 // Read-only files are disabled when creating
1213 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1214 return false;
1215 }
1216
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001217 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1218 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001219}