blob: e59443763c72356392bf32b72cd5cfcd5ec0fd00 [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 Sharkey42d26792013-09-06 13:22:09 -0700217 @Override
218 public void onStart() {
219 super.onStart();
220 updateDisplayState();
221 }
222
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700223 public void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700224 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700225
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700226 if (mLastSortOrder != state.sortOrder) {
227 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
228 mLastSortOrder = state.sortOrder;
229 }
230
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700231 mListView.smoothScrollToPosition(0);
232 mGridView.smoothScrollToPosition(0);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700233
Jeff Sharkey873daa32013-08-18 17:38:20 -0700234 mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE);
235 mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700236
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700237 mFilter = new MimePredicate(state.acceptMimes);
238
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700239 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700240 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700241 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
242 } else {
243 choiceMode = ListView.CHOICE_MODE_NONE;
244 }
245
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700246 final int thumbSize;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700247 if (state.mode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700248 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700249 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700250 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700251 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700252 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700253 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700254 mGridView.setChoiceMode(choiceMode);
255 mCurrentView = mGridView;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700256 } else if (state.mode == MODE_LIST) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700257 thumbSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700258 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700259 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700260 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700261 mListView.setChoiceMode(choiceMode);
262 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700263 } else {
264 throw new IllegalStateException();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700265 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700266
267 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700268 }
269
270 private OnItemClickListener mItemListener = new OnItemClickListener() {
271 @Override
272 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700273 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700274 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700275 if (mFilter.apply(doc)) {
276 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
277 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700278 }
279 };
280
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700281 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
282 @Override
283 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
284 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
285 return true;
286 }
287
288 @Override
289 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700290 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700291
292 final MenuItem open = menu.findItem(R.id.menu_open);
293 final MenuItem share = menu.findItem(R.id.menu_share);
294 final MenuItem delete = menu.findItem(R.id.menu_delete);
295
296 final boolean manageMode = state.action == ACTION_MANAGE;
297 open.setVisible(!manageMode);
298 share.setVisible(manageMode);
299 delete.setVisible(manageMode);
300
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700301 return true;
302 }
303
304 @Override
305 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700306 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700307 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700308 final int size = checked.size();
309 for (int i = 0; i < size; i++) {
310 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700311 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700312 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700313 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700314 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700315 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700316
Jeff Sharkey873daa32013-08-18 17:38:20 -0700317 final int id = item.getItemId();
318 if (id == R.id.menu_open) {
319 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700320 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700321 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700322
323 } else if (id == R.id.menu_share) {
324 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700325 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700326 return true;
327
328 } else if (id == R.id.menu_delete) {
329 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700330 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700331 return true;
332
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700333 } else {
334 return false;
335 }
336 }
337
338 @Override
339 public void onDestroyActionMode(ActionMode mode) {
340 // ignored
341 }
342
343 @Override
344 public void onItemCheckedStateChanged(
345 ActionMode mode, int position, long id, boolean checked) {
346 if (checked) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700347 // Directories cannot be checked
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700348 final Cursor cursor = mAdapter.getItem(position);
349 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
350 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700351 mCurrentView.setItemChecked(position, false);
352 }
353 }
354
355 mode.setTitle(getResources()
356 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
357 }
358 };
359
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700360 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700361 Intent intent;
362 if (docs.size() == 1) {
363 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700364
Jeff Sharkey873daa32013-08-18 17:38:20 -0700365 intent = new Intent(Intent.ACTION_SEND);
366 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
367 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700368 intent.setType(doc.mimeType);
369 intent.putExtra(Intent.EXTRA_STREAM, doc.uri);
370
371 } else if (docs.size() > 1) {
372 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
373 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
374 intent.addCategory(Intent.CATEGORY_DEFAULT);
375
376 final ArrayList<String> mimeTypes = Lists.newArrayList();
377 final ArrayList<Uri> uris = Lists.newArrayList();
378 for (DocumentInfo doc : docs) {
379 mimeTypes.add(doc.mimeType);
380 uris.add(doc.uri);
381 }
382
383 intent.setType(findCommonMimeType(mimeTypes));
384 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
385
386 } else {
387 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700388 }
389
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700390 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700391 startActivity(intent);
392 }
393
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700394 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700395 final Context context = getActivity();
396 final ContentResolver resolver = context.getContentResolver();
397
398 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700399 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700400 if (!doc.isDeleteSupported()) {
401 Log.w(TAG, "Skipping " + doc);
402 hadTrouble = true;
403 continue;
404 }
405
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700406 if (!DocumentsContract.deleteDocument(resolver, doc.uri)) {
407 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700408 hadTrouble = true;
409 }
410 }
411
412 if (hadTrouble) {
413 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
414 }
415 }
416
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700417 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700418 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700419 }
420
Jeff Sharkey20b32272013-09-03 15:25:52 -0700421 private interface Footer {
422 public View getView(View convertView, ViewGroup parent);
423 }
424
425 private static class LoadingFooter implements Footer {
426 @Override
427 public View getView(View convertView, ViewGroup parent) {
428 final Context context = parent.getContext();
429 if (convertView == null) {
430 final LayoutInflater inflater = LayoutInflater.from(context);
431 convertView = inflater.inflate(R.layout.item_loading, parent, false);
432 }
433 return convertView;
434 }
435 }
436
437 private class MessageFooter implements Footer {
438 private final int mIcon;
439 private final String mMessage;
440
441 public MessageFooter(int icon, String message) {
442 mIcon = icon;
443 mMessage = message;
444 }
445
446 @Override
447 public View getView(View convertView, ViewGroup parent) {
448 final Context context = parent.getContext();
449 final State state = getDisplayState(DirectoryFragment.this);
450
451 if (convertView == null) {
452 final LayoutInflater inflater = LayoutInflater.from(context);
453 if (state.mode == MODE_LIST) {
454 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
455 } else if (state.mode == MODE_GRID) {
456 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
457 } else {
458 throw new IllegalStateException();
459 }
460 }
461
462 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
463 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
464 icon.setImageResource(mIcon);
465 title.setText(mMessage);
466 return convertView;
467 }
468 }
469
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700470 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700471 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700472 private int mCursorCount;
473
474 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700475
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700476 public void swapCursor(Cursor cursor) {
477 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700478 mCursorCount = cursor != null ? cursor.getCount() : 0;
479
480 mFooters.clear();
481
482 final Bundle extras = cursor != null ? cursor.getExtras() : null;
483 if (extras != null) {
484 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
485 if (info != null) {
Jeff Sharkey06c41872013-09-06 10:43:45 -0700486 mFooters.add(new MessageFooter(R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700487 }
488 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
489 if (error != null) {
Jeff Sharkey06c41872013-09-06 10:43:45 -0700490 mFooters.add(new MessageFooter(R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700491 }
492 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
493 mFooters.add(new LoadingFooter());
494 }
495 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700496
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700497 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700498 mEmptyView.setVisibility(View.VISIBLE);
499 } else {
500 mEmptyView.setVisibility(View.GONE);
501 }
502
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700503 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700504 }
505
506 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700507 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700508 if (position < mCursorCount) {
509 return getDocumentView(position, convertView, parent);
510 } else {
511 position -= mCursorCount;
512 return mFooters.get(position).getView(convertView, parent);
513 }
514 }
515
516 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700517 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700518 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700519
Jeff Sharkey873daa32013-08-18 17:38:20 -0700520 final RootsCache roots = DocumentsApplication.getRootsCache(context);
521 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
522 context, mThumbSize);
523
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700524 if (convertView == null) {
525 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700526 if (state.mode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700527 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700528 } else if (state.mode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700529 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
530 } else {
531 throw new IllegalStateException();
532 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700533 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700534
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700535 final Cursor cursor = getItem(position);
536
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700537 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
538 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700539 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
540 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
541 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
542 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
543 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
544 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
545 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
546 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700547
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700548 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700549 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700550 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700551 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
552 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
553 final TextView date = (TextView) convertView.findViewById(R.id.date);
554 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700555
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700556 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag();
557 if (oldTask != null) {
558 oldTask.cancel(false);
559 }
560
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700561 if ((docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700562 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700563 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700564 if (cachedResult != null) {
565 icon.setImageBitmap(cachedResult);
566 } else {
567 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize);
568 icon.setImageBitmap(null);
569 icon.setTag(task);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700570 task.execute(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700571 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700572 } else if (docIcon != 0) {
Jeff Sharkey5629ec52013-09-04 18:03:18 -0700573 icon.setImageDrawable(IconUtils.loadPackageIcon(context, docAuthority, docIcon));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700574 } else {
Jeff Sharkey5629ec52013-09-04 18:03:18 -0700575 icon.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700576 }
577
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700578 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700579
Jeff Sharkey42d26792013-09-06 13:22:09 -0700580 boolean hasLine2 = false;
581
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700582 if (mType == TYPE_RECENT_OPEN) {
583 final RootInfo root = roots.getRoot(docAuthority, docRootId);
584 icon1.setVisibility(View.VISIBLE);
585 icon1.setImageDrawable(root.loadIcon(context));
586 summary.setText(root.getDirectoryString());
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700587 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700588 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
589 hasLine2 = true;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700590 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700591 icon1.setVisibility(View.GONE);
592 if (docSummary != null) {
593 summary.setText(docSummary);
594 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700595 hasLine2 = true;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700596 } else {
597 summary.setVisibility(View.INVISIBLE);
598 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700599 }
600
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700601 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700602 date.setText(null);
603 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700604 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700605 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700606 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700607
608 if (state.showSize) {
609 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700610 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700611 size.setText(null);
612 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700613 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700614 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700615 }
616 } else {
617 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700618 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700619
Jeff Sharkey42d26792013-09-06 13:22:09 -0700620 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
621
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700622 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700623 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700624
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700625 @Override
626 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700627 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700628 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700629
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700630 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700631 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700632 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700633 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700634 return mCursor;
635 } else {
636 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700637 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700638 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700639
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700640 @Override
641 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700642 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700643 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700644
645 @Override
646 public int getItemViewType(int position) {
647 if (position < mCursorCount) {
648 return 0;
649 } else {
650 return IGNORE_ITEM_VIEW_TYPE;
651 }
652 }
653
654 @Override
655 public boolean areAllItemsEnabled() {
656 return false;
657 }
658
659 @Override
660 public boolean isEnabled(int position) {
661 return position < mCursorCount;
662 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700663 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700664
665 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
666 private final ImageView mTarget;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700667 private final Point mThumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700668
Jeff Sharkey873daa32013-08-18 17:38:20 -0700669 public ThumbnailAsyncTask(ImageView target, Point thumbSize) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700670 mTarget = target;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700671 mThumbSize = thumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700672 }
673
674 @Override
675 protected void onPreExecute() {
676 mTarget.setTag(this);
677 }
678
679 @Override
680 protected Bitmap doInBackground(Uri... params) {
681 final Context context = mTarget.getContext();
682 final Uri uri = params[0];
683
684 Bitmap result = null;
685 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700686 result = DocumentsContract.getDocumentThumbnail(
687 context.getContentResolver(), uri, mThumbSize, null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700688 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700689 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
690 context, mThumbSize);
691 thumbs.put(uri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700692 }
693 } catch (Exception e) {
694 Log.w(TAG, "Failed to load thumbnail: " + e);
695 }
696 return result;
697 }
698
699 @Override
700 protected void onPostExecute(Bitmap result) {
701 if (mTarget.getTag() == this) {
702 mTarget.setImageBitmap(result);
703 mTarget.setTag(null);
704 }
705 }
706 }
707
708 private static String formatTime(Context context, long when) {
709 // TODO: DateUtils should make this easier
710 Time then = new Time();
711 then.set(when);
712 Time now = new Time();
713 now.setToNow();
714
715 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
716 | DateUtils.FORMAT_ABBREV_ALL;
717
718 if (then.year != now.year) {
719 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
720 } else if (then.yearDay != now.yearDay) {
721 flags |= DateUtils.FORMAT_SHOW_DATE;
722 } else {
723 flags |= DateUtils.FORMAT_SHOW_TIME;
724 }
725
726 return DateUtils.formatDateTime(context, when, flags);
727 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700728
729 private String findCommonMimeType(List<String> mimeTypes) {
730 String[] commonType = mimeTypes.get(0).split("/");
731 if (commonType.length != 2) {
732 return "*/*";
733 }
734
735 for (int i = 1; i < mimeTypes.size(); i++) {
736 String[] type = mimeTypes.get(i).split("/");
737 if (type.length != 2) continue;
738
739 if (!commonType[1].equals(type[1])) {
740 commonType[1] = "*";
741 }
742
743 if (!commonType[0].equals(type[0])) {
744 commonType[0] = "*";
745 commonType[1] = "*";
746 break;
747 }
748 }
749
750 return commonType[0] + "/" + commonType[1];
751 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700752}