blob: a13beba56c3a5b6d812c4e89a33936e45794e6ec [file] [log] [blame]
Jeff Sharkeye22d02e2013-04-26 16:54:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070019import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070020import static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE;
21import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
22import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070023import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
24import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070025import static com.android.documentsui.model.DocumentInfo.getCursorInt;
26import static com.android.documentsui.model.DocumentInfo.getCursorLong;
27import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070028
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070029import android.app.Fragment;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070030import android.app.FragmentManager;
31import android.app.FragmentTransaction;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070032import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey873daa32013-08-18 17:38:20 -070033import android.content.ContentResolver;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070034import android.content.Context;
Jeff Sharkey873daa32013-08-18 17:38:20 -070035import android.content.Intent;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070036import android.content.Loader;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070037import android.database.Cursor;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070038import android.graphics.Bitmap;
39import android.graphics.Point;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070040import android.net.Uri;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070041import android.os.AsyncTask;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070042import android.os.Bundle;
43import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070044import android.provider.DocumentsContract.Document;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070045import android.text.format.DateUtils;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070046import android.text.format.Formatter;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070047import android.text.format.Time;
48import android.util.Log;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070049import android.util.SparseBooleanArray;
50import android.view.ActionMode;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070051import android.view.LayoutInflater;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070052import android.view.Menu;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070053import android.view.MenuItem;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070054import android.view.View;
55import android.view.ViewGroup;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070056import android.widget.AbsListView;
57import android.widget.AbsListView.MultiChoiceModeListener;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070058import android.widget.AdapterView;
59import android.widget.AdapterView.OnItemClickListener;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070060import android.widget.BaseAdapter;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070061import android.widget.GridView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070062import android.widget.ImageView;
63import android.widget.ListView;
64import android.widget.TextView;
Jeff Sharkey873daa32013-08-18 17:38:20 -070065import android.widget.Toast;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -070066
Jeff Sharkeyb3620442013-09-01 18:41:04 -070067import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070068import com.android.documentsui.model.DocumentInfo;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070069import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070070import com.android.internal.util.Predicate;
Jeff Sharkeyc317af82013-07-01 16:56:54 -070071import com.google.android.collect.Lists;
72
73import java.util.ArrayList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070074import java.util.List;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070075import java.util.concurrent.atomic.AtomicInteger;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070076
77/**
78 * Display the documents inside a single directory.
79 */
80public class DirectoryFragment extends Fragment {
81
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070082 private View mEmptyView;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070083 private ListView mListView;
84 private GridView mGridView;
85
Jeff Sharkeyc317af82013-07-01 16:56:54 -070086 private AbsListView mCurrentView;
87
Jeff Sharkey724deeb2013-08-31 15:02:20 -070088 private Predicate<DocumentInfo> mFilter;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070089
Jeff Sharkeya5defe32013-08-05 17:56:48 -070090 public static final int TYPE_NORMAL = 1;
91 public static final int TYPE_SEARCH = 2;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070092 public static final int TYPE_RECENT_OPEN = 3;
Jeff Sharkey5b535922013-08-02 15:55:26 -070093
94 private int mType = TYPE_NORMAL;
95
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070096 private int mLastMode = MODE_UNKNOWN;
97 private int mLastSortOrder = SORT_ORDER_UNKNOWN;
98
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070099 private Point mThumbSize;
100
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700101 private DocumentsAdapter mAdapter;
Jeff Sharkey46899c82013-08-18 22:26:48 -0700102 private LoaderCallbacks<DirectoryResult> mCallbacks;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700103
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700104 private static final String EXTRA_TYPE = "type";
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700105 private static final String EXTRA_ROOT = "root";
106 private static final String EXTRA_DOC = "doc";
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700107 private static final String EXTRA_QUERY = "query";
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700108
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700109 private static AtomicInteger sLoaderId = new AtomicInteger(4000);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700110
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700111 private final int mLoaderId = sLoaderId.incrementAndGet();
112
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700113 public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc) {
114 show(fm, TYPE_NORMAL, root, doc, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700115 }
116
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700117 public static void showSearch(
118 FragmentManager fm, RootInfo root, DocumentInfo doc, String query) {
119 show(fm, TYPE_SEARCH, root, doc, query);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700120 }
121
122 public static void showRecentsOpen(FragmentManager fm) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700123 show(fm, TYPE_RECENT_OPEN, null, null, null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700124 }
125
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700126 private static void show(
127 FragmentManager fm, int type, RootInfo root, DocumentInfo doc, String query) {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700128 final Bundle args = new Bundle();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700129 args.putInt(EXTRA_TYPE, type);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700130 args.putParcelable(EXTRA_ROOT, root);
131 args.putParcelable(EXTRA_DOC, doc);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700132 args.putString(EXTRA_QUERY, query);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700133
134 final DirectoryFragment fragment = new DirectoryFragment();
135 fragment.setArguments(args);
136
137 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -0700138 ft.replace(R.id.container_directory, fragment);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700139 ft.commitAllowingStateLoss();
140 }
141
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700142 public static DirectoryFragment get(FragmentManager fm) {
143 // TODO: deal with multiple directories shown at once
Jeff Sharkey76112212013-08-06 11:26:10 -0700144 return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700145 }
146
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700147 @Override
148 public View onCreateView(
149 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
150 final Context context = inflater.getContext();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700151 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
152
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700153 mEmptyView = view.findViewById(android.R.id.empty);
154
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700155 mListView = (ListView) view.findViewById(R.id.list);
156 mListView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700157 mListView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700158
159 mGridView = (GridView) view.findViewById(R.id.grid);
160 mGridView.setOnItemClickListener(mItemListener);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700161 mGridView.setMultiChoiceModeListener(mMultiListener);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700162
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700163 return view;
164 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700165
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700166 @Override
167 public void onActivityCreated(Bundle savedInstanceState) {
168 super.onActivityCreated(savedInstanceState);
169
170 final Context context = getActivity();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700171 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700172
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700173 mAdapter = new DocumentsAdapter();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700174 mType = getArguments().getInt(EXTRA_TYPE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700175
Jeff Sharkey46899c82013-08-18 22:26:48 -0700176 mCallbacks = new LoaderCallbacks<DirectoryResult>() {
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700177 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700178 public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700179 final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
180 final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700181 final String query = getArguments().getString(EXTRA_QUERY);
Jeff Sharkey46165b52013-07-31 20:53:22 -0700182
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700183 Uri contentsUri;
184 switch (mType) {
185 case TYPE_NORMAL:
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700186 contentsUri = DocumentsContract.buildChildDocumentsUri(
187 doc.authority, doc.documentId);
188 return new DirectoryLoader(context, root, doc, contentsUri);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700189 case TYPE_SEARCH:
190 contentsUri = DocumentsContract.buildSearchDocumentsUri(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700191 doc.authority, doc.documentId, query);
192 return new DirectoryLoader(context, root, doc, contentsUri);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700193 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);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700196 return new RecentLoader(context, matchingRoots, state.acceptMimes);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700197 default:
198 throw new IllegalStateException("Unknown type " + mType);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700199 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700200 }
201
202 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700203 public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700204 if (!isAdded()) return;
205
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700206 mAdapter.swapCursor(result.cursor);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700207
208 // Push latest state up to UI
209 // TODO: if mode change was racing with us, don't overwrite it
210 state.mode = result.mode;
211 state.sortOrder = result.sortOrder;
212 ((DocumentsActivity) context).onStateChanged();
213
214 updateDisplayState();
215
216 if (mLastSortOrder != result.sortOrder) {
217 mLastSortOrder = result.sortOrder;
218 mListView.smoothScrollToPosition(0);
219 mGridView.smoothScrollToPosition(0);
220 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700221 }
222
223 @Override
Jeff Sharkey46899c82013-08-18 22:26:48 -0700224 public void onLoaderReset(Loader<DirectoryResult> loader) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700225 mAdapter.swapCursor(null);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700226 }
227 };
228
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700229 // Kick off loader at least once
230 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
231
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700232 updateDisplayState();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700233 }
234
Jeff Sharkey42d26792013-09-06 13:22:09 -0700235 @Override
236 public void onStart() {
237 super.onStart();
238 updateDisplayState();
239 }
240
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700241 public void onUserSortOrderChanged() {
242 // User change always triggers reload
243 getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
244 }
245
246 public void onUserModeChanged() {
247 // Mode change is just display; no need to reload
248 updateDisplayState();
249 }
250
251 private void updateDisplayState() {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700252 final State state = getDisplayState(this);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700253
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700254 mFilter = new MimePredicate(state.acceptMimes);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700256 if (mLastMode == state.mode) return;
257 mLastMode = state.mode;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700258
Jeff Sharkey873daa32013-08-18 17:38:20 -0700259 mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE);
260 mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700261
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700262 final int choiceMode;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700263 if (state.allowMultiple) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700264 choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
265 } else {
266 choiceMode = ListView.CHOICE_MODE_NONE;
267 }
268
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700269 final int thumbSize;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700270 if (state.mode == MODE_GRID) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700271 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700272 mListView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700273 mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700274 mGridView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700275 mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700276 mGridView.setNumColumns(GridView.AUTO_FIT);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700277 mGridView.setChoiceMode(choiceMode);
278 mCurrentView = mGridView;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700279 } else if (state.mode == MODE_LIST) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700280 thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700281 mGridView.setAdapter(null);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700282 mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700283 mListView.setAdapter(mAdapter);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700284 mListView.setChoiceMode(choiceMode);
285 mCurrentView = mListView;
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700286 } else {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700287 throw new IllegalStateException("Unknown state " + state.mode);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700288 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700289
290 mThumbSize = new Point(thumbSize, thumbSize);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700291 }
292
293 private OnItemClickListener mItemListener = new OnItemClickListener() {
294 @Override
295 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700296 final Cursor cursor = mAdapter.getItem(position);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700297 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkeyf339f252013-08-15 16:17:41 -0700298 if (mFilter.apply(doc)) {
299 ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
300 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700301 }
302 };
303
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700304 private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
305 @Override
306 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
307 mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
308 return true;
309 }
310
311 @Override
312 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700313 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700314
315 final MenuItem open = menu.findItem(R.id.menu_open);
316 final MenuItem share = menu.findItem(R.id.menu_share);
317 final MenuItem delete = menu.findItem(R.id.menu_delete);
318
319 final boolean manageMode = state.action == ACTION_MANAGE;
320 open.setVisible(!manageMode);
321 share.setVisible(manageMode);
322 delete.setVisible(manageMode);
323
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700324 return true;
325 }
326
327 @Override
328 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700329 final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700330 final ArrayList<DocumentInfo> docs = Lists.newArrayList();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700331 final int size = checked.size();
332 for (int i = 0; i < size; i++) {
333 if (checked.valueAt(i)) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700334 final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700335 final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700336 docs.add(doc);
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700337 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700338 }
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700339
Jeff Sharkey873daa32013-08-18 17:38:20 -0700340 final int id = item.getItemId();
341 if (id == R.id.menu_open) {
342 DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700343 mode.finish();
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700344 return true;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700345
346 } else if (id == R.id.menu_share) {
347 onShareDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700348 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700349 return true;
350
351 } else if (id == R.id.menu_delete) {
352 onDeleteDocuments(docs);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700353 mode.finish();
Jeff Sharkey873daa32013-08-18 17:38:20 -0700354 return true;
355
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700356 } else {
357 return false;
358 }
359 }
360
361 @Override
362 public void onDestroyActionMode(ActionMode mode) {
363 // ignored
364 }
365
366 @Override
367 public void onItemCheckedStateChanged(
368 ActionMode mode, int position, long id, boolean checked) {
369 if (checked) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700370 // Directories cannot be checked
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700371 final Cursor cursor = mAdapter.getItem(position);
372 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
373 if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
Jeff Sharkeyc317af82013-07-01 16:56:54 -0700374 mCurrentView.setItemChecked(position, false);
375 }
376 }
377
378 mode.setTitle(getResources()
379 .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
380 }
381 };
382
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700383 private void onShareDocuments(List<DocumentInfo> docs) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700384 Intent intent;
385 if (docs.size() == 1) {
386 final DocumentInfo doc = docs.get(0);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700387
Jeff Sharkey873daa32013-08-18 17:38:20 -0700388 intent = new Intent(Intent.ACTION_SEND);
389 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
390 intent.addCategory(Intent.CATEGORY_DEFAULT);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700391 intent.setType(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700392 intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700393
394 } else if (docs.size() > 1) {
395 intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
396 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
397 intent.addCategory(Intent.CATEGORY_DEFAULT);
398
399 final ArrayList<String> mimeTypes = Lists.newArrayList();
400 final ArrayList<Uri> uris = Lists.newArrayList();
401 for (DocumentInfo doc : docs) {
402 mimeTypes.add(doc.mimeType);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700403 uris.add(doc.derivedUri);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700404 }
405
406 intent.setType(findCommonMimeType(mimeTypes));
407 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
408
409 } else {
410 return;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700411 }
412
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700413 intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
Jeff Sharkey873daa32013-08-18 17:38:20 -0700414 startActivity(intent);
415 }
416
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700417 private void onDeleteDocuments(List<DocumentInfo> docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700418 final Context context = getActivity();
419 final ContentResolver resolver = context.getContentResolver();
420
421 boolean hadTrouble = false;
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700422 for (DocumentInfo doc : docs) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700423 if (!doc.isDeleteSupported()) {
424 Log.w(TAG, "Skipping " + doc);
425 hadTrouble = true;
426 continue;
427 }
428
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700429 if (!DocumentsContract.deleteDocument(resolver, doc.derivedUri)) {
Jeff Sharkey4c5fe522013-09-03 14:17:06 -0700430 Log.w(TAG, "Failed to delete " + doc);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700431 hadTrouble = true;
432 }
433 }
434
435 if (hadTrouble) {
436 Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
437 }
438 }
439
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700440 private static State getDisplayState(Fragment fragment) {
Jeff Sharkey3c28b792013-07-01 17:22:02 -0700441 return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700442 }
443
Jeff Sharkey20b32272013-09-03 15:25:52 -0700444 private interface Footer {
445 public View getView(View convertView, ViewGroup parent);
446 }
447
448 private static class LoadingFooter implements Footer {
449 @Override
450 public View getView(View convertView, ViewGroup parent) {
451 final Context context = parent.getContext();
452 if (convertView == null) {
453 final LayoutInflater inflater = LayoutInflater.from(context);
454 convertView = inflater.inflate(R.layout.item_loading, parent, false);
455 }
456 return convertView;
457 }
458 }
459
460 private class MessageFooter implements Footer {
461 private final int mIcon;
462 private final String mMessage;
463
464 public MessageFooter(int icon, String message) {
465 mIcon = icon;
466 mMessage = message;
467 }
468
469 @Override
470 public View getView(View convertView, ViewGroup parent) {
471 final Context context = parent.getContext();
472 final State state = getDisplayState(DirectoryFragment.this);
473
474 if (convertView == null) {
475 final LayoutInflater inflater = LayoutInflater.from(context);
476 if (state.mode == MODE_LIST) {
477 convertView = inflater.inflate(R.layout.item_message_list, parent, false);
478 } else if (state.mode == MODE_GRID) {
479 convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
480 } else {
481 throw new IllegalStateException();
482 }
483 }
484
485 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
486 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
487 icon.setImageResource(mIcon);
488 title.setText(mMessage);
489 return convertView;
490 }
491 }
492
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700493 private class DocumentsAdapter extends BaseAdapter {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700494 private Cursor mCursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700495 private int mCursorCount;
496
497 private List<Footer> mFooters = Lists.newArrayList();
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700498
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700499 public void swapCursor(Cursor cursor) {
500 mCursor = cursor;
Jeff Sharkey20b32272013-09-03 15:25:52 -0700501 mCursorCount = cursor != null ? cursor.getCount() : 0;
502
503 mFooters.clear();
504
505 final Bundle extras = cursor != null ? cursor.getExtras() : null;
506 if (extras != null) {
507 final String info = extras.getString(DocumentsContract.EXTRA_INFO);
508 if (info != null) {
Jeff Sharkey06c41872013-09-06 10:43:45 -0700509 mFooters.add(new MessageFooter(R.drawable.ic_dialog_alert, info));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700510 }
511 final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
512 if (error != null) {
Jeff Sharkey06c41872013-09-06 10:43:45 -0700513 mFooters.add(new MessageFooter(R.drawable.ic_dialog_alert, error));
Jeff Sharkey20b32272013-09-03 15:25:52 -0700514 }
515 if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
516 mFooters.add(new LoadingFooter());
517 }
518 }
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700519
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700520 if (isEmpty()) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700521 mEmptyView.setVisibility(View.VISIBLE);
522 } else {
523 mEmptyView.setVisibility(View.GONE);
524 }
525
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700526 notifyDataSetChanged();
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700527 }
528
529 @Override
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700530 public View getView(int position, View convertView, ViewGroup parent) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700531 if (position < mCursorCount) {
532 return getDocumentView(position, convertView, parent);
533 } else {
534 position -= mCursorCount;
535 return mFooters.get(position).getView(convertView, parent);
536 }
537 }
538
539 private View getDocumentView(int position, View convertView, ViewGroup parent) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700540 final Context context = parent.getContext();
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700541 final State state = getDisplayState(DirectoryFragment.this);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700542
Jeff Sharkey873daa32013-08-18 17:38:20 -0700543 final RootsCache roots = DocumentsApplication.getRootsCache(context);
544 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
545 context, mThumbSize);
546
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700547 if (convertView == null) {
548 final LayoutInflater inflater = LayoutInflater.from(context);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700549 if (state.mode == MODE_LIST) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700550 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700551 } else if (state.mode == MODE_GRID) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700552 convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
553 } else {
554 throw new IllegalStateException();
555 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700556 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700557
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700558 final Cursor cursor = getItem(position);
559
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700560 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
561 final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700562 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
563 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
564 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
565 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
566 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
567 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
568 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
569 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700570
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700571 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700572 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700573 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700574 final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
575 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
576 final TextView date = (TextView) convertView.findViewById(R.id.date);
577 final TextView size = (TextView) convertView.findViewById(R.id.size);
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700578
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700579 final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag();
580 if (oldTask != null) {
581 oldTask.cancel(false);
582 }
583
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700584 if ((docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0) {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700585 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700586 final Bitmap cachedResult = thumbs.get(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700587 if (cachedResult != null) {
588 icon.setImageBitmap(cachedResult);
589 } else {
590 final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize);
591 icon.setImageBitmap(null);
592 icon.setTag(task);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700593 task.execute(uri);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700594 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700595 } else if (docIcon != 0) {
Jeff Sharkey5629ec52013-09-04 18:03:18 -0700596 icon.setImageDrawable(IconUtils.loadPackageIcon(context, docAuthority, docIcon));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700597 } else {
Jeff Sharkey5629ec52013-09-04 18:03:18 -0700598 icon.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType));
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700599 }
600
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700601 title.setText(docDisplayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700602
Jeff Sharkey42d26792013-09-06 13:22:09 -0700603 boolean hasLine2 = false;
604
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700605 if (mType == TYPE_RECENT_OPEN) {
606 final RootInfo root = roots.getRoot(docAuthority, docRootId);
607 icon1.setVisibility(View.VISIBLE);
608 icon1.setImageDrawable(root.loadIcon(context));
609 summary.setText(root.getDirectoryString());
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700610 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700611 summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
612 hasLine2 = true;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700613 } else {
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700614 icon1.setVisibility(View.GONE);
615 if (docSummary != null) {
616 summary.setText(docSummary);
617 summary.setVisibility(View.VISIBLE);
Jeff Sharkey42d26792013-09-06 13:22:09 -0700618 hasLine2 = true;
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700619 } else {
620 summary.setVisibility(View.INVISIBLE);
621 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700622 }
623
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700624 if (docLastModified == -1) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700625 date.setText(null);
626 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700627 date.setText(formatTime(context, docLastModified));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700628 hasLine2 = true;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700629 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700630
631 if (state.showSize) {
632 size.setVisibility(View.VISIBLE);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700633 if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700634 size.setText(null);
635 } else {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700636 size.setText(Formatter.formatFileSize(context, docSize));
Jeff Sharkey42d26792013-09-06 13:22:09 -0700637 hasLine2 = true;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700638 }
639 } else {
640 size.setVisibility(View.GONE);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700641 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700642
Jeff Sharkey42d26792013-09-06 13:22:09 -0700643 line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
644
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700645 return convertView;
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700646 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700647
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700648 @Override
649 public int getCount() {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700650 return mCursorCount + mFooters.size();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700651 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700652
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700653 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700654 public Cursor getItem(int position) {
Jeff Sharkey20b32272013-09-03 15:25:52 -0700655 if (position < mCursorCount) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700656 mCursor.moveToPosition(position);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700657 return mCursor;
658 } else {
659 return null;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700660 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700661 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700662
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700663 @Override
664 public long getItemId(int position) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700665 return position;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700666 }
Jeff Sharkey20b32272013-09-03 15:25:52 -0700667
668 @Override
669 public int getItemViewType(int position) {
670 if (position < mCursorCount) {
671 return 0;
672 } else {
673 return IGNORE_ITEM_VIEW_TYPE;
674 }
675 }
676
677 @Override
678 public boolean areAllItemsEnabled() {
679 return false;
680 }
681
682 @Override
683 public boolean isEnabled(int position) {
684 return position < mCursorCount;
685 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700686 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700687
688 private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
689 private final ImageView mTarget;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700690 private final Point mThumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700691
Jeff Sharkey873daa32013-08-18 17:38:20 -0700692 public ThumbnailAsyncTask(ImageView target, Point thumbSize) {
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700693 mTarget = target;
Jeff Sharkey873daa32013-08-18 17:38:20 -0700694 mThumbSize = thumbSize;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700695 }
696
697 @Override
698 protected void onPreExecute() {
699 mTarget.setTag(this);
700 }
701
702 @Override
703 protected Bitmap doInBackground(Uri... params) {
704 final Context context = mTarget.getContext();
705 final Uri uri = params[0];
706
707 Bitmap result = null;
708 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700709 result = DocumentsContract.getDocumentThumbnail(
710 context.getContentResolver(), uri, mThumbSize, null);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700711 if (result != null) {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700712 final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
713 context, mThumbSize);
714 thumbs.put(uri, result);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700715 }
716 } catch (Exception e) {
717 Log.w(TAG, "Failed to load thumbnail: " + e);
718 }
719 return result;
720 }
721
722 @Override
723 protected void onPostExecute(Bitmap result) {
724 if (mTarget.getTag() == this) {
725 mTarget.setImageBitmap(result);
726 mTarget.setTag(null);
727 }
728 }
729 }
730
731 private static String formatTime(Context context, long when) {
732 // TODO: DateUtils should make this easier
733 Time then = new Time();
734 then.set(when);
735 Time now = new Time();
736 now.setToNow();
737
738 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
739 | DateUtils.FORMAT_ABBREV_ALL;
740
741 if (then.year != now.year) {
742 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
743 } else if (then.yearDay != now.yearDay) {
744 flags |= DateUtils.FORMAT_SHOW_DATE;
745 } else {
746 flags |= DateUtils.FORMAT_SHOW_TIME;
747 }
748
749 return DateUtils.formatDateTime(context, when, flags);
750 }
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700751
752 private String findCommonMimeType(List<String> mimeTypes) {
753 String[] commonType = mimeTypes.get(0).split("/");
754 if (commonType.length != 2) {
755 return "*/*";
756 }
757
758 for (int i = 1; i < mimeTypes.size(); i++) {
759 String[] type = mimeTypes.get(i).split("/");
760 if (type.length != 2) continue;
761
762 if (!commonType[1].equals(type[1])) {
763 commonType[1] = "*";
764 }
765
766 if (!commonType[0].equals(type[0])) {
767 commonType[0] = "*";
768 commonType[1] = "*";
769 break;
770 }
771 }
772
773 return commonType[0] + "/" + commonType[1];
774 }
Jeff Sharkeye22d02e2013-04-26 16:54:55 -0700775}