Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 17 | package com.android.documentsui.dirlist; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 18 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.DocumentInfo.getCursorInt; |
| 20 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.base.Shared.DEBUG; |
| 22 | import static com.android.documentsui.base.State.MODE_GRID; |
| 23 | import static com.android.documentsui.base.State.MODE_LIST; |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 24 | |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 25 | import android.annotation.IntDef; |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 26 | import android.annotation.StringRes; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 27 | import android.app.Activity; |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 29 | import android.app.AlertDialog; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 30 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 31 | import android.app.FragmentManager; |
| 32 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 33 | import android.app.LoaderManager.LoaderCallbacks; |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 34 | import android.content.ClipData; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 35 | import android.content.Context; |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 36 | import android.content.DialogInterface; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 37 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 38 | import android.content.Loader; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 39 | import android.database.Cursor; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 40 | import android.net.Uri; |
| 41 | import android.os.Bundle; |
Garfield, Tan | eba5bb9 | 2016-07-22 10:30:49 -0700 | [diff] [blame] | 42 | import android.os.Handler; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 43 | import android.os.Parcelable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 44 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 45 | import android.provider.DocumentsContract.Document; |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 46 | import android.support.v4.widget.SwipeRefreshLayout; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 47 | import android.support.v7.widget.GridLayoutManager; |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 48 | import android.support.v7.widget.GridLayoutManager.SpanSizeLookup; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 49 | import android.support.v7.widget.RecyclerView; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 50 | import android.support.v7.widget.RecyclerView.RecyclerListener; |
| 51 | import android.support.v7.widget.RecyclerView.ViewHolder; |
Steve McKay | 927d800 | 2016-05-18 16:25:41 -0700 | [diff] [blame] | 52 | import android.text.BidiFormatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 53 | import android.util.Log; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 54 | import android.util.SparseArray; |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 55 | import android.view.ContextMenu; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 56 | import android.view.DragEvent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 57 | import android.view.LayoutInflater; |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 58 | import android.view.MenuInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 59 | import android.view.MenuItem; |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 60 | import android.view.MotionEvent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 61 | import android.view.View; |
| 62 | import android.view.ViewGroup; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 63 | import android.widget.ImageView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 64 | import android.widget.TextView; |
| 65 | |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 66 | import com.android.documentsui.ActionHandler; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 67 | import com.android.documentsui.BaseActivity; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 68 | import com.android.documentsui.BaseActivity.RetainedState; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 69 | import com.android.documentsui.DirectoryLoader; |
| 70 | import com.android.documentsui.DirectoryResult; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 71 | import com.android.documentsui.DocumentsApplication; |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 72 | import com.android.documentsui.FocusManager; |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 73 | import com.android.documentsui.ItemDragListener; |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 74 | import com.android.documentsui.MenuManager; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 75 | import com.android.documentsui.MessageBar; |
Aga Wronska | b028905 | 2016-03-31 16:37:40 -0700 | [diff] [blame] | 76 | import com.android.documentsui.Metrics; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 77 | import com.android.documentsui.R; |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 78 | import com.android.documentsui.RecentsLoader; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 79 | import com.android.documentsui.Snackbars; |
Garfield, Tan | eba5bb9 | 2016-07-22 10:30:49 -0700 | [diff] [blame] | 80 | import com.android.documentsui.ThumbnailCache; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 81 | import com.android.documentsui.base.DocumentInfo; |
Ben Lin | 2d6caf3 | 2016-09-19 17:48:54 -0700 | [diff] [blame] | 82 | import com.android.documentsui.base.EventHandler; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 83 | import com.android.documentsui.base.EventListener; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 84 | import com.android.documentsui.base.Events.InputEvent; |
| 85 | import com.android.documentsui.base.Events.MotionInputEvent; |
| 86 | import com.android.documentsui.base.RootInfo; |
| 87 | import com.android.documentsui.base.Shared; |
| 88 | import com.android.documentsui.base.State; |
| 89 | import com.android.documentsui.base.State.ViewMode; |
Ben Lin | c5e3e8e | 2016-07-13 18:16:36 -0700 | [diff] [blame] | 90 | import com.android.documentsui.clipping.DocumentClipper; |
Garfield, Tan | 9666ce6 | 2016-07-12 11:02:09 -0700 | [diff] [blame] | 91 | import com.android.documentsui.clipping.UrisSupplier; |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 92 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 93 | import com.android.documentsui.picker.PickActivity; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 94 | import com.android.documentsui.roots.RootsCache; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 95 | import com.android.documentsui.services.FileOperation; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 96 | import com.android.documentsui.services.FileOperationService; |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 97 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 98 | import com.android.documentsui.services.FileOperations; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 99 | import com.android.documentsui.sorting.SortDimension; |
| 100 | import com.android.documentsui.sorting.SortDimension.SortDirection; |
| 101 | import com.android.documentsui.sorting.SortModel; |
Steve McKay | ce0b14c | 2016-05-03 11:25:52 -0700 | [diff] [blame] | 102 | |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 103 | import java.io.IOException; |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 104 | import java.lang.annotation.Retention; |
| 105 | import java.lang.annotation.RetentionPolicy; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 106 | import java.util.ArrayList; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 107 | import java.util.List; |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 108 | |
| 109 | import javax.annotation.Nullable; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Display the documents inside a single directory. |
| 113 | */ |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 114 | public class DirectoryFragment extends Fragment |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 115 | implements DocumentsAdapter.Environment, LoaderCallbacks<DirectoryResult>, |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 116 | ItemDragListener.DragHost, SwipeRefreshLayout.OnRefreshListener { |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 117 | |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 118 | @IntDef(flag = true, value = { |
| 119 | TYPE_NORMAL, |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 120 | TYPE_RECENT_OPEN |
| 121 | }) |
| 122 | @Retention(RetentionPolicy.SOURCE) |
| 123 | public @interface ResultType {} |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 124 | public static final int TYPE_NORMAL = 1; |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 125 | public static final int TYPE_RECENT_OPEN = 2; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 126 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 127 | @IntDef(flag = true, value = { |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 128 | REQUEST_COPY_DESTINATION |
| 129 | }) |
| 130 | @Retention(RetentionPolicy.SOURCE) |
| 131 | public @interface RequestCode {} |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 132 | public static final int REQUEST_COPY_DESTINATION = 1; |
| 133 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 134 | private static final String TAG = "DirectoryFragment"; |
| 135 | private static final int LOADER_ID = 42; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 136 | |
Garfield, Tan | eba5bb9 | 2016-07-22 10:30:49 -0700 | [diff] [blame] | 137 | private static final int CACHE_EVICT_LIMIT = 100; |
| 138 | private static final int REFRESH_SPINNER_DISMISS_DELAY = 500; |
| 139 | |
Ben Lin | 26819d7 | 2016-08-17 12:45:05 -0700 | [diff] [blame] | 140 | private final Model mModel = new Model(); |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 141 | private final EventListener<Model.Update> mModelUpdateListener = new ModelUpdateListener(); |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 142 | |
| 143 | // This dependency is informally "injected" from the owning Activity in our onCreate method. |
| 144 | private FragmentTuner mTuner; |
| 145 | |
| 146 | // This dependency is informally "injected" from the owning Activity in our onCreate method. |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 147 | private FocusManager mFocusManager; |
| 148 | |
| 149 | // This dependency is informally "injected" from the owning Activity in our onCreate method. |
| 150 | private ActionHandler<?> mActionHandler; |
| 151 | |
| 152 | // This dependency is informally "injected" from the owning Activity in our onCreate method. |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 153 | private MenuManager mMenuManager; |
| 154 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 155 | private MultiSelectManager mSelectionMgr; |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 156 | private ActionModeController mActionModeController; |
| 157 | private SelectionMetadata mSelectionMetadata; |
Steve McKay | 2d1e4a3 | 2016-07-13 13:03:16 -0700 | [diff] [blame] | 158 | private UserInputHandler<InputEvent> mInputHandler; |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 159 | private @Nullable BandController mBandController; |
| 160 | private DragHoverListener mDragHoverListener; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 161 | private IconHelper mIconHelper; |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 162 | private SwipeRefreshLayout mRefreshLayout; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 163 | private View mEmptyView; |
| 164 | private RecyclerView mRecView; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 165 | private View mFileList; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 166 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 167 | private DocumentsAdapter mAdapter; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 168 | private DocumentClipper mClipper; |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 169 | private GridLayoutManager mLayout; |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 170 | private int mColumnCount = 1; // This will get updated when layout changes. |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 171 | |
Steve McKay | f63e938 | 2016-03-01 08:28:02 -0800 | [diff] [blame] | 172 | private LayoutInflater mInflater; |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 173 | private MessageBar mMessageBar; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 174 | private View mProgressBar; |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 175 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 176 | private Config mConfig; |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 177 | |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 178 | // Note, we use !null to indicate that selection was restored (from rotation). |
| 179 | // So don't fiddle with this field unless you've got the bigger picture in mind. |
| 180 | private @Nullable Selection mRestoredSelection = null; |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 181 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 182 | private SortModel.UpdateListener mSortListener = (model, updateType) -> { |
| 183 | // Only when sort order has changed do we need to trigger another loading. |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 184 | if ((updateType & SortModel.UPDATE_TYPE_SORTING) != 0) { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 185 | getLoaderManager().restartLoader(LOADER_ID, null, this); |
| 186 | } |
| 187 | }; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 188 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public View onCreateView( |
| 191 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
Steve McKay | f63e938 | 2016-03-01 08:28:02 -0800 | [diff] [blame] | 192 | mInflater = inflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 193 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 194 | |
Ben Kwa | c42fa40 | 2015-09-16 08:04:37 -0700 | [diff] [blame] | 195 | mMessageBar = MessageBar.create(getChildFragmentManager()); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 196 | mProgressBar = getActivity().findViewById(R.id.progressbar); |
| 197 | assert(mProgressBar != null); |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 198 | |
| 199 | mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout); |
| 200 | mRefreshLayout.setOnRefreshListener(this); |
| 201 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 202 | mEmptyView = view.findViewById(android.R.id.empty); |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 203 | mRecView = (RecyclerView) view.findViewById(R.id.dir_list); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 204 | mRecView.setRecyclerListener( |
| 205 | new RecyclerListener() { |
| 206 | @Override |
| 207 | public void onViewRecycled(ViewHolder holder) { |
| 208 | cancelThumbnailTask(holder.itemView); |
| 209 | } |
| 210 | }); |
Ben Kwa | 60d91e4 | 2015-10-20 15:02:33 -0700 | [diff] [blame] | 211 | mRecView.setItemAnimator(new DirectoryItemAnimator(getActivity())); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 212 | mFileList = view.findViewById(R.id.file_list); |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 213 | |
Ben Lin | 2abb4c7 | 2016-08-01 18:05:23 -0700 | [diff] [blame] | 214 | final int edgeHeight = (int) getResources().getDimension(R.dimen.autoscroll_edge_height); |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 215 | mDragHoverListener = DragHoverListener.create( |
Ben Lin | 2abb4c7 | 2016-08-01 18:05:23 -0700 | [diff] [blame] | 216 | edgeHeight, new DirectoryDragListener(this), mRecView); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 217 | |
Ben Kwa | a8c0da4 | 2016-02-25 14:10:40 -0800 | [diff] [blame] | 218 | // Make the recycler and the empty views responsive to drop events. |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 219 | mRecView.setOnDragListener(mDragHoverListener); |
| 220 | mEmptyView.setOnDragListener(mDragHoverListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 222 | return view; |
| 223 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 224 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 225 | @Override |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 226 | public void onDestroyView() { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 227 | mSelectionMgr.clearSelection(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 228 | |
| 229 | // Cancel any outstanding thumbnail requests |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 230 | final int count = mRecView.getChildCount(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 231 | for (int i = 0; i < count; i++) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 232 | final View view = mRecView.getChildAt(i); |
| 233 | cancelThumbnailTask(view); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 234 | } |
Aga Wronska | 7e5b963 | 2016-02-26 11:36:07 -0800 | [diff] [blame] | 235 | |
| 236 | super.onDestroyView(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 240 | public void onActivityCreated(Bundle savedInstanceState) { |
| 241 | super.onActivityCreated(savedInstanceState); |
| 242 | |
| 243 | final Context context = getActivity(); |
Steve McKay | b85f90c | 2015-11-09 17:38:35 +0900 | [diff] [blame] | 244 | final State state = getDisplayState(); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 245 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 246 | // Read arguments when object created for the first time. |
| 247 | // Restore state if fragment recreated. |
| 248 | Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState; |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 249 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 250 | mConfig = new Config(); |
| 251 | mConfig.restore(args); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 252 | |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 253 | // Restore any selection we may have squirreled away in retained state. |
| 254 | @Nullable RetainedState retained = getBaseActivity().getRetainedState(); |
| 255 | if (retained != null && retained.hasSelection()) { |
| 256 | // We claim the selection for ourselves and null it out once used |
| 257 | // so we don't have a rando selection hanging around in RetainedState. |
| 258 | mRestoredSelection = retained.selection; |
| 259 | retained.selection = null; |
| 260 | } |
| 261 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 262 | mIconHelper = new IconHelper(context, MODE_GRID); |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 263 | mClipper = DocumentsApplication.getDocumentClipper(getContext()); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 264 | |
| 265 | mAdapter = new SectionBreakDocumentsAdapterWrapper( |
| 266 | this, new ModelBackedDocumentsAdapter(this, mIconHelper)); |
| 267 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 268 | mRecView.setAdapter(mAdapter); |
| 269 | |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 270 | mLayout = new GridLayoutManager(getContext(), mColumnCount) { |
| 271 | @Override |
| 272 | public void onLayoutCompleted(RecyclerView.State state) { |
| 273 | super.onLayoutCompleted(state); |
| 274 | mFocusManager.onLayoutCompleted(); |
| 275 | } |
| 276 | }; |
| 277 | |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 278 | SpanSizeLookup lookup = mAdapter.createSpanSizeLookup(); |
| 279 | if (lookup != null) { |
| 280 | mLayout.setSpanSizeLookup(lookup); |
| 281 | } |
| 282 | mRecView.setLayoutManager(mLayout); |
| 283 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 284 | // TODO: instead of inserting the view into the constructor, extract listener-creation code |
| 285 | // and set the listener on the view after the fact. Then the view doesn't need to be passed |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 286 | // into the selection manager. |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 287 | mSelectionMgr = new MultiSelectManager( |
Steve McKay | e45d1f0 | 2016-01-05 15:27:17 -0800 | [diff] [blame] | 288 | mAdapter, |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 289 | state.allowMultiple |
| 290 | ? MultiSelectManager.MODE_MULTIPLE |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 291 | : MultiSelectManager.MODE_SINGLE, |
| 292 | this::canSetSelectionState); |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 293 | mSelectionMetadata = new SelectionMetadata(mModel::getItem); |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 294 | mSelectionMgr.addItemCallback(mSelectionMetadata); |
Steve McKay | 79a6fe0 | 2016-06-30 12:09:34 -0700 | [diff] [blame] | 295 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 296 | mModel.addUpdateListener(mAdapter.getModelUpdateListener()); |
Ben Lin | 26819d7 | 2016-08-17 12:45:05 -0700 | [diff] [blame] | 297 | mModel.addUpdateListener(mModelUpdateListener); |
| 298 | |
Ben Lin | 4272880 | 2016-08-19 16:07:39 -0700 | [diff] [blame] | 299 | final int edgeHeight = (int) getResources().getDimension(R.dimen.autoscroll_edge_height); |
| 300 | GestureSelector gestureSel = GestureSelector.create( |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 301 | edgeHeight, mSelectionMgr, mRecView); |
Ben Lin | 4272880 | 2016-08-19 16:07:39 -0700 | [diff] [blame] | 302 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 303 | final BaseActivity activity = getBaseActivity(); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 304 | mTuner = activity.getFragmentTuner(mModel, mConfig.mSearchMode); |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 305 | mFocusManager = activity.getFocusManager(mRecView, mModel); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 306 | mActionHandler = activity.getActionHandler(mModel, mSelectionMgr); |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 307 | mMenuManager = activity.getMenuManager(); |
Steve McKay | f0fceb4 | 2016-09-07 17:35:55 -0700 | [diff] [blame] | 308 | |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 309 | if (state.allowMultiple) { |
| 310 | mBandController = new BandController(mRecView, mAdapter, mSelectionMgr); |
| 311 | } |
| 312 | |
Steve McKay | f0fceb4 | 2016-09-07 17:35:55 -0700 | [diff] [blame] | 313 | DragStartListener mDragStartListener = mTuner.dragAndDropEnabled() |
| 314 | ? DragStartListener.create( |
| 315 | mIconHelper, |
| 316 | getContext(), |
| 317 | mModel, |
| 318 | mSelectionMgr, |
| 319 | mClipper, |
| 320 | getDisplayState(), |
| 321 | this::getModelId, |
| 322 | mRecView::findChildViewUnder, |
| 323 | getContext().getDrawable(com.android.internal.R.drawable.ic_doc_generic)) |
| 324 | : DragStartListener.DUMMY; |
| 325 | |
Ben Lin | 2d6caf3 | 2016-09-19 17:48:54 -0700 | [diff] [blame] | 326 | EventHandler<InputEvent> gestureHandler = state.allowMultiple |
| 327 | ? gestureSel::start |
| 328 | : EventHandler.createStub(false); |
Steve McKay | 2d1e4a3 | 2016-07-13 13:03:16 -0700 | [diff] [blame] | 329 | mInputHandler = new UserInputHandler<>( |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 330 | mActionHandler, |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 331 | mFocusManager, |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame^] | 332 | mSelectionMgr, |
Garfield Tan | e6d61e3 | 2016-09-08 10:26:59 -0700 | [diff] [blame] | 333 | (MotionEvent t) -> MotionInputEvent.obtain(t, mRecView), |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 334 | this::canSelect, |
| 335 | this::onRightClick, |
Garfield Tan | e6d61e3 | 2016-09-08 10:26:59 -0700 | [diff] [blame] | 336 | (DocumentDetails ignored) -> onDeleteSelectedDocuments(), // delete handler |
Steve McKay | f0fceb4 | 2016-09-07 17:35:55 -0700 | [diff] [blame] | 337 | mDragStartListener::onTouchDragEvent, |
Ben Lin | 2d6caf3 | 2016-09-19 17:48:54 -0700 | [diff] [blame] | 338 | gestureHandler); |
Steve McKay | 79a6fe0 | 2016-06-30 12:09:34 -0700 | [diff] [blame] | 339 | |
Garfield Tan | e6d61e3 | 2016-09-08 10:26:59 -0700 | [diff] [blame] | 340 | new ListeningGestureDetector( |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 341 | this.getContext(), |
| 342 | mRecView, |
| 343 | mEmptyView, |
Steve McKay | f0fceb4 | 2016-09-07 17:35:55 -0700 | [diff] [blame] | 344 | mDragStartListener::onMouseDragEvent, |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 345 | gestureSel, |
| 346 | mInputHandler, |
| 347 | mBandController); |
Steve McKay | d586959 | 2016-05-26 12:04:43 -0700 | [diff] [blame] | 348 | |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 349 | mMenuManager = activity.getMenuManager(); |
| 350 | |
| 351 | mActionModeController = ActionModeController.create( |
| 352 | getContext(), |
| 353 | mSelectionMgr, |
| 354 | mMenuManager, |
| 355 | mSelectionMetadata, |
| 356 | getActivity(), |
| 357 | mRecView, |
| 358 | this::handleMenuItemClick); |
| 359 | mSelectionMgr.addCallback(mActionModeController); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 360 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 361 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 362 | Context.ACTIVITY_SERVICE); |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 363 | boolean svelte = am.isLowRamDevice() && (mConfig.mType == TYPE_RECENT_OPEN); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 364 | mIconHelper.setThumbnailsEnabled(!svelte); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 365 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 366 | // If mDocument is null, we sort it by last modified by default because it's in Recents. |
| 367 | final boolean prefersLastModified = |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 368 | (mConfig.mDocument != null) |
| 369 | ? (mConfig.mDocument.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0 |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 370 | : true; |
| 371 | // Call this before adding the listener to avoid restarting the loader one more time |
| 372 | state.sortModel.setDefaultDimension( |
| 373 | prefersLastModified |
| 374 | ? SortModel.SORT_DIMENSION_ID_DATE |
| 375 | : SortModel.SORT_DIMENSION_ID_TITLE); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 376 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 377 | // Kick off loader at least once |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 378 | getLoaderManager().restartLoader(LOADER_ID, null, this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 381 | @Override |
| 382 | public void onStart() { |
| 383 | super.onStart(); |
| 384 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 385 | // Add listener to update contents on sort model change |
| 386 | getDisplayState().sortModel.addListener(mSortListener); |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void onStop() { |
| 391 | super.onStop(); |
| 392 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 393 | getDisplayState().sortModel.removeListener(mSortListener); |
| 394 | |
| 395 | // Remember last scroll location |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 396 | final SparseArray<Parcelable> container = new SparseArray<>(); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 397 | getView().saveHierarchyState(container); |
| 398 | final State state = getDisplayState(); |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 399 | state.dirConfigs.put(mConfig.getConfigKey(), container); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 402 | public void retainState(RetainedState state) { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 403 | state.selection = mSelectionMgr.getSelection(new Selection()); |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 406 | @Override |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 407 | public void onSaveInstanceState(Bundle outState) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 408 | super.onSaveInstanceState(outState); |
| 409 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 410 | mConfig.save(outState); |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | @Override |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 414 | public void onActivityResult(@RequestCode int requestCode, int resultCode, Intent data) { |
Tomasz Mikolajewski | a468d34 | 2016-04-07 11:12:07 +0900 | [diff] [blame] | 415 | switch (requestCode) { |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 416 | case REQUEST_COPY_DESTINATION: |
| 417 | handleCopyResult(resultCode, data); |
| 418 | break; |
| 419 | default: |
| 420 | throw new UnsupportedOperationException("Unknown request code: " + requestCode); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 421 | } |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 424 | @Override |
| 425 | public void onCreateContextMenu(ContextMenu menu, |
| 426 | View v, |
| 427 | ContextMenu.ContextMenuInfo menuInfo) { |
| 428 | super.onCreateContextMenu(menu, v, menuInfo); |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 429 | final MenuInflater inflater = getActivity().getMenuInflater(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 430 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 431 | final String modelId = getModelId(v); |
| 432 | if (modelId == null) { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 433 | // TODO: inject DirectoryDetails into MenuManager constructor |
| 434 | // Since both classes are supplied by Activity and created |
| 435 | // at the same time. |
Steve McKay | e5d6ca2 | 2016-09-19 20:27:24 -0700 | [diff] [blame] | 436 | mMenuManager.inflateContextMenuForContainer(menu, inflater); |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 437 | } else { |
| 438 | mMenuManager.inflateContextMenuForDocs(menu, inflater, mSelectionMetadata); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public boolean onContextItemSelected(MenuItem item) { |
| 444 | return handleMenuItemClick(item); |
| 445 | } |
| 446 | |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 447 | private void handleCopyResult(int resultCode, Intent data) { |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 448 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 449 | FileOperation operation = mConfig.claimPendingOperation(); |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 450 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 451 | if (resultCode == Activity.RESULT_CANCELED || data == null) { |
| 452 | // User pressed the back button or otherwise cancelled the destination pick. Don't |
| 453 | // proceed with the copy. |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 454 | operation.dispose(); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 458 | operation.setDestination(data.getParcelableExtra(Shared.EXTRA_STACK)); |
| 459 | |
Garfield, Tan | a5588b6 | 2016-07-13 09:23:04 -0700 | [diff] [blame] | 460 | BaseActivity activity = getBaseActivity(); |
| 461 | FileOperations.start(activity, operation, activity.fileOpCallback); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 464 | protected boolean onRightClick(InputEvent e) { |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 465 | final View v; |
| 466 | final float x, y; |
Steve McKay | 1597e94 | 2016-09-09 11:54:05 -0700 | [diff] [blame] | 467 | if (e.isOverModelItem()) { |
| 468 | DocumentHolder doc = (DocumentHolder) e.getDocumentDetails(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 469 | |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 470 | v = doc.itemView; |
| 471 | x = e.getX() - v.getLeft(); |
| 472 | y = e.getY() - v.getTop(); |
| 473 | } else { |
| 474 | v = (mEmptyView.getVisibility() == View.VISIBLE) |
| 475 | ? mEmptyView |
| 476 | : mRecView; |
| 477 | x = e.getX(); |
| 478 | y = e.getY(); |
Steve McKay | 79a6fe0 | 2016-06-30 12:09:34 -0700 | [diff] [blame] | 479 | } |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 480 | |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 481 | mMenuManager.showContextMenu(this, v, x, y); |
Steve McKay | 1597e94 | 2016-09-09 11:54:05 -0700 | [diff] [blame] | 482 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 483 | return true; |
| 484 | } |
| 485 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 486 | public void onViewModeChanged() { |
| 487 | // Mode change is just visual change; no need to kick loader. |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 488 | updateDisplayState(); |
| 489 | } |
| 490 | |
| 491 | private void updateDisplayState() { |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 492 | State state = getDisplayState(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 493 | updateLayout(state.derivedMode); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 494 | mRecView.setAdapter(mAdapter); |
| 495 | } |
| 496 | |
| 497 | /** |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 498 | * Updates the layout after the view mode switches. |
| 499 | * @param mode The new view mode. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 500 | */ |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 501 | private void updateLayout(@ViewMode int mode) { |
| 502 | mColumnCount = calculateColumnCount(mode); |
| 503 | if (mLayout != null) { |
| 504 | mLayout.setSpanCount(mColumnCount); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 505 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 506 | |
Steve McKay | ac155ab | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 507 | int pad = getDirectoryPadding(mode); |
| 508 | mRecView.setPadding(pad, pad, pad, pad); |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 509 | mRecView.requestLayout(); |
Steve McKay | d586959 | 2016-05-26 12:04:43 -0700 | [diff] [blame] | 510 | if (mBandController != null) { |
| 511 | mBandController.handleLayoutChanged(); |
| 512 | } |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 513 | mIconHelper.setViewMode(mode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 516 | private int calculateColumnCount(@ViewMode int mode) { |
| 517 | if (mode == MODE_LIST) { |
| 518 | // List mode is a "grid" with 1 column. |
| 519 | return 1; |
| 520 | } |
| 521 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 522 | int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width); |
| 523 | int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin); |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 524 | int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight(); |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 525 | |
Ben Kwa | 2e3d418 | 2016-03-16 13:01:20 -0700 | [diff] [blame] | 526 | // RecyclerView sometimes gets a width of 0 (see b/27150284). Clamp so that we always lay |
| 527 | // out the grid with at least 2 columns. |
| 528 | int columnCount = Math.max(2, |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 529 | (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin)); |
| 530 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 531 | return columnCount; |
| 532 | } |
| 533 | |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 534 | private int getDirectoryPadding(@ViewMode int mode) { |
Steve McKay | ac155ab | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 535 | switch (mode) { |
| 536 | case MODE_GRID: |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 537 | return getResources().getDimensionPixelSize(R.dimen.grid_container_padding); |
Steve McKay | ac155ab | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 538 | case MODE_LIST: |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 539 | return getResources().getDimensionPixelSize(R.dimen.list_container_padding); |
Steve McKay | ac155ab | 2016-01-12 11:14:33 -0800 | [diff] [blame] | 540 | default: |
| 541 | throw new IllegalArgumentException("Unsupported layout mode: " + mode); |
| 542 | } |
| 543 | } |
| 544 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 545 | @Override |
| 546 | public int getColumnCount() { |
| 547 | return mColumnCount; |
| 548 | } |
| 549 | |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 550 | // Support method to replace getOwner().foo() with something |
| 551 | // slightly less clumsy like: getOwner().foo(). |
| 552 | private BaseActivity getBaseActivity() { |
| 553 | return (BaseActivity) getActivity(); |
| 554 | } |
| 555 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 556 | private boolean handleMenuItemClick(MenuItem item) { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 557 | Selection selection = mSelectionMgr.getSelection(new Selection()); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 558 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 559 | switch (item.getItemId()) { |
| 560 | case R.id.menu_open: |
| 561 | openDocuments(selection); |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 562 | mActionModeController.finishActionMode(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 563 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 564 | |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 565 | case R.id.menu_open_with: |
| 566 | showChooserForDoc(selection); |
| 567 | return true; |
| 568 | |
| 569 | case R.id.menu_open_in_new_window: |
| 570 | openInNewWindow(selection); |
| 571 | return true; |
| 572 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 573 | case R.id.menu_share: |
| 574 | shareDocuments(selection); |
| 575 | // TODO: Only finish selection if share action is completed. |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 576 | mActionModeController.finishActionMode(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 577 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 578 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 579 | case R.id.menu_delete: |
| 580 | // deleteDocuments will end action mode if the documents are deleted. |
| 581 | // It won't end action mode if user cancels the delete. |
| 582 | deleteDocuments(selection); |
| 583 | return true; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 584 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 585 | case R.id.menu_copy_to: |
| 586 | transferDocuments(selection, FileOperationService.OPERATION_COPY); |
| 587 | // TODO: Only finish selection mode if copy-to is not canceled. |
| 588 | // Need to plum down into handling the way we do with deleteDocuments. |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 589 | mActionModeController.finishActionMode(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 590 | return true; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 591 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 592 | case R.id.menu_move_to: |
| 593 | // Exit selection mode first, so we avoid deselecting deleted documents. |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 594 | mActionModeController.finishActionMode(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 595 | transferDocuments(selection, FileOperationService.OPERATION_MOVE); |
| 596 | return true; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 597 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 598 | case R.id.menu_cut_to_clipboard: |
| 599 | cutSelectedToClipboard(); |
| 600 | return true; |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 601 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 602 | case R.id.menu_copy_to_clipboard: |
| 603 | copySelectedToClipboard(); |
| 604 | return true; |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 605 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 606 | case R.id.menu_paste_from_clipboard: |
| 607 | pasteFromClipboard(); |
| 608 | return true; |
| 609 | |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 610 | case R.id.menu_paste_into_folder: |
| 611 | pasteIntoFolder(); |
| 612 | return true; |
| 613 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 614 | case R.id.menu_select_all: |
| 615 | selectAllFiles(); |
| 616 | return true; |
| 617 | |
| 618 | case R.id.menu_rename: |
| 619 | // Exit selection mode first, so we avoid deselecting deleted |
| 620 | // (renamed) documents. |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 621 | mActionModeController.finishActionMode(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 622 | renameDocuments(selection); |
| 623 | return true; |
| 624 | |
| 625 | default: |
| 626 | // See if BaseActivity can handle this particular MenuItem |
| 627 | if (!getBaseActivity().onOptionsItemSelected(item)) { |
Steve McKay | 3abb0e3 | 2015-12-01 17:02:42 -0800 | [diff] [blame] | 628 | if (DEBUG) Log.d(TAG, "Unhandled menu item selected: " + item); |
| 629 | return false; |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 630 | } |
| 631 | return true; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 632 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 633 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 634 | |
Steve McKay | 86c0576 | 2016-01-28 15:30:10 -0800 | [diff] [blame] | 635 | public final boolean onBackPressed() { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 636 | if (mSelectionMgr.hasSelection()) { |
Steve McKay | b8fd884 | 2016-05-06 12:07:54 -0700 | [diff] [blame] | 637 | if (DEBUG) Log.d(TAG, "Clearing selection on selection manager."); |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 638 | mSelectionMgr.clearSelection(); |
Steve McKay | 86c0576 | 2016-01-28 15:30:10 -0800 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 644 | private void cancelThumbnailTask(View view) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 645 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 646 | if (iconThumb != null) { |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 647 | mIconHelper.stopLoading(iconThumb); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 648 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 649 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 650 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 651 | // Support for opening multiple documents is currently exclusive to DocumentsActivity. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 652 | private void openDocuments(final Selection selected) { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 653 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_OPEN); |
| 654 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 655 | // Model must be accessed in UI thread, since underlying cursor is not threadsafe. |
| 656 | List<DocumentInfo> docs = mModel.getDocuments(selected); |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 657 | BaseActivity activity = getBaseActivity(); |
| 658 | if (docs.size() > 1) { |
| 659 | activity.onDocumentsPicked(docs); |
| 660 | } else { |
| 661 | activity.onDocumentPicked(docs.get(0), mModel); |
| 662 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 665 | private void showChooserForDoc(final Selection selected) { |
| 666 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_OPEN); |
| 667 | |
| 668 | assert(selected.size() == 1); |
| 669 | DocumentInfo doc = |
| 670 | DocumentInfo.fromDirectoryCursor(mModel.getItem(selected.iterator().next())); |
| 671 | mTuner.showChooserForDoc(doc); |
| 672 | } |
| 673 | |
| 674 | private void openInNewWindow(final Selection selected) { |
| 675 | assert(selected.size() == 1); |
| 676 | DocumentInfo doc = |
| 677 | DocumentInfo.fromDirectoryCursor(mModel.getItem(selected.iterator().next())); |
| 678 | mTuner.openInNewWindow(getDisplayState().stack, doc); |
| 679 | } |
| 680 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 681 | private void shareDocuments(final Selection selected) { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 682 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_SHARE); |
| 683 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 684 | // Model must be accessed in UI thread, since underlying cursor is not threadsafe. |
| 685 | List<DocumentInfo> docs = mModel.getDocuments(selected); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 686 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 687 | Intent intent; |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 688 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 689 | // Filter out directories and virtual files - those can't be shared. |
| 690 | List<DocumentInfo> docsForSend = new ArrayList<>(); |
| 691 | for (DocumentInfo doc: docs) { |
| 692 | if (!doc.isDirectory() && !doc.isVirtualDocument()) { |
| 693 | docsForSend.add(doc); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 694 | } |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | if (docsForSend.size() == 1) { |
| 698 | final DocumentInfo doc = docsForSend.get(0); |
| 699 | |
| 700 | intent = new Intent(Intent.ACTION_SEND); |
| 701 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 702 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 703 | intent.setType(doc.mimeType); |
| 704 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
| 705 | |
| 706 | } else if (docsForSend.size() > 1) { |
| 707 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 708 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 709 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 710 | |
| 711 | final ArrayList<String> mimeTypes = new ArrayList<>(); |
| 712 | final ArrayList<Uri> uris = new ArrayList<>(); |
| 713 | for (DocumentInfo doc : docsForSend) { |
| 714 | mimeTypes.add(doc.mimeType); |
| 715 | uris.add(doc.derivedUri); |
| 716 | } |
| 717 | |
| 718 | intent.setType(findCommonMimeType(mimeTypes)); |
| 719 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 720 | |
| 721 | } else { |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
| 726 | startActivity(intent); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Aga Wronska | 57ab046 | 2016-03-17 13:45:32 -0700 | [diff] [blame] | 729 | private String generateDeleteMessage(final List<DocumentInfo> docs) { |
| 730 | String message; |
| 731 | int dirsCount = 0; |
| 732 | |
| 733 | for (DocumentInfo doc : docs) { |
| 734 | if (doc.isDirectory()) { |
| 735 | ++dirsCount; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | if (docs.size() == 1) { |
| 740 | // Deleteing 1 file xor 1 folder in cwd |
Steve McKay | 927d800 | 2016-05-18 16:25:41 -0700 | [diff] [blame] | 741 | |
| 742 | // Address b/28772371, where including user strings in message can result in |
| 743 | // broken bidirectional support. |
| 744 | String displayName = BidiFormatter.getInstance().unicodeWrap(docs.get(0).displayName); |
Aga Wronska | 57ab046 | 2016-03-17 13:45:32 -0700 | [diff] [blame] | 745 | message = dirsCount == 0 |
| 746 | ? getActivity().getString(R.string.delete_filename_confirmation_message, |
Steve McKay | 927d800 | 2016-05-18 16:25:41 -0700 | [diff] [blame] | 747 | displayName) |
Aga Wronska | 57ab046 | 2016-03-17 13:45:32 -0700 | [diff] [blame] | 748 | : getActivity().getString(R.string.delete_foldername_confirmation_message, |
Steve McKay | 927d800 | 2016-05-18 16:25:41 -0700 | [diff] [blame] | 749 | displayName); |
Aga Wronska | 57ab046 | 2016-03-17 13:45:32 -0700 | [diff] [blame] | 750 | } else if (dirsCount == 0) { |
| 751 | // Deleting only files in cwd |
| 752 | message = Shared.getQuantityString(getActivity(), |
| 753 | R.plurals.delete_files_confirmation_message, docs.size()); |
| 754 | } else if (dirsCount == docs.size()) { |
| 755 | // Deleting only folders in cwd |
| 756 | message = Shared.getQuantityString(getActivity(), |
| 757 | R.plurals.delete_folders_confirmation_message, docs.size()); |
| 758 | } else { |
| 759 | // Deleting mixed items (files and folders) in cwd |
| 760 | message = Shared.getQuantityString(getActivity(), |
| 761 | R.plurals.delete_items_confirmation_message, docs.size()); |
| 762 | } |
| 763 | return message; |
| 764 | } |
| 765 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 766 | private boolean onDeleteSelectedDocuments() { |
| 767 | if (mSelectionMgr.hasSelection()) { |
| 768 | deleteDocuments(mSelectionMgr.getSelection(new Selection())); |
| 769 | } |
| 770 | return false; |
| 771 | } |
| 772 | |
Ben Kwa | 68f4bfb | 2016-03-14 15:12:12 -0700 | [diff] [blame] | 773 | private void deleteDocuments(final Selection selected) { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 774 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_DELETE); |
| 775 | |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 776 | assert(!selected.isEmpty()); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 777 | |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 778 | final DocumentInfo srcParent = getDisplayState().stack.peek(); |
Steve McKay | f63e938 | 2016-03-01 08:28:02 -0800 | [diff] [blame] | 779 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 780 | // Model must be accessed in UI thread, since underlying cursor is not threadsafe. |
| 781 | List<DocumentInfo> docs = mModel.getDocuments(selected); |
Steve McKay | f63e938 | 2016-03-01 08:28:02 -0800 | [diff] [blame] | 782 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 783 | TextView message = |
| 784 | (TextView) mInflater.inflate(R.layout.dialog_delete_confirmation, null); |
| 785 | message.setText(generateDeleteMessage(docs)); |
Garfield, Tan | 810fb82 | 2016-06-01 11:08:12 -0700 | [diff] [blame] | 786 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 787 | // For now, we implement this dialog NOT |
| 788 | // as a fragment (which can survive rotation and have its own state), |
| 789 | // but as a simple runtime dialog. So rotating a device with an |
| 790 | // active delete dialog...results in that dialog disappearing. |
| 791 | // We can do better, but don't have cycles for it now. |
| 792 | new AlertDialog.Builder(getActivity()) |
| 793 | .setView(message) |
| 794 | .setPositiveButton( |
| 795 | android.R.string.ok, |
| 796 | new DialogInterface.OnClickListener() { |
| 797 | @Override |
| 798 | public void onClick(DialogInterface dialog, int id) { |
| 799 | // Finish selection mode first which clears selection so we |
| 800 | // don't end up trying to deselect deleted documents. |
| 801 | // This is done here, rather in the onActionItemClicked |
| 802 | // so we can avoid de-selecting items in the case where |
| 803 | // the user cancels the delete. |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 804 | mActionModeController.finishActionMode(); |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 805 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 806 | UrisSupplier srcs; |
| 807 | try { |
| 808 | srcs = UrisSupplier.create( |
| 809 | selected, |
| 810 | mModel::getItemUri, |
| 811 | getContext()); |
| 812 | } catch(IOException e) { |
| 813 | throw new RuntimeException("Failed to create uri supplier.", e); |
| 814 | } |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 815 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 816 | FileOperation operation = new FileOperation.Builder() |
| 817 | .withOpType(FileOperationService.OPERATION_DELETE) |
| 818 | .withDestination(getDisplayState().stack) |
| 819 | .withSrcs(srcs) |
| 820 | .withSrcParent(srcParent.derivedUri) |
| 821 | .build(); |
| 822 | |
| 823 | BaseActivity activity = getBaseActivity(); |
| 824 | FileOperations.start(activity, operation, activity.fileOpCallback); |
| 825 | } |
| 826 | }) |
| 827 | .setNegativeButton(android.R.string.cancel, null) |
| 828 | .show(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 831 | private void transferDocuments(final Selection selected, final @OpType int mode) { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 832 | if(mode == FileOperationService.OPERATION_COPY) { |
| 833 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_COPY_TO); |
| 834 | } else if (mode == FileOperationService.OPERATION_MOVE) { |
| 835 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_MOVE_TO); |
| 836 | } |
| 837 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 838 | // Pop up a dialog to pick a destination. This is inadequate but works for now. |
| 839 | // TODO: Implement a picker that is to spec. |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 840 | final Intent intent = new Intent( |
Ben Kwa | ae96780 | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 841 | Shared.ACTION_PICK_COPY_DESTINATION, |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 842 | Uri.EMPTY, |
| 843 | getActivity(), |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 844 | PickActivity.class); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 845 | |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 846 | UrisSupplier srcs; |
| 847 | try { |
| 848 | srcs = UrisSupplier.create(selected, mModel::getItemUri, getContext()); |
| 849 | } catch(IOException e) { |
| 850 | throw new RuntimeException("Failed to create uri supplier.", e); |
| 851 | } |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 852 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 853 | Uri srcParent = getDisplayState().stack.peek().derivedUri; |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 854 | mConfig.mPendingOperation = new FileOperation.Builder() |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 855 | .withOpType(mode) |
| 856 | .withSrcParent(srcParent) |
| 857 | .withSrcs(srcs) |
| 858 | .build(); |
Steve McKay | 1abf6cf | 2016-04-08 17:34:11 -0700 | [diff] [blame] | 859 | |
| 860 | // Relay any config overrides bits present in the original intent. |
| 861 | Intent original = getActivity().getIntent(); |
| 862 | if (original != null && original.hasExtra(Shared.EXTRA_PRODUCTIVITY_MODE)) { |
| 863 | intent.putExtra( |
| 864 | Shared.EXTRA_PRODUCTIVITY_MODE, |
| 865 | original.getBooleanExtra(Shared.EXTRA_PRODUCTIVITY_MODE, false)); |
| 866 | } |
| 867 | |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 868 | // Set an appropriate title on the drawer when it is shown in the picker. |
| 869 | // Coupled with the fact that we auto-open the drawer for copy/move operations |
| 870 | // it should basically be the thing people see first. |
| 871 | int drawerTitleId = mode == FileOperationService.OPERATION_MOVE |
| 872 | ? R.string.menu_move : R.string.menu_copy; |
| 873 | intent.putExtra(DocumentsContract.EXTRA_PROMPT, getResources().getString(drawerTitleId)); |
| 874 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 875 | // Model must be accessed in UI thread, since underlying cursor is not threadsafe. |
| 876 | List<DocumentInfo> docs = mModel.getDocuments(selected); |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 877 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 878 | // Determine if there is a directory in the set of documents |
| 879 | // to be copied? Why? Directory creation isn't supported by some roots |
| 880 | // (like Downloads). This informs DocumentsActivity (the "picker") |
| 881 | // to restrict available roots to just those with support. |
| 882 | intent.putExtra(Shared.EXTRA_DIRECTORY_COPY, hasDirectory(docs)); |
| 883 | intent.putExtra(FileOperationService.EXTRA_OPERATION_TYPE, mode); |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 884 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 885 | // This just identifies the type of request...we'll check it |
| 886 | // when we reveive a response. |
| 887 | startActivityForResult(intent, REQUEST_COPY_DESTINATION); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 890 | private static boolean hasDirectory(List<DocumentInfo> docs) { |
| 891 | for (DocumentInfo info : docs) { |
| 892 | if (Document.MIME_TYPE_DIR.equals(info.mimeType)) { |
| 893 | return true; |
| 894 | } |
| 895 | } |
| 896 | return false; |
| 897 | } |
| 898 | |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 899 | private void renameDocuments(Selection selected) { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 900 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_RENAME); |
| 901 | |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 902 | // Batch renaming not supported |
| 903 | // Rename option is only available in menu when 1 document selected |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 904 | assert(selected.size() == 1); |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 905 | |
Steve McKay | f61f93a | 2016-09-01 15:39:18 -0700 | [diff] [blame] | 906 | // Model must be accessed in UI thread, since underlying cursor is not threadsafe. |
| 907 | List<DocumentInfo> docs = mModel.getDocuments(selected); |
| 908 | RenameDocumentFragment.show(getFragmentManager(), docs.get(0)); |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 911 | @Override |
| 912 | public void initDocumentHolder(DocumentHolder holder) { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 913 | holder.addKeyEventListener(mInputHandler); |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 914 | holder.itemView.setOnFocusChangeListener(mFocusManager); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | @Override |
| 918 | public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) { |
Ben Kwa | a8c0da4 | 2016-02-25 14:10:40 -0800 | [diff] [blame] | 919 | setupDragAndDropOnDocumentView(holder.itemView, cursor); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | @Override |
| 923 | public State getDisplayState() { |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 924 | return getBaseActivity().getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 927 | @Override |
| 928 | public Model getModel() { |
| 929 | return mModel; |
| 930 | } |
| 931 | |
| 932 | @Override |
| 933 | public boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 934 | return mTuner.isDocumentEnabled(docMimeType, docFlags); |
| 935 | } |
| 936 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 937 | private void showEmptyDirectory() { |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 938 | showEmptyView(R.string.empty, R.drawable.cabinet); |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 941 | private void showNoResults(RootInfo root) { |
| 942 | CharSequence msg = getContext().getResources().getText(R.string.no_results); |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 943 | showEmptyView(String.format(String.valueOf(msg), root.title), R.drawable.cabinet); |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 946 | private void showQueryError() { |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 947 | showEmptyView(R.string.query_error, R.drawable.hourglass); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 950 | private void showEmptyView(@StringRes int id, int drawable) { |
| 951 | showEmptyView(getContext().getResources().getText(id), drawable); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 954 | private void showEmptyView(CharSequence msg, int drawable) { |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 955 | View content = mEmptyView.findViewById(R.id.content); |
| 956 | TextView msgView = (TextView) mEmptyView.findViewById(R.id.message); |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 957 | ImageView imageView = (ImageView) mEmptyView.findViewById(R.id.artwork); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 958 | msgView.setText(msg); |
Ben Kwa | 80351c9 | 2016-02-04 16:35:27 -0800 | [diff] [blame] | 959 | imageView.setImageResource(drawable); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 960 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 961 | mEmptyView.setVisibility(View.VISIBLE); |
Aga Wronska | 9406c17 | 2016-02-12 09:15:32 -0800 | [diff] [blame] | 962 | mEmptyView.requestFocus(); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 963 | mFileList.setVisibility(View.GONE); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | private void showDirectory() { |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 967 | mEmptyView.setVisibility(View.GONE); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 968 | mFileList.setVisibility(View.VISIBLE); |
Aga Wronska | 9406c17 | 2016-02-12 09:15:32 -0800 | [diff] [blame] | 969 | mRecView.requestFocus(); |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 972 | private String findCommonMimeType(List<String> mimeTypes) { |
| 973 | String[] commonType = mimeTypes.get(0).split("/"); |
| 974 | if (commonType.length != 2) { |
| 975 | return "*/*"; |
| 976 | } |
| 977 | |
| 978 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 979 | String[] type = mimeTypes.get(i).split("/"); |
| 980 | if (type.length != 2) continue; |
| 981 | |
| 982 | if (!commonType[1].equals(type[1])) { |
| 983 | commonType[1] = "*"; |
| 984 | } |
| 985 | |
| 986 | if (!commonType[0].equals(type[0])) { |
| 987 | commonType[0] = "*"; |
| 988 | commonType[1] = "*"; |
| 989 | break; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | return commonType[0] + "/" + commonType[1]; |
| 994 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 995 | |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 996 | public void copySelectedToClipboard() { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 997 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_COPY_CLIPBOARD); |
| 998 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 999 | Selection selection = mSelectionMgr.getSelection(new Selection()); |
Ben Lin | ff4d584 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 1000 | if (selection.isEmpty()) { |
| 1001 | return; |
Steve McKay | e11ef24 | 2015-12-07 16:31:42 -0800 | [diff] [blame] | 1002 | } |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1003 | mSelectionMgr.clearSelection(); |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 1004 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 1005 | mClipper.clipDocumentsForCopy(mModel::getItemUri, selection); |
| 1006 | |
| 1007 | Snackbars.showDocumentsClipped(getActivity(), selection.size()); |
Ben Lin | ff4d584 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | public void cutSelectedToClipboard() { |
| 1011 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_CUT_CLIPBOARD); |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 1012 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1013 | Selection selection = mSelectionMgr.getSelection(new Selection()); |
Ben Lin | ff4d584 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 1014 | if (selection.isEmpty()) { |
| 1015 | return; |
| 1016 | } |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1017 | mSelectionMgr.clearSelection(); |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 1018 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 1019 | mClipper.clipDocumentsForCut(mModel::getItemUri, selection, getDisplayState().stack.peek()); |
| 1020 | |
| 1021 | Snackbars.showDocumentsClipped(getActivity(), selection.size()); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 1024 | public void pasteFromClipboard() { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 1025 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_PASTE_CLIPBOARD); |
| 1026 | |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 1027 | BaseActivity activity = (BaseActivity) getActivity(); |
| 1028 | DocumentInfo destination = activity.getCurrentDirectory(); |
Garfield, Tan | a5588b6 | 2016-07-13 09:23:04 -0700 | [diff] [blame] | 1029 | mClipper.copyFromClipboard( |
| 1030 | destination, activity.getDisplayState().stack, activity.fileOpCallback); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1031 | getActivity().invalidateOptionsMenu(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 1034 | public void pasteIntoFolder() { |
| 1035 | assert (mSelectionMgr.getSelection().size() == 1); |
| 1036 | |
| 1037 | String modelId = mSelectionMgr.getSelection().iterator().next(); |
| 1038 | Cursor dstCursor = mModel.getItem(modelId); |
| 1039 | if (dstCursor == null) { |
| 1040 | Log.w(TAG, "Invalid destination. Can't obtain cursor for modelId: " + modelId); |
| 1041 | return; |
| 1042 | } |
| 1043 | BaseActivity activity = getBaseActivity(); |
| 1044 | DocumentInfo destination = DocumentInfo.fromDirectoryCursor(dstCursor); |
| 1045 | mClipper.copyFromClipboard( |
| 1046 | destination, activity.getDisplayState().stack, activity.fileOpCallback); |
| 1047 | getActivity().invalidateOptionsMenu(); |
| 1048 | } |
| 1049 | |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 1050 | public void selectAllFiles() { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 1051 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_SELECT_ALL); |
| 1052 | |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 1053 | // Exclude disabled files |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 1054 | List<String> enabled = new ArrayList<>(); |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 1055 | for (String id : mAdapter.getModelIds()) { |
Aga Wronska | 87abbcd | 2016-04-01 13:16:11 -0700 | [diff] [blame] | 1056 | Cursor cursor = getModel().getItem(id); |
Tomasz Mikolajewski | 7773cba | 2016-04-13 16:18:15 +0900 | [diff] [blame] | 1057 | if (cursor == null) { |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 1058 | Log.w(TAG, "Skipping selection. Can't obtain cursor for modeId: " + id); |
| 1059 | continue; |
| 1060 | } |
Aga Wronska | 87abbcd | 2016-04-01 13:16:11 -0700 | [diff] [blame] | 1061 | String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1062 | int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 1063 | if (isDocumentEnabled(docMimeType, docFlags)) { |
Aga Wronska | 87abbcd | 2016-04-01 13:16:11 -0700 | [diff] [blame] | 1064 | enabled.add(id); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 1068 | // Only select things currently visible in the adapter. |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1069 | boolean changed = mSelectionMgr.setItemsSelected(enabled, true); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 1070 | if (changed) { |
| 1071 | updateDisplayState(); |
| 1072 | } |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 1075 | /** |
| 1076 | * Attempts to restore focus on the directory listing. |
| 1077 | */ |
| 1078 | public void requestFocus() { |
| 1079 | mFocusManager.restoreLastFocus(); |
| 1080 | } |
| 1081 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1082 | private void setupDragAndDropOnDocumentView(View view, Cursor cursor) { |
| 1083 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1084 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1085 | // Make a directory item a drop target. Drop on non-directories and empty space |
| 1086 | // is handled at the list/grid view level. |
Ben Lin | 35f99e0 | 2016-08-31 12:46:04 -0700 | [diff] [blame] | 1087 | view.setOnDragListener(mDragHoverListener); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1088 | } |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 1091 | void dragStarted() { |
| 1092 | // When files are selected for dragging, ActionMode is started. This obscures the breadcrumb |
| 1093 | // with an ActionBar. In order to make drag and drop to the breadcrumb possible, we first |
| 1094 | // end ActionMode so the breadcrumb is visible to the user. |
Garfield Tan | 58dced4 | 2016-09-15 19:16:04 -0700 | [diff] [blame] | 1095 | // |
| 1096 | // mActionModeController is null when dragStarted() is called on spring loaded |
| 1097 | // folders/roots. |
| 1098 | if (mActionModeController != null) { |
| 1099 | mActionModeController.finishActionMode(); |
| 1100 | } |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void dragStopped(boolean result) { |
| 1104 | if (result) { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1105 | mSelectionMgr.clearSelection(); |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 1106 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1107 | } |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1108 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1109 | @Override |
| 1110 | public void runOnUiThread(Runnable runnable) { |
| 1111 | getActivity().runOnUiThread(runnable); |
| 1112 | } |
Ben Kwa | 13e2605 | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 1113 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1114 | /** |
| 1115 | * {@inheritDoc} |
| 1116 | * |
| 1117 | * In DirectoryFragment, we spring loads the hovered folder. |
| 1118 | */ |
| 1119 | @Override |
| 1120 | public void onViewHovered(View view) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 1121 | BaseActivity activity = (BaseActivity) getActivity(); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1122 | if (getModelId(view) != null) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 1123 | activity.springOpenDirectory(getDestination(view)); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1124 | } |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 1125 | activity.setRootsDrawerOpen(false); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1126 | } |
Ben Kwa | a8c0da4 | 2016-02-25 14:10:40 -0800 | [diff] [blame] | 1127 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 1128 | boolean handleDropEvent(View v, DragEvent event) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 1129 | BaseActivity activity = (BaseActivity) getActivity(); |
| 1130 | activity.setRootsDrawerOpen(false); |
| 1131 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1132 | ClipData clipData = event.getClipData(); |
| 1133 | assert (clipData != null); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1134 | |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 1135 | assert(DocumentClipper.getOpType(clipData) == FileOperationService.OPERATION_COPY); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1136 | |
Ben Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 1137 | if (!shouldCopyTo(event.getLocalState(), v)) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
Ben Kwa | 13e2605 | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 1140 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1141 | // Recognize multi-window drag and drop based on the fact that localState is not |
| 1142 | // carried between processes. It will stop working when the localsState behavior |
| 1143 | // is changed. The info about window should be passed in the localState then. |
| 1144 | // The localState could also be null for copying from Recents in single window |
| 1145 | // mode, but Recents doesn't offer this functionality (no directories). |
| 1146 | Metrics.logUserAction(getContext(), |
Ben Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 1147 | event.getLocalState() == null ? Metrics.USER_ACTION_DRAG_N_DROP_MULTI_WINDOW |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1148 | : Metrics.USER_ACTION_DRAG_N_DROP); |
Ben Lin | ff4d584 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 1149 | |
Ben Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 1150 | DocumentInfo dst = getDestination(v); |
Garfield, Tan | a5588b6 | 2016-07-13 09:23:04 -0700 | [diff] [blame] | 1151 | mClipper.copyFromClipData(dst, getDisplayState().stack, clipData, activity.fileOpCallback); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1152 | return true; |
| 1153 | } |
| 1154 | |
Ben Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 1155 | // Don't copy from the cwd into a provided list of prohibited directories. (ie. into cwd, into |
| 1156 | // a selected directory). Note: this currently doesn't work for multi-window drag, because |
| 1157 | // localState isn't carried over from one process to another. |
| 1158 | boolean shouldCopyTo(Object dragLocalState, View destinationView) { |
| 1159 | if (dragLocalState == null || !(dragLocalState instanceof List<?>)) { |
| 1160 | if (DEBUG) Log.d(TAG, "Invalid local state object. Will allow copy."); |
| 1161 | return true; |
| 1162 | } |
| 1163 | DocumentInfo dst = getDestination(destinationView); |
| 1164 | List<?> src = (List<?>) dragLocalState; |
| 1165 | if (src.contains(dst)) { |
| 1166 | if (DEBUG) Log.d(TAG, "Drop target same as source. Ignoring."); |
| 1167 | return false; |
| 1168 | } |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1172 | private DocumentInfo getDestination(View v) { |
| 1173 | String id = getModelId(v); |
| 1174 | if (id != null) { |
| 1175 | Cursor dstCursor = mModel.getItem(id); |
| 1176 | if (dstCursor == null) { |
| 1177 | Log.w(TAG, "Invalid destination. Can't obtain cursor for modelId: " + id); |
| 1178 | return null; |
Ben Lin | ff4d584 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 1179 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1180 | return DocumentInfo.fromDirectoryCursor(dstCursor); |
Ben Lin | ff4d584 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1183 | if (v == mRecView || v == mEmptyView) { |
| 1184 | return getDisplayState().stack.peek(); |
Ben Kwa | a8c0da4 | 2016-02-25 14:10:40 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1187 | return null; |
| 1188 | } |
| 1189 | |
| 1190 | @Override |
| 1191 | public void setDropTargetHighlight(View v, boolean highlight) { |
| 1192 | // Note: use exact comparison - this code is searching for views which are children of |
| 1193 | // the RecyclerView instance in the UI. |
| 1194 | if (v.getParent() == mRecView) { |
| 1195 | RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(v); |
| 1196 | if (vh instanceof DocumentHolder) { |
| 1197 | ((DocumentHolder) vh).setHighlighted(highlight); |
Ben Kwa | 13e2605 | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 1198 | } |
| 1199 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 1200 | } |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1201 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1202 | /** |
| 1203 | * Gets the model ID for a given RecyclerView item. |
| 1204 | * @param view A View that is a document item view, or a child of a document item view. |
| 1205 | * @return The Model ID for the given document, or null if the given view is not associated with |
| 1206 | * a document item view. |
| 1207 | */ |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 1208 | protected @Nullable String getModelId(View view) { |
Ben Kwa | 13e2605 | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 1209 | View itemView = mRecView.findContainingItemView(view); |
| 1210 | if (itemView != null) { |
| 1211 | RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(itemView); |
| 1212 | if (vh instanceof DocumentHolder) { |
Steve McKay | bde20e1 | 2016-09-08 19:12:54 -0700 | [diff] [blame] | 1213 | return ((DocumentHolder) vh).getModelId(); |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1214 | } |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1215 | } |
Ben Kwa | 13e2605 | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 1216 | return null; |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 1219 | @Override |
| 1220 | public boolean isSelected(String modelId) { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1221 | return mSelectionMgr.getSelection().contains(modelId); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1222 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1223 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 1224 | private final class ModelUpdateListener implements EventListener<Model.Update> { |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 1225 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 1226 | @Override |
| 1227 | public void accept(Model.Update update) { |
| 1228 | if (update.hasError()) { |
| 1229 | showQueryError(); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | if (DEBUG) Log.d(TAG, "Received model update. Loading=" + mModel.isLoading()); |
| 1234 | |
| 1235 | if (mModel.info != null || mModel.error != null) { |
| 1236 | mMessageBar.setInfo(mModel.info); |
| 1237 | mMessageBar.setError(mModel.error); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1238 | mMessageBar.show(); |
| 1239 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 1240 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 1241 | mProgressBar.setVisibility(mModel.isLoading() ? View.VISIBLE : View.GONE); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1242 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 1243 | if (mModel.isEmpty()) { |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1244 | if (mConfig.mSearchMode) { |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1245 | showNoResults(getDisplayState().stack.root); |
| 1246 | } else { |
| 1247 | showEmptyDirectory(); |
| 1248 | } |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1249 | } else { |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1250 | showDirectory(); |
Ben Kwa | 2f97526 | 2015-09-16 13:15:38 -0700 | [diff] [blame] | 1251 | mAdapter.notifyDataSetChanged(); |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1252 | } |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 1253 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 1254 | if (!mModel.isLoading()) { |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 1255 | getBaseActivity().notifyDirectoryLoaded( |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 1256 | mModel.doc != null ? mModel.doc.derivedUri : null); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 1257 | } |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 1258 | } |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1259 | } |
Ben Kwa | 9fe25a7 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 1260 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1261 | private boolean canSelect(DocumentDetails doc) { |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 1262 | return canSetSelectionState(doc.getModelId(), true); |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 1265 | private boolean canSetSelectionState(String modelId, boolean nextState) { |
| 1266 | if (nextState) { |
| 1267 | // Check if an item can be selected |
| 1268 | final Cursor cursor = mModel.getItem(modelId); |
| 1269 | if (cursor == null) { |
| 1270 | Log.w(TAG, "Couldn't obtain cursor for modelId: " + modelId); |
| 1271 | return false; |
| 1272 | } |
Steve McKay | f794489 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 1273 | |
Garfield Tan | 84bd0f1 | 2016-09-12 14:18:32 -0700 | [diff] [blame] | 1274 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1275 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 1276 | return mTuner.canSelectType(docMimeType, docFlags); |
| 1277 | } else { |
| 1278 | // Right now all selected items can be deselected. |
| 1279 | return true; |
Steve McKay | f794489 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 1280 | } |
Steve McKay | f794489 | 2016-01-21 15:10:39 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1283 | public static void showDirectory( |
| 1284 | FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 1285 | if (DEBUG) Log.d(TAG, "Showing directory: " + DocumentInfo.debugString(doc)); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1286 | create(fm, TYPE_NORMAL, root, doc, null, anim); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1290 | create(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1293 | public static void reloadSearch(FragmentManager fm, RootInfo root, DocumentInfo doc, |
| 1294 | String query) { |
| 1295 | DirectoryFragment df = get(fm); |
| 1296 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1297 | df.mConfig.update(root, doc, query); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1298 | df.getLoaderManager().restartLoader(LOADER_ID, null, df); |
| 1299 | } |
| 1300 | |
| 1301 | public static void reload(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 1302 | String query) { |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 1303 | if (DEBUG) Log.d(TAG, "Reloading directory: " + DocumentInfo.debugString(doc)); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1304 | DirectoryFragment df = get(fm); |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1305 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1306 | df.mConfig.update(type, root, doc, query); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1307 | df.getLoaderManager().restartLoader(LOADER_ID, null, df); |
| 1308 | } |
| 1309 | |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 1310 | public static void create( |
| 1311 | FragmentManager fm, |
| 1312 | int type, |
| 1313 | RootInfo root, |
| 1314 | @Nullable DocumentInfo doc, |
| 1315 | String query, |
| 1316 | int anim) { |
| 1317 | |
| 1318 | if (DEBUG) { |
| 1319 | if (doc == null) { |
| 1320 | Log.d(TAG, "Creating new fragment null directory"); |
| 1321 | } else { |
| 1322 | Log.d(TAG, "Creating new fragment for directory: " + DocumentInfo.debugString(doc)); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1326 | final Bundle args = new Bundle(); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1327 | args.putInt(Shared.EXTRA_TYPE, type); |
| 1328 | args.putParcelable(Shared.EXTRA_ROOT, root); |
| 1329 | args.putParcelable(Shared.EXTRA_DOC, doc); |
| 1330 | args.putString(Shared.EXTRA_QUERY, query); |
Aga Wronska | 7e5b963 | 2016-02-26 11:36:07 -0800 | [diff] [blame] | 1331 | args.putParcelable(Shared.EXTRA_SELECTION, new Selection()); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1332 | |
| 1333 | final FragmentTransaction ft = fm.beginTransaction(); |
Steve McKay | fb4fd2f | 2016-03-11 10:49:32 -0800 | [diff] [blame] | 1334 | AnimationView.setupAnimations(ft, anim, args); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1335 | |
| 1336 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 1337 | fragment.setArguments(args); |
| 1338 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1339 | ft.replace(getFragmentId(), fragment); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1340 | ft.commitAllowingStateLoss(); |
| 1341 | } |
| 1342 | |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1343 | public static @Nullable DirectoryFragment get(FragmentManager fm) { |
| 1344 | // TODO: deal with multiple directories shown at once |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1345 | Fragment fragment = fm.findFragmentById(getFragmentId()); |
Steve McKay | 096c78b | 2016-01-21 18:46:15 -0800 | [diff] [blame] | 1346 | return fragment instanceof DirectoryFragment |
| 1347 | ? (DirectoryFragment) fragment |
| 1348 | : null; |
| 1349 | } |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1350 | |
| 1351 | private static int getFragmentId() { |
| 1352 | return R.id.container_directory; |
| 1353 | } |
| 1354 | |
| 1355 | @Override |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 1356 | public void onRefresh() { |
Garfield, Tan | eba5bb9 | 2016-07-22 10:30:49 -0700 | [diff] [blame] | 1357 | // Remove thumbnail cache. We do this not because we're worried about stale thumbnails as it |
| 1358 | // should be covered by last modified value we store in thumbnail cache, but rather to give |
| 1359 | // the user a greater sense that contents are being reloaded. |
| 1360 | ThumbnailCache cache = DocumentsApplication.getThumbnailCache(getContext()); |
| 1361 | String[] ids = mModel.getModelIds(); |
| 1362 | int numOfEvicts = Math.min(ids.length, CACHE_EVICT_LIMIT); |
| 1363 | for (int i = 0; i < numOfEvicts; ++i) { |
| 1364 | cache.removeUri(mModel.getItemUri(ids[i])); |
| 1365 | } |
| 1366 | |
| 1367 | // Trigger loading |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 1368 | getLoaderManager().restartLoader(LOADER_ID, null, this); |
| 1369 | } |
| 1370 | |
| 1371 | @Override |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1372 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
| 1373 | Context context = getActivity(); |
| 1374 | State state = getDisplayState(); |
| 1375 | |
| 1376 | Uri contentsUri; |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1377 | switch (mConfig.mType) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1378 | case TYPE_NORMAL: |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1379 | contentsUri = mConfig.mSearchMode ? DocumentsContract.buildSearchDocumentsUri( |
| 1380 | mConfig.mRoot.authority, mConfig.mRoot.rootId, mConfig.mQuery) |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1381 | : DocumentsContract.buildChildDocumentsUri( |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1382 | mConfig.mDocument.authority, mConfig.mDocument.documentId); |
Steve McKay | ce0b14c | 2016-05-03 11:25:52 -0700 | [diff] [blame] | 1383 | if (mTuner.managedModeEnabled()) { |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1384 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 1385 | } |
Steve McKay | c88f83c | 2016-08-31 12:01:43 -0700 | [diff] [blame] | 1386 | if (DEBUG) Log.d(TAG, "Creating new directory loader for: " |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1387 | + DocumentInfo.debugString(mConfig.mDocument)); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1388 | return new DirectoryLoader( |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1389 | context, mConfig.mRoot, mConfig.mDocument, contentsUri, state.sortModel, |
| 1390 | mConfig.mSearchMode); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1391 | case TYPE_RECENT_OPEN: |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 1392 | if (DEBUG) Log.d(TAG, "Creating new loader recents."); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1393 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 1394 | return new RecentsLoader(context, roots, state); |
Steve McKay | 9d7085f | 2016-03-07 11:51:31 -0800 | [diff] [blame] | 1395 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1396 | default: |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1397 | throw new IllegalStateException("Unknown type " + mConfig.mType); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | @Override |
| 1402 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1403 | if (DEBUG) Log.d(TAG, "Loader has finished for: " |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1404 | + DocumentInfo.debugString(mConfig.mDocument)); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 1405 | assert(result != null); |
| 1406 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1407 | if (!isAdded()) return; |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 1408 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1409 | if (mConfig.mSearchMode) { |
Aga Wronska | 94e53e4 | 2016-04-07 13:09:58 -0700 | [diff] [blame] | 1410 | Metrics.logUserAction(getContext(), Metrics.USER_ACTION_SEARCH); |
Aga Wronska | b028905 | 2016-03-31 16:37:40 -0700 | [diff] [blame] | 1411 | } |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1412 | |
| 1413 | State state = getDisplayState(); |
| 1414 | |
| 1415 | mAdapter.notifyDataSetChanged(); |
| 1416 | mModel.update(result); |
| 1417 | |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1418 | updateLayout(state.derivedMode); |
| 1419 | |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 1420 | if (mRestoredSelection != null) { |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 1421 | mSelectionMgr.restoreSelection(mRestoredSelection); |
Steve McKay | 2bab2f8 | 2016-06-03 09:23:39 -0700 | [diff] [blame] | 1422 | // Note, we'll take care of cleaning up retained selection |
| 1423 | // in the selection handler where we already have some |
| 1424 | // specialized code to handle when selection was restored. |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | // Restore any previous instance state |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1428 | final SparseArray<Parcelable> container = state.dirConfigs.remove(mConfig.getConfigKey()); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 1429 | final int curSortedDimensionId = state.sortModel.getSortedDimensionId(); |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1430 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 1431 | final SortDimension curSortedDimension = |
| 1432 | state.sortModel.getDimensionById(curSortedDimensionId); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1433 | if (container != null && !getArguments().getBoolean(Shared.EXTRA_IGNORE_STATE, false)) { |
| 1434 | getView().restoreHierarchyState(container); |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1435 | } else if (mConfig.mLastSortDimensionId != curSortedDimension.getId() |
| 1436 | || mConfig.mLastSortDimensionId == SortModel.SORT_DIMENSION_ID_UNKNOWN |
| 1437 | || mConfig.mLastSortDirection != curSortedDimension.getSortDirection()) { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 1438 | // Scroll to the top if the sort order actually changed. |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1439 | mRecView.smoothScrollToPosition(0); |
| 1440 | } |
| 1441 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1442 | mConfig.mLastSortDimensionId = curSortedDimension.getId(); |
| 1443 | mConfig.mLastSortDirection = curSortedDimension.getSortDirection(); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1444 | |
Garfield, Tan | eba5bb9 | 2016-07-22 10:30:49 -0700 | [diff] [blame] | 1445 | if (mRefreshLayout.isRefreshing()) { |
| 1446 | new Handler().postDelayed( |
| 1447 | () -> mRefreshLayout.setRefreshing(false), |
| 1448 | REFRESH_SPINNER_DISMISS_DELAY); |
| 1449 | } |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | @Override |
| 1453 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1454 | if (DEBUG) Log.d(TAG, "Resetting loader for: " |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1455 | + DocumentInfo.debugString(mConfig.mDocument)); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 1456 | mModel.onLoaderReset(); |
Garfield, Tan | ca7c088 | 2016-07-18 16:45:27 -0700 | [diff] [blame] | 1457 | |
| 1458 | mRefreshLayout.setRefreshing(false); |
Aga Wronska | af5ace5 | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 1459 | } |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1460 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1461 | private static final class Config { |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1462 | |
| 1463 | private static final String EXTRA_SORT_DIMENSION_ID = "sortDimensionId"; |
| 1464 | private static final String EXTRA_SORT_DIRECTION = "sortDirection"; |
| 1465 | |
| 1466 | // Directory fragment state is defined by: root, document, query, type, selection |
| 1467 | private @ResultType int mType = TYPE_NORMAL; |
| 1468 | private RootInfo mRoot; |
| 1469 | // Null when viewing Recents directory. |
| 1470 | private @Nullable DocumentInfo mDocument; |
| 1471 | private String mQuery = null; |
| 1472 | // Here we save the clip details of moveTo/copyTo actions when picker shows up. |
| 1473 | // This will be written to saved instance. |
| 1474 | private @Nullable FileOperation mPendingOperation; |
| 1475 | private boolean mSearchMode; |
| 1476 | private int mLastSortDimensionId = SortModel.SORT_DIMENSION_ID_UNKNOWN; |
| 1477 | private @SortDirection int mLastSortDirection; |
| 1478 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1479 | private String mConfigKey; |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1480 | |
| 1481 | public void restore(Bundle bundle) { |
| 1482 | mRoot = bundle.getParcelable(Shared.EXTRA_ROOT); |
| 1483 | mDocument = bundle.getParcelable(Shared.EXTRA_DOC); |
| 1484 | mQuery = bundle.getString(Shared.EXTRA_QUERY); |
| 1485 | mType = bundle.getInt(Shared.EXTRA_TYPE); |
| 1486 | mSearchMode = bundle.getBoolean(Shared.EXTRA_SEARCH_MODE); |
| 1487 | mPendingOperation = bundle.getParcelable(FileOperationService.EXTRA_OPERATION); |
| 1488 | mLastSortDimensionId = bundle.getInt(EXTRA_SORT_DIMENSION_ID); |
| 1489 | mLastSortDirection = bundle.getInt(EXTRA_SORT_DIRECTION); |
| 1490 | } |
| 1491 | |
| 1492 | public void save(Bundle bundle) { |
| 1493 | bundle.putInt(Shared.EXTRA_TYPE, mType); |
| 1494 | bundle.putParcelable(Shared.EXTRA_ROOT, mRoot); |
| 1495 | bundle.putParcelable(Shared.EXTRA_DOC, mDocument); |
| 1496 | bundle.putString(Shared.EXTRA_QUERY, mQuery); |
| 1497 | bundle.putBoolean(Shared.EXTRA_SEARCH_MODE, mSearchMode); |
| 1498 | bundle.putParcelable(FileOperationService.EXTRA_OPERATION, mPendingOperation); |
| 1499 | bundle.putInt(EXTRA_SORT_DIMENSION_ID, mLastSortDimensionId); |
| 1500 | bundle.putInt(EXTRA_SORT_DIRECTION, mLastSortDirection); |
| 1501 | } |
| 1502 | |
| 1503 | public FileOperation claimPendingOperation() { |
| 1504 | FileOperation op = mPendingOperation; |
| 1505 | mPendingOperation = null; |
| 1506 | return op; |
| 1507 | } |
| 1508 | |
| 1509 | public void update(int type, RootInfo root, DocumentInfo doc, String query) { |
| 1510 | mType = type; |
| 1511 | update(root, doc, query); |
| 1512 | } |
| 1513 | |
| 1514 | public void update(RootInfo root, DocumentInfo doc, String query) { |
| 1515 | mQuery = query; |
| 1516 | mRoot = root; |
| 1517 | mDocument = doc; |
| 1518 | mSearchMode = query != null; |
| 1519 | } |
| 1520 | |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1521 | private String getConfigKey() { |
| 1522 | if (mConfigKey == null) { |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1523 | final StringBuilder builder = new StringBuilder(); |
| 1524 | builder.append(mRoot != null ? mRoot.authority : "null").append(';'); |
| 1525 | builder.append(mRoot != null ? mRoot.rootId : "null").append(';'); |
| 1526 | builder.append(mDocument != null ? mDocument.documentId : "null"); |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1527 | mConfigKey = builder.toString(); |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1528 | } |
Ben Lin | 2d67ee2 | 2016-09-20 16:01:51 -0700 | [diff] [blame] | 1529 | return mConfigKey; |
Steve McKay | c8f165c | 2016-09-20 13:54:05 -0700 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | } |