Skip to content

Most visited

Recently visited

navigation
StorageProvider / src / com.example.android.storageprovider /

MyCloudProvider.java

1
/*
2
 * Copyright (C) 2013 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
 
18
package com.example.android.storageprovider;
19
 
20
import android.content.Context;
21
import android.content.SharedPreferences;
22
import android.content.res.AssetFileDescriptor;
23
import android.content.res.TypedArray;
24
import android.database.Cursor;
25
import android.database.MatrixCursor;
26
import android.graphics.Point;
27
import android.os.CancellationSignal;
28
import android.os.Handler;
29
import android.os.ParcelFileDescriptor;
30
import android.provider.DocumentsContract.Document;
31
import android.provider.DocumentsContract.Root;
32
import android.provider.DocumentsProvider;
33
import android.webkit.MimeTypeMap;
34
 
35
import com.example.android.common.logger.Log;
36
 
37
import java.io.ByteArrayOutputStream;
38
import java.io.File;
39
import java.io.FileNotFoundException;
40
import java.io.FileOutputStream;
41
import java.io.IOException;
42
import java.io.InputStream;
43
import java.util.Collections;
44
import java.util.Comparator;
45
import java.util.HashSet;
46
import java.util.LinkedList;
47
import java.util.PriorityQueue;
48
import java.util.Set;
49
 
50
/**
51
 * Manages documents and exposes them to the Android system for sharing.
52
 */
53
public class MyCloudProvider extends DocumentsProvider {
54
    private static final String TAG = "MyCloudProvider";
55
 
56
    // Use these as the default columns to return information about a root if no specific
57
    // columns are requested in a query.
58
    private static final String[] DEFAULT_ROOT_PROJECTION = new String[]{
59
            Root.COLUMN_ROOT_ID,
60
            Root.COLUMN_MIME_TYPES,
61
            Root.COLUMN_FLAGS,
62
            Root.COLUMN_ICON,
63
            Root.COLUMN_TITLE,
64
            Root.COLUMN_SUMMARY,
65
            Root.COLUMN_DOCUMENT_ID,
66
            Root.COLUMN_AVAILABLE_BYTES
67
    };
68
 
69
    // Use these as the default columns to return information about a document if no specific
70
    // columns are requested in a query.
71
    private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[]{
72
            Document.COLUMN_DOCUMENT_ID,
73
            Document.COLUMN_MIME_TYPE,
74
            Document.COLUMN_DISPLAY_NAME,
75
            Document.COLUMN_LAST_MODIFIED,
76
            Document.COLUMN_FLAGS,
77
            Document.COLUMN_SIZE
78
    };
79
 
80
    // No official policy on how many to return, but make sure you do limit the number of recent
81
    // and search results.
82
    private static final int MAX_SEARCH_RESULTS = 20;
83
    private static final int MAX_LAST_MODIFIED = 5;
84
 
85
    private static final String ROOT = "root";
86
 
87
    // A file object at the root of the file hierarchy.  Depending on your implementation, the root
88
    // does not need to be an existing file system directory.  For example, a tag-based document
89
    // provider might return a directory containing all tags, represented as child directories.
90
    private File mBaseDir;
91
 
92
    @Override
93
    public boolean onCreate() {
94
        Log.v(TAG, "onCreate");
95
 
96
        mBaseDir = getContext().getFilesDir();
97
 
98
        writeDummyFilesToStorage();
99
 
100
        return true;
101
    }
102
 
104
    @Override
105
    public Cursor queryRoots(String[] projection) throws FileNotFoundException {
106
        Log.v(TAG, "queryRoots");
107
 
108
        // Create a cursor with either the requested fields, or the default projection.  This
109
        // cursor is returned to the Android system picker UI and used to display all roots from
110
        // this provider.
111
        final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
112
 
113
        // If user is not logged in, return an empty root cursor.  This removes our provider from
114
        // the list entirely.
115
        if (!isUserLoggedIn()) {
116
            return result;
117
        }
118
 
119
        // It's possible to have multiple roots (e.g. for multiple accounts in the same app) -
120
        // just add multiple cursor rows.
121
        // Construct one row for a root called "MyCloud".
122
        final MatrixCursor.RowBuilder row = result.newRow();
123
 
124
        row.add(Root.COLUMN_ROOT_ID, ROOT);
125
        row.add(Root.COLUMN_SUMMARY, getContext().getString(R.string.root_summary));
126
 
127
        // FLAG_SUPPORTS_CREATE means at least one directory under the root supports creating
128
        // documents.  FLAG_SUPPORTS_RECENTS means your application's most recently used
129
        // documents will show up in the "Recents" category.  FLAG_SUPPORTS_SEARCH allows users
130
        // to search all documents the application shares.
131
        row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE |
132
                Root.FLAG_SUPPORTS_RECENTS |
133
                Root.FLAG_SUPPORTS_SEARCH);
134
 
135
        // COLUMN_TITLE is the root title (e.g. what will be displayed to identify your provider).
136
        row.add(Root.COLUMN_TITLE, getContext().getString(R.string.app_name));
137
 
138
        // This document id must be unique within this provider and consistent across time.  The
139
        // system picker UI may save it and refer to it later.
140
        row.add(Root.COLUMN_DOCUMENT_ID, getDocIdForFile(mBaseDir));
141
 
142
        // The child MIME types are used to filter the roots and only present to the user roots
143
        // that contain the desired type somewhere in their file hierarchy.
144
        row.add(Root.COLUMN_MIME_TYPES, getChildMimeTypes(mBaseDir));
145
        row.add(Root.COLUMN_AVAILABLE_BYTES, mBaseDir.getFreeSpace());
146
        row.add(Root.COLUMN_ICON, R.drawable.ic_launcher);
147
 
148
        return result;
149
    }
