blob: 6ff47f815926ab61d1e59d7e19f5ad1d28b87cf0 [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 Sharkey6defd072013-09-23 14:23:41 -070020import static com.android.documentsui.DocumentsActivity.State.ACTION_CREATE;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070021import static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE;
22import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
23import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070024import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
25import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070026import static com.android.documentsui.model.DocumentInfo.getCursorInt;
27import static com.android.documentsui.model.DocumentInfo.getCursorLong;
28import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070029
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070030import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070031import android.app.FragmentManager;
32import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070033import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070034import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070035import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070036import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070038import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070040import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070041import android.graphics.Bitmap;
42import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070043import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070044import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070045import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070046import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070048import android.os.CancellationSignal;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070049import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070050import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070051import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070052import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070053import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070054import android.text.format.Time;
55import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070056import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070057import android.util.SparseBooleanArray;
58import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070059import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070060import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070061import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.view.View;
63import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070064import android.widget.AbsListView;
65import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070066import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070067import android.widget.AdapterView;
68import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070069import android.widget.BaseAdapter;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070070import android.widget.FrameLayout;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070071import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070072import android.widget.ImageView;
73import android.widget.ListView;
74import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070075import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070076
Jeff Sharkeyb3620442013-09-01 18:41:04 -070077import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070078import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070079import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070080import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070081import com.google.android.collect.Lists;
82
83import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070084import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070085import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070086
87/**
88 * Display the documents inside a single directory.
89 */
90public class DirectoryFragment extends Fragment {
91
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070092 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070093 private ListView mListView;
94 private GridView mGridView;
95
Jeff Sharkeyc317af82013-07-01 16:56:54 -070096 private AbsListView mCurrentView;
97
Jeff Sharkeya5defe32013-08-05 17:56:48 -070098 public static final int TYPE_NORMAL = 1;
99 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700100 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700101
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700102 public static final int ANIM_NONE = 1;
103 public static final int ANIM_SIDE = 2;
104 public static final int ANIM_DOWN = 3;
105 public static final int ANIM_UP = 4;
106
Jeff Sharkey5b535922013-08-02 15:55:26 -0700107 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700108 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700109
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700110 private int mLastMode = MODE_UNKNOWN;
111 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700112 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700113
Jeff Sharkey9656a532013-09-13 13:42:19 -0700114 private boolean mHideGridTitles = false;
115
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700116 private Point mThumbSize;
117
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700118 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700119 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700120
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700121 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700122 private static final String EXTRA_ROOT = "root";
123 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700124 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700125 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700126
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700129 private final int mLoaderId = sLoaderId.incrementAndGet();
130
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700131 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
132 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700133 }
134
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700135 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
136 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700137 }
138
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700139 public static void showRecentsOpen(FragmentManager fm, int anim) {
140 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700141 }
142
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700143 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
144 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700145 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700146 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700147 args.putParcelable(EXTRA_ROOT, root);
148 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700149 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700150
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700151 final FragmentTransaction ft = fm.beginTransaction();
152 switch (anim) {
153 case ANIM_SIDE:
154 args.putBoolean(EXTRA_IGNORE_STATE, true);
155 break;
156 case ANIM_DOWN:
157 args.putBoolean(EXTRA_IGNORE_STATE, true);
158 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
159 break;
160 case ANIM_UP:
161 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
162 break;
163 }
164
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700165 final DirectoryFragment fragment = new DirectoryFragment();
166 fragment.setArguments(args);
167
Jeff Sharkey76112212013-08-06 11:26:10 -0700168 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700169 ft.commitAllowingStateLoss();
170 }
171
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700172 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
173 final StringBuilder builder = new StringBuilder();
174 builder.append(root != null ? root.authority : "null").append(';');
175 builder.append(root != null ? root.rootId : "null").append(';');
176 builder.append(doc != null ? doc.documentId : "null");
177 return builder.toString();
178 }
179
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700180 public static DirectoryFragment get(FragmentManager fm) {
181 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700182 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700183 }
184
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700185 @Override
186 public View onCreateView(
187 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
188 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700189 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
190
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700191 mEmptyView = view.findViewById(android.R.id.empty);
192
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700193 mListView = (ListView) view.findViewById(R.id.list);
194 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700195 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700196 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700197
198 mGridView = (GridView) view.findViewById(R.id.grid);
199 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700200 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700201 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700202
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700203 return view;
204 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700205
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700206 @Override
207 public void onActivityCreated(Bundle savedInstanceState) {
208 super.onActivityCreated(savedInstanceState);
209
210 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700211 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700212
Jeff Sharkey9656a532013-09-13 13:42:19 -0700213 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
214 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
215
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700216 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700217 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700218 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700219
Jeff Sharkey9656a532013-09-13 13:42:19 -0700220 if (mType == TYPE_RECENT_OPEN) {
221 // Hide titles when showing recents for picking images/videos
222 mHideGridTitles = MimePredicate.mimeMatches(
223 MimePredicate.VISUAL_MIMES, state.acceptMimes);
224 } else {
225 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
226 }
227
Jeff Sharkey46899c82013-08-18 22:26:48 -0700228 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700229 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700230 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700231 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700232
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700233 Uri contentsUri;
234 switch (mType) {
235 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700236 contentsUri = DocumentsContract.buildChildDocumentsUri(
237 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700238 if (state.action == ACTION_MANAGE) {
239 contentsUri = DocumentsContract.setManageMode(contentsUri);
240 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700241 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700242 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700243 case TYPE_SEARCH:
244 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700245 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700246 if (state.action == ACTION_MANAGE) {
247 contentsUri = DocumentsContract.setManageMode(contentsUri);
248 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700249 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700250 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700251 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700252 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700253 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700254 default:
255 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700256 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700257 }
258
259 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700260 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700261 if (!isAdded()) return;
262
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700263 mAdapter.swapResult(result.cursor, result.exception);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700264
265 // Push latest state up to UI
266 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700267 if (result.mode != MODE_UNKNOWN) {
268 state.derivedMode = result.mode;
269 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700270 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700271 ((DocumentsActivity) context).onStateChanged();
272
273 updateDisplayState();
274
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700275 // Restore any previous instance state
276 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
277 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
278 getView().restoreHierarchyState(container);
279 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700280 mListView.smoothScrollToPosition(0);
281 mGridView.smoothScrollToPosition(0);
282 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700283
284 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700285 }
286
287 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700288 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700289 mAdapter.swapResult(null, null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700290 }
291 };
292
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700293 // Kick off loader at least once
294 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
295
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700296 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700297 }
298
Jeff Sharkey42d26792013-09-06 13:22:09 -0700299 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700300 public void onStop() {
301 super.onStop();
302
303 // Remember last scroll location
304 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
305 getView().saveHierarchyState(container);
306 final State state = getDisplayState(this);
307 state.dirState.put(mStateKey, container);
308 }
309
310 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700311 public void onResume() {
312 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700313 updateDisplayState();
314 }
315
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700316 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700317 // Sort order change always triggers reload; we'll trigger state change
318 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700319 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
320 }
321
322 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700323 final ContentResolver resolver = getActivity().getContentResolver();
324 final State state = getDisplayState(this);
325
326 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
327 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
328
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700329 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700330 final Uri stateUri = RecentsProvider.buildState(
331 root.authority, root.rootId, doc.documentId);
332 final ContentValues values = new ContentValues();
333 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700334
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700335 new AsyncTask<Void, Void, Void>() {
336 @Override
337 protected Void doInBackground(Void... params) {
338 resolver.insert(stateUri, values);
339 return null;
340 }
341 }.execute();
342 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700343
344 // Mode change is just visual change; no need to kick loader, and
345 // deliver change event immediately.
346 state.derivedMode = state.userMode;
347 ((DocumentsActivity) getActivity()).onStateChanged();
348
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700349 updateDisplayState();
350 }
351
352 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700353 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700354
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700355 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700356 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700357 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700358
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700359 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
360 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700361
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700362 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700363 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700364 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
365 } else {
366 choiceMode = ListView.CHOICE_MODE_NONE;
367 }
368
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700369 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700370 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700371 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700372 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700373 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700374 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700375 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700376 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700377 mGridView.setChoiceMode(choiceMode);
378 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700379 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700380 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700381 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700382 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700383 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700384 mListView.setChoiceMode(choiceMode);
385 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700386 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700387 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700388 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700389
390 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700391 }
392
393 private OnItemClickListener mItemListener = new OnItemClickListener() {
394 @Override
395 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700396 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700397 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700398 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
399 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
400 if (isDocumentEnabled(docMimeType, docFlags)) {
401 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700402 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
403 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700404 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700405 }
406 };
407
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700408 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
409 @Override
410 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
411 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
412 return true;
413 }
414
415 @Override
416 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700417 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700418
419 final MenuItem open = menu.findItem(R.id.menu_open);
420 final MenuItem share = menu.findItem(R.id.menu_share);
421 final MenuItem delete = menu.findItem(R.id.menu_delete);
422
423 final boolean manageMode = state.action == ACTION_MANAGE;
424 open.setVisible(!manageMode);
425 share.setVisible(manageMode);
426 delete.setVisible(manageMode);
427
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700428 return true;
429 }
430
431 @Override
432 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700433 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700434 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700435 final int size = checked.size();
436 for (int i = 0; i < size; i++) {
437 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700438 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700439 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700440 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700441 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700442 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700443
Jeff Sharkey873daa32013-08-18 17:38:20 -0700444 final int id = item.getItemId();
445 if (id == R.id.menu_open) {
446 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700447 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700448 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700449
450 } else if (id == R.id.menu_share) {
451 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700452 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700453 return true;
454
455 } else if (id == R.id.menu_delete) {
456 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700457 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700458 return true;
459
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700460 } else {
461 return false;
462 }
463 }
464
465 @Override
466 public void onDestroyActionMode(ActionMode mode) {
467 // ignored
468 }
469
470 @Override
471 public void onItemCheckedStateChanged(
472 ActionMode mode, int position, long id, boolean checked) {
473 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700474 // Directories and footer items cannot be checked
475 boolean valid = false;
476
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700477 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700478 if (cursor != null) {
479 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700480 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
481 if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
482 valid = isDocumentEnabled(docMimeType, docFlags);
483 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700484 }
485
486 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700487 mCurrentView.setItemChecked(position, false);
488 }
489 }
490
491 mode.setTitle(getResources()
492 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
493 }
494 };
495
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700496 private RecyclerListener mRecycleListener = new RecyclerListener() {
497 @Override
498 public void onMovedToScrapHeap(View view) {
499 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
500 if (iconThumb != null) {
501 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
502 if (oldTask != null) {
503 oldTask.reallyCancel();
504 iconThumb.setTag(null);
505 }
506 }
507 }
508 };
509
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700510 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700511 Intent intent;
512 if (docs.size() == 1) {
513 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700514
Jeff Sharkey873daa32013-08-18 17:38:20 -0700515 intent = new Intent(Intent.ACTION_SEND);
516 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
517 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700518 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700519 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700520
521 } else if (docs.size() > 1) {
522 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
523 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
524 intent.addCategory(Intent.CATEGORY_DEFAULT);
525
526 final ArrayList<String> mimeTypes = Lists.newArrayList();
527 final ArrayList<Uri> uris = Lists.newArrayList();
528 for (DocumentInfo doc : docs) {
529 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700530 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700531 }
532
533 intent.setType(findCommonMimeType(mimeTypes));
534 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
535
536 } else {
537 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700538 }
539
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700540 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700541 startActivity(intent);
542 }
543
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700544 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700545 final Context context = getActivity();
546 final ContentResolver resolver = context.getContentResolver();
547
548 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700549 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700550 if (!doc.isDeleteSupported()) {
551 Log.w(TAG, "Skipping " + doc);
552 hadTrouble = true;
553 continue;
554 }
555
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700556 ContentProviderClient client = null;
557 try {
558 client = DocumentsApplication.acquireUnstableProviderOrThrow(
559 resolver, doc.derivedUri.getAuthority());
560 DocumentsContract.deleteDocument(client, doc.derivedUri);
561 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700562 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700563 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700564 } finally {
565 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700566 }
567 }
568
569 if (hadTrouble) {
570 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
571 }
572 }
573
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700574 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700575 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700576 }
577
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700578 private static abstract class Footer {
579 private final int mItemViewType;
580
581 public Footer(int itemViewType) {
582 mItemViewType = itemViewType;
583 }
584
585 public abstract View getView(View convertView, ViewGroup parent);
586
587 public int getItemViewType() {
588 return mItemViewType;
589 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700590 }
591
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700592 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700593 public LoadingFooter() {
594 super(1);
595 }
596
Jeff Sharkey20b32272013-09-03 15:25:52 -0700597 @Override
598 public View getView(View convertView, ViewGroup parent) {
599 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700600 final State state = getDisplayState(DirectoryFragment.this);
601
Jeff Sharkey20b32272013-09-03 15:25:52 -0700602 if (convertView == null) {
603 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700604 if (state.derivedMode == MODE_LIST) {
605 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
606 } else if (state.derivedMode == MODE_GRID) {
607 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
608 } else {
609 throw new IllegalStateException();
610 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700611 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700612
Jeff Sharkey20b32272013-09-03 15:25:52 -0700613 return convertView;
614 }
615 }
616
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700617 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700618 private final int mIcon;
619 private final String mMessage;
620
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700621 public MessageFooter(int itemViewType, int icon, String message) {
622 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700623 mIcon = icon;
624 mMessage = message;
625 }
626
627 @Override
628 public View getView(View convertView, ViewGroup parent) {
629 final Context context = parent.getContext();
630 final State state = getDisplayState(DirectoryFragment.this);
631
632 if (convertView == null) {
633 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700634 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700635 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700636 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700637 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
638 } else {
639 throw new IllegalStateException();
640 }
641 }
642
643 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
644 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
645 icon.setImageResource(mIcon);
646 title.setText(mMessage);
647 return convertView;
648 }
649 }
650
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700651 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700652 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700653 private int mCursorCount;
654
655 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700656
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700657 public void swapResult(Cursor cursor, Exception e) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700658 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700659 mCursorCount = cursor != null ? cursor.getCount() : 0;
660
661 mFooters.clear();
662
663 final Bundle extras = cursor != null ? cursor.getExtras() : null;
664 if (extras != null) {
665 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
666 if (info != null) {
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700667 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700668 }
669 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
670 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700671 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700672 }
673 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
674 mFooters.add(new LoadingFooter());
675 }
676 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700677
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700678 if (e != null) {
679 mFooters.add(new MessageFooter(
680 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
681 }
682
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700683 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700684 mEmptyView.setVisibility(View.VISIBLE);
685 } else {
686 mEmptyView.setVisibility(View.GONE);
687 }
688
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700689 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700690 }
691
692 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700693 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700694 if (position < mCursorCount) {
695 return getDocumentView(position, convertView, parent);
696 } else {
697 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700698 convertView = mFooters.get(position).getView(convertView, parent);
699 // Only the view itself is disabled; contents inside shouldn't
700 // be dimmed.
701 convertView.setEnabled(false);
702 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700703 }
704 }
705
706 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700707 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700708 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700709
Jeff Sharkey9656a532013-09-13 13:42:19 -0700710 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
711
Jeff Sharkey873daa32013-08-18 17:38:20 -0700712 final RootsCache roots = DocumentsApplication.getRootsCache(context);
713 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
714 context, mThumbSize);
715
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700716 if (convertView == null) {
717 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700718 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700719 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700720 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700721 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
Jeff Sharkeydb884f52013-09-21 15:11:19 -0700722
723 // Apply padding to grid items
724 final FrameLayout grid = (FrameLayout) convertView;
725 final int gridPadding = getResources()
726 .getDimensionPixelSize(R.dimen.grid_padding);
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700727
728 // Tricksy hobbitses! We need to fully clear the drawable so
729 // the view doesn't clobber the new InsetDrawable callback
730 // when setting back later.
731 final Drawable fg = grid.getForeground();
732 final Drawable bg = grid.getBackground();
733 grid.setForeground(null);
734 grid.setBackground(null);
735 grid.setForeground(new InsetDrawable(fg, gridPadding));
736 grid.setBackground(new InsetDrawable(bg, gridPadding));
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700737 } else {
738 throw new IllegalStateException();
739 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700740 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700741
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700742 final Cursor cursor = getItem(position);
743
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700744 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
745 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700746 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
747 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
748 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
749 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
750 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
751 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
752 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
753 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700754
Jeff Sharkey9656a532013-09-13 13:42:19 -0700755 final View line1 = convertView.findViewById(R.id.line1);
756 final View line2 = convertView.findViewById(R.id.line2);
757
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700758 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
759 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700760 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
761 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700762 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700763 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
764 final TextView date = (TextView) convertView.findViewById(R.id.date);
765 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700766
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700767 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700768 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700769 oldTask.reallyCancel();
770 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700771 }
772
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700773 iconMime.animate().cancel();
774 iconThumb.animate().cancel();
775
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700776 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700777 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700778 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
779 final boolean showThumbnail = supportsThumbnail && allowThumbnail;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700780
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700781 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700782 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700783 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700784 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700785 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700786 iconThumb.setImageBitmap(cachedResult);
787 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700788 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700789 iconThumb.setImageDrawable(null);
790 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
791 uri, iconMime, iconThumb, mThumbSize);
792 iconThumb.setTag(task);
793 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700794 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700795 }
796
797 // Always throw MIME icon into place, even when a thumbnail is being
798 // loaded in background.
799 if (cacheHit) {
800 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700801 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700802 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700803 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700804 iconMime.setAlpha(1f);
805 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700806 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700807 if (docIcon != 0) {
808 iconMime.setImageDrawable(
809 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
810 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700811 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
812 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700813 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700814 }
815
Jeff Sharkey9656a532013-09-13 13:42:19 -0700816 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700817 boolean hasLine2 = false;
818
Jeff Sharkey9656a532013-09-13 13:42:19 -0700819 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
820 if (!hideTitle) {
821 title.setText(docDisplayName);
822 hasLine1 = true;
823 }
824
825 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700826 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700827 // We've already had to enumerate roots before any results can
828 // be shown, so this will never block.
829 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700830 iconDrawable = root.loadIcon(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700831
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700832 if (summary != null) {
833 final boolean alwaysShowSummary = getResources()
834 .getBoolean(R.bool.always_show_summary);
835 if (alwaysShowSummary) {
836 summary.setText(root.getDirectoryString());
837 summary.setVisibility(View.VISIBLE);
838 hasLine2 = true;
839 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700840 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700841 // No summary needed if icon speaks for itself
842 summary.setVisibility(View.INVISIBLE);
843 } else {
844 summary.setText(root.getDirectoryString());
845 summary.setVisibility(View.VISIBLE);
846 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
847 hasLine2 = true;
848 }
849 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700850 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700851 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700852 // Directories showing thumbnails in grid mode get a little icon
853 // hint to remind user they're a directory.
854 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
855 && showThumbnail) {
856 iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
857 }
858
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700859 if (summary != null) {
860 if (docSummary != null) {
861 summary.setText(docSummary);
862 summary.setVisibility(View.VISIBLE);
863 hasLine2 = true;
864 } else {
865 summary.setVisibility(View.INVISIBLE);
866 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700867 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700868 }
869
Jeff Sharkey9656a532013-09-13 13:42:19 -0700870 if (icon1 != null) icon1.setVisibility(View.GONE);
871 if (icon2 != null) icon2.setVisibility(View.GONE);
872
873 if (iconDrawable != null) {
874 if (hasLine1) {
875 icon1.setVisibility(View.VISIBLE);
876 icon1.setImageDrawable(iconDrawable);
877 } else {
878 icon2.setVisibility(View.VISIBLE);
879 icon2.setImageDrawable(iconDrawable);
880 }
881 }
882
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700883 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700884 date.setText(null);
885 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700886 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700887 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700888 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700889
890 if (state.showSize) {
891 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700892 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700893 size.setText(null);
894 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700895 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700896 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700897 }
898 } else {
899 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700900 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700901
Jeff Sharkey9656a532013-09-13 13:42:19 -0700902 if (line1 != null) {
903 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
904 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700905 if (line2 != null) {
906 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
907 }
Jeff Sharkey42d26792013-09-06 13:22:09 -0700908
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700909 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700910 if (enabled) {
911 setEnabledRecursive(convertView, true);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700912 iconMime.setAlpha(1f);
913 iconThumb.setAlpha(1f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700914 if (icon1 != null) icon1.setAlpha(1f);
915 if (icon2 != null) icon2.setAlpha(1f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700916 } else {
917 setEnabledRecursive(convertView, false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700918 iconMime.setAlpha(0.5f);
919 iconThumb.setAlpha(0.5f);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700920 if (icon1 != null) icon1.setAlpha(0.5f);
921 if (icon2 != null) icon2.setAlpha(0.5f);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700922 }
923
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700924 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700925 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700926
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700927 @Override
928 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700929 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700930 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700931
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700932 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700933 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700934 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700935 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700936 return mCursor;
937 } else {
938 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700939 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700940 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700941
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700942 @Override
943 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700944 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700945 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700946
947 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700948 public int getViewTypeCount() {
949 return 4;
950 }
951
952 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700953 public int getItemViewType(int position) {
954 if (position < mCursorCount) {
955 return 0;
956 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700957 position -= mCursorCount;
958 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700959 }
960 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700961 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700962
963 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700964 private final Uri mUri;
965 private final ImageView mIconMime;
966 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700967 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700968 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700969
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700970 public ThumbnailAsyncTask(
971 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
972 mUri = uri;
973 mIconMime = iconMime;
974 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700975 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700976 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700977 }
978
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700979 public void reallyCancel() {
980 cancel(false);
981 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700982 }
983
984 @Override
985 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700986 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700987 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700988
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700989 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700990 Bitmap result = null;
991 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700992 client = DocumentsApplication.acquireUnstableProviderOrThrow(
993 resolver, mUri.getAuthority());
994 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700995 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700996 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
997 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700998 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700999 }
1000 } catch (Exception e) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001001 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1002 } finally {
1003 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001004 }
1005 return result;
1006 }
1007
1008 @Override
1009 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001010 if (mIconThumb.getTag() == this && result != null) {
1011 mIconThumb.setTag(null);
1012 mIconThumb.setImageBitmap(result);
1013
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001014 final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
1015 mIconMime.setAlpha(targetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001016 mIconMime.animate().alpha(0f).start();
1017 mIconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -07001018 mIconThumb.animate().alpha(targetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001019 }
1020 }
1021 }
1022
1023 private static String formatTime(Context context, long when) {
1024 // TODO: DateUtils should make this easier
1025 Time then = new Time();
1026 then.set(when);
1027 Time now = new Time();
1028 now.setToNow();
1029
1030 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1031 | DateUtils.FORMAT_ABBREV_ALL;
1032
1033 if (then.year != now.year) {
1034 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1035 } else if (then.yearDay != now.yearDay) {
1036 flags |= DateUtils.FORMAT_SHOW_DATE;
1037 } else {
1038 flags |= DateUtils.FORMAT_SHOW_TIME;
1039 }
1040
1041 return DateUtils.formatDateTime(context, when, flags);
1042 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001043
1044 private String findCommonMimeType(List<String> mimeTypes) {
1045 String[] commonType = mimeTypes.get(0).split("/");
1046 if (commonType.length != 2) {
1047 return "*/*";
1048 }
1049
1050 for (int i = 1; i < mimeTypes.size(); i++) {
1051 String[] type = mimeTypes.get(i).split("/");
1052 if (type.length != 2) continue;
1053
1054 if (!commonType[1].equals(type[1])) {
1055 commonType[1] = "*";
1056 }
1057
1058 if (!commonType[0].equals(type[0])) {
1059 commonType[0] = "*";
1060 commonType[1] = "*";
1061 break;
1062 }
1063 }
1064
1065 return commonType[0] + "/" + commonType[1];
1066 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001067
1068 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001069 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001070 if (v.isEnabled() == enabled) return;
1071 v.setEnabled(enabled);
1072
1073 if (v instanceof ViewGroup) {
1074 final ViewGroup vg = (ViewGroup) v;
1075 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1076 setEnabledRecursive(vg.getChildAt(i), enabled);
1077 }
1078 }
1079 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001080
1081 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1082 final State state = getDisplayState(DirectoryFragment.this);
1083
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001084 // Directories are always enabled
1085 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1086 return true;
1087 }
1088
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001089 // Read-only files are disabled when creating
1090 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1091 return false;
1092 }
1093
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001094 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1095 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001096}