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 | |
| 17 | package com.android.documentsui.picker; |
| 18 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.Shared.DEBUG; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.base.State.ACTION_CREATE; |
| 21 | import static com.android.documentsui.base.State.ACTION_GET_CONTENT; |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 22 | import static com.android.documentsui.base.State.ACTION_OPEN; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 23 | import static com.android.documentsui.base.State.ACTION_OPEN_TREE; |
| 24 | import static com.android.documentsui.base.State.ACTION_PICK_COPY_DESTINATION; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 25 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 26 | import android.app.Activity; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 27 | import android.app.FragmentManager; |
| 28 | import android.content.ClipData; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
| 30 | import android.content.pm.ResolveInfo; |
| 31 | import android.net.Uri; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 32 | import android.os.AsyncTask; |
| 33 | import android.os.Parcelable; |
Garfield Tan | fc72617 | 2016-11-04 17:49:29 -0700 | [diff] [blame] | 34 | import android.provider.DocumentsContract; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 35 | import android.provider.Settings; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 36 | import android.util.Log; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 37 | |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 38 | import com.android.documentsui.AbstractActionHandler; |
Steve McKay | 4f78ba6 | 2016-10-04 16:48:49 -0700 | [diff] [blame] | 39 | import com.android.documentsui.ActivityConfig; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 40 | import com.android.documentsui.DocumentsAccess; |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 41 | import com.android.documentsui.Injector; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 42 | import com.android.documentsui.Metrics; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 43 | import com.android.documentsui.base.BooleanConsumer; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 44 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 45 | import com.android.documentsui.base.DocumentStack; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 46 | import com.android.documentsui.base.Features; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 47 | import com.android.documentsui.base.Lookup; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 48 | import com.android.documentsui.base.RootInfo; |
Steve McKay | 0f5ec83 | 2016-10-04 14:21:56 -0700 | [diff] [blame] | 49 | import com.android.documentsui.base.Shared; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 50 | import com.android.documentsui.base.State; |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 51 | import com.android.documentsui.dirlist.AnimationView; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 52 | import com.android.documentsui.dirlist.DocumentDetails; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 53 | import com.android.documentsui.Model; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 54 | import com.android.documentsui.picker.ActionHandler.Addons; |
Steve McKay | 3a26823 | 2016-10-19 11:15:47 -0700 | [diff] [blame] | 55 | import com.android.documentsui.queries.SearchViewManager; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 56 | import com.android.documentsui.roots.RootsAccess; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 57 | import com.android.documentsui.services.FileOperationService; |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 58 | import com.android.internal.annotations.VisibleForTesting; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 59 | |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 60 | import java.util.Arrays; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 61 | import java.util.concurrent.Executor; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 62 | |
| 63 | import javax.annotation.Nullable; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Provides {@link PickActivity} action specializations to fragments. |
| 67 | */ |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 68 | class ActionHandler<T extends Activity & Addons> extends AbstractActionHandler<T> { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 69 | |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 70 | private static final String TAG = "PickerActionHandler"; |
| 71 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 72 | private final Features mFeatures; |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 73 | private final ActivityConfig mConfig; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 74 | private final Model mModel; |
| 75 | private final LastAccessedStorage mLastAccessed; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 76 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 77 | ActionHandler( |
| 78 | T activity, |
| 79 | State state, |
| 80 | RootsAccess roots, |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 81 | DocumentsAccess docs, |
Garfield Tan | 63bf813 | 2016-10-11 11:00:49 -0700 | [diff] [blame] | 82 | SearchViewManager searchMgr, |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 83 | Lookup<String, Executor> executors, |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 84 | Injector injector, |
| 85 | LastAccessedStorage lastAccessed) { |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 86 | |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 87 | super(activity, state, roots, docs, searchMgr, executors, injector); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 88 | |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 89 | mConfig = injector.config; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 90 | mFeatures = injector.features; |
Garfield Tan | e1ddda5 | 2017-03-08 17:30:35 -0800 | [diff] [blame] | 91 | mModel = injector.getModel(); |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 92 | mLastAccessed = lastAccessed; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 96 | public void initLocation(Intent intent) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 97 | assert(intent != null); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 98 | |
Garfield Tan | 40c8505 | 2017-02-02 12:44:35 -0800 | [diff] [blame] | 99 | // stack is initialized if it's restored from bundle, which means we're restoring a |
| 100 | // previously stored state. |
| 101 | if (mState.stack.isInitialized()) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 102 | if (DEBUG) Log.d(TAG, "Stack already resolved for uri: " + intent.getData()); |
| 103 | return; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 104 | } |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 105 | |
| 106 | // We set the activity title in AsyncTask.onPostExecute(). |
| 107 | // To prevent talkback from reading aloud the default title, we clear it here. |
| 108 | mActivity.setTitle(""); |
| 109 | |
| 110 | if (launchHomeForCopyDestination(intent)) { |
| 111 | if (DEBUG) Log.d(TAG, "Launching directly into Home directory for copy destination."); |
| 112 | return; |
| 113 | } |
| 114 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 115 | if (mFeatures.isLaunchToDocumentEnabled() && launchToDocument(intent)) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 116 | if (DEBUG) Log.d(TAG, "Launched to a document."); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (DEBUG) Log.d(TAG, "Load last accessed stack."); |
| 121 | loadLastAccessedStack(); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 124 | @Override |
| 125 | protected void launchToDefaultLocation() { |
| 126 | loadLastAccessedStack(); |
| 127 | } |
| 128 | |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 129 | private boolean launchHomeForCopyDestination(Intent intent) { |
| 130 | // As a matter of policy we don't load the last used stack for the copy |
| 131 | // destination picker (user is already in Files app). |
| 132 | // Consensus was that the experice was too confusing. |
| 133 | // In all other cases, where the user is visiting us from another app |
| 134 | // we restore the stack as last used from that app. |
| 135 | if (Shared.ACTION_PICK_COPY_DESTINATION.equals(intent.getAction())) { |
| 136 | loadHomeDir(); |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | private boolean launchToDocument(Intent intent) { |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 144 | Uri uri = intent.getParcelableExtra(DocumentsContract.EXTRA_INITIAL_URI); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 145 | if (uri != null) { |
Garfield Tan | f8969d6 | 2017-02-02 16:55:55 -0800 | [diff] [blame] | 146 | return launchToDocument(uri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 152 | private void loadLastAccessedStack() { |
| 153 | if (DEBUG) Log.d(TAG, "Attempting to load last used stack for calling package."); |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 154 | new LoadLastAccessedStackTask<>( |
| 155 | mActivity, mLastAccessed, mState, mRoots, this::onLastAccessedStackLoaded) |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 156 | .execute(); |
| 157 | } |
| 158 | |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 159 | private void onLastAccessedStackLoaded(@Nullable DocumentStack stack) { |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 160 | if (stack == null) { |
Garfield Tan | ae93758 | 2017-03-09 13:57:07 -0800 | [diff] [blame] | 161 | loadDefaultLocation(); |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 162 | } else { |
| 163 | mState.stack.reset(stack); |
Garfield Tan | ae93758 | 2017-03-09 13:57:07 -0800 | [diff] [blame] | 164 | mActivity.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE); |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 165 | } |
Garfield Tan | ae93758 | 2017-03-09 13:57:07 -0800 | [diff] [blame] | 166 | } |
Garfield Tan | bab2516 | 2017-03-08 18:46:25 -0800 | [diff] [blame] | 167 | |
Garfield Tan | ae93758 | 2017-03-09 13:57:07 -0800 | [diff] [blame] | 168 | private void loadDefaultLocation() { |
| 169 | switch (mState.action) { |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 170 | case ACTION_CREATE: |
Garfield Tan | ae93758 | 2017-03-09 13:57:07 -0800 | [diff] [blame] | 171 | loadHomeDir(); |
| 172 | break; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 173 | case ACTION_GET_CONTENT: |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 174 | case ACTION_OPEN: |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 175 | case ACTION_OPEN_TREE: |
Garfield Tan | ae93758 | 2017-03-09 13:57:07 -0800 | [diff] [blame] | 176 | mState.stack.changeRoot(mRoots.getRecentsRoot()); |
| 177 | mActivity.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE); |
| 178 | break; |
| 179 | default: |
| 180 | throw new UnsupportedOperationException("Unexpected action type: " + mState.action); |
| 181 | } |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 184 | @Override |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 185 | public void showAppDetails(ResolveInfo info) { |
| 186 | final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 187 | intent.setData(Uri.fromParts("package", info.activityInfo.packageName, null)); |
| 188 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 189 | mActivity.startActivity(intent); |
| 190 | } |
| 191 | |
| 192 | @Override |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 193 | public void openInNewWindow(DocumentStack path) { |
| 194 | // Open new window support only depends on vanilla Activity, so it is |
| 195 | // implemented in our parent class. But we don't support that in |
| 196 | // picking. So as a matter of defensiveness, we override that here. |
| 197 | throw new UnsupportedOperationException("Can't open in new window"); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void openRoot(RootInfo root) { |
Garfield Tan | 2a8719c | 2017-01-19 16:17:04 -0800 | [diff] [blame] | 202 | Metrics.logRootVisited(mActivity, Metrics.PICKER_SCOPE, root); |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 203 | mActivity.onRootPicked(root); |
| 204 | } |
| 205 | |
| 206 | @Override |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 207 | public void openRoot(ResolveInfo info) { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 208 | Metrics.logAppVisited(mActivity, info); |
| 209 | mActivity.onAppPicked(info); |
| 210 | } |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 211 | |
Steve McKay | 5f29694 | 2016-09-23 08:40:07 -0700 | [diff] [blame] | 212 | @Override |
Ben Lin | d8d0ad2 | 2017-01-11 13:30:50 -0800 | [diff] [blame] | 213 | public void springOpenDirectory(DocumentInfo doc) { |
| 214 | } |
| 215 | |
| 216 | @Override |
Tomasz Mikolajewski | d22cc18 | 2017-03-15 16:13:46 +0900 | [diff] [blame] | 217 | public boolean openDocument(DocumentDetails details, @ViewType int type, |
| 218 | @ViewType int fallback) { |
Ben Lin | f23cbf4 | 2016-12-22 15:12:13 -0800 | [diff] [blame] | 219 | DocumentInfo doc = mModel.getDocument(details.getModelId()); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 220 | if (doc == null) { |
| 221 | Log.w(TAG, |
| 222 | "Can't view item. No Document available for modeId: " + details.getModelId()); |
| 223 | return false; |
| 224 | } |
| 225 | |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 226 | if (mConfig.isDocumentEnabled(doc.mimeType, doc.flags, mState)) { |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 227 | mActivity.onDocumentPicked(doc); |
Steve McKay | 5b0a2c1 | 2016-10-07 11:22:31 -0700 | [diff] [blame] | 228 | mSelectionMgr.clearSelection(); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | return false; |
| 232 | } |
| 233 | |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 234 | void pickDocument(DocumentInfo pickTarget) { |
| 235 | assert(pickTarget != null); |
| 236 | Uri result; |
| 237 | switch (mState.action) { |
| 238 | case ACTION_OPEN_TREE: |
| 239 | result = DocumentsContract.buildTreeDocumentUri( |
| 240 | pickTarget.authority, pickTarget.documentId); |
| 241 | break; |
| 242 | case ACTION_PICK_COPY_DESTINATION: |
| 243 | result = pickTarget.derivedUri; |
| 244 | break; |
| 245 | default: |
| 246 | // Should not be reached |
| 247 | throw new IllegalStateException("Invalid mState.action"); |
| 248 | } |
| 249 | finishPicking(result); |
| 250 | } |
| 251 | |
| 252 | void saveDocument( |
| 253 | String mimeType, String displayName, BooleanConsumer inProgressStateListener) { |
| 254 | assert(mState.action == ACTION_CREATE); |
| 255 | new CreatePickedDocumentTask( |
| 256 | mActivity, |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 257 | mDocs, |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 258 | mLastAccessed, |
| 259 | mState.stack, |
| 260 | mimeType, |
| 261 | displayName, |
| 262 | inProgressStateListener, |
| 263 | this::onPickFinished) |
| 264 | .executeOnExecutor(getExecutorForCurrentDirectory()); |
| 265 | } |
| 266 | |
| 267 | // User requested to overwrite a target. If confirmed by user #finishPicking() will be |
| 268 | // called. |
| 269 | void saveDocument(FragmentManager fm, DocumentInfo replaceTarget) { |
| 270 | assert(mState.action == ACTION_CREATE); |
| 271 | assert(replaceTarget != null); |
| 272 | |
| 273 | mInjector.dialogs.confirmOverwrite(fm, replaceTarget); |
| 274 | } |
| 275 | |
| 276 | void finishPicking(Uri... docs) { |
| 277 | new SetLastAccessedStackTask( |
| 278 | mActivity, |
| 279 | mLastAccessed, |
| 280 | mState.stack, |
| 281 | () -> { |
| 282 | onPickFinished(docs); |
| 283 | } |
| 284 | ) .executeOnExecutor(getExecutorForCurrentDirectory()); |
| 285 | } |
| 286 | |
| 287 | private void onPickFinished(Uri... uris) { |
| 288 | if (DEBUG) Log.d(TAG, "onFinished() " + Arrays.toString(uris)); |
| 289 | |
| 290 | final Intent intent = new Intent(); |
| 291 | if (uris.length == 1) { |
| 292 | intent.setData(uris[0]); |
| 293 | } else if (uris.length > 1) { |
| 294 | final ClipData clipData = new ClipData( |
| 295 | null, mState.acceptMimes, new ClipData.Item(uris[0])); |
| 296 | for (int i = 1; i < uris.length; i++) { |
| 297 | clipData.addItem(new ClipData.Item(uris[i])); |
| 298 | } |
| 299 | intent.setClipData(clipData); |
| 300 | } |
| 301 | |
| 302 | // TODO: Separate this piece of logic per action. |
| 303 | // We don't instantiate different objects for different actions at the first place, so it's |
| 304 | // not a easy task to separate this logic cleanly. |
| 305 | // Maybe we can add an ActionPolicy class for IoC and provide various behaviors through its |
| 306 | // inheritance structure. |
| 307 | if (mState.action == ACTION_GET_CONTENT) { |
| 308 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 309 | } else if (mState.action == ACTION_OPEN_TREE) { |
| 310 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 311 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 312 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 313 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
| 314 | } else if (mState.action == ACTION_PICK_COPY_DESTINATION) { |
| 315 | // Picking a copy destination is only used internally by us, so we |
| 316 | // don't need to extend permissions to the caller. |
| 317 | intent.putExtra(Shared.EXTRA_STACK, (Parcelable) mState.stack); |
| 318 | intent.putExtra(FileOperationService.EXTRA_OPERATION_TYPE, mState.copyOperationSubType); |
| 319 | } else { |
| 320 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 321 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 322 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); |
| 323 | } |
| 324 | |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 325 | mActivity.setResult(Activity.RESULT_OK, intent, 0); |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 326 | mActivity.finish(); |
| 327 | } |
| 328 | |
| 329 | private Executor getExecutorForCurrentDirectory() { |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 330 | final DocumentInfo cwd = mState.stack.peek(); |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 331 | if (cwd != null && cwd.authority != null) { |
| 332 | return mExecutors.lookup(cwd.authority); |
| 333 | } else { |
| 334 | return AsyncTask.THREAD_POOL_EXECUTOR; |
| 335 | } |
| 336 | } |
| 337 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 338 | public interface Addons extends CommonAddons { |
| 339 | void onAppPicked(ResolveInfo info); |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 340 | void onDocumentPicked(DocumentInfo doc); |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Overload final method {@link Activity#setResult(int, Intent)} so that we can intercept |
| 344 | * this method call in test environment. |
| 345 | */ |
| 346 | @VisibleForTesting |
| 347 | void setResult(int resultCode, Intent result, int notUsed); |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 348 | } |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 349 | } |