summaryrefslogtreecommitdiffstats
path: root/packages/DocumentsUI/src/com/android/documentsui/model/DocumentsProviderInfo.java
blob: 96eb58ee246262de6f06499902bf114a947820c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.model;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.provider.DocumentsContract;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;

import com.android.documentsui.DocumentsActivity;
import com.google.android.collect.Lists;

import libcore.io.IoUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.List;

/**
 * Representation of a storage backend.
 */
public class DocumentsProviderInfo {
    private static final String TAG = DocumentsActivity.TAG;

    public ProviderInfo providerInfo;
    public boolean customRoots;
    public List<Icon> customIcons;

    public static class Icon {
        public String mimeType;
        public Drawable icon;
    }

    private static final String TAG_DOCUMENTS_PROVIDER = "documents-provider";
    private static final String TAG_ICON = "icon";

    public static DocumentsProviderInfo buildRecents(Context context, ProviderInfo providerInfo) {
        final DocumentsProviderInfo info = new DocumentsProviderInfo();
        info.providerInfo = providerInfo;
        info.customRoots = false;
        return info;
    }

    public static DocumentsProviderInfo parseInfo(Context context, ProviderInfo providerInfo) {
        final DocumentsProviderInfo info = new DocumentsProviderInfo();
        info.providerInfo = providerInfo;
        info.customIcons = Lists.newArrayList();

        final PackageManager pm = context.getPackageManager();
        final Resources res;
        try {
            res = pm.getResourcesForApplication(providerInfo.applicationInfo);
        } catch (NameNotFoundException e) {
            Log.w(TAG, "Failed to find resources for " + providerInfo, e);
            return null;
        }

        XmlResourceParser parser = null;
        try {
            parser = providerInfo.loadXmlMetaData(
                    pm, DocumentsContract.META_DATA_DOCUMENT_PROVIDER);
            AttributeSet attrs = Xml.asAttributeSet(parser);

            int type = 0;
            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
                final String tag = parser.getName();
                if (type == XmlPullParser.START_TAG && TAG_DOCUMENTS_PROVIDER.equals(tag)) {
                    final TypedArray a = res.obtainAttributes(
                            attrs, com.android.internal.R.styleable.DocumentsProviderInfo);
                    info.customRoots = a.getBoolean(
                            com.android.internal.R.styleable.DocumentsProviderInfo_customRoots,
                            false);
                    a.recycle();

                } else if (type == XmlPullParser.START_TAG && TAG_ICON.equals(tag)) {
                    final TypedArray a = res.obtainAttributes(
                            attrs, com.android.internal.R.styleable.Icon);
                    final Icon icon = new Icon();
                    icon.mimeType = a.getString(com.android.internal.R.styleable.Icon_mimeType);
                    icon.icon = a.getDrawable(com.android.internal.R.styleable.Icon_icon);
                    info.customIcons.add(icon);
                    a.recycle();
                }
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to parse metadata", e);
            return null;
        } catch (XmlPullParserException e) {
            Log.w(TAG, "Failed to parse metadata", e);
            return null;
        } finally {
            IoUtils.closeQuietly(parser);
        }

        return info;
    }
}