RootInfo.java revision 0b14db3cf5eac43736462999337c9a3efdc1ac81
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;
29
30import java.util.Objects;
31
32/**
33 * Representation of a {@link Root}.
34 */
35public class RootInfo {
36    public String authority;
37    public String rootId;
38    public int rootType;
39    public int flags;
40    public int icon;
41    public String title;
42    public String summary;
43    public String documentId;
44    public long availableBytes;
45
46    public static RootInfo fromRootsCursor(String authority, Cursor cursor) {
47        final RootInfo root = new RootInfo();
48        root.authority = authority;
49        root.rootId = getCursorString(cursor, Root.COLUMN_ROOT_ID);
50        root.rootType = getCursorInt(cursor, Root.COLUMN_ROOT_TYPE);
51        root.flags = getCursorInt(cursor, Root.COLUMN_FLAGS);
52        root.icon = getCursorInt(cursor, Root.COLUMN_ICON);
53        root.title = getCursorString(cursor, Root.COLUMN_TITLE);
54        root.summary = getCursorString(cursor, Root.COLUMN_SUMMARY);
55        root.documentId = getCursorString(cursor, Root.COLUMN_DOCUMENT_ID);
56        root.availableBytes = getCursorLong(cursor, Root.COLUMN_AVAILABLE_BYTES);
57
58        // TODO: remove this hack
59        if ("com.google.android.apps.docs.storage".equals(root.authority)) {
60            root.flags &= ~(Root.FLAG_PROVIDES_AUDIO | Root.FLAG_PROVIDES_IMAGES
61                    | Root.FLAG_PROVIDES_VIDEO);
62        }
63
64        return root;
65    }
66
67    public Drawable loadIcon(Context context) {
68        return IconUtils.loadPackageIcon(context, authority, icon);
69    }
70
71    @Override
72    public boolean equals(Object o) {
73        if (o instanceof RootInfo) {
74            final RootInfo root = (RootInfo) o;
75            return Objects.equals(authority, root.authority) && Objects.equals(rootId, root.rootId);
76        } else {
77            return false;
78        }
79    }
80
81    @Override
82    public int hashCode() {
83        return Objects.hash(authority, rootId);
84    }
85
86    public String getDirectoryString() {
87        return (summary != null) ? summary : title;
88    }
89}
90