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 | 311a7d8 | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE; |
| 20 | import static com.android.documentsui.BaseActivity.State.ACTION_BROWSE_ALL; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.BaseActivity.State.ACTION_CREATE; |
| 22 | import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE; |
| 23 | import static com.android.documentsui.BaseActivity.State.MODE_GRID; |
| 24 | import static com.android.documentsui.BaseActivity.State.MODE_LIST; |
| 25 | import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN; |
| 26 | import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN; |
Jeff Sharkey | 311a7d8 | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 27 | import static com.android.documentsui.DocumentsActivity.TAG; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 28 | import static com.android.documentsui.model.DocumentInfo.getCursorInt; |
| 29 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 30 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 31 | import static com.android.internal.util.Preconditions.checkNotNull; |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 32 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 33 | import android.annotation.NonNull; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 34 | import android.app.Activity; |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 35 | import android.app.ActivityManager; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 36 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 37 | import android.app.FragmentManager; |
| 38 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 39 | import android.app.LoaderManager.LoaderCallbacks; |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 40 | import android.content.ClipData; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 41 | import android.content.ContentProviderClient; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 42 | import android.content.ContentResolver; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 43 | import android.content.ContentValues; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 44 | import android.content.Context; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 45 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 46 | import android.content.Loader; |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 47 | import android.content.res.Resources; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 48 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 49 | import android.graphics.Bitmap; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 50 | import android.graphics.Canvas; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 51 | import android.graphics.Point; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 52 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 53 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 54 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 55 | import android.os.Bundle; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 56 | import android.os.CancellationSignal; |
Makoto Onuki | 7777875 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 57 | import android.os.Handler; |
| 58 | import android.os.Looper; |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 59 | import android.os.OperationCanceledException; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 60 | import android.os.Parcelable; |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 61 | import android.os.SystemProperties; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 62 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 63 | import android.provider.DocumentsContract.Document; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 64 | import android.support.v7.widget.GridLayoutManager; |
| 65 | import android.support.v7.widget.LinearLayoutManager; |
| 66 | import android.support.v7.widget.RecyclerView; |
| 67 | import android.support.v7.widget.RecyclerView.LayoutManager; |
| 68 | import android.support.v7.widget.RecyclerView.OnItemTouchListener; |
| 69 | import android.support.v7.widget.RecyclerView.RecyclerListener; |
| 70 | import android.support.v7.widget.RecyclerView.ViewHolder; |
Jeff Sharkey | 6d57927 | 2015-06-11 09:16:19 -0700 | [diff] [blame] | 71 | import android.text.TextUtils; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 72 | import android.text.format.DateUtils; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 73 | import android.text.format.Formatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 74 | import android.text.format.Time; |
| 75 | import android.util.Log; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 76 | import android.util.SparseArray; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 77 | import android.view.ActionMode; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 78 | import android.view.DragEvent; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 79 | import android.view.GestureDetector; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 80 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 81 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 82 | import android.view.MenuItem; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 83 | import android.view.MotionEvent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 84 | import android.view.View; |
| 85 | import android.view.ViewGroup; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 86 | import android.view.ViewParent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 87 | import android.widget.ImageView; |
| 88 | import android.widget.ListView; |
| 89 | import android.widget.TextView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 90 | import android.widget.Toast; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 91 | |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 92 | import com.android.documentsui.BaseActivity.State; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 93 | import com.android.documentsui.MultiSelectManager.Selection; |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 94 | import com.android.documentsui.ProviderExecutor.Preemptable; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 95 | import com.android.documentsui.RecentsProvider.StateColumns; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 96 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 97 | import com.android.documentsui.model.DocumentStack; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 98 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 99 | import com.android.internal.util.Preconditions; |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 100 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 101 | import com.google.android.collect.Lists; |
| 102 | |
| 103 | import java.util.ArrayList; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 104 | import java.util.Collections; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 105 | import java.util.List; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Display the documents inside a single directory. |
| 109 | */ |
| 110 | public class DirectoryFragment extends Fragment { |
| 111 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 112 | public static final int TYPE_NORMAL = 1; |
| 113 | public static final int TYPE_SEARCH = 2; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 114 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 115 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 116 | public static final int ANIM_NONE = 1; |
| 117 | public static final int ANIM_SIDE = 2; |
| 118 | public static final int ANIM_DOWN = 3; |
| 119 | public static final int ANIM_UP = 4; |
| 120 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 121 | public static final int REQUEST_COPY_DESTINATION = 1; |
| 122 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 123 | private static final int LOADER_ID = 42; |
| 124 | private static final boolean DEBUG = false; |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 125 | private static final boolean DEBUG_ENABLE_DND = false; |
| 126 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 127 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 128 | private static final String EXTRA_ROOT = "root"; |
| 129 | private static final String EXTRA_DOC = "doc"; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 130 | private static final String EXTRA_QUERY = "query"; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 131 | private static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 132 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 133 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 134 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 135 | private View mEmptyView; |
| 136 | private RecyclerView mRecView; |
| 137 | |
| 138 | private int mType = TYPE_NORMAL; |
| 139 | private String mStateKey; |
| 140 | |
| 141 | private int mLastMode = MODE_UNKNOWN; |
| 142 | private int mLastSortOrder = SORT_ORDER_UNKNOWN; |
| 143 | private boolean mLastShowSize; |
| 144 | private boolean mHideGridTitles; |
| 145 | private boolean mSvelteRecents; |
| 146 | private Point mThumbSize; |
| 147 | private DocumentsAdapter mAdapter; |
| 148 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 149 | private FragmentTuner mFragmentTuner; |
| 150 | private DocumentClipper mClipper; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 151 | private MultiSelectManager mSelectionManager; |
| 152 | // These are lazily initialized. |
| 153 | private LayoutManager mListLayout; |
| 154 | private LayoutManager mGridLayout; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 155 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 156 | public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
| 157 | show(fm, TYPE_NORMAL, root, doc, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 160 | public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) { |
| 161 | show(fm, TYPE_SEARCH, root, null, query, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 164 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
| 165 | show(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 168 | private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 169 | String query, int anim) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 170 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 171 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 172 | args.putParcelable(EXTRA_ROOT, root); |
| 173 | args.putParcelable(EXTRA_DOC, doc); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 174 | args.putString(EXTRA_QUERY, query); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 175 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 176 | final FragmentTransaction ft = fm.beginTransaction(); |
| 177 | switch (anim) { |
| 178 | case ANIM_SIDE: |
| 179 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 180 | break; |
| 181 | case ANIM_DOWN: |
| 182 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 183 | ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen); |
| 184 | break; |
| 185 | case ANIM_UP: |
| 186 | ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up); |
| 187 | break; |
| 188 | } |
| 189 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 190 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 191 | fragment.setArguments(args); |
| 192 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 193 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 194 | ft.commitAllowingStateLoss(); |
| 195 | } |
| 196 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 197 | private static String buildStateKey(RootInfo root, DocumentInfo doc) { |
| 198 | final StringBuilder builder = new StringBuilder(); |
| 199 | builder.append(root != null ? root.authority : "null").append(';'); |
| 200 | builder.append(root != null ? root.rootId : "null").append(';'); |
| 201 | builder.append(doc != null ? doc.documentId : "null"); |
| 202 | return builder.toString(); |
| 203 | } |
| 204 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 205 | public static DirectoryFragment get(FragmentManager fm) { |
| 206 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 207 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public View onCreateView( |
| 212 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 213 | final Context context = inflater.getContext(); |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 214 | final Resources res = context.getResources(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 215 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 216 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 217 | mEmptyView = view.findViewById(android.R.id.empty); |
| 218 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 219 | mRecView = (RecyclerView) view.findViewById(R.id.recyclerView); |
| 220 | mRecView.setRecyclerListener( |
| 221 | new RecyclerListener() { |
| 222 | @Override |
| 223 | public void onViewRecycled(ViewHolder holder) { |
| 224 | cancelThumbnailTask(holder.itemView); |
| 225 | } |
| 226 | }); |
| 227 | // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration). |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 228 | |
| 229 | if (DEBUG_ENABLE_DND) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 230 | setupDragAndDropOnDirectoryView(mRecView); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 231 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 232 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 233 | return view; |
| 234 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 235 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 236 | @Override |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 237 | public void onDestroyView() { |
| 238 | super.onDestroyView(); |
| 239 | |
| 240 | // Cancel any outstanding thumbnail requests |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 241 | final int count = mRecView.getChildCount(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 242 | for (int i = 0; i < count; i++) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 243 | final View view = mRecView.getChildAt(i); |
| 244 | cancelThumbnailTask(view); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 245 | } |
Jeff Sharkey | faaeb39 | 2013-10-04 14:44:56 -0700 | [diff] [blame] | 246 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 247 | // Clear any outstanding selection |
| 248 | mSelectionManager.clearSelection(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 252 | public void onActivityCreated(Bundle savedInstanceState) { |
| 253 | super.onActivityCreated(savedInstanceState); |
| 254 | |
| 255 | final Context context = getActivity(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 256 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 257 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 258 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 259 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 260 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 261 | mAdapter = new DocumentsAdapter(context); |
| 262 | mRecView.setAdapter(mAdapter); |
| 263 | |
| 264 | GestureDetector.SimpleOnGestureListener listener = |
| 265 | new GestureDetector.SimpleOnGestureListener() { |
| 266 | @Override |
| 267 | public boolean onSingleTapUp(MotionEvent e) { |
| 268 | return DirectoryFragment.this.onSingleTapUp(e); |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | mSelectionManager = new MultiSelectManager(mRecView, listener); |
| 273 | mSelectionManager.addCallback(new SelectionModeListener()); |
| 274 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 275 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 276 | mStateKey = buildStateKey(root, doc); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 277 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 278 | mFragmentTuner = pickFragmentTuner(state); |
| 279 | mClipper = new DocumentClipper(context); |
| 280 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 281 | if (mType == TYPE_RECENT_OPEN) { |
| 282 | // Hide titles when showing recents for picking images/videos |
| 283 | mHideGridTitles = MimePredicate.mimeMatches( |
| 284 | MimePredicate.VISUAL_MIMES, state.acceptMimes); |
| 285 | } else { |
| 286 | mHideGridTitles = (doc != null) && doc.isGridTitlesHidden(); |
| 287 | } |
| 288 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 289 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 290 | Context.ACTIVITY_SERVICE); |
| 291 | mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN); |
| 292 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 293 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 294 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 295 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 296 | final String query = getArguments().getString(EXTRA_QUERY); |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 297 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 298 | Uri contentsUri; |
| 299 | switch (mType) { |
| 300 | case TYPE_NORMAL: |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 301 | contentsUri = DocumentsContract.buildChildDocumentsUri( |
| 302 | doc.authority, doc.documentId); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 303 | if (state.action == ACTION_MANAGE) { |
| 304 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 305 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 306 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 307 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 308 | case TYPE_SEARCH: |
| 309 | contentsUri = DocumentsContract.buildSearchDocumentsUri( |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 310 | root.authority, root.rootId, query); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 311 | if (state.action == ACTION_MANAGE) { |
| 312 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 313 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 314 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 315 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 316 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 317 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 318 | return new RecentLoader(context, roots, state); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 319 | default: |
| 320 | throw new IllegalStateException("Unknown type " + mType); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 321 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 325 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Makoto Onuki | 7777875 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 326 | if (result == null || result.exception != null) { |
| 327 | // onBackPressed does a fragment transaction, which can't be done inside |
| 328 | // onLoadFinished |
| 329 | mHandler.post(new Runnable() { |
| 330 | @Override |
| 331 | public void run() { |
| 332 | final Activity activity = getActivity(); |
| 333 | if (activity != null) { |
| 334 | activity.onBackPressed(); |
| 335 | } |
| 336 | } |
| 337 | }); |
| 338 | return; |
| 339 | } |
| 340 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 341 | if (!isAdded()) return; |
| 342 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 343 | mAdapter.replaceResult(result); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 344 | |
| 345 | // Push latest state up to UI |
| 346 | // TODO: if mode change was racing with us, don't overwrite it |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 347 | if (result.mode != MODE_UNKNOWN) { |
| 348 | state.derivedMode = result.mode; |
| 349 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 350 | state.derivedSortOrder = result.sortOrder; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 351 | ((BaseActivity) context).onStateChanged(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 352 | |
| 353 | updateDisplayState(); |
| 354 | |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 355 | // When launched into empty recents, show drawer |
Steve McKay | b68dd22 | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 356 | if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched && |
| 357 | context instanceof DocumentsActivity) { |
| 358 | ((DocumentsActivity) context).setRootsDrawerOpen(true); |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 361 | // Restore any previous instance state |
| 362 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 363 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 364 | getView().restoreHierarchyState(container); |
| 365 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 366 | mRecView.smoothScrollToPosition(0); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 367 | } |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 368 | |
| 369 | mLastSortOrder = state.derivedSortOrder; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 373 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 374 | mAdapter.replaceResult(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 375 | } |
| 376 | }; |
| 377 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 378 | // Kick off loader at least once |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 379 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 380 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 381 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 384 | @Override |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 385 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 386 | // There's only one request code right now. Replace this with a switch statement or |
| 387 | // something more scalable when more codes are added. |
| 388 | if (requestCode != REQUEST_COPY_DESTINATION) { |
| 389 | return; |
| 390 | } |
| 391 | if (resultCode == Activity.RESULT_CANCELED || data == null) { |
| 392 | // User pressed the back button or otherwise cancelled the destination pick. Don't |
| 393 | // proceed with the copy. |
| 394 | return; |
| 395 | } |
| 396 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 397 | CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy, |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 398 | (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK), |
| 399 | data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE)); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 402 | private int getEventAdapterPosition(MotionEvent e) { |
| 403 | View view = mRecView.findChildViewUnder(e.getX(), e.getY()); |
| 404 | return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION; |
| 405 | } |
| 406 | |
| 407 | private boolean onSingleTapUp(MotionEvent e) { |
| 408 | int position = getEventAdapterPosition(e); |
| 409 | |
| 410 | if (position != RecyclerView.NO_POSITION) { |
| 411 | final Cursor cursor = mAdapter.getItem(position); |
| 412 | checkNotNull(cursor, "Cursor cannot be null."); |
| 413 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 414 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 415 | if (isDocumentEnabled(docMimeType, docFlags)) { |
| 416 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 417 | ((BaseActivity) getActivity()).onDocumentPicked(doc); |
| 418 | return true; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return false; |
| 423 | } |
| 424 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 425 | @Override |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 426 | public void onStop() { |
| 427 | super.onStop(); |
| 428 | |
| 429 | // Remember last scroll location |
| 430 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 431 | getView().saveHierarchyState(container); |
| 432 | final State state = getDisplayState(this); |
| 433 | state.dirState.put(mStateKey, container); |
| 434 | } |
| 435 | |
| 436 | @Override |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 437 | public void onResume() { |
| 438 | super.onResume(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 439 | updateDisplayState(); |
| 440 | } |
| 441 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 442 | public void onDisplayStateChanged() { |
| 443 | updateDisplayState(); |
| 444 | } |
| 445 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 446 | public void onUserSortOrderChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 447 | // Sort order change always triggers reload; we'll trigger state change |
| 448 | // on the flip side. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 449 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | public void onUserModeChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 453 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 454 | final State state = getDisplayState(this); |
| 455 | |
| 456 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 457 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 458 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 459 | if (root != null && doc != null) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 460 | final Uri stateUri = RecentsProvider.buildState( |
| 461 | root.authority, root.rootId, doc.documentId); |
| 462 | final ContentValues values = new ContentValues(); |
| 463 | values.put(StateColumns.MODE, state.userMode); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 464 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 465 | new AsyncTask<Void, Void, Void>() { |
| 466 | @Override |
| 467 | protected Void doInBackground(Void... params) { |
| 468 | resolver.insert(stateUri, values); |
| 469 | return null; |
| 470 | } |
| 471 | }.execute(); |
| 472 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 473 | |
| 474 | // Mode change is just visual change; no need to kick loader, and |
| 475 | // deliver change event immediately. |
| 476 | state.derivedMode = state.userMode; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 477 | ((BaseActivity) getActivity()).onStateChanged(); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 478 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 479 | updateDisplayState(); |
| 480 | } |
| 481 | |
| 482 | private void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 483 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 484 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 485 | if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 486 | mLastMode = state.derivedMode; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 487 | mLastShowSize = state.showSize; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 488 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 489 | updateLayout(state.derivedMode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 490 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 491 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 492 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 493 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 494 | } else { |
| 495 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 496 | } |
| 497 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 498 | final int thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
| 499 | mThumbSize = new Point(thumbSize, thumbSize); |
| 500 | mRecView.setAdapter(mAdapter); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Returns a {@code LayoutManager} for {@code mode}, lazily initializing |
| 505 | * classes as needed. |
| 506 | */ |
| 507 | private void updateLayout(int mode) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 508 | final int thumbSize; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 509 | |
| 510 | final LayoutManager layout; |
| 511 | switch (mode) { |
| 512 | case MODE_GRID: |
| 513 | if (mGridLayout == null) { |
| 514 | // TODO: Determine appropriate column count. |
| 515 | mGridLayout = new GridLayoutManager(getContext(), 4); |
| 516 | } |
| 517 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
| 518 | layout = mGridLayout; |
| 519 | break; |
| 520 | case MODE_LIST: |
| 521 | if (mListLayout == null) { |
| 522 | mListLayout = new LinearLayoutManager(getContext()); |
| 523 | } |
| 524 | thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
| 525 | layout = mListLayout; |
| 526 | break; |
| 527 | case MODE_UNKNOWN: |
| 528 | default: |
| 529 | throw new IllegalArgumentException("Unsupported layout mode: " + mode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 530 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 531 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 532 | mRecView.setLayoutManager(layout); |
| 533 | // setting layout manager automatically invalidates existing ViewHolders. |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 534 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 537 | /** |
| 538 | * Manages the integration between our ActionMode and MultiSelectManager, initiating |
| 539 | * ActionMode when there is a selection, canceling it when there is no selection, |
| 540 | * and clearing selection when action mode is explicitly exited by the user. |
| 541 | */ |
| 542 | private final class SelectionModeListener |
| 543 | implements MultiSelectManager.Callback, ActionMode.Callback { |
| 544 | |
| 545 | private Selection mSelected = new Selection(); |
| 546 | private ActionMode mActionMode; |
| 547 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 548 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 549 | public boolean onBeforeItemStateChange(int position, boolean selected) { |
| 550 | // Directories and footer items cannot be checked |
| 551 | if (selected) { |
| 552 | final Cursor cursor = mAdapter.getItem(position); |
| 553 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 554 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 555 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 556 | return isDocumentEnabled(docMimeType, docFlags); |
| 557 | } |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void onItemStateChanged(int position, boolean selected) { |
| 563 | mSelectionManager.getSelection(mSelected); |
| 564 | if (mSelected.size() == 0) { |
| 565 | if (DEBUG) Log.d(TAG, "Finishing action mode."); |
| 566 | if (mActionMode != null) { |
| 567 | mActionMode.finish(); |
| 568 | } |
| 569 | } else { |
| 570 | if (DEBUG) Log.d(TAG, "Maybe starting action mode."); |
| 571 | if (mActionMode == null) { |
| 572 | if (DEBUG) Log.d(TAG, "Yeah. Starting action mode."); |
| 573 | mActionMode = getActivity().startActionMode(this); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 574 | } |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 575 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 576 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 577 | if (mActionMode != null) { |
| 578 | mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size())); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Called when the user exits the action mode |
| 583 | @Override |
| 584 | public void onDestroyActionMode(ActionMode mode) { |
| 585 | if (DEBUG) Log.d(TAG, "Handling action mode destroyed."); |
| 586 | mActionMode = null; |
| 587 | // clear selection |
| 588 | mSelectionManager.clearSelection(); |
| 589 | } |
| 590 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 591 | @Override |
| 592 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 593 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 594 | mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size())); |
| 595 | return mSelectionManager.getSelection().size() > 0; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 600 | // Delegate update logic to our owning action, since specialized |
| 601 | // logic is desired. |
| 602 | mFragmentTuner.updateActionMenu(menu, mType); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 603 | return true; |
| 604 | } |
| 605 | |
| 606 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 607 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 608 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 609 | Selection selection = new Selection(); |
| 610 | mSelectionManager.getSelection(selection); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 611 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 612 | final int id = item.getItemId(); |
| 613 | if (id == R.id.menu_open) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 614 | openDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 615 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 616 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 617 | |
| 618 | } else if (id == R.id.menu_share) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 619 | shareDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 620 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 621 | return true; |
| 622 | |
| 623 | } else if (id == R.id.menu_delete) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 624 | deleteDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 625 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 626 | return true; |
| 627 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 628 | } else if (id == R.id.menu_copy_to) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 629 | transferDocuments(selection, CopyService.TRANSFER_MODE_COPY); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 630 | mode.finish(); |
| 631 | return true; |
| 632 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 633 | } else if (id == R.id.menu_move_to) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 634 | transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 635 | mode.finish(); |
| 636 | return true; |
| 637 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 638 | } else if (id == R.id.menu_copy_to_clipboard) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 639 | copySelectionToClipboard(selection); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 640 | mode.finish(); |
| 641 | return true; |
| 642 | |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 643 | } else if (id == R.id.menu_select_all) { |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 644 | selectAllFiles(); |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 645 | return true; |
| 646 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 647 | } else { |
| 648 | return false; |
| 649 | } |
| 650 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 651 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 652 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 653 | private static void cancelThumbnailTask(View view) { |
| 654 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 655 | if (iconThumb != null) { |
| 656 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
| 657 | if (oldTask != null) { |
| 658 | oldTask.preempt(); |
| 659 | iconThumb.setTag(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 662 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 663 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 664 | private void openDocuments(final Selection selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 665 | new GetDocumentsTask() { |
| 666 | @Override |
| 667 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 668 | // TODO: Implement support in standalone for opening multiple docs. |
| 669 | BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 670 | } |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 671 | }.execute(selected); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 674 | private void shareDocuments(final Selection selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 675 | new GetDocumentsTask() { |
| 676 | @Override |
| 677 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 678 | Intent intent; |
| 679 | |
| 680 | // Filter out directories - those can't be shared. |
| 681 | List<DocumentInfo> docsForSend = Lists.newArrayList(); |
| 682 | for (DocumentInfo doc: docs) { |
| 683 | if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) { |
| 684 | docsForSend.add(doc); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (docsForSend.size() == 1) { |
| 689 | final DocumentInfo doc = docsForSend.get(0); |
| 690 | |
| 691 | intent = new Intent(Intent.ACTION_SEND); |
| 692 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 693 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 694 | intent.setType(doc.mimeType); |
| 695 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
| 696 | |
| 697 | } else if (docsForSend.size() > 1) { |
| 698 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 699 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 700 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 701 | |
| 702 | final ArrayList<String> mimeTypes = Lists.newArrayList(); |
| 703 | final ArrayList<Uri> uris = Lists.newArrayList(); |
| 704 | for (DocumentInfo doc : docsForSend) { |
| 705 | mimeTypes.add(doc.mimeType); |
| 706 | uris.add(doc.derivedUri); |
| 707 | } |
| 708 | |
| 709 | intent.setType(findCommonMimeType(mimeTypes)); |
| 710 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 711 | |
| 712 | } else { |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
| 717 | startActivity(intent); |
| 718 | } |
| 719 | }.execute(selected); |
| 720 | } |
| 721 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 722 | private void deleteDocuments(final Selection selected) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 723 | final Context context = getActivity(); |
| 724 | final ContentResolver resolver = context.getContentResolver(); |
| 725 | |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 726 | new GetDocumentsTask() { |
| 727 | @Override |
| 728 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 729 | boolean hadTrouble = false; |
| 730 | for (DocumentInfo doc : docs) { |
| 731 | if (!doc.isDeleteSupported()) { |
| 732 | Log.w(TAG, "Skipping " + doc); |
| 733 | hadTrouble = true; |
| 734 | continue; |
| 735 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 736 | |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 737 | ContentProviderClient client = null; |
| 738 | try { |
| 739 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 740 | resolver, doc.derivedUri.getAuthority()); |
| 741 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 742 | } catch (Exception e) { |
| 743 | Log.w(TAG, "Failed to delete " + doc); |
| 744 | hadTrouble = true; |
| 745 | } finally { |
| 746 | ContentProviderClient.releaseQuietly(client); |
| 747 | } |
| 748 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 749 | |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 750 | if (hadTrouble) { |
| 751 | Toast.makeText( |
| 752 | context, |
| 753 | R.string.toast_failed_delete, |
| 754 | Toast.LENGTH_SHORT).show(); |
| 755 | } |
| 756 | } |
| 757 | }.execute(selected); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 760 | private void transferDocuments(final Selection selected, final int mode) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 761 | // Pop up a dialog to pick a destination. This is inadequate but works for now. |
| 762 | // TODO: Implement a picker that is to spec. |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 763 | final Intent intent = new Intent( |
Daichi Hirono | 22574ed | 2015-04-15 13:41:18 +0900 | [diff] [blame] | 764 | BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION, |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 765 | Uri.EMPTY, |
| 766 | getActivity(), |
| 767 | DocumentsActivity.class); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 768 | |
| 769 | new GetDocumentsTask() { |
| 770 | @Override |
| 771 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 772 | getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs; |
| 773 | |
| 774 | boolean directoryCopy = false; |
| 775 | for (DocumentInfo info : docs) { |
| 776 | if (Document.MIME_TYPE_DIR.equals(info.mimeType)) { |
| 777 | directoryCopy = true; |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy); |
| 782 | intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode); |
| 783 | startActivityForResult(intent, REQUEST_COPY_DESTINATION); |
Daichi Hirono | f2a822d | 2015-04-14 17:12:54 +0900 | [diff] [blame] | 784 | } |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 785 | }.execute(selected); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 788 | private static State getDisplayState(Fragment fragment) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 789 | return ((BaseActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 792 | private static abstract class Footer { |
| 793 | private final int mItemViewType; |
| 794 | |
| 795 | public Footer(int itemViewType) { |
| 796 | mItemViewType = itemViewType; |
| 797 | } |
| 798 | |
| 799 | public abstract View getView(View convertView, ViewGroup parent); |
| 800 | |
| 801 | public int getItemViewType() { |
| 802 | return mItemViewType; |
| 803 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 806 | private class LoadingFooter extends Footer { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 807 | public LoadingFooter() { |
| 808 | super(1); |
| 809 | } |
| 810 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 811 | @Override |
| 812 | public View getView(View convertView, ViewGroup parent) { |
| 813 | final Context context = parent.getContext(); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 814 | final State state = getDisplayState(DirectoryFragment.this); |
| 815 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 816 | if (convertView == null) { |
| 817 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 818 | if (state.derivedMode == MODE_LIST) { |
| 819 | convertView = inflater.inflate(R.layout.item_loading_list, parent, false); |
| 820 | } else if (state.derivedMode == MODE_GRID) { |
| 821 | convertView = inflater.inflate(R.layout.item_loading_grid, parent, false); |
| 822 | } else { |
| 823 | throw new IllegalStateException(); |
| 824 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 825 | } |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 826 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 827 | return convertView; |
| 828 | } |
| 829 | } |
| 830 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 831 | private class MessageFooter extends Footer { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 832 | private final int mIcon; |
| 833 | private final String mMessage; |
| 834 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 835 | public MessageFooter(int itemViewType, int icon, String message) { |
| 836 | super(itemViewType); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 837 | mIcon = icon; |
| 838 | mMessage = message; |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public View getView(View convertView, ViewGroup parent) { |
| 843 | final Context context = parent.getContext(); |
| 844 | final State state = getDisplayState(DirectoryFragment.this); |
| 845 | |
| 846 | if (convertView == null) { |
| 847 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 848 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 849 | convertView = inflater.inflate(R.layout.item_message_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 850 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 851 | convertView = inflater.inflate(R.layout.item_message_grid, parent, false); |
| 852 | } else { |
| 853 | throw new IllegalStateException(); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 858 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 859 | icon.setImageResource(mIcon); |
| 860 | title.setText(mMessage); |
| 861 | return convertView; |
| 862 | } |
| 863 | } |
| 864 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 865 | // Provide a reference to the views for each data item |
| 866 | // Complex data items may need more than one view per item, and |
| 867 | // you provide access to all the views for a data item in a view holder |
| 868 | private static final class DocumentHolder extends RecyclerView.ViewHolder { |
| 869 | // each data item is just a string in this case |
| 870 | public View view; |
| 871 | public String docId; // The stable document id. |
| 872 | public DocumentHolder(View view) { |
| 873 | super(view); |
| 874 | this.view = view; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> { |
| 879 | |
| 880 | private final Context mContext; |
| 881 | private final LayoutInflater mInflater; |
| 882 | // TODO: Bring back support for footers. |
| 883 | private final List<Footer> mFooters = Lists.newArrayList(); |
| 884 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 885 | private Cursor mCursor; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 886 | private int mCursorCount; |
| 887 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 888 | public DocumentsAdapter(Context context) { |
| 889 | mContext = context; |
| 890 | mInflater = LayoutInflater.from(context); |
| 891 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 892 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 893 | public void replaceResult(DirectoryResult result) { |
| 894 | if (DEBUG) Log.i(TAG, "Updating adapter with new result set."); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 895 | mCursor = result != null ? result.cursor : null; |
| 896 | mCursorCount = mCursor != null ? mCursor.getCount() : 0; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 897 | |
| 898 | mFooters.clear(); |
| 899 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 900 | final Bundle extras = mCursor != null ? mCursor.getExtras() : null; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 901 | if (extras != null) { |
| 902 | final String info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 903 | if (info != null) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 904 | mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 905 | } |
| 906 | final String error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 907 | if (error != null) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 908 | mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 909 | } |
| 910 | if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) { |
| 911 | mFooters.add(new LoadingFooter()); |
| 912 | } |
| 913 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 914 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 915 | if (result != null && result.exception != null) { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 916 | mFooters.add(new MessageFooter( |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 917 | 3, R.drawable.ic_dialog_alert, getString(R.string.query_error))); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 920 | if (isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 921 | mEmptyView.setVisibility(View.VISIBLE); |
| 922 | } else { |
| 923 | mEmptyView.setVisibility(View.GONE); |
| 924 | } |
| 925 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 926 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 930 | public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 931 | final State state = getDisplayState(DirectoryFragment.this); |
| 932 | final LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 933 | switch (state.derivedMode) { |
| 934 | case MODE_GRID: |
| 935 | return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false)); |
| 936 | case MODE_LIST: |
| 937 | return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false)); |
| 938 | case MODE_UNKNOWN: |
| 939 | default: |
| 940 | throw new IllegalStateException("Unsupported layout mode."); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 944 | @Override |
| 945 | public void onBindViewHolder(DocumentHolder holder, int position) { |
| 946 | |
| 947 | final Context context = getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 948 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 949 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 950 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 951 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 952 | context, mThumbSize); |
| 953 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 954 | final Cursor cursor = getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 955 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 956 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 957 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 958 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 959 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 960 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 961 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 962 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 963 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 964 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 965 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 966 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 967 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 968 | holder.docId = docId; |
| 969 | final View itemView = holder.view; |
| 970 | itemView.setActivated(mSelectionManager.getSelection().contains(position)); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 971 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 972 | final View line1 = itemView.findViewById(R.id.line1); |
| 973 | final View line2 = itemView.findViewById(R.id.line2); |
| 974 | |
| 975 | final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime); |
| 976 | final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); |
| 977 | final TextView title = (TextView) itemView.findViewById(android.R.id.title); |
| 978 | final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1); |
| 979 | final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2); |
| 980 | final TextView summary = (TextView) itemView.findViewById(android.R.id.summary); |
| 981 | final TextView date = (TextView) itemView.findViewById(R.id.date); |
| 982 | final TextView size = (TextView) itemView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 983 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 984 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 985 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 986 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 987 | iconThumb.setTag(null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 990 | iconMime.animate().cancel(); |
| 991 | iconThumb.animate().cancel(); |
| 992 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 993 | final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 994 | final boolean allowThumbnail = (state.derivedMode == MODE_GRID) |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 995 | || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 996 | final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 997 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 998 | final boolean enabled = isDocumentEnabled(docMimeType, docFlags); |
| 999 | final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f; |
| 1000 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1001 | boolean cacheHit = false; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1002 | if (showThumbnail) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1003 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1004 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1005 | if (cachedResult != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1006 | iconThumb.setImageBitmap(cachedResult); |
| 1007 | cacheHit = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1008 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1009 | iconThumb.setImageDrawable(null); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1010 | // TODO: Hang this off DocumentHolder? |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1011 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask( |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1012 | uri, iconMime, iconThumb, mThumbSize, iconAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1013 | iconThumb.setTag(task); |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 1014 | ProviderExecutor.forAuthority(docAuthority).execute(task); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1015 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Always throw MIME icon into place, even when a thumbnail is being |
| 1019 | // loaded in background. |
| 1020 | if (cacheHit) { |
| 1021 | iconMime.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1022 | iconMime.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1023 | iconThumb.setAlpha(1f); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1024 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1025 | iconMime.setAlpha(1f); |
| 1026 | iconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1027 | iconThumb.setImageDrawable(null); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1028 | iconMime.setImageDrawable( |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1029 | getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1032 | boolean hasLine1 = false; |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1033 | boolean hasLine2 = false; |
| 1034 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1035 | final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles; |
| 1036 | if (!hideTitle) { |
| 1037 | title.setText(docDisplayName); |
| 1038 | hasLine1 = true; |
| 1039 | } |
| 1040 | |
| 1041 | Drawable iconDrawable = null; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1042 | if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 1043 | // We've already had to enumerate roots before any results can |
| 1044 | // be shown, so this will never block. |
| 1045 | final RootInfo root = roots.getRootBlocking(docAuthority, docRootId); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1046 | if (state.derivedMode == MODE_GRID) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1047 | iconDrawable = root.loadGridIcon(mContext); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1048 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1049 | iconDrawable = root.loadIcon(mContext); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1050 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1051 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1052 | if (summary != null) { |
| 1053 | final boolean alwaysShowSummary = getResources() |
| 1054 | .getBoolean(R.bool.always_show_summary); |
| 1055 | if (alwaysShowSummary) { |
| 1056 | summary.setText(root.getDirectoryString()); |
| 1057 | summary.setVisibility(View.VISIBLE); |
| 1058 | hasLine2 = true; |
| 1059 | } else { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 1060 | if (iconDrawable != null && roots.isIconUniqueBlocking(root)) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1061 | // No summary needed if icon speaks for itself |
| 1062 | summary.setVisibility(View.INVISIBLE); |
| 1063 | } else { |
| 1064 | summary.setText(root.getDirectoryString()); |
| 1065 | summary.setVisibility(View.VISIBLE); |
| 1066 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 1067 | hasLine2 = true; |
| 1068 | } |
| 1069 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1070 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1071 | } else { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1072 | // Directories showing thumbnails in grid mode get a little icon |
| 1073 | // hint to remind user they're a directory. |
| 1074 | if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID |
| 1075 | && showThumbnail) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1076 | iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder, |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 1077 | android.R.attr.textColorPrimaryInverse); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1080 | if (summary != null) { |
| 1081 | if (docSummary != null) { |
| 1082 | summary.setText(docSummary); |
| 1083 | summary.setVisibility(View.VISIBLE); |
| 1084 | hasLine2 = true; |
| 1085 | } else { |
| 1086 | summary.setVisibility(View.INVISIBLE); |
| 1087 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1088 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1091 | if (icon1 != null) icon1.setVisibility(View.GONE); |
| 1092 | if (icon2 != null) icon2.setVisibility(View.GONE); |
| 1093 | |
| 1094 | if (iconDrawable != null) { |
| 1095 | if (hasLine1) { |
| 1096 | icon1.setVisibility(View.VISIBLE); |
| 1097 | icon1.setImageDrawable(iconDrawable); |
| 1098 | } else { |
| 1099 | icon2.setVisibility(View.VISIBLE); |
| 1100 | icon2.setImageDrawable(iconDrawable); |
| 1101 | } |
| 1102 | } |
| 1103 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1104 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1105 | date.setText(null); |
| 1106 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1107 | date.setText(formatTime(mContext, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1108 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1109 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1110 | |
| 1111 | if (state.showSize) { |
| 1112 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1113 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1114 | size.setText(null); |
| 1115 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1116 | size.setText(Formatter.formatFileSize(mContext, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1117 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } else { |
| 1120 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1121 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1122 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1123 | if (line1 != null) { |
| 1124 | line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE); |
| 1125 | } |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1126 | if (line2 != null) { |
| 1127 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 1128 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1129 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1130 | setEnabledRecursive(itemView, enabled); |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1131 | |
| 1132 | iconMime.setAlpha(iconAlpha); |
| 1133 | iconThumb.setAlpha(iconAlpha); |
| 1134 | if (icon1 != null) icon1.setAlpha(iconAlpha); |
| 1135 | if (icon2 != null) icon2.setAlpha(iconAlpha); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1136 | |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 1137 | if (DEBUG_ENABLE_DND) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1138 | setupDragAndDropOnDocumentView(itemView, cursor); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 1139 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1140 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1141 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1142 | private Cursor getItem(int position) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1143 | if (position < mCursorCount) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1144 | mCursor.moveToPosition(position); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1145 | return mCursor; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1146 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1147 | |
| 1148 | Log.w(TAG, "Returning null cursor for position: " + position); |
| 1149 | if (DEBUG) Log.d(TAG, "...Adapter size: " + mCursorCount); |
| 1150 | if (DEBUG) Log.d(TAG, "...Footer size: " + mFooters.size()); |
| 1151 | return null; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1152 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1153 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1154 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1155 | public int getItemCount() { |
| 1156 | return mCursorCount; |
| 1157 | // return mCursorCount + mFooters.size(); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | @Override |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1161 | public int getItemViewType(int position) { |
| 1162 | if (position < mCursorCount) { |
| 1163 | return 0; |
| 1164 | } else { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1165 | position -= mCursorCount; |
| 1166 | return mFooters.get(position).getItemViewType(); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1169 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1170 | private boolean isEmpty() { |
| 1171 | return getItemCount() > 0; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | private static String formatTime(Context context, long when) { |
| 1176 | // TODO: DateUtils should make this easier |
| 1177 | Time then = new Time(); |
| 1178 | then.set(when); |
| 1179 | Time now = new Time(); |
| 1180 | now.setToNow(); |
| 1181 | |
| 1182 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 1183 | | DateUtils.FORMAT_ABBREV_ALL; |
| 1184 | |
| 1185 | if (then.year != now.year) { |
| 1186 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 1187 | } else if (then.yearDay != now.yearDay) { |
| 1188 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 1189 | } else { |
| 1190 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1191 | } |
| 1192 | |
| 1193 | return DateUtils.formatDateTime(context, when, flags); |
| 1194 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1195 | |
| 1196 | private String findCommonMimeType(List<String> mimeTypes) { |
| 1197 | String[] commonType = mimeTypes.get(0).split("/"); |
| 1198 | if (commonType.length != 2) { |
| 1199 | return "*/*"; |
| 1200 | } |
| 1201 | |
| 1202 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 1203 | String[] type = mimeTypes.get(i).split("/"); |
| 1204 | if (type.length != 2) continue; |
| 1205 | |
| 1206 | if (!commonType[1].equals(type[1])) { |
| 1207 | commonType[1] = "*"; |
| 1208 | } |
| 1209 | |
| 1210 | if (!commonType[0].equals(type[0])) { |
| 1211 | commonType[0] = "*"; |
| 1212 | commonType[1] = "*"; |
| 1213 | break; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | return commonType[0] + "/" + commonType[1]; |
| 1218 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1219 | |
| 1220 | private void setEnabledRecursive(View v, boolean enabled) { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1221 | if (v == null) return; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1222 | if (v.isEnabled() == enabled) return; |
| 1223 | v.setEnabled(enabled); |
| 1224 | |
| 1225 | if (v instanceof ViewGroup) { |
| 1226 | final ViewGroup vg = (ViewGroup) v; |
| 1227 | for (int i = vg.getChildCount() - 1; i >= 0; i--) { |
| 1228 | setEnabledRecursive(vg.getChildAt(i), enabled); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1232 | |
| 1233 | private boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 1234 | final State state = getDisplayState(DirectoryFragment.this); |
| 1235 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1236 | // Directories are always enabled |
| 1237 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1238 | return true; |
| 1239 | } |
| 1240 | |
Jeff Sharkey | 783ebc2 | 2013-09-26 19:42:52 -0700 | [diff] [blame] | 1241 | // Read-only files are disabled when creating |
| 1242 | if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1246 | return MimePredicate.mimeMatches(state.acceptMimes, docMimeType); |
| 1247 | } |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1248 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1249 | private @NonNull List<DocumentInfo> getSelectedDocuments() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1250 | Selection sel = mSelectionManager.getSelection(new Selection()); |
| 1251 | return getItemsAsDocuments(sel); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1254 | private List<DocumentInfo> getItemsAsDocuments(Selection items) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1255 | if (items == null || items.size() == 0) { |
| 1256 | return new ArrayList<>(0); |
| 1257 | } |
| 1258 | |
| 1259 | final List<DocumentInfo> docs = new ArrayList<>(items.size()); |
| 1260 | final int size = items.size(); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1261 | for (int i = 0; i < size; i++) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1262 | final Cursor cursor = mAdapter.getItem(items.get(i)); |
| 1263 | checkNotNull(cursor, "Cursor cannot be null."); |
| 1264 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 1265 | docs.add(doc); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1266 | } |
| 1267 | return docs; |
| 1268 | } |
| 1269 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1270 | private void copyFromClipboard() { |
| 1271 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
| 1272 | |
| 1273 | @Override |
| 1274 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1275 | return mClipper.getClippedDocuments(); |
| 1276 | } |
| 1277 | |
| 1278 | @Override |
| 1279 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1280 | DocumentInfo destination = |
| 1281 | ((BaseActivity) getActivity()).getCurrentDirectory(); |
| 1282 | copyDocuments(docs, destination); |
| 1283 | } |
| 1284 | }.execute(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1287 | private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1288 | checkNotNull(clipData); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1289 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1290 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1291 | @Override |
| 1292 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1293 | return mClipper.getDocumentsFromClipData(clipData); |
| 1294 | } |
| 1295 | |
| 1296 | @Override |
| 1297 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1298 | copyDocuments(docs, destination); |
| 1299 | } |
| 1300 | }.execute(); |
| 1301 | } |
| 1302 | |
| 1303 | private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) { |
| 1304 | if (!canCopy(docs, destination)) { |
| 1305 | Toast.makeText( |
| 1306 | getActivity(), |
| 1307 | R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1308 | return; |
| 1309 | } |
| 1310 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1311 | if (docs.isEmpty()) { |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1312 | return; |
Vladislav Kaznacheev | e16887a | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1313 | } |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1314 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1315 | final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack; |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1316 | DocumentStack tmpStack = new DocumentStack(); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1317 | if (destination != null) { |
| 1318 | tmpStack.push(destination); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1319 | tmpStack.addAll(curStack); |
| 1320 | } else { |
| 1321 | tmpStack = curStack; |
| 1322 | } |
| 1323 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1324 | CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) { |
| 1328 | Context context = getActivity(); |
| 1329 | final ContentResolver resolver = context.getContentResolver(); |
| 1330 | ClipData clipData = null; |
| 1331 | for (DocumentInfo doc : docs) { |
| 1332 | final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId); |
| 1333 | if (clipData == null) { |
Vladislav Kaznacheev | e16887a | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1334 | // TODO: figure out what this string should be. |
| 1335 | // Currently it is not displayed anywhere in the UI, but this might change. |
| 1336 | final String label = ""; |
| 1337 | clipData = ClipData.newUri(resolver, label, uri); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1338 | } else { |
| 1339 | // TODO: update list of mime types in ClipData. |
| 1340 | clipData.addItem(new ClipData.Item(uri)); |
| 1341 | } |
| 1342 | } |
| 1343 | return clipData; |
| 1344 | } |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1345 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1346 | void copySelectedToClipboard() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1347 | Selection sel = mSelectionManager.getSelection(new Selection()); |
| 1348 | copySelectionToClipboard(sel); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1349 | } |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1350 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1351 | void copySelectionToClipboard(Selection items) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1352 | new GetDocumentsTask() { |
| 1353 | @Override |
| 1354 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 1355 | mClipper.clipDocuments(docs); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1356 | Activity activity = getActivity(); |
| 1357 | Toast.makeText(activity, |
| 1358 | activity.getResources().getQuantityString( |
| 1359 | R.plurals.clipboard_files_clipped, docs.size(), docs.size()), |
| 1360 | Toast.LENGTH_SHORT).show(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1361 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1362 | }.execute(items); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | void pasteFromClipboard() { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1366 | copyFromClipboard(); |
| 1367 | getActivity().invalidateOptionsMenu(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1370 | /** |
| 1371 | * Returns true if the list of files can be copied to destination. Note that this |
| 1372 | * is a policy check only. Currently the method does not attempt to verify |
| 1373 | * available space or any other environmental aspects possibly resulting in |
| 1374 | * failure to copy. |
| 1375 | * |
| 1376 | * @return true if the list of files can be copied to destination. |
| 1377 | */ |
| 1378 | boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) { |
| 1379 | BaseActivity activity = (BaseActivity)getActivity(); |
| 1380 | |
| 1381 | final RootInfo root = activity.getCurrentRoot(); |
| 1382 | |
| 1383 | // Can't copy folders to Downloads. |
| 1384 | if (root.isDownloads()) { |
| 1385 | for (DocumentInfo docs : files) { |
| 1386 | if (docs.isDirectory()) { |
| 1387 | return false; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return dest != null && dest.isDirectory() && dest.isCreateSupported(); |
| 1393 | } |
| 1394 | |
| 1395 | void selectAllFiles() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1396 | mSelectionManager.selectItems(0, mAdapter.getItemCount()); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1397 | updateDisplayState(); |
| 1398 | } |
| 1399 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1400 | private void setupDragAndDropOnDirectoryView(View view) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1401 | // Listen for drops on non-directory items and empty space. |
| 1402 | view.setOnDragListener(mOnDragListener); |
| 1403 | } |
| 1404 | |
| 1405 | private void setupDragAndDropOnDocumentView(View view, Cursor cursor) { |
| 1406 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1407 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1408 | // Make a directory item a drop target. Drop on non-directories and empty space |
| 1409 | // is handled at the list/grid view level. |
| 1410 | view.setOnDragListener(mOnDragListener); |
| 1411 | } |
| 1412 | |
| 1413 | // Temporary: attaching the listener to the title only. |
| 1414 | // Attaching to the entire item conflicts with the item long click handler responsible |
| 1415 | // for item selection. |
| 1416 | final View title = view.findViewById(android.R.id.title); |
| 1417 | title.setOnLongClickListener(mLongClickListener); |
| 1418 | } |
| 1419 | |
| 1420 | private View.OnDragListener mOnDragListener = new View.OnDragListener() { |
| 1421 | @Override |
| 1422 | public boolean onDrag(View v, DragEvent event) { |
| 1423 | switch (event.getAction()) { |
| 1424 | case DragEvent.ACTION_DRAG_STARTED: |
| 1425 | // TODO: Check if the event contains droppable data. |
| 1426 | return true; |
| 1427 | |
| 1428 | // TODO: Highlight potential drop target directory? |
| 1429 | // TODO: Expand drop target directory on hover? |
| 1430 | case DragEvent.ACTION_DRAG_ENTERED: |
| 1431 | case DragEvent.ACTION_DRAG_LOCATION: |
| 1432 | case DragEvent.ACTION_DRAG_EXITED: |
| 1433 | case DragEvent.ACTION_DRAG_ENDED: |
| 1434 | return true; |
| 1435 | |
| 1436 | case DragEvent.ACTION_DROP: |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1437 | int dstPosition = mRecView.getChildAdapterPosition(v); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1438 | DocumentInfo dstDir = null; |
| 1439 | if (dstPosition != android.widget.AdapterView.INVALID_POSITION) { |
| 1440 | Cursor dstCursor = mAdapter.getItem(dstPosition); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1441 | checkNotNull(dstCursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1442 | dstDir = DocumentInfo.fromDirectoryCursor(dstCursor); |
| 1443 | // TODO: Do not drop into the directory where the documents came from. |
| 1444 | } |
| 1445 | copyFromClipData(event.getClipData(), dstDir); |
| 1446 | return true; |
| 1447 | } |
| 1448 | return false; |
| 1449 | } |
| 1450 | }; |
| 1451 | |
| 1452 | private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { |
| 1453 | @Override |
| 1454 | public boolean onLongClick(View v) { |
| 1455 | final List<DocumentInfo> docs = getDraggableDocuments(v); |
| 1456 | if (docs.isEmpty()) { |
| 1457 | return false; |
| 1458 | } |
| 1459 | v.startDrag( |
| 1460 | getClipDataFromDocuments(docs), |
| 1461 | new DrawableShadowBuilder(getDragShadowIcon(docs)), |
| 1462 | null, |
| 1463 | View.DRAG_FLAG_GLOBAL |
| 1464 | ); |
| 1465 | return true; |
| 1466 | } |
| 1467 | }; |
| 1468 | |
| 1469 | private List<DocumentInfo> getDraggableDocuments(View currentItemView) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1470 | int position = mRecView.getChildAdapterPosition(currentItemView); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1471 | if (position == android.widget.AdapterView.INVALID_POSITION) { |
| 1472 | return Collections.EMPTY_LIST; |
| 1473 | } |
| 1474 | |
| 1475 | final List<DocumentInfo> selectedDocs = getSelectedDocuments(); |
| 1476 | if (!selectedDocs.isEmpty()) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1477 | if (!mSelectionManager.getSelection().contains(position)) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1478 | // There is a selection that does not include the current item, drag nothing. |
| 1479 | return Collections.EMPTY_LIST; |
| 1480 | } |
| 1481 | return selectedDocs; |
| 1482 | } |
| 1483 | |
| 1484 | final Cursor cursor = mAdapter.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1485 | checkNotNull(cursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1486 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 1487 | return Lists.newArrayList(doc); |
| 1488 | } |
| 1489 | |
| 1490 | private Drawable getDragShadowIcon(List<DocumentInfo> docs) { |
| 1491 | if (docs.size() == 1) { |
| 1492 | final DocumentInfo doc = docs.get(0); |
| 1493 | return getDocumentIcon(getActivity(), doc.authority, doc.documentId, |
| 1494 | doc.mimeType, doc.icon, getDisplayState(this)); |
| 1495 | } |
| 1496 | return getActivity().getDrawable(R.drawable.ic_doc_generic); |
| 1497 | } |
| 1498 | |
| 1499 | public static Drawable getDocumentIcon(Context context, String docAuthority, String docId, |
| 1500 | String docMimeType, int docIcon, State state) { |
| 1501 | if (docIcon != 0) { |
| 1502 | return IconUtils.loadPackageIcon(context, docAuthority, docIcon); |
| 1503 | } else { |
| 1504 | return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId, |
| 1505 | state.derivedMode); |
| 1506 | } |
| 1507 | } |
| 1508 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1509 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> |
| 1510 | implements Preemptable { |
| 1511 | private final Uri mUri; |
| 1512 | private final ImageView mIconMime; |
| 1513 | private final ImageView mIconThumb; |
| 1514 | private final Point mThumbSize; |
| 1515 | private final float mTargetAlpha; |
| 1516 | private final CancellationSignal mSignal; |
| 1517 | |
| 1518 | public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize, |
| 1519 | float targetAlpha) { |
| 1520 | mUri = uri; |
| 1521 | mIconMime = iconMime; |
| 1522 | mIconThumb = iconThumb; |
| 1523 | mThumbSize = thumbSize; |
| 1524 | mTargetAlpha = targetAlpha; |
| 1525 | mSignal = new CancellationSignal(); |
| 1526 | } |
| 1527 | |
| 1528 | @Override |
| 1529 | public void preempt() { |
| 1530 | cancel(false); |
| 1531 | mSignal.cancel(); |
| 1532 | } |
| 1533 | |
| 1534 | @Override |
| 1535 | protected Bitmap doInBackground(Uri... params) { |
| 1536 | if (isCancelled()) return null; |
| 1537 | |
| 1538 | final Context context = mIconThumb.getContext(); |
| 1539 | final ContentResolver resolver = context.getContentResolver(); |
| 1540 | |
| 1541 | ContentProviderClient client = null; |
| 1542 | Bitmap result = null; |
| 1543 | try { |
| 1544 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1545 | resolver, mUri.getAuthority()); |
| 1546 | result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal); |
| 1547 | if (result != null) { |
| 1548 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1549 | context, mThumbSize); |
| 1550 | thumbs.put(mUri, result); |
| 1551 | } |
| 1552 | } catch (Exception e) { |
| 1553 | if (!(e instanceof OperationCanceledException)) { |
| 1554 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 1555 | } |
| 1556 | } finally { |
| 1557 | ContentProviderClient.releaseQuietly(client); |
| 1558 | } |
| 1559 | return result; |
| 1560 | } |
| 1561 | |
| 1562 | @Override |
| 1563 | protected void onPostExecute(Bitmap result) { |
| 1564 | if (mIconThumb.getTag() == this && result != null) { |
| 1565 | mIconThumb.setTag(null); |
| 1566 | mIconThumb.setImageBitmap(result); |
| 1567 | |
| 1568 | mIconMime.setAlpha(mTargetAlpha); |
| 1569 | mIconMime.animate().alpha(0f).start(); |
| 1570 | mIconThumb.setAlpha(0f); |
| 1571 | mIconThumb.animate().alpha(mTargetAlpha).start(); |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1576 | private class DrawableShadowBuilder extends View.DragShadowBuilder { |
| 1577 | |
| 1578 | private final Drawable mShadow; |
| 1579 | |
| 1580 | private final int mShadowDimension; |
| 1581 | |
| 1582 | public DrawableShadowBuilder(Drawable shadow) { |
| 1583 | mShadow = shadow; |
| 1584 | mShadowDimension = getResources().getDimensionPixelSize( |
| 1585 | R.dimen.drag_shadow_size); |
| 1586 | mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension); |
| 1587 | } |
| 1588 | |
| 1589 | public void onProvideShadowMetrics( |
| 1590 | Point shadowSize, Point shadowTouchPoint) { |
| 1591 | shadowSize.set(mShadowDimension, mShadowDimension); |
| 1592 | shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2); |
| 1593 | } |
| 1594 | |
| 1595 | public void onDrawShadow(Canvas canvas) { |
| 1596 | mShadow.draw(canvas); |
| 1597 | } |
| 1598 | } |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1599 | |
| 1600 | private FragmentTuner pickFragmentTuner(final State state) { |
| 1601 | return state.action == ACTION_BROWSE_ALL |
| 1602 | ? new StandaloneTuner() |
| 1603 | : new DefaultTuner(state); |
| 1604 | } |
| 1605 | |
| 1606 | /** |
| 1607 | * Interface for specializing the Fragment for the "host" Activity. |
| 1608 | * Feel free to expand the role of this class to handle other specializations. |
| 1609 | */ |
| 1610 | private interface FragmentTuner { |
| 1611 | void updateActionMenu(Menu menu, int dirType); |
| 1612 | } |
| 1613 | |
| 1614 | /** |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1615 | * Abstract task providing support for loading documents *off* |
| 1616 | * the main thread. And if it isn't obvious, creating a list |
| 1617 | * of documents (especially large lists) can be pretty expensive. |
| 1618 | */ |
| 1619 | private abstract class GetDocumentsTask |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1620 | extends AsyncTask<Selection, Void, List<DocumentInfo>> { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1621 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame^] | 1622 | protected final List<DocumentInfo> doInBackground(Selection... selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1623 | return getItemsAsDocuments(selected[0]); |
| 1624 | } |
| 1625 | |
| 1626 | @Override |
| 1627 | protected final void onPostExecute(List<DocumentInfo> docs) { |
| 1628 | onDocumentsReady(docs); |
| 1629 | } |
| 1630 | |
| 1631 | abstract void onDocumentsReady(List<DocumentInfo> docs); |
| 1632 | } |
| 1633 | |
| 1634 | /** |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1635 | * Provides support for Platform specific specializations of DirectoryFragment. |
| 1636 | */ |
| 1637 | private static final class DefaultTuner implements FragmentTuner { |
| 1638 | |
| 1639 | private final State mState; |
| 1640 | |
| 1641 | public DefaultTuner(State state) { |
| 1642 | mState = state; |
| 1643 | } |
| 1644 | |
| 1645 | @Override |
| 1646 | public void updateActionMenu(Menu menu, int dirType) { |
| 1647 | Preconditions.checkState(mState.action != ACTION_BROWSE_ALL); |
| 1648 | |
| 1649 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 1650 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 1651 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 1652 | final MenuItem copyTo = menu.findItem(R.id.menu_copy_to); |
| 1653 | final MenuItem moveTo = menu.findItem(R.id.menu_move_to); |
| 1654 | final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard); |
| 1655 | |
| 1656 | final boolean manageOrBrowse = (mState.action == ACTION_MANAGE |
| 1657 | || mState.action == ACTION_BROWSE); |
| 1658 | |
| 1659 | open.setVisible(!manageOrBrowse); |
| 1660 | share.setVisible(manageOrBrowse); |
| 1661 | delete.setVisible(manageOrBrowse); |
| 1662 | // Disable copying from the Recents view. |
| 1663 | copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN); |
| 1664 | moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false)); |
| 1665 | |
| 1666 | // Only shown in standalone mode. |
| 1667 | copyToClipboard.setVisible(false); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * Provides support for Standalone specific specializations of DirectoryFragment. |
| 1673 | */ |
| 1674 | private static final class StandaloneTuner implements FragmentTuner { |
| 1675 | @Override |
| 1676 | public void updateActionMenu(Menu menu, int dirType) { |
| 1677 | menu.findItem(R.id.menu_share).setVisible(true); |
| 1678 | menu.findItem(R.id.menu_delete).setVisible(true); |
| 1679 | menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true); |
| 1680 | |
| 1681 | menu.findItem(R.id.menu_open).setVisible(false); |
| 1682 | menu.findItem(R.id.menu_copy_to).setVisible(false); |
| 1683 | menu.findItem(R.id.menu_move_to).setVisible(false); |
| 1684 | } |
| 1685 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1686 | } |