Jeff Sharkey | 9e0036e | 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 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 17 | package com.android.documentsui.dirlist; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 18 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.State.ACTION_MANAGE; |
| 21 | import static com.android.documentsui.State.MODE_GRID; |
| 22 | import static com.android.documentsui.State.MODE_LIST; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 23 | import static com.android.documentsui.State.SORT_ORDER_UNKNOWN; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 24 | import static com.android.documentsui.model.DocumentInfo.getCursorInt; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 25 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 26 | import static com.android.internal.util.Preconditions.checkNotNull; |
Steve McKay | 0ba3a14 | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 27 | import static com.android.internal.util.Preconditions.checkState; |
Steve McKay | d28c5c3 | 2015-12-07 16:31:42 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkArgument; |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 29 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 30 | import android.annotation.IntDef; |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 31 | import android.annotation.StringRes; |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 32 | import android.app.Activity; |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 33 | import android.app.ActivityManager; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 34 | import android.app.Fragment; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 35 | import android.app.FragmentManager; |
| 36 | import android.app.FragmentTransaction; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 37 | import android.app.LoaderManager.LoaderCallbacks; |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 38 | import android.content.ClipData; |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 40 | import android.content.Context; |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 41 | import android.content.Intent; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 42 | import android.content.Loader; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 43 | import android.database.Cursor; |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 44 | import android.graphics.Canvas; |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 45 | import android.graphics.Point; |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 46 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 47 | import android.net.Uri; |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 48 | import android.os.AsyncTask; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 49 | import android.os.Bundle; |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 50 | import android.os.Parcelable; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 51 | import android.provider.DocumentsContract; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 52 | import android.provider.DocumentsContract.Document; |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 53 | import android.support.annotation.Nullable; |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 54 | import android.support.design.widget.Snackbar; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 55 | import android.support.v7.widget.GridLayoutManager; |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 56 | import android.support.v7.widget.GridLayoutManager.SpanSizeLookup; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 57 | import android.support.v7.widget.RecyclerView; |
Steve McKay | 669ebe7 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 58 | import android.support.v7.widget.RecyclerView.OnItemTouchListener; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 59 | import android.support.v7.widget.RecyclerView.RecyclerListener; |
| 60 | import android.support.v7.widget.RecyclerView.ViewHolder; |
Jeff Sharkey | f491c72 | 2015-06-11 09:16:19 -0700 | [diff] [blame] | 61 | import android.text.TextUtils; |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 62 | import android.util.Log; |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 63 | import android.util.SparseArray; |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 64 | import android.util.TypedValue; |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 65 | import android.view.ActionMode; |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 66 | import android.view.DragEvent; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 67 | import android.view.GestureDetector; |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 68 | import android.view.KeyEvent; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 69 | import android.view.LayoutInflater; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 70 | import android.view.Menu; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 71 | import android.view.MenuItem; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 72 | import android.view.MotionEvent; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 73 | import android.view.View; |
| 74 | import android.view.ViewGroup; |
Vladislav Kaznacheev | b5999ef | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 75 | import android.view.ViewParent; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 76 | import android.widget.ImageView; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 77 | import android.widget.TextView; |
| 78 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 79 | import com.android.documentsui.BaseActivity; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 80 | import com.android.documentsui.DirectoryLoader; |
| 81 | import com.android.documentsui.DirectoryResult; |
| 82 | import com.android.documentsui.DocumentClipper; |
| 83 | import com.android.documentsui.DocumentsActivity; |
| 84 | import com.android.documentsui.DocumentsApplication; |
| 85 | import com.android.documentsui.Events; |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 86 | import com.android.documentsui.Events.MotionInputEvent; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 87 | import com.android.documentsui.Menus; |
| 88 | import com.android.documentsui.MessageBar; |
| 89 | import com.android.documentsui.MimePredicate; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 90 | import com.android.documentsui.R; |
Steve McKay | e2af078 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 91 | import com.android.documentsui.RecentsLoader; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 92 | import com.android.documentsui.RootsCache; |
| 93 | import com.android.documentsui.Shared; |
| 94 | import com.android.documentsui.Snackbars; |
| 95 | import com.android.documentsui.State; |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 96 | import com.android.documentsui.State.ViewMode; |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 97 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 98 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | f8c3f32 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 99 | import com.android.documentsui.model.DocumentStack; |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 100 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 101 | import com.android.documentsui.services.FileOperationService; |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 102 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 103 | import com.android.documentsui.services.FileOperations; |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 104 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 105 | import com.google.common.collect.Lists; |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 106 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 107 | import java.lang.annotation.Retention; |
| 108 | import java.lang.annotation.RetentionPolicy; |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 109 | import java.util.ArrayList; |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 110 | import java.util.Collections; |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 111 | import java.util.List; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Display the documents inside a single directory. |
| 115 | */ |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 116 | public class DirectoryFragment extends Fragment implements DocumentsAdapter.Environment { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 117 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 118 | @IntDef(flag = true, value = { |
| 119 | TYPE_NORMAL, |
| 120 | TYPE_SEARCH, |
| 121 | TYPE_RECENT_OPEN |
| 122 | }) |
| 123 | @Retention(RetentionPolicy.SOURCE) |
| 124 | public @interface ResultType {} |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 125 | public static final int TYPE_NORMAL = 1; |
| 126 | public static final int TYPE_SEARCH = 2; |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 127 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | dc2963a | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 128 | |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 129 | public static final int ANIM_NONE = 1; |
| 130 | public static final int ANIM_SIDE = 2; |
Tomasz Mikolajewski | e3fe9d7 | 2016-02-03 16:53:21 +0900 | [diff] [blame] | 131 | public static final int ANIM_LEAVE = 3; |
| 132 | public static final int ANIM_ENTER = 4; |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 133 | |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 134 | @IntDef(flag = true, value = { |
| 135 | REQUEST_COPY_DESTINATION |
| 136 | }) |
| 137 | @Retention(RetentionPolicy.SOURCE) |
| 138 | public @interface RequestCode {} |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 139 | public static final int REQUEST_COPY_DESTINATION = 1; |
| 140 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 141 | static final boolean DEBUG_ENABLE_DND = true; |
Steve McKay | b46383b | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 142 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 143 | private static final String TAG = "DirectoryFragment"; |
| 144 | private static final int LOADER_ID = 42; |
| 145 | private static final int DELETE_UNDO_TIMEOUT = 5000; |
| 146 | private static final int DELETE_JOB_DELAY = 5500; |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 147 | private static final int EMPTY_REVEAL_DURATION = 250; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 148 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 149 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 150 | private static final String EXTRA_ROOT = "root"; |
| 151 | private static final String EXTRA_DOC = "doc"; |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 152 | private static final String EXTRA_QUERY = "query"; |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 153 | private static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 154 | |
Ben Kwa | 18fce3c | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 155 | private Model mModel; |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 156 | private MultiSelectManager mSelectionManager; |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 157 | private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener(); |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 158 | private ItemEventListener mItemEventListener = new ItemEventListener(); |
Ben Kwa | b0bfe2d | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 159 | private FocusManager mFocusManager; |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 160 | |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 161 | private IconHelper mIconHelper; |
| 162 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 163 | private View mEmptyView; |
| 164 | private RecyclerView mRecView; |
Steve McKay | 41585d5 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 165 | private ListeningGestureDetector mGestureDetector; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 166 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 167 | private @ResultType int mType = TYPE_NORMAL; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 168 | private String mStateKey; |
| 169 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 170 | private int mLastSortOrder = SORT_ORDER_UNKNOWN; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 171 | private DocumentsAdapter mAdapter; |
| 172 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 173 | private FragmentTuner mTuner; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 174 | private DocumentClipper mClipper; |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 175 | private GridLayoutManager mLayout; |
Steve McKay | 0ba3a14 | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 176 | private int mColumnCount = 1; // This will get updated when layout changes. |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 177 | |
Ben Kwa | 3b19e31 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 178 | private MessageBar mMessageBar; |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 179 | private View mProgressBar; |
Ben Kwa | 3b19e31 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public View onCreateView( |
| 183 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 184 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 185 | |
Ben Kwa | 3b19e31 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 186 | mMessageBar = MessageBar.create(getChildFragmentManager()); |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 187 | mProgressBar = view.findViewById(R.id.progressbar); |
Ben Kwa | 3b19e31 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 188 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 189 | mEmptyView = view.findViewById(android.R.id.empty); |
| 190 | |
Ben Kwa | 2036dad | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 191 | mRecView = (RecyclerView) view.findViewById(R.id.dir_list); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 192 | mRecView.setRecyclerListener( |
| 193 | new RecyclerListener() { |
| 194 | @Override |
| 195 | public void onViewRecycled(ViewHolder holder) { |
| 196 | cancelThumbnailTask(holder.itemView); |
| 197 | } |
| 198 | }); |
Steve McKay | b46383b | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 199 | |
Ben Kwa | e48e4ca | 2015-10-20 15:02:33 -0700 | [diff] [blame] | 200 | mRecView.setItemAnimator(new DirectoryItemAnimator(getActivity())); |
Ben Kwa | bd96456 | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 201 | |
Steve McKay | 0ba3a14 | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 202 | // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration). |
Steve McKay | b46383b | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 203 | if (DEBUG_ENABLE_DND) { |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 204 | setupDragAndDropOnDirectoryView(mRecView); |
Steve McKay | b46383b | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 205 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 206 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 207 | return view; |
| 208 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 209 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 210 | @Override |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 211 | public void onDestroyView() { |
| 212 | super.onDestroyView(); |
| 213 | |
| 214 | // Cancel any outstanding thumbnail requests |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 215 | final int count = mRecView.getChildCount(); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 216 | for (int i = 0; i < count; i++) { |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 217 | final View view = mRecView.getChildAt(i); |
| 218 | cancelThumbnailTask(view); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | @Override |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 223 | public void onActivityCreated(Bundle savedInstanceState) { |
| 224 | super.onActivityCreated(savedInstanceState); |
| 225 | |
| 226 | final Context context = getActivity(); |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 227 | final State state = getDisplayState(); |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 228 | |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 229 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 230 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 231 | mStateKey = buildStateKey(root, doc); |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 232 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 233 | mIconHelper = new IconHelper(context, MODE_GRID); |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 234 | |
| 235 | mAdapter = new SectionBreakDocumentsAdapterWrapper( |
| 236 | this, new ModelBackedDocumentsAdapter(this, mIconHelper)); |
| 237 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 238 | mRecView.setAdapter(mAdapter); |
| 239 | |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 240 | mLayout = new GridLayoutManager(getContext(), mColumnCount); |
| 241 | SpanSizeLookup lookup = mAdapter.createSpanSizeLookup(); |
| 242 | if (lookup != null) { |
| 243 | mLayout.setSpanSizeLookup(lookup); |
| 244 | } |
| 245 | mRecView.setLayoutManager(mLayout); |
| 246 | |
Steve McKay | 41585d5 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 247 | mGestureDetector = new ListeningGestureDetector(this.getContext(), new GestureListener()); |
Steve McKay | 669ebe7 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 248 | |
Steve McKay | 41585d5 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 249 | mRecView.addOnItemTouchListener(mGestureDetector); |
Steve McKay | 669ebe7 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 250 | |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 251 | // final here because we'll manually bump the listener iwhen we had an initial selection, |
| 252 | // but only after the model is fully loaded. |
| 253 | final SelectionModeListener selectionListener = new SelectionModeListener(); |
| 254 | final Selection initialSelection = state.selectedDocuments.hasDirectoryKey(mStateKey) |
| 255 | ? state.selectedDocuments |
| 256 | : null; |
| 257 | |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 258 | // TODO: instead of inserting the view into the constructor, extract listener-creation code |
| 259 | // and set the listener on the view after the fact. Then the view doesn't need to be passed |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 260 | // into the selection manager. |
| 261 | mSelectionManager = new MultiSelectManager( |
Steve McKay | dbec47a | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 262 | mRecView, |
Steve McKay | 4440826 | 2016-01-05 15:27:17 -0800 | [diff] [blame] | 263 | mAdapter, |
Steve McKay | dbec47a | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 264 | state.allowMultiple |
| 265 | ? MultiSelectManager.MODE_MULTIPLE |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 266 | : MultiSelectManager.MODE_SINGLE, |
| 267 | initialSelection); |
| 268 | |
| 269 | mSelectionManager.addCallback(selectionListener); |
Ben Kwa | 18fce3c | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 270 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 271 | mModel = new Model(); |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 272 | mModel.addUpdateListener(mAdapter); |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 273 | mModel.addUpdateListener(mModelUpdateListener); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 274 | |
Ben Kwa | a4acc90 | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 275 | // Make sure this is done after the RecyclerView is set up. |
| 276 | mFocusManager = new FocusManager(context, mRecView, mModel); |
| 277 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 278 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 279 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 280 | mTuner = FragmentTuner.pick(getContext(), state); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 281 | mClipper = new DocumentClipper(context); |
| 282 | |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 283 | boolean hideGridTitles; |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 284 | if (mType == TYPE_RECENT_OPEN) { |
| 285 | // Hide titles when showing recents for picking images/videos |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 286 | hideGridTitles = MimePredicate.mimeMatches( |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 287 | MimePredicate.VISUAL_MIMES, state.acceptMimes); |
| 288 | } else { |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 289 | hideGridTitles = (doc != null) && doc.isGridTitlesHidden(); |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 290 | } |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 291 | GridDocumentHolder.setHideTitles(hideGridTitles); |
Jeff Sharkey | f6db154 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 292 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 293 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 294 | Context.ACTIVITY_SERVICE); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 295 | boolean svelte = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN); |
| 296 | mIconHelper.setThumbnailsEnabled(!svelte); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 297 | |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 298 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 299 | @Override |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 300 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 301 | final String query = getArguments().getString(EXTRA_QUERY); |
Jeff Sharkey | bc2971d | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 302 | |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 303 | Uri contentsUri; |
| 304 | switch (mType) { |
| 305 | case TYPE_NORMAL: |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 306 | contentsUri = DocumentsContract.buildChildDocumentsUri( |
| 307 | doc.authority, doc.documentId); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 308 | if (state.action == ACTION_MANAGE) { |
| 309 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 310 | } |
Jeff Sharkey | fb3445c | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 311 | return new DirectoryLoader( |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 312 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 313 | case TYPE_SEARCH: |
| 314 | contentsUri = DocumentsContract.buildSearchDocumentsUri( |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 315 | root.authority, root.rootId, query); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 316 | if (state.action == ACTION_MANAGE) { |
| 317 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 318 | } |
Jeff Sharkey | fb3445c | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 319 | return new DirectoryLoader( |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 320 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 321 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 348ad68 | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 322 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Steve McKay | e2af078 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 323 | return new RecentsLoader(context, roots, state); |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 324 | default: |
| 325 | throw new IllegalStateException("Unknown type " + mType); |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 326 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 330 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 331 | if (!isAdded()) return; |
| 332 | |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 333 | mModel.update(result); |
Jeff Sharkey | fb3445c | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 334 | state.derivedSortOrder = result.sortOrder; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 335 | |
| 336 | updateDisplayState(); |
| 337 | |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 338 | if (initialSelection != null) { |
| 339 | selectionListener.onSelectionChanged(); |
| 340 | } |
| 341 | |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 342 | // Restore any previous instance state |
| 343 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 344 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 345 | getView().restoreHierarchyState(container); |
| 346 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 347 | // The derived sort order takes the user sort order into account, but applies |
| 348 | // directory-specific defaults when the user doesn't explicitly set the sort |
| 349 | // order. Scroll to the top if the sort order actually changed. |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 350 | mRecView.smoothScrollToPosition(0); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 351 | } |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 352 | |
| 353 | mLastSortOrder = state.derivedSortOrder; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 354 | |
| 355 | mTuner.onModelLoaded(mModel, mType); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 359 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 360 | mModel.update(null); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 361 | } |
| 362 | }; |
| 363 | |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 364 | // Kick off loader at least once |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 365 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 368 | @Override |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 369 | public void onSaveInstanceState(Bundle outState) { |
| 370 | State state = getDisplayState(); |
| 371 | if (mSelectionManager.hasSelection()) { |
| 372 | mSelectionManager.getSelection(state.selectedDocuments); |
| 373 | state.selectedDocuments.setDirectoryKey(mStateKey); |
| 374 | if (!state.selectedDocuments.isEmpty()) { |
| 375 | if (DEBUG) Log.d(TAG, "Persisted selection: " + state.selectedDocuments); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | @Override |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 381 | public void onActivityResult(@RequestCode int requestCode, int resultCode, Intent data) { |
| 382 | switch(requestCode) { |
| 383 | case REQUEST_COPY_DESTINATION: |
| 384 | handleCopyResult(resultCode, data); |
| 385 | break; |
| 386 | default: |
| 387 | throw new UnsupportedOperationException("Unknown request code: " + requestCode); |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 388 | } |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | private void handleCopyResult(int resultCode, Intent data) { |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 392 | if (resultCode == Activity.RESULT_CANCELED || data == null) { |
| 393 | // User pressed the back button or otherwise cancelled the destination pick. Don't |
| 394 | // proceed with the copy. |
| 395 | return; |
| 396 | } |
| 397 | |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 398 | @OpType int operationType = data.getIntExtra( |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 399 | FileOperationService.EXTRA_OPERATION, |
| 400 | FileOperationService.OPERATION_COPY); |
| 401 | |
| 402 | FileOperations.start( |
| 403 | getActivity(), |
| 404 | getDisplayState().selectedDocumentsForCopy, |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 405 | getDisplayState().stack.peek(), |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 406 | (DocumentStack) data.getParcelableExtra(Shared.EXTRA_STACK), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 407 | operationType); |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Steve McKay | 5353a1e | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 410 | protected boolean onDoubleTap(MotionEvent e) { |
| 411 | if (Events.isMouseEvent(e)) { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 412 | String id = getModelId(e); |
| 413 | if (id != null) { |
| 414 | return handleViewItem(id); |
Steve McKay | 5353a1e | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 420 | private boolean handleViewItem(String id) { |
| 421 | final Cursor cursor = mModel.getItem(id); |
Steve McKay | 5353a1e | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 422 | checkNotNull(cursor, "Cursor cannot be null."); |
| 423 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 424 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 425 | if (mTuner.isDocumentEnabled(docMimeType, docFlags)) { |
Steve McKay | 5353a1e | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 426 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 427 | ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel); |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 428 | mSelectionManager.clearSelection(); |
Steve McKay | 5353a1e | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 429 | return true; |
| 430 | } |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 434 | @Override |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 435 | public void onStop() { |
| 436 | super.onStop(); |
| 437 | |
| 438 | // Remember last scroll location |
| 439 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 440 | getView().saveHierarchyState(container); |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 441 | final State state = getDisplayState(); |
Jeff Sharkey | e20a3ac | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 442 | state.dirState.put(mStateKey, container); |
| 443 | } |
| 444 | |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 445 | public void onDisplayStateChanged() { |
| 446 | updateDisplayState(); |
| 447 | } |
| 448 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 449 | public void onSortOrderChanged() { |
| 450 | // Sort order is implemented as a sorting wrapper around directory |
| 451 | // results. So when sort order changes, we force a reload of the directory. |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 452 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 455 | public void onViewModeChanged() { |
| 456 | // Mode change is just visual change; no need to kick loader. |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 457 | updateDisplayState(); |
| 458 | } |
| 459 | |
| 460 | private void updateDisplayState() { |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 461 | State state = getDisplayState(); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 462 | updateLayout(state.derivedMode); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 463 | mRecView.setAdapter(mAdapter); |
| 464 | } |
| 465 | |
| 466 | /** |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 467 | * Updates the layout after the view mode switches. |
| 468 | * @param mode The new view mode. |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 469 | */ |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 470 | private void updateLayout(@ViewMode int mode) { |
| 471 | mColumnCount = calculateColumnCount(mode); |
| 472 | if (mLayout != null) { |
| 473 | mLayout.setSpanCount(mColumnCount); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 474 | } |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 475 | |
Steve McKay | 76be620 | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 476 | int pad = getDirectoryPadding(mode); |
| 477 | mRecView.setPadding(pad, pad, pad, pad); |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 478 | mRecView.requestLayout(); |
Steve McKay | 76be620 | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 479 | mSelectionManager.handleLayoutChanged(); // RecyclerView doesn't do this for us |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 480 | mIconHelper.setViewMode(mode); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 483 | private int calculateColumnCount(@ViewMode int mode) { |
| 484 | if (mode == MODE_LIST) { |
| 485 | // List mode is a "grid" with 1 column. |
| 486 | return 1; |
| 487 | } |
| 488 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 489 | int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width); |
| 490 | int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin); |
Steve McKay | 0ba3a14 | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 491 | int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight(); |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 492 | |
Steve McKay | 0ba3a14 | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 493 | checkState(mRecView.getWidth() > 0); |
| 494 | int columnCount = Math.max(1, |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 495 | (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin)); |
| 496 | |
Steve McKay | 0ba3a14 | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 497 | return columnCount; |
| 498 | } |
| 499 | |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 500 | private int getDirectoryPadding(@ViewMode int mode) { |
Steve McKay | 76be620 | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 501 | switch (mode) { |
| 502 | case MODE_GRID: |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 503 | return getResources().getDimensionPixelSize(R.dimen.grid_container_padding); |
Steve McKay | 76be620 | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 504 | case MODE_LIST: |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 505 | return getResources().getDimensionPixelSize(R.dimen.list_container_padding); |
Steve McKay | 76be620 | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 506 | default: |
| 507 | throw new IllegalArgumentException("Unsupported layout mode: " + mode); |
| 508 | } |
| 509 | } |
| 510 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 511 | @Override |
| 512 | public int getColumnCount() { |
| 513 | return mColumnCount; |
| 514 | } |
| 515 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 516 | /** |
| 517 | * Manages the integration between our ActionMode and MultiSelectManager, initiating |
| 518 | * ActionMode when there is a selection, canceling it when there is no selection, |
| 519 | * and clearing selection when action mode is explicitly exited by the user. |
| 520 | */ |
| 521 | private final class SelectionModeListener |
| 522 | implements MultiSelectManager.Callback, ActionMode.Callback { |
| 523 | |
| 524 | private Selection mSelected = new Selection(); |
| 525 | private ActionMode mActionMode; |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 526 | private int mNoDeleteCount = 0; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 527 | private int mNoRenameCount = -1; |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 528 | private Menu mMenu; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 529 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 530 | @Override |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 531 | public boolean onBeforeItemStateChange(String modelId, boolean selected) { |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 532 | if (selected) { |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 533 | final Cursor cursor = mModel.getItem(modelId); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 534 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | 2a030b0 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 535 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 536 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 537 | return mTuner.canSelectType(docMimeType, docFlags); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 538 | } |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | @Override |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 543 | public void onItemStateChanged(String modelId, boolean selected) { |
| 544 | final Cursor cursor = mModel.getItem(modelId); |
Steve McKay | 30551a2 | 2016-02-16 13:08:10 -0800 | [diff] [blame] | 545 | if (cursor == null) { |
| 546 | Log.e(TAG, "Model returned null cursor for document: " + modelId |
| 547 | + ". Ignoring state changed event."); |
| 548 | return; |
| 549 | } |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 550 | |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 551 | // TODO: Should this be happening in onSelectionChanged? Technically this callback is |
| 552 | // triggered on "silent" selection updates (i.e. we might be reacting to unfinalized |
| 553 | // selection changes here) |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 554 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 555 | if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) { |
| 556 | mNoDeleteCount += selected ? 1 : -1; |
| 557 | } |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 558 | if ((docFlags & Document.FLAG_SUPPORTS_RENAME) != 0) { |
| 559 | mNoRenameCount += selected ? 1 : -1; |
| 560 | } |
Steve McKay | dbec47a | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 561 | } |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 562 | |
Steve McKay | dbec47a | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 563 | @Override |
| 564 | public void onSelectionChanged() { |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 565 | mSelectionManager.getSelection(mSelected); |
Ben Kwa | 7cc8001 | 2015-09-11 15:40:18 -0700 | [diff] [blame] | 566 | TypedValue color = new TypedValue(); |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 567 | if (mSelected.size() > 0) { |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 568 | if (DEBUG) Log.d(TAG, "Maybe starting action mode."); |
| 569 | if (mActionMode == null) { |
| 570 | if (DEBUG) Log.d(TAG, "Yeah. Starting action mode."); |
| 571 | mActionMode = getActivity().startActionMode(this); |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 572 | } |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 573 | getActivity().getTheme().resolveAttribute(R.attr.colorActionMode, color, true); |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 574 | updateActionMenu(); |
| 575 | } else { |
| 576 | if (DEBUG) Log.d(TAG, "Finishing action mode."); |
| 577 | if (mActionMode != null) { |
| 578 | mActionMode.finish(); |
| 579 | } |
Ben Kwa | 0f7078f0 | 2015-09-08 07:31:19 -0700 | [diff] [blame] | 580 | getActivity().getTheme().resolveAttribute( |
| 581 | android.R.attr.colorPrimaryDark, color, true); |
Jeff Sharkey | a5599ef | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 582 | } |
Ben Kwa | 7cc8001 | 2015-09-11 15:40:18 -0700 | [diff] [blame] | 583 | getActivity().getWindow().setStatusBarColor(color.data); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 584 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 585 | if (mActionMode != null) { |
Steve McKay | 5aacf1f | 2015-10-27 14:15:58 -0700 | [diff] [blame] | 586 | mActionMode.setTitle(String.valueOf(mSelected.size())); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | // Called when the user exits the action mode |
| 591 | @Override |
| 592 | public void onDestroyActionMode(ActionMode mode) { |
| 593 | if (DEBUG) Log.d(TAG, "Handling action mode destroyed."); |
| 594 | mActionMode = null; |
| 595 | // clear selection |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 596 | mSelectionManager.clearSelection(); |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 597 | mSelected.clear(); |
| 598 | mNoDeleteCount = 0; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 599 | mNoRenameCount = -1; |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 602 | @Override |
| 603 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 604 | int size = mSelectionManager.getSelection().size(); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 605 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 606 | mode.setTitle(TextUtils.formatSelectedCount(size)); |
| 607 | return (size > 0); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | @Override |
| 611 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 612 | mMenu = menu; |
| 613 | updateActionMenu(); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 617 | boolean canRenameSelection() { |
| 618 | return mNoRenameCount == 0 && mSelectionManager.getSelection().size() == 1; |
| 619 | } |
| 620 | |
| 621 | boolean canDeleteSelection() { |
| 622 | return mNoDeleteCount == 0; |
| 623 | } |
| 624 | |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 625 | private void updateActionMenu() { |
| 626 | checkNotNull(mMenu); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 627 | |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 628 | // Delegate update logic to our owning action, since specialized logic is desired. |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 629 | mTuner.updateActionMenu(mMenu, mType, canDeleteSelection(), canRenameSelection()); |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 630 | Menus.disableHiddenItems(mMenu); |
Steve McKay | 503648d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 633 | @Override |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 634 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 635 | |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 636 | Selection selection = mSelectionManager.getSelection(new Selection()); |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 637 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 638 | switch (item.getItemId()) { |
| 639 | case R.id.menu_open: |
| 640 | openDocuments(selection); |
| 641 | mode.finish(); |
| 642 | return true; |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 643 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 644 | case R.id.menu_share: |
| 645 | shareDocuments(selection); |
| 646 | mode.finish(); |
| 647 | return true; |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 648 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 649 | case R.id.menu_delete: |
| 650 | // Exit selection mode first, so we avoid deselecting deleted documents. |
| 651 | mode.finish(); |
| 652 | deleteDocuments(selection); |
| 653 | return true; |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 654 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 655 | case R.id.menu_copy_to: |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 656 | transferDocuments(selection, FileOperationService.OPERATION_COPY); |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 657 | mode.finish(); |
| 658 | return true; |
Ben Kwa | 0b4a3c4 | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 659 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 660 | case R.id.menu_move_to: |
| 661 | // Exit selection mode first, so we avoid deselecting deleted documents. |
| 662 | mode.finish(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 663 | transferDocuments(selection, FileOperationService.OPERATION_MOVE); |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 664 | return true; |
Ben Kwa | d99109f | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 665 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 666 | case R.id.menu_copy_to_clipboard: |
Steve McKay | cbee544 | 2016-01-28 15:30:10 -0800 | [diff] [blame] | 667 | copySelectedToClipboard(); |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 668 | return true; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 669 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 670 | case R.id.menu_select_all: |
| 671 | selectAllFiles(); |
| 672 | return true; |
Ben Kwa | 3bcc9488 | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 673 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 674 | case R.id.menu_rename: |
| 675 | renameDocuments(selection); |
| 676 | mode.finish(); |
| 677 | return true; |
| 678 | |
Steve McKay | c2b4b3d | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 679 | default: |
| 680 | if (DEBUG) Log.d(TAG, "Unhandled menu item selected: " + item); |
| 681 | return false; |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 684 | } |
Jeff Sharkey | be8b12e | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 685 | |
Steve McKay | cbee544 | 2016-01-28 15:30:10 -0800 | [diff] [blame] | 686 | public final boolean onBackPressed() { |
| 687 | if (mSelectionManager.hasSelection()) { |
| 688 | if (DEBUG) Log.d(TAG, "Clearing selection on back pressed."); |
| 689 | mSelectionManager.clearSelection(); |
| 690 | return true; |
| 691 | } |
| 692 | return false; |
| 693 | } |
| 694 | |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 695 | private void cancelThumbnailTask(View view) { |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 696 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 697 | if (iconThumb != null) { |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 698 | mIconHelper.stopLoading(iconThumb); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 699 | } |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 700 | } |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 701 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 702 | private void openDocuments(final Selection selected) { |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 703 | new GetDocumentsTask() { |
| 704 | @Override |
| 705 | void onDocumentsReady(List<DocumentInfo> docs) { |
Steve McKay | 1205547 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 706 | // TODO: Implement support in Files activity for opening multiple docs. |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 707 | BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Ben Kwa | 726cf70 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 708 | } |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 709 | }.execute(selected); |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 712 | private void shareDocuments(final Selection selected) { |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 713 | new GetDocumentsTask() { |
| 714 | @Override |
| 715 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 716 | Intent intent; |
| 717 | |
| 718 | // Filter out directories - those can't be shared. |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 719 | List<DocumentInfo> docsForSend = new ArrayList<>(); |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 720 | for (DocumentInfo doc: docs) { |
| 721 | if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) { |
| 722 | docsForSend.add(doc); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | if (docsForSend.size() == 1) { |
| 727 | final DocumentInfo doc = docsForSend.get(0); |
| 728 | |
| 729 | intent = new Intent(Intent.ACTION_SEND); |
| 730 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 731 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 732 | intent.setType(doc.mimeType); |
| 733 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
| 734 | |
| 735 | } else if (docsForSend.size() > 1) { |
| 736 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 737 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 738 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 739 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 740 | final ArrayList<String> mimeTypes = new ArrayList<>(); |
| 741 | final ArrayList<Uri> uris = new ArrayList<>(); |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 742 | for (DocumentInfo doc : docsForSend) { |
| 743 | mimeTypes.add(doc.mimeType); |
| 744 | uris.add(doc.derivedUri); |
| 745 | } |
| 746 | |
| 747 | intent.setType(findCommonMimeType(mimeTypes)); |
| 748 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 749 | |
| 750 | } else { |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
| 755 | startActivity(intent); |
| 756 | } |
| 757 | }.execute(selected); |
| 758 | } |
| 759 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 760 | private void deleteDocuments(final Selection selected) { |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 761 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 762 | checkArgument(!selected.isEmpty()); |
| 763 | final DocumentInfo srcParent = getDisplayState().stack.peek(); |
| 764 | new GetDocumentsTask() { |
| 765 | @Override |
| 766 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 767 | // Hide the files in the UI. |
| 768 | final SparseArray<String> hidden = mAdapter.hide(selected.getAll()); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 769 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 770 | checkState(DELETE_JOB_DELAY > DELETE_UNDO_TIMEOUT); |
| 771 | String operationId = FileOperations.delete( |
| 772 | getActivity(), docs, srcParent, getDisplayState().stack, |
| 773 | DELETE_JOB_DELAY); |
| 774 | showDeleteSnackbar(hidden, operationId); |
| 775 | } |
| 776 | }.execute(selected); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | private void showDeleteSnackbar(final SparseArray<String> hidden, final String jobId) { |
| 780 | |
| 781 | Context context = getActivity(); |
| 782 | String message = Shared.getQuantityString(context, R.plurals.deleting, hidden.size()); |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 783 | |
Ben Kwa | db65cd5 | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 784 | // Show a snackbar informing the user that files will be deleted, and give them an option to |
| 785 | // cancel. |
Ben Kwa | 94b486d | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 786 | final Activity activity = getActivity(); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 787 | Snackbars.makeSnackbar(activity, message, DELETE_UNDO_TIMEOUT) |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 788 | .setAction( |
| 789 | R.string.undo, |
Ben Kwa | bd96456 | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 790 | new View.OnClickListener() { |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 791 | @Override |
| 792 | public void onClick(View view) {} |
| 793 | }) |
| 794 | .setCallback( |
| 795 | new Snackbar.Callback() { |
| 796 | @Override |
| 797 | public void onDismissed(Snackbar snackbar, int event) { |
| 798 | if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { |
Ben Kwa | db65cd5 | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 799 | // If the delete was cancelled, just unhide the files. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 800 | FileOperations.cancel(activity, jobId); |
| 801 | mAdapter.unhide(hidden); |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 802 | } |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 803 | } |
| 804 | }) |
| 805 | .show(); |
Jeff Sharkey | 4eb407a | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Ben Kwa | 1e2fa5e | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 808 | private void transferDocuments(final Selection selected, final @OpType int mode) { |
Ben Kwa | ef3f262 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 809 | // Pop up a dialog to pick a destination. This is inadequate but works for now. |
| 810 | // TODO: Implement a picker that is to spec. |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 811 | final Intent intent = new Intent( |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 812 | Shared.ACTION_PICK_COPY_DESTINATION, |
Daichi Hirono | bbe2292 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 813 | Uri.EMPTY, |
| 814 | getActivity(), |
| 815 | DocumentsActivity.class); |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 816 | |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 817 | // Set an appropriate title on the drawer when it is shown in the picker. |
| 818 | // Coupled with the fact that we auto-open the drawer for copy/move operations |
| 819 | // it should basically be the thing people see first. |
| 820 | int drawerTitleId = mode == FileOperationService.OPERATION_MOVE |
| 821 | ? R.string.menu_move : R.string.menu_copy; |
| 822 | intent.putExtra(DocumentsContract.EXTRA_PROMPT, getResources().getString(drawerTitleId)); |
| 823 | |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 824 | new GetDocumentsTask() { |
| 825 | @Override |
| 826 | void onDocumentsReady(List<DocumentInfo> docs) { |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 827 | // TODO: Can this move to Fragment bundle state? |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 828 | getDisplayState().selectedDocumentsForCopy = docs; |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 829 | |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 830 | // Determine if there is a directory in the set of documents |
| 831 | // to be copied? Why? Directory creation isn't supported by some roots |
| 832 | // (like Downloads). This informs DocumentsActivity (the "picker") |
| 833 | // to restrict available roots to just those with support. |
| 834 | intent.putExtra(Shared.EXTRA_DIRECTORY_COPY, hasDirectory(docs)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 835 | intent.putExtra(FileOperationService.EXTRA_OPERATION, mode); |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 836 | |
| 837 | // This just identifies the type of request...we'll check it |
| 838 | // when we reveive a response. |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 839 | startActivityForResult(intent, REQUEST_COPY_DESTINATION); |
Daichi Hirono | 9be3429 | 2015-04-14 17:12:54 +0900 | [diff] [blame] | 840 | } |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 841 | |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 842 | }.execute(selected); |
Ben Kwa | d99109f | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 845 | private static boolean hasDirectory(List<DocumentInfo> docs) { |
| 846 | for (DocumentInfo info : docs) { |
| 847 | if (Document.MIME_TYPE_DIR.equals(info.mimeType)) { |
| 848 | return true; |
| 849 | } |
| 850 | } |
| 851 | return false; |
| 852 | } |
| 853 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 854 | private void renameDocuments(Selection selected) { |
| 855 | // Batch renaming not supported |
| 856 | // Rename option is only available in menu when 1 document selected |
| 857 | checkArgument(selected.size() == 1); |
| 858 | |
| 859 | new GetDocumentsTask() { |
| 860 | @Override |
| 861 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 862 | RenameDocumentFragment.show(getFragmentManager(), docs.get(0)); |
| 863 | } |
| 864 | }.execute(selected); |
| 865 | } |
| 866 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 867 | @Override |
| 868 | public void initDocumentHolder(DocumentHolder holder) { |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 869 | holder.addEventListener(mItemEventListener); |
Ben Kwa | 2036dad | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 870 | holder.itemView.setOnFocusChangeListener(mFocusManager); |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | @Override |
| 874 | public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) { |
| 875 | if (DEBUG_ENABLE_DND) { |
| 876 | setupDragAndDropOnDocumentView(holder.itemView, cursor); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | @Override |
| 881 | public State getDisplayState() { |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 882 | return ((BaseActivity) getActivity()).getDisplayState(); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 885 | @Override |
| 886 | public Model getModel() { |
| 887 | return mModel; |
| 888 | } |
| 889 | |
| 890 | @Override |
| 891 | public boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 892 | return mTuner.isDocumentEnabled(docMimeType, docFlags); |
| 893 | } |
| 894 | |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 895 | private void showEmptyDirectory() { |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 896 | showEmptyView(R.string.empty, R.drawable.cabinet); |
Ben Kwa | 91bec53 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 899 | private void showNoResults(RootInfo root) { |
| 900 | CharSequence msg = getContext().getResources().getText(R.string.no_results); |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 901 | showEmptyView(String.format(String.valueOf(msg), root.title), R.drawable.cabinet); |
Ben Kwa | 91bec53 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 904 | private void showQueryError() { |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 905 | showEmptyView(R.string.query_error, R.drawable.hourglass); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 908 | private void showEmptyView(@StringRes int id, int drawable) { |
| 909 | showEmptyView(getContext().getResources().getText(id), drawable); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 912 | private void showEmptyView(CharSequence msg, int drawable) { |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 913 | View content = mEmptyView.findViewById(R.id.content); |
| 914 | TextView msgView = (TextView) mEmptyView.findViewById(R.id.message); |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 915 | ImageView imageView = (ImageView) mEmptyView.findViewById(R.id.artwork); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 916 | msgView.setText(msg); |
Ben Kwa | 0e9aae4 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 917 | imageView.setImageResource(drawable); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 918 | |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 919 | mEmptyView.setVisibility(View.VISIBLE); |
Aga Wronska | d4b1753 | 2016-02-12 09:15:32 -0800 | [diff] [blame] | 920 | mEmptyView.requestFocus(); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 921 | mRecView.setVisibility(View.GONE); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | private void showDirectory() { |
Ben Kwa | 91bec53 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 925 | mEmptyView.setVisibility(View.GONE); |
| 926 | mRecView.setVisibility(View.VISIBLE); |
Aga Wronska | d4b1753 | 2016-02-12 09:15:32 -0800 | [diff] [blame] | 927 | mRecView.requestFocus(); |
Ben Kwa | 91bec53 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 930 | private String findCommonMimeType(List<String> mimeTypes) { |
| 931 | String[] commonType = mimeTypes.get(0).split("/"); |
| 932 | if (commonType.length != 2) { |
| 933 | return "*/*"; |
| 934 | } |
| 935 | |
| 936 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 937 | String[] type = mimeTypes.get(i).split("/"); |
| 938 | if (type.length != 2) continue; |
| 939 | |
| 940 | if (!commonType[1].equals(type[1])) { |
| 941 | commonType[1] = "*"; |
| 942 | } |
| 943 | |
| 944 | if (!commonType[0].equals(type[0])) { |
| 945 | commonType[0] = "*"; |
| 946 | commonType[1] = "*"; |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | return commonType[0] + "/" + commonType[1]; |
| 952 | } |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 953 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 954 | private void copyFromClipboard() { |
| 955 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
| 956 | |
| 957 | @Override |
| 958 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 959 | return mClipper.getClippedDocuments(); |
| 960 | } |
| 961 | |
| 962 | @Override |
| 963 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 964 | DocumentInfo destination = |
| 965 | ((BaseActivity) getActivity()).getCurrentDirectory(); |
| 966 | copyDocuments(docs, destination); |
| 967 | } |
| 968 | }.execute(); |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 971 | private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) { |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 972 | checkNotNull(clipData); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 973 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 974 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 975 | @Override |
| 976 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 977 | return mClipper.getDocumentsFromClipData(clipData); |
| 978 | } |
| 979 | |
| 980 | @Override |
| 981 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 982 | copyDocuments(docs, destination); |
| 983 | } |
| 984 | }.execute(); |
| 985 | } |
| 986 | |
| 987 | private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) { |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 988 | BaseActivity activity = (BaseActivity) getActivity(); |
| 989 | if (!canCopy(docs, activity.getCurrentRoot(), destination)) { |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 990 | Snackbars.makeSnackbar( |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 991 | getActivity(), |
Ben Kwa | 94b486d | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 992 | R.string.clipboard_files_cannot_paste, |
| 993 | Snackbar.LENGTH_SHORT) |
| 994 | .show(); |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 995 | return; |
| 996 | } |
| 997 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 998 | if (docs.isEmpty()) { |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 999 | return; |
Vladislav Kaznacheev | d69d3c4 | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1000 | } |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1001 | |
Steve McKay | f69502c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 1002 | final DocumentStack curStack = getDisplayState().stack; |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1003 | DocumentStack tmpStack = new DocumentStack(); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1004 | if (destination != null) { |
| 1005 | tmpStack.push(destination); |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1006 | tmpStack.addAll(curStack); |
| 1007 | } else { |
| 1008 | tmpStack = curStack; |
| 1009 | } |
| 1010 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 1011 | FileOperations.copy(getActivity(), docs, tmpStack); |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) { |
| 1015 | Context context = getActivity(); |
| 1016 | final ContentResolver resolver = context.getContentResolver(); |
| 1017 | ClipData clipData = null; |
| 1018 | for (DocumentInfo doc : docs) { |
| 1019 | final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId); |
| 1020 | if (clipData == null) { |
Vladislav Kaznacheev | d69d3c4 | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1021 | // TODO: figure out what this string should be. |
| 1022 | // Currently it is not displayed anywhere in the UI, but this might change. |
| 1023 | final String label = ""; |
| 1024 | clipData = ClipData.newUri(resolver, label, uri); |
Vladislav Kaznacheev | 335dba5 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1025 | } else { |
| 1026 | // TODO: update list of mime types in ClipData. |
| 1027 | clipData.addItem(new ClipData.Item(uri)); |
| 1028 | } |
| 1029 | } |
| 1030 | return clipData; |
| 1031 | } |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1032 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 1033 | public void copySelectedToClipboard() { |
Steve McKay | d28c5c3 | 2015-12-07 16:31:42 -0800 | [diff] [blame] | 1034 | Selection selection = mSelectionManager.getSelection(new Selection()); |
| 1035 | if (!selection.isEmpty()) { |
| 1036 | copySelectionToClipboard(selection); |
Steve McKay | cbee544 | 2016-01-28 15:30:10 -0800 | [diff] [blame] | 1037 | mSelectionManager.clearSelection(); |
Steve McKay | d28c5c3 | 2015-12-07 16:31:42 -0800 | [diff] [blame] | 1038 | } |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1039 | } |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1040 | |
Steve McKay | d28c5c3 | 2015-12-07 16:31:42 -0800 | [diff] [blame] | 1041 | void copySelectionToClipboard(Selection selection) { |
| 1042 | checkArgument(!selection.isEmpty()); |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1043 | new GetDocumentsTask() { |
| 1044 | @Override |
| 1045 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 1046 | mClipper.clipDocuments(docs); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1047 | Activity activity = getActivity(); |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 1048 | Snackbars.makeSnackbar(activity, |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1049 | activity.getResources().getQuantityString( |
| 1050 | R.plurals.clipboard_files_clipped, docs.size(), docs.size()), |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 1051 | Snackbar.LENGTH_SHORT).show(); |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1052 | } |
Steve McKay | d28c5c3 | 2015-12-07 16:31:42 -0800 | [diff] [blame] | 1053 | }.execute(selection); |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 1056 | public void pasteFromClipboard() { |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1057 | copyFromClipboard(); |
| 1058 | getActivity().invalidateOptionsMenu(); |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1061 | /** |
| 1062 | * Returns true if the list of files can be copied to destination. Note that this |
| 1063 | * is a policy check only. Currently the method does not attempt to verify |
| 1064 | * available space or any other environmental aspects possibly resulting in |
| 1065 | * failure to copy. |
| 1066 | * |
| 1067 | * @return true if the list of files can be copied to destination. |
| 1068 | */ |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 1069 | private boolean canCopy(List<DocumentInfo> files, RootInfo root, DocumentInfo dest) { |
| 1070 | if (dest == null || !dest.isDirectory() || !dest.isCreateSupported()) { |
| 1071 | return false; |
| 1072 | } |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1073 | |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 1074 | // Can't copy folders to downloads, because we don't show folders there. |
| 1075 | if (!root.isDownloads()) { |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1076 | for (DocumentInfo docs : files) { |
| 1077 | if (docs.isDirectory()) { |
| 1078 | return false; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 1083 | return true; |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 1086 | public void selectAllFiles() { |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 1087 | // Only select things currently visible in the adapter. |
| 1088 | boolean changed = mSelectionManager.setItemsSelected(mAdapter.getModelIds(), true); |
Steve McKay | b04b164 | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 1089 | if (changed) { |
| 1090 | updateDisplayState(); |
| 1091 | } |
Steve McKay | 3da8afc | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Ben Kwa | 2036dad | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 1094 | /** |
| 1095 | * Attempts to restore focus on the directory listing. |
| 1096 | */ |
| 1097 | public void requestFocus() { |
| 1098 | mFocusManager.restoreLastFocus(); |
| 1099 | } |
| 1100 | |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1101 | private void setupDragAndDropOnDirectoryView(View view) { |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1102 | // Listen for drops on non-directory items and empty space. |
| 1103 | view.setOnDragListener(mOnDragListener); |
| 1104 | } |
| 1105 | |
| 1106 | private void setupDragAndDropOnDocumentView(View view, Cursor cursor) { |
| 1107 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1108 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1109 | // Make a directory item a drop target. Drop on non-directories and empty space |
| 1110 | // is handled at the list/grid view level. |
| 1111 | view.setOnDragListener(mOnDragListener); |
| 1112 | } |
| 1113 | |
Steve McKay | 41585d5 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 1114 | view.setOnLongClickListener(mLongClickListener); |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | private View.OnDragListener mOnDragListener = new View.OnDragListener() { |
| 1118 | @Override |
| 1119 | public boolean onDrag(View v, DragEvent event) { |
| 1120 | switch (event.getAction()) { |
| 1121 | case DragEvent.ACTION_DRAG_STARTED: |
| 1122 | // TODO: Check if the event contains droppable data. |
| 1123 | return true; |
| 1124 | |
| 1125 | // TODO: Highlight potential drop target directory? |
| 1126 | // TODO: Expand drop target directory on hover? |
| 1127 | case DragEvent.ACTION_DRAG_ENTERED: |
| 1128 | case DragEvent.ACTION_DRAG_LOCATION: |
| 1129 | case DragEvent.ACTION_DRAG_EXITED: |
| 1130 | case DragEvent.ACTION_DRAG_ENDED: |
| 1131 | return true; |
| 1132 | |
| 1133 | case DragEvent.ACTION_DROP: |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1134 | String dstId = getModelId(v); |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1135 | DocumentInfo dstDir = null; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1136 | if (dstId != null) { |
| 1137 | Cursor dstCursor = mModel.getItem(dstId); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1138 | checkNotNull(dstCursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1139 | dstDir = DocumentInfo.fromDirectoryCursor(dstCursor); |
| 1140 | // TODO: Do not drop into the directory where the documents came from. |
| 1141 | } |
| 1142 | copyFromClipData(event.getClipData(), dstDir); |
| 1143 | return true; |
| 1144 | } |
| 1145 | return false; |
| 1146 | } |
| 1147 | }; |
| 1148 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1149 | /** |
| 1150 | * Gets the model ID for a given motion event (using the event position) |
| 1151 | */ |
| 1152 | private String getModelId(MotionEvent e) { |
| 1153 | View view = mRecView.findChildViewUnder(e.getX(), e.getY()); |
| 1154 | if (view == null) { |
| 1155 | return null; |
| 1156 | } |
| 1157 | RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(view); |
| 1158 | if (vh instanceof DocumentHolder) { |
| 1159 | return ((DocumentHolder) vh).modelId; |
| 1160 | } else { |
| 1161 | return null; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * Gets the model ID for a given RecyclerView item. |
| 1167 | * @param view A View that is a document item view, or a child of a document item view. |
| 1168 | * @return The Model ID for the given document, or null if the given view is not associated with |
| 1169 | * a document item view. |
| 1170 | */ |
| 1171 | private String getModelId(View view) { |
Vladislav Kaznacheev | b5999ef | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1172 | while (true) { |
| 1173 | if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1174 | RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(view); |
| 1175 | if (vh instanceof DocumentHolder) { |
| 1176 | return ((DocumentHolder) vh).modelId; |
| 1177 | } else { |
| 1178 | return null; |
| 1179 | } |
Vladislav Kaznacheev | b5999ef | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1180 | } |
| 1181 | ViewParent parent = view.getParent(); |
| 1182 | if (parent == null || !(parent instanceof View)) { |
| 1183 | return null; |
| 1184 | } |
| 1185 | view = (View) parent; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1189 | private List<DocumentInfo> getDraggableDocuments(View currentItemView) { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1190 | String modelId = getModelId(currentItemView); |
| 1191 | if (modelId == null) { |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1192 | return Collections.EMPTY_LIST; |
| 1193 | } |
| 1194 | |
Ben Kwa | 8250db4 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1195 | final List<DocumentInfo> selectedDocs = |
| 1196 | mModel.getDocuments(mSelectionManager.getSelection()); |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1197 | if (!selectedDocs.isEmpty()) { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1198 | if (!isSelected(modelId)) { |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1199 | // There is a selection that does not include the current item, drag nothing. |
| 1200 | return Collections.EMPTY_LIST; |
| 1201 | } |
| 1202 | return selectedDocs; |
| 1203 | } |
| 1204 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1205 | final Cursor cursor = mModel.getItem(modelId); |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1206 | checkNotNull(cursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1207 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 1208 | |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1209 | return Lists.newArrayList(doc); |
| 1210 | } |
| 1211 | |
| 1212 | private Drawable getDragShadowIcon(List<DocumentInfo> docs) { |
| 1213 | if (docs.size() == 1) { |
| 1214 | final DocumentInfo doc = docs.get(0); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 1215 | return mIconHelper.getDocumentIcon(getActivity(), doc.authority, doc.documentId, |
| 1216 | doc.mimeType, doc.icon); |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1217 | } |
| 1218 | return getActivity().getDrawable(R.drawable.ic_doc_generic); |
| 1219 | } |
| 1220 | |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1221 | private class DrawableShadowBuilder extends View.DragShadowBuilder { |
| 1222 | |
| 1223 | private final Drawable mShadow; |
| 1224 | |
| 1225 | private final int mShadowDimension; |
| 1226 | |
| 1227 | public DrawableShadowBuilder(Drawable shadow) { |
| 1228 | mShadow = shadow; |
| 1229 | mShadowDimension = getResources().getDimensionPixelSize( |
| 1230 | R.dimen.drag_shadow_size); |
| 1231 | mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension); |
| 1232 | } |
| 1233 | |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1234 | @Override |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1235 | public void onProvideShadowMetrics( |
| 1236 | Point shadowSize, Point shadowTouchPoint) { |
| 1237 | shadowSize.set(mShadowDimension, mShadowDimension); |
| 1238 | shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2); |
| 1239 | } |
| 1240 | |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1241 | @Override |
Vladislav Kaznacheev | 42a6bbc | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1242 | public void onDrawShadow(Canvas canvas) { |
| 1243 | mShadow.draw(canvas); |
| 1244 | } |
| 1245 | } |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1246 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1247 | /** |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1248 | * Abstract task providing support for loading documents *off* |
| 1249 | * the main thread. And if it isn't obvious, creating a list |
| 1250 | * of documents (especially large lists) can be pretty expensive. |
| 1251 | */ |
| 1252 | private abstract class GetDocumentsTask |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1253 | extends AsyncTask<Selection, Void, List<DocumentInfo>> { |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1254 | @Override |
Steve McKay | 4b3a13c | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1255 | protected final List<DocumentInfo> doInBackground(Selection... selected) { |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1256 | return mModel.getDocuments(selected[0]); |
Steve McKay | ab7865c | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | @Override |
| 1260 | protected final void onPostExecute(List<DocumentInfo> docs) { |
| 1261 | onDocumentsReady(docs); |
| 1262 | } |
| 1263 | |
| 1264 | abstract void onDocumentsReady(List<DocumentInfo> docs); |
| 1265 | } |
| 1266 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 1267 | @Override |
| 1268 | public boolean isSelected(String modelId) { |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1269 | return mSelectionManager.getSelection().contains(modelId); |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1270 | } |
Ben Kwa | 18fce3c | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1271 | |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 1272 | private class ItemEventListener implements DocumentHolder.EventListener { |
Ben Kwa | bd96456 | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 1273 | @Override |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 1274 | public boolean onActivate(DocumentHolder doc) { |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 1275 | // Toggle selection if we're in selection mode, othewise, view item. |
| 1276 | if (mSelectionManager.hasSelection()) { |
| 1277 | mSelectionManager.toggleSelection(doc.modelId); |
| 1278 | } else { |
| 1279 | handleViewItem(doc.modelId); |
| 1280 | } |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 1281 | return true; |
| 1282 | } |
| 1283 | |
| 1284 | @Override |
| 1285 | public boolean onSelect(DocumentHolder doc) { |
| 1286 | mSelectionManager.toggleSelection(doc.modelId); |
| 1287 | mSelectionManager.setSelectionRangeBegin(doc.getAdapterPosition()); |
| 1288 | return true; |
Ben Kwa | bd96456 | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 1289 | } |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 1290 | |
| 1291 | @Override |
| 1292 | public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event) { |
| 1293 | // Only handle key-down events. This is simpler, consistent with most other UIs, and |
| 1294 | // enables the handling of repeated key events from holding down a key. |
| 1295 | if (event.getAction() != KeyEvent.ACTION_DOWN) { |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
Ben Kwa | b0bfe2d | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 1299 | if (mFocusManager.handleKey(doc, keyCode, event)) { |
Ben Kwa | 83df50f | 2016-02-10 14:01:19 -0800 | [diff] [blame] | 1300 | // Handle range selection adjustments. Extending the selection will adjust the |
| 1301 | // bounds of the in-progress range selection. Each time an unshifted navigation |
| 1302 | // event is received, the range selection is restarted. |
| 1303 | if (shouldExtendSelection(event)) { |
| 1304 | if (!mSelectionManager.isRangeSelectionActive()) { |
| 1305 | // Start a range selection if one isn't active |
| 1306 | mSelectionManager.startRangeSelection(doc.getAdapterPosition()); |
| 1307 | } |
| 1308 | mSelectionManager.snapRangeSelection(mFocusManager.getFocusPosition()); |
| 1309 | } else { |
| 1310 | mSelectionManager.endRangeSelection(); |
| 1311 | } |
Ben Kwa | b0bfe2d | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 1312 | return true; |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Ben Kwa | b0bfe2d | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 1315 | // Handle enter key events |
| 1316 | if (keyCode == KeyEvent.KEYCODE_ENTER) { |
Ben Kwa | 09eb77b | 2016-02-10 08:57:25 -0800 | [diff] [blame] | 1317 | if (event.isShiftPressed()) { |
| 1318 | return onSelect(doc); |
| 1319 | } else { |
| 1320 | return onActivate(doc); |
| 1321 | } |
Ben Kwa | b0bfe2d | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | return false; |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 1325 | } |
Ben Kwa | 83df50f | 2016-02-10 14:01:19 -0800 | [diff] [blame] | 1326 | |
| 1327 | private boolean shouldExtendSelection(KeyEvent event) { |
| 1328 | return Events.isNavigationKeyCode(event.getKeyCode()) && |
| 1329 | event.isShiftPressed(); |
| 1330 | } |
Ben Kwa | bd96456 | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 1333 | private final class ModelUpdateListener implements Model.UpdateListener { |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1334 | @Override |
| 1335 | public void onModelUpdate(Model model) { |
| 1336 | if (model.info != null || model.error != null) { |
| 1337 | mMessageBar.setInfo(model.info); |
| 1338 | mMessageBar.setError(model.error); |
| 1339 | mMessageBar.show(); |
| 1340 | } |
Ben Kwa | 18fce3c | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1341 | |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1342 | mProgressBar.setVisibility(model.isLoading() ? View.VISIBLE : View.GONE); |
| 1343 | |
| 1344 | if (model.isEmpty()) { |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1345 | if (getDisplayState().currentSearch != null) { |
| 1346 | showNoResults(getDisplayState().stack.root); |
| 1347 | } else { |
| 1348 | showEmptyDirectory(); |
| 1349 | } |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1350 | } else { |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1351 | showDirectory(); |
Ben Kwa | 91bec53 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 1352 | mAdapter.notifyDataSetChanged(); |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1353 | } |
Ben Kwa | 91caed8 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | @Override |
| 1357 | public void onModelUpdateFailed(Exception e) { |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1358 | showQueryError(); |
Ben Kwa | 18fce3c | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1359 | } |
Ben Kwa | c64cb25 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1360 | } |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 1361 | |
Steve McKay | 41585d5 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 1362 | private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { |
| 1363 | @Override |
| 1364 | public boolean onLongClick(View v) { |
| 1365 | if (mGestureDetector.mouseSpawnedLastEvent()) { |
| 1366 | List<DocumentInfo> docs = getDraggableDocuments(v); |
| 1367 | if (docs.isEmpty()) { |
| 1368 | return false; |
| 1369 | } |
| 1370 | v.startDrag( |
| 1371 | getClipDataFromDocuments(docs), |
| 1372 | new DrawableShadowBuilder(getDragShadowIcon(docs)), |
| 1373 | null, |
| 1374 | View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ | |
| 1375 | View.DRAG_FLAG_GLOBAL_URI_WRITE |
| 1376 | ); |
| 1377 | return true; |
| 1378 | } |
| 1379 | |
| 1380 | return false; |
| 1381 | } |
| 1382 | }; |
| 1383 | |
| 1384 | // Previously we listened to events with one class, only to bounce them forward |
| 1385 | // to GestureDetector. We're still doing that here, but with a single class |
| 1386 | // that reduces overall complexity in our glue code. |
| 1387 | private static final class ListeningGestureDetector extends GestureDetector |
| 1388 | implements OnItemTouchListener { |
| 1389 | |
| 1390 | private int mLastTool = -1; |
| 1391 | |
| 1392 | public ListeningGestureDetector(Context context, GestureListener listener) { |
| 1393 | super(context, listener); |
| 1394 | setOnDoubleTapListener(listener); |
| 1395 | } |
| 1396 | |
| 1397 | boolean mouseSpawnedLastEvent() { |
| 1398 | return Events.isMouseType(mLastTool); |
| 1399 | } |
| 1400 | |
| 1401 | boolean touchSpawnedLastEvent() { |
| 1402 | return Events.isTouchType(mLastTool); |
| 1403 | } |
| 1404 | |
| 1405 | @Override |
| 1406 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { |
| 1407 | mLastTool = e.getToolType(0); |
| 1408 | onTouchEvent(e); // bounce this forward to our detecty heart |
| 1409 | return false; |
| 1410 | } |
| 1411 | |
| 1412 | @Override |
| 1413 | public void onTouchEvent(RecyclerView rv, MotionEvent e) {} |
| 1414 | |
| 1415 | @Override |
| 1416 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} |
| 1417 | } |
| 1418 | |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 1419 | /** |
| 1420 | * The gesture listener for items in the list/grid view. Interprets gestures and sends the |
| 1421 | * events to the target DocumentHolder, whence they are routed to the appropriate listener. |
| 1422 | */ |
| 1423 | private class GestureListener extends GestureDetector.SimpleOnGestureListener { |
| 1424 | @Override |
| 1425 | public boolean onSingleTapUp(MotionEvent e) { |
| 1426 | // Single tap logic: |
| 1427 | // If the selection manager is active, it gets first whack at handling tap |
| 1428 | // events. Otherwise, tap events are routed to the target DocumentHolder. |
| 1429 | boolean handled = mSelectionManager.onSingleTapUp( |
| 1430 | new MotionInputEvent(e, mRecView)); |
| 1431 | |
| 1432 | if (handled) { |
| 1433 | return handled; |
| 1434 | } |
| 1435 | |
| 1436 | // Give the DocumentHolder a crack at the event. |
| 1437 | DocumentHolder holder = getTarget(e); |
| 1438 | if (holder != null) { |
| 1439 | handled = holder.onSingleTapUp(e); |
| 1440 | } |
| 1441 | |
| 1442 | return handled; |
| 1443 | } |
| 1444 | |
| 1445 | @Override |
| 1446 | public void onLongPress(MotionEvent e) { |
| 1447 | // Long-press events get routed directly to the selection manager. They can be |
| 1448 | // changed to route through the DocumentHolder if necessary. |
| 1449 | mSelectionManager.onLongPress(new MotionInputEvent(e, mRecView)); |
| 1450 | } |
| 1451 | |
| 1452 | @Override |
| 1453 | public boolean onDoubleTap(MotionEvent e) { |
| 1454 | // Double-tap events are handled directly by the DirectoryFragment. They can be changed |
| 1455 | // to route through the DocumentHolder if necessary. |
| 1456 | return DirectoryFragment.this.onDoubleTap(e); |
| 1457 | } |
| 1458 | |
| 1459 | private @Nullable DocumentHolder getTarget(MotionEvent e) { |
| 1460 | View childView = mRecView.findChildViewUnder(e.getX(), e.getY()); |
| 1461 | if (childView != null) { |
| 1462 | return (DocumentHolder) mRecView.getChildViewHolder(childView); |
| 1463 | } else { |
| 1464 | return null; |
| 1465 | } |
| 1466 | } |
| 1467 | } |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1468 | |
| 1469 | public static void showDirectory( |
| 1470 | FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
| 1471 | show(fm, TYPE_NORMAL, root, doc, null, anim); |
| 1472 | } |
| 1473 | |
| 1474 | public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) { |
| 1475 | show(fm, TYPE_SEARCH, root, null, query, anim); |
| 1476 | } |
| 1477 | |
| 1478 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
| 1479 | show(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
| 1480 | } |
| 1481 | |
| 1482 | private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 1483 | String query, int anim) { |
| 1484 | final Bundle args = new Bundle(); |
| 1485 | args.putInt(EXTRA_TYPE, type); |
| 1486 | args.putParcelable(EXTRA_ROOT, root); |
| 1487 | args.putParcelable(EXTRA_DOC, doc); |
| 1488 | args.putString(EXTRA_QUERY, query); |
| 1489 | |
| 1490 | final FragmentTransaction ft = fm.beginTransaction(); |
| 1491 | switch (anim) { |
| 1492 | case ANIM_SIDE: |
| 1493 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 1494 | break; |
Tomasz Mikolajewski | e3fe9d7 | 2016-02-03 16:53:21 +0900 | [diff] [blame] | 1495 | case ANIM_ENTER: |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1496 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
Tomasz Mikolajewski | e3fe9d7 | 2016-02-03 16:53:21 +0900 | [diff] [blame] | 1497 | ft.setCustomAnimations(R.animator.dir_enter, R.animator.dir_frozen); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1498 | break; |
Tomasz Mikolajewski | e3fe9d7 | 2016-02-03 16:53:21 +0900 | [diff] [blame] | 1499 | case ANIM_LEAVE: |
| 1500 | ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_leave); |
Steve McKay | 9ed88a4 | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1501 | break; |
| 1502 | } |
| 1503 | |
| 1504 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 1505 | fragment.setArguments(args); |
| 1506 | |
| 1507 | ft.replace(R.id.container_directory, fragment); |
| 1508 | ft.commitAllowingStateLoss(); |
| 1509 | } |
| 1510 | |
| 1511 | private static String buildStateKey(RootInfo root, DocumentInfo doc) { |
| 1512 | final StringBuilder builder = new StringBuilder(); |
| 1513 | builder.append(root != null ? root.authority : "null").append(';'); |
| 1514 | builder.append(root != null ? root.rootId : "null").append(';'); |
| 1515 | builder.append(doc != null ? doc.documentId : "null"); |
| 1516 | return builder.toString(); |
| 1517 | } |
| 1518 | |
| 1519 | public static @Nullable DirectoryFragment get(FragmentManager fm) { |
| 1520 | // TODO: deal with multiple directories shown at once |
| 1521 | Fragment fragment = fm.findFragmentById(R.id.container_directory); |
| 1522 | return fragment instanceof DirectoryFragment |
| 1523 | ? (DirectoryFragment) fragment |
| 1524 | : null; |
| 1525 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1526 | } |