151
 
153
    @Override
154
    public Cursor queryRecentDocuments(String rootId, String[] projection)
155
            throws FileNotFoundException {
156
        Log.v(TAG, "queryRecentDocuments");
157
 
158
        // This example implementation walks a local file structure to find the most recently
159
        // modified files.  Other implementations might include making a network call to query a
160
        // server.
161
 
162
        // Create a cursor with the requested projection, or the default projection.
163
        final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
164
 
165
        final File parent = getFileForDocId(rootId);
166
 
167
        // Create a queue to store the most recent documents, which orders by last modified.
168
        PriorityQueue<File> lastModifiedFiles = new PriorityQueue<File>(5, new Comparator<File>() {
169
            public int compare(File i, File j) {
170
                return Long.compare(i.lastModified(), j.lastModified());
171
            }
172
        });
173
 
174
        // Iterate through all files and directories in the file structure under the root.  If
175
        // the file is more recent than the least recently modified, add it to the queue,
176
        // limiting the number of results.
177
        final LinkedList<File> pending = new LinkedList<File>();
178
 
179
        // Start by adding the parent to the list of files to be processed
180
        pending.add(parent);
181
 
182
        // Do while we still have unexamined files
183
        while (!pending.isEmpty()) {
184
            // Take a file from the list of unprocessed files
185
            final File file = pending.removeFirst();
186
            if (file.isDirectory()) {
187
                // If it's a directory, add all its children to the unprocessed list
188
                Collections.addAll(pending, file.listFiles());
189
            } else {
190
                // If it's a file, add it to the ordered queue.
191
                lastModifiedFiles.add(file);
192
            }
193
        }
194
 
195
        // Add the most recent files to the cursor, not exceeding the max number of results.
196
        for (int i = 0; i < Math.min(MAX_LAST_MODIFIED + 1, lastModifiedFiles.size()); i++) {
197
            final File file = lastModifiedFiles.remove();
198
            includeFile(result, null, file);
199
        }
200
        return result;
201
    }
203
 
205
    @Override
206
    public Cursor querySearchDocuments(String rootId, String query, String[] projection)
207
            throws FileNotFoundException {
208
        Log.v(TAG, "querySearchDocuments");
209
 
210
        // Create a cursor with the requested projection, or the default projection.
211
        final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
212
        final File parent = getFileForDocId(rootId);
213
 
214
        // This example implementation searches file names for the query and doesn't rank search
215
        // results, so we can stop as soon as we find a sufficient number of matches.  Other
216
        // implementations might use other data about files, rather than the file name, to
217
        // produce a match; it might also require a network call to query a remote server.
218
 
219
        // Iterate through all files in the file structure under the root until we reach the
220
        // desired number of matches.
221
        final LinkedList<File> pending = new LinkedList<File>();
222
 
223
        // Start by adding the parent to the list of files to be processed
224
        pending.add(parent);
225
 
226
        // Do while we still have unexamined files, and fewer than the max search results
227
        while (!pending.isEmpty() && result.getCount() < MAX_SEARCH_RESULTS) {
228
            // Take a file from the list of unprocessed files
229
            final File file = pending.removeFirst();
230
            if (file.isDirectory()) {
231
                // If it's a directory, add all its children to the unprocessed list
232
                Collections.addAll(pending, file.listFiles());
233
            } else {
234
                // If it's a file and it matches, add it to the result cursor.
235
                if (file.getName().toLowerCase().contains(query)) {
236
                    includeFile(result, null, file);
237
                }
238
            }
239
        }
240
        return result;
241
    }
243
 
245
    @Override
246
    public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint,
247
                                                     CancellationSignal signal)
248
            throws FileNotFoundException {
249
        Log.v(TAG, "openDocumentThumbnail");
250
 
251
        final File file = getFileForDocId(documentId);
252
        final ParcelFileDescriptor pfd =
253
                ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
254
        return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
255
    }
257
 
259
    @Override
260
    public Cursor queryDocument(String documentId, String[] projection)
261
            throws FileNotFoundException {
262
        Log.v(TAG, "queryDocument");
263
 
264
        // Create a cursor with the requested projection, or the default projection.
265
        final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
266
        includeFile(result, documentId, null);
267
        return result;
268
    }
270
 
272
    @Override
273
    public Cursor queryChildDocuments(String parentDocumentId, String[] projection,
274
                                      String sortOrder) throws FileNotFoundException {
275
        Log.v(TAG, "queryChildDocuments, parentDocumentId: " +
276
                parentDocumentId +
277
                " sortOrder: " +
278
                sortOrder);
279
 
280
        final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
281
        final File parent = getFileForDocId(parentDocumentId);
282
        for (File file : parent.listFiles()) {
283
            includeFile(result, null, file);
284
        }
285
        return result;
286
    }
288
 
289
 
291
    @Override
292
    public ParcelFileDescriptor openDocument(final String documentId, final String mode,
293
                                             CancellationSignal signal)
294
            throws FileNotFoundException {
295
        Log.v(TAG, "openDocument, mode: " + mode);
296
        // It's OK to do network operations in this method to download the document, as long as you
297
        // periodically check the CancellationSignal.  If you have an extremely large file to
298
        // transfer from the network, a better solution may be pipes or sockets
299
        // (see ParcelFileDescriptor for helper methods).
300
 
301
        final File file = getFileForDocId(documentId);
302
        final int accessMode = ParcelFileDescriptor.parseMode(mode);
303
 
304
        final boolean isWrite = (mode.indexOf('w') != -1);
305
        if (isWrite) {
306
            // Attach a close listener if the document is opened in write mode.
307
            try {