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