RootInfo.java revision 6d97d3c1a2ebac4e3f32c7e5bc134864ace1c17f
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
17package com.android.documentsui.model;
18
19import static com.android.documentsui.model.DocumentInfo.getCursorInt;
20import static com.android.documentsui.model.DocumentInfo.getCursorLong;
21import static com.android.documentsui.model.DocumentInfo.getCursorString;
22
23import android.content.Context;
24import android.database.Cursor;
25import android.graphics.drawable.Drawable;
26import android.provider.DocumentsContract.Root;
27
28import com.android.documentsui.IconUtils;
29import com.android.documentsui.R;
30
31import java.util.Objects;
32
33/**
34 * Representation of a {@link Root}.
35 */
36public class RootInfo {
37    public String authority;
38    public String rootId;
39    public int rootType;
40    public int flags;
41    public int icon;
42    public int localIcon;
43    public String title;
44    public String summary;
45    public String documentId;
46    public long availableBytes;
47    public String[] mimeTypes;
48
49    public static RootInfo fromRootsCursor(String authority, Cursor cursor) {
50        final RootInfo root = new RootInfo();
51        root.authority = authority;
52        root.rootId = getCursorString(cursor, Root.COLUMN_ROOT_ID);
53        root.rootType = getCursorInt(cursor, Root.COLUMN_ROOT_TYPE);
54        root.flags = getCursorInt(cursor, Root.COLUMN_FLAGS);
55        root.icon = getCursorInt(cursor, Root.COLUMN_ICON);
56        root.title = getCursorString(cursor, Root.COLUMN_TITLE);
57        root.summary = getCursorString(cursor, Root.COLUMN_SUMMARY);
58        root.documentId = getCursorString(cursor, Root.COLUMN_DOCUMENT_ID);
59        root.availableBytes = getCursorLong(cursor, Root.COLUMN_AVAILABLE_BYTES);
60
61        final String raw = getCursorString(cursor, Root.COLUMN_MIME_TYPES);
62        root.mimeTypes = (raw != null) ? raw.split("\n") : null;
63
64        // TODO: remove these special case icons
65        if ("com.android.externalstorage.documents".equals(authority)) {
66            root.localIcon = R.drawable.ic_root_sdcard;
67        }
68        if ("com.android.providers.downloads.documents".equals(authority)) {
69            root.localIcon = R.drawable.ic_root_download;
70        }
71        if ("com.android.providers.media.documents".equals(authority)) {
72            if ("image".equals(root.rootId)) {
73                root.localIcon = R.drawable.ic_doc_image;
74            } else if ("audio".equals(root.rootId)) {
75                root.localIcon = R.drawable.ic_doc_audio;
76            }
77        }
78
79        return root;
80    }
81
82    public Drawable loadIcon(Context context) {
83        if (localIcon != 0) {
84            return context.getResources().getDrawable(localIcon);
85        } else {
86            return IconUtils.loadPackageIcon(context, authority, icon);
87        }
88    }
89
90    @Override
91    public boolean equals(Object o) {
92        if (o instanceof RootInfo) {
93            final RootInfo root = (RootInfo) o;
94            return Objects.equals(authority, root.authority) && Objects.equals(rootId, root.rootId);
95        } else {
96            return false;
97        }
98    }
99
100    @Override
101    public int hashCode() {
102        return Objects.hash(authority, rootId);
103    }
104
105    public String getDirectoryString() {
106        return (summary != null) ? summary : title;
107    }
108}
109