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
17package com.android.documentsui.picker;
18
19import static com.android.documentsui.base.State.ACTION_CREATE;
20import static com.android.documentsui.base.State.ACTION_GET_CONTENT;
21import static com.android.documentsui.base.State.ACTION_OPEN;
22import static com.android.documentsui.base.State.ACTION_OPEN_TREE;
23import static com.android.documentsui.base.State.ACTION_PICK_COPY_DESTINATION;
24
25import android.provider.DocumentsContract.Document;
26
27import com.android.documentsui.ActivityConfig;
28import com.android.documentsui.base.MimeTypes;
29import com.android.documentsui.base.State;
30
31/**
32 * Provides support for Platform specific specializations of DirectoryFragment.
33 */
34final class Config extends ActivityConfig {
35
36    @Override
37    public boolean canSelectType(String docMimeType, int docFlags, State state) {
38        if (!isDocumentEnabled(docMimeType, docFlags, state)) {
39            return false;
40        }
41
42        if (MimeTypes.isDirectoryType(docMimeType)) {
43            return false;
44        }
45
46        if (state.action == ACTION_OPEN_TREE || state.action == ACTION_PICK_COPY_DESTINATION) {
47            // In this case nothing *ever* is selectable...the expected user behavior is
48            // they navigate *into* a folder, then click a confirmation button indicating
49            // that the current directory is the directory they are picking.
50            return false;
51        }
52
53        return true;
54    }
55
56    @Override
57    public boolean isDocumentEnabled(String mimeType, int docFlags, State state) {
58        // Directories are always enabled.
59        if (MimeTypes.isDirectoryType(mimeType)) {
60            return true;
61        }
62
63        switch (state.action) {
64            case ACTION_CREATE:
65                // Read-only files are disabled when creating.
66                if ((docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
67                    return false;
68                }
69            case ACTION_OPEN:
70            case ACTION_GET_CONTENT:
71                final boolean isVirtual = (docFlags & Document.FLAG_VIRTUAL_DOCUMENT) != 0;
72                if (isVirtual && state.openableOnly) {
73                    return false;
74                }
75        }
76
77        return MimeTypes.mimeMatches(state.acceptMimes, mimeType);
78    }
79}
80