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 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 20 | import static com.android.documentsui.Shared.TAG; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.State.ACTION_BROWSE; |
| 22 | import static com.android.documentsui.State.ACTION_CREATE; |
| 23 | import static com.android.documentsui.State.ACTION_MANAGE; |
| 24 | import static com.android.documentsui.State.MODE_GRID; |
| 25 | import static com.android.documentsui.State.MODE_LIST; |
| 26 | import static com.android.documentsui.State.MODE_UNKNOWN; |
| 27 | import static com.android.documentsui.State.SORT_ORDER_UNKNOWN; |
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 | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 32 | import static com.android.internal.util.Preconditions.checkState; |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 33 | |
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 | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 47 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 48 | import android.graphics.Bitmap; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 49 | import android.graphics.Canvas; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 50 | import android.graphics.Point; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 51 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 52 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 53 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 54 | import android.os.Bundle; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 55 | import android.os.CancellationSignal; |
Makoto Onuki | 7777875 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 56 | import android.os.Looper; |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 57 | import android.os.OperationCanceledException; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 58 | import android.os.Parcelable; |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 59 | import android.os.SystemProperties; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 60 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 61 | import android.provider.DocumentsContract.Document; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 62 | import android.support.annotation.Nullable; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 63 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 64 | import android.support.design.widget.Snackbar; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 65 | import android.support.v7.widget.GridLayoutManager; |
| 66 | import android.support.v7.widget.LinearLayoutManager; |
| 67 | import android.support.v7.widget.RecyclerView; |
| 68 | import android.support.v7.widget.RecyclerView.LayoutManager; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 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; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 77 | import android.util.SparseBooleanArray; |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 78 | import android.util.TypedValue; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 79 | import android.view.ActionMode; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 80 | import android.view.DragEvent; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 81 | import android.view.GestureDetector; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 82 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 83 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 84 | import android.view.MenuItem; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 85 | import android.view.MotionEvent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 86 | import android.view.View; |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 87 | import android.view.View.OnLayoutChangeListener; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 88 | import android.view.ViewGroup; |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 89 | import android.view.ViewParent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 90 | import android.widget.ImageView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 91 | import android.widget.TextView; |
| 92 | |
Steve McKay | 351a749 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 93 | import com.android.documentsui.BaseActivity.DocumentContext; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 94 | import com.android.documentsui.MultiSelectManager.Selection; |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 95 | import com.android.documentsui.ProviderExecutor.Preemptable; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 96 | import com.android.documentsui.RecentsProvider.StateColumns; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 97 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 98 | import com.android.documentsui.model.DocumentStack; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 99 | import com.android.documentsui.model.RootInfo; |
Steve McKay | fad3d4a | 2015-09-22 15:09:21 -0700 | [diff] [blame] | 100 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 101 | import com.google.common.collect.Lists; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 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; |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 124 | private static final boolean DEBUG_ENABLE_DND = false; |
| 125 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 126 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 127 | private static final String EXTRA_ROOT = "root"; |
| 128 | private static final String EXTRA_DOC = "doc"; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 129 | private static final String EXTRA_QUERY = "query"; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 130 | private static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 131 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 132 | private Model mModel; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 133 | private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener(); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -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 | // These are lazily initialized. |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 152 | private LinearLayoutManager mListLayout; |
| 153 | private GridLayoutManager mGridLayout; |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 154 | private int mColumnCount = 1; // This will get updated when layout changes. |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 155 | |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 156 | private MessageBar mMessageBar; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 157 | private View mProgressBar; |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 158 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 159 | public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
| 160 | show(fm, TYPE_NORMAL, root, doc, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 163 | public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) { |
| 164 | show(fm, TYPE_SEARCH, root, null, query, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 167 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
| 168 | show(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 171 | private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 172 | String query, int anim) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 173 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 174 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 175 | args.putParcelable(EXTRA_ROOT, root); |
| 176 | args.putParcelable(EXTRA_DOC, doc); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 177 | args.putString(EXTRA_QUERY, query); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 178 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 179 | final FragmentTransaction ft = fm.beginTransaction(); |
| 180 | switch (anim) { |
| 181 | case ANIM_SIDE: |
| 182 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 183 | break; |
| 184 | case ANIM_DOWN: |
| 185 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 186 | ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen); |
| 187 | break; |
| 188 | case ANIM_UP: |
| 189 | ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up); |
| 190 | break; |
| 191 | } |
| 192 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 193 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 194 | fragment.setArguments(args); |
| 195 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 196 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 197 | ft.commitAllowingStateLoss(); |
| 198 | } |
| 199 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 200 | private static String buildStateKey(RootInfo root, DocumentInfo doc) { |
| 201 | final StringBuilder builder = new StringBuilder(); |
| 202 | builder.append(root != null ? root.authority : "null").append(';'); |
| 203 | builder.append(root != null ? root.rootId : "null").append(';'); |
| 204 | builder.append(doc != null ? doc.documentId : "null"); |
| 205 | return builder.toString(); |
| 206 | } |
| 207 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 208 | public static DirectoryFragment get(FragmentManager fm) { |
| 209 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 210 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 213 | @Override |
| 214 | public View onCreateView( |
| 215 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 216 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 217 | |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 218 | mMessageBar = MessageBar.create(getChildFragmentManager()); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 219 | mProgressBar = view.findViewById(R.id.progressbar); |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 220 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 221 | mEmptyView = view.findViewById(android.R.id.empty); |
| 222 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 223 | mRecView = (RecyclerView) view.findViewById(R.id.recyclerView); |
| 224 | mRecView.setRecyclerListener( |
| 225 | new RecyclerListener() { |
| 226 | @Override |
| 227 | public void onViewRecycled(ViewHolder holder) { |
| 228 | cancelThumbnailTask(holder.itemView); |
| 229 | } |
| 230 | }); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 231 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 232 | // TODO: Rather than update columns on layout changes, push this |
| 233 | // code (or something like it) into GridLayoutManager. |
| 234 | mRecView.addOnLayoutChangeListener( |
| 235 | new OnLayoutChangeListener() { |
| 236 | |
| 237 | @Override |
| 238 | public void onLayoutChange( |
| 239 | View v, int left, int top, int right, int bottom, int oldLeft, |
| 240 | int oldTop, int oldRight, int oldBottom) { |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 241 | mColumnCount = calculateColumnCount(); |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 242 | if (mGridLayout != null) { |
| 243 | mGridLayout.setSpanCount(mColumnCount); |
| 244 | } |
| 245 | } |
| 246 | }); |
| 247 | |
| 248 | // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration). |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 249 | if (DEBUG_ENABLE_DND) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 250 | setupDragAndDropOnDirectoryView(mRecView); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 251 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 252 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 253 | return view; |
| 254 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 255 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 256 | @Override |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 257 | public void onDestroyView() { |
| 258 | super.onDestroyView(); |
| 259 | |
| 260 | // Cancel any outstanding thumbnail requests |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 261 | final int count = mRecView.getChildCount(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 262 | for (int i = 0; i < count; i++) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 263 | final View view = mRecView.getChildAt(i); |
| 264 | cancelThumbnailTask(view); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 265 | } |
Jeff Sharkey | faaeb39 | 2013-10-04 14:44:56 -0700 | [diff] [blame] | 266 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 267 | // Clear any outstanding selection |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 268 | mModel.clearSelection(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 272 | public void onActivityCreated(Bundle savedInstanceState) { |
| 273 | super.onActivityCreated(savedInstanceState); |
| 274 | |
| 275 | final Context context = getActivity(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 276 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 277 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 278 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 279 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 280 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 281 | mAdapter = new DocumentsAdapter(context); |
| 282 | mRecView.setAdapter(mAdapter); |
| 283 | |
| 284 | GestureDetector.SimpleOnGestureListener listener = |
| 285 | new GestureDetector.SimpleOnGestureListener() { |
| 286 | @Override |
| 287 | public boolean onSingleTapUp(MotionEvent e) { |
| 288 | return DirectoryFragment.this.onSingleTapUp(e); |
| 289 | } |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 290 | @Override |
| 291 | public boolean onDoubleTap(MotionEvent e) { |
| 292 | Log.d(TAG, "Handling double tap."); |
| 293 | return DirectoryFragment.this.onDoubleTap(e); |
| 294 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 295 | }; |
| 296 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 297 | // TODO: instead of inserting the view into the constructor, extract listener-creation code |
| 298 | // and set the listener on the view after the fact. Then the view doesn't need to be passed |
| 299 | // into the selection manager which is passed into the model. |
| 300 | MultiSelectManager selMgr= new MultiSelectManager( |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 301 | mRecView, |
| 302 | listener, |
| 303 | state.allowMultiple |
| 304 | ? MultiSelectManager.MODE_MULTIPLE |
| 305 | : MultiSelectManager.MODE_SINGLE); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 306 | selMgr.addCallback(new SelectionModeListener()); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 307 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 308 | mModel = new Model(context, selMgr, mAdapter); |
| 309 | mModel.addUpdateListener(mModelUpdateListener); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 310 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 311 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 312 | mStateKey = buildStateKey(root, doc); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 313 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 314 | mFragmentTuner = pickFragmentTuner(state); |
| 315 | mClipper = new DocumentClipper(context); |
| 316 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 317 | if (mType == TYPE_RECENT_OPEN) { |
| 318 | // Hide titles when showing recents for picking images/videos |
| 319 | mHideGridTitles = MimePredicate.mimeMatches( |
| 320 | MimePredicate.VISUAL_MIMES, state.acceptMimes); |
| 321 | } else { |
| 322 | mHideGridTitles = (doc != null) && doc.isGridTitlesHidden(); |
| 323 | } |
| 324 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 325 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 326 | Context.ACTIVITY_SERVICE); |
| 327 | mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN); |
| 328 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 329 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 330 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 331 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 332 | final String query = getArguments().getString(EXTRA_QUERY); |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 333 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 334 | Uri contentsUri; |
| 335 | switch (mType) { |
| 336 | case TYPE_NORMAL: |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 337 | contentsUri = DocumentsContract.buildChildDocumentsUri( |
| 338 | doc.authority, doc.documentId); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 339 | if (state.action == ACTION_MANAGE) { |
| 340 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 341 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 342 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 343 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 344 | case TYPE_SEARCH: |
| 345 | contentsUri = DocumentsContract.buildSearchDocumentsUri( |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 346 | root.authority, root.rootId, query); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 347 | if (state.action == ACTION_MANAGE) { |
| 348 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 349 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 350 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 351 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 352 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 353 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 354 | return new RecentLoader(context, roots, state); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 355 | default: |
| 356 | throw new IllegalStateException("Unknown type " + mType); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 357 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 361 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 362 | if (!isAdded()) return; |
| 363 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 364 | mModel.update(result); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 365 | |
| 366 | // Push latest state up to UI |
| 367 | // TODO: if mode change was racing with us, don't overwrite it |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 368 | if (result.mode != MODE_UNKNOWN) { |
| 369 | state.derivedMode = result.mode; |
| 370 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 371 | state.derivedSortOrder = result.sortOrder; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 372 | ((BaseActivity) context).onStateChanged(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 373 | |
| 374 | updateDisplayState(); |
| 375 | |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 376 | // When launched into empty recents, show drawer |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 377 | if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched && |
Steve McKay | b68dd22 | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 378 | context instanceof DocumentsActivity) { |
| 379 | ((DocumentsActivity) context).setRootsDrawerOpen(true); |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 382 | // Restore any previous instance state |
| 383 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 384 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 385 | getView().restoreHierarchyState(container); |
| 386 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 387 | mRecView.smoothScrollToPosition(0); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 388 | } |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 389 | |
| 390 | mLastSortOrder = state.derivedSortOrder; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 394 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 395 | mModel.update(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 396 | } |
| 397 | }; |
| 398 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 399 | // Kick off loader at least once |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 400 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 401 | |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 402 | mFragmentTuner.afterActivityCreated(this); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 403 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 406 | @Override |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 407 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 408 | // There's only one request code right now. Replace this with a switch statement or |
| 409 | // something more scalable when more codes are added. |
| 410 | if (requestCode != REQUEST_COPY_DESTINATION) { |
| 411 | return; |
| 412 | } |
| 413 | if (resultCode == Activity.RESULT_CANCELED || data == null) { |
| 414 | // User pressed the back button or otherwise cancelled the destination pick. Don't |
| 415 | // proceed with the copy. |
| 416 | return; |
| 417 | } |
| 418 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 419 | CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy, |
Steve McKay | 4d0255f | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 420 | (DocumentStack) data.getParcelableExtra(Shared.EXTRA_STACK), |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 421 | data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE)); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 424 | private int getEventAdapterPosition(MotionEvent e) { |
| 425 | View view = mRecView.findChildViewUnder(e.getX(), e.getY()); |
| 426 | return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION; |
| 427 | } |
| 428 | |
| 429 | private boolean onSingleTapUp(MotionEvent e) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 430 | if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) { |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 431 | int position = getEventAdapterPosition(e); |
| 432 | if (position != RecyclerView.NO_POSITION) { |
| 433 | return handleViewItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 436 | return false; |
| 437 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 438 | |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 439 | protected boolean onDoubleTap(MotionEvent e) { |
| 440 | if (Events.isMouseEvent(e)) { |
| 441 | Log.d(TAG, "Handling double tap from mouse."); |
| 442 | int position = getEventAdapterPosition(e); |
| 443 | if (position != RecyclerView.NO_POSITION) { |
| 444 | return handleViewItem(position); |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | private boolean handleViewItem(int position) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 451 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 452 | checkNotNull(cursor, "Cursor cannot be null."); |
| 453 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 454 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 455 | if (isDocumentEnabled(docMimeType, docFlags)) { |
| 456 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 457 | ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel); |
| 458 | mModel.clearSelection(); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 459 | return true; |
| 460 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 464 | @Override |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 465 | public void onStop() { |
| 466 | super.onStop(); |
| 467 | |
| 468 | // Remember last scroll location |
| 469 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 470 | getView().saveHierarchyState(container); |
| 471 | final State state = getDisplayState(this); |
| 472 | state.dirState.put(mStateKey, container); |
| 473 | } |
| 474 | |
| 475 | @Override |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 476 | public void onResume() { |
| 477 | super.onResume(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 478 | updateDisplayState(); |
| 479 | } |
| 480 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 481 | public void onDisplayStateChanged() { |
| 482 | updateDisplayState(); |
| 483 | } |
| 484 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 485 | public void onUserSortOrderChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 486 | // Sort order change always triggers reload; we'll trigger state change |
| 487 | // on the flip side. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 488 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | public void onUserModeChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 492 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 493 | final State state = getDisplayState(this); |
| 494 | |
| 495 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 496 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 497 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 498 | if (root != null && doc != null) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 499 | final Uri stateUri = RecentsProvider.buildState( |
| 500 | root.authority, root.rootId, doc.documentId); |
| 501 | final ContentValues values = new ContentValues(); |
| 502 | values.put(StateColumns.MODE, state.userMode); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 503 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 504 | new AsyncTask<Void, Void, Void>() { |
| 505 | @Override |
| 506 | protected Void doInBackground(Void... params) { |
| 507 | resolver.insert(stateUri, values); |
| 508 | return null; |
| 509 | } |
| 510 | }.execute(); |
| 511 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 512 | |
| 513 | // Mode change is just visual change; no need to kick loader, and |
| 514 | // deliver change event immediately. |
| 515 | state.derivedMode = state.userMode; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 516 | ((BaseActivity) getActivity()).onStateChanged(); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 517 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 518 | updateDisplayState(); |
| 519 | } |
| 520 | |
| 521 | private void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 522 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 523 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 524 | if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 525 | mLastMode = state.derivedMode; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 526 | mLastShowSize = state.showSize; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 527 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 528 | updateLayout(state.derivedMode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 529 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 530 | mRecView.setAdapter(mAdapter); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Returns a {@code LayoutManager} for {@code mode}, lazily initializing |
| 535 | * classes as needed. |
| 536 | */ |
| 537 | private void updateLayout(int mode) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 538 | final int thumbSize; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 539 | |
| 540 | final LayoutManager layout; |
| 541 | switch (mode) { |
| 542 | case MODE_GRID: |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 543 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Steve McKay | a9be718 | 2015-07-22 16:03:35 -0700 | [diff] [blame] | 544 | if (mGridLayout == null) { |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 545 | mGridLayout = new GridLayoutManager(getContext(), mColumnCount ); |
Steve McKay | a9be718 | 2015-07-22 16:03:35 -0700 | [diff] [blame] | 546 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 547 | layout = mGridLayout; |
| 548 | break; |
| 549 | case MODE_LIST: |
Steve McKay | a9be718 | 2015-07-22 16:03:35 -0700 | [diff] [blame] | 550 | thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 551 | if (mListLayout == null) { |
| 552 | mListLayout = new LinearLayoutManager(getContext()); |
| 553 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 554 | layout = mListLayout; |
| 555 | break; |
| 556 | case MODE_UNKNOWN: |
| 557 | default: |
| 558 | throw new IllegalArgumentException("Unsupported layout mode: " + mode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 559 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 560 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 561 | mRecView.setLayoutManager(layout); |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 562 | // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of |
| 563 | // imperatively calling this function. |
Steve McKay | 9058e04 | 2015-09-01 12:31:24 -0700 | [diff] [blame] | 564 | mModel.mSelectionManager.handleLayoutChanged(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 565 | // setting layout manager automatically invalidates existing ViewHolders. |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 566 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 569 | private int calculateColumnCount() { |
| 570 | int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width); |
| 571 | int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin); |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 572 | int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight(); |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 573 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 574 | checkState(mRecView.getWidth() > 0); |
| 575 | int columnCount = Math.max(1, |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 576 | (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin)); |
| 577 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 578 | return columnCount; |
| 579 | } |
| 580 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 581 | /** |
| 582 | * Manages the integration between our ActionMode and MultiSelectManager, initiating |
| 583 | * ActionMode when there is a selection, canceling it when there is no selection, |
| 584 | * and clearing selection when action mode is explicitly exited by the user. |
| 585 | */ |
| 586 | private final class SelectionModeListener |
| 587 | implements MultiSelectManager.Callback, ActionMode.Callback { |
| 588 | |
| 589 | private Selection mSelected = new Selection(); |
| 590 | private ActionMode mActionMode; |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 591 | private int mNoDeleteCount = 0; |
| 592 | private Menu mMenu; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 593 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 594 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 595 | public boolean onBeforeItemStateChange(int position, boolean selected) { |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 596 | // Directories cannot be checked |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 597 | if (selected) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 598 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 599 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 600 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 601 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 602 | return isDocumentEnabled(docMimeType, docFlags); |
| 603 | } |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void onItemStateChanged(int position, boolean selected) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 609 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 610 | checkNotNull(cursor, "Cursor cannot be null."); |
| 611 | |
| 612 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 613 | if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) { |
| 614 | mNoDeleteCount += selected ? 1 : -1; |
| 615 | } |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 616 | } |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 617 | |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 618 | @Override |
| 619 | public void onSelectionChanged() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 620 | mModel.getSelection(mSelected); |
Ben Kwa | fe18c1b | 2015-09-11 15:40:18 -0700 | [diff] [blame] | 621 | TypedValue color = new TypedValue(); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 622 | if (mSelected.size() > 0) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 623 | if (DEBUG) Log.d(TAG, "Maybe starting action mode."); |
| 624 | if (mActionMode == null) { |
| 625 | if (DEBUG) Log.d(TAG, "Yeah. Starting action mode."); |
| 626 | mActionMode = getActivity().startActionMode(this); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 627 | } |
Ben Kwa | fe18c1b | 2015-09-11 15:40:18 -0700 | [diff] [blame] | 628 | getActivity().getTheme().resolveAttribute( |
| 629 | R.attr.colorActionMode, color, true); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 630 | updateActionMenu(); |
| 631 | } else { |
| 632 | if (DEBUG) Log.d(TAG, "Finishing action mode."); |
| 633 | if (mActionMode != null) { |
| 634 | mActionMode.finish(); |
| 635 | } |
Ben Kwa | 0574b18 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 636 | getActivity().getTheme().resolveAttribute( |
| 637 | android.R.attr.colorPrimaryDark, color, true); |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 638 | } |
Ben Kwa | fe18c1b | 2015-09-11 15:40:18 -0700 | [diff] [blame] | 639 | getActivity().getWindow().setStatusBarColor(color.data); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 640 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 641 | if (mActionMode != null) { |
| 642 | mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size())); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Called when the user exits the action mode |
| 647 | @Override |
| 648 | public void onDestroyActionMode(ActionMode mode) { |
| 649 | if (DEBUG) Log.d(TAG, "Handling action mode destroyed."); |
| 650 | mActionMode = null; |
| 651 | // clear selection |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 652 | mModel.clearSelection(); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 653 | mSelected.clear(); |
| 654 | mNoDeleteCount = 0; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 657 | @Override |
| 658 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 659 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 660 | mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size())); |
| 661 | return mModel.getSelection().size() > 0; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 666 | mMenu = menu; |
| 667 | updateActionMenu(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 668 | return true; |
| 669 | } |
| 670 | |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 671 | private void updateActionMenu() { |
| 672 | checkNotNull(mMenu); |
| 673 | // Delegate update logic to our owning action, since specialized logic is desired. |
| 674 | mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0); |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 675 | Menus.disableHiddenItems(mMenu); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 678 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 679 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 680 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 681 | Selection selection = mModel.getSelection(new Selection()); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 682 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 683 | final int id = item.getItemId(); |
| 684 | if (id == R.id.menu_open) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 685 | openDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 686 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 687 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 688 | |
| 689 | } else if (id == R.id.menu_share) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 690 | shareDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 691 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 692 | return true; |
| 693 | |
| 694 | } else if (id == R.id.menu_delete) { |
Ben Kwa | 33ba334 | 2015-09-24 15:03:51 -0700 | [diff] [blame] | 695 | // Exit selection mode first, so we avoid deselecting deleted documents. |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 696 | mode.finish(); |
Ben Kwa | 33ba334 | 2015-09-24 15:03:51 -0700 | [diff] [blame] | 697 | deleteDocuments(selection); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 698 | return true; |
| 699 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 700 | } else if (id == R.id.menu_copy_to) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 701 | transferDocuments(selection, CopyService.TRANSFER_MODE_COPY); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 702 | mode.finish(); |
| 703 | return true; |
| 704 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 705 | } else if (id == R.id.menu_move_to) { |
Ben Kwa | 33ba334 | 2015-09-24 15:03:51 -0700 | [diff] [blame] | 706 | // Exit selection mode first, so we avoid deselecting deleted documents. |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 707 | mode.finish(); |
Ben Kwa | 33ba334 | 2015-09-24 15:03:51 -0700 | [diff] [blame] | 708 | transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 709 | return true; |
| 710 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 711 | } else if (id == R.id.menu_copy_to_clipboard) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 712 | copySelectionToClipboard(selection); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 713 | mode.finish(); |
| 714 | return true; |
| 715 | |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 716 | } else if (id == R.id.menu_select_all) { |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 717 | selectAllFiles(); |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 718 | return true; |
| 719 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 720 | } else { |
| 721 | return false; |
| 722 | } |
| 723 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 724 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 725 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 726 | private static void cancelThumbnailTask(View view) { |
| 727 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 728 | if (iconThumb != null) { |
| 729 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
| 730 | if (oldTask != null) { |
| 731 | oldTask.preempt(); |
| 732 | iconThumb.setTag(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 735 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 736 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 737 | private void openDocuments(final Selection selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 738 | new GetDocumentsTask() { |
| 739 | @Override |
| 740 | void onDocumentsReady(List<DocumentInfo> docs) { |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 741 | // TODO: Implement support in Files activity for opening multiple docs. |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 742 | BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 743 | } |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 744 | }.execute(selected); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 747 | private void shareDocuments(final Selection selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 748 | new GetDocumentsTask() { |
| 749 | @Override |
| 750 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 751 | Intent intent; |
| 752 | |
| 753 | // Filter out directories - those can't be shared. |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 754 | List<DocumentInfo> docsForSend = new ArrayList<>(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 755 | for (DocumentInfo doc: docs) { |
| 756 | if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) { |
| 757 | docsForSend.add(doc); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (docsForSend.size() == 1) { |
| 762 | final DocumentInfo doc = docsForSend.get(0); |
| 763 | |
| 764 | intent = new Intent(Intent.ACTION_SEND); |
| 765 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 766 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 767 | intent.setType(doc.mimeType); |
| 768 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
| 769 | |
| 770 | } else if (docsForSend.size() > 1) { |
| 771 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 772 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 773 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 774 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 775 | final ArrayList<String> mimeTypes = new ArrayList<>(); |
| 776 | final ArrayList<Uri> uris = new ArrayList<>(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 777 | for (DocumentInfo doc : docsForSend) { |
| 778 | mimeTypes.add(doc.mimeType); |
| 779 | uris.add(doc.derivedUri); |
| 780 | } |
| 781 | |
| 782 | intent.setType(findCommonMimeType(mimeTypes)); |
| 783 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 784 | |
| 785 | } else { |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
| 790 | startActivity(intent); |
| 791 | } |
| 792 | }.execute(selected); |
| 793 | } |
| 794 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 795 | private void deleteDocuments(final Selection selected) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 796 | Context context = getActivity(); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 797 | String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size()); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 798 | |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 799 | mModel.markForDeletion(selected); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 800 | |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 801 | final Activity activity = getActivity(); |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 802 | Snackbars.makeSnackbar(activity, message, Snackbar.LENGTH_LONG) |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 803 | .setAction( |
| 804 | R.string.undo, |
| 805 | new android.view.View.OnClickListener() { |
| 806 | @Override |
| 807 | public void onClick(View view) {} |
| 808 | }) |
| 809 | .setCallback( |
| 810 | new Snackbar.Callback() { |
| 811 | @Override |
| 812 | public void onDismissed(Snackbar snackbar, int event) { |
| 813 | if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { |
| 814 | mModel.undoDeletion(); |
| 815 | } else { |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 816 | mModel.finalizeDeletion( |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 817 | new Model.DeletionListener() { |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 818 | @Override |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 819 | public void onError() { |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 820 | Snackbars.makeSnackbar( |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 821 | activity, |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 822 | R.string.toast_failed_delete, |
| 823 | Snackbar.LENGTH_LONG) |
| 824 | .show(); |
| 825 | |
| 826 | } |
| 827 | }); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 828 | } |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 829 | } |
| 830 | }) |
| 831 | .show(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 834 | private void transferDocuments(final Selection selected, final int mode) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 835 | // Pop up a dialog to pick a destination. This is inadequate but works for now. |
| 836 | // TODO: Implement a picker that is to spec. |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 837 | final Intent intent = new Intent( |
Daichi Hirono | 22574ed | 2015-04-15 13:41:18 +0900 | [diff] [blame] | 838 | BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION, |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 839 | Uri.EMPTY, |
| 840 | getActivity(), |
| 841 | DocumentsActivity.class); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 842 | |
| 843 | new GetDocumentsTask() { |
| 844 | @Override |
| 845 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 846 | getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs; |
| 847 | |
| 848 | boolean directoryCopy = false; |
| 849 | for (DocumentInfo info : docs) { |
| 850 | if (Document.MIME_TYPE_DIR.equals(info.mimeType)) { |
| 851 | directoryCopy = true; |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy); |
| 856 | intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode); |
| 857 | startActivityForResult(intent, REQUEST_COPY_DESTINATION); |
Daichi Hirono | f2a822d | 2015-04-14 17:12:54 +0900 | [diff] [blame] | 858 | } |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 859 | }.execute(selected); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 862 | private static State getDisplayState(Fragment fragment) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 863 | return ((BaseActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 866 | // Provide a reference to the views for each data item |
| 867 | // Complex data items may need more than one view per item, and |
| 868 | // you provide access to all the views for a data item in a view holder |
| 869 | private static final class DocumentHolder extends RecyclerView.ViewHolder { |
| 870 | // each data item is just a string in this case |
| 871 | public View view; |
| 872 | public String docId; // The stable document id. |
| 873 | public DocumentHolder(View view) { |
| 874 | super(view); |
| 875 | this.view = view; |
| 876 | } |
| 877 | } |
| 878 | |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 879 | void showEmptyView() { |
| 880 | mEmptyView.setVisibility(View.VISIBLE); |
| 881 | mRecView.setVisibility(View.GONE); |
| 882 | TextView msg = (TextView) mEmptyView.findViewById(R.id.message); |
| 883 | msg.setText(R.string.empty); |
| 884 | // No retry button for the empty view. |
| 885 | mEmptyView.findViewById(R.id.button_retry).setVisibility(View.GONE); |
| 886 | } |
| 887 | |
| 888 | void showErrorView() { |
| 889 | mEmptyView.setVisibility(View.VISIBLE); |
| 890 | mRecView.setVisibility(View.GONE); |
| 891 | TextView msg = (TextView) mEmptyView.findViewById(R.id.message); |
| 892 | msg.setText(R.string.query_error); |
| 893 | // TODO: Enable this once the retry button does something. |
| 894 | mEmptyView.findViewById(R.id.button_retry).setVisibility(View.GONE); |
| 895 | } |
| 896 | |
| 897 | void showRecyclerView() { |
| 898 | mEmptyView.setVisibility(View.GONE); |
| 899 | mRecView.setVisibility(View.VISIBLE); |
| 900 | } |
| 901 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 902 | private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 903 | |
| 904 | private final Context mContext; |
| 905 | private final LayoutInflater mInflater; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 906 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 907 | public DocumentsAdapter(Context context) { |
| 908 | mContext = context; |
| 909 | mInflater = LayoutInflater.from(context); |
| 910 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 911 | |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 912 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 913 | public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 914 | final State state = getDisplayState(DirectoryFragment.this); |
| 915 | final LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 916 | switch (state.derivedMode) { |
| 917 | case MODE_GRID: |
| 918 | return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false)); |
| 919 | case MODE_LIST: |
| 920 | return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false)); |
| 921 | case MODE_UNKNOWN: |
| 922 | default: |
| 923 | throw new IllegalStateException("Unsupported layout mode."); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 927 | @Override |
| 928 | public void onBindViewHolder(DocumentHolder holder, int position) { |
| 929 | |
| 930 | final Context context = getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 931 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 932 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 933 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 934 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 935 | context, mThumbSize); |
| 936 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 937 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 938 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 939 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 940 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 941 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 942 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 943 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 944 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 945 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 946 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 947 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 948 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 949 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 950 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 951 | holder.docId = docId; |
| 952 | final View itemView = holder.view; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 953 | itemView.setActivated(mModel.isSelected(position)); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 954 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 955 | final View line1 = itemView.findViewById(R.id.line1); |
| 956 | final View line2 = itemView.findViewById(R.id.line2); |
| 957 | |
| 958 | final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime); |
| 959 | final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); |
| 960 | final TextView title = (TextView) itemView.findViewById(android.R.id.title); |
| 961 | final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1); |
| 962 | final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2); |
| 963 | final TextView summary = (TextView) itemView.findViewById(android.R.id.summary); |
| 964 | final TextView date = (TextView) itemView.findViewById(R.id.date); |
| 965 | final TextView size = (TextView) itemView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 966 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 967 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 968 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 969 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 970 | iconThumb.setTag(null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 973 | iconMime.animate().cancel(); |
| 974 | iconThumb.animate().cancel(); |
| 975 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 976 | final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 977 | final boolean allowThumbnail = (state.derivedMode == MODE_GRID) |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 978 | || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 979 | final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 980 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 981 | final boolean enabled = isDocumentEnabled(docMimeType, docFlags); |
| 982 | final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f; |
| 983 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 984 | boolean cacheHit = false; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 985 | if (showThumbnail) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 986 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 987 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 988 | if (cachedResult != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 989 | iconThumb.setImageBitmap(cachedResult); |
| 990 | cacheHit = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 991 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 992 | iconThumb.setImageDrawable(null); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 993 | // TODO: Hang this off DocumentHolder? |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 994 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask( |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 995 | uri, iconMime, iconThumb, mThumbSize, iconAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 996 | iconThumb.setTag(task); |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 997 | ProviderExecutor.forAuthority(docAuthority).execute(task); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 998 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | // Always throw MIME icon into place, even when a thumbnail is being |
| 1002 | // loaded in background. |
| 1003 | if (cacheHit) { |
| 1004 | iconMime.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1005 | iconMime.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1006 | iconThumb.setAlpha(1f); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1007 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1008 | iconMime.setAlpha(1f); |
| 1009 | iconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1010 | iconThumb.setImageDrawable(null); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1011 | iconMime.setImageDrawable( |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1012 | getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1015 | boolean hasLine1 = false; |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1016 | boolean hasLine2 = false; |
| 1017 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1018 | final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles; |
| 1019 | if (!hideTitle) { |
| 1020 | title.setText(docDisplayName); |
| 1021 | hasLine1 = true; |
| 1022 | } |
| 1023 | |
| 1024 | Drawable iconDrawable = null; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1025 | if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 1026 | // We've already had to enumerate roots before any results can |
| 1027 | // be shown, so this will never block. |
| 1028 | final RootInfo root = roots.getRootBlocking(docAuthority, docRootId); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1029 | if (state.derivedMode == MODE_GRID) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1030 | iconDrawable = root.loadGridIcon(mContext); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1031 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1032 | iconDrawable = root.loadIcon(mContext); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1033 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1034 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1035 | if (summary != null) { |
| 1036 | final boolean alwaysShowSummary = getResources() |
| 1037 | .getBoolean(R.bool.always_show_summary); |
| 1038 | if (alwaysShowSummary) { |
| 1039 | summary.setText(root.getDirectoryString()); |
| 1040 | summary.setVisibility(View.VISIBLE); |
| 1041 | hasLine2 = true; |
| 1042 | } else { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 1043 | if (iconDrawable != null && roots.isIconUniqueBlocking(root)) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1044 | // No summary needed if icon speaks for itself |
| 1045 | summary.setVisibility(View.INVISIBLE); |
| 1046 | } else { |
| 1047 | summary.setText(root.getDirectoryString()); |
| 1048 | summary.setVisibility(View.VISIBLE); |
| 1049 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 1050 | hasLine2 = true; |
| 1051 | } |
| 1052 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1053 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1054 | } else { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1055 | // Directories showing thumbnails in grid mode get a little icon |
| 1056 | // hint to remind user they're a directory. |
| 1057 | if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID |
| 1058 | && showThumbnail) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1059 | iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder, |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 1060 | android.R.attr.textColorPrimaryInverse); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1063 | if (summary != null) { |
| 1064 | if (docSummary != null) { |
| 1065 | summary.setText(docSummary); |
| 1066 | summary.setVisibility(View.VISIBLE); |
| 1067 | hasLine2 = true; |
| 1068 | } else { |
| 1069 | summary.setVisibility(View.INVISIBLE); |
| 1070 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1071 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1074 | if (icon1 != null) icon1.setVisibility(View.GONE); |
| 1075 | if (icon2 != null) icon2.setVisibility(View.GONE); |
| 1076 | |
| 1077 | if (iconDrawable != null) { |
| 1078 | if (hasLine1) { |
| 1079 | icon1.setVisibility(View.VISIBLE); |
| 1080 | icon1.setImageDrawable(iconDrawable); |
| 1081 | } else { |
| 1082 | icon2.setVisibility(View.VISIBLE); |
| 1083 | icon2.setImageDrawable(iconDrawable); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1087 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1088 | date.setText(null); |
| 1089 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1090 | date.setText(formatTime(mContext, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1091 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1092 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1093 | |
| 1094 | if (state.showSize) { |
| 1095 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1096 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1097 | size.setText(null); |
| 1098 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1099 | size.setText(Formatter.formatFileSize(mContext, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1100 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } else { |
| 1103 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1104 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1105 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1106 | if (line1 != null) { |
| 1107 | line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE); |
| 1108 | } |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1109 | if (line2 != null) { |
| 1110 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 1111 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1112 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1113 | setEnabledRecursive(itemView, enabled); |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1114 | |
| 1115 | iconMime.setAlpha(iconAlpha); |
| 1116 | iconThumb.setAlpha(iconAlpha); |
| 1117 | if (icon1 != null) icon1.setAlpha(iconAlpha); |
| 1118 | if (icon2 != null) icon2.setAlpha(iconAlpha); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1119 | |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 1120 | if (DEBUG_ENABLE_DND) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1121 | setupDragAndDropOnDocumentView(itemView, cursor); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 1122 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1123 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1124 | |
Steve McKay | 351a749 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 1125 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1126 | public int getItemCount() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1127 | return mModel.getItemCount(); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | private static String formatTime(Context context, long when) { |
| 1133 | // TODO: DateUtils should make this easier |
| 1134 | Time then = new Time(); |
| 1135 | then.set(when); |
| 1136 | Time now = new Time(); |
| 1137 | now.setToNow(); |
| 1138 | |
| 1139 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 1140 | | DateUtils.FORMAT_ABBREV_ALL; |
| 1141 | |
| 1142 | if (then.year != now.year) { |
| 1143 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 1144 | } else if (then.yearDay != now.yearDay) { |
| 1145 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 1146 | } else { |
| 1147 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1148 | } |
| 1149 | |
| 1150 | return DateUtils.formatDateTime(context, when, flags); |
| 1151 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1152 | |
| 1153 | private String findCommonMimeType(List<String> mimeTypes) { |
| 1154 | String[] commonType = mimeTypes.get(0).split("/"); |
| 1155 | if (commonType.length != 2) { |
| 1156 | return "*/*"; |
| 1157 | } |
| 1158 | |
| 1159 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 1160 | String[] type = mimeTypes.get(i).split("/"); |
| 1161 | if (type.length != 2) continue; |
| 1162 | |
| 1163 | if (!commonType[1].equals(type[1])) { |
| 1164 | commonType[1] = "*"; |
| 1165 | } |
| 1166 | |
| 1167 | if (!commonType[0].equals(type[0])) { |
| 1168 | commonType[0] = "*"; |
| 1169 | commonType[1] = "*"; |
| 1170 | break; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return commonType[0] + "/" + commonType[1]; |
| 1175 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1176 | |
| 1177 | private void setEnabledRecursive(View v, boolean enabled) { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1178 | if (v == null) return; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1179 | if (v.isEnabled() == enabled) return; |
| 1180 | v.setEnabled(enabled); |
| 1181 | |
| 1182 | if (v instanceof ViewGroup) { |
| 1183 | final ViewGroup vg = (ViewGroup) v; |
| 1184 | for (int i = vg.getChildCount() - 1; i >= 0; i--) { |
| 1185 | setEnabledRecursive(vg.getChildAt(i), enabled); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1189 | |
| 1190 | private boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 1191 | final State state = getDisplayState(DirectoryFragment.this); |
| 1192 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1193 | // Directories are always enabled |
| 1194 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
Jeff Sharkey | 783ebc2 | 2013-09-26 19:42:52 -0700 | [diff] [blame] | 1198 | // Read-only files are disabled when creating |
| 1199 | if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1203 | return MimePredicate.mimeMatches(state.acceptMimes, docMimeType); |
| 1204 | } |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1205 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1206 | private void copyFromClipboard() { |
| 1207 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
| 1208 | |
| 1209 | @Override |
| 1210 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1211 | return mClipper.getClippedDocuments(); |
| 1212 | } |
| 1213 | |
| 1214 | @Override |
| 1215 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1216 | DocumentInfo destination = |
| 1217 | ((BaseActivity) getActivity()).getCurrentDirectory(); |
| 1218 | copyDocuments(docs, destination); |
| 1219 | } |
| 1220 | }.execute(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1223 | private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1224 | checkNotNull(clipData); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1225 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1226 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1227 | @Override |
| 1228 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1229 | return mClipper.getDocumentsFromClipData(clipData); |
| 1230 | } |
| 1231 | |
| 1232 | @Override |
| 1233 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1234 | copyDocuments(docs, destination); |
| 1235 | } |
| 1236 | }.execute(); |
| 1237 | } |
| 1238 | |
| 1239 | private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) { |
| 1240 | if (!canCopy(docs, destination)) { |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 1241 | Snackbars.makeSnackbar( |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1242 | getActivity(), |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 1243 | R.string.clipboard_files_cannot_paste, |
| 1244 | Snackbar.LENGTH_SHORT) |
| 1245 | .show(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1246 | return; |
| 1247 | } |
| 1248 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1249 | if (docs.isEmpty()) { |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1250 | return; |
Vladislav Kaznacheev | e16887a | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1251 | } |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1252 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1253 | final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack; |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1254 | DocumentStack tmpStack = new DocumentStack(); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1255 | if (destination != null) { |
| 1256 | tmpStack.push(destination); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1257 | tmpStack.addAll(curStack); |
| 1258 | } else { |
| 1259 | tmpStack = curStack; |
| 1260 | } |
| 1261 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1262 | CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) { |
| 1266 | Context context = getActivity(); |
| 1267 | final ContentResolver resolver = context.getContentResolver(); |
| 1268 | ClipData clipData = null; |
| 1269 | for (DocumentInfo doc : docs) { |
| 1270 | final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId); |
| 1271 | if (clipData == null) { |
Vladislav Kaznacheev | e16887a | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1272 | // TODO: figure out what this string should be. |
| 1273 | // Currently it is not displayed anywhere in the UI, but this might change. |
| 1274 | final String label = ""; |
| 1275 | clipData = ClipData.newUri(resolver, label, uri); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1276 | } else { |
| 1277 | // TODO: update list of mime types in ClipData. |
| 1278 | clipData.addItem(new ClipData.Item(uri)); |
| 1279 | } |
| 1280 | } |
| 1281 | return clipData; |
| 1282 | } |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1283 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1284 | void copySelectedToClipboard() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1285 | Selection sel = mModel.getSelection(new Selection()); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1286 | copySelectionToClipboard(sel); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1287 | } |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1288 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1289 | void copySelectionToClipboard(Selection items) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1290 | new GetDocumentsTask() { |
| 1291 | @Override |
| 1292 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 1293 | mClipper.clipDocuments(docs); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1294 | Activity activity = getActivity(); |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 1295 | Snackbars.makeSnackbar(activity, |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1296 | activity.getResources().getQuantityString( |
| 1297 | R.plurals.clipboard_files_clipped, docs.size(), docs.size()), |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 1298 | Snackbar.LENGTH_SHORT).show(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1299 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1300 | }.execute(items); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void pasteFromClipboard() { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1304 | copyFromClipboard(); |
| 1305 | getActivity().invalidateOptionsMenu(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1308 | /** |
| 1309 | * Returns true if the list of files can be copied to destination. Note that this |
| 1310 | * is a policy check only. Currently the method does not attempt to verify |
| 1311 | * available space or any other environmental aspects possibly resulting in |
| 1312 | * failure to copy. |
| 1313 | * |
| 1314 | * @return true if the list of files can be copied to destination. |
| 1315 | */ |
| 1316 | boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1317 | BaseActivity activity = (BaseActivity) getActivity(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1318 | |
| 1319 | final RootInfo root = activity.getCurrentRoot(); |
| 1320 | |
| 1321 | // Can't copy folders to Downloads. |
| 1322 | if (root.isDownloads()) { |
| 1323 | for (DocumentInfo docs : files) { |
| 1324 | if (docs.isDirectory()) { |
| 1325 | return false; |
| 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | return dest != null && dest.isDirectory() && dest.isCreateSupported(); |
| 1331 | } |
| 1332 | |
| 1333 | void selectAllFiles() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1334 | boolean changed = mModel.selectAll(); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 1335 | if (changed) { |
| 1336 | updateDisplayState(); |
| 1337 | } |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1340 | private void setupDragAndDropOnDirectoryView(View view) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1341 | // Listen for drops on non-directory items and empty space. |
| 1342 | view.setOnDragListener(mOnDragListener); |
| 1343 | } |
| 1344 | |
| 1345 | private void setupDragAndDropOnDocumentView(View view, Cursor cursor) { |
| 1346 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1347 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1348 | // Make a directory item a drop target. Drop on non-directories and empty space |
| 1349 | // is handled at the list/grid view level. |
| 1350 | view.setOnDragListener(mOnDragListener); |
| 1351 | } |
| 1352 | |
| 1353 | // Temporary: attaching the listener to the title only. |
| 1354 | // Attaching to the entire item conflicts with the item long click handler responsible |
| 1355 | // for item selection. |
| 1356 | final View title = view.findViewById(android.R.id.title); |
| 1357 | title.setOnLongClickListener(mLongClickListener); |
| 1358 | } |
| 1359 | |
| 1360 | private View.OnDragListener mOnDragListener = new View.OnDragListener() { |
| 1361 | @Override |
| 1362 | public boolean onDrag(View v, DragEvent event) { |
| 1363 | switch (event.getAction()) { |
| 1364 | case DragEvent.ACTION_DRAG_STARTED: |
| 1365 | // TODO: Check if the event contains droppable data. |
| 1366 | return true; |
| 1367 | |
| 1368 | // TODO: Highlight potential drop target directory? |
| 1369 | // TODO: Expand drop target directory on hover? |
| 1370 | case DragEvent.ACTION_DRAG_ENTERED: |
| 1371 | case DragEvent.ACTION_DRAG_LOCATION: |
| 1372 | case DragEvent.ACTION_DRAG_EXITED: |
| 1373 | case DragEvent.ACTION_DRAG_ENDED: |
| 1374 | return true; |
| 1375 | |
| 1376 | case DragEvent.ACTION_DROP: |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1377 | int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v)); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1378 | DocumentInfo dstDir = null; |
| 1379 | if (dstPosition != android.widget.AdapterView.INVALID_POSITION) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1380 | Cursor dstCursor = mModel.getItem(dstPosition); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1381 | checkNotNull(dstCursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1382 | dstDir = DocumentInfo.fromDirectoryCursor(dstCursor); |
| 1383 | // TODO: Do not drop into the directory where the documents came from. |
| 1384 | } |
| 1385 | copyFromClipData(event.getClipData(), dstDir); |
| 1386 | return true; |
| 1387 | } |
| 1388 | return false; |
| 1389 | } |
| 1390 | }; |
| 1391 | |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1392 | private View getContainingItemView(View view) { |
| 1393 | while (true) { |
| 1394 | if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) { |
| 1395 | return view; |
| 1396 | } |
| 1397 | ViewParent parent = view.getParent(); |
| 1398 | if (parent == null || !(parent instanceof View)) { |
| 1399 | return null; |
| 1400 | } |
| 1401 | view = (View) parent; |
| 1402 | } |
| 1403 | } |
| 1404 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1405 | private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { |
| 1406 | @Override |
| 1407 | public boolean onLongClick(View v) { |
| 1408 | final List<DocumentInfo> docs = getDraggableDocuments(v); |
| 1409 | if (docs.isEmpty()) { |
| 1410 | return false; |
| 1411 | } |
| 1412 | v.startDrag( |
| 1413 | getClipDataFromDocuments(docs), |
| 1414 | new DrawableShadowBuilder(getDragShadowIcon(docs)), |
| 1415 | null, |
Vladislav Kaznacheev | e3ce8a9 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 1416 | View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ | |
| 1417 | View.DRAG_FLAG_GLOBAL_URI_WRITE |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1418 | ); |
| 1419 | return true; |
| 1420 | } |
| 1421 | }; |
| 1422 | |
| 1423 | private List<DocumentInfo> getDraggableDocuments(View currentItemView) { |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1424 | int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView)); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1425 | if (position == android.widget.AdapterView.INVALID_POSITION) { |
| 1426 | return Collections.EMPTY_LIST; |
| 1427 | } |
| 1428 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1429 | final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments(); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1430 | if (!selectedDocs.isEmpty()) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1431 | if (!mModel.isSelected(position)) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1432 | // There is a selection that does not include the current item, drag nothing. |
| 1433 | return Collections.EMPTY_LIST; |
| 1434 | } |
| 1435 | return selectedDocs; |
| 1436 | } |
| 1437 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1438 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1439 | checkNotNull(cursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1440 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 1441 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1442 | return Lists.newArrayList(doc); |
| 1443 | } |
| 1444 | |
| 1445 | private Drawable getDragShadowIcon(List<DocumentInfo> docs) { |
| 1446 | if (docs.size() == 1) { |
| 1447 | final DocumentInfo doc = docs.get(0); |
| 1448 | return getDocumentIcon(getActivity(), doc.authority, doc.documentId, |
| 1449 | doc.mimeType, doc.icon, getDisplayState(this)); |
| 1450 | } |
| 1451 | return getActivity().getDrawable(R.drawable.ic_doc_generic); |
| 1452 | } |
| 1453 | |
| 1454 | public static Drawable getDocumentIcon(Context context, String docAuthority, String docId, |
| 1455 | String docMimeType, int docIcon, State state) { |
| 1456 | if (docIcon != 0) { |
| 1457 | return IconUtils.loadPackageIcon(context, docAuthority, docIcon); |
| 1458 | } else { |
| 1459 | return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId, |
| 1460 | state.derivedMode); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1464 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> |
| 1465 | implements Preemptable { |
| 1466 | private final Uri mUri; |
| 1467 | private final ImageView mIconMime; |
| 1468 | private final ImageView mIconThumb; |
| 1469 | private final Point mThumbSize; |
| 1470 | private final float mTargetAlpha; |
| 1471 | private final CancellationSignal mSignal; |
| 1472 | |
| 1473 | public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize, |
| 1474 | float targetAlpha) { |
| 1475 | mUri = uri; |
| 1476 | mIconMime = iconMime; |
| 1477 | mIconThumb = iconThumb; |
| 1478 | mThumbSize = thumbSize; |
| 1479 | mTargetAlpha = targetAlpha; |
| 1480 | mSignal = new CancellationSignal(); |
| 1481 | } |
| 1482 | |
| 1483 | @Override |
| 1484 | public void preempt() { |
| 1485 | cancel(false); |
| 1486 | mSignal.cancel(); |
| 1487 | } |
| 1488 | |
| 1489 | @Override |
| 1490 | protected Bitmap doInBackground(Uri... params) { |
| 1491 | if (isCancelled()) return null; |
| 1492 | |
| 1493 | final Context context = mIconThumb.getContext(); |
| 1494 | final ContentResolver resolver = context.getContentResolver(); |
| 1495 | |
| 1496 | ContentProviderClient client = null; |
| 1497 | Bitmap result = null; |
| 1498 | try { |
| 1499 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1500 | resolver, mUri.getAuthority()); |
| 1501 | result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal); |
| 1502 | if (result != null) { |
| 1503 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1504 | context, mThumbSize); |
| 1505 | thumbs.put(mUri, result); |
| 1506 | } |
| 1507 | } catch (Exception e) { |
| 1508 | if (!(e instanceof OperationCanceledException)) { |
| 1509 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 1510 | } |
| 1511 | } finally { |
| 1512 | ContentProviderClient.releaseQuietly(client); |
| 1513 | } |
| 1514 | return result; |
| 1515 | } |
| 1516 | |
| 1517 | @Override |
| 1518 | protected void onPostExecute(Bitmap result) { |
| 1519 | if (mIconThumb.getTag() == this && result != null) { |
| 1520 | mIconThumb.setTag(null); |
| 1521 | mIconThumb.setImageBitmap(result); |
| 1522 | |
| 1523 | mIconMime.setAlpha(mTargetAlpha); |
| 1524 | mIconMime.animate().alpha(0f).start(); |
| 1525 | mIconThumb.setAlpha(0f); |
| 1526 | mIconThumb.animate().alpha(mTargetAlpha).start(); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1531 | private class DrawableShadowBuilder extends View.DragShadowBuilder { |
| 1532 | |
| 1533 | private final Drawable mShadow; |
| 1534 | |
| 1535 | private final int mShadowDimension; |
| 1536 | |
| 1537 | public DrawableShadowBuilder(Drawable shadow) { |
| 1538 | mShadow = shadow; |
| 1539 | mShadowDimension = getResources().getDimensionPixelSize( |
| 1540 | R.dimen.drag_shadow_size); |
| 1541 | mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension); |
| 1542 | } |
| 1543 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1544 | @Override |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1545 | public void onProvideShadowMetrics( |
| 1546 | Point shadowSize, Point shadowTouchPoint) { |
| 1547 | shadowSize.set(mShadowDimension, mShadowDimension); |
| 1548 | shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2); |
| 1549 | } |
| 1550 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1551 | @Override |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1552 | public void onDrawShadow(Canvas canvas) { |
| 1553 | mShadow.draw(canvas); |
| 1554 | } |
| 1555 | } |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1556 | |
| 1557 | private FragmentTuner pickFragmentTuner(final State state) { |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 1558 | return state.action == ACTION_BROWSE |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1559 | ? new FilesTuner() |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 1560 | : new DefaultTuner(state.action); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * Interface for specializing the Fragment for the "host" Activity. |
| 1565 | * Feel free to expand the role of this class to handle other specializations. |
| 1566 | */ |
| 1567 | private interface FragmentTuner { |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1568 | void updateActionMenu(Menu menu, int dirType, boolean canDelete); |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 1569 | void afterActivityCreated(DirectoryFragment fragment); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /** |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1573 | * Abstract task providing support for loading documents *off* |
| 1574 | * the main thread. And if it isn't obvious, creating a list |
| 1575 | * of documents (especially large lists) can be pretty expensive. |
| 1576 | */ |
| 1577 | private abstract class GetDocumentsTask |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1578 | extends AsyncTask<Selection, Void, List<DocumentInfo>> { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1579 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1580 | protected final List<DocumentInfo> doInBackground(Selection... selected) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1581 | return mModel.getDocuments(selected[0]); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | @Override |
| 1585 | protected final void onPostExecute(List<DocumentInfo> docs) { |
| 1586 | onDocumentsReady(docs); |
| 1587 | } |
| 1588 | |
| 1589 | abstract void onDocumentsReady(List<DocumentInfo> docs); |
| 1590 | } |
| 1591 | |
| 1592 | /** |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1593 | * Provides support for Platform specific specializations of DirectoryFragment. |
| 1594 | */ |
| 1595 | private static final class DefaultTuner implements FragmentTuner { |
| 1596 | |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 1597 | private final boolean mManaging; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1598 | |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 1599 | public DefaultTuner(int action) { |
| 1600 | mManaging = (action == ACTION_MANAGE); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | @Override |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1604 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete) { |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 1605 | boolean copyEnabled = mManaging && dirType != TYPE_RECENT_OPEN; |
| 1606 | // TODO: The selection needs to be deletable. |
| 1607 | boolean moveEnabled = |
| 1608 | SystemProperties.getBoolean("debug.documentsui.enable_move", false); |
| 1609 | menu.findItem(R.id.menu_copy_to_clipboard).setEnabled(copyEnabled); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1610 | |
| 1611 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 1612 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 1613 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 1614 | final MenuItem copyTo = menu.findItem(R.id.menu_copy_to); |
| 1615 | final MenuItem moveTo = menu.findItem(R.id.menu_move_to); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1616 | |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 1617 | open.setVisible(!mManaging); |
| 1618 | share.setVisible(mManaging); |
| 1619 | delete.setVisible(mManaging && canDelete); |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 1620 | copyTo.setVisible(copyEnabled); |
| 1621 | copyTo.setEnabled(copyEnabled); |
| 1622 | moveTo.setVisible(moveEnabled); |
| 1623 | moveTo.setEnabled(moveEnabled); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1624 | } |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 1625 | |
| 1626 | @Override |
| 1627 | public void afterActivityCreated(DirectoryFragment fragment) {} |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | /** |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1631 | * Provides support for Files activity specific specializations of DirectoryFragment. |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1632 | */ |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1633 | private static final class FilesTuner implements FragmentTuner { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1634 | @Override |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1635 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete) { |
Steve McKay | 459bc2b | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 1636 | |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 1637 | menu.findItem(R.id.menu_copy_to_clipboard).setEnabled(dirType != TYPE_RECENT_OPEN); |
| 1638 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1639 | menu.findItem(R.id.menu_share).setVisible(true); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1640 | menu.findItem(R.id.menu_delete).setVisible(canDelete); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1641 | |
| 1642 | menu.findItem(R.id.menu_open).setVisible(false); |
Steve McKay | e980927 | 2015-10-01 11:39:24 -0700 | [diff] [blame^] | 1643 | menu.findItem(R.id.menu_copy_to).setVisible(true); |
| 1644 | menu.findItem(R.id.menu_move_to).setVisible(true); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1645 | } |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 1646 | |
| 1647 | @Override |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1648 | public void afterActivityCreated(DirectoryFragment fragment) {} |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1649 | } |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1650 | |
| 1651 | /** |
| 1652 | * The data model for the current loaded directory. |
| 1653 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1654 | @VisibleForTesting |
| 1655 | public static final class Model implements DocumentContext { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1656 | private MultiSelectManager mSelectionManager; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1657 | private RecyclerView.Adapter<?> mViewAdapter; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1658 | private Context mContext; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1659 | private int mCursorCount; |
| 1660 | private boolean mIsLoading; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1661 | private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray(); |
| 1662 | private UpdateListener mUpdateListener; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1663 | @Nullable private Cursor mCursor; |
| 1664 | @Nullable private String info; |
| 1665 | @Nullable private String error; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1666 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1667 | Model(Context context, MultiSelectManager selectionManager, |
| 1668 | RecyclerView.Adapter<?> viewAdapter) { |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1669 | mContext = context; |
| 1670 | mSelectionManager = selectionManager; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1671 | mViewAdapter = viewAdapter; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * Selects all files in the current directory. |
| 1676 | * @return true if the selection state changed for any files. |
| 1677 | */ |
| 1678 | boolean selectAll() { |
| 1679 | return mSelectionManager.setItemsSelected(0, mCursorCount, true); |
| 1680 | } |
| 1681 | |
| 1682 | /** |
| 1683 | * Clones the current selection into the given Selection object. |
| 1684 | * @param selection |
| 1685 | * @return The selection that was passed in, for convenience. |
| 1686 | */ |
| 1687 | Selection getSelection(Selection selection) { |
| 1688 | return mSelectionManager.getSelection(selection); |
| 1689 | } |
| 1690 | |
| 1691 | /** |
| 1692 | * @return The current selection (the live instance, not a copy). |
| 1693 | */ |
| 1694 | Selection getSelection() { |
| 1695 | return mSelectionManager.getSelection(); |
| 1696 | } |
| 1697 | |
| 1698 | boolean isSelected(int position) { |
| 1699 | return mSelectionManager.getSelection().contains(position); |
| 1700 | } |
| 1701 | |
| 1702 | void clearSelection() { |
| 1703 | mSelectionManager.clearSelection(); |
| 1704 | } |
| 1705 | |
| 1706 | void update(DirectoryResult result) { |
| 1707 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
| 1708 | |
| 1709 | if (result == null) { |
| 1710 | mCursor = null; |
| 1711 | mCursorCount = 0; |
| 1712 | info = null; |
| 1713 | error = null; |
| 1714 | mIsLoading = false; |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 1715 | mUpdateListener.onModelUpdate(this); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1716 | return; |
| 1717 | } |
| 1718 | |
| 1719 | if (result.exception != null) { |
| 1720 | Log.e(TAG, "Error while loading directory contents", result.exception); |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 1721 | mUpdateListener.onModelUpdateFailed(result.exception); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1722 | return; |
| 1723 | } |
| 1724 | |
| 1725 | mCursor = result.cursor; |
| 1726 | mCursorCount = mCursor.getCount(); |
| 1727 | |
| 1728 | final Bundle extras = mCursor.getExtras(); |
| 1729 | if (extras != null) { |
| 1730 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 1731 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 1732 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 1733 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1734 | |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 1735 | mUpdateListener.onModelUpdate(this); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1738 | int getItemCount() { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1739 | return mCursorCount - mMarkedForDeletion.size(); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1742 | Cursor getItem(int position) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1743 | // Items marked for deletion are masked out of the UI. To do this, for every marked |
| 1744 | // item whose position is less than the requested item position, advance the requested |
| 1745 | // position by 1. |
| 1746 | final int originalPos = position; |
| 1747 | final int size = mMarkedForDeletion.size(); |
Ben Kwa | 0d0daff | 2015-09-09 13:49:07 -0700 | [diff] [blame] | 1748 | for (int i = 0; i < size; ++i) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1749 | // It'd be more concise, but less efficient, to iterate over positions while calling |
| 1750 | // mMarkedForDeletion.get. Instead, iterate over deleted entries. |
| 1751 | if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) { |
| 1752 | ++position; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | if (DEBUG) { |
| 1757 | Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos |
| 1758 | + " Adjusted: " + position); |
| 1759 | } |
| 1760 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1761 | if (position >= mCursorCount) { |
| 1762 | throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " + |
| 1763 | mCursorCount + " items"); |
| 1764 | } |
| 1765 | |
| 1766 | mCursor.moveToPosition(position); |
| 1767 | return mCursor; |
| 1768 | } |
| 1769 | |
| 1770 | private boolean isEmpty() { |
| 1771 | return mCursorCount == 0; |
| 1772 | } |
| 1773 | |
| 1774 | private boolean isLoading() { |
| 1775 | return mIsLoading; |
| 1776 | } |
| 1777 | |
| 1778 | private List<DocumentInfo> getSelectedDocuments() { |
| 1779 | Selection sel = getSelection(new Selection()); |
| 1780 | return getDocuments(sel); |
| 1781 | } |
| 1782 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1783 | List<DocumentInfo> getDocuments(Selection items) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1784 | final int size = (items != null) ? items.size() : 0; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1785 | |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1786 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1787 | for (int i = 0; i < size; i++) { |
| 1788 | final Cursor cursor = getItem(items.get(i)); |
| 1789 | checkNotNull(cursor, "Cursor cannot be null."); |
| 1790 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 1791 | docs.add(doc); |
| 1792 | } |
| 1793 | return docs; |
| 1794 | } |
| 1795 | |
| 1796 | @Override |
| 1797 | public Cursor getCursor() { |
| 1798 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 1799 | throw new IllegalStateException("Can't call getCursor from non-main thread."); |
| 1800 | } |
| 1801 | return mCursor; |
| 1802 | } |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1803 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1804 | List<DocumentInfo> getDocumentsMarkedForDeletion() { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1805 | final int size = mMarkedForDeletion.size(); |
| 1806 | List<DocumentInfo> docs = new ArrayList<>(size); |
| 1807 | |
| 1808 | for (int i = 0; i < size; ++i) { |
| 1809 | final int position = mMarkedForDeletion.keyAt(i); |
| 1810 | checkState(position < mCursorCount); |
| 1811 | mCursor.moveToPosition(position); |
| 1812 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor); |
| 1813 | docs.add(doc); |
| 1814 | } |
| 1815 | return docs; |
| 1816 | } |
| 1817 | |
| 1818 | /** |
| 1819 | * Marks the given files for deletion. This will remove them from the UI. Clients must then |
| 1820 | * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm |
| 1821 | * the deletion, respectively. Only one deletion operation is allowed at a time. |
| 1822 | * |
| 1823 | * @param selected A selection representing the files to delete. |
| 1824 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1825 | void markForDeletion(Selection selected) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1826 | // Only one deletion operation at a time. |
| 1827 | checkState(mMarkedForDeletion.size() == 0); |
| 1828 | // There should never be more to delete than what exists. |
| 1829 | checkState(mCursorCount >= selected.size()); |
| 1830 | |
| 1831 | final int size = selected.size(); |
| 1832 | for (int i = 0; i < size; ++i) { |
| 1833 | int position = selected.get(i); |
| 1834 | if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion"); |
| 1835 | mMarkedForDeletion.append(position, true); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1836 | mViewAdapter.notifyItemRemoved(position); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | /** |
| 1841 | * Cancels an ongoing deletion operation. All files currently marked for deletion will be |
| 1842 | * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}. |
| 1843 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1844 | void undoDeletion() { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1845 | // Iterate over deleted items, temporarily marking them false in the deletion list, and |
| 1846 | // re-adding them to the UI. |
| 1847 | final int size = mMarkedForDeletion.size(); |
| 1848 | for (int i = 0; i < size; ++i) { |
| 1849 | final int position = mMarkedForDeletion.keyAt(i); |
| 1850 | mMarkedForDeletion.put(position, false); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1851 | mViewAdapter.notifyItemInserted(position); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | // Then, clear the deletion list. |
| 1855 | mMarkedForDeletion.clear(); |
| 1856 | } |
| 1857 | |
| 1858 | /** |
| 1859 | * Finalizes an ongoing deletion operation. All files currently marked for deletion will be |
| 1860 | * deleted. See {@link #markForDeletion(Selection)}. |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1861 | * |
| 1862 | * @param view The view which will be used to interact with the user (e.g. surfacing |
| 1863 | * snackbars) for errors, info, etc. |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1864 | */ |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1865 | void finalizeDeletion(DeletionListener listener) { |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1866 | final ContentResolver resolver = mContext.getContentResolver(); |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1867 | DeleteFilesTask task = new DeleteFilesTask(resolver, listener); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1868 | task.execute(); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | * A Task which collects the DocumentInfo for documents that have been marked for deletion, |
| 1873 | * and actually deletes them. |
| 1874 | */ |
| 1875 | private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> { |
| 1876 | private ContentResolver mResolver; |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1877 | private DeletionListener mListener; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1878 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1879 | /** |
| 1880 | * @param resolver A ContentResolver for performing the actual file deletions. |
| 1881 | * @param errorCallback A Runnable that is executed in the event that one or more errors |
| 1882 | * occured while copying files. Execution will occur on the UI thread. |
| 1883 | */ |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1884 | public DeleteFilesTask(ContentResolver resolver, DeletionListener listener) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1885 | mResolver = resolver; |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1886 | mListener = listener; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | @Override |
| 1890 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1891 | return getDocumentsMarkedForDeletion(); |
| 1892 | } |
| 1893 | |
| 1894 | @Override |
| 1895 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1896 | boolean hadTrouble = false; |
| 1897 | for (DocumentInfo doc : docs) { |
| 1898 | if (!doc.isDeleteSupported()) { |
| 1899 | Log.w(TAG, doc + " could not be deleted. Skipping..."); |
| 1900 | hadTrouble = true; |
| 1901 | continue; |
| 1902 | } |
| 1903 | |
| 1904 | ContentProviderClient client = null; |
| 1905 | try { |
| 1906 | if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName); |
| 1907 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1908 | mResolver, doc.derivedUri.getAuthority()); |
| 1909 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 1910 | } catch (Exception e) { |
| 1911 | Log.w(TAG, "Failed to delete " + doc); |
| 1912 | hadTrouble = true; |
| 1913 | } finally { |
| 1914 | ContentProviderClient.releaseQuietly(client); |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | if (hadTrouble) { |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1919 | // TODO show which files failed? b/23720103 |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1920 | mListener.onError(); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1921 | if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed."); |
| 1922 | } else { |
| 1923 | if (DEBUG) Log.d(TAG, "Deletion task completed successfully."); |
| 1924 | } |
| 1925 | mMarkedForDeletion.clear(); |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1926 | |
| 1927 | mListener.onCompletion(); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1930 | |
Ben Kwa | c21888e | 2015-09-30 14:14:16 -0700 | [diff] [blame] | 1931 | static class DeletionListener { |
| 1932 | /** |
| 1933 | * Called when deletion has completed (regardless of whether an error occurred). |
| 1934 | */ |
| 1935 | void onCompletion() {} |
| 1936 | |
| 1937 | /** |
| 1938 | * Called at the end of a deletion operation that produced one or more errors. |
| 1939 | */ |
| 1940 | void onError() {} |
| 1941 | } |
| 1942 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1943 | void addUpdateListener(UpdateListener listener) { |
| 1944 | checkState(mUpdateListener == null); |
| 1945 | mUpdateListener = listener; |
| 1946 | } |
| 1947 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1948 | static class UpdateListener { |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1949 | /** |
| 1950 | * Called when a successful update has occurred. |
| 1951 | */ |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1952 | void onModelUpdate(Model model) {} |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1953 | |
| 1954 | /** |
| 1955 | * Called when an update has been attempted but failed. |
| 1956 | */ |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1957 | void onModelUpdateFailed(Exception e) {} |
| 1958 | } |
| 1959 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1960 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1961 | private class ModelUpdateListener extends Model.UpdateListener { |
| 1962 | @Override |
| 1963 | public void onModelUpdate(Model model) { |
| 1964 | if (model.info != null || model.error != null) { |
| 1965 | mMessageBar.setInfo(model.info); |
| 1966 | mMessageBar.setError(model.error); |
| 1967 | mMessageBar.show(); |
| 1968 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1969 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1970 | mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE); |
| 1971 | |
| 1972 | if (model.isEmpty()) { |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 1973 | showEmptyView(); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1974 | } else { |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 1975 | showRecyclerView(); |
| 1976 | mAdapter.notifyDataSetChanged(); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1977 | } |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | @Override |
| 1981 | public void onModelUpdateFailed(Exception e) { |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 1982 | showErrorView(); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1983 | } |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1984 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1985 | } |