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