1/*
2 * Copyright (C) 2009 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 android.provider;
18
19import android.content.ComponentName;
20import android.content.ContentResolver;
21import android.database.Cursor;
22import android.net.Uri;
23
24import java.util.List;
25
26/**
27 * The Applications provider gives information about installed applications.
28 *
29 * @hide Only used by ApplicationsProvider so far.
30 */
31public class Applications {
32
33    /**
34     * The content authority for this provider.
35     */
36    public static final String AUTHORITY = "applications";
37
38    /**
39     * The content:// style URL for this provider
40     */
41    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
42
43    /**
44     * The content path for application component URIs.
45     */
46    public static final String APPLICATION_PATH = "applications";
47
48    /**
49     * The content path for application search.
50     */
51    public static final String SEARCH_PATH = "search";
52
53    private static final String APPLICATION_SUB_TYPE = "vnd.android.application";
54
55    /**
56     * The MIME type for a single application item.
57     */
58    public static final String APPLICATION_ITEM_TYPE =
59            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + APPLICATION_SUB_TYPE;
60
61    /**
62     * The MIME type for a list of application items.
63     */
64    public static final String APPLICATION_DIR_TYPE =
65            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + APPLICATION_SUB_TYPE;
66
67    /**
68     * no public constructor since this is a utility class
69     */
70    private Applications() {}
71
72    /**
73     * Gets a cursor with application search results.
74     * See {@link ApplicationColumns} for the columns available in the returned cursor.
75     */
76    public static Cursor search(ContentResolver resolver, String query) {
77        Uri searchUri = CONTENT_URI.buildUpon().appendPath(SEARCH_PATH).appendPath(query).build();
78        return resolver.query(searchUri, null, null, null, null);
79    }
80
81    /**
82     * Gets the application component name from an application URI.
83     *
84     * @param appUri A URI of the form
85     * "content://applications/applications/<packageName>/<className>".
86     * @return The component name for the application, or
87     * <code>null</code> if the given URI was <code>null</code>
88     * or malformed.
89     */
90    public static ComponentName uriToComponentName(Uri appUri) {
91        if (appUri == null) return null;
92        if (!ContentResolver.SCHEME_CONTENT.equals(appUri.getScheme())) return null;
93        if (!AUTHORITY.equals(appUri.getAuthority())) return null;
94        List<String> pathSegments = appUri.getPathSegments();
95        if (pathSegments.size() != 3) return null;
96        if (!APPLICATION_PATH.equals(pathSegments.get(0))) return null;
97        String packageName = pathSegments.get(1);
98        String name = pathSegments.get(2);
99        return new ComponentName(packageName, name);
100    }
101
102    /**
103     * Gets the URI for an application component.
104     *
105     * @param packageName The name of the application's package.
106     * @param className The class name of the application.
107     * @return A URI of the form
108     * "content://applications/applications/&lt;packageName&gt;/&lt;className&gt;".
109     */
110    public static Uri componentNameToUri(String packageName, String className) {
111        return Applications.CONTENT_URI.buildUpon()
112                .appendEncodedPath(APPLICATION_PATH)
113                .appendPath(packageName)
114                .appendPath(className)
115                .build();
116    }
117
118    /**
119     * The columns in application cursors, like those returned by
120     * {@link Applications#search(ContentResolver, String)}.
121     */
122    public interface ApplicationColumns extends BaseColumns {
123        public static final String NAME = "name";
124        public static final String ICON = "icon";
125        public static final String URI = "uri";
126    }
127}
128