Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 19 | import android.content.ClipData; |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 20 | import android.content.ClipDescription; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 21 | import android.content.ClipboardManager; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 24 | import android.content.SharedPreferences; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 26 | import android.os.BaseBundle; |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 27 | import android.os.PersistableBundle; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 28 | import android.provider.DocumentsContract; |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 29 | import android.support.annotation.Nullable; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 32 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 33 | import com.android.documentsui.model.DocumentInfo; |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 34 | import com.android.documentsui.model.DocumentStack; |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 35 | import com.android.documentsui.services.FileOperationService; |
| 36 | import com.android.documentsui.services.FileOperationService.OpType; |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 37 | import com.android.documentsui.services.FileOperations; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 38 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 39 | import java.io.IOException; |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 40 | import java.util.ArrayList; |
Vladislav Kaznacheev | 067d6fa | 2016-05-03 15:37:21 -0700 | [diff] [blame] | 41 | import java.util.HashSet; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 42 | import java.util.List; |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 43 | import java.util.Set; |
| 44 | import java.util.function.Function; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * ClipboardManager wrapper class providing higher level logical |
| 48 | * support for dealing with Documents. |
| 49 | */ |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 50 | public final class DocumentClipper implements ClipboardManager.OnPrimaryClipChangedListener { |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 51 | |
| 52 | private static final String TAG = "DocumentClipper"; |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 53 | |
| 54 | static final String SRC_PARENT_KEY = "srcParent"; |
| 55 | static final String OP_TYPE_KEY = "opType"; |
| 56 | static final String OP_JUMBO_SELECTION_SIZE = "jumboSelection-size"; |
| 57 | static final String OP_JUMBO_SELECTION_TAG = "jumboSelection-tag"; |
| 58 | |
| 59 | // Use shared preference to store last seen primary clip tag, so that we can delete the file |
| 60 | // when we realize primary clip has been changed when we're not running. |
| 61 | private static final String PREF_NAME = "DocumentClipperPref"; |
| 62 | private static final String LAST_PRIMARY_CLIP_TAG = "lastPrimaryClipTag"; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 63 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 64 | private final Context mContext; |
| 65 | private final ClipStorage mClipStorage; |
| 66 | private final ClipboardManager mClipboard; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 67 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 68 | // Here we're tracking the last clipped tag ids so we can delete them later. |
| 69 | private long mLastDragClipTag = ClipStorage.NO_SELECTION_TAG; |
| 70 | private long mLastUnusedPrimaryClipTag = ClipStorage.NO_SELECTION_TAG; |
| 71 | |
| 72 | private final SharedPreferences mPref; |
| 73 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 74 | DocumentClipper(Context context, ClipStorage storage) { |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 75 | mContext = context; |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 76 | mClipStorage = storage; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 77 | mClipboard = context.getSystemService(ClipboardManager.class); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 78 | |
| 79 | mClipboard.addPrimaryClipChangedListener(this); |
| 80 | |
| 81 | // Primary clips may be changed when we're not running, now it's time to clean up the |
| 82 | // remnant. |
| 83 | mPref = context.getSharedPreferences(PREF_NAME, 0); |
| 84 | mLastUnusedPrimaryClipTag = |
| 85 | mPref.getLong(LAST_PRIMARY_CLIP_TAG, ClipStorage.NO_SELECTION_TAG); |
| 86 | deleteLastUnusedPrimaryClip(); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public boolean hasItemsToPaste() { |
| 90 | if (mClipboard.hasPrimaryClip()) { |
| 91 | ClipData clipData = mClipboard.getPrimaryClip(); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 92 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 93 | int count = clipData.getItemCount(); |
| 94 | if (count > 0) { |
| 95 | for (int i = 0; i < count; ++i) { |
| 96 | ClipData.Item item = clipData.getItemAt(i); |
| 97 | Uri uri = item.getUri(); |
| 98 | if (isDocumentUri(uri)) { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | private boolean isDocumentUri(@Nullable Uri uri) { |
| 108 | return uri != null && DocumentsContract.isDocumentUri(mContext, uri); |
| 109 | } |
| 110 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Returns {@link ClipData} representing the selection, or null if selection is empty, |
| 113 | * or cannot be converted. |
| 114 | * |
| 115 | * This is specialized for drag and drop so that we know which file to delete if nobody accepts |
| 116 | * the drop. |
| 117 | */ |
| 118 | public @Nullable ClipData getClipDataForDrag( |
| 119 | Function<String, Uri> uriBuilder, Selection selection, @OpType int opType) { |
| 120 | ClipData data = getClipDataForDocuments(uriBuilder, selection, opType); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 121 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 122 | mLastDragClipTag = getTag(data); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 123 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 124 | return data; |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 128 | * Returns {@link ClipData} representing the selection, or null if selection is empty, |
| 129 | * or cannot be converted. |
| 130 | */ |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 131 | private @Nullable ClipData getClipDataForDocuments( |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 132 | Function<String, Uri> uriBuilder, Selection selection, @OpType int opType) { |
| 133 | |
| 134 | assert(selection != null); |
| 135 | |
| 136 | if (selection.isEmpty()) { |
| 137 | Log.w(TAG, "Attempting to clip empty selection. Ignoring."); |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | return (selection.size() > Shared.MAX_DOCS_IN_INTENT) |
| 142 | ? createJumboClipData(uriBuilder, selection, opType) |
| 143 | : createStandardClipData(uriBuilder, selection, opType); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns ClipData representing the selection. |
| 148 | */ |
| 149 | private @Nullable ClipData createStandardClipData( |
| 150 | Function<String, Uri> uriBuilder, Selection selection, @OpType int opType) { |
| 151 | |
| 152 | assert(!selection.isEmpty()); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 153 | assert(selection.size() <= Shared.MAX_DOCS_IN_INTENT); |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 154 | |
| 155 | final ContentResolver resolver = mContext.getContentResolver(); |
| 156 | final ArrayList<ClipData.Item> clipItems = new ArrayList<>(); |
| 157 | final Set<String> clipTypes = new HashSet<>(); |
| 158 | |
| 159 | PersistableBundle bundle = new PersistableBundle(); |
| 160 | bundle.putInt(OP_TYPE_KEY, opType); |
| 161 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 162 | for (String id : selection) { |
| 163 | assert(id != null); |
| 164 | Uri uri = uriBuilder.apply(id); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 165 | DocumentInfo.addMimeTypes(resolver, uri, clipTypes); |
| 166 | clipItems.add(new ClipData.Item(uri)); |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | ClipDescription description = new ClipDescription( |
| 170 | "", // Currently "label" is not displayed anywhere in the UI. |
| 171 | clipTypes.toArray(new String[0])); |
| 172 | description.setExtras(bundle); |
| 173 | |
| 174 | return new ClipData(description, clipItems); |
| 175 | } |
| 176 | |
| 177 | /** |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 178 | * Returns ClipData representing the list of docs |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 179 | */ |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 180 | private @Nullable ClipData createJumboClipData( |
| 181 | Function<String, Uri> uriBuilder, Selection selection, @OpType int opType) { |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 182 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 183 | assert(!selection.isEmpty()); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 184 | assert(selection.size() > Shared.MAX_DOCS_IN_INTENT); |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 185 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 186 | final List<Uri> uris = new ArrayList<>(selection.size()); |
| 187 | |
| 188 | final int capacity = Math.min(selection.size(), Shared.MAX_DOCS_IN_INTENT); |
| 189 | final ArrayList<ClipData.Item> clipItems = new ArrayList<>(capacity); |
| 190 | |
| 191 | // Set up mime types for the first Shared.MAX_DOCS_IN_INTENT |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 192 | final ContentResolver resolver = mContext.getContentResolver(); |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 193 | final Set<String> clipTypes = new HashSet<>(); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 194 | int docCount = 0; |
| 195 | for (String id : selection) { |
| 196 | assert(id != null); |
| 197 | Uri uri = uriBuilder.apply(id); |
| 198 | if (docCount++ < Shared.MAX_DOCS_IN_INTENT) { |
| 199 | DocumentInfo.addMimeTypes(resolver, uri, clipTypes); |
| 200 | clipItems.add(new ClipData.Item(uri)); |
| 201 | } |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 202 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 203 | uris.add(uri); |
| 204 | } |
| 205 | |
| 206 | // Prepare metadata |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 207 | PersistableBundle bundle = new PersistableBundle(); |
| 208 | bundle.putInt(OP_TYPE_KEY, opType); |
| 209 | bundle.putInt(OP_JUMBO_SELECTION_SIZE, selection.size()); |
| 210 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 211 | // Creates a clip tag |
| 212 | long tag = mClipStorage.createTag(); |
| 213 | bundle.putLong(OP_JUMBO_SELECTION_TAG, tag); |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 214 | |
| 215 | ClipDescription description = new ClipDescription( |
| 216 | "", // Currently "label" is not displayed anywhere in the UI. |
| 217 | clipTypes.toArray(new String[0])); |
| 218 | description.setExtras(bundle); |
| 219 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 220 | // Persists clip items |
| 221 | new ClipStorage.PersistTask(mClipStorage, uris, tag).execute(); |
| 222 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 223 | return new ClipData(description, clipItems); |
Vladislav Kaznacheev | 067d6fa | 2016-05-03 15:37:21 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Puts {@code ClipData} in a primary clipboard, describing a copy operation |
| 228 | */ |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 229 | public void clipDocumentsForCopy(Function<String, Uri> uriBuilder, Selection selection) { |
| 230 | ClipData data = |
| 231 | getClipDataForDocuments(uriBuilder, selection, FileOperationService.OPERATION_COPY); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 232 | assert(data != null); |
| 233 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 234 | setPrimaryClip(data); |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 235 | } |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Puts {@Code ClipData} in a primary clipboard, describing a cut operation |
| 239 | */ |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 240 | public void clipDocumentsForCut( |
| 241 | Function<String, Uri> uriBuilder, Selection selection, DocumentInfo parent) { |
| 242 | assert(!selection.isEmpty()); |
| 243 | assert(parent.derivedUri != null); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 244 | |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 245 | ClipData data = getClipDataForDocuments(uriBuilder, selection, |
| 246 | FileOperationService.OPERATION_MOVE); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 247 | assert(data != null); |
| 248 | |
| 249 | PersistableBundle bundle = data.getDescription().getExtras(); |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 250 | bundle.putString(SRC_PARENT_KEY, parent.derivedUri.toString()); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 251 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 252 | setPrimaryClip(data); |
| 253 | } |
| 254 | |
| 255 | private void setPrimaryClip(ClipData data) { |
| 256 | deleteLastPrimaryClip(); |
| 257 | |
| 258 | long tag = getTag(data); |
| 259 | setLastUnusedPrimaryClipTag(tag); |
| 260 | |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 261 | mClipboard.setPrimaryClip(data); |
| 262 | } |
| 263 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Sets this primary tag to both class variable and shared preference. |
| 266 | */ |
| 267 | private void setLastUnusedPrimaryClipTag(long tag) { |
| 268 | mLastUnusedPrimaryClipTag = tag; |
| 269 | mPref.edit().putLong(LAST_PRIMARY_CLIP_TAG, tag).commit(); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * This is a good chance for us to remove previous clip file for cut/copy because we know a new |
| 274 | * primary clip is set. |
| 275 | */ |
| 276 | @Override |
| 277 | public void onPrimaryClipChanged() { |
| 278 | deleteLastUnusedPrimaryClip(); |
| 279 | } |
| 280 | |
| 281 | private void deleteLastUnusedPrimaryClip() { |
| 282 | ClipData primary = mClipboard.getPrimaryClip(); |
| 283 | long primaryTag = getTag(primary); |
| 284 | |
| 285 | // onPrimaryClipChanged is also called after we call setPrimaryClip(), so make sure we don't |
| 286 | // delete the clip file we just created. |
| 287 | if (mLastUnusedPrimaryClipTag != primaryTag) { |
| 288 | deleteLastPrimaryClip(); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 289 | } |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | private void deleteLastPrimaryClip() { |
| 293 | deleteClip(mLastUnusedPrimaryClipTag); |
| 294 | setLastUnusedPrimaryClipTag(ClipStorage.NO_SELECTION_TAG); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Deletes the last seen drag clip file. |
| 299 | */ |
| 300 | public void deleteDragClip() { |
| 301 | deleteClip(mLastDragClipTag); |
| 302 | mLastDragClipTag = ClipStorage.NO_SELECTION_TAG; |
| 303 | } |
| 304 | |
| 305 | private void deleteClip(long tag) { |
| 306 | try { |
| 307 | mClipStorage.delete(tag); |
| 308 | } catch (IOException e) { |
| 309 | Log.w(TAG, "Error deleting clip file with tag: " + tag, e); |
| 310 | } |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 313 | /** |
| 314 | * Copies documents from clipboard. It's the same as {@link #copyFromClipData} with clipData |
| 315 | * returned from {@link ClipboardManager#getPrimaryClip()}. |
| 316 | * |
| 317 | * @param destination destination document. |
| 318 | * @param docStack the document stack to the destination folder, |
| 319 | * @param callback callback to notify when operation finishes. |
| 320 | */ |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 321 | public void copyFromClipboard( |
| 322 | DocumentInfo destination, |
| 323 | DocumentStack docStack, |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 324 | FileOperations.Callback callback) { |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 325 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 326 | // The primary clip has been claimed by a file operation. It's now the operation's duty |
| 327 | // to make sure the clip file is deleted after use. |
| 328 | setLastUnusedPrimaryClipTag(ClipStorage.NO_SELECTION_TAG); |
| 329 | |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 330 | copyFromClipData(destination, docStack, mClipboard.getPrimaryClip(), callback); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Copies documents from given clip data. |
| 335 | * |
| 336 | * @param destination destination document |
| 337 | * @param docStack the document stack to the destination folder |
| 338 | * @param clipData the clipData to copy from, or null to copy from clipboard |
| 339 | * @param callback callback to notify when operation finishes |
| 340 | */ |
Steve McKay | d818796 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 341 | public void copyFromClipData( |
| 342 | final DocumentInfo destination, |
| 343 | DocumentStack docStack, |
| 344 | final @Nullable ClipData clipData, |
| 345 | final FileOperations.Callback callback) { |
| 346 | |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 347 | if (clipData == null) { |
| 348 | Log.i(TAG, "Received null clipData. Ignoring."); |
| 349 | return; |
| 350 | } |
| 351 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 352 | ClipDetails details = ClipDetails.createClipDetails(clipData); |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 353 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 354 | if (!canCopy(destination)) { |
| 355 | callback.onOperationResult( |
| 356 | FileOperations.Callback.STATUS_REJECTED, details.getOpType(), 0); |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 360 | if (details.getItemCount() == 0) { |
| 361 | callback.onOperationResult( |
| 362 | FileOperations.Callback.STATUS_ACCEPTED, details.getOpType(), 0); |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
| 366 | DocumentStack dstStack = new DocumentStack(); |
| 367 | dstStack.push(destination); |
| 368 | dstStack.addAll(docStack); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 369 | |
| 370 | // Pass root here so that we can perform "download" root check when |
| 371 | dstStack.root = docStack.root; |
| 372 | |
| 373 | FileOperations.start(mContext, details, dstStack, callback); |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Returns true if the list of files can be copied to destination. Note that this |
| 378 | * is a policy check only. Currently the method does not attempt to verify |
| 379 | * available space or any other environmental aspects possibly resulting in |
| 380 | * failure to copy. |
| 381 | * |
| 382 | * @return true if the list of files can be copied to destination. |
| 383 | */ |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 384 | private static boolean canCopy(DocumentInfo dest) { |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 385 | if (dest == null || !dest.isDirectory() || !dest.isCreateSupported()) { |
| 386 | return false; |
| 387 | } |
| 388 | |
Garfield, Tan | 6d0b46e | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 389 | return true; |
| 390 | } |
| 391 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Obtains tag from {@link ClipData}. Returns {@link ClipStorage#NO_SELECTION_TAG} |
| 394 | * if it's not a jumbo clip. |
| 395 | */ |
| 396 | private static long getTag(@Nullable ClipData data) { |
| 397 | if (data == null) { |
| 398 | return ClipStorage.NO_SELECTION_TAG; |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 399 | } |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 400 | |
| 401 | ClipDescription description = data.getDescription(); |
| 402 | BaseBundle bundle = description.getExtras(); |
| 403 | return bundle.getLong(OP_JUMBO_SELECTION_TAG, ClipStorage.NO_SELECTION_TAG); |
Ben Lin | b1404b7 | 2016-04-18 14:35:28 -0700 | [diff] [blame] | 404 | } |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 405 | |
Steve McKay | bdbd0ff | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 406 | } |