blob: ba5a511661abfe58bf6fa76ae349a43f27cd7712 [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070019import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070020import static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE;
21import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
22import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070023import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
24import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070025import static com.android.documentsui.model.DocumentInfo.getCursorInt;
26import static com.android.documentsui.model.DocumentInfo.getCursorLong;
27import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070028
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070029import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070030import android.app.FragmentManager;
31import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070032import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey873daa32013-08-18 17:38:20 -070033import android.content.ContentResolver;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070034import android.content.ContentValues;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070035import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070036import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070038import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070039import android.graphics.Bitmap;
40import android.graphics.Point;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070041import android.graphics.drawable.Drawable;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070043import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070044import android.os.Bundle;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070045import android.os.CancellationSignal;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070046import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070047import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070048import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070049import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070050import android.text.format.Time;
51import android.util.Log;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070052import android.util.SparseBooleanArray;
53import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070055import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070056import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070057import android.view.View;
58import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070059import android.widget.AbsListView;
60import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070061import android.widget.AbsListView.RecyclerListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070062import android.widget.AdapterView;
63import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070064import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070065import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070066import android.widget.ImageView;
67import android.widget.ListView;
68import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070069import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070070
Jeff Sharkeyb3620442013-09-01 18:41:04 -070071import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070072import com.android.documentsui.RecentsProvider.StateColumns;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070073import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070074import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070075import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070076import com.google.android.collect.Lists;
77
78import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070079import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070080import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081
82/**
83 * Display the documents inside a single directory.
84 */
85public class DirectoryFragment extends Fragment {
86
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070087 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070088 private ListView mListView;
89 private GridView mGridView;
90
Jeff Sharkeyc317af82013-07-01 16:56:54 -070091 private AbsListView mCurrentView;
92
Jeff Sharkey724deeb2013-08-31 15:02:20 -070093 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070094
Jeff Sharkeya5defe32013-08-05 17:56:48 -070095 public static final int TYPE_NORMAL = 1;
96 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070097 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -070098
99 private int mType = TYPE_NORMAL;
100
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700101 private int mLastMode = MODE_UNKNOWN;
102 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700103 private boolean mLastShowSize = false;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700104
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700105 private Point mThumbSize;
106
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700107 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700108 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700109
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700110 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700111 private static final String EXTRA_ROOT = "root";
112 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700113 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700114
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700115 /**
116 * MIME types that should always show thumbnails in list mode.
117 */
118 private static final String[] LIST_THUMBNAIL_MIMES = new String[] { "image/*", "video/*" };
119
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700120 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700121
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700122 private final int mLoaderId = sLoaderId.incrementAndGet();
123
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700124 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc) {
125 show(fm, TYPE_NORMAL, root, doc, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700126 }
127
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700128 public static void showSearch(
129 FragmentManager fm, RootInfo root, DocumentInfo doc, String query) {
130 show(fm, TYPE_SEARCH, root, doc, query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700131 }
132
133 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700134 show(fm, TYPE_RECENT_OPEN, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700135 }
136
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700137 private static void show(
138 FragmentManager fm, int type, RootInfo root, DocumentInfo doc, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700139 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700140 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700141 args.putParcelable(EXTRA_ROOT, root);
142 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700143 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700144
145 final DirectoryFragment fragment = new DirectoryFragment();
146 fragment.setArguments(args);
147
148 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700149 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700150 ft.commitAllowingStateLoss();
151 }
152
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700153 public static DirectoryFragment get(FragmentManager fm) {
154 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700155 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700156 }
157
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700158 @Override
159 public View onCreateView(
160 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
161 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700162 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
163
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700164 mEmptyView = view.findViewById(android.R.id.empty);
165
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700166 mListView = (ListView) view.findViewById(R.id.list);
167 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700168 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700169 mListView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700170
171 mGridView = (GridView) view.findViewById(R.id.grid);
172 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700173 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700174 mGridView.setRecyclerListener(mRecycleListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700175
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700176 return view;
177 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700178
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700179 @Override
180 public void onActivityCreated(Bundle savedInstanceState) {
181 super.onActivityCreated(savedInstanceState);
182
183 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700184 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700185
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700186 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700187 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700188
Jeff Sharkey46899c82013-08-18 22:26:48 -0700189 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700190 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700191 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700192 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
193 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700194 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700195
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700196 Uri contentsUri;
197 switch (mType) {
198 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700199 contentsUri = DocumentsContract.buildChildDocumentsUri(
200 doc.authority, doc.documentId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700201 if (state.action == ACTION_MANAGE) {
202 contentsUri = DocumentsContract.setManageMode(contentsUri);
203 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700204 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700205 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700206 case TYPE_SEARCH:
207 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700208 doc.authority, doc.documentId, query);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700209 if (state.action == ACTION_MANAGE) {
210 contentsUri = DocumentsContract.setManageMode(contentsUri);
211 }
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700212 return new DirectoryLoader(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700213 context, mType, root, doc, contentsUri, state.userSortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700214 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700215 final RootsCache roots = DocumentsApplication.getRootsCache(context);
216 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700217 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700218 default:
219 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700220 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700221 }
222
223 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700224 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700225 if (!isAdded()) return;
226
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700227 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700228
229 // Push latest state up to UI
230 // TODO: if mode change was racing with us, don't overwrite it
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700231 state.derivedMode = result.mode;
232 state.derivedSortOrder = result.sortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700233 ((DocumentsActivity) context).onStateChanged();
234
235 updateDisplayState();
236
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700237 if (mLastSortOrder != state.derivedSortOrder) {
238 mLastSortOrder = state.derivedSortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700239 mListView.smoothScrollToPosition(0);
240 mGridView.smoothScrollToPosition(0);
241 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700242 }
243
244 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700245 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700246 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700247 }
248 };
249
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700250 // Kick off loader at least once
251 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
252
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700253 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700254 }
255
Jeff Sharkey42d26792013-09-06 13:22:09 -0700256 @Override
257 public void onStart() {
258 super.onStart();
259 updateDisplayState();
260 }
261
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700262 public void onUserSortOrderChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700263 // Sort order change always triggers reload; we'll trigger state change
264 // on the flip side.
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700265 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
266 }
267
268 public void onUserModeChanged() {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700269 final ContentResolver resolver = getActivity().getContentResolver();
270 final State state = getDisplayState(this);
271
272 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
273 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
274
275 final Uri stateUri = RecentsProvider.buildState(
276 root.authority, root.rootId, doc.documentId);
277 final ContentValues values = new ContentValues();
278 values.put(StateColumns.MODE, state.userMode);
279
280 new AsyncTask<Void, Void, Void>() {
281 @Override
282 protected Void doInBackground(Void... params) {
283 resolver.insert(stateUri, values);
284 return null;
285 }
286 }.execute();
287
288 // Mode change is just visual change; no need to kick loader, and
289 // deliver change event immediately.
290 state.derivedMode = state.userMode;
291 ((DocumentsActivity) getActivity()).onStateChanged();
292
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700293 updateDisplayState();
294 }
295
296 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700297 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700298
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700299 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700300
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700301 if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700302 mLastMode = state.derivedMode;
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700303 mLastShowSize = state.showSize;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700304
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700305 mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
306 mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700307
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700308 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700309 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700310 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
311 } else {
312 choiceMode = ListView.CHOICE_MODE_NONE;
313 }
314
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700315 final int thumbSize;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700316 if (state.derivedMode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700317 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700318 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700319 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700320 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700321 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700322 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700323 mGridView.setChoiceMode(choiceMode);
324 mCurrentView = mGridView;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700325 } else if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700326 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700327 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700328 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700329 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700330 mListView.setChoiceMode(choiceMode);
331 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700332 } else {
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700333 throw new IllegalStateException("Unknown state " + state.derivedMode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700334 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700335
336 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700337 }
338
339 private OnItemClickListener mItemListener = new OnItemClickListener() {
340 @Override
341 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700342 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700343 if (cursor != null) {
344 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
345 if (mFilter.apply(doc)) {
346 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
347 }
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700348 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700349 }
350 };
351
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700352 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
353 @Override
354 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
355 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
356 return true;
357 }
358
359 @Override
360 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700361 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700362
363 final MenuItem open = menu.findItem(R.id.menu_open);
364 final MenuItem share = menu.findItem(R.id.menu_share);
365 final MenuItem delete = menu.findItem(R.id.menu_delete);
366
367 final boolean manageMode = state.action == ACTION_MANAGE;
368 open.setVisible(!manageMode);
369 share.setVisible(manageMode);
370 delete.setVisible(manageMode);
371
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700372 return true;
373 }
374
375 @Override
376 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700377 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700378 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700379 final int size = checked.size();
380 for (int i = 0; i < size; i++) {
381 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700382 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700383 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700384 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700385 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700386 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700387
Jeff Sharkey873daa32013-08-18 17:38:20 -0700388 final int id = item.getItemId();
389 if (id == R.id.menu_open) {
390 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700391 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700392 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700393
394 } else if (id == R.id.menu_share) {
395 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700396 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700397 return true;
398
399 } else if (id == R.id.menu_delete) {
400 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700401 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700402 return true;
403
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700404 } else {
405 return false;
406 }
407 }
408
409 @Override
410 public void onDestroyActionMode(ActionMode mode) {
411 // ignored
412 }
413
414 @Override
415 public void onItemCheckedStateChanged(
416 ActionMode mode, int position, long id, boolean checked) {
417 if (checked) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700418 // Directories and footer items cannot be checked
419 boolean valid = false;
420
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700421 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700422 if (cursor != null) {
423 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
424
425 // Only valid if non-directory matches filter
426 final State state = getDisplayState(DirectoryFragment.this);
427 valid = !Document.MIME_TYPE_DIR.equals(docMimeType)
428 && MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
429 }
430
431 if (!valid) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700432 mCurrentView.setItemChecked(position, false);
433 }
434 }
435
436 mode.setTitle(getResources()
437 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
438 }
439 };
440
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700441 private RecyclerListener mRecycleListener = new RecyclerListener() {
442 @Override
443 public void onMovedToScrapHeap(View view) {
444 final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
445 if (iconThumb != null) {
446 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
447 if (oldTask != null) {
448 oldTask.reallyCancel();
449 iconThumb.setTag(null);
450 }
451 }
452 }
453 };
454
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700455 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700456 Intent intent;
457 if (docs.size() == 1) {
458 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700459
Jeff Sharkey873daa32013-08-18 17:38:20 -0700460 intent = new Intent(Intent.ACTION_SEND);
461 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
462 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700463 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700464 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700465
466 } else if (docs.size() > 1) {
467 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
468 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
469 intent.addCategory(Intent.CATEGORY_DEFAULT);
470
471 final ArrayList<String> mimeTypes = Lists.newArrayList();
472 final ArrayList<Uri> uris = Lists.newArrayList();
473 for (DocumentInfo doc : docs) {
474 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700475 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700476 }
477
478 intent.setType(findCommonMimeType(mimeTypes));
479 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
480
481 } else {
482 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700483 }
484
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700485 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700486 startActivity(intent);
487 }
488
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700489 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700490 final Context context = getActivity();
491 final ContentResolver resolver = context.getContentResolver();
492
493 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700494 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700495 if (!doc.isDeleteSupported()) {
496 Log.w(TAG, "Skipping " + doc);
497 hadTrouble = true;
498 continue;
499 }
500
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700501 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700502 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700503 hadTrouble = true;
504 }
505 }
506
507 if (hadTrouble) {
508 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
509 }
510 }
511
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700512 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700513 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700514 }
515
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700516 private static abstract class Footer {
517 private final int mItemViewType;
518
519 public Footer(int itemViewType) {
520 mItemViewType = itemViewType;
521 }
522
523 public abstract View getView(View convertView, ViewGroup parent);
524
525 public int getItemViewType() {
526 return mItemViewType;
527 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700528 }
529
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700530 private class LoadingFooter extends Footer {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700531 public LoadingFooter() {
532 super(1);
533 }
534
Jeff Sharkey20b32272013-09-03 15:25:52 -0700535 @Override
536 public View getView(View convertView, ViewGroup parent) {
537 final Context context = parent.getContext();
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700538 final State state = getDisplayState(DirectoryFragment.this);
539
Jeff Sharkey20b32272013-09-03 15:25:52 -0700540 if (convertView == null) {
541 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700542 if (state.derivedMode == MODE_LIST) {
543 convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
544 } else if (state.derivedMode == MODE_GRID) {
545 convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
546 } else {
547 throw new IllegalStateException();
548 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700549 }
Jeff Sharkey5e1884d2013-09-10 17:56:39 -0700550
Jeff Sharkey20b32272013-09-03 15:25:52 -0700551 return convertView;
552 }
553 }
554
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700555 private class MessageFooter extends Footer {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700556 private final int mIcon;
557 private final String mMessage;
558
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700559 public MessageFooter(int itemViewType, int icon, String message) {
560 super(itemViewType);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700561 mIcon = icon;
562 mMessage = message;
563 }
564
565 @Override
566 public View getView(View convertView, ViewGroup parent) {
567 final Context context = parent.getContext();
568 final State state = getDisplayState(DirectoryFragment.this);
569
570 if (convertView == null) {
571 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700572 if (state.derivedMode == MODE_LIST) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700573 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700574 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700575 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
576 } else {
577 throw new IllegalStateException();
578 }
579 }
580
581 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
582 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
583 icon.setImageResource(mIcon);
584 title.setText(mMessage);
585 return convertView;
586 }
587 }
588
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700589 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700590 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700591 private int mCursorCount;
592
593 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700594
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700595 public void swapCursor(Cursor cursor) {
596 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700597 mCursorCount = cursor != null ? cursor.getCount() : 0;
598
599 mFooters.clear();
600
601 final Bundle extras = cursor != null ? cursor.getExtras() : null;
602 if (extras != null) {
603 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
604 if (info != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700605 mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700606 }
607 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
608 if (error != null) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700609 mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700610 }
611 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
612 mFooters.add(new LoadingFooter());
613 }
614 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700615
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700616 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700617 mEmptyView.setVisibility(View.VISIBLE);
618 } else {
619 mEmptyView.setVisibility(View.GONE);
620 }
621
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700622 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700623 }
624
625 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700626 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700627 if (position < mCursorCount) {
628 return getDocumentView(position, convertView, parent);
629 } else {
630 position -= mCursorCount;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700631 convertView = mFooters.get(position).getView(convertView, parent);
632 // Only the view itself is disabled; contents inside shouldn't
633 // be dimmed.
634 convertView.setEnabled(false);
635 return convertView;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700636 }
637 }
638
639 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700640 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700641 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700642
Jeff Sharkey873daa32013-08-18 17:38:20 -0700643 final RootsCache roots = DocumentsApplication.getRootsCache(context);
644 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
645 context, mThumbSize);
646
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700647 if (convertView == null) {
648 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700649 if (state.derivedMode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700650 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700651 } else if (state.derivedMode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700652 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
653 } else {
654 throw new IllegalStateException();
655 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700656 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700657
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700658 final Cursor cursor = getItem(position);
659
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700660 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
661 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700662 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
663 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
664 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
665 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
666 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
667 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
668 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
669 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700670
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700671 final View icon = convertView.findViewById(android.R.id.icon);
672 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
673 final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700674 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700675 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700676 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
677 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
678 final TextView date = (TextView) convertView.findViewById(R.id.date);
679 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700680
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700681 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700682 if (oldTask != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700683 oldTask.reallyCancel();
684 iconThumb.setTag(null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700685 }
686
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700687 iconMime.animate().cancel();
688 iconThumb.animate().cancel();
689
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700690 final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700691 final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700692 || MimePredicate.mimeMatches(LIST_THUMBNAIL_MIMES, docMimeType);
693
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700694 boolean cacheHit = false;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700695 if (supportsThumbnail && allowThumbnail) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700696 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700697 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700698 if (cachedResult != null) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700699 iconThumb.setImageBitmap(cachedResult);
700 cacheHit = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700701 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700702 iconThumb.setImageDrawable(null);
703 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
704 uri, iconMime, iconThumb, mThumbSize);
705 iconThumb.setTag(task);
706 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700707 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700708 }
709
710 // Always throw MIME icon into place, even when a thumbnail is being
711 // loaded in background.
712 if (cacheHit) {
713 iconMime.setAlpha(0f);
714 iconThumb.setAlpha(1f);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700715 } else {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700716 iconMime.setAlpha(1f);
717 iconThumb.setAlpha(0f);
718 if (docIcon != 0) {
719 iconMime.setImageDrawable(
720 IconUtils.loadPackageIcon(context, docAuthority, docIcon));
721 } else {
722 iconMime.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
723 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700724 }
725
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700726 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700727
Jeff Sharkey42d26792013-09-06 13:22:09 -0700728 boolean hasLine2 = false;
729
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700730 if (mType == TYPE_RECENT_OPEN) {
731 final RootInfo root = roots.getRoot(docAuthority, docRootId);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700732 final Drawable iconDrawable = root.loadIcon(context);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700733 icon1.setVisibility(View.VISIBLE);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700734 icon1.setImageDrawable(iconDrawable);
735
736 if (iconDrawable != null && roots.isIconUnique(root)) {
737 // No summary needed if icon speaks for itself
738 summary.setVisibility(View.INVISIBLE);
739 } else {
740 summary.setText(root.getDirectoryString());
741 summary.setVisibility(View.VISIBLE);
742 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
743 hasLine2 = true;
744 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700745 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700746 icon1.setVisibility(View.GONE);
747 if (docSummary != null) {
748 summary.setText(docSummary);
749 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700750 hasLine2 = true;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700751 } else {
752 summary.setVisibility(View.INVISIBLE);
753 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700754 }
755
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700756 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700757 date.setText(null);
758 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700759 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700760 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700761 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700762
763 if (state.showSize) {
764 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700765 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700766 size.setText(null);
767 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700768 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700769 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700770 }
771 } else {
772 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700773 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700774
Jeff Sharkey42d26792013-09-06 13:22:09 -0700775 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
776
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700777 final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
778 || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
779 if (enabled) {
780 setEnabledRecursive(convertView, true);
781 icon.setAlpha(1f);
782 icon1.setAlpha(1f);
783 } else {
784 setEnabledRecursive(convertView, false);
785 icon.setAlpha(0.5f);
786 icon1.setAlpha(0.5f);
787 }
788
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700789 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700790 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700791
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700792 @Override
793 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700794 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700795 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700796
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700797 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700798 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700799 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700800 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700801 return mCursor;
802 } else {
803 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700804 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700805 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700806
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700807 @Override
808 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700809 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700810 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700811
812 @Override
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700813 public int getViewTypeCount() {
814 return 4;
815 }
816
817 @Override
Jeff Sharkey20b32272013-09-03 15:25:52 -0700818 public int getItemViewType(int position) {
819 if (position < mCursorCount) {
820 return 0;
821 } else {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700822 position -= mCursorCount;
823 return mFooters.get(position).getItemViewType();
Jeff Sharkey20b32272013-09-03 15:25:52 -0700824 }
825 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700826 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700827
828 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700829 private final Uri mUri;
830 private final ImageView mIconMime;
831 private final ImageView mIconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700832 private final Point mThumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700833 private final CancellationSignal mSignal;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700834
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700835 public ThumbnailAsyncTask(
836 Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
837 mUri = uri;
838 mIconMime = iconMime;
839 mIconThumb = iconThumb;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700840 mThumbSize = thumbSize;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700841 mSignal = new CancellationSignal();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700842 }
843
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700844 public void reallyCancel() {
845 cancel(false);
846 mSignal.cancel();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700847 }
848
849 @Override
850 protected Bitmap doInBackground(Uri... params) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700851 final Context context = mIconThumb.getContext();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700852
853 Bitmap result = null;
854 try {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700855 // TODO: switch to using unstable provider
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700856 result = DocumentsContract.getDocumentThumbnail(
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700857 context.getContentResolver(), mUri, mThumbSize, mSignal);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700858 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700859 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
860 context, mThumbSize);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700861 thumbs.put(mUri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700862 }
863 } catch (Exception e) {
864 Log.w(TAG, "Failed to load thumbnail: " + e);
865 }
866 return result;
867 }
868
869 @Override
870 protected void onPostExecute(Bitmap result) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700871 if (mIconThumb.getTag() == this && result != null) {
872 mIconThumb.setTag(null);
873 mIconThumb.setImageBitmap(result);
874
875 mIconMime.setAlpha(1f);
876 mIconMime.animate().alpha(0f).start();
877 mIconThumb.setAlpha(0f);
878 mIconThumb.animate().alpha(1f).start();
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700879 }
880 }
881 }
882
883 private static String formatTime(Context context, long when) {
884 // TODO: DateUtils should make this easier
885 Time then = new Time();
886 then.set(when);
887 Time now = new Time();
888 now.setToNow();
889
890 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
891 | DateUtils.FORMAT_ABBREV_ALL;
892
893 if (then.year != now.year) {
894 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
895 } else if (then.yearDay != now.yearDay) {
896 flags |= DateUtils.FORMAT_SHOW_DATE;
897 } else {
898 flags |= DateUtils.FORMAT_SHOW_TIME;
899 }
900
901 return DateUtils.formatDateTime(context, when, flags);
902 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700903
904 private String findCommonMimeType(List<String> mimeTypes) {
905 String[] commonType = mimeTypes.get(0).split("/");
906 if (commonType.length != 2) {
907 return "*/*";
908 }
909
910 for (int i = 1; i < mimeTypes.size(); i++) {
911 String[] type = mimeTypes.get(i).split("/");
912 if (type.length != 2) continue;
913
914 if (!commonType[1].equals(type[1])) {
915 commonType[1] = "*";
916 }
917
918 if (!commonType[0].equals(type[0])) {
919 commonType[0] = "*";
920 commonType[1] = "*";
921 break;
922 }
923 }
924
925 return commonType[0] + "/" + commonType[1];
926 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700927
928 private void setEnabledRecursive(View v, boolean enabled) {
929 if (v.isEnabled() == enabled) return;
930 v.setEnabled(enabled);
931
932 if (v instanceof ViewGroup) {
933 final ViewGroup vg = (ViewGroup) v;
934 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
935 setEnabledRecursive(vg.getChildAt(i), enabled);
936 }
937 }
938 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700939}