blob: 5b6ec4d87cdb69d99ff63ccde21126c036f523b2 [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;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070020import static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE;
21import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
22import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070023import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
24import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070025import static com.android.documentsui.model.DocumentInfo.getCursorInt;
26import static com.android.documentsui.model.DocumentInfo.getCursorLong;
27import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070028
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070029import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070030import android.app.FragmentManager;
31import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070032import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey873daa32013-08-18 17:38:20 -070033import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070034import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070035import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070036import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070038import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070039import android.graphics.Bitmap;
40import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070041import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070043import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070045import android.os.CancellationSignal;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070046import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070049import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070050import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070051import android.text.format.Time;
52import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070053import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070054import android.util.SparseBooleanArray;
55import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070056import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070057import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070058import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070059import android.view.View;
60import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070061import android.widget.AbsListView;
62import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070063import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070064import android.widget.AdapterView;
65import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070066import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070067import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070068import android.widget.ImageView;
69import android.widget.ListView;
70import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070071import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070072
Jeff Sharkeyb3620442013-09-01 18:41:04 -070073import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070074import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070075import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070076import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070077import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070078import com.google.android.collect.Lists;
79
80import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070081import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070082import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070083
84/**
85 * Display the documents inside a single directory.
86 */
87public class DirectoryFragment extends Fragment {
88
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070089 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070090 private ListView mListView;
91 private GridView mGridView;
92
Jeff Sharkeyc317af82013-07-01 16:56:54 -070093 private AbsListView mCurrentView;
94
Jeff Sharkey724deeb2013-08-31 15:02:20 -070095 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070096
Jeff Sharkeya5defe32013-08-05 17:56:48 -070097 public static final int TYPE_NORMAL = 1;
98 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070099 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700100
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700101 public static final int ANIM_NONE = 1;
102 public static final int ANIM_SIDE = 2;
103 public static final int ANIM_DOWN = 3;
104 public static final int ANIM_UP = 4;
105
Jeff Sharkey5b535922013-08-02 15:55:26 -0700106 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700107 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700108
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700109 private int mLastMode = MODE_UNKNOWN;
110 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700111 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700112
Jeff Sharkey9656a532013-09-13 13:42:19 -0700113 private boolean mHideGridTitles = false;
114
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700115 private Point mThumbSize;
116
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700117 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700118 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700119
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700120 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700121 private static final String EXTRA_ROOT = "root";
122 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700123 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700124 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700125
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700126 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700127
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700128 private final int mLoaderId = sLoaderId.incrementAndGet();
129
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700130 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
131 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700132 }
133
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700134 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
135 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700136 }
137
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700138 public static void showRecentsOpen(FragmentManager fm, int anim) {
139 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140 }
141
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700142 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
143 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700144 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700145 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700146 args.putParcelable(EXTRA_ROOT, root);
147 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700148 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700149
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700150 final FragmentTransaction ft = fm.beginTransaction();
151 switch (anim) {
152 case ANIM_SIDE:
153 args.putBoolean(EXTRA_IGNORE_STATE, true);
154 break;
155 case ANIM_DOWN:
156 args.putBoolean(EXTRA_IGNORE_STATE, true);
157 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
158 break;
159 case ANIM_UP:
160 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
161 break;
162 }
163
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700164 final DirectoryFragment fragment = new DirectoryFragment();
165 fragment.setArguments(args);
166
Jeff Sharkey76112212013-08-06 11:26:10 -0700167 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700168 ft.commitAllowingStateLoss();
169 }
170
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700171 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
172 final StringBuilder builder = new StringBuilder();
173 builder.append(root != null ? root.authority : "null").append(';');
174 builder.append(root != null ? root.rootId : "null").append(';');
175 builder.append(doc != null ? doc.documentId : "null");
176 return builder.toString();
177 }
178
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700179 public static DirectoryFragment get(FragmentManager fm) {
180 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700181 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700182 }
183
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700184 @Override
185 public View onCreateView(
186 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
187 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700188 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
189
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700190 mEmptyView = view.findViewById(android.R.id.empty);
191
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700192 mListView = (ListView) view.findViewById(R.id.list);
193 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700194 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700195 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700196
197 mGridView = (GridView) view.findViewById(R.id.grid);
198 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700199 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700200 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700201
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700202 return view;
203 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700204
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700205 @Override
206 public void onActivityCreated(Bundle savedInstanceState) {
207 super.onActivityCreated(savedInstanceState);
208
209 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700210 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700211
Jeff Sharkey9656a532013-09-13 13:42:19 -0700212 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
213 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
214
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700215 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700216 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700217 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700218
Jeff Sharkey9656a532013-09-13 13:42:19 -0700219 if (mType == TYPE_RECENT_OPEN) {
220 // Hide titles when showing recents for picking images/videos
221 mHideGridTitles = MimePredicate.mimeMatches(
222 MimePredicate.VISUAL_MIMES, state.acceptMimes);
223 } else {
224 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
225 }
226
Jeff Sharkey46899c82013-08-18 22:26:48 -0700227 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700228 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700229 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700230 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700231
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700232 Uri contentsUri;
233 switch (mType) {
234 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700235 contentsUri = DocumentsContract.buildChildDocumentsUri(
236 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700237 if (state.action == ACTION_MANAGE) {
238 contentsUri = DocumentsContract.setManageMode(contentsUri);
239 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700240 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700241 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700242 case TYPE_SEARCH:
243 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700244 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700245 if (state.action == ACTION_MANAGE) {
246 contentsUri = DocumentsContract.setManageMode(contentsUri);
247 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700248 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700249 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700250 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700251 final RootsCache roots = DocumentsApplication.getRootsCache(context);
252 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700253 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700254 default:
255 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700256 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700257 }
258
259 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700260 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700261 if (!isAdded()) return;
262
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700263 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700264
265 // Push latest state up to UI
266 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700267 if (result.mode != MODE_UNKNOWN) {
268 state.derivedMode = result.mode;
269 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700270 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700271 ((DocumentsActivity) context).onStateChanged();
272
273 updateDisplayState();
274
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700275 // Restore any previous instance state
276 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
277 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
278 getView().restoreHierarchyState(container);
279 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700280 mListView.smoothScrollToPosition(0);
281 mGridView.smoothScrollToPosition(0);
282 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700283
284 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700285 }
286
287 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700288 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700289 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700290 }
291 };
292
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700293 // Kick off loader at least once
294 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
295
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700296 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700297 }
298
Jeff Sharkey42d26792013-09-06 13:22:09 -0700299 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700300 public void onStop() {
301 super.onStop();
302
303 // Remember last scroll location
304 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
305 getView().saveHierarchyState(container);
306 final State state = getDisplayState(this);
307 state.dirState.put(mStateKey, container);
308 }
309
310 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700311 public void onResume() {
312 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700313 updateDisplayState();
314 }
315
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700316 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700317 // Sort order change always triggers reload; we'll trigger state change
318 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700319 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
320 }
321
322 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700323 final ContentResolver resolver = getActivity().getContentResolver();
324 final State state = getDisplayState(this);
325
326 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
327 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
328
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700329 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700330 final Uri stateUri = RecentsProvider.buildState(
331 root.authority, root.rootId, doc.documentId);
332 final ContentValues values = new ContentValues();
333 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700334
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700335 new AsyncTask<Void, Void, Void>() {
336 @Override
337 protected Void doInBackground(Void... params) {
338 resolver.insert(stateUri, values);
339 return null;
340 }
341 }.execute();
342 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700343
344 // Mode change is just visual change; no need to kick loader, and
345 // deliver change event immediately.
346 state.derivedMode = state.userMode;
347 ((DocumentsActivity) getActivity()).onStateChanged();
348
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700349 updateDisplayState();
350 }
351
352 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700353 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700354
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700355 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700356
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700357 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700358 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700359 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700360
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700361 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
362 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700363
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700364 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700365 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700366 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
367 } else {
368 choiceMode = ListView.CHOICE_MODE_NONE;
369 }
370
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700371 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700372 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700373 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700374 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700375 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700376 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700377 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700378 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700379 mGridView.setChoiceMode(choiceMode);
380 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700381 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700382 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700383 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700384 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700385 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700386 mListView.setChoiceMode(choiceMode);
387 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700388 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700389 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700390 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700391
392 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700393 }
394
395 private OnItemClickListener mItemListener = new OnItemClickListener() {
396 @Override
397 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700398 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700399 if (cursor != null) {
400 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
401 if (mFilter.apply(doc)) {
402 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
403 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700404 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700405 }
406 };
407
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700408 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
409 @Override
410 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
411 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
412 return true;
413 }
414
415 @Override
416 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700417 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700418
419 final MenuItem open = menu.findItem(R.id.menu_open);
420 final MenuItem share = menu.findItem(R.id.menu_share);
421 final MenuItem delete = menu.findItem(R.id.menu_delete);
422
423 final boolean manageMode = state.action == ACTION_MANAGE;
424 open.setVisible(!manageMode);
425 share.setVisible(manageMode);
426 delete.setVisible(manageMode);
427
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700428 return true;
429 }
430
431 @Override
432 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700433 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700434 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700435 final int size = checked.size();
436 for (int i = 0; i < size; i++) {
437 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700438 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700439 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700440 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700441 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700442 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700443
Jeff Sharkey873daa32013-08-18 17:38:20 -0700444 final int id = item.getItemId();
445 if (id == R.id.menu_open) {
446 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700447 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700448 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700449
450 } else if (id == R.id.menu_share) {
451 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700452 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700453 return true;
454
455 } else if (id == R.id.menu_delete) {
456 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700457 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700458 return true;
459
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700460 } else {
461 return false;
462 }
463 }
464
465 @Override
466 public void onDestroyActionMode(ActionMode mode) {
467 // ignored
468 }
469
470 @Override
471 public void onItemCheckedStateChanged(
472 ActionMode mode, int position, long id, boolean checked) {
473 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700474 // Directories and footer items cannot be checked
475 boolean valid = false;
476
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700477 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700478 if (cursor != null) {
479 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
480
481 // Only valid if non-directory matches filter
482 final State state = getDisplayState(DirectoryFragment.this);
483 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
484 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
485 }
486
487 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700488 mCurrentView.setItemChecked(position, false);
489 }
490 }
491
492 mode.setTitle(getResources()
493 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
494 }
495 };
496
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700497 private RecyclerListener mRecycleListener = new RecyclerListener() {
498 @Override
499 public void onMovedToScrapHeap(View view) {
500 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
501 if (iconThumb != null) {
502 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
503 if (oldTask != null) {
504 oldTask.reallyCancel();
505 iconThumb.setTag(null);
506 }
507 }
508 }
509 };
510
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700511 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700512 Intent intent;
513 if (docs.size() == 1) {
514 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700515
Jeff Sharkey873daa32013-08-18 17:38:20 -0700516 intent = new Intent(Intent.ACTION_SEND);
517 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
518 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700519 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700520 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700521
522 } else if (docs.size() > 1) {
523 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
524 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
525 intent.addCategory(Intent.CATEGORY_DEFAULT);
526
527 final ArrayList<String> mimeTypes = Lists.newArrayList();
528 final ArrayList<Uri> uris = Lists.newArrayList();
529 for (DocumentInfo doc : docs) {
530 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700531 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700532 }
533
534 intent.setType(findCommonMimeType(mimeTypes));
535 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
536
537 } else {
538 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700539 }
540
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700541 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700542 startActivity(intent);
543 }
544
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700545 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700546 final Context context = getActivity();
547 final ContentResolver resolver = context.getContentResolver();
548
549 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700550 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700551 if (!doc.isDeleteSupported()) {
552 Log.w(TAG, "Skipping " + doc);
553 hadTrouble = true;
554 continue;
555 }
556
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700557 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700558 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700559 hadTrouble = true;
560 }
561 }
562
563 if (hadTrouble) {
564 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
565 }
566 }
567
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700568 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700569 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700570 }
571
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700572 private static abstract class Footer {
573 private final int mItemViewType;
574
575 public Footer(int itemViewType) {
576 mItemViewType = itemViewType;
577 }
578
579 public abstract View getView(View convertView, ViewGroup parent);
580
581 public int getItemViewType() {
582 return mItemViewType;
583 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700584 }
585
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700586 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700587 public LoadingFooter() {
588 super(1);
589 }
590
Jeff Sharkey20b32272013-09-03 15:25:52 -0700591 @Override
592 public View getView(View convertView, ViewGroup parent) {
593 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700594 final State state = getDisplayState(DirectoryFragment.this);
595
Jeff Sharkey20b32272013-09-03 15:25:52 -0700596 if (convertView == null) {
597 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700598 if (state.derivedMode == MODE_LIST) {
599 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
600 } else if (state.derivedMode == MODE_GRID) {
601 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
602 } else {
603 throw new IllegalStateException();
604 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700605 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700606
Jeff Sharkey20b32272013-09-03 15:25:52 -0700607 return convertView;
608 }
609 }
610
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700611 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700612 private final int mIcon;
613 private final String mMessage;
614
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700615 public MessageFooter(int itemViewType, int icon, String message) {
616 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700617 mIcon = icon;
618 mMessage = message;
619 }
620
621 @Override
622 public View getView(View convertView, ViewGroup parent) {
623 final Context context = parent.getContext();
624 final State state = getDisplayState(DirectoryFragment.this);
625
626 if (convertView == null) {
627 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700628 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700629 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700630 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700631 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
632 } else {
633 throw new IllegalStateException();
634 }
635 }
636
637 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
638 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
639 icon.setImageResource(mIcon);
640 title.setText(mMessage);
641 return convertView;
642 }
643 }
644
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700645 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700646 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700647 private int mCursorCount;
648
649 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700650
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700651 public void swapCursor(Cursor cursor) {
652 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700653 mCursorCount = cursor != null ? cursor.getCount() : 0;
654
655 mFooters.clear();
656
657 final Bundle extras = cursor != null ? cursor.getExtras() : null;
658 if (extras != null) {
659 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
660 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700661 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700662 }
663 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
664 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700665 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700666 }
667 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
668 mFooters.add(new LoadingFooter());
669 }
670 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700671
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700672 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700673 mEmptyView.setVisibility(View.VISIBLE);
674 } else {
675 mEmptyView.setVisibility(View.GONE);
676 }
677
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700678 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700679 }
680
681 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700682 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700683 if (position < mCursorCount) {
684 return getDocumentView(position, convertView, parent);
685 } else {
686 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700687 convertView = mFooters.get(position).getView(convertView, parent);
688 // Only the view itself is disabled; contents inside shouldn't
689 // be dimmed.
690 convertView.setEnabled(false);
691 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700692 }
693 }
694
695 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700696 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700697 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700698
Jeff Sharkey9656a532013-09-13 13:42:19 -0700699 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
700
Jeff Sharkey873daa32013-08-18 17:38:20 -0700701 final RootsCache roots = DocumentsApplication.getRootsCache(context);
702 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
703 context, mThumbSize);
704
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700705 if (convertView == null) {
706 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700707 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700708 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700709 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700710 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
711 } else {
712 throw new IllegalStateException();
713 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700714 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700715
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700716 final Cursor cursor = getItem(position);
717
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700718 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
719 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700720 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
721 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
722 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
723 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
724 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
725 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
726 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
727 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700728
Jeff Sharkey9656a532013-09-13 13:42:19 -0700729 final View line1 = convertView.findViewById(R.id.line1);
730 final View line2 = convertView.findViewById(R.id.line2);
731
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700732 final View icon = convertView.findViewById(android.R.id.icon);
733 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
734 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700735 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
736 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700737 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700738 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
739 final TextView date = (TextView) convertView.findViewById(R.id.date);
740 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700741
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700742 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700743 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700744 oldTask.reallyCancel();
745 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700746 }
747
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700748 iconMime.animate().cancel();
749 iconThumb.animate().cancel();
750
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700751 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700752 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700753 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
754 final boolean showThumbnail = supportsThumbnail && allowThumbnail;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700755
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700756 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700757 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700758 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700759 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700760 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700761 iconThumb.setImageBitmap(cachedResult);
762 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700763 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700764 iconThumb.setImageDrawable(null);
765 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
766 uri, iconMime, iconThumb, mThumbSize);
767 iconThumb.setTag(task);
768 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700769 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700770 }
771
772 // Always throw MIME icon into place, even when a thumbnail is being
773 // loaded in background.
774 if (cacheHit) {
775 iconMime.setAlpha(0f);
776 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700777 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700778 iconMime.setAlpha(1f);
779 iconThumb.setAlpha(0f);
780 if (docIcon != 0) {
781 iconMime.setImageDrawable(
782 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
783 } else {
784 iconMime.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
785 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700786 }
787
Jeff Sharkey9656a532013-09-13 13:42:19 -0700788 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700789 boolean hasLine2 = false;
790
Jeff Sharkey9656a532013-09-13 13:42:19 -0700791 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
792 if (!hideTitle) {
793 title.setText(docDisplayName);
794 hasLine1 = true;
795 }
796
797 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700798 if (mType == TYPE_RECENT_OPEN) {
799 final RootInfo root = roots.getRoot(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700800 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700801
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700802 if (summary != null) {
803 final boolean alwaysShowSummary = getResources()
804 .getBoolean(R.bool.always_show_summary);
805 if (alwaysShowSummary) {
806 summary.setText(root.getDirectoryString());
807 summary.setVisibility(View.VISIBLE);
808 hasLine2 = true;
809 } else {
810 if (iconDrawable != null && roots.isIconUnique(root)) {
811 // No summary needed if icon speaks for itself
812 summary.setVisibility(View.INVISIBLE);
813 } else {
814 summary.setText(root.getDirectoryString());
815 summary.setVisibility(View.VISIBLE);
816 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
817 hasLine2 = true;
818 }
819 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700820 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700821 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700822 // Directories showing thumbnails in grid mode get a little icon
823 // hint to remind user they're a directory.
824 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
825 && showThumbnail) {
826 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
827 }
828
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700829 if (summary != null) {
830 if (docSummary != null) {
831 summary.setText(docSummary);
832 summary.setVisibility(View.VISIBLE);
833 hasLine2 = true;
834 } else {
835 summary.setVisibility(View.INVISIBLE);
836 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700837 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700838 }
839
Jeff Sharkey9656a532013-09-13 13:42:19 -0700840 if (icon1 != null) icon1.setVisibility(View.GONE);
841 if (icon2 != null) icon2.setVisibility(View.GONE);
842
843 if (iconDrawable != null) {
844 if (hasLine1) {
845 icon1.setVisibility(View.VISIBLE);
846 icon1.setImageDrawable(iconDrawable);
847 } else {
848 icon2.setVisibility(View.VISIBLE);
849 icon2.setImageDrawable(iconDrawable);
850 }
851 }
852
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700853 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700854 date.setText(null);
855 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700856 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700857 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700858 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700859
860 if (state.showSize) {
861 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700862 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700863 size.setText(null);
864 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700865 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700866 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700867 }
868 } else {
869 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700870 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700871
Jeff Sharkey9656a532013-09-13 13:42:19 -0700872 if (line1 != null) {
873 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
874 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700875 if (line2 != null) {
876 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
877 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700878
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700879 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
880 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
881 if (enabled) {
882 setEnabledRecursive(convertView, true);
883 icon.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700884 if (icon1 != null) icon1.setAlpha(1f);
885 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700886 } else {
887 setEnabledRecursive(convertView, false);
888 icon.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700889 if (icon1 != null) icon1.setAlpha(0.5f);
890 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700891 }
892
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700893 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700894 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700895
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700896 @Override
897 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700898 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700899 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700900
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700901 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700902 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700903 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700904 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700905 return mCursor;
906 } else {
907 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700908 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700909 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700910
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700911 @Override
912 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700913 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700914 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700915
916 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700917 public int getViewTypeCount() {
918 return 4;
919 }
920
921 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700922 public int getItemViewType(int position) {
923 if (position < mCursorCount) {
924 return 0;
925 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700926 position -= mCursorCount;
927 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700928 }
929 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700930 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700931
932 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700933 private final Uri mUri;
934 private final ImageView mIconMime;
935 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700936 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700937 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700938
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700939 public ThumbnailAsyncTask(
940 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
941 mUri = uri;
942 mIconMime = iconMime;
943 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700944 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700945 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700946 }
947
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700948 public void reallyCancel() {
949 cancel(false);
950 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700951 }
952
953 @Override
954 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700955 final Context context = mIconThumb.getContext();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700956
957 Bitmap result = null;
958 try {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700959 // TODO: switch to using unstable provider
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700960 result = DocumentsContract.getDocumentThumbnail(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700961 context.getContentResolver(), mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700962 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700963 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
964 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700965 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700966 }
967 } catch (Exception e) {
968 Log.w(TAG, "Failed to load thumbnail: " + e);
969 }
970 return result;
971 }
972
973 @Override
974 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700975 if (mIconThumb.getTag() == this && result != null) {
976 mIconThumb.setTag(null);
977 mIconThumb.setImageBitmap(result);
978
979 mIconMime.setAlpha(1f);
980 mIconMime.animate().alpha(0f).start();
981 mIconThumb.setAlpha(0f);
982 mIconThumb.animate().alpha(1f).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700983 }
984 }
985 }
986
987 private static String formatTime(Context context, long when) {
988 // TODO: DateUtils should make this easier
989 Time then = new Time();
990 then.set(when);
991 Time now = new Time();
992 now.setToNow();
993
994 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
995 | DateUtils.FORMAT_ABBREV_ALL;
996
997 if (then.year != now.year) {
998 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
999 } else if (then.yearDay != now.yearDay) {
1000 flags |= DateUtils.FORMAT_SHOW_DATE;
1001 } else {
1002 flags |= DateUtils.FORMAT_SHOW_TIME;
1003 }
1004
1005 return DateUtils.formatDateTime(context, when, flags);
1006 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001007
1008 private String findCommonMimeType(List<String> mimeTypes) {
1009 String[] commonType = mimeTypes.get(0).split("/");
1010 if (commonType.length != 2) {
1011 return "*/*";
1012 }
1013
1014 for (int i = 1; i < mimeTypes.size(); i++) {
1015 String[] type = mimeTypes.get(i).split("/");
1016 if (type.length != 2) continue;
1017
1018 if (!commonType[1].equals(type[1])) {
1019 commonType[1] = "*";
1020 }
1021
1022 if (!commonType[0].equals(type[0])) {
1023 commonType[0] = "*";
1024 commonType[1] = "*";
1025 break;
1026 }
1027 }
1028
1029 return commonType[0] + "/" + commonType[1];
1030 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001031
1032 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001033 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001034 if (v.isEnabled() == enabled) return;
1035 v.setEnabled(enabled);
1036
1037 if (v instanceof ViewGroup) {
1038 final ViewGroup vg = (ViewGroup) v;
1039 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1040 setEnabledRecursive(vg.getChildAt(i), enabled);
1041 }
1042 }
1043 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001044}