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