DocumentsProviderHelper.java revision c7b832202a8f1f91e378e255e61c4aa703f53394
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 17package com.android.documentsui; 18 19import static android.provider.DocumentsContract.buildChildDocumentsUri; 20import static android.provider.DocumentsContract.buildDocumentUri; 21import static android.provider.DocumentsContract.buildRootsUri; 22import static com.android.documentsui.model.DocumentInfo.getCursorString; 23import static com.android.internal.util.Preconditions.checkArgument; 24import static junit.framework.Assert.assertEquals; 25import static junit.framework.Assert.assertNotNull; 26import static junit.framework.Assert.fail; 27 28import android.content.ContentProviderClient; 29import android.database.Cursor; 30import android.net.Uri; 31import android.os.Bundle; 32import android.os.ParcelFileDescriptor; 33import android.os.ParcelFileDescriptor.AutoCloseInputStream; 34import android.os.ParcelFileDescriptor.AutoCloseOutputStream; 35import android.os.RemoteException; 36import android.provider.DocumentsContract; 37import android.provider.DocumentsContract.Document; 38import android.provider.DocumentsContract.Root; 39import android.support.annotation.Nullable; 40import android.test.MoreAsserts; 41import android.text.TextUtils; 42 43import com.android.documentsui.model.DocumentInfo; 44import com.android.documentsui.model.RootInfo; 45 46import com.google.android.collect.Lists; 47 48import libcore.io.IoUtils; 49import libcore.io.Streams; 50 51import java.io.IOException; 52import java.util.ArrayList; 53import java.util.List; 54 55/** 56 * Provides support for creation of documents in a test settings. 57 */ 58public class DocumentsProviderHelper { 59 60 private final String mAuthority; 61 private final ContentProviderClient mClient; 62 63 public DocumentsProviderHelper(String authority, ContentProviderClient client) { 64 checkArgument(!TextUtils.isEmpty(authority)); 65 mAuthority = authority; 66 mClient = client; 67 } 68 69 public RootInfo getRoot(String documentId) throws RemoteException { 70 final Uri rootsUri = buildRootsUri(mAuthority); 71 Cursor cursor = null; 72 try { 73 cursor = mClient.query(rootsUri, null, null, null, null); 74 while (cursor.moveToNext()) { 75 if (documentId.equals(getCursorString(cursor, Root.COLUMN_ROOT_ID))) { 76 return RootInfo.fromRootsCursor(mAuthority, cursor); 77 } 78 } 79 throw new IllegalArgumentException("Can't find matching root for id=" + documentId); 80 } catch (Exception e) { 81 throw new RuntimeException("Can't load root for id=" + documentId , e); 82 } finally { 83 IoUtils.closeQuietly(cursor); 84 } 85 } 86 87 public Uri createDocument(Uri parentUri, String mimeType, String name) { 88 if (name.contains("/")) { 89 throw new IllegalArgumentException("Name and mimetype probably interposed."); 90 } 91 try { 92 Uri uri = DocumentsContract.createDocument(mClient, parentUri, mimeType, name); 93 return uri; 94 } catch (RemoteException e) { 95 throw new RuntimeException("Couldn't create document: " + name + " with mimetype " 96 + mimeType, e); 97 } 98 } 99 100 public Uri createDocument(String parentId, String mimeType, String name) { 101 Uri parentUri = buildDocumentUri(mAuthority, parentId); 102 return createDocument(parentUri, mimeType, name); 103 } 104 105 public Uri createDocument(RootInfo root, String mimeType, String name) { 106 return createDocument(root.documentId, mimeType, name); 107 } 108 109 public Uri createDocumentWithFlags(String documentId, String mimeType, String name, int flags) 110 throws RemoteException { 111 Bundle in = new Bundle(); 112 in.putInt(StubProvider.EXTRA_FLAGS, flags); 113 in.putString(StubProvider.EXTRA_PARENT_ID, documentId); 114 in.putString(Document.COLUMN_MIME_TYPE, mimeType); 115 in.putString(Document.COLUMN_DISPLAY_NAME, name); 116 117 Bundle out = mClient.call("createDocumentWithFlags", null, in); 118 Uri uri = out.getParcelable(DocumentsContract.EXTRA_URI); 119 return uri; 120 } 121 122 public Uri createFolder(Uri parentUri, String name) { 123 return createDocument(parentUri, Document.MIME_TYPE_DIR, name); 124 } 125 126 public Uri createFolder(String parentId, String name) { 127 Uri parentUri = buildDocumentUri(mAuthority, parentId); 128 return createDocument(parentUri, Document.MIME_TYPE_DIR, name); 129 } 130 131 public Uri createFolder(RootInfo root, String name) { 132 return createDocument(root, Document.MIME_TYPE_DIR, name); 133 } 134 135 public void writeDocument(Uri documentUri, byte[] contents) 136 throws RemoteException, IOException { 137 ParcelFileDescriptor file = mClient.openFile(documentUri, "w", null); 138 try (AutoCloseOutputStream out = new AutoCloseOutputStream(file)) { 139 out.write(contents, 0, contents.length); 140 } 141 } 142 143 public byte[] readDocument(Uri documentUri) throws RemoteException, IOException { 144 ParcelFileDescriptor file = mClient.openFile(documentUri, "r", null); 145 byte[] buf = null; 146 try (AutoCloseInputStream in = new AutoCloseInputStream(file)) { 147 buf = Streams.readFully(in); 148 } 149 return buf; 150 } 151 152 public void assertChildCount(Uri parentUri, int expected) throws Exception { 153 List<DocumentInfo> children = listChildren(parentUri); 154 assertEquals("Incorrect file count after copy", expected, children.size()); 155 } 156 157 public void assertChildCount(String parentId, int expected) throws Exception { 158 List<DocumentInfo> children = listChildren(parentId); 159 assertEquals("Incorrect file count after copy", expected, children.size()); 160 } 161 162 public void assertChildCount(RootInfo root, int expected) throws Exception { 163 assertChildCount(root.documentId, expected); 164 } 165 166 public void assertHasFile(Uri parentUri, String name) throws Exception { 167 List<DocumentInfo> children = listChildren(parentUri); 168 for (DocumentInfo child : children) { 169 if (name.equals(child.displayName) && !child.isDirectory()) { 170 return; 171 } 172 } 173 fail("Could not find file named=" + name + " in children " + children); 174 } 175 176 public void assertHasFile(String parentId, String name) throws Exception { 177 Uri parentUri = buildDocumentUri(mAuthority, parentId); 178 assertHasFile(parentUri, name); 179 } 180 181 public void assertHasFile(RootInfo root, String name) throws Exception { 182 assertHasFile(root.documentId, name); 183 } 184 185 public void assertHasDirectory(Uri parentUri, String name) throws Exception { 186 List<DocumentInfo> children = listChildren(parentUri); 187 for (DocumentInfo child : children) { 188 if (name.equals(child.displayName) && child.isDirectory()) { 189 return; 190 } 191 } 192 fail("Could not find name=" + name + " in children " + children); 193 } 194 195 public void assertHasDirectory(String parentId, String name) throws Exception { 196 Uri parentUri = buildDocumentUri(mAuthority, parentId); 197 assertHasDirectory(parentUri, name); 198 } 199 200 public void assertHasDirectory(RootInfo root, String name) throws Exception { 201 assertHasDirectory(root.documentId, name); 202 } 203 204 public void assertDoesNotExist(Uri parentUri, String name) throws Exception { 205 List<DocumentInfo> children = listChildren(parentUri); 206 for (DocumentInfo child : children) { 207 if (name.equals(child.displayName)) { 208 fail("Found name=" + name + " in children " + children); 209 } 210 } 211 } 212 213 public void assertDoesNotExist(String parentId, String name) throws Exception { 214 Uri parentUri = buildDocumentUri(mAuthority, parentId); 215 assertDoesNotExist(parentUri, name); 216 } 217 218 public void assertDoesNotExist(RootInfo root, String name) throws Exception { 219 assertDoesNotExist(root.getUri(), name); 220 } 221 222 public @Nullable DocumentInfo findFile(String parentId, String name) 223 throws Exception { 224 List<DocumentInfo> children = listChildren(parentId); 225 for (DocumentInfo child : children) { 226 if (name.equals(child.displayName)) { 227 return child; 228 } 229 } 230 return null; 231 } 232 233 public DocumentInfo findDocument(String parentId, String name) throws Exception { 234 List<DocumentInfo> children = listChildren(parentId); 235 for (DocumentInfo child : children) { 236 if (name.equals(child.displayName)) { 237 return child; 238 } 239 } 240 return null; 241 } 242 243 public DocumentInfo findDocument(Uri parentUri, String name) throws Exception { 244 List<DocumentInfo> children = listChildren(parentUri); 245 for (DocumentInfo child : children) { 246 if (name.equals(child.displayName)) { 247 return child; 248 } 249 } 250 return null; 251 } 252 253 public List<DocumentInfo> listChildren(Uri parentUri) throws Exception { 254 String id = DocumentsContract.getDocumentId(parentUri); 255 return listChildren(id); 256 } 257 258 public List<DocumentInfo> listChildren(String documentId) throws Exception { 259 Uri uri = buildChildDocumentsUri(mAuthority, documentId); 260 List<DocumentInfo> children = new ArrayList<>(); 261 try (Cursor cursor = mClient.query(uri, null, null, null, null, null)) { 262 Cursor wrapper = new RootCursorWrapper(mAuthority, "totally-fake", cursor, 100); 263 while (wrapper.moveToNext()) { 264 children.add(DocumentInfo.fromDirectoryCursor(wrapper)); 265 } 266 } 267 return children; 268 } 269 270 public void assertFileContents(Uri documentUri, byte[] expected) throws Exception { 271 MoreAsserts.assertEquals( 272 "Copied file contents differ", 273 expected, readDocument(documentUri)); 274 } 275 276 public void assertFileContents(String parentId, String fileName, byte[] expected) 277 throws Exception { 278 DocumentInfo file = findFile(parentId, fileName); 279 assertNotNull(file); 280 assertFileContents(file.derivedUri, expected); 281 } 282 283 /** 284 * A helper method for StubProvider only. Won't work with other providers. 285 * @throws RemoteException 286 */ 287 public Uri createVirtualFile( 288 RootInfo root, String path, String mimeType, byte[] content, String... streamTypes) 289 throws RemoteException { 290 291 Bundle args = new Bundle(); 292 args.putString(StubProvider.EXTRA_ROOT, root.rootId); 293 args.putString(StubProvider.EXTRA_PATH, path); 294 args.putString(Document.COLUMN_MIME_TYPE, mimeType); 295 args.putStringArrayList(StubProvider.EXTRA_STREAM_TYPES, Lists.newArrayList(streamTypes)); 296 args.putByteArray(StubProvider.EXTRA_CONTENT, content); 297 298 Bundle result = mClient.call("createVirtualFile", null, args); 299 String documentId = result.getString(Document.COLUMN_DOCUMENT_ID); 300 301 return DocumentsContract.buildDocumentUri(mAuthority, documentId); 302 } 303 304} 305