blob: 79d20a443057bcb48492974aec02edadd88a3c35 [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 Sharkey873daa32013-08-18 17:38:20 -070020import static com.android.documentsui.DocumentsActivity.DisplayState.ACTION_MANAGE;
21import static com.android.documentsui.DocumentsActivity.DisplayState.MODE_GRID;
22import static com.android.documentsui.DocumentsActivity.DisplayState.MODE_LIST;
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070023import static com.android.documentsui.DocumentsActivity.DisplayState.SORT_ORDER_DISPLAY_NAME;
24import static com.android.documentsui.DocumentsActivity.DisplayState.SORT_ORDER_LAST_MODIFIED;
Jeff Sharkey873daa32013-08-18 17:38:20 -070025import static com.android.documentsui.DocumentsActivity.DisplayState.SORT_ORDER_SIZE;
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 Sharkey8a8fb672013-05-07 12:41:33 -070035import android.graphics.Bitmap;
36import android.graphics.Point;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070037import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070038import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070039import android.os.Bundle;
40import android.provider.DocumentsContract;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070041import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070042import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070043import android.text.format.Time;
44import android.util.Log;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070045import android.util.SparseBooleanArray;
46import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070047import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070048import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070049import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070050import android.view.View;
51import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070052import android.widget.AbsListView;
53import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070054import android.widget.AdapterView;
55import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070056import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070057import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070058import android.widget.ImageView;
59import android.widget.ListView;
60import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070061import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062
Jeff Sharkey3c28b792013-07-01 17:22:02 -070063import com.android.documentsui.DocumentsActivity.DisplayState;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070064import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070065import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070066import com.google.android.collect.Lists;
67
68import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070069import java.util.Comparator;
70import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070071import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070072
73/**
74 * Display the documents inside a single directory.
75 */
76public class DirectoryFragment extends Fragment {
77
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070078 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070079 private ListView mListView;
80 private GridView mGridView;
81
Jeff Sharkeyc317af82013-07-01 16:56:54 -070082 private AbsListView mCurrentView;
83
Jeff Sharkey724deeb2013-08-31 15:02:20 -070084 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070085
Jeff Sharkeya5defe32013-08-05 17:56:48 -070086 public static final int TYPE_NORMAL = 1;
87 public static final int TYPE_SEARCH = 2;
88 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -070089
90 private int mType = TYPE_NORMAL;
91
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070092 private Point mThumbSize;
93
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070094 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -070095 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070096
Jeff Sharkey2e694f82013-08-06 16:26:14 -070097 private static final String EXTRA_TYPE = "type";
Jeff Sharkey5b535922013-08-02 15:55:26 -070098 private static final String EXTRA_URI = "uri";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070099
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700100 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700101
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700102 private final int mLoaderId = sLoaderId.incrementAndGet();
103
104 public static void showNormal(FragmentManager fm, Uri uri) {
105 show(fm, TYPE_NORMAL, uri);
106 }
107
108 public static void showSearch(FragmentManager fm, Uri uri, String query) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700109 final Uri searchUri = DocumentsContract.buildSearchDocumentsUri(
110 uri.getAuthority(), DocumentsContract.getDocumentId(uri), query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700111 show(fm, TYPE_SEARCH, searchUri);
112 }
113
114 public static void showRecentsOpen(FragmentManager fm) {
115 show(fm, TYPE_RECENT_OPEN, null);
116 }
117
118 private static void show(FragmentManager fm, int type, Uri uri) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700119 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700120 args.putInt(EXTRA_TYPE, type);
Jeff Sharkey5b535922013-08-02 15:55:26 -0700121 args.putParcelable(EXTRA_URI, uri);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700122
123 final DirectoryFragment fragment = new DirectoryFragment();
124 fragment.setArguments(args);
125
126 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700127 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128 ft.commitAllowingStateLoss();
129 }
130
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700131 public static DirectoryFragment get(FragmentManager fm) {
132 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700133 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700134 }
135
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700136 @Override
137 public View onCreateView(
138 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
139 final Context context = inflater.getContext();
140
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700141 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
142
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700143 mEmptyView = view.findViewById(android.R.id.empty);
144
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700145 mListView = (ListView) view.findViewById(R.id.list);
146 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700147 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700148
149 mGridView = (GridView) view.findViewById(R.id.grid);
150 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700151 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700152
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700153 mAdapter = new DocumentsAdapter();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700154
Jeff Sharkey5b535922013-08-02 15:55:26 -0700155 final Uri uri = getArguments().getParcelable(EXTRA_URI);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700156 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700157
Jeff Sharkey46899c82013-08-18 22:26:48 -0700158 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700159 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700160 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700161 final DisplayState state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700162 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700163
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700164 Uri contentsUri;
Jeff Sharkey5b535922013-08-02 15:55:26 -0700165 if (mType == TYPE_NORMAL) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700166 contentsUri = DocumentsContract.buildChildDocumentsUri(
167 uri.getAuthority(), DocumentsContract.getDocumentId(uri));
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700168 } else if (mType == TYPE_RECENT_OPEN) {
169 contentsUri = RecentsProvider.buildRecentOpen();
Jeff Sharkey46165b52013-07-31 20:53:22 -0700170 } else {
Jeff Sharkey5b535922013-08-02 15:55:26 -0700171 contentsUri = uri;
Jeff Sharkey46165b52013-07-31 20:53:22 -0700172 }
173
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700174 final Comparator<DocumentInfo> sortOrder;
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -0700175 if (state.sortOrder == SORT_ORDER_LAST_MODIFIED || mType == TYPE_RECENT_OPEN) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700176 sortOrder = new DocumentInfo.LastModifiedComparator();
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -0700177 } else if (state.sortOrder == SORT_ORDER_DISPLAY_NAME) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700178 sortOrder = new DocumentInfo.DisplayNameComparator();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700179 } else if (state.sortOrder == SORT_ORDER_SIZE) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700180 sortOrder = new DocumentInfo.SizeComparator();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700181 } else {
182 throw new IllegalArgumentException("Unknown sort order " + state.sortOrder);
183 }
184
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700185 return new DirectoryLoader(context, contentsUri, mType, null, sortOrder);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700186 }
187
188 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700189 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
190 mAdapter.swapDocuments(result.contents);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700191 }
192
193 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700194 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700195 mAdapter.swapDocuments(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700196 }
197 };
198
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700199 updateDisplayState();
200
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700201 return view;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700202 }
203
204 @Override
205 public void onStart() {
206 super.onStart();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700207 getLoaderManager().restartLoader(mLoaderId, getArguments(), mCallbacks);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700208 }
209
210 @Override
211 public void onStop() {
212 super.onStop();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700213 getLoaderManager().destroyLoader(mLoaderId);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700214 }
215
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700216 public void updateDisplayState() {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700217 final DisplayState state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700218
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700219 // TODO: avoid kicking loader when nothing changed
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700220 getLoaderManager().restartLoader(mLoaderId, getArguments(), mCallbacks);
221 mListView.smoothScrollToPosition(0);
222 mGridView.smoothScrollToPosition(0);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700223
Jeff Sharkey873daa32013-08-18 17:38:20 -0700224 mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE);
225 mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700226
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700227 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700228 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700229 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
230 } else {
231 choiceMode = ListView.CHOICE_MODE_NONE;
232 }
233
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700234 final int thumbSize;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700235 if (state.mode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700236 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700237 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700238 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700239 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700240 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700241 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700242 mGridView.setChoiceMode(choiceMode);
243 mCurrentView = mGridView;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700244 } else if (state.mode == MODE_LIST) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700245 thumbSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700246 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700247 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700248 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700249 mListView.setChoiceMode(choiceMode);
250 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700251 } else {
252 throw new IllegalStateException();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700253 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700254
255 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700256 }
257
258 private OnItemClickListener mItemListener = new OnItemClickListener() {
259 @Override
260 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700261 final DocumentInfo doc = mAdapter.getItem(position);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700262 if (mFilter.apply(doc)) {
263 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
264 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700265 }
266 };
267
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700268 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
269 @Override
270 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
271 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
272 return true;
273 }
274
275 @Override
276 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700277 final DisplayState state = getDisplayState(DirectoryFragment.this);
278
279 final MenuItem open = menu.findItem(R.id.menu_open);
280 final MenuItem share = menu.findItem(R.id.menu_share);
281 final MenuItem delete = menu.findItem(R.id.menu_delete);
282
283 final boolean manageMode = state.action == ACTION_MANAGE;
284 open.setVisible(!manageMode);
285 share.setVisible(manageMode);
286 delete.setVisible(manageMode);
287
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700288 return true;
289 }
290
291 @Override
292 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700293 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700294 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700295 final int size = checked.size();
296 for (int i = 0; i < size; i++) {
297 if (checked.valueAt(i)) {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700298 final DocumentInfo doc = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700299 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700300 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700301 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700302
Jeff Sharkey873daa32013-08-18 17:38:20 -0700303 final int id = item.getItemId();
304 if (id == R.id.menu_open) {
305 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700306 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700307
308 } else if (id == R.id.menu_share) {
309 onShareDocuments(docs);
310 return true;
311
312 } else if (id == R.id.menu_delete) {
313 onDeleteDocuments(docs);
314 return true;
315
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700316 } else {
317 return false;
318 }
319 }
320
321 @Override
322 public void onDestroyActionMode(ActionMode mode) {
323 // ignored
324 }
325
326 @Override
327 public void onItemCheckedStateChanged(
328 ActionMode mode, int position, long id, boolean checked) {
329 if (checked) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700330 // Directories cannot be checked
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700331 final DocumentInfo doc = mAdapter.getItem(position);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700332 if (doc.isDirectory()) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700333 mCurrentView.setItemChecked(position, false);
334 }
335 }
336
337 mode.setTitle(getResources()
338 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
339 }
340 };
341
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700342 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700343 final ArrayList<Uri> uris = Lists.newArrayList();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700344 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700345 uris.add(doc.uri);
346 }
347
348 final Intent intent;
349 if (uris.size() > 1) {
350 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
351 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
352 intent.addCategory(Intent.CATEGORY_DEFAULT);
353 // TODO: find common mimetype
354 intent.setType("*/*");
355 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
356 } else {
357 intent = new Intent(Intent.ACTION_SEND);
358 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
359 intent.addCategory(Intent.CATEGORY_DEFAULT);
360 intent.setData(uris.get(0));
361 }
362
363 startActivity(intent);
364 }
365
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700366 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700367 final Context context = getActivity();
368 final ContentResolver resolver = context.getContentResolver();
369
370 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700371 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700372 if (!doc.isDeleteSupported()) {
373 Log.w(TAG, "Skipping " + doc);
374 hadTrouble = true;
375 continue;
376 }
377
378 try {
379 if (resolver.delete(doc.uri, null, null) != 1) {
380 Log.w(TAG, "Failed to delete " + doc);
381 hadTrouble = true;
382 }
383 } catch (Exception e) {
384 Log.w(TAG, "Failed to delete " + doc + ": " + e);
385 hadTrouble = true;
386 }
387 }
388
389 if (hadTrouble) {
390 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
391 }
392 }
393
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700394 private static DisplayState getDisplayState(Fragment fragment) {
395 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700396 }
397
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700398 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700399 private List<DocumentInfo> mDocuments;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700400
401 public DocumentsAdapter() {
402 }
403
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700404 public void swapDocuments(List<DocumentInfo> documents) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700405 mDocuments = documents;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700406
Jeff Sharkey46899c82013-08-18 22:26:48 -0700407 if (mDocuments != null && mDocuments.isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700408 mEmptyView.setVisibility(View.VISIBLE);
409 } else {
410 mEmptyView.setVisibility(View.GONE);
411 }
412
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700413 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700414 }
415
416 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700417 public View getView(int position, View convertView, ViewGroup parent) {
418 final Context context = parent.getContext();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700419 final DisplayState state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700420
Jeff Sharkey873daa32013-08-18 17:38:20 -0700421 final RootsCache roots = DocumentsApplication.getRootsCache(context);
422 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
423 context, mThumbSize);
424
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700425 if (convertView == null) {
426 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700427 if (state.mode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700428 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700429 } else if (state.mode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700430 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
431 } else {
432 throw new IllegalStateException();
433 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700434 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700435
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700436 final DocumentInfo doc = getItem(position);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700437
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700438 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700439 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700440 final View summaryGrid = convertView.findViewById(R.id.summary_grid);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700441 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
442 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
443 final TextView date = (TextView) convertView.findViewById(R.id.date);
444 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700445
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700446 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag();
447 if (oldTask != null) {
448 oldTask.cancel(false);
449 }
450
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700451 if (doc.isThumbnailSupported()) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700452 final Bitmap cachedResult = thumbs.get(doc.uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700453 if (cachedResult != null) {
454 icon.setImageBitmap(cachedResult);
455 } else {
456 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize);
457 icon.setImageBitmap(null);
458 icon.setTag(task);
459 task.execute(doc.uri);
460 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700461 } else {
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -0700462 icon.setImageDrawable(RootsCache.resolveDocumentIcon(context, doc.mimeType));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700463 }
464
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700465 title.setText(doc.displayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700466
467 if (mType == TYPE_NORMAL || mType == TYPE_SEARCH) {
468 icon1.setVisibility(View.GONE);
469 if (doc.summary != null) {
470 summary.setText(doc.summary);
471 summary.setVisibility(View.VISIBLE);
472 } else {
473 summary.setVisibility(View.INVISIBLE);
474 }
475 } else if (mType == TYPE_RECENT_OPEN) {
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -0700476 // TODO: resolve storage root
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700477 }
478
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700479 if (summaryGrid != null) {
480 summaryGrid.setVisibility(
481 (summary.getVisibility() == View.VISIBLE) ? View.VISIBLE : View.GONE);
482 }
483
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700484 if (doc.lastModified == -1) {
485 date.setText(null);
486 } else {
487 date.setText(formatTime(context, doc.lastModified));
488 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700489
490 if (state.showSize) {
491 size.setVisibility(View.VISIBLE);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700492 if (doc.isDirectory() || doc.size == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700493 size.setText(null);
494 } else {
495 size.setText(Formatter.formatFileSize(context, doc.size));
496 }
497 } else {
498 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700499 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700500
501 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700502 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700503
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700504 @Override
505 public int getCount() {
506 return mDocuments != null ? mDocuments.size() : 0;
507 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700508
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700509 @Override
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700510 public DocumentInfo getItem(int position) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700511 return mDocuments.get(position);
512 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700513
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700514 @Override
515 public long getItemId(int position) {
516 return getItem(position).uri.hashCode();
517 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700518 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700519
520 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
521 private final ImageView mTarget;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700522 private final Point mThumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700523
Jeff Sharkey873daa32013-08-18 17:38:20 -0700524 public ThumbnailAsyncTask(ImageView target, Point thumbSize) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700525 mTarget = target;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700526 mThumbSize = thumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700527 }
528
529 @Override
530 protected void onPreExecute() {
531 mTarget.setTag(this);
532 }
533
534 @Override
535 protected Bitmap doInBackground(Uri... params) {
536 final Context context = mTarget.getContext();
537 final Uri uri = params[0];
538
539 Bitmap result = null;
540 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700541 result = DocumentsContract.getDocumentThumbnail(
542 context.getContentResolver(), uri, mThumbSize, null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700543 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700544 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
545 context, mThumbSize);
546 thumbs.put(uri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700547 }
548 } catch (Exception e) {
549 Log.w(TAG, "Failed to load thumbnail: " + e);
550 }
551 return result;
552 }
553
554 @Override
555 protected void onPostExecute(Bitmap result) {
556 if (mTarget.getTag() == this) {
557 mTarget.setImageBitmap(result);
558 mTarget.setTag(null);
559 }
560 }
561 }
562
563 private static String formatTime(Context context, long when) {
564 // TODO: DateUtils should make this easier
565 Time then = new Time();
566 then.set(when);
567 Time now = new Time();
568 now.setToNow();
569
570 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
571 | DateUtils.FORMAT_ABBREV_ALL;
572
573 if (then.year != now.year) {
574 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
575 } else if (then.yearDay != now.yearDay) {
576 flags |= DateUtils.FORMAT_SHOW_DATE;
577 } else {
578 flags |= DateUtils.FORMAT_SHOW_TIME;
579 }
580
581 return DateUtils.formatDateTime(context, when, flags);
582 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700583}