• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
56     @Override
reset()57     public void reset() {
58         mLastId = 0;
59         mCursor = new MatrixCursor(COLUMNS);
60     }
61 
update()62     public void update() {
63         DirectoryResult r = new DirectoryResult();
64         r.cursor = mCursor;
65         super.update(r);
66     }
67 
setCursorExtras(Bundle bundle)68     public void setCursorExtras(Bundle bundle) {
69         mCursor.setExtras(bundle);
70     }
71 
createFile(String name)72     public DocumentInfo createFile(String name) {
73         return createFile(
74                 name,
75                 Document.FLAG_SUPPORTS_WRITE
76                         | Document.FLAG_SUPPORTS_DELETE
77                         | Document.FLAG_SUPPORTS_RENAME);
78     }
79 
createFile(String name, int flags)80     public DocumentInfo createFile(String name, int flags) {
81         return createDocument(
82                 name,
83                 guessMimeType(name),
84                 flags);
85     }
86 
createFolder(String name)87     public DocumentInfo createFolder(String name) {
88         return createFolder(
89                 name,
90                 Document.FLAG_SUPPORTS_WRITE
91                         | Document.FLAG_SUPPORTS_DELETE
92                         | Document.FLAG_SUPPORTS_REMOVE
93                         | Document.FLAG_DIR_SUPPORTS_CREATE);
94     }
95 
createFolder(String name, int flags)96     public DocumentInfo createFolder(String name, int flags) {
97         return createDocument(
98                 name,
99                 DocumentsContract.Document.MIME_TYPE_DIR,
100                 flags);
101     }
102 
createDocument(String name, String mimeType, int flags)103     public DocumentInfo createDocument(String name, String mimeType, int flags) {
104         DocumentInfo doc = new DocumentInfo();
105         doc.authority = mAuthority;
106         doc.documentId = Integer.toString(++mLastId);
107         doc.derivedUri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
108         doc.displayName = name;
109         doc.mimeType = mimeType;
110         doc.flags = flags;
111         doc.size = mRand.nextInt();
112 
113         addToCursor(doc);
114 
115         return doc;
116     }
117 
addToCursor(DocumentInfo doc)118     private void addToCursor(DocumentInfo doc) {
119         MatrixCursor.RowBuilder row = mCursor.newRow();
120         row.add(Document.COLUMN_DOCUMENT_ID, doc.documentId);
121         row.add(RootCursorWrapper.COLUMN_AUTHORITY, doc.authority);
122         row.add(Document.COLUMN_DISPLAY_NAME, doc.displayName);
123         row.add(Document.COLUMN_MIME_TYPE, doc.mimeType);
124         row.add(Document.COLUMN_FLAGS, doc.flags);
125         row.add(Document.COLUMN_SIZE, doc.size);
126     }
127 
guessMimeType(String name)128     private static String guessMimeType(String name) {
129         int i = name.indexOf('.');
130 
131         while(i != -1) {
132             name = name.substring(i + 1);
133             String type = MimeUtils.guessMimeTypeFromExtension(name);
134             if (type != null) {
135                 return type;
136             }
137             i = name.indexOf('.');
138         }
139 
140         return "text/plain";
141     }
142 }
143