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