blob: b3c28469e71dd83a895baa0bc6f117cd743a4712 [file] [log] [blame]
Steve McKaybdbd0ff2015-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 McKaybdbd0ff2015-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 McKay58efce32015-08-20 16:19:38 +000027import android.support.annotation.Nullable;
Steve McKaybdbd0ff2015-05-20 15:58:42 -070028import android.util.Log;
29
30import com.android.documentsui.model.DocumentInfo;
31import com.android.internal.util.Preconditions;
32
Steve McKaybdbd0ff2015-05-20 15:58:42 -070033import libcore.io.IoUtils;
34
Steve McKay58efce32015-08-20 16:19:38 +000035import java.util.ArrayList;
Steve McKaybdbd0ff2015-05-20 15:58:42 -070036import java.util.List;
37
38/**
39 * ClipboardManager wrapper class providing higher level logical
40 * support for dealing with Documents.
41 */
Steve McKayf8621552015-11-03 15:23:16 -080042public final class DocumentClipper {
Steve McKaybdbd0ff2015-05-20 15:58:42 -070043
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 McKay58efce32015-08-20 16:19:38 +000079 public List<DocumentInfo> getClippedDocuments() {
Steve McKaybdbd0ff2015-05-20 15:58:42 -070080 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 McKay58efce32015-08-20 16:19:38 +000087 public List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
Steve McKaybdbd0ff2015-05-20 15:58:42 -070088 Preconditions.checkNotNull(clipData);
Steve McKay58efce32015-08-20 16:19:38 +000089 final List<DocumentInfo> srcDocs = new ArrayList<>();
Steve McKaybdbd0ff2015-05-20 15:58:42 -070090
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}