blob: f9ac3f344d61e111f2859d155c31b597f344ec65 [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 Sharkeye22d02e2013-04-26 16:54:55 -070034import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070035import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070037import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070038import android.graphics.Bitmap;
39import android.graphics.Point;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070041import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.os.Bundle;
43import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070044import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070045import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070046import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070047import android.text.format.Time;
48import android.util.Log;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070049import android.util.SparseBooleanArray;
50import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070052import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070053import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.view.View;
55import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070056import android.widget.AbsListView;
57import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070058import android.widget.AdapterView;
59import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070060import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070061import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.widget.ImageView;
63import android.widget.ListView;
64import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070065import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070066
Jeff Sharkeyb3620442013-09-01 18:41:04 -070067import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070068import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070069import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070070import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070071import com.google.android.collect.Lists;
72
73import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070074import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070075import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070076
77/**
78 * Display the documents inside a single directory.
79 */
80public class DirectoryFragment extends Fragment {
81
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070082 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070083 private ListView mListView;
84 private GridView mGridView;
85
Jeff Sharkeyc317af82013-07-01 16:56:54 -070086 private AbsListView mCurrentView;
87
Jeff Sharkey724deeb2013-08-31 15:02:20 -070088 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070089
Jeff Sharkeya5defe32013-08-05 17:56:48 -070090 public static final int TYPE_NORMAL = 1;
91 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070092 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -070093
94 private int mType = TYPE_NORMAL;
95
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070096 private int mLastMode = MODE_UNKNOWN;
97 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
98
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070099 private Point mThumbSize;
100
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700101 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700102 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700103
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700104 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700105 private static final String EXTRA_ROOT = "root";
106 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700107 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700108
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700109 /**
110 * MIME types that should always show thumbnails in list mode.
111 */
112 private static final String[] LIST_THUMBNAIL_MIMES = new String[] { "image/*", "video/*" };
113
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700114 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700115
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700116 private final int mLoaderId = sLoaderId.incrementAndGet();
117
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700118 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc) {
119 show(fm, TYPE_NORMAL, root, doc, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700120 }
121
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700122 public static void showSearch(
123 FragmentManager fm, RootInfo root, DocumentInfo doc, String query) {
124 show(fm, TYPE_SEARCH, root, doc, query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700125 }
126
127 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 show(fm, TYPE_RECENT_OPEN, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700129 }
130
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700131 private static void show(
132 FragmentManager fm, int type, RootInfo root, DocumentInfo doc, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700135 args.putParcelable(EXTRA_ROOT, root);
136 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700137 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700138
139 final DirectoryFragment fragment = new DirectoryFragment();
140 fragment.setArguments(args);
141
142 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700143 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700144 ft.commitAllowingStateLoss();
145 }
146
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700147 public static DirectoryFragment get(FragmentManager fm) {
148 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700149 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700150 }
151
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700152 @Override
153 public View onCreateView(
154 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
155 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700156 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
157
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700158 mEmptyView = view.findViewById(android.R.id.empty);
159
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700160 mListView = (ListView) view.findViewById(R.id.list);
161 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700162 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700163
164 mGridView = (GridView) view.findViewById(R.id.grid);
165 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700166 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700167
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700168 return view;
169 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700170
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700171 @Override
172 public void onActivityCreated(Bundle savedInstanceState) {
173 super.onActivityCreated(savedInstanceState);
174
175 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700176 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700177
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700178 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700179 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700180
Jeff Sharkey46899c82013-08-18 22:26:48 -0700181 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700182 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700183 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700184 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
185 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700186 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700187
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700188 Uri contentsUri;
189 switch (mType) {
190 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700191 contentsUri = DocumentsContract.buildChildDocumentsUri(
192 doc.authority, doc.documentId);
193 return new DirectoryLoader(context, root, doc, contentsUri);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700194 case TYPE_SEARCH:
195 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700196 doc.authority, doc.documentId, query);
197 return new DirectoryLoader(context, root, doc, contentsUri);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700198 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700199 final RootsCache roots = DocumentsApplication.getRootsCache(context);
200 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700201 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700202 default:
203 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700204 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700205 }
206
207 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700208 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700209 if (!isAdded()) return;
210
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700211 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700212
213 // Push latest state up to UI
214 // TODO: if mode change was racing with us, don't overwrite it
215 state.mode = result.mode;
216 state.sortOrder = result.sortOrder;
217 ((DocumentsActivity) context).onStateChanged();
218
219 updateDisplayState();
220
221 if (mLastSortOrder != result.sortOrder) {
222 mLastSortOrder = result.sortOrder;
223 mListView.smoothScrollToPosition(0);
224 mGridView.smoothScrollToPosition(0);
225 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700226 }
227
228 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700229 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700230 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700231 }
232 };
233
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700234 // Kick off loader at least once
235 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
236
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700237 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700238 }
239
Jeff Sharkey42d26792013-09-06 13:22:09 -0700240 @Override
241 public void onStart() {
242 super.onStart();
243 updateDisplayState();
244 }
245
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700246 public void onUserSortOrderChanged() {
247 // User change always triggers reload
248 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
249 }
250
251 public void onUserModeChanged() {
252 // Mode change is just display; no need to reload
253 updateDisplayState();
254 }
255
256 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700257 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700258
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700259 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700260
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700261 if (mLastMode == state.mode) return;
262 mLastMode = state.mode;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700263
Jeff Sharkey873daa32013-08-18 17:38:20 -0700264 mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE);
265 mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700266
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700267 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700268 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700269 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
270 } else {
271 choiceMode = ListView.CHOICE_MODE_NONE;
272 }
273
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700274 final int thumbSize;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700275 if (state.mode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700276 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700277 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700278 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700279 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700280 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700281 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700282 mGridView.setChoiceMode(choiceMode);
283 mCurrentView = mGridView;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700284 } else if (state.mode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700285 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700286 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700287 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700288 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700289 mListView.setChoiceMode(choiceMode);
290 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700291 } else {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700292 throw new IllegalStateException("Unknown state " + state.mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700293 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700294
295 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700296 }
297
298 private OnItemClickListener mItemListener = new OnItemClickListener() {
299 @Override
300 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700301 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700302 if (cursor != null) {
303 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
304 if (mFilter.apply(doc)) {
305 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
306 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700307 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700308 }
309 };
310
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700311 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
312 @Override
313 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
314 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
315 return true;
316 }
317
318 @Override
319 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700320 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700321
322 final MenuItem open = menu.findItem(R.id.menu_open);
323 final MenuItem share = menu.findItem(R.id.menu_share);
324 final MenuItem delete = menu.findItem(R.id.menu_delete);
325
326 final boolean manageMode = state.action == ACTION_MANAGE;
327 open.setVisible(!manageMode);
328 share.setVisible(manageMode);
329 delete.setVisible(manageMode);
330
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700331 return true;
332 }
333
334 @Override
335 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700336 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700337 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700338 final int size = checked.size();
339 for (int i = 0; i < size; i++) {
340 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700341 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700342 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700343 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700344 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700345 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700346
Jeff Sharkey873daa32013-08-18 17:38:20 -0700347 final int id = item.getItemId();
348 if (id == R.id.menu_open) {
349 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700350 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700351 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700352
353 } else if (id == R.id.menu_share) {
354 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700355 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700356 return true;
357
358 } else if (id == R.id.menu_delete) {
359 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700360 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700361 return true;
362
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700363 } else {
364 return false;
365 }
366 }
367
368 @Override
369 public void onDestroyActionMode(ActionMode mode) {
370 // ignored
371 }
372
373 @Override
374 public void onItemCheckedStateChanged(
375 ActionMode mode, int position, long id, boolean checked) {
376 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700377 // Directories and footer items cannot be checked
378 boolean valid = false;
379
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700380 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700381 if (cursor != null) {
382 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
383
384 // Only valid if non-directory matches filter
385 final State state = getDisplayState(DirectoryFragment.this);
386 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
387 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
388 }
389
390 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700391 mCurrentView.setItemChecked(position, false);
392 }
393 }
394
395 mode.setTitle(getResources()
396 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
397 }
398 };
399
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700400 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700401 Intent intent;
402 if (docs.size() == 1) {
403 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700404
Jeff Sharkey873daa32013-08-18 17:38:20 -0700405 intent = new Intent(Intent.ACTION_SEND);
406 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
407 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700408 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700409 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700410
411 } else if (docs.size() > 1) {
412 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
413 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
414 intent.addCategory(Intent.CATEGORY_DEFAULT);
415
416 final ArrayList<String> mimeTypes = Lists.newArrayList();
417 final ArrayList<Uri> uris = Lists.newArrayList();
418 for (DocumentInfo doc : docs) {
419 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700420 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700421 }
422
423 intent.setType(findCommonMimeType(mimeTypes));
424 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
425
426 } else {
427 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700428 }
429
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700430 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700431 startActivity(intent);
432 }
433
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700434 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700435 final Context context = getActivity();
436 final ContentResolver resolver = context.getContentResolver();
437
438 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700439 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700440 if (!doc.isDeleteSupported()) {
441 Log.w(TAG, "Skipping " + doc);
442 hadTrouble = true;
443 continue;
444 }
445
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700446 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700447 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700448 hadTrouble = true;
449 }
450 }
451
452 if (hadTrouble) {
453 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
454 }
455 }
456
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700457 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700458 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700459 }
460
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700461 private static abstract class Footer {
462 private final int mItemViewType;
463
464 public Footer(int itemViewType) {
465 mItemViewType = itemViewType;
466 }
467
468 public abstract View getView(View convertView, ViewGroup parent);
469
470 public int getItemViewType() {
471 return mItemViewType;
472 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700473 }
474
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700475 private static class LoadingFooter extends Footer {
476 public LoadingFooter() {
477 super(1);
478 }
479
Jeff Sharkey20b32272013-09-03 15:25:52 -0700480 @Override
481 public View getView(View convertView, ViewGroup parent) {
482 final Context context = parent.getContext();
483 if (convertView == null) {
484 final LayoutInflater inflater = LayoutInflater.from(context);
485 convertView = inflater.inflate(R.layout.item_loading, parent, false);
486 }
487 return convertView;
488 }
489 }
490
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700491 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700492 private final int mIcon;
493 private final String mMessage;
494
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700495 public MessageFooter(int itemViewType, int icon, String message) {
496 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700497 mIcon = icon;
498 mMessage = message;
499 }
500
501 @Override
502 public View getView(View convertView, ViewGroup parent) {
503 final Context context = parent.getContext();
504 final State state = getDisplayState(DirectoryFragment.this);
505
506 if (convertView == null) {
507 final LayoutInflater inflater = LayoutInflater.from(context);
508 if (state.mode == MODE_LIST) {
509 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
510 } else if (state.mode == MODE_GRID) {
511 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
512 } else {
513 throw new IllegalStateException();
514 }
515 }
516
517 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
518 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
519 icon.setImageResource(mIcon);
520 title.setText(mMessage);
521 return convertView;
522 }
523 }
524
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700525 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700526 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700527 private int mCursorCount;
528
529 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700530
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700531 public void swapCursor(Cursor cursor) {
532 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700533 mCursorCount = cursor != null ? cursor.getCount() : 0;
534
535 mFooters.clear();
536
537 final Bundle extras = cursor != null ? cursor.getExtras() : null;
538 if (extras != null) {
539 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
540 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700541 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700542 }
543 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
544 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700545 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700546 }
547 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
548 mFooters.add(new LoadingFooter());
549 }
550 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700551
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700552 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700553 mEmptyView.setVisibility(View.VISIBLE);
554 } else {
555 mEmptyView.setVisibility(View.GONE);
556 }
557
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700558 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700559 }
560
561 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700562 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700563 if (position < mCursorCount) {
564 return getDocumentView(position, convertView, parent);
565 } else {
566 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700567 convertView = mFooters.get(position).getView(convertView, parent);
568 // Only the view itself is disabled; contents inside shouldn't
569 // be dimmed.
570 convertView.setEnabled(false);
571 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700572 }
573 }
574
575 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700576 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700577 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700578
Jeff Sharkey873daa32013-08-18 17:38:20 -0700579 final RootsCache roots = DocumentsApplication.getRootsCache(context);
580 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
581 context, mThumbSize);
582
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700583 if (convertView == null) {
584 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700585 if (state.mode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700586 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700587 } else if (state.mode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700588 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
589 } else {
590 throw new IllegalStateException();
591 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700592 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700593
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700594 final Cursor cursor = getItem(position);
595
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700596 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
597 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700598 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
599 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
600 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
601 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
602 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
603 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
604 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
605 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700606
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700607 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700608 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700609 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700610 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
611 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
612 final TextView date = (TextView) convertView.findViewById(R.id.date);
613 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700614
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700615 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag();
616 if (oldTask != null) {
617 oldTask.cancel(false);
618 }
619
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700620 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
621 final boolean allowThumbnail = (state.mode == MODE_GRID)
622 || MimePredicate.mimeMatches(LIST_THUMBNAIL_MIMES, docMimeType);
623
624 if (supportsThumbnail && allowThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700625 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700626 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700627 if (cachedResult != null) {
628 icon.setImageBitmap(cachedResult);
629 } else {
630 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize);
631 icon.setImageBitmap(null);
632 icon.setTag(task);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700633 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700634 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700635 } else if (docIcon != 0) {
Jeff Sharkey5629ec52013-09-04 18:03:18 -0700636 icon.setImageDrawable(IconUtils.loadPackageIcon(context, docAuthority, docIcon));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700637 } else {
Jeff Sharkey5629ec52013-09-04 18:03:18 -0700638 icon.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700639 }
640
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700641 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700642
Jeff Sharkey42d26792013-09-06 13:22:09 -0700643 boolean hasLine2 = false;
644
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700645 if (mType == TYPE_RECENT_OPEN) {
646 final RootInfo root = roots.getRoot(docAuthority, docRootId);
647 icon1.setVisibility(View.VISIBLE);
648 icon1.setImageDrawable(root.loadIcon(context));
649 summary.setText(root.getDirectoryString());
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700650 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700651 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
652 hasLine2 = true;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700653 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700654 icon1.setVisibility(View.GONE);
655 if (docSummary != null) {
656 summary.setText(docSummary);
657 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700658 hasLine2 = true;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700659 } else {
660 summary.setVisibility(View.INVISIBLE);
661 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700662 }
663
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700664 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700665 date.setText(null);
666 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700667 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700668 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700669 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700670
671 if (state.showSize) {
672 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700673 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700674 size.setText(null);
675 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700676 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700677 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700678 }
679 } else {
680 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700681 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700682
Jeff Sharkey42d26792013-09-06 13:22:09 -0700683 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
684
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700685 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
686 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
687 if (enabled) {
688 setEnabledRecursive(convertView, true);
689 icon.setAlpha(1f);
690 icon1.setAlpha(1f);
691 } else {
692 setEnabledRecursive(convertView, false);
693 icon.setAlpha(0.5f);
694 icon1.setAlpha(0.5f);
695 }
696
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700697 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700698 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700699
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700700 @Override
701 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700702 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700703 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700704
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700705 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700706 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700707 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700708 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700709 return mCursor;
710 } else {
711 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700712 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700713 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700714
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700715 @Override
716 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700717 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700718 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700719
720 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700721 public int getViewTypeCount() {
722 return 4;
723 }
724
725 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700726 public int getItemViewType(int position) {
727 if (position < mCursorCount) {
728 return 0;
729 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700730 position -= mCursorCount;
731 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700732 }
733 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700734 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700735
736 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
737 private final ImageView mTarget;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700738 private final Point mThumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700739
Jeff Sharkey873daa32013-08-18 17:38:20 -0700740 public ThumbnailAsyncTask(ImageView target, Point thumbSize) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700741 mTarget = target;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700742 mThumbSize = thumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700743 }
744
745 @Override
746 protected void onPreExecute() {
747 mTarget.setTag(this);
748 }
749
750 @Override
751 protected Bitmap doInBackground(Uri... params) {
752 final Context context = mTarget.getContext();
753 final Uri uri = params[0];
754
755 Bitmap result = null;
756 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700757 result = DocumentsContract.getDocumentThumbnail(
758 context.getContentResolver(), uri, mThumbSize, null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700759 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700760 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
761 context, mThumbSize);
762 thumbs.put(uri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700763 }
764 } catch (Exception e) {
765 Log.w(TAG, "Failed to load thumbnail: " + e);
766 }
767 return result;
768 }
769
770 @Override
771 protected void onPostExecute(Bitmap result) {
772 if (mTarget.getTag() == this) {
773 mTarget.setImageBitmap(result);
774 mTarget.setTag(null);
775 }
776 }
777 }
778
779 private static String formatTime(Context context, long when) {
780 // TODO: DateUtils should make this easier
781 Time then = new Time();
782 then.set(when);
783 Time now = new Time();
784 now.setToNow();
785
786 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
787 | DateUtils.FORMAT_ABBREV_ALL;
788
789 if (then.year != now.year) {
790 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
791 } else if (then.yearDay != now.yearDay) {
792 flags |= DateUtils.FORMAT_SHOW_DATE;
793 } else {
794 flags |= DateUtils.FORMAT_SHOW_TIME;
795 }
796
797 return DateUtils.formatDateTime(context, when, flags);
798 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700799
800 private String findCommonMimeType(List<String> mimeTypes) {
801 String[] commonType = mimeTypes.get(0).split("/");
802 if (commonType.length != 2) {
803 return "*/*";
804 }
805
806 for (int i = 1; i < mimeTypes.size(); i++) {
807 String[] type = mimeTypes.get(i).split("/");
808 if (type.length != 2) continue;
809
810 if (!commonType[1].equals(type[1])) {
811 commonType[1] = "*";
812 }
813
814 if (!commonType[0].equals(type[0])) {
815 commonType[0] = "*";
816 commonType[1] = "*";
817 break;
818 }
819 }
820
821 return commonType[0] + "/" + commonType[1];
822 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700823
824 private void setEnabledRecursive(View v, boolean enabled) {
825 if (v.isEnabled() == enabled) return;
826 v.setEnabled(enabled);
827
828 if (v instanceof ViewGroup) {
829 final ViewGroup vg = (ViewGroup) v;
830 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
831 setEnabledRecursive(vg.getChildAt(i), enabled);
832 }
833 }
834 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700835}