Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.ProviderInfo; |
| 21 | import android.database.Cursor; |
| 22 | import android.database.MatrixCursor.RowBuilder; |
| 23 | import android.database.MatrixCursor; |
| 24 | import android.os.CancellationSignal; |
| 25 | import android.os.FileUtils; |
| 26 | import android.os.ParcelFileDescriptor; |
| 27 | import android.provider.DocumentsContract.Document; |
| 28 | import android.provider.DocumentsContract.Root; |
| 29 | import android.provider.DocumentsContract; |
| 30 | import android.provider.DocumentsProvider; |
| 31 | |
| 32 | import java.io.File; |
| 33 | import java.io.FileNotFoundException; |
| 34 | import java.util.ArrayList; |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 35 | import java.util.HashMap; |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 36 | import java.util.List; |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 37 | import java.util.Map; |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 38 | import java.util.Random; |
| 39 | |
| 40 | /** |
| 41 | * Provider with thousands of files for testing loading time of directories in DocumentsUI. |
| 42 | * It doesn't support any file operations. |
| 43 | */ |
| 44 | public class StressProvider extends DocumentsProvider { |
| 45 | |
| 46 | public static final String DEFAULT_AUTHORITY = "com.android.documentsui.stressprovider"; |
| 47 | |
| 48 | // Empty root. |
| 49 | public static final String STRESS_ROOT_0_ID = "STRESS_ROOT_0"; |
| 50 | |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 51 | // Root with thousands of directories. |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 52 | public static final String STRESS_ROOT_1_ID = "STRESS_ROOT_1"; |
| 53 | |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 54 | // Root with hundreds of files. |
| 55 | public static final String STRESS_ROOT_2_ID = "STRESS_ROOT_2"; |
| 56 | |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 57 | private static final String STRESS_ROOT_0_DOC_ID = "STRESS_ROOT_0_DOC"; |
| 58 | private static final String STRESS_ROOT_1_DOC_ID = "STRESS_ROOT_1_DOC"; |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 59 | private static final String STRESS_ROOT_2_DOC_ID = "STRESS_ROOT_2_DOC"; |
| 60 | |
| 61 | private static final int STRESS_ROOT_1_ITEMS = 10000; |
| 62 | private static final int STRESS_ROOT_2_ITEMS = 300; |
| 63 | |
| 64 | private static final String MIME_TYPE_IMAGE = "image/jpeg"; |
| 65 | private static final long REFERENCE_TIMESTAMP = 1459159369359L; |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 66 | |
| 67 | private static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
| 68 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 69 | Root.COLUMN_AVAILABLE_BYTES |
| 70 | }; |
| 71 | private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
| 72 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME, |
| 73 | Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 74 | }; |
| 75 | |
| 76 | private String mAuthority = DEFAULT_AUTHORITY; |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 77 | |
| 78 | // Map from a root document id to children document ids. |
| 79 | private Map<String, ArrayList<StubDocument>> mChildDocuments = new HashMap<>(); |
| 80 | |
| 81 | private Map<String, StubDocument> mDocuments = new HashMap<>(); |
| 82 | private Map<String, StubRoot> mRoots = new HashMap<>(); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public void attachInfo(Context context, ProviderInfo info) { |
| 86 | mAuthority = info.authority; |
| 87 | super.attachInfo(context, info); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean onCreate() { |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 92 | StubDocument document; |
| 93 | |
| 94 | ArrayList<StubDocument> children = new ArrayList<StubDocument>(); |
| 95 | mChildDocuments.put(STRESS_ROOT_1_DOC_ID, children); |
| 96 | for (int i = 0; i < STRESS_ROOT_1_ITEMS; i++) { |
| 97 | document = StubDocument.createDirectory(i); |
| 98 | mDocuments.put(document.id, document); |
| 99 | children.add(document); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 100 | } |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 101 | |
| 102 | children = new ArrayList<StubDocument>(); |
| 103 | mChildDocuments.put(STRESS_ROOT_2_DOC_ID, children); |
| 104 | for (int i = 0; i < STRESS_ROOT_2_ITEMS; i++) { |
| 105 | document = StubDocument.createFile(STRESS_ROOT_1_ITEMS + i); |
| 106 | mDocuments.put(document.id, document); |
| 107 | children.add(document); |
| 108 | } |
| 109 | |
| 110 | mRoots.put(STRESS_ROOT_0_ID, new StubRoot(STRESS_ROOT_0_ID, STRESS_ROOT_0_DOC_ID)); |
| 111 | mRoots.put(STRESS_ROOT_1_ID, new StubRoot(STRESS_ROOT_1_ID, STRESS_ROOT_1_DOC_ID)); |
| 112 | mRoots.put(STRESS_ROOT_2_ID, new StubRoot(STRESS_ROOT_2_ID, STRESS_ROOT_2_DOC_ID)); |
| 113 | |
| 114 | mDocuments.put(STRESS_ROOT_0_DOC_ID, StubDocument.createDirectory(STRESS_ROOT_0_DOC_ID)); |
| 115 | mDocuments.put(STRESS_ROOT_1_DOC_ID, StubDocument.createDirectory(STRESS_ROOT_1_DOC_ID)); |
| 116 | mDocuments.put(STRESS_ROOT_2_DOC_ID, StubDocument.createDirectory(STRESS_ROOT_2_DOC_ID)); |
| 117 | |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
| 123 | final MatrixCursor result = new MatrixCursor(DEFAULT_ROOT_PROJECTION); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 124 | for (StubRoot root : mRoots.values()) { |
| 125 | includeRoot(result, root); |
| 126 | } |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 127 | return result; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Cursor queryDocument(String documentId, String[] projection) |
| 132 | throws FileNotFoundException { |
| 133 | final MatrixCursor result = new MatrixCursor(DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 134 | final StubDocument document = mDocuments.get(documentId); |
| 135 | includeDocument(result, document); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 136 | return result; |
| 137 | } |
| 138 | |
| 139 | @Override |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 140 | public Cursor queryChildDocuments(String parentDocumentId, String[] projection, |
| 141 | String sortOrder) |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 142 | throws FileNotFoundException { |
| 143 | final MatrixCursor result = new MatrixCursor(DEFAULT_DOCUMENT_PROJECTION); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 144 | final ArrayList<StubDocument> childDocuments = mChildDocuments.get(parentDocumentId); |
| 145 | if (childDocuments != null) { |
| 146 | for (StubDocument document : childDocuments) { |
| 147 | includeDocument(result, document); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | @Override |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 154 | public ParcelFileDescriptor openDocument(String docId, String mode, |
| 155 | CancellationSignal signal) |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 156 | throws FileNotFoundException { |
| 157 | throw new UnsupportedOperationException(); |
| 158 | } |
| 159 | |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 160 | private void includeRoot(MatrixCursor result, StubRoot root) { |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 161 | final RowBuilder row = result.newRow(); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 162 | row.add(Root.COLUMN_ROOT_ID, root.id); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 163 | row.add(Root.COLUMN_FLAGS, 0); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 164 | row.add(Root.COLUMN_TITLE, root.id); |
| 165 | row.add(Root.COLUMN_DOCUMENT_ID, root.documentId); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 166 | } |
| 167 | |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 168 | private void includeDocument(MatrixCursor result, StubDocument document) { |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 169 | final RowBuilder row = result.newRow(); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 170 | row.add(Document.COLUMN_DOCUMENT_ID, document.id); |
| 171 | row.add(Document.COLUMN_DISPLAY_NAME, document.id); |
| 172 | row.add(Document.COLUMN_SIZE, document.size); |
| 173 | row.add(Document.COLUMN_MIME_TYPE, document.mimeType); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 174 | row.add(Document.COLUMN_FLAGS, 0); |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 175 | row.add(Document.COLUMN_LAST_MODIFIED, document.lastModified); |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 176 | } |
| 177 | |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 178 | private static String getStubDocumentIdForFile(File file) { |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 179 | return file.getAbsolutePath(); |
| 180 | } |
| 181 | |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 182 | private static class StubDocument { |
| 183 | final String mimeType; |
| 184 | final String id; |
| 185 | final int size; |
| 186 | final long lastModified; |
| 187 | |
| 188 | private StubDocument(String mimeType, String id, int size, long lastModified) { |
| 189 | this.mimeType = mimeType; |
| 190 | this.id = id; |
| 191 | this.size = size; |
| 192 | this.lastModified = lastModified; |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 193 | } |
Tomasz Mikolajewski | 8ee8ebf | 2016-03-28 12:13:43 +0900 | [diff] [blame] | 194 | |
| 195 | public static StubDocument createDirectory(int index) { |
| 196 | return new StubDocument( |
| 197 | DocumentsContract.Document.MIME_TYPE_DIR, createRandomId(index), 0, |
| 198 | createRandomTime(index)); |
| 199 | } |
| 200 | |
| 201 | public static StubDocument createDirectory(String id) { |
| 202 | return new StubDocument(DocumentsContract.Document.MIME_TYPE_DIR, id, 0, 0); |
| 203 | } |
| 204 | |
| 205 | public static StubDocument createFile(int index) { |
| 206 | return new StubDocument( |
| 207 | MIME_TYPE_IMAGE, createRandomId(index), createRandomSize(index), |
| 208 | createRandomTime(index)); |
| 209 | } |
| 210 | |
| 211 | private static String createRandomId(int index) { |
| 212 | final Random random = new Random(index); |
| 213 | final StringBuilder builder = new StringBuilder(); |
| 214 | for (int i = 0; i < 20; i++) { |
| 215 | builder.append((char) (random.nextInt(96) + 32)); |
| 216 | } |
| 217 | builder.append(index); // Append a number to guarantee uniqueness. |
| 218 | return builder.toString(); |
| 219 | } |
| 220 | |
| 221 | private static int createRandomSize(int index) { |
| 222 | final Random random = new Random(index); |
| 223 | return random.nextInt(1024 * 1024 * 100); // Up to 100 MB. |
| 224 | } |
| 225 | |
| 226 | private static long createRandomTime(int index) { |
| 227 | final Random random = new Random(index); |
| 228 | // Up to 30 days backwards from REFERENCE_TIMESTAMP. |
| 229 | return REFERENCE_TIMESTAMP - random.nextLong() % 1000L * 60 * 60 * 24 * 30; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private static class StubRoot { |
| 234 | final String id; |
| 235 | final String documentId; |
| 236 | |
| 237 | public StubRoot(String id, String documentId) { |
| 238 | this.id = id; |
| 239 | this.documentId = documentId; |
| 240 | } |
Tomasz Mikolajewski | ae6d6b4 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 241 | } |
| 242 | } |