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