blob: 911e9ed99244b3a5c6d3db10b36e448d6af73cf1 [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);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700252 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700253 default:
254 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700255 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700256 }
257
258 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700259 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700260 if (!isAdded()) return;
261
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700262 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700263
264 // Push latest state up to UI
265 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700266 if (result.mode != MODE_UNKNOWN) {
267 state.derivedMode = result.mode;
268 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700269 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700270 ((DocumentsActivity) context).onStateChanged();
271
272 updateDisplayState();
273
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700274 // Restore any previous instance state
275 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
276 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
277 getView().restoreHierarchyState(container);
278 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700279 mListView.smoothScrollToPosition(0);
280 mGridView.smoothScrollToPosition(0);
281 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700282
283 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700284 }
285
286 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700287 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700288 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700289 }
290 };
291
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700292 // Kick off loader at least once
293 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
294
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700295 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700296 }
297
Jeff Sharkey42d26792013-09-06 13:22:09 -0700298 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700299 public void onStop() {
300 super.onStop();
301
302 // Remember last scroll location
303 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
304 getView().saveHierarchyState(container);
305 final State state = getDisplayState(this);
306 state.dirState.put(mStateKey, container);
307 }
308
309 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700310 public void onResume() {
311 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700312 updateDisplayState();
313 }
314
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700315 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700316 // Sort order change always triggers reload; we'll trigger state change
317 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700318 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
319 }
320
321 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700322 final ContentResolver resolver = getActivity().getContentResolver();
323 final State state = getDisplayState(this);
324
325 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
326 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
327
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700328 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700329 final Uri stateUri = RecentsProvider.buildState(
330 root.authority, root.rootId, doc.documentId);
331 final ContentValues values = new ContentValues();
332 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700333
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700334 new AsyncTask<Void, Void, Void>() {
335 @Override
336 protected Void doInBackground(Void... params) {
337 resolver.insert(stateUri, values);
338 return null;
339 }
340 }.execute();
341 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700342
343 // Mode change is just visual change; no need to kick loader, and
344 // deliver change event immediately.
345 state.derivedMode = state.userMode;
346 ((DocumentsActivity) getActivity()).onStateChanged();
347
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700348 updateDisplayState();
349 }
350
351 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700352 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700353
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700354 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700355
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700356 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700357 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700358 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700359
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700360 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
361 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700362
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700363 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700364 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700365 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
366 } else {
367 choiceMode = ListView.CHOICE_MODE_NONE;
368 }
369
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700370 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700371 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700372 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700373 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700374 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700375 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700376 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700377 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700378 mGridView.setChoiceMode(choiceMode);
379 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700380 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700381 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700382 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700383 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700384 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700385 mListView.setChoiceMode(choiceMode);
386 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700387 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700388 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700389 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700390
391 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700392 }
393
394 private OnItemClickListener mItemListener = new OnItemClickListener() {
395 @Override
396 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700397 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700398 if (cursor != null) {
399 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
400 if (mFilter.apply(doc)) {
401 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
402 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700403 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700404 }
405 };
406
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700407 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
408 @Override
409 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
410 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
411 return true;
412 }
413
414 @Override
415 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700416 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700417
418 final MenuItem open = menu.findItem(R.id.menu_open);
419 final MenuItem share = menu.findItem(R.id.menu_share);
420 final MenuItem delete = menu.findItem(R.id.menu_delete);
421
422 final boolean manageMode = state.action == ACTION_MANAGE;
423 open.setVisible(!manageMode);
424 share.setVisible(manageMode);
425 delete.setVisible(manageMode);
426
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700427 return true;
428 }
429
430 @Override
431 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700432 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700433 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700434 final int size = checked.size();
435 for (int i = 0; i < size; i++) {
436 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700437 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700438 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700439 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700440 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700441 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700442
Jeff Sharkey873daa32013-08-18 17:38:20 -0700443 final int id = item.getItemId();
444 if (id == R.id.menu_open) {
445 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700446 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700448
449 } else if (id == R.id.menu_share) {
450 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700451 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700452 return true;
453
454 } else if (id == R.id.menu_delete) {
455 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700456 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700457 return true;
458
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700459 } else {
460 return false;
461 }
462 }
463
464 @Override
465 public void onDestroyActionMode(ActionMode mode) {
466 // ignored
467 }
468
469 @Override
470 public void onItemCheckedStateChanged(
471 ActionMode mode, int position, long id, boolean checked) {
472 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700473 // Directories and footer items cannot be checked
474 boolean valid = false;
475
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700476 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700477 if (cursor != null) {
478 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
479
480 // Only valid if non-directory matches filter
481 final State state = getDisplayState(DirectoryFragment.this);
482 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
483 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
484 }
485
486 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700487 mCurrentView.setItemChecked(position, false);
488 }
489 }
490
491 mode.setTitle(getResources()
492 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
493 }
494 };
495
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700496 private RecyclerListener mRecycleListener = new RecyclerListener() {
497 @Override
498 public void onMovedToScrapHeap(View view) {
499 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
500 if (iconThumb != null) {
501 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
502 if (oldTask != null) {
503 oldTask.reallyCancel();
504 iconThumb.setTag(null);
505 }
506 }
507 }
508 };
509
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700510 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700511 Intent intent;
512 if (docs.size() == 1) {
513 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700514
Jeff Sharkey873daa32013-08-18 17:38:20 -0700515 intent = new Intent(Intent.ACTION_SEND);
516 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
517 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700518 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700519 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700520
521 } else if (docs.size() > 1) {
522 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
523 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
524 intent.addCategory(Intent.CATEGORY_DEFAULT);
525
526 final ArrayList<String> mimeTypes = Lists.newArrayList();
527 final ArrayList<Uri> uris = Lists.newArrayList();
528 for (DocumentInfo doc : docs) {
529 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700530 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700531 }
532
533 intent.setType(findCommonMimeType(mimeTypes));
534 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
535
536 } else {
537 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700538 }
539
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700540 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700541 startActivity(intent);
542 }
543
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700544 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700545 final Context context = getActivity();
546 final ContentResolver resolver = context.getContentResolver();
547
548 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700549 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700550 if (!doc.isDeleteSupported()) {
551 Log.w(TAG, "Skipping " + doc);
552 hadTrouble = true;
553 continue;
554 }
555
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700556 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700557 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700558 hadTrouble = true;
559 }
560 }
561
562 if (hadTrouble) {
563 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
564 }
565 }
566
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700567 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700568 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700569 }
570
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700571 private static abstract class Footer {
572 private final int mItemViewType;
573
574 public Footer(int itemViewType) {
575 mItemViewType = itemViewType;
576 }
577
578 public abstract View getView(View convertView, ViewGroup parent);
579
580 public int getItemViewType() {
581 return mItemViewType;
582 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700583 }
584
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700585 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700586 public LoadingFooter() {
587 super(1);
588 }
589
Jeff Sharkey20b32272013-09-03 15:25:52 -0700590 @Override
591 public View getView(View convertView, ViewGroup parent) {
592 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700593 final State state = getDisplayState(DirectoryFragment.this);
594
Jeff Sharkey20b32272013-09-03 15:25:52 -0700595 if (convertView == null) {
596 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700597 if (state.derivedMode == MODE_LIST) {
598 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
599 } else if (state.derivedMode == MODE_GRID) {
600 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
601 } else {
602 throw new IllegalStateException();
603 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700604 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700605
Jeff Sharkey20b32272013-09-03 15:25:52 -0700606 return convertView;
607 }
608 }
609
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700610 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700611 private final int mIcon;
612 private final String mMessage;
613
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700614 public MessageFooter(int itemViewType, int icon, String message) {
615 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700616 mIcon = icon;
617 mMessage = message;
618 }
619
620 @Override
621 public View getView(View convertView, ViewGroup parent) {
622 final Context context = parent.getContext();
623 final State state = getDisplayState(DirectoryFragment.this);
624
625 if (convertView == null) {
626 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700627 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700628 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700629 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700630 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
631 } else {
632 throw new IllegalStateException();
633 }
634 }
635
636 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
637 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
638 icon.setImageResource(mIcon);
639 title.setText(mMessage);
640 return convertView;
641 }
642 }
643
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700644 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700645 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700646 private int mCursorCount;
647
648 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700649
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700650 public void swapCursor(Cursor cursor) {
651 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700652 mCursorCount = cursor != null ? cursor.getCount() : 0;
653
654 mFooters.clear();
655
656 final Bundle extras = cursor != null ? cursor.getExtras() : null;
657 if (extras != null) {
658 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
659 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700660 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700661 }
662 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
663 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700664 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700665 }
666 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
667 mFooters.add(new LoadingFooter());
668 }
669 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700670
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700671 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700672 mEmptyView.setVisibility(View.VISIBLE);
673 } else {
674 mEmptyView.setVisibility(View.GONE);
675 }
676
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700677 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700678 }
679
680 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700681 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700682 if (position < mCursorCount) {
683 return getDocumentView(position, convertView, parent);
684 } else {
685 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700686 convertView = mFooters.get(position).getView(convertView, parent);
687 // Only the view itself is disabled; contents inside shouldn't
688 // be dimmed.
689 convertView.setEnabled(false);
690 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700691 }
692 }
693
694 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700695 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700696 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700697
Jeff Sharkey9656a532013-09-13 13:42:19 -0700698 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
699
Jeff Sharkey873daa32013-08-18 17:38:20 -0700700 final RootsCache roots = DocumentsApplication.getRootsCache(context);
701 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
702 context, mThumbSize);
703
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700704 if (convertView == null) {
705 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700706 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700707 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700708 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700709 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
710 } else {
711 throw new IllegalStateException();
712 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700713 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700714
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700715 final Cursor cursor = getItem(position);
716
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700717 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
718 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700719 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
720 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
721 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
722 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
723 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
724 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
725 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
726 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700727
Jeff Sharkey9656a532013-09-13 13:42:19 -0700728 final View line1 = convertView.findViewById(R.id.line1);
729 final View line2 = convertView.findViewById(R.id.line2);
730
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700731 final View icon = convertView.findViewById(android.R.id.icon);
732 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
733 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700734 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
735 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700736 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700737 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
738 final TextView date = (TextView) convertView.findViewById(R.id.date);
739 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700740
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700741 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700742 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700743 oldTask.reallyCancel();
744 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700745 }
746
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700747 iconMime.animate().cancel();
748 iconThumb.animate().cancel();
749
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700750 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700751 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700752 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
753 final boolean showThumbnail = supportsThumbnail && allowThumbnail;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700754
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700755 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700756 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700757 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700758 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700759 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700760 iconThumb.setImageBitmap(cachedResult);
761 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700762 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700763 iconThumb.setImageDrawable(null);
764 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
765 uri, iconMime, iconThumb, mThumbSize);
766 iconThumb.setTag(task);
767 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700768 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700769 }
770
771 // Always throw MIME icon into place, even when a thumbnail is being
772 // loaded in background.
773 if (cacheHit) {
774 iconMime.setAlpha(0f);
775 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700776 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700777 iconMime.setAlpha(1f);
778 iconThumb.setAlpha(0f);
779 if (docIcon != 0) {
780 iconMime.setImageDrawable(
781 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
782 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700783 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
784 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700785 }
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) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700799 // We've already had to enumerate roots before any results can
800 // be shown, so this will never block.
801 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700802 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700803
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700804 if (summary != null) {
805 final boolean alwaysShowSummary = getResources()
806 .getBoolean(R.bool.always_show_summary);
807 if (alwaysShowSummary) {
808 summary.setText(root.getDirectoryString());
809 summary.setVisibility(View.VISIBLE);
810 hasLine2 = true;
811 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700812 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700813 // No summary needed if icon speaks for itself
814 summary.setVisibility(View.INVISIBLE);
815 } else {
816 summary.setText(root.getDirectoryString());
817 summary.setVisibility(View.VISIBLE);
818 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
819 hasLine2 = true;
820 }
821 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700822 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700823 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700824 // Directories showing thumbnails in grid mode get a little icon
825 // hint to remind user they're a directory.
826 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
827 && showThumbnail) {
828 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
829 }
830
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700831 if (summary != null) {
832 if (docSummary != null) {
833 summary.setText(docSummary);
834 summary.setVisibility(View.VISIBLE);
835 hasLine2 = true;
836 } else {
837 summary.setVisibility(View.INVISIBLE);
838 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700839 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700840 }
841
Jeff Sharkey9656a532013-09-13 13:42:19 -0700842 if (icon1 != null) icon1.setVisibility(View.GONE);
843 if (icon2 != null) icon2.setVisibility(View.GONE);
844
845 if (iconDrawable != null) {
846 if (hasLine1) {
847 icon1.setVisibility(View.VISIBLE);
848 icon1.setImageDrawable(iconDrawable);
849 } else {
850 icon2.setVisibility(View.VISIBLE);
851 icon2.setImageDrawable(iconDrawable);
852 }
853 }
854
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700855 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700856 date.setText(null);
857 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700858 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700859 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700860 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700861
862 if (state.showSize) {
863 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700864 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700865 size.setText(null);
866 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700867 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700868 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700869 }
870 } else {
871 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700872 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700873
Jeff Sharkey9656a532013-09-13 13:42:19 -0700874 if (line1 != null) {
875 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
876 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700877 if (line2 != null) {
878 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
879 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700880
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700881 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
882 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
883 if (enabled) {
884 setEnabledRecursive(convertView, true);
885 icon.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700886 if (icon1 != null) icon1.setAlpha(1f);
887 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700888 } else {
889 setEnabledRecursive(convertView, false);
890 icon.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700891 if (icon1 != null) icon1.setAlpha(0.5f);
892 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700893 }
894
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700895 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700896 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700897
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700898 @Override
899 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700900 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700901 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700902
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700903 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700904 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700905 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700906 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700907 return mCursor;
908 } else {
909 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700910 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700911 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700912
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700913 @Override
914 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700915 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700916 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700917
918 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700919 public int getViewTypeCount() {
920 return 4;
921 }
922
923 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700924 public int getItemViewType(int position) {
925 if (position < mCursorCount) {
926 return 0;
927 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700928 position -= mCursorCount;
929 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700930 }
931 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700932 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700933
934 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700935 private final Uri mUri;
936 private final ImageView mIconMime;
937 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700938 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700939 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700940
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700941 public ThumbnailAsyncTask(
942 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
943 mUri = uri;
944 mIconMime = iconMime;
945 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700946 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700947 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700948 }
949
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700950 public void reallyCancel() {
951 cancel(false);
952 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700953 }
954
955 @Override
956 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700957 final Context context = mIconThumb.getContext();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700958
959 Bitmap result = null;
960 try {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700961 // TODO: switch to using unstable provider
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700962 result = DocumentsContract.getDocumentThumbnail(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700963 context.getContentResolver(), mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700964 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700965 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
966 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700967 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700968 }
969 } catch (Exception e) {
970 Log.w(TAG, "Failed to load thumbnail: " + e);
971 }
972 return result;
973 }
974
975 @Override
976 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700977 if (mIconThumb.getTag() == this && result != null) {
978 mIconThumb.setTag(null);
979 mIconThumb.setImageBitmap(result);
980
981 mIconMime.setAlpha(1f);
982 mIconMime.animate().alpha(0f).start();
983 mIconThumb.setAlpha(0f);
984 mIconThumb.animate().alpha(1f).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700985 }
986 }
987 }
988
989 private static String formatTime(Context context, long when) {
990 // TODO: DateUtils should make this easier
991 Time then = new Time();
992 then.set(when);
993 Time now = new Time();
994 now.setToNow();
995
996 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
997 | DateUtils.FORMAT_ABBREV_ALL;
998
999 if (then.year != now.year) {
1000 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1001 } else if (then.yearDay != now.yearDay) {
1002 flags |= DateUtils.FORMAT_SHOW_DATE;
1003 } else {
1004 flags |= DateUtils.FORMAT_SHOW_TIME;
1005 }
1006
1007 return DateUtils.formatDateTime(context, when, flags);
1008 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001009
1010 private String findCommonMimeType(List<String> mimeTypes) {
1011 String[] commonType = mimeTypes.get(0).split("/");
1012 if (commonType.length != 2) {
1013 return "*/*";
1014 }
1015
1016 for (int i = 1; i < mimeTypes.size(); i++) {
1017 String[] type = mimeTypes.get(i).split("/");
1018 if (type.length != 2) continue;
1019
1020 if (!commonType[1].equals(type[1])) {
1021 commonType[1] = "*";
1022 }
1023
1024 if (!commonType[0].equals(type[0])) {
1025 commonType[0] = "*";
1026 commonType[1] = "*";
1027 break;
1028 }
1029 }
1030
1031 return commonType[0] + "/" + commonType[1];
1032 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001033
1034 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001035 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001036 if (v.isEnabled() == enabled) return;
1037 v.setEnabled(enabled);
1038
1039 if (v instanceof ViewGroup) {
1040 final ViewGroup vg = (ViewGroup) v;
1041 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1042 setEnabledRecursive(vg.getChildAt(i), enabled);
1043 }
1044 }
1045 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001046}