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