blob: 15bfc3b93087583feb9e4f71c41867e57df856f6 [file] [log] [blame]
Steve McKay1f199482015-05-20 15:58:42 -07001/*
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
17package com.android.documentsui;
18
Steve McKay1f199482015-05-20 15:58:42 -070019import android.content.ClipData;
20import android.content.ClipboardManager;
21import android.content.ContentProviderClient;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.database.Cursor;
25import android.net.Uri;
26import android.provider.DocumentsContract;
Steve McKayfefcd702015-08-20 16:19:38 +000027import android.support.annotation.Nullable;
Steve McKay1f199482015-05-20 15:58:42 -070028import android.util.Log;
29
30import com.android.documentsui.model.DocumentInfo;
Steve McKay1f199482015-05-20 15:58:42 -070031
Steve McKay1f199482015-05-20 15:58:42 -070032import libcore.io.IoUtils;
33
Steve McKayfefcd702015-08-20 16:19:38 +000034import java.util.ArrayList;
Steve McKay1f199482015-05-20 15:58:42 -070035import java.util.List;
36
37/**
38 * ClipboardManager wrapper class providing higher level logical
39 * support for dealing with Documents.
40 */
Steve McKayf68210e2015-11-03 15:23:16 -080041public final class DocumentClipper {
Steve McKay1f199482015-05-20 15:58:42 -070042
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 McKayfefcd702015-08-20 16:19:38 +000078 public List<DocumentInfo> getClippedDocuments() {
Steve McKay1f199482015-05-20 15:58:42 -070079 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 McKayfefcd702015-08-20 16:19:38 +000086 public List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
Steve McKay0af8afd2016-02-25 13:34:03 -080087 assert(clipData != null);
Steve McKayfefcd702015-08-20 16:19:38 +000088 final List<DocumentInfo> srcDocs = new ArrayList<>();
Steve McKay1f199482015-05-20 15:58:42 -070089
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}