blob: b2c4d59c1d9c5a7cf2c05e6513042511bb7840ea [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 Sharkey311a7d82015-04-11 21:27:21 -070019import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE;
20import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL;
Steve McKayd0a2a2c2015-03-25 14:35:33 -070021import static com.android.documentsui.BaseActivity.State.ACTION_CREATE;
22import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE;
23import static com.android.documentsui.BaseActivity.State.MODE_GRID;
24import static com.android.documentsui.BaseActivity.State.MODE_LIST;
25import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN;
26import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkey311a7d82015-04-11 21:27:21 -070027import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import static com.android.documentsui.model.DocumentInfo.getCursorInt;
29import static com.android.documentsui.model.DocumentInfo.getCursorLong;
30import static com.android.documentsui.model.DocumentInfo.getCursorString;
Ben Kwaf5858932015-04-07 15:43:39 -070031import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070032import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070033import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070034import android.app.FragmentManager;
35import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070037import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070038import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070039import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070041import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070043import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070044import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070045import android.graphics.Bitmap;
46import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070047import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070048import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070049import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070053import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070054import android.os.Parcelable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070055import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070056import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070057import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070058import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070059import android.text.format.Time;
60import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070061import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070062import android.util.SparseBooleanArray;
63import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070064import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070065import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070066import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070067import android.view.View;
68import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070069import android.widget.AbsListView;
70import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070071import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072import android.widget.AdapterView;
73import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070074import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070075import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070076import android.widget.ImageView;
77import android.widget.ListView;
78import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070079import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070080
Steve McKayd0a2a2c2015-03-25 14:35:33 -070081import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070082import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070083import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070084import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070085import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070086import com.google.android.collect.Lists;
87
88import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070089import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070090
91/**
92 * Display the documents inside a single directory.
93 */
94public class DirectoryFragment extends Fragment {
95
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070096 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070097 private ListView mListView;
98 private GridView mGridView;
99
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700100 private AbsListView mCurrentView;
101
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700102 public static final int TYPE_NORMAL = 1;
103 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700104 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700105
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700106 public static final int ANIM_NONE = 1;
107 public static final int ANIM_SIDE = 2;
108 public static final int ANIM_DOWN = 3;
109 public static final int ANIM_UP = 4;
110
Ben Kwaf5858932015-04-07 15:43:39 -0700111 public static final int REQUEST_COPY_DESTINATION = 1;
112
Jeff Sharkey5b535922013-08-02 15:55:26 -0700113 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700114 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700115
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700116 private int mLastMode = MODE_UNKNOWN;
117 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700118 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700119
Jeff Sharkey9656a532013-09-13 13:42:19 -0700120 private boolean mHideGridTitles = false;
121
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700122 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700123 private Point mThumbSize;
124
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700125 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700126 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700127
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700128 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700129 private static final String EXTRA_ROOT = "root";
130 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700131 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700132 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700134 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700135
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700136 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
137 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700138 }
139
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700140 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
141 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700142 }
143
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700144 public static void showRecentsOpen(FragmentManager fm, int anim) {
145 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700146 }
147
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700148 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
149 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700150 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700151 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700152 args.putParcelable(EXTRA_ROOT, root);
153 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700154 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700155
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700156 final FragmentTransaction ft = fm.beginTransaction();
157 switch (anim) {
158 case ANIM_SIDE:
159 args.putBoolean(EXTRA_IGNORE_STATE, true);
160 break;
161 case ANIM_DOWN:
162 args.putBoolean(EXTRA_IGNORE_STATE, true);
163 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
164 break;
165 case ANIM_UP:
166 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
167 break;
168 }
169
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700170 final DirectoryFragment fragment = new DirectoryFragment();
171 fragment.setArguments(args);
172
Jeff Sharkey76112212013-08-06 11:26:10 -0700173 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700174 ft.commitAllowingStateLoss();
175 }
176
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700177 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
178 final StringBuilder builder = new StringBuilder();
179 builder.append(root != null ? root.authority : "null").append(';');
180 builder.append(root != null ? root.rootId : "null").append(';');
181 builder.append(doc != null ? doc.documentId : "null");
182 return builder.toString();
183 }
184
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700185 public static DirectoryFragment get(FragmentManager fm) {
186 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700187 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700188 }
189
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700190 @Override
191 public View onCreateView(
192 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
193 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700194 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700195 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
196
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700197 mEmptyView = view.findViewById(android.R.id.empty);
198
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700199 mListView = (ListView) view.findViewById(R.id.list);
200 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700201 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700202 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700203
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700204 // Indent our list divider to align with text
205 final Drawable divider = mListView.getDivider();
206 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
207 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
208 if (insetLeft) {
209 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
210 } else {
211 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
212 }
213
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700214 mGridView = (GridView) view.findViewById(R.id.grid);
215 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700216 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700217 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700218
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700219 return view;
220 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700221
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700222 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700223 public void onDestroyView() {
224 super.onDestroyView();
225
226 // Cancel any outstanding thumbnail requests
227 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
228 final int count = target.getChildCount();
229 for (int i = 0; i < count; i++) {
230 final View view = target.getChildAt(i);
231 mRecycleListener.onMovedToScrapHeap(view);
232 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700233
234 // Tear down any selection in progress
235 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
236 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700237 }
238
239 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700240 public void onActivityCreated(Bundle savedInstanceState) {
241 super.onActivityCreated(savedInstanceState);
242
243 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700244 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700245
Jeff Sharkey9656a532013-09-13 13:42:19 -0700246 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
247 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
248
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700249 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700250 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700251 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700252
Jeff Sharkey9656a532013-09-13 13:42:19 -0700253 if (mType == TYPE_RECENT_OPEN) {
254 // Hide titles when showing recents for picking images/videos
255 mHideGridTitles = MimePredicate.mimeMatches(
256 MimePredicate.VISUAL_MIMES, state.acceptMimes);
257 } else {
258 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
259 }
260
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700261 final ActivityManager am = (ActivityManager) context.getSystemService(
262 Context.ACTIVITY_SERVICE);
263 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
264
Jeff Sharkey46899c82013-08-18 22:26:48 -0700265 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700266 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700267 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700268 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700269
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700270 Uri contentsUri;
271 switch (mType) {
272 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700273 contentsUri = DocumentsContract.buildChildDocumentsUri(
274 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700275 if (state.action == ACTION_MANAGE) {
276 contentsUri = DocumentsContract.setManageMode(contentsUri);
277 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700278 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700279 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700280 case TYPE_SEARCH:
281 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700282 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700283 if (state.action == ACTION_MANAGE) {
284 contentsUri = DocumentsContract.setManageMode(contentsUri);
285 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700286 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700287 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700288 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700289 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700290 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700291 default:
292 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700293 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700294 }
295
296 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700297 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700298 if (!isAdded()) return;
299
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700300 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700301
302 // Push latest state up to UI
303 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700304 if (result.mode != MODE_UNKNOWN) {
305 state.derivedMode = result.mode;
306 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700307 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700308 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700309
310 updateDisplayState();
311
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700312 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700313 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
314 context instanceof DocumentsActivity) {
315 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700316 }
317
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700318 // Restore any previous instance state
319 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
320 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
321 getView().restoreHierarchyState(container);
322 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700323 mListView.smoothScrollToPosition(0);
324 mGridView.smoothScrollToPosition(0);
325 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700326
327 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700328 }
329
330 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700331 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700332 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700333 }
334 };
335
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700336 // Kick off loader at least once
337 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
338
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700339 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700340 }
341
Jeff Sharkey42d26792013-09-06 13:22:09 -0700342 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700343 public void onActivityResult(int requestCode, int resultCode, Intent data) {
344 final Context context = getActivity();
345 final Resources res = context.getResources();
346
347 // There's only one request code right now. Replace this with a switch statement or
348 // something more scalable when more codes are added.
349 if (requestCode != REQUEST_COPY_DESTINATION) {
350 return;
351 }
352 if (resultCode == Activity.RESULT_CANCELED || data == null) {
353 // User pressed the back button or otherwise cancelled the destination pick. Don't
354 // proceed with the copy.
355 return;
356 }
357
Daichi Hironof500ef22015-04-17 16:19:15 +0900358 final List<DocumentInfo> docs = getDisplayState(this).selectedDocumentsForCopy;
Tomasz Mikolajewski2023fcf2015-04-10 10:30:33 +0900359 final Intent copyIntent = new Intent(context, CopyService.class);
360 copyIntent.putParcelableArrayListExtra(CopyService.EXTRA_SRC_LIST, new ArrayList<DocumentInfo>(docs));
361 copyIntent.putExtra(CopyService.EXTRA_STACK, data.getParcelableExtra(CopyService.EXTRA_STACK));
Ben Kwaf5858932015-04-07 15:43:39 -0700362
363 Toast.makeText(context,
364 res.getQuantityString(R.plurals.copy_begin, docs.size(), docs.size()),
365 Toast.LENGTH_SHORT).show();
366 context.startService(copyIntent);
367 }
368
369 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700370 public void onStop() {
371 super.onStop();
372
373 // Remember last scroll location
374 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
375 getView().saveHierarchyState(container);
376 final State state = getDisplayState(this);
377 state.dirState.put(mStateKey, container);
378 }
379
380 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700381 public void onResume() {
382 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700383 updateDisplayState();
384 }
385
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700386 public void onDisplayStateChanged() {
387 updateDisplayState();
388 }
389
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700390 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700391 // Sort order change always triggers reload; we'll trigger state change
392 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700393 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
394 }
395
396 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700397 final ContentResolver resolver = getActivity().getContentResolver();
398 final State state = getDisplayState(this);
399
400 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
401 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
402
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700403 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700404 final Uri stateUri = RecentsProvider.buildState(
405 root.authority, root.rootId, doc.documentId);
406 final ContentValues values = new ContentValues();
407 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700408
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700409 new AsyncTask<Void, Void, Void>() {
410 @Override
411 protected Void doInBackground(Void... params) {
412 resolver.insert(stateUri, values);
413 return null;
414 }
415 }.execute();
416 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700417
418 // Mode change is just visual change; no need to kick loader, and
419 // deliver change event immediately.
420 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700421 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700422
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700423 updateDisplayState();
424 }
425
426 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700427 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700428
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700429 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700430 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700431 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700432
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700433 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
434 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700435
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700436 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700437 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700438 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
439 } else {
440 choiceMode = ListView.CHOICE_MODE_NONE;
441 }
442
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700443 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700444 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700445 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700446 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700447 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700448 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700449 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700450 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700451 mGridView.setChoiceMode(choiceMode);
452 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700453 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700454 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700455 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700456 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700457 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700458 mListView.setChoiceMode(choiceMode);
459 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700460 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700461 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700462 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700463
464 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700465 }
466
467 private OnItemClickListener mItemListener = new OnItemClickListener() {
468 @Override
469 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700470 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700471 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700472 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
473 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
474 if (isDocumentEnabled(docMimeType, docFlags)) {
475 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700476 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700477 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700478 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700479 }
480 };
481
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700482 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
483 @Override
484 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
485 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100486 mode.setTitle(getResources()
487 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700488 return true;
489 }
490
491 @Override
492 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700493 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700494
495 final MenuItem open = menu.findItem(R.id.menu_open);
496 final MenuItem share = menu.findItem(R.id.menu_share);
497 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700498 final MenuItem copy = menu.findItem(R.id.menu_copy);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700499
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700500 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
501 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
502
503 open.setVisible(!manageOrBrowse);
504 share.setVisible(manageOrBrowse);
505 delete.setVisible(manageOrBrowse);
Ben Kwa16eb1222015-04-16 18:17:25 -0700506 copy.setVisible(manageOrBrowse);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700507
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700508 return true;
509 }
510
511 @Override
512 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700513 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700514 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700515 final int size = checked.size();
516 for (int i = 0; i < size; i++) {
517 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700518 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700519 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700520 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700521 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700522 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700523
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 final int id = item.getItemId();
525 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700526 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700527 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700528 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700529
530 } else if (id == R.id.menu_share) {
531 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700532 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700533 return true;
534
535 } else if (id == R.id.menu_delete) {
536 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700537 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700538 return true;
539
Ben Kwa41b26c12015-03-31 10:11:43 -0700540 } else if (id == R.id.menu_copy) {
541 onCopyDocuments(docs);
542 mode.finish();
543 return true;
544
Ben Kwa512a6ba2015-03-31 08:15:21 -0700545 } else if (id == R.id.menu_select_all) {
546 int count = mCurrentView.getCount();
547 for (int i = 0; i < count; i++) {
548 mCurrentView.setItemChecked(i, true);
549 }
550 updateDisplayState();
551 return true;
552
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700553 } else {
554 return false;
555 }
556 }
557
558 @Override
559 public void onDestroyActionMode(ActionMode mode) {
560 // ignored
561 }
562
563 @Override
564 public void onItemCheckedStateChanged(
565 ActionMode mode, int position, long id, boolean checked) {
566 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700567 // Directories and footer items cannot be checked
568 boolean valid = false;
569
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700570 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700571 if (cursor != null) {
572 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700573 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700574 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700575 }
576
577 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700578 mCurrentView.setItemChecked(position, false);
579 }
580 }
581
582 mode.setTitle(getResources()
583 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
584 }
585 };
586
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700587 private RecyclerListener mRecycleListener = new RecyclerListener() {
588 @Override
589 public void onMovedToScrapHeap(View view) {
590 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
591 if (iconThumb != null) {
592 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
593 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700594 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700595 iconThumb.setTag(null);
596 }
597 }
598 }
599 };
600
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700601 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700602 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700603
604 // Filter out directories - those can't be shared.
605 List<DocumentInfo> docsForSend = Lists.newArrayList();
606 for (DocumentInfo doc: docs) {
607 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
608 docsForSend.add(doc);
609 }
610 }
611
612 if (docsForSend.size() == 1) {
613 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700614
Jeff Sharkey873daa32013-08-18 17:38:20 -0700615 intent = new Intent(Intent.ACTION_SEND);
616 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
617 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700618 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700619 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700620
Ben Kwaf527c632015-04-08 15:03:35 -0700621 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700622 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
623 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
624 intent.addCategory(Intent.CATEGORY_DEFAULT);
625
626 final ArrayList<String> mimeTypes = Lists.newArrayList();
627 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700628 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700629 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700630 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700631 }
632
633 intent.setType(findCommonMimeType(mimeTypes));
634 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
635
636 } else {
637 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700638 }
639
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700640 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700641 startActivity(intent);
642 }
643
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700644 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700645 final Context context = getActivity();
646 final ContentResolver resolver = context.getContentResolver();
647
648 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700649 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700650 if (!doc.isDeleteSupported()) {
651 Log.w(TAG, "Skipping " + doc);
652 hadTrouble = true;
653 continue;
654 }
655
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700656 ContentProviderClient client = null;
657 try {
658 client = DocumentsApplication.acquireUnstableProviderOrThrow(
659 resolver, doc.derivedUri.getAuthority());
660 DocumentsContract.deleteDocument(client, doc.derivedUri);
661 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700662 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700663 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700664 } finally {
665 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700666 }
667 }
668
669 if (hadTrouble) {
670 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
671 }
672 }
673
Ben Kwa41b26c12015-03-31 10:11:43 -0700674 private void onCopyDocuments(List<DocumentInfo> docs) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900675 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700676
Ben Kwaf5858932015-04-07 15:43:39 -0700677 // Pop up a dialog to pick a destination. This is inadequate but works for now.
678 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900679 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900680 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900681 Uri.EMPTY,
682 getActivity(),
683 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900684 boolean directoryCopy = false;
685 for (DocumentInfo info : docs) {
686 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
687 directoryCopy = true;
688 break;
689 }
690 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900691 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwaf5858932015-04-07 15:43:39 -0700692 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700693 }
694
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700695 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700696 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700697 }
698
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700699 private static abstract class Footer {
700 private final int mItemViewType;
701
702 public Footer(int itemViewType) {
703 mItemViewType = itemViewType;
704 }
705
706 public abstract View getView(View convertView, ViewGroup parent);
707
708 public int getItemViewType() {
709 return mItemViewType;
710 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700711 }
712
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700713 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700714 public LoadingFooter() {
715 super(1);
716 }
717
Jeff Sharkey20b32272013-09-03 15:25:52 -0700718 @Override
719 public View getView(View convertView, ViewGroup parent) {
720 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700721 final State state = getDisplayState(DirectoryFragment.this);
722
Jeff Sharkey20b32272013-09-03 15:25:52 -0700723 if (convertView == null) {
724 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700725 if (state.derivedMode == MODE_LIST) {
726 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
727 } else if (state.derivedMode == MODE_GRID) {
728 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
729 } else {
730 throw new IllegalStateException();
731 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700732 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700733
Jeff Sharkey20b32272013-09-03 15:25:52 -0700734 return convertView;
735 }
736 }
737
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700738 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700739 private final int mIcon;
740 private final String mMessage;
741
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700742 public MessageFooter(int itemViewType, int icon, String message) {
743 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700744 mIcon = icon;
745 mMessage = message;
746 }
747
748 @Override
749 public View getView(View convertView, ViewGroup parent) {
750 final Context context = parent.getContext();
751 final State state = getDisplayState(DirectoryFragment.this);
752
753 if (convertView == null) {
754 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700755 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700756 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700757 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700758 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
759 } else {
760 throw new IllegalStateException();
761 }
762 }
763
764 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
765 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
766 icon.setImageResource(mIcon);
767 title.setText(mMessage);
768 return convertView;
769 }
770 }
771
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700772 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700773 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700774 private int mCursorCount;
775
776 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700777
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700778 public void swapResult(DirectoryResult result) {
779 mCursor = result != null ? result.cursor : null;
780 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700781
782 mFooters.clear();
783
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700784 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700785 if (extras != null) {
786 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
787 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700788 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700789 }
790 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
791 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700792 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700793 }
794 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
795 mFooters.add(new LoadingFooter());
796 }
797 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700798
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700799 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700800 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700801 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700802 }
803
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700804 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700805 mEmptyView.setVisibility(View.VISIBLE);
806 } else {
807 mEmptyView.setVisibility(View.GONE);
808 }
809
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700810 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700811 }
812
813 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700814 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700815 if (position < mCursorCount) {
816 return getDocumentView(position, convertView, parent);
817 } else {
818 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700819 convertView = mFooters.get(position).getView(convertView, parent);
820 // Only the view itself is disabled; contents inside shouldn't
821 // be dimmed.
822 convertView.setEnabled(false);
823 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700824 }
825 }
826
827 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700828 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700829 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700830
Jeff Sharkey9656a532013-09-13 13:42:19 -0700831 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
832
Jeff Sharkey873daa32013-08-18 17:38:20 -0700833 final RootsCache roots = DocumentsApplication.getRootsCache(context);
834 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
835 context, mThumbSize);
836
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700837 if (convertView == null) {
838 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700839 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700840 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700841 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700842 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
843 } else {
844 throw new IllegalStateException();
845 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700846 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700847
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700848 final Cursor cursor = getItem(position);
849
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700850 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
851 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700852 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
853 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
854 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
855 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
856 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
857 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
858 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
859 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700860
Jeff Sharkey9656a532013-09-13 13:42:19 -0700861 final View line1 = convertView.findViewById(R.id.line1);
862 final View line2 = convertView.findViewById(R.id.line2);
863
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700864 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
865 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700866 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
867 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700868 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700869 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
870 final TextView date = (TextView) convertView.findViewById(R.id.date);
871 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700872
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700873 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700874 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700875 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700876 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700877 }
878
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700879 iconMime.animate().cancel();
880 iconThumb.animate().cancel();
881
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700882 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700883 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700884 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700885 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700886
Jeff Sharkey7e544612014-08-29 15:38:27 -0700887 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
888 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
889
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700890 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700891 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700892 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700893 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700894 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700895 iconThumb.setImageBitmap(cachedResult);
896 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700897 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700898 iconThumb.setImageDrawable(null);
899 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700900 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700901 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700902 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700903 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700904 }
905
906 // Always throw MIME icon into place, even when a thumbnail is being
907 // loaded in background.
908 if (cacheHit) {
909 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700910 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700911 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700912 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700913 iconMime.setAlpha(1f);
914 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700915 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700916 if (docIcon != 0) {
917 iconMime.setImageDrawable(
918 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
919 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700920 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
921 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700922 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700923 }
924
Jeff Sharkey9656a532013-09-13 13:42:19 -0700925 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700926 boolean hasLine2 = false;
927
Jeff Sharkey9656a532013-09-13 13:42:19 -0700928 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
929 if (!hideTitle) {
930 title.setText(docDisplayName);
931 hasLine1 = true;
932 }
933
934 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700935 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700936 // We've already had to enumerate roots before any results can
937 // be shown, so this will never block.
938 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700939 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700940 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700941 } else {
942 iconDrawable = root.loadIcon(context);
943 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700944
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700945 if (summary != null) {
946 final boolean alwaysShowSummary = getResources()
947 .getBoolean(R.bool.always_show_summary);
948 if (alwaysShowSummary) {
949 summary.setText(root.getDirectoryString());
950 summary.setVisibility(View.VISIBLE);
951 hasLine2 = true;
952 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700953 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700954 // No summary needed if icon speaks for itself
955 summary.setVisibility(View.INVISIBLE);
956 } else {
957 summary.setText(root.getDirectoryString());
958 summary.setVisibility(View.VISIBLE);
959 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
960 hasLine2 = true;
961 }
962 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700963 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700964 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700965 // Directories showing thumbnails in grid mode get a little icon
966 // hint to remind user they're a directory.
967 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
968 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700969 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700970 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700971 }
972
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700973 if (summary != null) {
974 if (docSummary != null) {
975 summary.setText(docSummary);
976 summary.setVisibility(View.VISIBLE);
977 hasLine2 = true;
978 } else {
979 summary.setVisibility(View.INVISIBLE);
980 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700981 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700982 }
983
Jeff Sharkey9656a532013-09-13 13:42:19 -0700984 if (icon1 != null) icon1.setVisibility(View.GONE);
985 if (icon2 != null) icon2.setVisibility(View.GONE);
986
987 if (iconDrawable != null) {
988 if (hasLine1) {
989 icon1.setVisibility(View.VISIBLE);
990 icon1.setImageDrawable(iconDrawable);
991 } else {
992 icon2.setVisibility(View.VISIBLE);
993 icon2.setImageDrawable(iconDrawable);
994 }
995 }
996
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700997 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700998 date.setText(null);
999 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001000 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001001 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001002 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001003
1004 if (state.showSize) {
1005 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001006 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001007 size.setText(null);
1008 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001009 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001010 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001011 }
1012 } else {
1013 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001014 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001015
Jeff Sharkey9656a532013-09-13 13:42:19 -07001016 if (line1 != null) {
1017 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1018 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001019 if (line2 != null) {
1020 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1021 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001022
Jeff Sharkey7e544612014-08-29 15:38:27 -07001023 setEnabledRecursive(convertView, enabled);
1024
1025 iconMime.setAlpha(iconAlpha);
1026 iconThumb.setAlpha(iconAlpha);
1027 if (icon1 != null) icon1.setAlpha(iconAlpha);
1028 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001029
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001030 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001031 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001032
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001033 @Override
1034 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001035 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001036 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001037
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001038 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001039 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001040 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001041 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001042 return mCursor;
1043 } else {
1044 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001045 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001046 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001047
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001048 @Override
1049 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001050 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001051 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001052
1053 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001054 public int getViewTypeCount() {
1055 return 4;
1056 }
1057
1058 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001059 public int getItemViewType(int position) {
1060 if (position < mCursorCount) {
1061 return 0;
1062 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001063 position -= mCursorCount;
1064 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001065 }
1066 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001067 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001068
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001069 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1070 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001071 private final Uri mUri;
1072 private final ImageView mIconMime;
1073 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001074 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001075 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001076 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001077
Jeff Sharkey7e544612014-08-29 15:38:27 -07001078 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1079 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001080 mUri = uri;
1081 mIconMime = iconMime;
1082 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001083 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001084 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001085 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001086 }
1087
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001088 @Override
1089 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001090 cancel(false);
1091 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001092 }
1093
1094 @Override
1095 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001096 if (isCancelled()) return null;
1097
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001098 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001099 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001100
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001101 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001102 Bitmap result = null;
1103 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001104 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1105 resolver, mUri.getAuthority());
1106 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001107 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001108 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1109 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001110 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001111 }
1112 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001113 if (!(e instanceof OperationCanceledException)) {
1114 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1115 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001116 } finally {
1117 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001118 }
1119 return result;
1120 }
1121
1122 @Override
1123 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001124 if (mIconThumb.getTag() == this && result != null) {
1125 mIconThumb.setTag(null);
1126 mIconThumb.setImageBitmap(result);
1127
Jeff Sharkey7e544612014-08-29 15:38:27 -07001128 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001129 mIconMime.animate().alpha(0f).start();
1130 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001131 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001132 }
1133 }
1134 }
1135
1136 private static String formatTime(Context context, long when) {
1137 // TODO: DateUtils should make this easier
1138 Time then = new Time();
1139 then.set(when);
1140 Time now = new Time();
1141 now.setToNow();
1142
1143 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1144 | DateUtils.FORMAT_ABBREV_ALL;
1145
1146 if (then.year != now.year) {
1147 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1148 } else if (then.yearDay != now.yearDay) {
1149 flags |= DateUtils.FORMAT_SHOW_DATE;
1150 } else {
1151 flags |= DateUtils.FORMAT_SHOW_TIME;
1152 }
1153
1154 return DateUtils.formatDateTime(context, when, flags);
1155 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001156
1157 private String findCommonMimeType(List<String> mimeTypes) {
1158 String[] commonType = mimeTypes.get(0).split("/");
1159 if (commonType.length != 2) {
1160 return "*/*";
1161 }
1162
1163 for (int i = 1; i < mimeTypes.size(); i++) {
1164 String[] type = mimeTypes.get(i).split("/");
1165 if (type.length != 2) continue;
1166
1167 if (!commonType[1].equals(type[1])) {
1168 commonType[1] = "*";
1169 }
1170
1171 if (!commonType[0].equals(type[0])) {
1172 commonType[0] = "*";
1173 commonType[1] = "*";
1174 break;
1175 }
1176 }
1177
1178 return commonType[0] + "/" + commonType[1];
1179 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001180
1181 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001182 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001183 if (v.isEnabled() == enabled) return;
1184 v.setEnabled(enabled);
1185
1186 if (v instanceof ViewGroup) {
1187 final ViewGroup vg = (ViewGroup) v;
1188 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1189 setEnabledRecursive(vg.getChildAt(i), enabled);
1190 }
1191 }
1192 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001193
1194 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1195 final State state = getDisplayState(DirectoryFragment.this);
1196
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001197 // Directories are always enabled
1198 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1199 return true;
1200 }
1201
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001202 // Read-only files are disabled when creating
1203 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1204 return false;
1205 }
1206
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001207 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1208 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001209}