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 | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 217 | @Override |
| 218 | public void onStart() { |
| 219 | super.onStart(); |
| 220 | updateDisplayState(); |
| 221 | } |
| 222 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 223 | public void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 224 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 225 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 226 | if (mLastSortOrder != state.sortOrder) { |
| 227 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 228 | mLastSortOrder = state.sortOrder; |
| 229 | } |
| 230 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 231 | mListView.smoothScrollToPosition(0); |
| 232 | mGridView.smoothScrollToPosition(0); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 233 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 234 | mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE); |
| 235 | mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 236 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 237 | mFilter = new MimePredicate(state.acceptMimes); |
| 238 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 239 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 240 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 241 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 242 | } else { |
| 243 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 244 | } |
| 245 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 246 | final int thumbSize; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 247 | if (state.mode == MODE_GRID) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 248 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 249 | mListView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 250 | mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 251 | mGridView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 252 | mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width)); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 253 | mGridView.setNumColumns(GridView.AUTO_FIT); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 254 | mGridView.setChoiceMode(choiceMode); |
| 255 | mCurrentView = mGridView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 256 | } else if (state.mode == MODE_LIST) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 257 | thumbSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 258 | mGridView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 259 | mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 260 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 261 | mListView.setChoiceMode(choiceMode); |
| 262 | mCurrentView = mListView; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 263 | } else { |
| 264 | throw new IllegalStateException(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 265 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 266 | |
| 267 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 271 | @Override |
| 272 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 273 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 274 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 275 | if (mFilter.apply(doc)) { |
| 276 | ((DocumentsActivity) getActivity()).onDocumentPicked(doc); |
| 277 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 278 | } |
| 279 | }; |
| 280 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 281 | private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() { |
| 282 | @Override |
| 283 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 284 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 290 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 291 | |
| 292 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 293 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 294 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 295 | |
| 296 | final boolean manageMode = state.action == ACTION_MANAGE; |
| 297 | open.setVisible(!manageMode); |
| 298 | share.setVisible(manageMode); |
| 299 | delete.setVisible(manageMode); |
| 300 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 306 | final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions(); |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 307 | final ArrayList<DocumentInfo> docs = Lists.newArrayList(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 308 | final int size = checked.size(); |
| 309 | for (int i = 0; i < size; i++) { |
| 310 | if (checked.valueAt(i)) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 311 | final Cursor cursor = mAdapter.getItem(checked.keyAt(i)); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 312 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 313 | docs.add(doc); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 314 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 315 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 316 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 317 | final int id = item.getItemId(); |
| 318 | if (id == R.id.menu_open) { |
| 319 | DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 320 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 321 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 322 | |
| 323 | } else if (id == R.id.menu_share) { |
| 324 | onShareDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 325 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 326 | return true; |
| 327 | |
| 328 | } else if (id == R.id.menu_delete) { |
| 329 | onDeleteDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 330 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 331 | return true; |
| 332 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 333 | } else { |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void onDestroyActionMode(ActionMode mode) { |
| 340 | // ignored |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void onItemCheckedStateChanged( |
| 345 | ActionMode mode, int position, long id, boolean checked) { |
| 346 | if (checked) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 347 | // Directories cannot be checked |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 348 | final Cursor cursor = mAdapter.getItem(position); |
| 349 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 350 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 351 | mCurrentView.setItemChecked(position, false); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | mode.setTitle(getResources() |
| 356 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
| 357 | } |
| 358 | }; |
| 359 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 360 | private void onShareDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 361 | Intent intent; |
| 362 | if (docs.size() == 1) { |
| 363 | final DocumentInfo doc = docs.get(0); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 364 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 365 | intent = new Intent(Intent.ACTION_SEND); |
| 366 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 367 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 368 | intent.setType(doc.mimeType); |
| 369 | intent.putExtra(Intent.EXTRA_STREAM, doc.uri); |
| 370 | |
| 371 | } else if (docs.size() > 1) { |
| 372 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 373 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 374 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 375 | |
| 376 | final ArrayList<String> mimeTypes = Lists.newArrayList(); |
| 377 | final ArrayList<Uri> uris = Lists.newArrayList(); |
| 378 | for (DocumentInfo doc : docs) { |
| 379 | mimeTypes.add(doc.mimeType); |
| 380 | uris.add(doc.uri); |
| 381 | } |
| 382 | |
| 383 | intent.setType(findCommonMimeType(mimeTypes)); |
| 384 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 385 | |
| 386 | } else { |
| 387 | return; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 390 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 391 | startActivity(intent); |
| 392 | } |
| 393 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 394 | private void onDeleteDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 395 | final Context context = getActivity(); |
| 396 | final ContentResolver resolver = context.getContentResolver(); |
| 397 | |
| 398 | boolean hadTrouble = false; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 399 | for (DocumentInfo doc : docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 400 | if (!doc.isDeleteSupported()) { |
| 401 | Log.w(TAG, "Skipping " + doc); |
| 402 | hadTrouble = true; |
| 403 | continue; |
| 404 | } |
| 405 | |
Jeff Sharkey | 4c5fe52 | 2013-09-03 14:17:06 -0700 | [diff] [blame] | 406 | if (!DocumentsContract.deleteDocument(resolver, doc.uri)) { |
| 407 | Log.w(TAG, "Failed to delete " + doc); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 408 | hadTrouble = true; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (hadTrouble) { |
| 413 | Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show(); |
| 414 | } |
| 415 | } |
| 416 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 417 | private static State getDisplayState(Fragment fragment) { |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 418 | return ((DocumentsActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 421 | private interface Footer { |
| 422 | public View getView(View convertView, ViewGroup parent); |
| 423 | } |
| 424 | |
| 425 | private static class LoadingFooter implements Footer { |
| 426 | @Override |
| 427 | public View getView(View convertView, ViewGroup parent) { |
| 428 | final Context context = parent.getContext(); |
| 429 | if (convertView == null) { |
| 430 | final LayoutInflater inflater = LayoutInflater.from(context); |
| 431 | convertView = inflater.inflate(R.layout.item_loading, parent, false); |
| 432 | } |
| 433 | return convertView; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | private class MessageFooter implements Footer { |
| 438 | private final int mIcon; |
| 439 | private final String mMessage; |
| 440 | |
| 441 | public MessageFooter(int icon, String message) { |
| 442 | mIcon = icon; |
| 443 | mMessage = message; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public View getView(View convertView, ViewGroup parent) { |
| 448 | final Context context = parent.getContext(); |
| 449 | final State state = getDisplayState(DirectoryFragment.this); |
| 450 | |
| 451 | if (convertView == null) { |
| 452 | final LayoutInflater inflater = LayoutInflater.from(context); |
| 453 | if (state.mode == MODE_LIST) { |
| 454 | convertView = inflater.inflate(R.layout.item_message_list, parent, false); |
| 455 | } else if (state.mode == MODE_GRID) { |
| 456 | convertView = inflater.inflate(R.layout.item_message_grid, parent, false); |
| 457 | } else { |
| 458 | throw new IllegalStateException(); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 463 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 464 | icon.setImageResource(mIcon); |
| 465 | title.setText(mMessage); |
| 466 | return convertView; |
| 467 | } |
| 468 | } |
| 469 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 470 | private class DocumentsAdapter extends BaseAdapter { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 471 | private Cursor mCursor; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 472 | private int mCursorCount; |
| 473 | |
| 474 | private List<Footer> mFooters = Lists.newArrayList(); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 475 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 476 | public void swapCursor(Cursor cursor) { |
| 477 | mCursor = cursor; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 478 | mCursorCount = cursor != null ? cursor.getCount() : 0; |
| 479 | |
| 480 | mFooters.clear(); |
| 481 | |
| 482 | final Bundle extras = cursor != null ? cursor.getExtras() : null; |
| 483 | if (extras != null) { |
| 484 | final String info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 485 | if (info != null) { |
Jeff Sharkey | 06c4187 | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 486 | mFooters.add(new MessageFooter(R.drawable.ic_dialog_alert, info)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 487 | } |
| 488 | final String error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 489 | if (error != null) { |
Jeff Sharkey | 06c4187 | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 490 | mFooters.add(new MessageFooter(R.drawable.ic_dialog_alert, error)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 491 | } |
| 492 | if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) { |
| 493 | mFooters.add(new LoadingFooter()); |
| 494 | } |
| 495 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 496 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 497 | if (isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 498 | mEmptyView.setVisibility(View.VISIBLE); |
| 499 | } else { |
| 500 | mEmptyView.setVisibility(View.GONE); |
| 501 | } |
| 502 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 503 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Override |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 507 | public View getView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 508 | if (position < mCursorCount) { |
| 509 | return getDocumentView(position, convertView, parent); |
| 510 | } else { |
| 511 | position -= mCursorCount; |
| 512 | return mFooters.get(position).getView(convertView, parent); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | private View getDocumentView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 517 | final Context context = parent.getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 518 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 519 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 520 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 521 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 522 | context, mThumbSize); |
| 523 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 524 | if (convertView == null) { |
| 525 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 526 | if (state.mode == MODE_LIST) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 527 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 528 | } else if (state.mode == MODE_GRID) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 529 | convertView = inflater.inflate(R.layout.item_doc_grid, parent, false); |
| 530 | } else { |
| 531 | throw new IllegalStateException(); |
| 532 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 533 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 534 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 535 | final Cursor cursor = getItem(position); |
| 536 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 537 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 538 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 539 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 540 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 541 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 542 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 543 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 544 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 545 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 546 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 547 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 548 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 549 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 550 | final View line2 = convertView.findViewById(R.id.line2); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 551 | final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1); |
| 552 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 553 | final TextView date = (TextView) convertView.findViewById(R.id.date); |
| 554 | final TextView size = (TextView) convertView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 555 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 556 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag(); |
| 557 | if (oldTask != null) { |
| 558 | oldTask.cancel(false); |
| 559 | } |
| 560 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 561 | if ((docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 562 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 563 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 564 | if (cachedResult != null) { |
| 565 | icon.setImageBitmap(cachedResult); |
| 566 | } else { |
| 567 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize); |
| 568 | icon.setImageBitmap(null); |
| 569 | icon.setTag(task); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 570 | task.execute(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 571 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 572 | } else if (docIcon != 0) { |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 573 | icon.setImageDrawable(IconUtils.loadPackageIcon(context, docAuthority, docIcon)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 574 | } else { |
Jeff Sharkey | 5629ec5 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 575 | icon.setImageDrawable(IconUtils.loadMimeIcon(context, docMimeType)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 578 | title.setText(docDisplayName); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 579 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 580 | boolean hasLine2 = false; |
| 581 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 582 | if (mType == TYPE_RECENT_OPEN) { |
| 583 | final RootInfo root = roots.getRoot(docAuthority, docRootId); |
| 584 | icon1.setVisibility(View.VISIBLE); |
| 585 | icon1.setImageDrawable(root.loadIcon(context)); |
| 586 | summary.setText(root.getDirectoryString()); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 587 | summary.setVisibility(View.VISIBLE); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 588 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 589 | hasLine2 = true; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 590 | } else { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 591 | icon1.setVisibility(View.GONE); |
| 592 | if (docSummary != null) { |
| 593 | summary.setText(docSummary); |
| 594 | summary.setVisibility(View.VISIBLE); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 595 | hasLine2 = true; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 596 | } else { |
| 597 | summary.setVisibility(View.INVISIBLE); |
| 598 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 601 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 602 | date.setText(null); |
| 603 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 604 | date.setText(formatTime(context, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 605 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 606 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 607 | |
| 608 | if (state.showSize) { |
| 609 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 610 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 611 | size.setText(null); |
| 612 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 613 | size.setText(Formatter.formatFileSize(context, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 614 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 615 | } |
| 616 | } else { |
| 617 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 618 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 619 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame^] | 620 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 621 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 622 | return convertView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 623 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 624 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 625 | @Override |
| 626 | public int getCount() { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 627 | return mCursorCount + mFooters.size(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 628 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 629 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 630 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 631 | public Cursor getItem(int position) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 632 | if (position < mCursorCount) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 633 | mCursor.moveToPosition(position); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 634 | return mCursor; |
| 635 | } else { |
| 636 | return null; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 637 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 638 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 639 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 640 | @Override |
| 641 | public long getItemId(int position) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 642 | return position; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 643 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 644 | |
| 645 | @Override |
| 646 | public int getItemViewType(int position) { |
| 647 | if (position < mCursorCount) { |
| 648 | return 0; |
| 649 | } else { |
| 650 | return IGNORE_ITEM_VIEW_TYPE; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public boolean areAllItemsEnabled() { |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public boolean isEnabled(int position) { |
| 661 | return position < mCursorCount; |
| 662 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 663 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 664 | |
| 665 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> { |
| 666 | private final ImageView mTarget; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 667 | private final Point mThumbSize; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 668 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 669 | public ThumbnailAsyncTask(ImageView target, Point thumbSize) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 670 | mTarget = target; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 671 | mThumbSize = thumbSize; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | @Override |
| 675 | protected void onPreExecute() { |
| 676 | mTarget.setTag(this); |
| 677 | } |
| 678 | |
| 679 | @Override |
| 680 | protected Bitmap doInBackground(Uri... params) { |
| 681 | final Context context = mTarget.getContext(); |
| 682 | final Uri uri = params[0]; |
| 683 | |
| 684 | Bitmap result = null; |
| 685 | try { |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 686 | result = DocumentsContract.getDocumentThumbnail( |
| 687 | context.getContentResolver(), uri, mThumbSize, null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 688 | if (result != null) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 689 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 690 | context, mThumbSize); |
| 691 | thumbs.put(uri, result); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 692 | } |
| 693 | } catch (Exception e) { |
| 694 | Log.w(TAG, "Failed to load thumbnail: " + e); |
| 695 | } |
| 696 | return result; |
| 697 | } |
| 698 | |
| 699 | @Override |
| 700 | protected void onPostExecute(Bitmap result) { |
| 701 | if (mTarget.getTag() == this) { |
| 702 | mTarget.setImageBitmap(result); |
| 703 | mTarget.setTag(null); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | private static String formatTime(Context context, long when) { |
| 709 | // TODO: DateUtils should make this easier |
| 710 | Time then = new Time(); |
| 711 | then.set(when); |
| 712 | Time now = new Time(); |
| 713 | now.setToNow(); |
| 714 | |
| 715 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 716 | | DateUtils.FORMAT_ABBREV_ALL; |
| 717 | |
| 718 | if (then.year != now.year) { |
| 719 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 720 | } else if (then.yearDay != now.yearDay) { |
| 721 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 722 | } else { |
| 723 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 724 | } |
| 725 | |
| 726 | return DateUtils.formatDateTime(context, when, flags); |
| 727 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 728 | |
| 729 | private String findCommonMimeType(List<String> mimeTypes) { |
| 730 | String[] commonType = mimeTypes.get(0).split("/"); |
| 731 | if (commonType.length != 2) { |
| 732 | return "*/*"; |
| 733 | } |
| 734 | |
| 735 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 736 | String[] type = mimeTypes.get(i).split("/"); |
| 737 | if (type.length != 2) continue; |
| 738 | |
| 739 | if (!commonType[1].equals(type[1])) { |
| 740 | commonType[1] = "*"; |
| 741 | } |
| 742 | |
| 743 | if (!commonType[0].equals(type[0])) { |
| 744 | commonType[0] = "*"; |
| 745 | commonType[1] = "*"; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return commonType[0] + "/" + commonType[1]; |
| 751 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 752 | } |