1 /* 2 * Copyright (C) 2017 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.testing; 18 19 import android.database.MatrixCursor; 20 import android.os.Bundle; 21 import android.provider.DocumentsContract; 22 import android.provider.DocumentsContract.Document; 23 24 import com.android.documentsui.DirectoryResult; 25 import com.android.documentsui.Model; 26 import com.android.documentsui.base.DocumentInfo; 27 import com.android.documentsui.base.Features; 28 import com.android.documentsui.roots.RootCursorWrapper; 29 30 import libcore.net.MimeUtils; 31 32 import java.util.Random; 33 34 public class TestModel extends Model { 35 36 static final String[] COLUMNS = new String[]{ 37 RootCursorWrapper.COLUMN_AUTHORITY, 38 Document.COLUMN_DOCUMENT_ID, 39 Document.COLUMN_FLAGS, 40 Document.COLUMN_DISPLAY_NAME, 41 Document.COLUMN_SIZE, 42 Document.COLUMN_MIME_TYPE 43 }; 44 45 private final String mAuthority; 46 private int mLastId = 0; 47 private Random mRand = new Random(); 48 private MatrixCursor mCursor; 49 TestModel(String authority, Features features)50 public TestModel(String authority, Features features) { 51 super(features); 52 mAuthority = authority; 53 reset(); 54 } 55 reset()56 public void reset() { 57 mLastId = 0; 58 mCursor = new MatrixCursor(COLUMNS); 59 } 60 update()61 public void update() { 62 DirectoryResult r = new DirectoryResult(); 63 r.cursor = mCursor; 64 super.update(r); 65 } 66 setCursorExtras(Bundle bundle)67 public void setCursorExtras(Bundle bundle) { 68 mCursor.setExtras(bundle); 69 } 70 createFile(String name)71 public DocumentInfo createFile(String name) { 72 return createFile( 73 name, 74 Document.FLAG_SUPPORTS_WRITE 75 | Document.FLAG_SUPPORTS_DELETE 76 | Document.FLAG_SUPPORTS_RENAME); 77 } 78 createFile(String name, int flags)79 public DocumentInfo createFile(String name, int flags) { 80 return createDocument( 81 name, 82 guessMimeType(name), 83 flags); 84 } 85 createFolder(String name)86 public DocumentInfo createFolder(String name) { 87 return createFolder( 88 name, 89 Document.FLAG_SUPPORTS_WRITE 90 | Document.FLAG_SUPPORTS_DELETE 91 | Document.FLAG_SUPPORTS_REMOVE 92 | Document.FLAG_DIR_SUPPORTS_CREATE); 93 } 94 createFolder(String name, int flags)95 public DocumentInfo createFolder(String name, int flags) { 96 return createDocument( 97 name, 98 DocumentsContract.Document.MIME_TYPE_DIR, 99 flags); 100 } 101 createDocument(String name, String mimeType, int flags)102 public DocumentInfo createDocument(String name, String mimeType, int flags) { 103 DocumentInfo doc = new DocumentInfo(); 104 doc.authority = mAuthority; 105 doc.documentId = Integer.toString(++mLastId); 106 doc.derivedUri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId); 107 doc.displayName = name; 108 doc.mimeType = mimeType; 109 doc.flags = flags; 110 doc.size = mRand.nextInt(); 111 112 addToCursor(doc); 113 114 return doc; 115 } 116 addToCursor(DocumentInfo doc)117 private void addToCursor(DocumentInfo doc) { 118 MatrixCursor.RowBuilder row = mCursor.newRow(); 119 row.add(Document.COLUMN_DOCUMENT_ID, doc.documentId); 120 row.add(RootCursorWrapper.COLUMN_AUTHORITY, doc.authority); 121 row.add(Document.COLUMN_DISPLAY_NAME, doc.displayName); 122 row.add(Document.COLUMN_MIME_TYPE, doc.mimeType); 123 row.add(Document.COLUMN_FLAGS, doc.flags); 124 row.add(Document.COLUMN_SIZE, doc.size); 125 } 126 guessMimeType(String name)127 private static String guessMimeType(String name) { 128 int i = name.indexOf('.'); 129 130 while(i != -1) { 131 name = name.substring(i + 1); 132 String type = MimeUtils.guessMimeTypeFromExtension(name); 133 if (type != null) { 134 return type; 135 } 136 i = name.indexOf('.'); 137 } 138 139 return "text/plain"; 140 } 141 } 142