blob: aee865b3527154e17af6a14128610247c3de0a37 [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 Sharkey0e8c8712013-09-12 21:59:06 -0700128 public static void showSearch(FragmentManager fm, RootInfo root, String query) {
129 show(fm, TYPE_SEARCH, root, null, query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700130 }
131
132 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700133 show(fm, TYPE_RECENT_OPEN, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134 }
135
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700136 private static void show(
137 FragmentManager fm, int type, RootInfo root, DocumentInfo doc, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700138 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700139 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700140 args.putParcelable(EXTRA_ROOT, root);
141 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700142 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700143
144 final DirectoryFragment fragment = new DirectoryFragment();
145 fragment.setArguments(args);
146
147 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700148 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700149 ft.commitAllowingStateLoss();
150 }
151
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700152 public static DirectoryFragment get(FragmentManager fm) {
153 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700154 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700155 }
156
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700157 @Override
158 public View onCreateView(
159 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
160 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700161 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
162
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700163 mEmptyView = view.findViewById(android.R.id.empty);
164
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700165 mListView = (ListView) view.findViewById(R.id.list);
166 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700167 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700168 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700169
170 mGridView = (GridView) view.findViewById(R.id.grid);
171 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700172 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700173 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700174
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700175 return view;
176 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700177
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700178 @Override
179 public void onActivityCreated(Bundle savedInstanceState) {
180 super.onActivityCreated(savedInstanceState);
181
182 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700183 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700184
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 Sharkey46899c82013-08-18 22:26:48 -0700188 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700189 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700190 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700191 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
192 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700193 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700194
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700195 Uri contentsUri;
196 switch (mType) {
197 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700198 contentsUri = DocumentsContract.buildChildDocumentsUri(
199 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700200 if (state.action == ACTION_MANAGE) {
201 contentsUri = DocumentsContract.setManageMode(contentsUri);
202 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700203 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700204 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700205 case TYPE_SEARCH:
206 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700207 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700208 if (state.action == ACTION_MANAGE) {
209 contentsUri = DocumentsContract.setManageMode(contentsUri);
210 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700211 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700212 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700213 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700214 final RootsCache roots = DocumentsApplication.getRootsCache(context);
215 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700216 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700217 default:
218 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700219 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700220 }
221
222 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700223 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700224 if (!isAdded()) return;
225
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700226 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700227
228 // Push latest state up to UI
229 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700230 if (result.mode != MODE_UNKNOWN) {
231 state.derivedMode = result.mode;
232 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700233 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700234 ((DocumentsActivity) context).onStateChanged();
235
236 updateDisplayState();
237
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700238 if (mLastSortOrder != state.derivedSortOrder) {
239 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700240 mListView.smoothScrollToPosition(0);
241 mGridView.smoothScrollToPosition(0);
242 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700243 }
244
245 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700246 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700247 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700248 }
249 };
250
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700251 // Kick off loader at least once
252 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
253
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700254 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700255 }
256
Jeff Sharkey42d26792013-09-06 13:22:09 -0700257 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700258 public void onResume() {
259 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700260 updateDisplayState();
261 }
262
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700263 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700264 // Sort order change always triggers reload; we'll trigger state change
265 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700266 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
267 }
268
269 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700270 final ContentResolver resolver = getActivity().getContentResolver();
271 final State state = getDisplayState(this);
272
273 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
274 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
275
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700276 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700277 final Uri stateUri = RecentsProvider.buildState(
278 root.authority, root.rootId, doc.documentId);
279 final ContentValues values = new ContentValues();
280 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700281
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700282 new AsyncTask<Void, Void, Void>() {
283 @Override
284 protected Void doInBackground(Void... params) {
285 resolver.insert(stateUri, values);
286 return null;
287 }
288 }.execute();
289 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700290
291 // Mode change is just visual change; no need to kick loader, and
292 // deliver change event immediately.
293 state.derivedMode = state.userMode;
294 ((DocumentsActivity) getActivity()).onStateChanged();
295
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700296 updateDisplayState();
297 }
298
299 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700300 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700301
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700302 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700303
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700304 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700305 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700306 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700307
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700308 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
309 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700310
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700311 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700312 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700313 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
314 } else {
315 choiceMode = ListView.CHOICE_MODE_NONE;
316 }
317
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700318 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700319 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700320 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700321 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700322 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700323 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700324 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700325 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700326 mGridView.setChoiceMode(choiceMode);
327 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700328 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700329 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700330 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700331 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700332 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700333 mListView.setChoiceMode(choiceMode);
334 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700335 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700336 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700337 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700338
339 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700340 }
341
342 private OnItemClickListener mItemListener = new OnItemClickListener() {
343 @Override
344 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700345 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700346 if (cursor != null) {
347 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
348 if (mFilter.apply(doc)) {
349 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
350 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700351 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700352 }
353 };
354
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700355 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
356 @Override
357 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
358 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
359 return true;
360 }
361
362 @Override
363 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700364 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700365
366 final MenuItem open = menu.findItem(R.id.menu_open);
367 final MenuItem share = menu.findItem(R.id.menu_share);
368 final MenuItem delete = menu.findItem(R.id.menu_delete);
369
370 final boolean manageMode = state.action == ACTION_MANAGE;
371 open.setVisible(!manageMode);
372 share.setVisible(manageMode);
373 delete.setVisible(manageMode);
374
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700375 return true;
376 }
377
378 @Override
379 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700380 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700381 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700382 final int size = checked.size();
383 for (int i = 0; i < size; i++) {
384 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700385 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700386 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700387 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700388 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700389 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700390
Jeff Sharkey873daa32013-08-18 17:38:20 -0700391 final int id = item.getItemId();
392 if (id == R.id.menu_open) {
393 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700394 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700395 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700396
397 } else if (id == R.id.menu_share) {
398 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700399 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700400 return true;
401
402 } else if (id == R.id.menu_delete) {
403 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700404 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700405 return true;
406
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700407 } else {
408 return false;
409 }
410 }
411
412 @Override
413 public void onDestroyActionMode(ActionMode mode) {
414 // ignored
415 }
416
417 @Override
418 public void onItemCheckedStateChanged(
419 ActionMode mode, int position, long id, boolean checked) {
420 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700421 // Directories and footer items cannot be checked
422 boolean valid = false;
423
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700424 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700425 if (cursor != null) {
426 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
427
428 // Only valid if non-directory matches filter
429 final State state = getDisplayState(DirectoryFragment.this);
430 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
431 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
432 }
433
434 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700435 mCurrentView.setItemChecked(position, false);
436 }
437 }
438
439 mode.setTitle(getResources()
440 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
441 }
442 };
443
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700444 private RecyclerListener mRecycleListener = new RecyclerListener() {
445 @Override
446 public void onMovedToScrapHeap(View view) {
447 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
448 if (iconThumb != null) {
449 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
450 if (oldTask != null) {
451 oldTask.reallyCancel();
452 iconThumb.setTag(null);
453 }
454 }
455 }
456 };
457
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700458 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700459 Intent intent;
460 if (docs.size() == 1) {
461 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700462
Jeff Sharkey873daa32013-08-18 17:38:20 -0700463 intent = new Intent(Intent.ACTION_SEND);
464 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
465 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700466 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700467 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700468
469 } else if (docs.size() > 1) {
470 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
471 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
472 intent.addCategory(Intent.CATEGORY_DEFAULT);
473
474 final ArrayList<String> mimeTypes = Lists.newArrayList();
475 final ArrayList<Uri> uris = Lists.newArrayList();
476 for (DocumentInfo doc : docs) {
477 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700478 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700479 }
480
481 intent.setType(findCommonMimeType(mimeTypes));
482 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
483
484 } else {
485 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700486 }
487
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700488 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700489 startActivity(intent);
490 }
491
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700492 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700493 final Context context = getActivity();
494 final ContentResolver resolver = context.getContentResolver();
495
496 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700497 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700498 if (!doc.isDeleteSupported()) {
499 Log.w(TAG, "Skipping " + doc);
500 hadTrouble = true;
501 continue;
502 }
503
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700504 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700505 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700506 hadTrouble = true;
507 }
508 }
509
510 if (hadTrouble) {
511 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
512 }
513 }
514
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700515 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700516 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700517 }
518
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700519 private static abstract class Footer {
520 private final int mItemViewType;
521
522 public Footer(int itemViewType) {
523 mItemViewType = itemViewType;
524 }
525
526 public abstract View getView(View convertView, ViewGroup parent);
527
528 public int getItemViewType() {
529 return mItemViewType;
530 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700531 }
532
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700533 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700534 public LoadingFooter() {
535 super(1);
536 }
537
Jeff Sharkey20b32272013-09-03 15:25:52 -0700538 @Override
539 public View getView(View convertView, ViewGroup parent) {
540 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700541 final State state = getDisplayState(DirectoryFragment.this);
542
Jeff Sharkey20b32272013-09-03 15:25:52 -0700543 if (convertView == null) {
544 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700545 if (state.derivedMode == MODE_LIST) {
546 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
547 } else if (state.derivedMode == MODE_GRID) {
548 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
549 } else {
550 throw new IllegalStateException();
551 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700552 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700553
Jeff Sharkey20b32272013-09-03 15:25:52 -0700554 return convertView;
555 }
556 }
557
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700558 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700559 private final int mIcon;
560 private final String mMessage;
561
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700562 public MessageFooter(int itemViewType, int icon, String message) {
563 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700564 mIcon = icon;
565 mMessage = message;
566 }
567
568 @Override
569 public View getView(View convertView, ViewGroup parent) {
570 final Context context = parent.getContext();
571 final State state = getDisplayState(DirectoryFragment.this);
572
573 if (convertView == null) {
574 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700575 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700576 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700577 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700578 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
579 } else {
580 throw new IllegalStateException();
581 }
582 }
583
584 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
585 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
586 icon.setImageResource(mIcon);
587 title.setText(mMessage);
588 return convertView;
589 }
590 }
591
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700592 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700593 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700594 private int mCursorCount;
595
596 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700597
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700598 public void swapCursor(Cursor cursor) {
599 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700600 mCursorCount = cursor != null ? cursor.getCount() : 0;
601
602 mFooters.clear();
603
604 final Bundle extras = cursor != null ? cursor.getExtras() : null;
605 if (extras != null) {
606 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
607 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700608 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700609 }
610 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
611 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700612 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700613 }
614 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
615 mFooters.add(new LoadingFooter());
616 }
617 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700618
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700619 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700620 mEmptyView.setVisibility(View.VISIBLE);
621 } else {
622 mEmptyView.setVisibility(View.GONE);
623 }
624
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700625 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700626 }
627
628 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700629 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700630 if (position < mCursorCount) {
631 return getDocumentView(position, convertView, parent);
632 } else {
633 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700634 convertView = mFooters.get(position).getView(convertView, parent);
635 // Only the view itself is disabled; contents inside shouldn't
636 // be dimmed.
637 convertView.setEnabled(false);
638 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700639 }
640 }
641
642 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700643 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700644 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700645
Jeff Sharkey873daa32013-08-18 17:38:20 -0700646 final RootsCache roots = DocumentsApplication.getRootsCache(context);
647 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
648 context, mThumbSize);
649
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700650 if (convertView == null) {
651 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700652 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700653 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700654 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700655 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
656 } else {
657 throw new IllegalStateException();
658 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700659 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700660
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700661 final Cursor cursor = getItem(position);
662
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700663 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
664 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700665 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
666 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
667 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
668 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
669 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
670 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
671 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
672 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700673
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700674 final View icon = convertView.findViewById(android.R.id.icon);
675 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
676 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700677 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700678 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700679 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
680 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
681 final TextView date = (TextView) convertView.findViewById(R.id.date);
682 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700683
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700684 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700685 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700686 oldTask.reallyCancel();
687 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700688 }
689
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700690 iconMime.animate().cancel();
691 iconThumb.animate().cancel();
692
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700693 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700694 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700695 || MimePredicate.mimeMatches(LIST_THUMBNAIL_MIMES, docMimeType);
696
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700697 boolean cacheHit = false;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700698 if (supportsThumbnail && allowThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700699 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700700 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700701 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700702 iconThumb.setImageBitmap(cachedResult);
703 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700704 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700705 iconThumb.setImageDrawable(null);
706 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
707 uri, iconMime, iconThumb, mThumbSize);
708 iconThumb.setTag(task);
709 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700710 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700711 }
712
713 // Always throw MIME icon into place, even when a thumbnail is being
714 // loaded in background.
715 if (cacheHit) {
716 iconMime.setAlpha(0f);
717 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700718 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700719 iconMime.setAlpha(1f);
720 iconThumb.setAlpha(0f);
721 if (docIcon != 0) {
722 iconMime.setImageDrawable(
723 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
724 } else {
725 iconMime.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
726 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700727 }
728
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700729 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700730
Jeff Sharkey42d26792013-09-06 13:22:09 -0700731 boolean hasLine2 = false;
732
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700733 if (mType == TYPE_RECENT_OPEN) {
734 final RootInfo root = roots.getRoot(docAuthority, docRootId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700735 final Drawable iconDrawable = root.loadIcon(context);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700736 icon1.setVisibility(View.VISIBLE);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700737 icon1.setImageDrawable(iconDrawable);
738
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700739 if (summary != null) {
740 final boolean alwaysShowSummary = getResources()
741 .getBoolean(R.bool.always_show_summary);
742 if (alwaysShowSummary) {
743 summary.setText(root.getDirectoryString());
744 summary.setVisibility(View.VISIBLE);
745 hasLine2 = true;
746 } else {
747 if (iconDrawable != null && roots.isIconUnique(root)) {
748 // No summary needed if icon speaks for itself
749 summary.setVisibility(View.INVISIBLE);
750 } else {
751 summary.setText(root.getDirectoryString());
752 summary.setVisibility(View.VISIBLE);
753 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
754 hasLine2 = true;
755 }
756 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700757 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700758 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700759 icon1.setVisibility(View.GONE);
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700760 if (summary != null) {
761 if (docSummary != null) {
762 summary.setText(docSummary);
763 summary.setVisibility(View.VISIBLE);
764 hasLine2 = true;
765 } else {
766 summary.setVisibility(View.INVISIBLE);
767 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700768 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700769 }
770
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700771 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700772 date.setText(null);
773 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700774 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700775 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700776 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700777
778 if (state.showSize) {
779 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700780 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700781 size.setText(null);
782 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700783 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700784 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700785 }
786 } else {
787 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700788 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700789
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700790 if (line2 != null) {
791 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
792 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700793
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700794 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
795 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
796 if (enabled) {
797 setEnabledRecursive(convertView, true);
798 icon.setAlpha(1f);
799 icon1.setAlpha(1f);
800 } else {
801 setEnabledRecursive(convertView, false);
802 icon.setAlpha(0.5f);
803 icon1.setAlpha(0.5f);
804 }
805
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700806 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700807 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700808
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700809 @Override
810 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700811 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700812 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700813
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700814 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700815 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700816 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700817 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700818 return mCursor;
819 } else {
820 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700821 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700822 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700823
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700824 @Override
825 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700826 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700827 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700828
829 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700830 public int getViewTypeCount() {
831 return 4;
832 }
833
834 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700835 public int getItemViewType(int position) {
836 if (position < mCursorCount) {
837 return 0;
838 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700839 position -= mCursorCount;
840 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700841 }
842 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700843 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700844
845 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700846 private final Uri mUri;
847 private final ImageView mIconMime;
848 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700849 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700850 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700851
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700852 public ThumbnailAsyncTask(
853 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
854 mUri = uri;
855 mIconMime = iconMime;
856 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700857 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700858 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700859 }
860
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700861 public void reallyCancel() {
862 cancel(false);
863 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700864 }
865
866 @Override
867 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700868 final Context context = mIconThumb.getContext();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700869
870 Bitmap result = null;
871 try {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700872 // TODO: switch to using unstable provider
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700873 result = DocumentsContract.getDocumentThumbnail(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700874 context.getContentResolver(), mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700875 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700876 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
877 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700878 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700879 }
880 } catch (Exception e) {
881 Log.w(TAG, "Failed to load thumbnail: " + e);
882 }
883 return result;
884 }
885
886 @Override
887 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700888 if (mIconThumb.getTag() == this && result != null) {
889 mIconThumb.setTag(null);
890 mIconThumb.setImageBitmap(result);
891
892 mIconMime.setAlpha(1f);
893 mIconMime.animate().alpha(0f).start();
894 mIconThumb.setAlpha(0f);
895 mIconThumb.animate().alpha(1f).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700896 }
897 }
898 }
899
900 private static String formatTime(Context context, long when) {
901 // TODO: DateUtils should make this easier
902 Time then = new Time();
903 then.set(when);
904 Time now = new Time();
905 now.setToNow();
906
907 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
908 | DateUtils.FORMAT_ABBREV_ALL;
909
910 if (then.year != now.year) {
911 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
912 } else if (then.yearDay != now.yearDay) {
913 flags |= DateUtils.FORMAT_SHOW_DATE;
914 } else {
915 flags |= DateUtils.FORMAT_SHOW_TIME;
916 }
917
918 return DateUtils.formatDateTime(context, when, flags);
919 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700920
921 private String findCommonMimeType(List<String> mimeTypes) {
922 String[] commonType = mimeTypes.get(0).split("/");
923 if (commonType.length != 2) {
924 return "*/*";
925 }
926
927 for (int i = 1; i < mimeTypes.size(); i++) {
928 String[] type = mimeTypes.get(i).split("/");
929 if (type.length != 2) continue;
930
931 if (!commonType[1].equals(type[1])) {
932 commonType[1] = "*";
933 }
934
935 if (!commonType[0].equals(type[0])) {
936 commonType[0] = "*";
937 commonType[1] = "*";
938 break;
939 }
940 }
941
942 return commonType[0] + "/" + commonType[1];
943 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700944
945 private void setEnabledRecursive(View v, boolean enabled) {
946 if (v.isEnabled() == enabled) return;
947 v.setEnabled(enabled);
948
949 if (v instanceof ViewGroup) {
950 final ViewGroup vg = (ViewGroup) v;
951 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
952 setEnabledRecursive(vg.getChildAt(i), enabled);
953 }
954 }
955 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700956}