Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 17 | package com.android.documentsui.files; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 19 | import static android.content.ContentResolver.wrap; |
| 20 | |
Felipe Leme | 9de5807 | 2018-01-19 16:40:04 -0800 | [diff] [blame] | 21 | import static com.android.documentsui.base.SharedMinimal.DEBUG; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 22 | |
Tony Huang | bb6d8f6 | 2019-02-27 14:26:34 +0800 | [diff] [blame] | 23 | import android.app.DownloadManager; |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 24 | import android.content.ActivityNotFoundException; |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 25 | import android.content.ClipData; |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 26 | import android.content.ContentProviderClient; |
| 27 | import android.content.ContentResolver; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 30 | import android.os.FileUtils; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 31 | import android.provider.DocumentsContract; |
Steve McKay | efd10ac | 2017-07-25 13:33:57 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Ben Lin | 174fc2e | 2017-03-01 17:53:20 -0800 | [diff] [blame] | 34 | import android.view.DragEvent; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 35 | |
Tony Huang | 71dac09 | 2019-06-14 11:37:32 +0800 | [diff] [blame] | 36 | import androidx.annotation.VisibleForTesting; |
Tony Huang | 8d8d92f | 2018-09-13 14:41:16 +0800 | [diff] [blame] | 37 | import androidx.fragment.app.FragmentActivity; |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 38 | import androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails; |
| 39 | import androidx.recyclerview.selection.MutableSelection; |
| 40 | import androidx.recyclerview.selection.Selection; |
| 41 | |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 42 | import com.android.documentsui.AbstractActionHandler; |
Steve McKay | bd9f05a | 2016-10-10 10:18:36 -0700 | [diff] [blame] | 43 | import com.android.documentsui.ActionModeAddons; |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 44 | import com.android.documentsui.ActivityConfig; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 45 | import com.android.documentsui.DocumentsAccess; |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 46 | import com.android.documentsui.DocumentsApplication; |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 47 | import com.android.documentsui.DragAndDropManager; |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 48 | import com.android.documentsui.Injector; |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 49 | import com.android.documentsui.MetricConsts; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 50 | import com.android.documentsui.Metrics; |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 51 | import com.android.documentsui.Model; |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 52 | import com.android.documentsui.R; |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 53 | import com.android.documentsui.TimeoutTask; |
Steve McKay | 84440eb | 2017-06-23 12:35:15 -0700 | [diff] [blame] | 54 | import com.android.documentsui.base.DebugFlags; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 55 | import com.android.documentsui.base.DocumentFilters; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 56 | import com.android.documentsui.base.DocumentInfo; |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 57 | import com.android.documentsui.base.DocumentStack; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 58 | import com.android.documentsui.base.Features; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 59 | import com.android.documentsui.base.Lookup; |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 60 | import com.android.documentsui.base.MimeTypes; |
Tony Huang | bb6d8f6 | 2019-02-27 14:26:34 +0800 | [diff] [blame] | 61 | import com.android.documentsui.base.Providers; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 62 | import com.android.documentsui.base.RootInfo; |
Ben Lin | df0fe89 | 2016-10-18 17:36:05 -0700 | [diff] [blame] | 63 | import com.android.documentsui.base.Shared; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 64 | import com.android.documentsui.base.State; |
| 65 | import com.android.documentsui.clipping.ClipStore; |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 66 | import com.android.documentsui.clipping.DocumentClipper; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 67 | import com.android.documentsui.clipping.UrisSupplier; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 68 | import com.android.documentsui.dirlist.AnimationView; |
Steve McKay | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 69 | import com.android.documentsui.files.ActionHandler.Addons; |
Steve McKay | f433d20 | 2017-07-12 18:46:09 -0700 | [diff] [blame] | 70 | import com.android.documentsui.inspector.InspectorActivity; |
Steve McKay | 3a26823 | 2016-10-19 11:15:47 -0700 | [diff] [blame] | 71 | import com.android.documentsui.queries.SearchViewManager; |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 72 | import com.android.documentsui.roots.ProvidersAccess; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 73 | import com.android.documentsui.services.FileOperation; |
| 74 | import com.android.documentsui.services.FileOperationService; |
| 75 | import com.android.documentsui.services.FileOperations; |
| 76 | import com.android.documentsui.ui.DialogController; |
Tony Huang | 7e72a2d | 2018-08-28 17:36:30 +0800 | [diff] [blame] | 77 | |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 78 | import java.util.ArrayList; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 79 | import java.util.List; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 80 | import java.util.concurrent.Executor; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 81 | |
| 82 | import javax.annotation.Nullable; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
Steve McKay | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 85 | * Provides {@link FilesActivity} action specializations to fragments. |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 86 | */ |
Tony Huang | 8d8d92f | 2018-09-13 14:41:16 +0800 | [diff] [blame] | 87 | public class ActionHandler<T extends FragmentActivity & Addons> extends AbstractActionHandler<T> { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 88 | |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 89 | private static final String TAG = "ManagerActionHandler"; |
Tony Huang | ff4915e | 2019-10-04 15:07:19 +0800 | [diff] [blame] | 90 | private static final int SHARE_FILES_COUNT_LIMIT = 100; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 91 | |
Steve McKay | bd9f05a | 2016-10-10 10:18:36 -0700 | [diff] [blame] | 92 | private final ActionModeAddons mActionModeAddons; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 93 | private final Features mFeatures; |
| 94 | private final ActivityConfig mConfig; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 95 | private final DialogController mDialogs; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 96 | private final DocumentClipper mClipper; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 97 | private final ClipStore mClipStore; |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 98 | private final DragAndDropManager mDragAndDropManager; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 99 | private final Model mModel; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 100 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 101 | ActionHandler( |
| 102 | T activity, |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 103 | State state, |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 104 | ProvidersAccess providers, |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 105 | DocumentsAccess docs, |
Garfield Tan | 63bf813 | 2016-10-11 11:00:49 -0700 | [diff] [blame] | 106 | SearchViewManager searchMgr, |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 107 | Lookup<String, Executor> executors, |
Steve McKay | bd9f05a | 2016-10-10 10:18:36 -0700 | [diff] [blame] | 108 | ActionModeAddons actionModeAddons, |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 109 | DocumentClipper clipper, |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 110 | ClipStore clipStore, |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 111 | DragAndDropManager dragAndDropManager, |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 112 | Injector injector) { |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 113 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 114 | super(activity, state, providers, docs, searchMgr, executors, injector); |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 115 | |
Steve McKay | bd9f05a | 2016-10-10 10:18:36 -0700 | [diff] [blame] | 116 | mActionModeAddons = actionModeAddons; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 117 | mFeatures = injector.features; |
| 118 | mConfig = injector.config; |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 119 | mDialogs = injector.dialogs; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 120 | mClipper = clipper; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 121 | mClipStore = clipStore; |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 122 | mDragAndDropManager = dragAndDropManager; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 123 | mModel = injector.getModel(); |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Ben Lin | 174fc2e | 2017-03-01 17:53:20 -0800 | [diff] [blame] | 127 | public boolean dropOn(DragEvent event, RootInfo root) { |
Ben Lin | d6a85b9 | 2017-03-13 16:26:26 -0700 | [diff] [blame] | 128 | if (!root.supportsCreate() || root.isLibrary()) { |
| 129 | return false; |
| 130 | } |
| 131 | |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 132 | // DragEvent gets recycled, so it is possible that by the time the callback is called, |
| 133 | // event.getLocalState() and event.getClipData() returns null. Thus, we want to save |
| 134 | // references to ensure they are non null. |
| 135 | final ClipData clipData = event.getClipData(); |
| 136 | final Object localState = event.getLocalState(); |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 137 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 138 | return mDragAndDropManager.drop( |
| 139 | clipData, localState, root, this, mDialogs::showFileOperationStatus); |
Ben Lin | 174fc2e | 2017-03-01 17:53:20 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 142 | @Override |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 143 | public void openSelectedInNewWindow() { |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 144 | Selection<String> selection = getStableSelection(); |
Tony Huang | e2ad38f | 2019-07-30 14:43:05 +0800 | [diff] [blame] | 145 | if (selection.isEmpty()) { |
| 146 | return; |
| 147 | } |
| 148 | |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 149 | assert(selection.size() == 1); |
Ben Lin | f23cbf4 | 2016-12-22 15:12:13 -0800 | [diff] [blame] | 150 | DocumentInfo doc = mModel.getDocument(selection.iterator().next()); |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 151 | assert(doc != null); |
| 152 | openInNewWindow(new DocumentStack(mState.stack, doc)); |
| 153 | } |
| 154 | |
| 155 | @Override |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 156 | public void openSettings(RootInfo root) { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 157 | Metrics.logUserAction(MetricConsts.USER_ACTION_SETTINGS); |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 158 | final Intent intent = new Intent(DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS); |
| 159 | intent.setDataAndType(root.getUri(), DocumentsContract.Root.MIME_TYPE_ITEM); |
| 160 | mActivity.startActivity(intent); |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void pasteIntoFolder(RootInfo root) { |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 165 | this.getRootDocument( |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 166 | root, |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 167 | TimeoutTask.DEFAULT_TIMEOUT, |
| 168 | (DocumentInfo doc) -> pasteIntoFolder(root, doc)); |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 171 | private void pasteIntoFolder(RootInfo root, @Nullable DocumentInfo doc) { |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 172 | DocumentStack stack = new DocumentStack(root, doc); |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 173 | mClipper.copyFromClipboard(doc, stack, mDialogs::showFileOperationStatus); |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 177 | public @Nullable DocumentInfo renameDocument(String name, DocumentInfo document) { |
| 178 | ContentResolver resolver = mActivity.getContentResolver(); |
| 179 | ContentProviderClient client = null; |
| 180 | |
| 181 | try { |
| 182 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 183 | resolver, document.derivedUri.getAuthority()); |
| 184 | Uri newUri = DocumentsContract.renameDocument( |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 185 | wrap(client), document.derivedUri, name); |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 186 | return DocumentInfo.fromUri(resolver, newUri); |
| 187 | } catch (Exception e) { |
| 188 | Log.w(TAG, "Failed to rename file", e); |
| 189 | return null; |
| 190 | } finally { |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 191 | FileUtils.closeQuietly(client); |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 196 | public void openRoot(RootInfo root) { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 197 | Metrics.logRootVisited(MetricConsts.FILES_SCOPE, root); |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 198 | mActivity.onRootPicked(root); |
| 199 | } |
| 200 | |
| 201 | @Override |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 202 | public boolean openItem(ItemDetails<String> details, @ViewType int type, |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 203 | @ViewType int fallback) { |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 204 | DocumentInfo doc = mModel.getDocument(details.getSelectionKey()); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 205 | if (doc == null) { |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 206 | Log.w(TAG, "Can't view item. No Document available for modeId: " |
| 207 | + details.getSelectionKey()); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 208 | return false; |
| 209 | } |
Tony Huang | 2a022ba | 2019-01-10 20:02:52 +0800 | [diff] [blame] | 210 | mInjector.searchManager.recordHistory(); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 211 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 212 | return openDocument(doc, type, fallback); |
| 213 | } |
| 214 | |
| 215 | // TODO: Make this private and make tests call openDocument(DocumentDetails, int, int) instead. |
| 216 | @VisibleForTesting |
| 217 | public boolean openDocument(DocumentInfo doc, @ViewType int type, @ViewType int fallback) { |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 218 | if (mConfig.isDocumentEnabled(doc.mimeType, doc.flags, mState)) { |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 219 | onDocumentPicked(doc, type, fallback); |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 220 | mSelectionMgr.clearSelection(); |
Tony Huang | 71dac09 | 2019-06-14 11:37:32 +0800 | [diff] [blame] | 221 | return !doc.isContainer(); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | @Override |
Ben Lin | d8d0ad2 | 2017-01-11 13:30:50 -0800 | [diff] [blame] | 227 | public void springOpenDirectory(DocumentInfo doc) { |
| 228 | assert(doc.isDirectory()); |
| 229 | mActionModeAddons.finishActionMode(); |
| 230 | openContainerDocument(doc); |
| 231 | } |
| 232 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 233 | private Selection<String> getSelectedOrFocused() { |
| 234 | final MutableSelection<String> selection = this.getStableSelection(); |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 235 | if (selection.isEmpty()) { |
| 236 | String focusModelId = mFocusHandler.getFocusModelId(); |
| 237 | if (focusModelId != null) { |
| 238 | selection.add(focusModelId); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return selection; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void cutToClipboard() { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 247 | Metrics.logUserAction(MetricConsts.USER_ACTION_CUT_CLIPBOARD); |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 248 | Selection<String> selection = getSelectedOrFocused(); |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 249 | |
| 250 | if (selection.isEmpty()) { |
| 251 | return; |
| 252 | } |
Ben Lin | c1a32ae | 2017-04-19 15:19:49 -0700 | [diff] [blame] | 253 | |
| 254 | if (mModel.hasDocuments(selection, DocumentFilters.NOT_MOVABLE)) { |
| 255 | mDialogs.showOperationUnsupported(); |
| 256 | return; |
| 257 | } |
| 258 | |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 259 | mSelectionMgr.clearSelection(); |
| 260 | |
Ben Lin | f23cbf4 | 2016-12-22 15:12:13 -0800 | [diff] [blame] | 261 | mClipper.clipDocumentsForCut(mModel::getItemUri, selection, mState.stack.peek()); |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 262 | |
| 263 | mDialogs.showDocumentsClipped(selection.size()); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void copyToClipboard() { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 268 | Metrics.logUserAction(MetricConsts.USER_ACTION_COPY_CLIPBOARD); |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 269 | Selection<String> selection = getSelectedOrFocused(); |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 270 | |
| 271 | if (selection.isEmpty()) { |
| 272 | return; |
| 273 | } |
| 274 | mSelectionMgr.clearSelection(); |
| 275 | |
Ben Lin | f23cbf4 | 2016-12-22 15:12:13 -0800 | [diff] [blame] | 276 | mClipper.clipDocumentsForCopy(mModel::getItemUri, selection); |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 277 | |
| 278 | mDialogs.showDocumentsClipped(selection.size()); |
| 279 | } |
| 280 | |
Jon Mann | 253a992 | 2017-03-21 18:53:27 -0700 | [diff] [blame] | 281 | @Override |
| 282 | public void viewInOwner() { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 283 | Metrics.logUserAction(MetricConsts.USER_ACTION_VIEW_IN_APPLICATION); |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 284 | Selection<String> selection = getSelectedOrFocused(); |
Jon Mann | 253a992 | 2017-03-21 18:53:27 -0700 | [diff] [blame] | 285 | |
| 286 | if (selection.isEmpty() || selection.size() > 1) { |
| 287 | return; |
| 288 | } |
| 289 | DocumentInfo doc = mModel.getDocument(selection.iterator().next()); |
| 290 | Intent intent = new Intent(DocumentsContract.ACTION_DOCUMENT_SETTINGS); |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 291 | intent.setPackage(mProviders.getPackageName(doc.authority)); |
Jon Mann | 253a992 | 2017-03-21 18:53:27 -0700 | [diff] [blame] | 292 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 293 | intent.setData(doc.derivedUri); |
| 294 | try { |
| 295 | mActivity.startActivity(intent); |
| 296 | } catch (ActivityNotFoundException e) { |
| 297 | Log.e(TAG, "Failed to view settings in application for " + doc.derivedUri, e); |
| 298 | mDialogs.showNoApplicationFound(); |
| 299 | } |
| 300 | } |
| 301 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 302 | @Override |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 303 | public void showDeleteDialog() { |
shawnlin | 48200cd | 2018-10-16 16:20:42 +0800 | [diff] [blame] | 304 | Selection selection = getSelectedOrFocused(); |
Ben Lin | d947f01 | 2016-10-18 14:32:49 -0700 | [diff] [blame] | 305 | if (selection.isEmpty()) { |
| 306 | return; |
| 307 | } |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 308 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 309 | DeleteDocumentFragment.show(mActivity.getSupportFragmentManager(), |
| 310 | mModel.getDocuments(selection), |
| 311 | mState.stack.peek()); |
| 312 | } |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 313 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 314 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 315 | @Override |
| 316 | public void deleteSelectedDocuments(List<DocumentInfo> docs, DocumentInfo srcParent) { |
| 317 | if (docs == null || docs.isEmpty()) { |
| 318 | return; |
| 319 | } |
shawnlin | 65bbe79 | 2018-08-20 16:20:43 +0800 | [diff] [blame] | 320 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 321 | mActionModeAddons.finishActionMode(); |
shawnlin | 48200cd | 2018-10-16 16:20:42 +0800 | [diff] [blame] | 322 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 323 | List<Uri> uris = new ArrayList<>(docs.size()); |
| 324 | for (DocumentInfo doc : docs) { |
| 325 | uris.add(doc.derivedUri); |
| 326 | } |
shawnlin | 48200cd | 2018-10-16 16:20:42 +0800 | [diff] [blame] | 327 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 328 | UrisSupplier srcs; |
| 329 | try { |
| 330 | srcs = UrisSupplier.create( |
| 331 | uris, |
| 332 | mClipStore); |
| 333 | } catch (Exception e) { |
| 334 | Log.e(TAG, "Failed to delete a file because we were unable to get item URIs.", e); |
| 335 | mDialogs.showFileOperationStatus( |
| 336 | FileOperations.Callback.STATUS_FAILED, |
| 337 | FileOperationService.OPERATION_DELETE, |
| 338 | uris.size()); |
| 339 | return; |
| 340 | } |
shawnlin | 48200cd | 2018-10-16 16:20:42 +0800 | [diff] [blame] | 341 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 342 | FileOperation operation = new FileOperation.Builder() |
| 343 | .withOpType(FileOperationService.OPERATION_DELETE) |
| 344 | .withDestination(mState.stack) |
| 345 | .withSrcs(srcs) |
| 346 | .withSrcParent(srcParent == null ? null : srcParent.derivedUri) |
| 347 | .build(); |
shawnlin | ea3de8b | 2018-08-03 16:20:55 +0800 | [diff] [blame] | 348 | |
shawnlin | 8dafe61 | 2019-08-14 20:10:18 +0800 | [diff] [blame] | 349 | FileOperations.start(mActivity, operation, mDialogs::showFileOperationStatus, |
| 350 | FileOperations.createJobId()); |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 353 | @Override |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 354 | public void shareSelectedDocuments() { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 355 | Metrics.logUserAction(MetricConsts.USER_ACTION_SHARE); |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 356 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 357 | Selection<String> selection = getStableSelection(); |
Tony Huang | e2ad38f | 2019-07-30 14:43:05 +0800 | [diff] [blame] | 358 | if (selection.isEmpty()) { |
| 359 | return; |
Tony Huang | ff4915e | 2019-10-04 15:07:19 +0800 | [diff] [blame] | 360 | } else if (selection.size() > SHARE_FILES_COUNT_LIMIT) { |
| 361 | mDialogs.showShareOverLimit(SHARE_FILES_COUNT_LIMIT); |
| 362 | return; |
Tony Huang | e2ad38f | 2019-07-30 14:43:05 +0800 | [diff] [blame] | 363 | } |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 364 | |
| 365 | // Model must be accessed in UI thread, since underlying cursor is not threadsafe. |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 366 | List<DocumentInfo> docs = mModel.loadDocuments( |
| 367 | selection, DocumentFilters.sharable(mFeatures)); |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 368 | |
| 369 | Intent intent; |
| 370 | |
| 371 | if (docs.size() == 1) { |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 372 | intent = new Intent(Intent.ACTION_SEND); |
Tomasz Mikolajewski | 5b127ac | 2016-10-24 18:24:58 +0900 | [diff] [blame] | 373 | DocumentInfo doc = docs.get(0); |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 374 | intent.setType(doc.mimeType); |
| 375 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
| 376 | |
| 377 | } else if (docs.size() > 1) { |
| 378 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 379 | |
| 380 | final ArrayList<String> mimeTypes = new ArrayList<>(); |
| 381 | final ArrayList<Uri> uris = new ArrayList<>(); |
| 382 | for (DocumentInfo doc : docs) { |
| 383 | mimeTypes.add(doc.mimeType); |
| 384 | uris.add(doc.derivedUri); |
| 385 | } |
| 386 | |
| 387 | intent.setType(MimeTypes.findCommonMimeType(mimeTypes)); |
| 388 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 389 | |
| 390 | } else { |
Tomasz Mikolajewski | 5b127ac | 2016-10-24 18:24:58 +0900 | [diff] [blame] | 391 | // Everything filtered out, nothing to share. |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
Tomasz Mikolajewski | 5b127ac | 2016-10-24 18:24:58 +0900 | [diff] [blame] | 395 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 396 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Tomasz Mikolajewski | 1abcfd1 | 2016-12-12 10:08:49 +0900 | [diff] [blame] | 397 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 398 | if (mFeatures.isVirtualFilesSharingEnabled() |
| 399 | && mModel.hasDocuments(selection, DocumentFilters.VIRTUAL)) { |
Tomasz Mikolajewski | 5b127ac | 2016-10-24 18:24:58 +0900 | [diff] [blame] | 400 | intent.addCategory(Intent.CATEGORY_TYPED_OPENABLE); |
| 401 | } |
| 402 | |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame] | 403 | Intent chooserIntent = Intent.createChooser( |
| 404 | intent, mActivity.getResources().getText(R.string.share_via)); |
| 405 | |
| 406 | mActivity.startActivity(chooserIntent); |
| 407 | } |
| 408 | |
| 409 | @Override |
shawnlin | ea3de8b | 2018-08-03 16:20:55 +0800 | [diff] [blame] | 410 | public void loadDocumentsForCurrentStack() { |
shawnlin | ea3de8b | 2018-08-03 16:20:55 +0800 | [diff] [blame] | 411 | super.loadDocumentsForCurrentStack(); |
| 412 | } |
| 413 | |
| 414 | @Override |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 415 | public void initLocation(Intent intent) { |
| 416 | assert(intent != null); |
| 417 | |
Garfield Tan | 40c8505 | 2017-02-02 12:44:35 -0800 | [diff] [blame] | 418 | // stack is initialized if it's restored from bundle, which means we're restoring a |
| 419 | // previously stored state. |
| 420 | if (mState.stack.isInitialized()) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 421 | if (DEBUG) { |
| 422 | Log.d(TAG, "Stack already resolved for uri: " + intent.getData()); |
| 423 | } |
Garfield Tan | 5f2a9ba | 2017-05-26 14:35:44 -0700 | [diff] [blame] | 424 | restoreRootAndDirectory(); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
Garfield Tan | 40c8505 | 2017-02-02 12:44:35 -0800 | [diff] [blame] | 428 | if (launchToStackLocation(intent)) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 429 | if (DEBUG) { |
| 430 | Log.d(TAG, "Launched to location from stack."); |
| 431 | } |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 435 | if (launchToRoot(intent)) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 436 | if (DEBUG) { |
| 437 | Log.d(TAG, "Launched to root for browsing."); |
| 438 | } |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 442 | if (launchToDocument(intent)) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 443 | if (DEBUG) { |
| 444 | Log.d(TAG, "Launched to a document."); |
| 445 | } |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
Tony Huang | bb6d8f6 | 2019-02-27 14:26:34 +0800 | [diff] [blame] | 449 | if (launchToDownloads(intent)) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 450 | if (DEBUG) { |
| 451 | Log.d(TAG, "Launched to a downloads."); |
| 452 | } |
Tony Huang | bb6d8f6 | 2019-02-27 14:26:34 +0800 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 456 | if (DEBUG) { |
| 457 | Log.d(TAG, "Launching directly into Home directory."); |
| 458 | } |
Tony Huang | 4e6242a | 2019-01-25 11:45:31 +0800 | [diff] [blame] | 459 | launchToDefaultLocation(); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 462 | @Override |
| 463 | protected void launchToDefaultLocation() { |
Tony Huang | 4e6242a | 2019-01-25 11:45:31 +0800 | [diff] [blame] | 464 | if (mFeatures.isDefaultRootInBrowseEnabled()) { |
| 465 | loadHomeDir(); |
| 466 | } else { |
| 467 | loadRecent(); |
| 468 | } |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Garfield Tan | 40c8505 | 2017-02-02 12:44:35 -0800 | [diff] [blame] | 471 | // If EXTRA_STACK is not null in intent, we'll skip other means of loading |
| 472 | // or restoring the stack (like URI). |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 473 | // |
| 474 | // When restoring from a stack, if a URI is present, it should only ever be: |
| 475 | // -- a launch URI: Launch URIs support sensible activity management, |
| 476 | // but don't specify a real content target) |
| 477 | // -- a fake Uri from notifications. These URIs have no authority (TODO: details). |
| 478 | // |
| 479 | // Any other URI is *sorta* unexpected...except when browsing an archive |
| 480 | // in downloads. |
Garfield Tan | 40c8505 | 2017-02-02 12:44:35 -0800 | [diff] [blame] | 481 | private boolean launchToStackLocation(Intent intent) { |
| 482 | DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 483 | if (stack == null || stack.getRoot() == null) { |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | |
Garfield Tan | 40c8505 | 2017-02-02 12:44:35 -0800 | [diff] [blame] | 487 | mState.stack.reset(stack); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 488 | if (mState.stack.isEmpty()) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 489 | mActivity.onRootPicked(mState.stack.getRoot()); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 490 | } else { |
| 491 | mActivity.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE); |
| 492 | } |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 497 | private boolean launchToRoot(Intent intent) { |
Garfield Tan | c5efea0 | 2017-02-22 12:58:29 -0800 | [diff] [blame] | 498 | String action = intent.getAction(); |
Garfield Tan | 8339202 | 2017-05-31 18:05:40 -0700 | [diff] [blame] | 499 | if (Intent.ACTION_VIEW.equals(action)) { |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 500 | Uri uri = intent.getData(); |
| 501 | if (DocumentsContract.isRootUri(mActivity, uri)) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 502 | if (DEBUG) { |
| 503 | Log.d(TAG, "Launching with root URI."); |
| 504 | } |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 505 | // If we've got a specific root to display, restore that root using a dedicated |
| 506 | // authority. That way a misbehaving provider won't result in an ANR. |
| 507 | loadRoot(uri); |
| 508 | return true; |
Ivan Chiang | 9b9a282 | 2018-09-19 17:03:22 +0800 | [diff] [blame] | 509 | } else if (DocumentsContract.isRootsUri(mActivity, uri)) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 510 | if (DEBUG) { |
| 511 | Log.d(TAG, "Launching first root with roots URI."); |
| 512 | } |
Ivan Chiang | 9b9a282 | 2018-09-19 17:03:22 +0800 | [diff] [blame] | 513 | // TODO: b/116760996 Let the user can disambiguate between roots if there are |
| 514 | // multiple from DocumentsProvider instead of launching the first root in default |
| 515 | loadFirstRoot(uri); |
| 516 | return true; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | return false; |
| 520 | } |
| 521 | |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 522 | private boolean launchToDocument(Intent intent) { |
Garfield Tan | c5efea0 | 2017-02-22 12:58:29 -0800 | [diff] [blame] | 523 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 524 | Uri uri = intent.getData(); |
| 525 | if (DocumentsContract.isDocumentUri(mActivity, uri)) { |
| 526 | return launchToDocument(intent.getData()); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return false; |
| 531 | } |
| 532 | |
Tony Huang | bb6d8f6 | 2019-02-27 14:26:34 +0800 | [diff] [blame] | 533 | private boolean launchToDownloads(Intent intent) { |
| 534 | if (DownloadManager.ACTION_VIEW_DOWNLOADS.equals(intent.getAction())) { |
| 535 | Uri uri = DocumentsContract.buildRootUri(Providers.AUTHORITY_DOWNLOADS, |
| 536 | Providers.ROOT_ID_DOWNLOADS); |
| 537 | loadRoot(uri); |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | return false; |
| 542 | } |
| 543 | |
Garfield Tan | 208945c | 2016-10-04 14:36:38 -0700 | [diff] [blame] | 544 | @Override |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 545 | public void showChooserForDoc(DocumentInfo doc) { |
Tomasz Mikolajewski | ac3e63e | 2017-02-13 10:08:58 +0900 | [diff] [blame] | 546 | assert(!doc.isDirectory()); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 547 | |
| 548 | if (manageDocument(doc)) { |
| 549 | Log.w(TAG, "Open with is not yet supported for managed doc."); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | Intent intent = Intent.createChooser(buildViewIntent(doc), null); |
Tony Huang | e6bdbeb | 2018-11-07 17:56:33 +0800 | [diff] [blame] | 554 | intent.putExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, false); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 555 | try { |
| 556 | mActivity.startActivity(intent); |
| 557 | } catch (ActivityNotFoundException e) { |
| 558 | mDialogs.showNoApplicationFound(); |
| 559 | } |
| 560 | } |
| 561 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 562 | private void onDocumentPicked(DocumentInfo doc, @ViewType int type, @ViewType int fallback) { |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 563 | if (doc.isContainer()) { |
Garfield Tan | 63bf813 | 2016-10-11 11:00:49 -0700 | [diff] [blame] | 564 | openContainerDocument(doc); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | if (manageDocument(doc)) { |
| 569 | return; |
| 570 | } |
| 571 | |
Geoffrey Pitsch | 71bc981 | 2018-01-09 15:38:24 -0500 | [diff] [blame] | 572 | // For APKs, even if the type is preview, we send an ACTION_VIEW intent to allow |
| 573 | // PackageManager to install it. This allows users to install APKs from any root. |
| 574 | // The Downloads special case is handled above in #manageDocument. |
| 575 | if (MimeTypes.isApkType(doc.mimeType)) { |
| 576 | viewDocument(doc); |
| 577 | return; |
| 578 | } |
| 579 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 580 | switch (type) { |
| 581 | case VIEW_TYPE_REGULAR: |
| 582 | if (viewDocument(doc)) { |
| 583 | return; |
| 584 | } |
| 585 | break; |
| 586 | |
| 587 | case VIEW_TYPE_PREVIEW: |
| 588 | if (previewDocument(doc)) { |
| 589 | return; |
| 590 | } |
| 591 | break; |
| 592 | |
| 593 | default: |
| 594 | throw new IllegalArgumentException("Illegal view type."); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 597 | switch (fallback) { |
| 598 | case VIEW_TYPE_REGULAR: |
| 599 | if (viewDocument(doc)) { |
| 600 | return; |
| 601 | } |
| 602 | break; |
| 603 | |
| 604 | case VIEW_TYPE_PREVIEW: |
| 605 | if (previewDocument(doc)) { |
| 606 | return; |
| 607 | } |
| 608 | break; |
| 609 | |
| 610 | case VIEW_TYPE_NONE: |
| 611 | break; |
| 612 | |
| 613 | default: |
| 614 | throw new IllegalArgumentException("Illegal fallback view type."); |
| 615 | } |
| 616 | |
| 617 | // Failed to view including fallback, and it's in an archive. |
| 618 | if (type != VIEW_TYPE_NONE && fallback != VIEW_TYPE_NONE && doc.isInArchive()) { |
| 619 | mDialogs.showViewInArchivesUnsupported(); |
| 620 | } |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 623 | private boolean viewDocument(DocumentInfo doc) { |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 624 | if (doc.isPartial()) { |
| 625 | Log.w(TAG, "Can't view partial file."); |
| 626 | return false; |
| 627 | } |
| 628 | |
Tomasz Mikolajewski | e535c57 | 2016-11-25 13:58:35 +0900 | [diff] [blame] | 629 | if (doc.isInArchive()) { |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 630 | Log.w(TAG, "Can't view files in archives."); |
Tomasz Mikolajewski | e535c57 | 2016-11-25 13:58:35 +0900 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 634 | if (doc.isDirectory()) { |
| 635 | Log.w(TAG, "Can't view directories."); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 636 | return true; |
| 637 | } |
| 638 | |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 639 | Intent intent = buildViewIntent(doc); |
| 640 | if (DEBUG && intent.getClipData() != null) { |
| 641 | Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData()); |
| 642 | } |
| 643 | |
| 644 | try { |
| 645 | mActivity.startActivity(intent); |
| 646 | return true; |
| 647 | } catch (ActivityNotFoundException e) { |
| 648 | mDialogs.showNoApplicationFound(); |
| 649 | } |
| 650 | return false; |
| 651 | } |
| 652 | |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 653 | private boolean previewDocument(DocumentInfo doc) { |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 654 | if (doc.isPartial()) { |
| 655 | Log.w(TAG, "Can't view partial file."); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | Intent intent = new QuickViewIntentBuilder( |
| 660 | mActivity.getPackageManager(), |
| 661 | mActivity.getResources(), |
| 662 | doc, |
Tony Huang | 7a7e7df | 2018-11-06 17:16:47 +0800 | [diff] [blame] | 663 | mModel, |
| 664 | false /* fromPicker */).build(); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 665 | |
| 666 | if (intent != null) { |
| 667 | // TODO: un-work around issue b/24963914. Should be fixed soon. |
| 668 | try { |
| 669 | mActivity.startActivity(intent); |
| 670 | return true; |
| 671 | } catch (SecurityException e) { |
| 672 | // Carry on to regular view mode. |
| 673 | Log.e(TAG, "Caught security error: " + e.getLocalizedMessage()); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | private boolean manageDocument(DocumentInfo doc) { |
| 681 | if (isManagedDownload(doc)) { |
| 682 | // First try managing the document; we expect manager to filter |
| 683 | // based on authority, so we don't grant. |
| 684 | Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT); |
| 685 | manage.setData(doc.derivedUri); |
| 686 | try { |
| 687 | mActivity.startActivity(manage); |
| 688 | return true; |
| 689 | } catch (ActivityNotFoundException ex) { |
| 690 | // Fall back to regular handling. |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | private boolean isManagedDownload(DocumentInfo doc) { |
| 698 | // Anything on downloads goes through the back through downloads manager |
| 699 | // (that's the MANAGE_DOCUMENT bit). |
| 700 | // This is done for two reasons: |
| 701 | // 1) The file in question might be a failed/queued or otherwise have some |
| 702 | // specialized download handling. |
| 703 | // 2) For APKs, the download manager will add on some important security stuff |
| 704 | // like origin URL. |
| 705 | // 3) For partial files, the download manager will offer to restart/retry downloads. |
| 706 | |
| 707 | // All other files not on downloads, event APKs, would get no benefit from this |
| 708 | // treatment, thusly the "isDownloads" check. |
| 709 | |
| 710 | // Launch MANAGE_DOCUMENTS only for the root level files, so it's not called for |
Tony Huang | 74d3a2d | 2019-06-27 14:44:09 +0800 | [diff] [blame] | 711 | // files in archives or in child folders. Also, if the activity is already browsing |
| 712 | // a ZIP from downloads, then skip MANAGE_DOCUMENTS. |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 713 | if (Intent.ACTION_VIEW.equals(mActivity.getIntent().getAction()) |
| 714 | && mState.stack.size() > 1) { |
| 715 | // viewing the contents of an archive. |
| 716 | return false; |
| 717 | } |
| 718 | |
Tony Huang | 74d3a2d | 2019-06-27 14:44:09 +0800 | [diff] [blame] | 719 | // management is only supported in Downloads root or downloaded files show in Recent root. |
| 720 | if (Providers.AUTHORITY_DOWNLOADS.equals(doc.authority)) { |
| 721 | // only on APKs or partial files. |
| 722 | return MimeTypes.isApkType(doc.mimeType) || doc.isPartial(); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return false; |
| 726 | } |
| 727 | |
| 728 | private Intent buildViewIntent(DocumentInfo doc) { |
| 729 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 730 | intent.setDataAndType(doc.derivedUri, doc.mimeType); |
| 731 | |
| 732 | // Downloads has traditionally added the WRITE permission |
| 733 | // in the TrampolineActivity. Since this behavior is long |
| 734 | // established, we set the same permission for non-managed files |
| 735 | // This ensures consistent behavior between the Downloads root |
| 736 | // and other roots. |
Jason Chang | 02d46d9 | 2019-08-01 14:48:09 +0800 | [diff] [blame] | 737 | int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_SINGLE_TOP; |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 738 | if (doc.isWriteSupported()) { |
| 739 | flags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION; |
| 740 | } |
| 741 | intent.setFlags(flags); |
| 742 | |
| 743 | return intent; |
| 744 | } |
| 745 | |
Dooper | 0930d4c | 2017-06-02 10:32:00 -0700 | [diff] [blame] | 746 | @Override |
Austin Kolander | f5042d0 | 2017-06-08 09:20:30 -0700 | [diff] [blame] | 747 | public void showInspector(DocumentInfo doc) { |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 748 | Metrics.logUserAction(MetricConsts.USER_ACTION_INSPECTOR); |
Steve McKay | f433d20 | 2017-07-12 18:46:09 -0700 | [diff] [blame] | 749 | Intent intent = new Intent(mActivity, InspectorActivity.class); |
Steve McKay | efd10ac | 2017-07-25 13:33:57 -0700 | [diff] [blame] | 750 | intent.setData(doc.derivedUri); |
| 751 | |
| 752 | // permit the display of debug info about the file. |
Steve McKay | 84440eb | 2017-06-23 12:35:15 -0700 | [diff] [blame] | 753 | intent.putExtra( |
| 754 | Shared.EXTRA_SHOW_DEBUG, |
Steve McKay | 7bd92f7 | 2017-07-25 17:21:53 -0700 | [diff] [blame] | 755 | mFeatures.isDebugSupportEnabled() && |
Jeff Sharkey | 94785ef | 2018-07-09 16:37:41 -0600 | [diff] [blame] | 756 | (DEBUG || DebugFlags.getDocumentDetailsEnabled())); |
Steve McKay | efd10ac | 2017-07-25 13:33:57 -0700 | [diff] [blame] | 757 | |
| 758 | // The "root document" (top level folder in a root) don't usually have a |
| 759 | // human friendly display name. That's because we've never shown the root |
| 760 | // folder's name to anyone. |
| 761 | // For that reason when the doc being inspected is the root folder, |
| 762 | // we override the displayName of the doc w/ the Root's name instead. |
| 763 | // The Root's name is shown to the user in the sidebar. |
| 764 | if (doc.isDirectory() && mState.stack.size() == 1 && mState.stack.get(0).equals(doc)) { |
| 765 | RootInfo root = mActivity.getCurrentRoot(); |
| 766 | // Recents root title isn't defined, but inspector is disabled for recents root folder. |
| 767 | assert !TextUtils.isEmpty(root.title); |
| 768 | intent.putExtra(Intent.EXTRA_TITLE, root.title); |
| 769 | } |
Dooper | 0930d4c | 2017-06-02 10:32:00 -0700 | [diff] [blame] | 770 | mActivity.startActivity(intent); |
| 771 | } |
| 772 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 773 | public interface Addons extends CommonAddons { |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 774 | } |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 775 | } |