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