Steve McKay | 1f19948 | 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 | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 19 | import android.content.ClipData; |
| 20 | import android.content.ClipboardManager; |
| 21 | import android.content.ContentProviderClient; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.database.Cursor; |
| 25 | import android.net.Uri; |
| 26 | import android.provider.DocumentsContract; |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 27 | import android.support.annotation.Nullable; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
| 30 | import com.android.documentsui.model.DocumentInfo; |
| 31 | import com.android.internal.util.Preconditions; |
| 32 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 33 | import libcore.io.IoUtils; |
| 34 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 35 | import java.util.ArrayList; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * ClipboardManager wrapper class providing higher level logical |
| 40 | * support for dealing with Documents. |
| 41 | */ |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame^] | 42 | public final class DocumentClipper { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String TAG = "DocumentClipper"; |
| 45 | |
| 46 | private Context mContext; |
| 47 | private ClipboardManager mClipboard; |
| 48 | |
| 49 | public DocumentClipper(Context context) { |
| 50 | mContext = context; |
| 51 | mClipboard = context.getSystemService(ClipboardManager.class); |
| 52 | } |
| 53 | |
| 54 | public boolean hasItemsToPaste() { |
| 55 | if (mClipboard.hasPrimaryClip()) { |
| 56 | ClipData clipData = mClipboard.getPrimaryClip(); |
| 57 | int count = clipData.getItemCount(); |
| 58 | if (count > 0) { |
| 59 | for (int i = 0; i < count; ++i) { |
| 60 | ClipData.Item item = clipData.getItemAt(i); |
| 61 | Uri uri = item.getUri(); |
| 62 | if (isDocumentUri(uri)) { |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | private boolean isDocumentUri(@Nullable Uri uri) { |
| 72 | return uri != null && DocumentsContract.isDocumentUri(mContext, uri); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns a list of Documents as decoded from Clipboard primary clipdata. |
| 77 | * This should be run from inside an AsyncTask. |
| 78 | */ |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 79 | public List<DocumentInfo> getClippedDocuments() { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 80 | return getDocumentsFromClipData(mClipboard.getPrimaryClip()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns a list of Documents as decoded in clipData. |
| 85 | * This should be run from inside an AsyncTask. |
| 86 | */ |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 87 | public List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 88 | Preconditions.checkNotNull(clipData); |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 89 | final List<DocumentInfo> srcDocs = new ArrayList<>(); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 90 | |
| 91 | int count = clipData.getItemCount(); |
| 92 | if (count == 0) { |
| 93 | return srcDocs; |
| 94 | } |
| 95 | |
| 96 | ContentResolver resolver = mContext.getContentResolver(); |
| 97 | for (int i = 0; i < count; ++i) { |
| 98 | ClipData.Item item = clipData.getItemAt(i); |
| 99 | Uri itemUri = item.getUri(); |
| 100 | if (itemUri != null && DocumentsContract.isDocumentUri(mContext, itemUri)) { |
| 101 | ContentProviderClient client = null; |
| 102 | Cursor cursor = null; |
| 103 | try { |
| 104 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 105 | resolver, itemUri.getAuthority()); |
| 106 | cursor = client.query(itemUri, null, null, null, null); |
| 107 | cursor.moveToPosition(0); |
| 108 | srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority())); |
| 109 | } catch (Exception e) { |
| 110 | Log.e(TAG, e.getMessage()); |
| 111 | } finally { |
| 112 | IoUtils.closeQuietly(cursor); |
| 113 | ContentProviderClient.releaseQuietly(client); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return srcDocs; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns ClipData representing the list of docs, or null if docs is empty, |
| 123 | * or docs cannot be converted. |
| 124 | */ |
| 125 | public @Nullable ClipData getClipDataForDocuments(List<DocumentInfo> docs) { |
| 126 | final ContentResolver resolver = mContext.getContentResolver(); |
| 127 | ClipData clipData = null; |
| 128 | for (DocumentInfo doc : docs) { |
| 129 | final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId); |
| 130 | if (clipData == null) { |
| 131 | // TODO: figure out what this string should be. |
| 132 | // Currently it is not displayed anywhere in the UI, but this might change. |
| 133 | final String label = ""; |
| 134 | clipData = ClipData.newUri(resolver, label, uri); |
| 135 | } else { |
| 136 | // TODO: update list of mime types in ClipData. |
| 137 | clipData.addItem(new ClipData.Item(uri)); |
| 138 | } |
| 139 | } |
| 140 | return clipData; |
| 141 | } |
| 142 | |
| 143 | public void clipDocuments(List<DocumentInfo> docs) { |
| 144 | ClipData data = getClipDataForDocuments(docs); |
| 145 | mClipboard.setPrimaryClip(data); |
| 146 | } |
| 147 | } |