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 import android.webkit.MimeTypeMap;
24 
25 import com.android.documentsui.DirectoryResult;
26 import com.android.documentsui.Model;
27 import com.android.documentsui.base.DocumentInfo;
28 import com.android.documentsui.base.Features;
29 import com.android.documentsui.base.UserId;
30 import com.android.documentsui.roots.RootCursorWrapper;
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         RootCursorWrapper.COLUMN_USER_ID,
39         Document.COLUMN_DOCUMENT_ID,
40         Document.COLUMN_FLAGS,
41         Document.COLUMN_DISPLAY_NAME,
42         Document.COLUMN_SIZE,
43         Document.COLUMN_MIME_TYPE
44     };
45 
46     public final UserId mUserId;
47     private final String mAuthority;
48     private int mLastId = 0;
49     private Random mRand = new Random();
50     private MatrixCursor mCursor;
51 
TestModel(UserId userId, String authority, Features features)52     public TestModel(UserId userId, String authority, Features features) {
53         super(features);
54         mUserId = userId;
55         mAuthority = authority;
56         reset();
57     }
58 
59     @Override
reset()60     public void reset() {
61         mLastId = 0;
62         mCursor = new MatrixCursor(COLUMNS);
63     }
64 
update()65     public void update() {
66         DirectoryResult r = new DirectoryResult();
67         r.cursor = mCursor;
68         super.update(r);
69     }
70 
setCursorExtras(Bundle bundle)71     public void setCursorExtras(Bundle bundle) {
72         mCursor.setExtras(bundle);
73     }
74 
createFile(String name)75     public DocumentInfo createFile(String name) {
76         return createFile(
77                 name,
78                 Document.FLAG_SUPPORTS_WRITE
79                         | Document.FLAG_SUPPORTS_DELETE
80                         | Document.FLAG_SUPPORTS_RENAME);
81     }
82 
createFile(String name, int flags)83     public DocumentInfo createFile(String name, int flags) {
84         return createDocument(
85                 name,
86                 guessMimeType(name),
87                 flags);
88     }
89 
createFolder(String name)90     public DocumentInfo createFolder(String name) {
91         return createFolder(
92                 name,
93                 Document.FLAG_SUPPORTS_WRITE
94                         | Document.FLAG_SUPPORTS_DELETE
95                         | Document.FLAG_SUPPORTS_REMOVE
96                         | Document.FLAG_DIR_SUPPORTS_CREATE);
97     }
98 
createFolder(String name, int flags)99     public DocumentInfo createFolder(String name, int flags) {
100         return createDocument(
101                 name,
102                 DocumentsContract.Document.MIME_TYPE_DIR,
103                 flags);
104     }
105 
createDocumentForUser(String name, String mimeType, int flags, UserId userId)106     public DocumentInfo createDocumentForUser(String name, String mimeType, int flags,
107             UserId userId) {
108         DocumentInfo doc = new DocumentInfo();
109         doc.userId = userId;
110         doc.authority = mAuthority;
111         doc.documentId = Integer.toString(++mLastId);
112         doc.derivedUri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
113         doc.displayName = name;
114         doc.mimeType = mimeType;
115         doc.flags = flags;
116         doc.size = mRand.nextInt();
117 
118         addToCursor(doc);
119 
120         return doc;
121     }
122 
createDocument(String name, String mimeType, int flags)123     public DocumentInfo createDocument(String name, String mimeType, int flags) {
124         return createDocumentForUser(name, mimeType, flags, mUserId);
125     }
126 
addToCursor(DocumentInfo doc)127     private void addToCursor(DocumentInfo doc) {
128         MatrixCursor.RowBuilder row = mCursor.newRow();
129         row.add(Document.COLUMN_DOCUMENT_ID, doc.documentId);
130         row.add(RootCursorWrapper.COLUMN_AUTHORITY, doc.authority);
131         row.add(RootCursorWrapper.COLUMN_USER_ID, doc.userId);
132         row.add(Document.COLUMN_DISPLAY_NAME, doc.displayName);
133         row.add(Document.COLUMN_MIME_TYPE, doc.mimeType);
134         row.add(Document.COLUMN_FLAGS, doc.flags);
135         row.add(Document.COLUMN_SIZE, doc.size);
136     }
137 
guessMimeType(String name)138     private static String guessMimeType(String name) {
139         int i = name.indexOf('.');
140 
141         while(i != -1) {
142             name = name.substring(i + 1);
143             String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(name);
144             if (type != null) {
145                 return type;
146             }
147             i = name.indexOf('.');
148         }
149 
150         return "text/plain";
151     }
152 }
153