Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE; |
| 21 | import static com.android.documentsui.DocumentsActivity.State.MODE_GRID; |
| 22 | import static com.android.documentsui.DocumentsActivity.State.MODE_LIST; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 23 | import static com.android.documentsui.model.DocumentInfo.getCursorInt; |
| 24 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 25 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 26 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 27 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 28 | import android.app.FragmentManager; |
| 29 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 30 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 31 | import android.content.ContentResolver; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 34 | import android.content.Loader; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 35 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 36 | import android.graphics.Bitmap; |
| 37 | import android.graphics.Point; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 39 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 40 | import android.os.Bundle; |
| 41 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 42 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 43 | import android.text.format.DateUtils; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 44 | import android.text.format.Formatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 45 | import android.text.format.Time; |
| 46 | import android.util.Log; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 47 | import android.util.SparseBooleanArray; |
| 48 | import android.view.ActionMode; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 50 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 51 | import android.view.MenuItem; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 52 | import android.view.View; |
| 53 | import android.view.ViewGroup; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 54 | import android.widget.AbsListView; |
| 55 | import android.widget.AbsListView.MultiChoiceModeListener; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 56 | import android.widget.AdapterView; |
| 57 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 58 | import android.widget.BaseAdapter; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 59 | import android.widget.GridView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 60 | import android.widget.ImageView; |
| 61 | import android.widget.ListView; |
| 62 | import android.widget.TextView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 63 | import android.widget.Toast; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 64 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 65 | import com.android.documentsui.DocumentsActivity.State; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 66 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 67 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 68 | import com.android.internal.util.Predicate; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 69 | import com.google.android.collect.Lists; |
| 70 | |
| 71 | import java.util.ArrayList; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 72 | import java.util.List; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 73 | import java.util.concurrent.atomic.AtomicInteger; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Display the documents inside a single directory. |
| 77 | */ |
| 78 | public class DirectoryFragment extends Fragment { |
| 79 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 80 | private View mEmptyView; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 81 | private ListView mListView; |
| 82 | private GridView mGridView; |
| 83 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 84 | private AbsListView mCurrentView; |
| 85 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 86 | private Predicate<DocumentInfo> mFilter; |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 87 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 88 | public static final int TYPE_NORMAL = 1; |
| 89 | public static final int TYPE_SEARCH = 2; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 90 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 91 | |
| 92 | private int mType = TYPE_NORMAL; |
| 93 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 94 | private Point mThumbSize; |
| 95 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 96 | private DocumentsAdapter mAdapter; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 97 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 98 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 99 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 100 | 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 Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 104 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 105 | private static AtomicInteger sLoaderId = new AtomicInteger(4000); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 106 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 107 | private int mLastSortOrder = -1; |
| 108 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 109 | private final int mLoaderId = sLoaderId.incrementAndGet(); |
| 110 | |
| 111 | public static void showNormal(FragmentManager fm, Uri uri) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 112 | show(fm, TYPE_NORMAL, uri.getAuthority(), null, DocumentsContract.getDocumentId(uri), null); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public static void showSearch(FragmentManager fm, Uri uri, String query) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 116 | show(fm, TYPE_SEARCH, uri.getAuthority(), null, DocumentsContract.getDocumentId(uri), |
| 117 | query); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public static void showRecentsOpen(FragmentManager fm) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 121 | show(fm, TYPE_RECENT_OPEN, null, null, null, null); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 124 | private static void show(FragmentManager fm, int type, String authority, String rootId, |
| 125 | String docId, String query) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 126 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 127 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 128 | 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 Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 132 | |
| 133 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 134 | fragment.setArguments(args); |
| 135 | |
| 136 | final FragmentTransaction ft = fm.beginTransaction(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 137 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 138 | ft.commitAllowingStateLoss(); |
| 139 | } |
| 140 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 141 | public static DirectoryFragment get(FragmentManager fm) { |
| 142 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 143 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public View onCreateView( |
| 148 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 149 | final Context context = inflater.getContext(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 150 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 151 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 152 | mEmptyView = view.findViewById(android.R.id.empty); |
| 153 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 154 | mListView = (ListView) view.findViewById(R.id.list); |
| 155 | mListView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 156 | mListView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 157 | |
| 158 | mGridView = (GridView) view.findViewById(R.id.grid); |
| 159 | mGridView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 160 | mGridView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 161 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 162 | return view; |
| 163 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 164 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public void onActivityCreated(Bundle savedInstanceState) { |
| 167 | super.onActivityCreated(savedInstanceState); |
| 168 | |
| 169 | final Context context = getActivity(); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 170 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 171 | mAdapter = new DocumentsAdapter(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 172 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 173 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 174 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 175 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 176 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 177 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 178 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 179 | 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 Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 183 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 184 | 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 Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame^] | 194 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 195 | final List<RootInfo> matchingRoots = roots.getMatchingRoots(state); |
| 196 | return new RecentLoader(context, matchingRoots); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 197 | default: |
| 198 | throw new IllegalStateException("Unknown type " + mType); |
| 199 | |
| 200 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 204 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 205 | mAdapter.swapCursor(result.cursor); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 209 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 210 | mAdapter.swapCursor(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 214 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 217 | public void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 218 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 219 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 220 | if (mLastSortOrder != state.sortOrder) { |
| 221 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 222 | mLastSortOrder = state.sortOrder; |
| 223 | } |
| 224 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 225 | mListView.smoothScrollToPosition(0); |
| 226 | mGridView.smoothScrollToPosition(0); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 228 | mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE); |
| 229 | mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 230 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 231 | mFilter = new MimePredicate(state.acceptMimes); |
| 232 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 233 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 234 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 235 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 236 | } else { |
| 237 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 238 | } |
| 239 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 240 | final int thumbSize; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 241 | if (state.mode == MODE_GRID) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 242 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 243 | mListView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 244 | mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 245 | mGridView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 246 | mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width)); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 247 | mGridView.setNumColumns(GridView.AUTO_FIT); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 248 | mGridView.setChoiceMode(choiceMode); |
| 249 | mCurrentView = mGridView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 250 | } else if (state.mode == MODE_LIST) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 251 | thumbSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 252 | mGridView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 253 | mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 254 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 255 | mListView.setChoiceMode(choiceMode); |
| 256 | mCurrentView = mListView; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 257 | } else { |
| 258 | throw new IllegalStateException(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 259 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 260 | |
| 261 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 265 | @Override |
| 266 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 267 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 268 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 269 | if (mFilter.apply(doc)) { |
| 270 | ((DocumentsActivity) getActivity()).onDocumentPicked(doc); |
| 271 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 272 | } |
| 273 | }; |
| 274 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 275 | private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() { |
| 276 | @Override |
| 277 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 278 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 284 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 285 | |
| 286 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 287 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 288 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 289 | |
| 290 | final boolean manageMode = state.action == ACTION_MANAGE; |
| 291 | open.setVisible(!manageMode); |
| 292 | share.setVisible(manageMode); |
| 293 | delete.setVisible(manageMode); |
| 294 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 300 | final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions(); |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 301 | final ArrayList<DocumentInfo> docs = Lists.newArrayList(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 302 | final int size = checked.size(); |
| 303 | for (int i = 0; i < size; i++) { |
| 304 | if (checked.valueAt(i)) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 305 | final Cursor cursor = mAdapter.getItem(checked.keyAt(i)); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 306 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 307 | docs.add(doc); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 308 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 309 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 310 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 311 | final int id = item.getItemId(); |
| 312 | if (id == R.id.menu_open) { |
| 313 | DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 314 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 315 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 316 | |
| 317 | } else if (id == R.id.menu_share) { |
| 318 | onShareDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 319 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 320 | return true; |
| 321 | |
| 322 | } else if (id == R.id.menu_delete) { |
| 323 | onDeleteDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 324 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 325 | return true; |
| 326 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 327 | } else { |
| 328 | return false; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public void onDestroyActionMode(ActionMode mode) { |
| 334 | // ignored |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void onItemCheckedStateChanged( |
| 339 | ActionMode mode, int position, long id, boolean checked) { |
| 340 | if (checked) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 341 | // Directories cannot be checked |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 342 | final Cursor cursor = mAdapter.getItem(position); |
| 343 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 344 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 345 | mCurrentView.setItemChecked(position, false); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | mode.setTitle(getResources() |
| 350 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
| 351 | } |
| 352 | }; |
| 353 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 354 | private void onShareDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 355 | Intent intent; |
| 356 | if (docs.size() == 1) { |
| 357 | final DocumentInfo doc = docs.get(0); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 358 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 359 | intent = new Intent(Intent.ACTION_SEND); |
| 360 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 361 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 362 | intent.setType(doc.mimeType); |
| 363 | intent.putExtra(Intent.EXTRA_STREAM, doc.uri); |
| 364 | |
| 365 | } else if (docs.size() > 1) { |
| 366 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 367 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 368 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 369 | |
| 370 | final ArrayList<String> mimeTypes = Lists.newArrayList(); |
| 371 | final ArrayList<Uri> uris = Lists.newArrayList(); |
| 372 | for (DocumentInfo doc : docs) { |
| 373 | mimeTypes.add(doc.mimeType); |
| 374 | uris.add(doc.uri); |
| 375 | } |
| 376 | |
| 377 | intent.setType(findCommonMimeType(mimeTypes)); |
| 378 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 379 | |
| 380 | } else { |
| 381 | return; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 384 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 385 | startActivity(intent); |
| 386 | } |
| 387 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 388 | private void onDeleteDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 389 | final Context context = getActivity(); |
| 390 | final ContentResolver resolver = context.getContentResolver(); |
| 391 | |
| 392 | boolean hadTrouble = false; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 393 | for (DocumentInfo doc : docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 394 | if (!doc.isDeleteSupported()) { |
| 395 | Log.w(TAG, "Skipping " + doc); |
| 396 | hadTrouble = true; |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | try { |
| 401 | if (resolver.delete(doc.uri, null, null) != 1) { |
| 402 | Log.w(TAG, "Failed to delete " + doc); |
| 403 | hadTrouble = true; |
| 404 | } |
| 405 | } catch (Exception e) { |
| 406 | Log.w(TAG, "Failed to delete " + doc + ": " + e); |
| 407 | hadTrouble = true; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (hadTrouble) { |
| 412 | Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show(); |
| 413 | } |
| 414 | } |
| 415 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 416 | private static State getDisplayState(Fragment fragment) { |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 417 | return ((DocumentsActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 420 | private class DocumentsAdapter extends BaseAdapter { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 421 | private Cursor mCursor; |
| 422 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 423 | public void swapCursor(Cursor cursor) { |
| 424 | mCursor = cursor; |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 425 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 426 | if (isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 427 | mEmptyView.setVisibility(View.VISIBLE); |
| 428 | } else { |
| 429 | mEmptyView.setVisibility(View.GONE); |
| 430 | } |
| 431 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 432 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 436 | public View getView(int position, View convertView, ViewGroup parent) { |
| 437 | final Context context = parent.getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 438 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 439 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 440 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 441 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 442 | context, mThumbSize); |
| 443 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 444 | if (convertView == null) { |
| 445 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 446 | if (state.mode == MODE_LIST) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 447 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 448 | } else if (state.mode == MODE_GRID) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 449 | convertView = inflater.inflate(R.layout.item_doc_grid, parent, false); |
| 450 | } else { |
| 451 | throw new IllegalStateException(); |
| 452 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 453 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 454 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 455 | final Cursor cursor = getItem(position); |
| 456 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 457 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 458 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 459 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 460 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 461 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 462 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 463 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 464 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 465 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 466 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 467 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 468 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 469 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 470 | final View summaryGrid = convertView.findViewById(R.id.summary_grid); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 471 | final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1); |
| 472 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 473 | final TextView date = (TextView) convertView.findViewById(R.id.date); |
| 474 | final TextView size = (TextView) convertView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 475 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 476 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag(); |
| 477 | if (oldTask != null) { |
| 478 | oldTask.cancel(false); |
| 479 | } |
| 480 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 481 | if ((docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 482 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 483 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 484 | if (cachedResult != null) { |
| 485 | icon.setImageBitmap(cachedResult); |
| 486 | } else { |
| 487 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize); |
| 488 | icon.setImageBitmap(null); |
| 489 | icon.setTag(task); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 490 | task.execute(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 491 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 492 | } else if (docIcon != 0) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 493 | icon.setImageDrawable(DocumentInfo.loadIcon(context, docAuthority, docIcon)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 494 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 495 | icon.setImageDrawable(RootsCache.resolveDocumentIcon(context, docMimeType)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 498 | title.setText(docDisplayName); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 499 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 500 | if (mType == TYPE_RECENT_OPEN) { |
| 501 | final RootInfo root = roots.getRoot(docAuthority, docRootId); |
| 502 | icon1.setVisibility(View.VISIBLE); |
| 503 | icon1.setImageDrawable(root.loadIcon(context)); |
| 504 | summary.setText(root.getDirectoryString()); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 505 | summary.setVisibility(View.VISIBLE); |
| 506 | } else { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 507 | icon1.setVisibility(View.GONE); |
| 508 | if (docSummary != null) { |
| 509 | summary.setText(docSummary); |
| 510 | summary.setVisibility(View.VISIBLE); |
| 511 | } else { |
| 512 | summary.setVisibility(View.INVISIBLE); |
| 513 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 516 | if (summaryGrid != null) { |
| 517 | summaryGrid.setVisibility( |
| 518 | (summary.getVisibility() == View.VISIBLE) ? View.VISIBLE : View.GONE); |
| 519 | } |
| 520 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 521 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 522 | date.setText(null); |
| 523 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 524 | date.setText(formatTime(context, docLastModified)); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 525 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 526 | |
| 527 | if (state.showSize) { |
| 528 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 529 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 530 | size.setText(null); |
| 531 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 532 | size.setText(Formatter.formatFileSize(context, docSize)); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 533 | } |
| 534 | } else { |
| 535 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 536 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 537 | |
| 538 | return convertView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 539 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 540 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 541 | @Override |
| 542 | public int getCount() { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 543 | return mCursor != null ? mCursor.getCount() : 0; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 544 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 545 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 546 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 547 | public Cursor getItem(int position) { |
| 548 | if (mCursor != null) { |
| 549 | mCursor.moveToPosition(position); |
| 550 | } |
| 551 | return mCursor; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 552 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 553 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 554 | @Override |
| 555 | public long getItemId(int position) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 556 | return position; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 557 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 558 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 559 | |
| 560 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> { |
| 561 | private final ImageView mTarget; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 562 | private final Point mThumbSize; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 563 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 564 | public ThumbnailAsyncTask(ImageView target, Point thumbSize) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 565 | mTarget = target; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 566 | mThumbSize = thumbSize; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
| 570 | protected void onPreExecute() { |
| 571 | mTarget.setTag(this); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | protected Bitmap doInBackground(Uri... params) { |
| 576 | final Context context = mTarget.getContext(); |
| 577 | final Uri uri = params[0]; |
| 578 | |
| 579 | Bitmap result = null; |
| 580 | try { |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 581 | result = DocumentsContract.getDocumentThumbnail( |
| 582 | context.getContentResolver(), uri, mThumbSize, null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 583 | if (result != null) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 584 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 585 | context, mThumbSize); |
| 586 | thumbs.put(uri, result); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 587 | } |
| 588 | } catch (Exception e) { |
| 589 | Log.w(TAG, "Failed to load thumbnail: " + e); |
| 590 | } |
| 591 | return result; |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | protected void onPostExecute(Bitmap result) { |
| 596 | if (mTarget.getTag() == this) { |
| 597 | mTarget.setImageBitmap(result); |
| 598 | mTarget.setTag(null); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | private static String formatTime(Context context, long when) { |
| 604 | // TODO: DateUtils should make this easier |
| 605 | Time then = new Time(); |
| 606 | then.set(when); |
| 607 | Time now = new Time(); |
| 608 | now.setToNow(); |
| 609 | |
| 610 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 611 | | DateUtils.FORMAT_ABBREV_ALL; |
| 612 | |
| 613 | if (then.year != now.year) { |
| 614 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 615 | } else if (then.yearDay != now.yearDay) { |
| 616 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 617 | } else { |
| 618 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 619 | } |
| 620 | |
| 621 | return DateUtils.formatDateTime(context, when, flags); |
| 622 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 623 | |
| 624 | private String findCommonMimeType(List<String> mimeTypes) { |
| 625 | String[] commonType = mimeTypes.get(0).split("/"); |
| 626 | if (commonType.length != 2) { |
| 627 | return "*/*"; |
| 628 | } |
| 629 | |
| 630 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 631 | String[] type = mimeTypes.get(i).split("/"); |
| 632 | if (type.length != 2) continue; |
| 633 | |
| 634 | if (!commonType[1].equals(type[1])) { |
| 635 | commonType[1] = "*"; |
| 636 | } |
| 637 | |
| 638 | if (!commonType[0].equals(type[0])) { |
| 639 | commonType[0] = "*"; |
| 640 | commonType[1] = "*"; |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return commonType[0] + "/" + commonType[1]; |
| 646 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 647 | } |