blob: 33d7d6afd18dbd6a1fd0ade33475c78847177f80 [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 Sharkeyac9e6272013-08-31 21:27:44 -070023import static com.android.documentsui.model.DocumentInfo.getCursorInt;
24import static com.android.documentsui.model.DocumentInfo.getCursorLong;
25import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070026
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070027import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070028import android.app.FragmentManager;
29import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070030import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey873daa32013-08-18 17:38:20 -070031import android.content.ContentResolver;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070032import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070033import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070034import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070035import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070036import android.graphics.Bitmap;
37import android.graphics.Point;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070038import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070039import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.os.Bundle;
41import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070042import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070043import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070044import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070045import android.text.format.Time;
46import android.util.Log;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070047import android.util.SparseBooleanArray;
48import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070049import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070050import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070051import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070052import android.view.View;
53import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070054import android.widget.AbsListView;
55import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070056import android.widget.AdapterView;
57import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070058import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070059import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070060import android.widget.ImageView;
61import android.widget.ListView;
62import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070063import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070064
Jeff Sharkeyb3620442013-09-01 18:41:04 -070065import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070066import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070067import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070068import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070069import com.google.android.collect.Lists;
70
71import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070072import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070073import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070074
75/**
76 * Display the documents inside a single directory.
77 */
78public class DirectoryFragment extends Fragment {
79
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070080 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070081 private ListView mListView;
82 private GridView mGridView;
83
Jeff Sharkeyc317af82013-07-01 16:56:54 -070084 private AbsListView mCurrentView;
85
Jeff Sharkey724deeb2013-08-31 15:02:20 -070086 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070087
Jeff Sharkeya5defe32013-08-05 17:56:48 -070088 public static final int TYPE_NORMAL = 1;
89 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070090 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -070091
92 private int mType = TYPE_NORMAL;
93
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070094 private Point mThumbSize;
95
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070096 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -070097 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070098
Jeff Sharkey2e694f82013-08-06 16:26:14 -070099 private static final String EXTRA_TYPE = "type";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700100 private static final String EXTRA_AUTHORITY = "authority";
101 private static final String EXTRA_ROOT_ID = "rootId";
102 private static final String EXTRA_DOC_ID = "docId";
103 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700104
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700105 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700106
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700107 private int mLastSortOrder = -1;
108
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700109 private final int mLoaderId = sLoaderId.incrementAndGet();
110
111 public static void showNormal(FragmentManager fm, Uri uri) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700112 show(fm, TYPE_NORMAL, uri.getAuthority(), null, DocumentsContract.getDocumentId(uri), null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700113 }
114
115 public static void showSearch(FragmentManager fm, Uri uri, String query) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700116 show(fm, TYPE_SEARCH, uri.getAuthority(), null, DocumentsContract.getDocumentId(uri),
117 query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700118 }
119
120 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700121 show(fm, TYPE_RECENT_OPEN, null, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700122 }
123
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700124 private static void show(FragmentManager fm, int type, String authority, String rootId,
125 String docId, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700126 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700127 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700128 args.putString(EXTRA_AUTHORITY, authority);
129 args.putString(EXTRA_ROOT_ID, rootId);
130 args.putString(EXTRA_DOC_ID, docId);
131 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700132
133 final DirectoryFragment fragment = new DirectoryFragment();
134 fragment.setArguments(args);
135
136 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700137 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700138 ft.commitAllowingStateLoss();
139 }
140
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700141 public static DirectoryFragment get(FragmentManager fm) {
142 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700143 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700144 }
145
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700146 @Override
147 public View onCreateView(
148 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
149 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700150 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
151
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700152 mEmptyView = view.findViewById(android.R.id.empty);
153
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700154 mListView = (ListView) view.findViewById(R.id.list);
155 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700156 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700157
158 mGridView = (GridView) view.findViewById(R.id.grid);
159 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700160 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700161
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700162 return view;
163 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700164
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700165 @Override
166 public void onActivityCreated(Bundle savedInstanceState) {
167 super.onActivityCreated(savedInstanceState);
168
169 final Context context = getActivity();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700170
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700171 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700172 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700173
Jeff Sharkey46899c82013-08-18 22:26:48 -0700174 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700175 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700176 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700177 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700178
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700179 final String authority = getArguments().getString(EXTRA_AUTHORITY);
180 final String rootId = getArguments().getString(EXTRA_ROOT_ID);
181 final String docId = getArguments().getString(EXTRA_DOC_ID);
182 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700183
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700184 Uri contentsUri;
185 switch (mType) {
186 case TYPE_NORMAL:
187 contentsUri = DocumentsContract.buildChildDocumentsUri(authority, docId);
188 return new DirectoryLoader(context, rootId, contentsUri, state.sortOrder);
189 case TYPE_SEARCH:
190 contentsUri = DocumentsContract.buildSearchDocumentsUri(
191 authority, docId, query);
192 return new DirectoryLoader(context, rootId, contentsUri, state.sortOrder);
193 case TYPE_RECENT_OPEN:
Jeff Sharkey1c903cc2013-09-02 17:19:40 -0700194 final RootsCache roots = DocumentsApplication.getRootsCache(context);
195 final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
196 return new RecentLoader(context, matchingRoots);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700197 default:
198 throw new IllegalStateException("Unknown type " + mType);
199
200 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700201 }
202
203 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700204 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700205 mAdapter.swapCursor(result.cursor);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700206 }
207
208 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700209 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700210 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700211 }
212 };
213
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700214 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700215 }
216
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700217 public void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700218 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700219
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700220 if (mLastSortOrder != state.sortOrder) {
221 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
222 mLastSortOrder = state.sortOrder;
223 }
224
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700225 mListView.smoothScrollToPosition(0);
226 mGridView.smoothScrollToPosition(0);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700227
Jeff Sharkey873daa32013-08-18 17:38:20 -0700228 mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE);
229 mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700230
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700231 mFilter = new MimePredicate(state.acceptMimes);
232
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700233 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700234 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700235 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
236 } else {
237 choiceMode = ListView.CHOICE_MODE_NONE;
238 }
239
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700240 final int thumbSize;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700241 if (state.mode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700242 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700243 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700244 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700245 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700246 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700247 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700248 mGridView.setChoiceMode(choiceMode);
249 mCurrentView = mGridView;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700250 } else if (state.mode == MODE_LIST) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700251 thumbSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700252 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700253 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700254 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700255 mListView.setChoiceMode(choiceMode);
256 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700257 } else {
258 throw new IllegalStateException();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700259 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700260
261 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700262 }
263
264 private OnItemClickListener mItemListener = new OnItemClickListener() {
265 @Override
266 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700267 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700268 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700269 if (mFilter.apply(doc)) {
270 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
271 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700272 }
273 };
274
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700275 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
276 @Override
277 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
278 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
279 return true;
280 }
281
282 @Override
283 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700284 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700285
286 final MenuItem open = menu.findItem(R.id.menu_open);
287 final MenuItem share = menu.findItem(R.id.menu_share);
288 final MenuItem delete = menu.findItem(R.id.menu_delete);
289
290 final boolean manageMode = state.action == ACTION_MANAGE;
291 open.setVisible(!manageMode);
292 share.setVisible(manageMode);
293 delete.setVisible(manageMode);
294
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700295 return true;
296 }
297
298 @Override
299 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700300 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700301 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700302 final int size = checked.size();
303 for (int i = 0; i < size; i++) {
304 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700305 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700306 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700307 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700308 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700309 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700310
Jeff Sharkey873daa32013-08-18 17:38:20 -0700311 final int id = item.getItemId();
312 if (id == R.id.menu_open) {
313 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700314 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700315 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700316
317 } else if (id == R.id.menu_share) {
318 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700319 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700320 return true;
321
322 } else if (id == R.id.menu_delete) {
323 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700324 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700325 return true;
326
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700327 } else {
328 return false;
329 }
330 }
331
332 @Override
333 public void onDestroyActionMode(ActionMode mode) {
334 // ignored
335 }
336
337 @Override
338 public void onItemCheckedStateChanged(
339 ActionMode mode, int position, long id, boolean checked) {
340 if (checked) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700341 // Directories cannot be checked
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700342 final Cursor cursor = mAdapter.getItem(position);
343 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
344 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700345 mCurrentView.setItemChecked(position, false);
346 }
347 }
348
349 mode.setTitle(getResources()
350 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
351 }
352 };
353
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700354 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700355 Intent intent;
356 if (docs.size() == 1) {
357 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700358
Jeff Sharkey873daa32013-08-18 17:38:20 -0700359 intent = new Intent(Intent.ACTION_SEND);
360 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
361 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700362 intent.setType(doc.mimeType);
363 intent.putExtra(Intent.EXTRA_STREAM, doc.uri);
364
365 } else if (docs.size() > 1) {
366 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
367 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
368 intent.addCategory(Intent.CATEGORY_DEFAULT);
369
370 final ArrayList<String> mimeTypes = Lists.newArrayList();
371 final ArrayList<Uri> uris = Lists.newArrayList();
372 for (DocumentInfo doc : docs) {
373 mimeTypes.add(doc.mimeType);
374 uris.add(doc.uri);
375 }
376
377 intent.setType(findCommonMimeType(mimeTypes));
378 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
379
380 } else {
381 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700382 }
383
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700384 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700385 startActivity(intent);
386 }
387
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700388 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700389 final Context context = getActivity();
390 final ContentResolver resolver = context.getContentResolver();
391
392 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700393 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700394 if (!doc.isDeleteSupported()) {
395 Log.w(TAG, "Skipping " + doc);
396 hadTrouble = true;
397 continue;
398 }
399
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700400 if (!DocumentsContract.deleteDocument(resolver, doc.uri)) {
401 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700402 hadTrouble = true;
403 }
404 }
405
406 if (hadTrouble) {
407 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
408 }
409 }
410
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700411 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700412 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700413 }
414
Jeff Sharkey20b32272013-09-03 15:25:52 -0700415 private interface Footer {
416 public View getView(View convertView, ViewGroup parent);
417 }
418
419 private static class LoadingFooter implements Footer {
420 @Override
421 public View getView(View convertView, ViewGroup parent) {
422 final Context context = parent.getContext();
423 if (convertView == null) {
424 final LayoutInflater inflater = LayoutInflater.from(context);
425 convertView = inflater.inflate(R.layout.item_loading, parent, false);
426 }
427 return convertView;
428 }
429 }
430
431 private class MessageFooter implements Footer {
432 private final int mIcon;
433 private final String mMessage;
434
435 public MessageFooter(int icon, String message) {
436 mIcon = icon;
437 mMessage = message;
438 }
439
440 @Override
441 public View getView(View convertView, ViewGroup parent) {
442 final Context context = parent.getContext();
443 final State state = getDisplayState(DirectoryFragment.this);
444
445 if (convertView == null) {
446 final LayoutInflater inflater = LayoutInflater.from(context);
447 if (state.mode == MODE_LIST) {
448 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
449 } else if (state.mode == MODE_GRID) {
450 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
451 } else {
452 throw new IllegalStateException();
453 }
454 }
455
456 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
457 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
458 icon.setImageResource(mIcon);
459 title.setText(mMessage);
460 return convertView;
461 }
462 }
463
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700464 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700465 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700466 private int mCursorCount;
467
468 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700469
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700470 public void swapCursor(Cursor cursor) {
471 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700472 mCursorCount = cursor != null ? cursor.getCount() : 0;
473
474 mFooters.clear();
475
476 final Bundle extras = cursor != null ? cursor.getExtras() : null;
477 if (extras != null) {
478 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
479 if (info != null) {
480 mFooters.add(new MessageFooter(
481 com.android.internal.R.drawable.ic_menu_info_details, info));
482 }
483 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
484 if (error != null) {
485 mFooters.add(new MessageFooter(
486 com.android.internal.R.drawable.ic_dialog_alert, error));
487 }
488 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
489 mFooters.add(new LoadingFooter());
490 }
491 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700492
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700493 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700494 mEmptyView.setVisibility(View.VISIBLE);
495 } else {
496 mEmptyView.setVisibility(View.GONE);
497 }
498
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700499 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700500 }
501
502 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700503 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700504 if (position < mCursorCount) {
505 return getDocumentView(position, convertView, parent);
506 } else {
507 position -= mCursorCount;
508 return mFooters.get(position).getView(convertView, parent);
509 }
510 }
511
512 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700513 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700514 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700515
Jeff Sharkey873daa32013-08-18 17:38:20 -0700516 final RootsCache roots = DocumentsApplication.getRootsCache(context);
517 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
518 context, mThumbSize);
519
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700520 if (convertView == null) {
521 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700522 if (state.mode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700523 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 } else if (state.mode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700525 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
526 } else {
527 throw new IllegalStateException();
528 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700529 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700530
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700531 final Cursor cursor = getItem(position);
532
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700533 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
534 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700535 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
536 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
537 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
538 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
539 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
540 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
541 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
542 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700543
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700544 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700545 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700546 final View summaryGrid = convertView.findViewById(R.id.summary_grid);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700547 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
548 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
549 final TextView date = (TextView) convertView.findViewById(R.id.date);
550 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700551
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700552 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag();
553 if (oldTask != null) {
554 oldTask.cancel(false);
555 }
556
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700557 if ((docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700558 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700559 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700560 if (cachedResult != null) {
561 icon.setImageBitmap(cachedResult);
562 } else {
563 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize);
564 icon.setImageBitmap(null);
565 icon.setTag(task);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700566 task.execute(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700567 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700568 } else if (docIcon != 0) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700569 icon.setImageDrawable(DocumentInfo.loadIcon(context, docAuthority, docIcon));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700570 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700571 icon.setImageDrawable(RootsCache.resolveDocumentIcon(context, docMimeType));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700572 }
573
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700574 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700575
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700576 if (mType == TYPE_RECENT_OPEN) {
577 final RootInfo root = roots.getRoot(docAuthority, docRootId);
578 icon1.setVisibility(View.VISIBLE);
579 icon1.setImageDrawable(root.loadIcon(context));
580 summary.setText(root.getDirectoryString());
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700581 summary.setVisibility(View.VISIBLE);
582 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700583 icon1.setVisibility(View.GONE);
584 if (docSummary != null) {
585 summary.setText(docSummary);
586 summary.setVisibility(View.VISIBLE);
587 } else {
588 summary.setVisibility(View.INVISIBLE);
589 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700590 }
591
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700592 if (summaryGrid != null) {
593 summaryGrid.setVisibility(
594 (summary.getVisibility() == View.VISIBLE) ? View.VISIBLE : View.GONE);
595 }
596
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700597 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700598 date.setText(null);
599 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700600 date.setText(formatTime(context, docLastModified));
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700601 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700602
603 if (state.showSize) {
604 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700605 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700606 size.setText(null);
607 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700608 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700609 }
610 } else {
611 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700612 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700613
614 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700615 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700616
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700617 @Override
618 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700619 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700620 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700621
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700622 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700623 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700624 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700625 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700626 return mCursor;
627 } else {
628 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700629 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700630 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700631
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700632 @Override
633 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700634 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700635 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700636
637 @Override
638 public int getItemViewType(int position) {
639 if (position < mCursorCount) {
640 return 0;
641 } else {
642 return IGNORE_ITEM_VIEW_TYPE;
643 }
644 }
645
646 @Override
647 public boolean areAllItemsEnabled() {
648 return false;
649 }
650
651 @Override
652 public boolean isEnabled(int position) {
653 return position < mCursorCount;
654 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700655 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700656
657 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
658 private final ImageView mTarget;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700659 private final Point mThumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700660
Jeff Sharkey873daa32013-08-18 17:38:20 -0700661 public ThumbnailAsyncTask(ImageView target, Point thumbSize) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700662 mTarget = target;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700663 mThumbSize = thumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700664 }
665
666 @Override
667 protected void onPreExecute() {
668 mTarget.setTag(this);
669 }
670
671 @Override
672 protected Bitmap doInBackground(Uri... params) {
673 final Context context = mTarget.getContext();
674 final Uri uri = params[0];
675
676 Bitmap result = null;
677 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700678 result = DocumentsContract.getDocumentThumbnail(
679 context.getContentResolver(), uri, mThumbSize, null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700680 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700681 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
682 context, mThumbSize);
683 thumbs.put(uri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700684 }
685 } catch (Exception e) {
686 Log.w(TAG, "Failed to load thumbnail: " + e);
687 }
688 return result;
689 }
690
691 @Override
692 protected void onPostExecute(Bitmap result) {
693 if (mTarget.getTag() == this) {
694 mTarget.setImageBitmap(result);
695 mTarget.setTag(null);
696 }
697 }
698 }
699
700 private static String formatTime(Context context, long when) {
701 // TODO: DateUtils should make this easier
702 Time then = new Time();
703 then.set(when);
704 Time now = new Time();
705 now.setToNow();
706
707 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
708 | DateUtils.FORMAT_ABBREV_ALL;
709
710 if (then.year != now.year) {
711 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
712 } else if (then.yearDay != now.yearDay) {
713 flags |= DateUtils.FORMAT_SHOW_DATE;
714 } else {
715 flags |= DateUtils.FORMAT_SHOW_TIME;
716 }
717
718 return DateUtils.formatDateTime(context, when, flags);
719 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700720
721 private String findCommonMimeType(List<String> mimeTypes) {
722 String[] commonType = mimeTypes.get(0).split("/");
723 if (commonType.length != 2) {
724 return "*/*";
725 }
726
727 for (int i = 1; i < mimeTypes.size(); i++) {
728 String[] type = mimeTypes.get(i).split("/");
729 if (type.length != 2) continue;
730
731 if (!commonType[1].equals(type[1])) {
732 commonType[1] = "*";
733 }
734
735 if (!commonType[0].equals(type[0])) {
736 commonType[0] = "*";
737 commonType[1] = "*";
738 break;
739 }
740 }
741
742 return commonType[0] + "/" + commonType[1];
743 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700744}