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