blob: b2981dbe301b4248f2945896f24d4f9f41024f30 [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 Sharkey8a8fb672013-05-07 12:41:33 -0700105 private Point mThumbSize;
106
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700107 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700108 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700109
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700110 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700111 private static final String EXTRA_ROOT = "root";
112 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700113 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700114
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700115 /**
116 * MIME types that should always show thumbnails in list mode.
117 */
118 private static final String[] LIST_THUMBNAIL_MIMES = new String[] { "image/*", "video/*" };
119
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700120 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700121
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700122 private final int mLoaderId = sLoaderId.incrementAndGet();
123
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700124 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc) {
125 show(fm, TYPE_NORMAL, root, doc, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700126 }
127
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 public static void showSearch(
129 FragmentManager fm, RootInfo root, DocumentInfo doc, String query) {
130 show(fm, TYPE_SEARCH, root, doc, query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 }
132
133 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700134 show(fm, TYPE_RECENT_OPEN, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700135 }
136
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700137 private static void show(
138 FragmentManager fm, int type, RootInfo root, DocumentInfo doc, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700139 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700141 args.putParcelable(EXTRA_ROOT, root);
142 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700143 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700144
145 final DirectoryFragment fragment = new DirectoryFragment();
146 fragment.setArguments(args);
147
148 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700149 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700150 ft.commitAllowingStateLoss();
151 }
152
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700153 public static DirectoryFragment get(FragmentManager fm) {
154 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700155 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700156 }
157
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700158 @Override
159 public View onCreateView(
160 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
161 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700162 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
163
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700164 mEmptyView = view.findViewById(android.R.id.empty);
165
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700166 mListView = (ListView) view.findViewById(R.id.list);
167 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700168 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700169 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700170
171 mGridView = (GridView) view.findViewById(R.id.grid);
172 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700173 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700174 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700175
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700176 return view;
177 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700178
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700179 @Override
180 public void onActivityCreated(Bundle savedInstanceState) {
181 super.onActivityCreated(savedInstanceState);
182
183 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700184 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700185
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700186 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700187 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700188
Jeff Sharkey46899c82013-08-18 22:26:48 -0700189 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700190 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700191 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700192 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
193 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700194 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700195
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700196 Uri contentsUri;
197 switch (mType) {
198 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700199 contentsUri = DocumentsContract.buildChildDocumentsUri(
200 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700201 if (state.action == ACTION_MANAGE) {
202 contentsUri = DocumentsContract.setManageMode(contentsUri);
203 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700204 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700205 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700206 case TYPE_SEARCH:
207 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700208 doc.authority, doc.documentId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700209 if (state.action == ACTION_MANAGE) {
210 contentsUri = DocumentsContract.setManageMode(contentsUri);
211 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700212 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700213 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700214 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700215 final RootsCache roots = DocumentsApplication.getRootsCache(context);
216 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700217 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700218 default:
219 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700220 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700221 }
222
223 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700224 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700225 if (!isAdded()) return;
226
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700227 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700228
229 // Push latest state up to UI
230 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700231 if (result.mode != MODE_UNKNOWN) {
232 state.derivedMode = result.mode;
233 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700234 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700235 ((DocumentsActivity) context).onStateChanged();
236
237 updateDisplayState();
238
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700239 if (mLastSortOrder != state.derivedSortOrder) {
240 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700241 mListView.smoothScrollToPosition(0);
242 mGridView.smoothScrollToPosition(0);
243 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700244 }
245
246 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700247 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700248 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700249 }
250 };
251
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700252 // Kick off loader at least once
253 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
254
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700255 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700256 }
257
Jeff Sharkey42d26792013-09-06 13:22:09 -0700258 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700259 public void onResume() {
260 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700261 updateDisplayState();
262 }
263
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700264 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700265 // Sort order change always triggers reload; we'll trigger state change
266 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700267 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
268 }
269
270 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700271 final ContentResolver resolver = getActivity().getContentResolver();
272 final State state = getDisplayState(this);
273
274 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
275 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
276
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700277 if (root != null) {
278 final Uri stateUri = RecentsProvider.buildState(
279 root.authority, root.rootId, doc.documentId);
280 final ContentValues values = new ContentValues();
281 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700282
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700283 new AsyncTask<Void, Void, Void>() {
284 @Override
285 protected Void doInBackground(Void... params) {
286 resolver.insert(stateUri, values);
287 return null;
288 }
289 }.execute();
290 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700291
292 // Mode change is just visual change; no need to kick loader, and
293 // deliver change event immediately.
294 state.derivedMode = state.userMode;
295 ((DocumentsActivity) getActivity()).onStateChanged();
296
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700297 updateDisplayState();
298 }
299
300 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700301 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700302
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700303 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700304
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700305 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700306 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700307 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700308
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700309 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
310 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700311
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700312 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700313 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700314 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
315 } else {
316 choiceMode = ListView.CHOICE_MODE_NONE;
317 }
318
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700319 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700320 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700321 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700322 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700323 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700324 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700325 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700326 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700327 mGridView.setChoiceMode(choiceMode);
328 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700329 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700330 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700331 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700332 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700333 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700334 mListView.setChoiceMode(choiceMode);
335 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700336 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700337 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700338 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700339
340 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700341 }
342
343 private OnItemClickListener mItemListener = new OnItemClickListener() {
344 @Override
345 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700346 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700347 if (cursor != null) {
348 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
349 if (mFilter.apply(doc)) {
350 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
351 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700352 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700353 }
354 };
355
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700356 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
357 @Override
358 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
359 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
360 return true;
361 }
362
363 @Override
364 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700365 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700366
367 final MenuItem open = menu.findItem(R.id.menu_open);
368 final MenuItem share = menu.findItem(R.id.menu_share);
369 final MenuItem delete = menu.findItem(R.id.menu_delete);
370
371 final boolean manageMode = state.action == ACTION_MANAGE;
372 open.setVisible(!manageMode);
373 share.setVisible(manageMode);
374 delete.setVisible(manageMode);
375
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700376 return true;
377 }
378
379 @Override
380 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700381 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700382 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700383 final int size = checked.size();
384 for (int i = 0; i < size; i++) {
385 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700386 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700387 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700388 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700389 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700390 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700391
Jeff Sharkey873daa32013-08-18 17:38:20 -0700392 final int id = item.getItemId();
393 if (id == R.id.menu_open) {
394 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700395 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700396 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700397
398 } else if (id == R.id.menu_share) {
399 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700400 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700401 return true;
402
403 } else if (id == R.id.menu_delete) {
404 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700405 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700406 return true;
407
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700408 } else {
409 return false;
410 }
411 }
412
413 @Override
414 public void onDestroyActionMode(ActionMode mode) {
415 // ignored
416 }
417
418 @Override
419 public void onItemCheckedStateChanged(
420 ActionMode mode, int position, long id, boolean checked) {
421 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700422 // Directories and footer items cannot be checked
423 boolean valid = false;
424
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700425 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700426 if (cursor != null) {
427 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
428
429 // Only valid if non-directory matches filter
430 final State state = getDisplayState(DirectoryFragment.this);
431 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
432 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
433 }
434
435 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700436 mCurrentView.setItemChecked(position, false);
437 }
438 }
439
440 mode.setTitle(getResources()
441 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
442 }
443 };
444
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700445 private RecyclerListener mRecycleListener = new RecyclerListener() {
446 @Override
447 public void onMovedToScrapHeap(View view) {
448 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
449 if (iconThumb != null) {
450 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
451 if (oldTask != null) {
452 oldTask.reallyCancel();
453 iconThumb.setTag(null);
454 }
455 }
456 }
457 };
458
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700459 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700460 Intent intent;
461 if (docs.size() == 1) {
462 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700463
Jeff Sharkey873daa32013-08-18 17:38:20 -0700464 intent = new Intent(Intent.ACTION_SEND);
465 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
466 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700467 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700468 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700469
470 } else if (docs.size() > 1) {
471 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
472 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
473 intent.addCategory(Intent.CATEGORY_DEFAULT);
474
475 final ArrayList<String> mimeTypes = Lists.newArrayList();
476 final ArrayList<Uri> uris = Lists.newArrayList();
477 for (DocumentInfo doc : docs) {
478 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700479 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700480 }
481
482 intent.setType(findCommonMimeType(mimeTypes));
483 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
484
485 } else {
486 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700487 }
488
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700489 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700490 startActivity(intent);
491 }
492
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700493 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700494 final Context context = getActivity();
495 final ContentResolver resolver = context.getContentResolver();
496
497 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700498 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499 if (!doc.isDeleteSupported()) {
500 Log.w(TAG, "Skipping " + doc);
501 hadTrouble = true;
502 continue;
503 }
504
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700505 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700506 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700507 hadTrouble = true;
508 }
509 }
510
511 if (hadTrouble) {
512 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
513 }
514 }
515
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700516 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700517 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700518 }
519
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700520 private static abstract class Footer {
521 private final int mItemViewType;
522
523 public Footer(int itemViewType) {
524 mItemViewType = itemViewType;
525 }
526
527 public abstract View getView(View convertView, ViewGroup parent);
528
529 public int getItemViewType() {
530 return mItemViewType;
531 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700532 }
533
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700534 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700535 public LoadingFooter() {
536 super(1);
537 }
538
Jeff Sharkey20b32272013-09-03 15:25:52 -0700539 @Override
540 public View getView(View convertView, ViewGroup parent) {
541 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700542 final State state = getDisplayState(DirectoryFragment.this);
543
Jeff Sharkey20b32272013-09-03 15:25:52 -0700544 if (convertView == null) {
545 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700546 if (state.derivedMode == MODE_LIST) {
547 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
548 } else if (state.derivedMode == MODE_GRID) {
549 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
550 } else {
551 throw new IllegalStateException();
552 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700553 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700554
Jeff Sharkey20b32272013-09-03 15:25:52 -0700555 return convertView;
556 }
557 }
558
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700559 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700560 private final int mIcon;
561 private final String mMessage;
562
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700563 public MessageFooter(int itemViewType, int icon, String message) {
564 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700565 mIcon = icon;
566 mMessage = message;
567 }
568
569 @Override
570 public View getView(View convertView, ViewGroup parent) {
571 final Context context = parent.getContext();
572 final State state = getDisplayState(DirectoryFragment.this);
573
574 if (convertView == null) {
575 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700576 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700577 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700578 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700579 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
580 } else {
581 throw new IllegalStateException();
582 }
583 }
584
585 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
586 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
587 icon.setImageResource(mIcon);
588 title.setText(mMessage);
589 return convertView;
590 }
591 }
592
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700593 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700594 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700595 private int mCursorCount;
596
597 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700598
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700599 public void swapCursor(Cursor cursor) {
600 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700601 mCursorCount = cursor != null ? cursor.getCount() : 0;
602
603 mFooters.clear();
604
605 final Bundle extras = cursor != null ? cursor.getExtras() : null;
606 if (extras != null) {
607 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
608 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700609 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700610 }
611 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
612 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700613 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700614 }
615 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
616 mFooters.add(new LoadingFooter());
617 }
618 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700619
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700620 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700621 mEmptyView.setVisibility(View.VISIBLE);
622 } else {
623 mEmptyView.setVisibility(View.GONE);
624 }
625
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700626 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700627 }
628
629 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700630 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700631 if (position < mCursorCount) {
632 return getDocumentView(position, convertView, parent);
633 } else {
634 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700635 convertView = mFooters.get(position).getView(convertView, parent);
636 // Only the view itself is disabled; contents inside shouldn't
637 // be dimmed.
638 convertView.setEnabled(false);
639 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700640 }
641 }
642
643 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700644 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700645 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700646
Jeff Sharkey873daa32013-08-18 17:38:20 -0700647 final RootsCache roots = DocumentsApplication.getRootsCache(context);
648 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
649 context, mThumbSize);
650
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700651 if (convertView == null) {
652 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700653 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700654 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700655 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700656 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
657 } else {
658 throw new IllegalStateException();
659 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700660 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700661
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700662 final Cursor cursor = getItem(position);
663
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700664 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
665 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700666 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
667 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
668 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
669 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
670 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
671 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
672 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
673 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700674
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700675 final View icon = convertView.findViewById(android.R.id.icon);
676 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
677 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700678 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700679 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700680 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
681 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
682 final TextView date = (TextView) convertView.findViewById(R.id.date);
683 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700684
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700685 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700686 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700687 oldTask.reallyCancel();
688 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700689 }
690
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700691 iconMime.animate().cancel();
692 iconThumb.animate().cancel();
693
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700694 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700695 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700696 || MimePredicate.mimeMatches(LIST_THUMBNAIL_MIMES, docMimeType);
697
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700698 boolean cacheHit = false;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700699 if (supportsThumbnail && allowThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700700 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700701 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700702 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700703 iconThumb.setImageBitmap(cachedResult);
704 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700705 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700706 iconThumb.setImageDrawable(null);
707 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
708 uri, iconMime, iconThumb, mThumbSize);
709 iconThumb.setTag(task);
710 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700711 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700712 }
713
714 // Always throw MIME icon into place, even when a thumbnail is being
715 // loaded in background.
716 if (cacheHit) {
717 iconMime.setAlpha(0f);
718 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700719 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700720 iconMime.setAlpha(1f);
721 iconThumb.setAlpha(0f);
722 if (docIcon != 0) {
723 iconMime.setImageDrawable(
724 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
725 } else {
726 iconMime.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
727 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700728 }
729
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700730 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700731
Jeff Sharkey42d26792013-09-06 13:22:09 -0700732 boolean hasLine2 = false;
733
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700734 if (mType == TYPE_RECENT_OPEN) {
735 final RootInfo root = roots.getRoot(docAuthority, docRootId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700736 final Drawable iconDrawable = root.loadIcon(context);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700737 icon1.setVisibility(View.VISIBLE);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700738 icon1.setImageDrawable(iconDrawable);
739
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700740 if (summary != null) {
741 final boolean alwaysShowSummary = getResources()
742 .getBoolean(R.bool.always_show_summary);
743 if (alwaysShowSummary) {
744 summary.setText(root.getDirectoryString());
745 summary.setVisibility(View.VISIBLE);
746 hasLine2 = true;
747 } else {
748 if (iconDrawable != null && roots.isIconUnique(root)) {
749 // No summary needed if icon speaks for itself
750 summary.setVisibility(View.INVISIBLE);
751 } else {
752 summary.setText(root.getDirectoryString());
753 summary.setVisibility(View.VISIBLE);
754 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
755 hasLine2 = true;
756 }
757 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700758 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700759 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700760 icon1.setVisibility(View.GONE);
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700761 if (summary != null) {
762 if (docSummary != null) {
763 summary.setText(docSummary);
764 summary.setVisibility(View.VISIBLE);
765 hasLine2 = true;
766 } else {
767 summary.setVisibility(View.INVISIBLE);
768 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700769 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700770 }
771
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700772 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700773 date.setText(null);
774 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700775 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700776 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700777 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700778
779 if (state.showSize) {
780 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700781 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700782 size.setText(null);
783 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700784 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700785 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700786 }
787 } else {
788 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700789 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700790
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700791 if (line2 != null) {
792 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
793 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700794
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700795 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
796 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
797 if (enabled) {
798 setEnabledRecursive(convertView, true);
799 icon.setAlpha(1f);
800 icon1.setAlpha(1f);
801 } else {
802 setEnabledRecursive(convertView, false);
803 icon.setAlpha(0.5f);
804 icon1.setAlpha(0.5f);
805 }
806
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700807 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700808 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700809
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700810 @Override
811 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700812 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700813 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700814
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700815 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700816 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700817 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700818 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700819 return mCursor;
820 } else {
821 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700822 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700823 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700824
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700825 @Override
826 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700827 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700828 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700829
830 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700831 public int getViewTypeCount() {
832 return 4;
833 }
834
835 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700836 public int getItemViewType(int position) {
837 if (position < mCursorCount) {
838 return 0;
839 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700840 position -= mCursorCount;
841 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700842 }
843 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700844 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700845
846 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700847 private final Uri mUri;
848 private final ImageView mIconMime;
849 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700850 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700851 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700852
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700853 public ThumbnailAsyncTask(
854 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
855 mUri = uri;
856 mIconMime = iconMime;
857 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700858 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700859 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700860 }
861
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700862 public void reallyCancel() {
863 cancel(false);
864 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700865 }
866
867 @Override
868 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700869 final Context context = mIconThumb.getContext();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700870
871 Bitmap result = null;
872 try {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700873 // TODO: switch to using unstable provider
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700874 result = DocumentsContract.getDocumentThumbnail(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700875 context.getContentResolver(), mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700876 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700877 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
878 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700879 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700880 }
881 } catch (Exception e) {
882 Log.w(TAG, "Failed to load thumbnail: " + e);
883 }
884 return result;
885 }
886
887 @Override
888 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700889 if (mIconThumb.getTag() == this && result != null) {
890 mIconThumb.setTag(null);
891 mIconThumb.setImageBitmap(result);
892
893 mIconMime.setAlpha(1f);
894 mIconMime.animate().alpha(0f).start();
895 mIconThumb.setAlpha(0f);
896 mIconThumb.animate().alpha(1f).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700897 }
898 }
899 }
900
901 private static String formatTime(Context context, long when) {
902 // TODO: DateUtils should make this easier
903 Time then = new Time();
904 then.set(when);
905 Time now = new Time();
906 now.setToNow();
907
908 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
909 | DateUtils.FORMAT_ABBREV_ALL;
910
911 if (then.year != now.year) {
912 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
913 } else if (then.yearDay != now.yearDay) {
914 flags |= DateUtils.FORMAT_SHOW_DATE;
915 } else {
916 flags |= DateUtils.FORMAT_SHOW_TIME;
917 }
918
919 return DateUtils.formatDateTime(context, when, flags);
920 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700921
922 private String findCommonMimeType(List<String> mimeTypes) {
923 String[] commonType = mimeTypes.get(0).split("/");
924 if (commonType.length != 2) {
925 return "*/*";
926 }
927
928 for (int i = 1; i < mimeTypes.size(); i++) {
929 String[] type = mimeTypes.get(i).split("/");
930 if (type.length != 2) continue;
931
932 if (!commonType[1].equals(type[1])) {
933 commonType[1] = "*";
934 }
935
936 if (!commonType[0].equals(type[0])) {
937 commonType[0] = "*";
938 commonType[1] = "*";
939 break;
940 }
941 }
942
943 return commonType[0] + "/" + commonType[1];
944 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700945
946 private void setEnabledRecursive(View v, boolean enabled) {
947 if (v.isEnabled() == enabled) return;
948 v.setEnabled(enabled);
949
950 if (v instanceof ViewGroup) {
951 final ViewGroup vg = (ViewGroup) v;
952 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
953 setEnabledRecursive(vg.getChildAt(i), enabled);
954 }
955 }
956 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700957}