blob: 5b23ca5956e804b95e67678ec25ce3c0b7dcf63f [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
400 try {
401 if (resolver.delete(doc.uri, null, null) != 1) {
402 Log.w(TAG, "Failed to delete " + doc);
403 hadTrouble = true;
404 }
405 } catch (Exception e) {
406 Log.w(TAG, "Failed to delete " + doc + ": " + e);
407 hadTrouble = true;
408 }
409 }
410
411 if (hadTrouble) {
412 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
413 }
414 }
415
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700416 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700417 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700418 }
419
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700420 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700421 private Cursor mCursor;
422
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700423 public void swapCursor(Cursor cursor) {
424 mCursor = cursor;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700425
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700426 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700427 mEmptyView.setVisibility(View.VISIBLE);
428 } else {
429 mEmptyView.setVisibility(View.GONE);
430 }
431
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700432 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700433 }
434
435 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700436 public View getView(int position, View convertView, ViewGroup parent) {
437 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700438 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700439
Jeff Sharkey873daa32013-08-18 17:38:20 -0700440 final RootsCache roots = DocumentsApplication.getRootsCache(context);
441 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
442 context, mThumbSize);
443
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700444 if (convertView == null) {
445 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700446 if (state.mode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700447 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700448 } else if (state.mode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700449 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
450 } else {
451 throw new IllegalStateException();
452 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700453 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700454
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700455 final Cursor cursor = getItem(position);
456
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700457 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
458 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700459 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
460 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
461 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
462 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
463 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
464 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
465 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
466 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700467
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700468 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700469 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700470 final View summaryGrid = convertView.findViewById(R.id.summary_grid);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700471 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
472 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
473 final TextView date = (TextView) convertView.findViewById(R.id.date);
474 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700475
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700476 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag();
477 if (oldTask != null) {
478 oldTask.cancel(false);
479 }
480
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700481 if ((docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700482 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700483 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700484 if (cachedResult != null) {
485 icon.setImageBitmap(cachedResult);
486 } else {
487 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize);
488 icon.setImageBitmap(null);
489 icon.setTag(task);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700490 task.execute(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700491 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700492 } else if (docIcon != 0) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700493 icon.setImageDrawable(DocumentInfo.loadIcon(context, docAuthority, docIcon));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700494 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700495 icon.setImageDrawable(RootsCache.resolveDocumentIcon(context, docMimeType));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700496 }
497
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700498 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700499
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700500 if (mType == TYPE_RECENT_OPEN) {
501 final RootInfo root = roots.getRoot(docAuthority, docRootId);
502 icon1.setVisibility(View.VISIBLE);
503 icon1.setImageDrawable(root.loadIcon(context));
504 summary.setText(root.getDirectoryString());
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700505 summary.setVisibility(View.VISIBLE);
506 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700507 icon1.setVisibility(View.GONE);
508 if (docSummary != null) {
509 summary.setText(docSummary);
510 summary.setVisibility(View.VISIBLE);
511 } else {
512 summary.setVisibility(View.INVISIBLE);
513 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700514 }
515
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700516 if (summaryGrid != null) {
517 summaryGrid.setVisibility(
518 (summary.getVisibility() == View.VISIBLE) ? View.VISIBLE : View.GONE);
519 }
520
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700521 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700522 date.setText(null);
523 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700524 date.setText(formatTime(context, docLastModified));
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700525 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700526
527 if (state.showSize) {
528 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700529 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700530 size.setText(null);
531 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700532 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700533 }
534 } else {
535 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700536 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700537
538 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700539 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700540
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700541 @Override
542 public int getCount() {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700543 return mCursor != null ? mCursor.getCount() : 0;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700544 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700545
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700546 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700547 public Cursor getItem(int position) {
548 if (mCursor != null) {
549 mCursor.moveToPosition(position);
550 }
551 return mCursor;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700552 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700553
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700554 @Override
555 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700556 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700557 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700558 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700559
560 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
561 private final ImageView mTarget;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700562 private final Point mThumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700563
Jeff Sharkey873daa32013-08-18 17:38:20 -0700564 public ThumbnailAsyncTask(ImageView target, Point thumbSize) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700565 mTarget = target;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700566 mThumbSize = thumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700567 }
568
569 @Override
570 protected void onPreExecute() {
571 mTarget.setTag(this);
572 }
573
574 @Override
575 protected Bitmap doInBackground(Uri... params) {
576 final Context context = mTarget.getContext();
577 final Uri uri = params[0];
578
579 Bitmap result = null;
580 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700581 result = DocumentsContract.getDocumentThumbnail(
582 context.getContentResolver(), uri, mThumbSize, null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700583 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700584 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
585 context, mThumbSize);
586 thumbs.put(uri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700587 }
588 } catch (Exception e) {
589 Log.w(TAG, "Failed to load thumbnail: " + e);
590 }
591 return result;
592 }
593
594 @Override
595 protected void onPostExecute(Bitmap result) {
596 if (mTarget.getTag() == this) {
597 mTarget.setImageBitmap(result);
598 mTarget.setTag(null);
599 }
600 }
601 }
602
603 private static String formatTime(Context context, long when) {
604 // TODO: DateUtils should make this easier
605 Time then = new Time();
606 then.set(when);
607 Time now = new Time();
608 now.setToNow();
609
610 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
611 | DateUtils.FORMAT_ABBREV_ALL;
612
613 if (then.year != now.year) {
614 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
615 } else if (then.yearDay != now.yearDay) {
616 flags |= DateUtils.FORMAT_SHOW_DATE;
617 } else {
618 flags |= DateUtils.FORMAT_SHOW_TIME;
619 }
620
621 return DateUtils.formatDateTime(context, when, flags);
622 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700623
624 private String findCommonMimeType(List<String> mimeTypes) {
625 String[] commonType = mimeTypes.get(0).split("/");
626 if (commonType.length != 2) {
627 return "*/*";
628 }
629
630 for (int i = 1; i < mimeTypes.size(); i++) {
631 String[] type = mimeTypes.get(i).split("/");
632 if (type.length != 2) continue;
633
634 if (!commonType[1].equals(type[1])) {
635 commonType[1] = "*";
636 }
637
638 if (!commonType[0].equals(type[0])) {
639 commonType[0] = "*";
640 commonType[1] = "*";
641 break;
642 }
643 }
644
645 return commonType[0] + "/" + commonType[1];
646 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700647}