blob: 05dd16ce7bba9a99775fca25bd91a9dccdcdd7cb [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 Kwacb4461f2015-05-05 11:50:11 -070031
Ben Kwaf5858932015-04-07 15:43:39 -070032import android.app.Activity;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070033import android.app.ActivityManager;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070034import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070035import android.app.FragmentManager;
36import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.app.LoaderManager.LoaderCallbacks;
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -070038import android.content.ClipData;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070039import android.content.ContentProviderClient;
Jeff Sharkey873daa32013-08-18 17:38:20 -070040import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070041import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070043import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.content.Loader;
Jeff Sharkey083d7e12014-07-27 21:01:45 -070045import android.content.res.Resources;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070046import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070047import android.graphics.Bitmap;
48import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070049import android.graphics.drawable.Drawable;
Jeff Sharkeydb884f52013-09-21 15:11:19 -070050import android.graphics.drawable.InsetDrawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070052import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070053import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070054import android.os.CancellationSignal;
Jeff Sharkeye39a89b2013-10-29 11:56:37 -070055import android.os.OperationCanceledException;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070056import android.os.Parcelable;
Ben Kwacb4461f2015-05-05 11:50:11 -070057import android.os.SystemProperties;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070058import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070059import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070060import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070061import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070062import android.text.format.Time;
63import android.util.Log;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -070064import android.util.SparseArray;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070065import android.util.SparseBooleanArray;
66import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070067import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070068import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070069import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070070import android.view.View;
71import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070072import android.widget.AbsListView;
73import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070074import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070075import android.widget.AdapterView;
76import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070077import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070078import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070079import android.widget.ImageView;
80import android.widget.ListView;
81import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070082import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070083
Steve McKayd0a2a2c2015-03-25 14:35:33 -070084import com.android.documentsui.BaseActivity.State;
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070085import com.android.documentsui.ProviderExecutor.Preemptable;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070086import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070087import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090088import com.android.documentsui.model.DocumentStack;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070089import com.android.documentsui.model.RootInfo;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070090import com.google.android.collect.Lists;
91
92import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070093import java.util.List;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070094
95/**
96 * Display the documents inside a single directory.
97 */
98public class DirectoryFragment extends Fragment {
99
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700100 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700101 private ListView mListView;
102 private GridView mGridView;
103
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700104 private AbsListView mCurrentView;
105
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700106 public static final int TYPE_NORMAL = 1;
107 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700108 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700109
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700110 public static final int ANIM_NONE = 1;
111 public static final int ANIM_SIDE = 2;
112 public static final int ANIM_DOWN = 3;
113 public static final int ANIM_UP = 4;
114
Ben Kwaf5858932015-04-07 15:43:39 -0700115 public static final int REQUEST_COPY_DESTINATION = 1;
116
Jeff Sharkey5b535922013-08-02 15:55:26 -0700117 private int mType = TYPE_NORMAL;
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700118 private String mStateKey;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700119
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700120 private int mLastMode = MODE_UNKNOWN;
121 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700122 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700123
Jeff Sharkey9656a532013-09-13 13:42:19 -0700124 private boolean mHideGridTitles = false;
125
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700126 private boolean mSvelteRecents;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700127 private Point mThumbSize;
128
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700129 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700130 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700131
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700132 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700133 private static final String EXTRA_ROOT = "root";
134 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700135 private static final String EXTRA_QUERY = "query";
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700136 private static final String EXTRA_IGNORE_STATE = "ignoreState";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700137
Jeff Sharkeyc0075dc2013-10-25 17:12:49 -0700138 private final int mLoaderId = 42;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700139
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700140 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
141 show(fm, TYPE_NORMAL, root, doc, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700142 }
143
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700144 public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
145 show(fm, TYPE_SEARCH, root, null, query, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700146 }
147
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700148 public static void showRecentsOpen(FragmentManager fm, int anim) {
149 show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700150 }
151
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700152 private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
153 String query, int anim) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700154 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700155 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700156 args.putParcelable(EXTRA_ROOT, root);
157 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700158 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700159
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700160 final FragmentTransaction ft = fm.beginTransaction();
161 switch (anim) {
162 case ANIM_SIDE:
163 args.putBoolean(EXTRA_IGNORE_STATE, true);
164 break;
165 case ANIM_DOWN:
166 args.putBoolean(EXTRA_IGNORE_STATE, true);
167 ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
168 break;
169 case ANIM_UP:
170 ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
171 break;
172 }
173
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700174 final DirectoryFragment fragment = new DirectoryFragment();
175 fragment.setArguments(args);
176
Jeff Sharkey76112212013-08-06 11:26:10 -0700177 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700178 ft.commitAllowingStateLoss();
179 }
180
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700181 private static String buildStateKey(RootInfo root, DocumentInfo doc) {
182 final StringBuilder builder = new StringBuilder();
183 builder.append(root != null ? root.authority : "null").append(';');
184 builder.append(root != null ? root.rootId : "null").append(';');
185 builder.append(doc != null ? doc.documentId : "null");
186 return builder.toString();
187 }
188
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700189 public static DirectoryFragment get(FragmentManager fm) {
190 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700191 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700192 }
193
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700194 @Override
195 public View onCreateView(
196 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
197 final Context context = inflater.getContext();
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700198 final Resources res = context.getResources();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700199 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
200
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700201 mEmptyView = view.findViewById(android.R.id.empty);
202
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700203 mListView = (ListView) view.findViewById(R.id.list);
204 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700205 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700206 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700207
Jeff Sharkey083d7e12014-07-27 21:01:45 -0700208 // Indent our list divider to align with text
209 final Drawable divider = mListView.getDivider();
210 final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
211 final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
212 if (insetLeft) {
213 mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
214 } else {
215 mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
216 }
217
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700218 mGridView = (GridView) view.findViewById(R.id.grid);
219 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700220 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700221 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700222
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700223 return view;
224 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700225
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700226 @Override
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700227 public void onDestroyView() {
228 super.onDestroyView();
229
230 // Cancel any outstanding thumbnail requests
231 final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
232 final int count = target.getChildCount();
233 for (int i = 0; i < count; i++) {
234 final View view = target.getChildAt(i);
235 mRecycleListener.onMovedToScrapHeap(view);
236 }
Jeff Sharkeyfaaeb392013-10-04 14:44:56 -0700237
238 // Tear down any selection in progress
239 mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
240 mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700241 }
242
243 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700244 public void onActivityCreated(Bundle savedInstanceState) {
245 super.onActivityCreated(savedInstanceState);
246
247 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700248 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700249
Jeff Sharkey9656a532013-09-13 13:42:19 -0700250 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
251 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
252
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700253 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700254 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700255 mStateKey = buildStateKey(root, doc);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700256
Jeff Sharkey9656a532013-09-13 13:42:19 -0700257 if (mType == TYPE_RECENT_OPEN) {
258 // Hide titles when showing recents for picking images/videos
259 mHideGridTitles = MimePredicate.mimeMatches(
260 MimePredicate.VISUAL_MIMES, state.acceptMimes);
261 } else {
262 mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
263 }
264
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700265 final ActivityManager am = (ActivityManager) context.getSystemService(
266 Context.ACTIVITY_SERVICE);
267 mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
268
Jeff Sharkey46899c82013-08-18 22:26:48 -0700269 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700270 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700271 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700272 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700273
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700274 Uri contentsUri;
275 switch (mType) {
276 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700277 contentsUri = DocumentsContract.buildChildDocumentsUri(
278 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700279 if (state.action == ACTION_MANAGE) {
280 contentsUri = DocumentsContract.setManageMode(contentsUri);
281 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700282 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700283 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700284 case TYPE_SEARCH:
285 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700286 root.authority, root.rootId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700287 if (state.action == ACTION_MANAGE) {
288 contentsUri = DocumentsContract.setManageMode(contentsUri);
289 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700290 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700291 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700292 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700293 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700294 return new RecentLoader(context, roots, state);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700295 default:
296 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700297 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700298 }
299
300 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700301 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700302 if (!isAdded()) return;
303
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700304 mAdapter.swapResult(result);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700305
306 // Push latest state up to UI
307 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700308 if (result.mode != MODE_UNKNOWN) {
309 state.derivedMode = result.mode;
310 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700311 state.derivedSortOrder = result.sortOrder;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700312 ((BaseActivity) context).onStateChanged();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700313
314 updateDisplayState();
315
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700316 // When launched into empty recents, show drawer
Steve McKayb68dd222015-04-20 17:18:15 -0700317 if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched &&
318 context instanceof DocumentsActivity) {
319 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700320 }
321
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700322 // Restore any previous instance state
323 final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
324 if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
325 getView().restoreHierarchyState(container);
326 } else if (mLastSortOrder != state.derivedSortOrder) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700327 mListView.smoothScrollToPosition(0);
328 mGridView.smoothScrollToPosition(0);
329 }
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700330
331 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700332 }
333
334 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700335 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700336 mAdapter.swapResult(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700337 }
338 };
339
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700340 // Kick off loader at least once
341 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
342
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700343 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700344 }
345
Jeff Sharkey42d26792013-09-06 13:22:09 -0700346 @Override
Ben Kwaf5858932015-04-07 15:43:39 -0700347 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Ben Kwaf5858932015-04-07 15:43:39 -0700348 // There's only one request code right now. Replace this with a switch statement or
349 // something more scalable when more codes are added.
350 if (requestCode != REQUEST_COPY_DESTINATION) {
351 return;
352 }
353 if (resultCode == Activity.RESULT_CANCELED || data == null) {
354 // User pressed the back button or otherwise cancelled the destination pick. Don't
355 // proceed with the copy.
356 return;
357 }
358
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +0900359 CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy,
Ben Kwacb4461f2015-05-05 11:50:11 -0700360 (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK),
361 data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE));
Ben Kwaf5858932015-04-07 15:43:39 -0700362 }
363
364 @Override
Jeff Sharkeyc8ae7a52013-09-18 16:26:49 -0700365 public void onStop() {
366 super.onStop();
367
368 // Remember last scroll location
369 final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
370 getView().saveHierarchyState(container);
371 final State state = getDisplayState(this);
372 state.dirState.put(mStateKey, container);
373 }
374
375 @Override
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700376 public void onResume() {
377 super.onResume();
Jeff Sharkey42d26792013-09-06 13:22:09 -0700378 updateDisplayState();
379 }
380
Jeff Sharkeye8d13ea2014-08-08 15:10:03 -0700381 public void onDisplayStateChanged() {
382 updateDisplayState();
383 }
384
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700385 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700386 // Sort order change always triggers reload; we'll trigger state change
387 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700388 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
389 }
390
391 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700392 final ContentResolver resolver = getActivity().getContentResolver();
393 final State state = getDisplayState(this);
394
395 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
396 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
397
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700398 if (root != null && doc != null) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700399 final Uri stateUri = RecentsProvider.buildState(
400 root.authority, root.rootId, doc.documentId);
401 final ContentValues values = new ContentValues();
402 values.put(StateColumns.MODE, state.userMode);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700403
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700404 new AsyncTask<Void, Void, Void>() {
405 @Override
406 protected Void doInBackground(Void... params) {
407 resolver.insert(stateUri, values);
408 return null;
409 }
410 }.execute();
411 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700412
413 // Mode change is just visual change; no need to kick loader, and
414 // deliver change event immediately.
415 state.derivedMode = state.userMode;
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700416 ((BaseActivity) getActivity()).onStateChanged();
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700417
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700418 updateDisplayState();
419 }
420
421 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700422 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700423
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700424 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700425 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700426 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700427
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700428 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
429 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700430
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700431 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700432 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700433 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
434 } else {
435 choiceMode = ListView.CHOICE_MODE_NONE;
436 }
437
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700438 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700439 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700440 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700441 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700442 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700443 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700444 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700445 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700446 mGridView.setChoiceMode(choiceMode);
447 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700448 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700449 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700450 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700451 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700452 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700453 mListView.setChoiceMode(choiceMode);
454 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700455 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700456 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700457 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700458
459 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700460 }
461
462 private OnItemClickListener mItemListener = new OnItemClickListener() {
463 @Override
464 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700465 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700466 if (cursor != null) {
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700467 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
468 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
469 if (isDocumentEnabled(docMimeType, docFlags)) {
470 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700471 ((BaseActivity) getActivity()).onDocumentPicked(doc);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700472 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700473 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700474 }
475 };
476
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700477 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
478 @Override
479 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
480 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
Stefan Wysocki43c97ea2014-03-04 11:27:55 +0100481 mode.setTitle(getResources()
482 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700483 return true;
484 }
485
486 @Override
487 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700488 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700489
490 final MenuItem open = menu.findItem(R.id.menu_open);
491 final MenuItem share = menu.findItem(R.id.menu_share);
492 final MenuItem delete = menu.findItem(R.id.menu_delete);
Ben Kwa41b26c12015-03-31 10:11:43 -0700493 final MenuItem copy = menu.findItem(R.id.menu_copy);
Ben Kwacb4461f2015-05-05 11:50:11 -0700494 final MenuItem move = menu.findItem(R.id.menu_move);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700495
Jeff Sharkey311a7d82015-04-11 21:27:21 -0700496 final boolean manageOrBrowse = (state.action == ACTION_MANAGE
497 || state.action == ACTION_BROWSE || state.action == ACTION_BROWSE_ALL);
498
499 open.setVisible(!manageOrBrowse);
500 share.setVisible(manageOrBrowse);
501 delete.setVisible(manageOrBrowse);
Ben Kwada1cea52015-04-22 10:33:53 -0700502 // Disable copying from the Recents view.
503 copy.setVisible(manageOrBrowse && mType != TYPE_RECENT_OPEN);
Ben Kwacb4461f2015-05-05 11:50:11 -0700504 move.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false));
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700505 return true;
506 }
507
508 @Override
509 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -0700510 final List<DocumentInfo> docs = getSelectedDocuments();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700511
Jeff Sharkey873daa32013-08-18 17:38:20 -0700512 final int id = item.getItemId();
513 if (id == R.id.menu_open) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700514 BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700515 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700516 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700517
518 } else if (id == R.id.menu_share) {
519 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700520 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700521 return true;
522
523 } else if (id == R.id.menu_delete) {
524 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700525 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700526 return true;
527
Ben Kwa41b26c12015-03-31 10:11:43 -0700528 } else if (id == R.id.menu_copy) {
Ben Kwacb4461f2015-05-05 11:50:11 -0700529 onTransferDocuments(docs, CopyService.TRANSFER_MODE_COPY);
530 mode.finish();
531 return true;
532
533 } else if (id == R.id.menu_move) {
534 onTransferDocuments(docs, CopyService.TRANSFER_MODE_MOVE);
Ben Kwa41b26c12015-03-31 10:11:43 -0700535 mode.finish();
536 return true;
537
Ben Kwa512a6ba2015-03-31 08:15:21 -0700538 } else if (id == R.id.menu_select_all) {
539 int count = mCurrentView.getCount();
540 for (int i = 0; i < count; i++) {
541 mCurrentView.setItemChecked(i, true);
542 }
543 updateDisplayState();
544 return true;
545
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700546 } else {
547 return false;
548 }
549 }
550
551 @Override
552 public void onDestroyActionMode(ActionMode mode) {
553 // ignored
554 }
555
556 @Override
557 public void onItemCheckedStateChanged(
558 ActionMode mode, int position, long id, boolean checked) {
559 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700560 // Directories and footer items cannot be checked
561 boolean valid = false;
562
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700563 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700564 if (cursor != null) {
565 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
Jeff Sharkey7cf49032013-09-26 10:54:16 -0700566 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Ben Kwaf527c632015-04-08 15:03:35 -0700567 valid = isDocumentEnabled(docMimeType, docFlags);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700568 }
569
570 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700571 mCurrentView.setItemChecked(position, false);
572 }
573 }
574
575 mode.setTitle(getResources()
576 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
577 }
578 };
579
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700580 private RecyclerListener mRecycleListener = new RecyclerListener() {
581 @Override
582 public void onMovedToScrapHeap(View view) {
583 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
584 if (iconThumb != null) {
585 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
586 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700587 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700588 iconThumb.setTag(null);
589 }
590 }
591 }
592 };
593
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700594 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700595 Intent intent;
Ben Kwaf527c632015-04-08 15:03:35 -0700596
597 // Filter out directories - those can't be shared.
598 List<DocumentInfo> docsForSend = Lists.newArrayList();
599 for (DocumentInfo doc: docs) {
600 if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) {
601 docsForSend.add(doc);
602 }
603 }
604
605 if (docsForSend.size() == 1) {
606 final DocumentInfo doc = docsForSend.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700607
Jeff Sharkey873daa32013-08-18 17:38:20 -0700608 intent = new Intent(Intent.ACTION_SEND);
609 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
610 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700611 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700612 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700613
Ben Kwaf527c632015-04-08 15:03:35 -0700614 } else if (docsForSend.size() > 1) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700615 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
616 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
617 intent.addCategory(Intent.CATEGORY_DEFAULT);
618
619 final ArrayList<String> mimeTypes = Lists.newArrayList();
620 final ArrayList<Uri> uris = Lists.newArrayList();
Ben Kwaf527c632015-04-08 15:03:35 -0700621 for (DocumentInfo doc : docsForSend) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700622 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700623 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700624 }
625
626 intent.setType(findCommonMimeType(mimeTypes));
627 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
628
629 } else {
630 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700631 }
632
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700633 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700634 startActivity(intent);
635 }
636
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700637 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700638 final Context context = getActivity();
639 final ContentResolver resolver = context.getContentResolver();
640
641 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700642 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700643 if (!doc.isDeleteSupported()) {
644 Log.w(TAG, "Skipping " + doc);
645 hadTrouble = true;
646 continue;
647 }
648
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700649 ContentProviderClient client = null;
650 try {
651 client = DocumentsApplication.acquireUnstableProviderOrThrow(
652 resolver, doc.derivedUri.getAuthority());
653 DocumentsContract.deleteDocument(client, doc.derivedUri);
654 } catch (Exception e) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700655 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700656 hadTrouble = true;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700657 } finally {
658 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700659 }
660 }
661
662 if (hadTrouble) {
663 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
664 }
665 }
666
Ben Kwacb4461f2015-05-05 11:50:11 -0700667 private void onTransferDocuments(List<DocumentInfo> docs, int mode) {
Daichi Hironof500ef22015-04-17 16:19:15 +0900668 getDisplayState(this).selectedDocumentsForCopy = docs;
Ben Kwa41b26c12015-03-31 10:11:43 -0700669
Ben Kwaf5858932015-04-07 15:43:39 -0700670 // Pop up a dialog to pick a destination. This is inadequate but works for now.
671 // TODO: Implement a picker that is to spec.
Daichi Hironocaadd412015-04-10 15:50:38 +0900672 final Intent intent = new Intent(
Daichi Hirono22574ed2015-04-15 13:41:18 +0900673 BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION,
Daichi Hironocaadd412015-04-10 15:50:38 +0900674 Uri.EMPTY,
675 getActivity(),
676 DocumentsActivity.class);
Daichi Hironof2a822d2015-04-14 17:12:54 +0900677 boolean directoryCopy = false;
678 for (DocumentInfo info : docs) {
679 if (Document.MIME_TYPE_DIR.equals(info.mimeType)) {
680 directoryCopy = true;
681 break;
682 }
683 }
Daichi Hirono22574ed2015-04-15 13:41:18 +0900684 intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy);
Ben Kwacb4461f2015-05-05 11:50:11 -0700685 intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode);
Ben Kwaf5858932015-04-07 15:43:39 -0700686 startActivityForResult(intent, REQUEST_COPY_DESTINATION);
Ben Kwa41b26c12015-03-31 10:11:43 -0700687 }
688
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700689 private static State getDisplayState(Fragment fragment) {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700690 return ((BaseActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700691 }
692
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700693 private static abstract class Footer {
694 private final int mItemViewType;
695
696 public Footer(int itemViewType) {
697 mItemViewType = itemViewType;
698 }
699
700 public abstract View getView(View convertView, ViewGroup parent);
701
702 public int getItemViewType() {
703 return mItemViewType;
704 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700705 }
706
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700707 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700708 public LoadingFooter() {
709 super(1);
710 }
711
Jeff Sharkey20b32272013-09-03 15:25:52 -0700712 @Override
713 public View getView(View convertView, ViewGroup parent) {
714 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700715 final State state = getDisplayState(DirectoryFragment.this);
716
Jeff Sharkey20b32272013-09-03 15:25:52 -0700717 if (convertView == null) {
718 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700719 if (state.derivedMode == MODE_LIST) {
720 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
721 } else if (state.derivedMode == MODE_GRID) {
722 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
723 } else {
724 throw new IllegalStateException();
725 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700726 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700727
Jeff Sharkey20b32272013-09-03 15:25:52 -0700728 return convertView;
729 }
730 }
731
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700732 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700733 private final int mIcon;
734 private final String mMessage;
735
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700736 public MessageFooter(int itemViewType, int icon, String message) {
737 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700738 mIcon = icon;
739 mMessage = message;
740 }
741
742 @Override
743 public View getView(View convertView, ViewGroup parent) {
744 final Context context = parent.getContext();
745 final State state = getDisplayState(DirectoryFragment.this);
746
747 if (convertView == null) {
748 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700749 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700750 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700751 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700752 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
753 } else {
754 throw new IllegalStateException();
755 }
756 }
757
758 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
759 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
760 icon.setImageResource(mIcon);
761 title.setText(mMessage);
762 return convertView;
763 }
764 }
765
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700766 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700767 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700768 private int mCursorCount;
769
770 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700771
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700772 public void swapResult(DirectoryResult result) {
773 mCursor = result != null ? result.cursor : null;
774 mCursorCount = mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700775
776 mFooters.clear();
777
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700778 final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700779 if (extras != null) {
780 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
781 if (info != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700782 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700783 }
784 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
785 if (error != null) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700786 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700787 }
788 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
789 mFooters.add(new LoadingFooter());
790 }
791 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700792
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700793 if (result != null && result.exception != null) {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700794 mFooters.add(new MessageFooter(
Jeff Sharkey34c54092014-08-08 13:08:56 -0700795 3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700796 }
797
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700798 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700799 mEmptyView.setVisibility(View.VISIBLE);
800 } else {
801 mEmptyView.setVisibility(View.GONE);
802 }
803
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700804 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700805 }
806
807 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700808 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700809 if (position < mCursorCount) {
810 return getDocumentView(position, convertView, parent);
811 } else {
812 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700813 convertView = mFooters.get(position).getView(convertView, parent);
814 // Only the view itself is disabled; contents inside shouldn't
815 // be dimmed.
816 convertView.setEnabled(false);
817 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700818 }
819 }
820
821 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700822 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700823 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700824
Jeff Sharkey9656a532013-09-13 13:42:19 -0700825 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
826
Jeff Sharkey873daa32013-08-18 17:38:20 -0700827 final RootsCache roots = DocumentsApplication.getRootsCache(context);
828 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
829 context, mThumbSize);
830
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700831 if (convertView == null) {
832 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700833 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700834 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700835 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700836 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
837 } else {
838 throw new IllegalStateException();
839 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700840 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700841
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700842 final Cursor cursor = getItem(position);
843
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700844 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
845 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700846 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
847 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
848 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
849 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
850 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
851 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
852 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
853 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700854
Jeff Sharkey9656a532013-09-13 13:42:19 -0700855 final View line1 = convertView.findViewById(R.id.line1);
856 final View line2 = convertView.findViewById(R.id.line2);
857
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700858 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
859 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700860 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
861 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700862 final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700863 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
864 final TextView date = (TextView) convertView.findViewById(R.id.date);
865 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700866
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700867 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700868 if (oldTask != null) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700869 oldTask.preempt();
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700870 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700871 }
872
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700873 iconMime.animate().cancel();
874 iconThumb.animate().cancel();
875
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700876 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700877 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkey9656a532013-09-13 13:42:19 -0700878 || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700879 final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700880
Jeff Sharkey7e544612014-08-29 15:38:27 -0700881 final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
882 final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f;
883
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700884 boolean cacheHit = false;
Jeff Sharkey9656a532013-09-13 13:42:19 -0700885 if (showThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700886 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700887 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700888 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700889 iconThumb.setImageBitmap(cachedResult);
890 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700891 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700892 iconThumb.setImageDrawable(null);
893 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
Jeff Sharkey7e544612014-08-29 15:38:27 -0700894 uri, iconMime, iconThumb, mThumbSize, iconAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700895 iconThumb.setTag(task);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -0700896 ProviderExecutor.forAuthority(docAuthority).execute(task);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700897 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700898 }
899
900 // Always throw MIME icon into place, even when a thumbnail is being
901 // loaded in background.
902 if (cacheHit) {
903 iconMime.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700904 iconMime.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700905 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700906 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700907 iconMime.setAlpha(1f);
908 iconThumb.setAlpha(0f);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700909 iconThumb.setImageDrawable(null);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700910 if (docIcon != 0) {
911 iconMime.setImageDrawable(
912 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
913 } else {
Jeff Sharkey2ceff512013-09-18 18:03:49 -0700914 iconMime.setImageDrawable(IconUtils.loadMimeIcon(
915 context, docMimeType, docAuthority, docId, state.derivedMode));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700916 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700917 }
918
Jeff Sharkey9656a532013-09-13 13:42:19 -0700919 boolean hasLine1 = false;
Jeff Sharkey42d26792013-09-06 13:22:09 -0700920 boolean hasLine2 = false;
921
Jeff Sharkey9656a532013-09-13 13:42:19 -0700922 final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
923 if (!hideTitle) {
924 title.setText(docDisplayName);
925 hasLine1 = true;
926 }
927
928 Drawable iconDrawable = null;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700929 if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700930 // We've already had to enumerate roots before any results can
931 // be shown, so this will never block.
932 final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700933 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey34c54092014-08-08 13:08:56 -0700934 iconDrawable = root.loadGridIcon(context);
Jeff Sharkey93cdbc22014-07-29 17:33:36 -0700935 } else {
936 iconDrawable = root.loadIcon(context);
937 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700938
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700939 if (summary != null) {
940 final boolean alwaysShowSummary = getResources()
941 .getBoolean(R.bool.always_show_summary);
942 if (alwaysShowSummary) {
943 summary.setText(root.getDirectoryString());
944 summary.setVisibility(View.VISIBLE);
945 hasLine2 = true;
946 } else {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700947 if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700948 // No summary needed if icon speaks for itself
949 summary.setVisibility(View.INVISIBLE);
950 } else {
951 summary.setText(root.getDirectoryString());
952 summary.setVisibility(View.VISIBLE);
953 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
954 hasLine2 = true;
955 }
956 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700957 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700958 } else {
Jeff Sharkey9656a532013-09-13 13:42:19 -0700959 // Directories showing thumbnails in grid mode get a little icon
960 // hint to remind user they're a directory.
961 if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
962 && showThumbnail) {
Jeff Sharkey7e544612014-08-29 15:38:27 -0700963 iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder,
Jeff Sharkey34c54092014-08-08 13:08:56 -0700964 android.R.attr.textColorPrimaryInverse);
Jeff Sharkey9656a532013-09-13 13:42:19 -0700965 }
966
Jeff Sharkey7d58fc62013-09-12 16:25:02 -0700967 if (summary != null) {
968 if (docSummary != null) {
969 summary.setText(docSummary);
970 summary.setVisibility(View.VISIBLE);
971 hasLine2 = true;
972 } else {
973 summary.setVisibility(View.INVISIBLE);
974 }
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700975 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700976 }
977
Jeff Sharkey9656a532013-09-13 13:42:19 -0700978 if (icon1 != null) icon1.setVisibility(View.GONE);
979 if (icon2 != null) icon2.setVisibility(View.GONE);
980
981 if (iconDrawable != null) {
982 if (hasLine1) {
983 icon1.setVisibility(View.VISIBLE);
984 icon1.setImageDrawable(iconDrawable);
985 } else {
986 icon2.setVisibility(View.VISIBLE);
987 icon2.setImageDrawable(iconDrawable);
988 }
989 }
990
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700991 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700992 date.setText(null);
993 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700994 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700995 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700996 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700997
998 if (state.showSize) {
999 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001000 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001001 size.setText(null);
1002 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001003 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -07001004 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001005 }
1006 } else {
1007 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001008 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001009
Jeff Sharkey9656a532013-09-13 13:42:19 -07001010 if (line1 != null) {
1011 line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
1012 }
Jeff Sharkey7d58fc62013-09-12 16:25:02 -07001013 if (line2 != null) {
1014 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
1015 }
Jeff Sharkey42d26792013-09-06 13:22:09 -07001016
Jeff Sharkey7e544612014-08-29 15:38:27 -07001017 setEnabledRecursive(convertView, enabled);
1018
1019 iconMime.setAlpha(iconAlpha);
1020 iconThumb.setAlpha(iconAlpha);
1021 if (icon1 != null) icon1.setAlpha(iconAlpha);
1022 if (icon2 != null) icon2.setAlpha(iconAlpha);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001023
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001024 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001025 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001026
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001027 @Override
1028 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001029 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001030 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001031
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001032 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001033 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -07001034 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001035 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -07001036 return mCursor;
1037 } else {
1038 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001039 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001040 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001041
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001042 @Override
1043 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -07001044 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001045 }
Jeff Sharkey20b32272013-09-03 15:25:52 -07001046
1047 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001048 public int getViewTypeCount() {
1049 return 4;
1050 }
1051
1052 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -07001053 public int getItemViewType(int position) {
1054 if (position < mCursorCount) {
1055 return 0;
1056 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001057 position -= mCursorCount;
1058 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -07001059 }
1060 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001061 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001062
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001063 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
1064 implements Preemptable {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001065 private final Uri mUri;
1066 private final ImageView mIconMime;
1067 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001068 private final Point mThumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001069 private final float mTargetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001070 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001071
Jeff Sharkey7e544612014-08-29 15:38:27 -07001072 public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize,
1073 float targetAlpha) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001074 mUri = uri;
1075 mIconMime = iconMime;
1076 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -07001077 mThumbSize = thumbSize;
Jeff Sharkey7e544612014-08-29 15:38:27 -07001078 mTargetAlpha = targetAlpha;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001079 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001080 }
1081
Jeff Sharkey753a3ae2013-10-22 17:09:44 -07001082 @Override
1083 public void preempt() {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001084 cancel(false);
1085 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001086 }
1087
1088 @Override
1089 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001090 if (isCancelled()) return null;
1091
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001092 final Context context = mIconThumb.getContext();
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001093 final ContentResolver resolver = context.getContentResolver();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001094
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001095 ContentProviderClient client = null;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001096 Bitmap result = null;
1097 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001098 client = DocumentsApplication.acquireUnstableProviderOrThrow(
1099 resolver, mUri.getAuthority());
1100 result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001101 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -07001102 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
1103 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001104 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001105 }
1106 } catch (Exception e) {
Jeff Sharkeye39a89b2013-10-29 11:56:37 -07001107 if (!(e instanceof OperationCanceledException)) {
1108 Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
1109 }
Jeff Sharkey3fd11772013-09-30 14:26:27 -07001110 } finally {
1111 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001112 }
1113 return result;
1114 }
1115
1116 @Override
1117 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001118 if (mIconThumb.getTag() == this && result != null) {
1119 mIconThumb.setTag(null);
1120 mIconThumb.setImageBitmap(result);
1121
Jeff Sharkey7e544612014-08-29 15:38:27 -07001122 mIconMime.setAlpha(mTargetAlpha);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -07001123 mIconMime.animate().alpha(0f).start();
1124 mIconThumb.setAlpha(0f);
Jeff Sharkey7e544612014-08-29 15:38:27 -07001125 mIconThumb.animate().alpha(mTargetAlpha).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001126 }
1127 }
1128 }
1129
1130 private static String formatTime(Context context, long when) {
1131 // TODO: DateUtils should make this easier
1132 Time then = new Time();
1133 then.set(when);
1134 Time now = new Time();
1135 now.setToNow();
1136
1137 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
1138 | DateUtils.FORMAT_ABBREV_ALL;
1139
1140 if (then.year != now.year) {
1141 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
1142 } else if (then.yearDay != now.yearDay) {
1143 flags |= DateUtils.FORMAT_SHOW_DATE;
1144 } else {
1145 flags |= DateUtils.FORMAT_SHOW_TIME;
1146 }
1147
1148 return DateUtils.formatDateTime(context, when, flags);
1149 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -07001150
1151 private String findCommonMimeType(List<String> mimeTypes) {
1152 String[] commonType = mimeTypes.get(0).split("/");
1153 if (commonType.length != 2) {
1154 return "*/*";
1155 }
1156
1157 for (int i = 1; i < mimeTypes.size(); i++) {
1158 String[] type = mimeTypes.get(i).split("/");
1159 if (type.length != 2) continue;
1160
1161 if (!commonType[1].equals(type[1])) {
1162 commonType[1] = "*";
1163 }
1164
1165 if (!commonType[0].equals(type[0])) {
1166 commonType[0] = "*";
1167 commonType[1] = "*";
1168 break;
1169 }
1170 }
1171
1172 return commonType[0] + "/" + commonType[1];
1173 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001174
1175 private void setEnabledRecursive(View v, boolean enabled) {
Jeff Sharkey9656a532013-09-13 13:42:19 -07001176 if (v == null) return;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -07001177 if (v.isEnabled() == enabled) return;
1178 v.setEnabled(enabled);
1179
1180 if (v instanceof ViewGroup) {
1181 final ViewGroup vg = (ViewGroup) v;
1182 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
1183 setEnabledRecursive(vg.getChildAt(i), enabled);
1184 }
1185 }
1186 }
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001187
1188 private boolean isDocumentEnabled(String docMimeType, int docFlags) {
1189 final State state = getDisplayState(DirectoryFragment.this);
1190
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001191 // Directories are always enabled
1192 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
1193 return true;
1194 }
1195
Jeff Sharkey783ebc22013-09-26 19:42:52 -07001196 // Read-only files are disabled when creating
1197 if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
1198 return false;
1199 }
1200
Jeff Sharkey7cf49032013-09-26 10:54:16 -07001201 return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
1202 }
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001203
1204 public List<DocumentInfo> getSelectedDocuments() {
1205 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
1206 final List<DocumentInfo> docs = Lists.newArrayList();
1207 final int size = checked.size();
1208 for (int i = 0; i < size; i++) {
1209 if (checked.valueAt(i)) {
1210 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
1211 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
1212 docs.add(doc);
1213 }
1214 }
1215 return docs;
1216 }
1217
1218 private void copyFromClipData(ClipData clipData, DocumentInfo dstDir) {
1219 final List<DocumentInfo> srcDocs = getDocumentsFromClipData(clipData);
1220
1221 if (srcDocs.isEmpty())
1222 return;
1223
1224 final DocumentStack curStack = getDisplayState(this).stack;
1225 DocumentStack tmpStack = new DocumentStack();
1226 if (dstDir != null) {
1227 tmpStack.push(dstDir);
1228 tmpStack.addAll(curStack);
1229 } else {
1230 tmpStack = curStack;
1231 }
1232
Ben Kwacb4461f2015-05-05 11:50:11 -07001233 CopyService.start(getActivity(), srcDocs, tmpStack, CopyService.TRANSFER_MODE_COPY);
Vladislav Kaznacheev89b90332015-05-01 13:46:57 -07001234 }
1235
1236 private List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
1237 final List<DocumentInfo> srcDocs = Lists.newArrayList();
1238
1239 Context context = getActivity();
1240 final ContentResolver resolver = context.getContentResolver();
1241
1242 int itemCount = clipData.getItemCount();
1243 for (int i = 0; i < itemCount; ++i) {
1244 ClipData.Item item = clipData.getItemAt(i);
1245 Uri itemUri = item.getUri();
1246 if (itemUri != null && DocumentsContract.isDocumentUri(context, itemUri)) {
1247 try {
1248 Cursor cursor = resolver.query(itemUri, null, null, null, null);
1249 cursor.moveToPosition(0);
1250 srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority()));
1251 } catch (Exception e) {
1252 Log.e(TAG, e.getMessage());
1253 }
1254 }
1255 }
1256
1257 return srcDocs;
1258 }
1259
1260 private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) {
1261 Context context = getActivity();
1262 final ContentResolver resolver = context.getContentResolver();
1263 ClipData clipData = null;
1264 for (DocumentInfo doc : docs) {
1265 final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
1266 if (clipData == null) {
1267 clipData = ClipData.newUri(resolver, "", uri);
1268 } else {
1269 // TODO: update list of mime types in ClipData.
1270 clipData.addItem(new ClipData.Item(uri));
1271 }
1272 }
1273 return clipData;
1274 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001275}