blob: 198927c24cf60fc1c4ddf5f600182c473799f3fc [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 Sharkeye22d02e2013-04-26 16:54:55 -070046import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070047import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070048import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070049import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.text.format.Time;
51import android.util.Log;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070052import android.util.SparseBooleanArray;
53import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070055import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070056import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070057import android.view.View;
58import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070059import android.widget.AbsListView;
60import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070061import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070062import android.widget.AdapterView;
63import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070064import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070065import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070066import android.widget.ImageView;
67import android.widget.ListView;
68import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070069import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070070
Jeff Sharkeyb3620442013-09-01 18:41:04 -070071import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070072import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070073import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070074import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070075import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070076import com.google.android.collect.Lists;
77
78import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070079import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070080import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081
82/**
83 * Display the documents inside a single directory.
84 */
85public class DirectoryFragment extends Fragment {
86
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070087 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070088 private ListView mListView;
89 private GridView mGridView;
90
Jeff Sharkeyc317af82013-07-01 16:56:54 -070091 private AbsListView mCurrentView;
92
Jeff Sharkey724deeb2013-08-31 15:02:20 -070093 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070094
Jeff Sharkeya5defe32013-08-05 17:56:48 -070095 public static final int TYPE_NORMAL = 1;
96 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070097 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -070098
99 private int mType = TYPE_NORMAL;
100
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700101 private int mLastMode = MODE_UNKNOWN;
102 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700103 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700104
Jeff Sharkey9656a532013-09-13 13:42:19 -0700105 private boolean mHideGridTitles = false;
106
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700107 private Point mThumbSize;
108
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700109 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700110 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700111
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700112 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700113 private static final String EXTRA_ROOT = "root";
114 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700115 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700116
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700117 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700118
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700119 private final int mLoaderId = sLoaderId.incrementAndGet();
120
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700121 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc) {
122 show(fm, TYPE_NORMAL, root, doc, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700123 }
124
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700125 public static void showSearch(FragmentManager fm, RootInfo root, String query) {
126 show(fm, TYPE_SEARCH, root, null, query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 }
128
129 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700130 show(fm, TYPE_RECENT_OPEN, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 }
132
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700133 private static void show(
134 FragmentManager fm, int type, RootInfo root, DocumentInfo doc, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700135 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700136 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700137 args.putParcelable(EXTRA_ROOT, root);
138 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700139 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700140
141 final DirectoryFragment fragment = new DirectoryFragment();
142 fragment.setArguments(args);
143
144 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700145 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700146 ft.commitAllowingStateLoss();
147 }
148
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700149 public static DirectoryFragment get(FragmentManager fm) {
150 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700151 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700152 }
153
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700154 @Override
155 public View onCreateView(
156 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
157 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700158 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
159
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700160 mEmptyView = view.findViewById(android.R.id.empty);
161
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700162 mListView = (ListView) view.findViewById(R.id.list);
163 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700164 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700165 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700166
167 mGridView = (GridView) view.findViewById(R.id.grid);
168 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700169 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700170 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700171
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700172 return view;
173 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700174
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700175 @Override
176 public void onActivityCreated(Bundle savedInstanceState) {
177 super.onActivityCreated(savedInstanceState);
178
179 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700180 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700181
Jeff Sharkey9656a532013-09-13 13:42:19 -0700182 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
183 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
184
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700185 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700186 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700187
Jeff Sharkey9656a532013-09-13 13:42:19 -0700188 if (mType == TYPE_RECENT_OPEN) {
189 // Hide titles when showing recents for picking images/videos
190 mHideGridTitles = MimePredicate.mimeMatches(
191 MimePredicate.VISUAL_MIMES, state.acceptMimes);
192 } else {
193 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
194 }
195
Jeff Sharkey46899c82013-08-18 22:26:48 -0700196 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700197 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700198 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700199 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700200
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700201 Uri contentsUri;
202 switch (mType) {
203 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700204 contentsUri = DocumentsContract.buildChildDocumentsUri(
205 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700206 if (state.action == ACTION_MANAGE) {
207 contentsUri = DocumentsContract.setManageMode(contentsUri);
208 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700209 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700210 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700211 case TYPE_SEARCH:
212 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700213 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700214 if (state.action == ACTION_MANAGE) {
215 contentsUri = DocumentsContract.setManageMode(contentsUri);
216 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700217 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700218 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700219 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700220 final RootsCache roots = DocumentsApplication.getRootsCache(context);
221 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700222 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700223 default:
224 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700225 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700226 }
227
228 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700229 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700230 if (!isAdded()) return;
231
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700232 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700233
234 // Push latest state up to UI
235 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700236 if (result.mode != MODE_UNKNOWN) {
237 state.derivedMode = result.mode;
238 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700239 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700240 ((DocumentsActivity) context).onStateChanged();
241
242 updateDisplayState();
243
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700244 if (mLastSortOrder != state.derivedSortOrder) {
245 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700246 mListView.smoothScrollToPosition(0);
247 mGridView.smoothScrollToPosition(0);
248 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700249 }
250
251 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700252 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700253 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700254 }
255 };
256
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700257 // Kick off loader at least once
258 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
259
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700260 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700261 }
262
Jeff Sharkey42d26792013-09-06 13:22:09 -0700263 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700264 public void onResume() {
265 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700266 updateDisplayState();
267 }
268
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700269 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700270 // Sort order change always triggers reload; we'll trigger state change
271 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700272 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
273 }
274
275 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700276 final ContentResolver resolver = getActivity().getContentResolver();
277 final State state = getDisplayState(this);
278
279 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
280 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
281
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700282 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700283 final Uri stateUri = RecentsProvider.buildState(
284 root.authority, root.rootId, doc.documentId);
285 final ContentValues values = new ContentValues();
286 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700287
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700288 new AsyncTask<Void, Void, Void>() {
289 @Override
290 protected Void doInBackground(Void... params) {
291 resolver.insert(stateUri, values);
292 return null;
293 }
294 }.execute();
295 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700296
297 // Mode change is just visual change; no need to kick loader, and
298 // deliver change event immediately.
299 state.derivedMode = state.userMode;
300 ((DocumentsActivity) getActivity()).onStateChanged();
301
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700302 updateDisplayState();
303 }
304
305 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700306 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700307
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700308 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700309
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700310 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700311 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700312 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700313
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700314 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
315 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700316
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700317 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700318 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700319 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
320 } else {
321 choiceMode = ListView.CHOICE_MODE_NONE;
322 }
323
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700324 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700325 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700326 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700327 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700328 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700329 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700330 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700331 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700332 mGridView.setChoiceMode(choiceMode);
333 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700334 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700335 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700336 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700337 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700338 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700339 mListView.setChoiceMode(choiceMode);
340 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700341 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700342 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700343 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700344
345 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700346 }
347
348 private OnItemClickListener mItemListener = new OnItemClickListener() {
349 @Override
350 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700351 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700352 if (cursor != null) {
353 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
354 if (mFilter.apply(doc)) {
355 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
356 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700357 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700358 }
359 };
360
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700361 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
362 @Override
363 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
364 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
365 return true;
366 }
367
368 @Override
369 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700370 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700371
372 final MenuItem open = menu.findItem(R.id.menu_open);
373 final MenuItem share = menu.findItem(R.id.menu_share);
374 final MenuItem delete = menu.findItem(R.id.menu_delete);
375
376 final boolean manageMode = state.action == ACTION_MANAGE;
377 open.setVisible(!manageMode);
378 share.setVisible(manageMode);
379 delete.setVisible(manageMode);
380
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700381 return true;
382 }
383
384 @Override
385 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700386 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700387 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700388 final int size = checked.size();
389 for (int i = 0; i < size; i++) {
390 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700391 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700392 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700393 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700394 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700395 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700396
Jeff Sharkey873daa32013-08-18 17:38:20 -0700397 final int id = item.getItemId();
398 if (id == R.id.menu_open) {
399 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700400 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700401 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700402
403 } else if (id == R.id.menu_share) {
404 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700405 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700406 return true;
407
408 } else if (id == R.id.menu_delete) {
409 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700410 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700411 return true;
412
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700413 } else {
414 return false;
415 }
416 }
417
418 @Override
419 public void onDestroyActionMode(ActionMode mode) {
420 // ignored
421 }
422
423 @Override
424 public void onItemCheckedStateChanged(
425 ActionMode mode, int position, long id, boolean checked) {
426 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700427 // Directories and footer items cannot be checked
428 boolean valid = false;
429
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700430 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700431 if (cursor != null) {
432 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
433
434 // Only valid if non-directory matches filter
435 final State state = getDisplayState(DirectoryFragment.this);
436 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
437 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
438 }
439
440 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700441 mCurrentView.setItemChecked(position, false);
442 }
443 }
444
445 mode.setTitle(getResources()
446 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
447 }
448 };
449
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700450 private RecyclerListener mRecycleListener = new RecyclerListener() {
451 @Override
452 public void onMovedToScrapHeap(View view) {
453 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
454 if (iconThumb != null) {
455 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
456 if (oldTask != null) {
457 oldTask.reallyCancel();
458 iconThumb.setTag(null);
459 }
460 }
461 }
462 };
463
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700464 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700465 Intent intent;
466 if (docs.size() == 1) {
467 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700468
Jeff Sharkey873daa32013-08-18 17:38:20 -0700469 intent = new Intent(Intent.ACTION_SEND);
470 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
471 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700472 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700473 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700474
475 } else if (docs.size() > 1) {
476 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
477 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
478 intent.addCategory(Intent.CATEGORY_DEFAULT);
479
480 final ArrayList<String> mimeTypes = Lists.newArrayList();
481 final ArrayList<Uri> uris = Lists.newArrayList();
482 for (DocumentInfo doc : docs) {
483 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700484 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700485 }
486
487 intent.setType(findCommonMimeType(mimeTypes));
488 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
489
490 } else {
491 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700492 }
493
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700494 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700495 startActivity(intent);
496 }
497
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700498 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499 final Context context = getActivity();
500 final ContentResolver resolver = context.getContentResolver();
501
502 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700503 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700504 if (!doc.isDeleteSupported()) {
505 Log.w(TAG, "Skipping " + doc);
506 hadTrouble = true;
507 continue;
508 }
509
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700510 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700511 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700512 hadTrouble = true;
513 }
514 }
515
516 if (hadTrouble) {
517 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
518 }
519 }
520
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700521 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700522 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700523 }
524
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700525 private static abstract class Footer {
526 private final int mItemViewType;
527
528 public Footer(int itemViewType) {
529 mItemViewType = itemViewType;
530 }
531
532 public abstract View getView(View convertView, ViewGroup parent);
533
534 public int getItemViewType() {
535 return mItemViewType;
536 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700537 }
538
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700539 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700540 public LoadingFooter() {
541 super(1);
542 }
543
Jeff Sharkey20b32272013-09-03 15:25:52 -0700544 @Override
545 public View getView(View convertView, ViewGroup parent) {
546 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700547 final State state = getDisplayState(DirectoryFragment.this);
548
Jeff Sharkey20b32272013-09-03 15:25:52 -0700549 if (convertView == null) {
550 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700551 if (state.derivedMode == MODE_LIST) {
552 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
553 } else if (state.derivedMode == MODE_GRID) {
554 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
555 } else {
556 throw new IllegalStateException();
557 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700558 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700559
Jeff Sharkey20b32272013-09-03 15:25:52 -0700560 return convertView;
561 }
562 }
563
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700564 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700565 private final int mIcon;
566 private final String mMessage;
567
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700568 public MessageFooter(int itemViewType, int icon, String message) {
569 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700570 mIcon = icon;
571 mMessage = message;
572 }
573
574 @Override
575 public View getView(View convertView, ViewGroup parent) {
576 final Context context = parent.getContext();
577 final State state = getDisplayState(DirectoryFragment.this);
578
579 if (convertView == null) {
580 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700581 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700582 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700583 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700584 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
585 } else {
586 throw new IllegalStateException();
587 }
588 }
589
590 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
591 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
592 icon.setImageResource(mIcon);
593 title.setText(mMessage);
594 return convertView;
595 }
596 }
597
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700598 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700599 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700600 private int mCursorCount;
601
602 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700603
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700604 public void swapCursor(Cursor cursor) {
605 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700606 mCursorCount = cursor != null ? cursor.getCount() : 0;
607
608 mFooters.clear();
609
610 final Bundle extras = cursor != null ? cursor.getExtras() : null;
611 if (extras != null) {
612 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
613 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700614 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700615 }
616 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
617 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700618 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700619 }
620 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
621 mFooters.add(new LoadingFooter());
622 }
623 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700624
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700625 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700626 mEmptyView.setVisibility(View.VISIBLE);
627 } else {
628 mEmptyView.setVisibility(View.GONE);
629 }
630
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700631 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700632 }
633
634 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700635 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700636 if (position < mCursorCount) {
637 return getDocumentView(position, convertView, parent);
638 } else {
639 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700640 convertView = mFooters.get(position).getView(convertView, parent);
641 // Only the view itself is disabled; contents inside shouldn't
642 // be dimmed.
643 convertView.setEnabled(false);
644 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700645 }
646 }
647
648 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700649 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700650 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700651
Jeff Sharkey9656a532013-09-13 13:42:19 -0700652 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
653
Jeff Sharkey873daa32013-08-18 17:38:20 -0700654 final RootsCache roots = DocumentsApplication.getRootsCache(context);
655 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
656 context, mThumbSize);
657
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700658 if (convertView == null) {
659 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700660 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700661 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700662 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700663 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
664 } else {
665 throw new IllegalStateException();
666 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700667 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700668
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700669 final Cursor cursor = getItem(position);
670
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700671 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
672 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700673 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
674 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
675 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
676 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
677 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
678 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
679 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
680 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700681
Jeff Sharkey9656a532013-09-13 13:42:19 -0700682 final View line1 = convertView.findViewById(R.id.line1);
683 final View line2 = convertView.findViewById(R.id.line2);
684
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700685 final View icon = convertView.findViewById(android.R.id.icon);
686 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
687 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700688 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
689 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700690 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700691 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
692 final TextView date = (TextView) convertView.findViewById(R.id.date);
693 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700694
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700695 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700696 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700697 oldTask.reallyCancel();
698 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700699 }
700
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700701 iconMime.animate().cancel();
702 iconThumb.animate().cancel();
703
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700704 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700705 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700706 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
707 final boolean showThumbnail = supportsThumbnail && allowThumbnail;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700708
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700709 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700710 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700711 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700712 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700713 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700714 iconThumb.setImageBitmap(cachedResult);
715 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700716 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700717 iconThumb.setImageDrawable(null);
718 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
719 uri, iconMime, iconThumb, mThumbSize);
720 iconThumb.setTag(task);
721 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700722 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700723 }
724
725 // Always throw MIME icon into place, even when a thumbnail is being
726 // loaded in background.
727 if (cacheHit) {
728 iconMime.setAlpha(0f);
729 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700730 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700731 iconMime.setAlpha(1f);
732 iconThumb.setAlpha(0f);
733 if (docIcon != 0) {
734 iconMime.setImageDrawable(
735 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
736 } else {
737 iconMime.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
738 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700739 }
740
Jeff Sharkey9656a532013-09-13 13:42:19 -0700741 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700742 boolean hasLine2 = false;
743
Jeff Sharkey9656a532013-09-13 13:42:19 -0700744 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
745 if (!hideTitle) {
746 title.setText(docDisplayName);
747 hasLine1 = true;
748 }
749
750 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700751 if (mType == TYPE_RECENT_OPEN) {
752 final RootInfo root = roots.getRoot(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700753 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700754
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700755 if (summary != null) {
756 final boolean alwaysShowSummary = getResources()
757 .getBoolean(R.bool.always_show_summary);
758 if (alwaysShowSummary) {
759 summary.setText(root.getDirectoryString());
760 summary.setVisibility(View.VISIBLE);
761 hasLine2 = true;
762 } else {
763 if (iconDrawable != null && roots.isIconUnique(root)) {
764 // No summary needed if icon speaks for itself
765 summary.setVisibility(View.INVISIBLE);
766 } else {
767 summary.setText(root.getDirectoryString());
768 summary.setVisibility(View.VISIBLE);
769 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
770 hasLine2 = true;
771 }
772 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700773 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700774 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700775 // Directories showing thumbnails in grid mode get a little icon
776 // hint to remind user they're a directory.
777 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
778 && showThumbnail) {
779 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
780 }
781
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700782 if (summary != null) {
783 if (docSummary != null) {
784 summary.setText(docSummary);
785 summary.setVisibility(View.VISIBLE);
786 hasLine2 = true;
787 } else {
788 summary.setVisibility(View.INVISIBLE);
789 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700790 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700791 }
792
Jeff Sharkey9656a532013-09-13 13:42:19 -0700793 if (icon1 != null) icon1.setVisibility(View.GONE);
794 if (icon2 != null) icon2.setVisibility(View.GONE);
795
796 if (iconDrawable != null) {
797 if (hasLine1) {
798 icon1.setVisibility(View.VISIBLE);
799 icon1.setImageDrawable(iconDrawable);
800 } else {
801 icon2.setVisibility(View.VISIBLE);
802 icon2.setImageDrawable(iconDrawable);
803 }
804 }
805
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700806 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700807 date.setText(null);
808 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700809 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700810 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700811 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700812
813 if (state.showSize) {
814 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700815 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700816 size.setText(null);
817 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700818 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700819 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700820 }
821 } else {
822 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700823 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700824
Jeff Sharkey9656a532013-09-13 13:42:19 -0700825 if (line1 != null) {
826 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
827 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700828 if (line2 != null) {
829 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
830 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700831
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700832 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
833 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
834 if (enabled) {
835 setEnabledRecursive(convertView, true);
836 icon.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700837 if (icon1 != null) icon1.setAlpha(1f);
838 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700839 } else {
840 setEnabledRecursive(convertView, false);
841 icon.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700842 if (icon1 != null) icon1.setAlpha(0.5f);
843 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700844 }
845
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700846 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700847 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700848
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700849 @Override
850 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700851 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700852 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700853
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700854 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700855 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700856 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700857 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700858 return mCursor;
859 } else {
860 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700861 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700862 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700863
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700864 @Override
865 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700866 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700867 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700868
869 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700870 public int getViewTypeCount() {
871 return 4;
872 }
873
874 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700875 public int getItemViewType(int position) {
876 if (position < mCursorCount) {
877 return 0;
878 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700879 position -= mCursorCount;
880 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700881 }
882 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700883 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700884
885 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700886 private final Uri mUri;
887 private final ImageView mIconMime;
888 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700889 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700890 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700891
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700892 public ThumbnailAsyncTask(
893 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
894 mUri = uri;
895 mIconMime = iconMime;
896 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700897 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700898 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700899 }
900
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700901 public void reallyCancel() {
902 cancel(false);
903 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700904 }
905
906 @Override
907 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700908 final Context context = mIconThumb.getContext();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700909
910 Bitmap result = null;
911 try {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700912 // TODO: switch to using unstable provider
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700913 result = DocumentsContract.getDocumentThumbnail(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700914 context.getContentResolver(), mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700915 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700916 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
917 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700918 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700919 }
920 } catch (Exception e) {
921 Log.w(TAG, "Failed to load thumbnail: " + e);
922 }
923 return result;
924 }
925
926 @Override
927 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700928 if (mIconThumb.getTag() == this && result != null) {
929 mIconThumb.setTag(null);
930 mIconThumb.setImageBitmap(result);
931
932 mIconMime.setAlpha(1f);
933 mIconMime.animate().alpha(0f).start();
934 mIconThumb.setAlpha(0f);
935 mIconThumb.animate().alpha(1f).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700936 }
937 }
938 }
939
940 private static String formatTime(Context context, long when) {
941 // TODO: DateUtils should make this easier
942 Time then = new Time();
943 then.set(when);
944 Time now = new Time();
945 now.setToNow();
946
947 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
948 | DateUtils.FORMAT_ABBREV_ALL;
949
950 if (then.year != now.year) {
951 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
952 } else if (then.yearDay != now.yearDay) {
953 flags |= DateUtils.FORMAT_SHOW_DATE;
954 } else {
955 flags |= DateUtils.FORMAT_SHOW_TIME;
956 }
957
958 return DateUtils.formatDateTime(context, when, flags);
959 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700960
961 private String findCommonMimeType(List<String> mimeTypes) {
962 String[] commonType = mimeTypes.get(0).split("/");
963 if (commonType.length != 2) {
964 return "*/*";
965 }
966
967 for (int i = 1; i < mimeTypes.size(); i++) {
968 String[] type = mimeTypes.get(i).split("/");
969 if (type.length != 2) continue;
970
971 if (!commonType[1].equals(type[1])) {
972 commonType[1] = "*";
973 }
974
975 if (!commonType[0].equals(type[0])) {
976 commonType[0] = "*";
977 commonType[1] = "*";
978 break;
979 }
980 }
981
982 return commonType[0] + "/" + commonType[1];
983 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700984
985 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700986 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700987 if (v.isEnabled() == enabled) return;
988 v.setEnabled(enabled);
989
990 if (v instanceof ViewGroup) {
991 final ViewGroup vg = (ViewGroup) v;
992 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
993 setEnabledRecursive(vg.getChildAt(i), enabled);
994 }
995 }
996 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700997}