1875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen/*
2875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * Copyright (C) 2009 The Android Open Source Project
3875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen *
4875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * Licensed under the Apache License, Version 2.0 (the "License");
5875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * you may not use this file except in compliance with the License.
6875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * You may obtain a copy of the License at
7875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen *
8875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen *      http://www.apache.org/licenses/LICENSE-2.0
9875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen *
10875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * Unless required by applicable law or agreed to in writing, software
11875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * distributed under the License is distributed on an "AS IS" BASIS,
12875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * See the License for the specific language governing permissions and
14875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen * limitations under the License.
15875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen */
16875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen
17875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaenpackage android.provider;
18875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen
1911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringertimport android.content.ComponentName;
2011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringertimport android.content.ContentResolver;
2111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringertimport android.database.Cursor;
22875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaenimport android.net.Uri;
2311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
2411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringertimport java.util.List;
25875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen
26875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen/**
2711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert * The Applications provider gives information about installed applications.
28875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen *
2911f4ae76f016d72486aedd33cfef47ba41e6592ePeter Visontay * @hide Only used by ApplicationsProvider so far.
30875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen */
31875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaenpublic class Applications {
32875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen
33875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen    /**
34875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen     * The content authority for this provider.
35875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen     */
36875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen    public static final String AUTHORITY = "applications";
37875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen
38875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen    /**
39875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen     * The content:// style URL for this provider
40875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen     */
41875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
42875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen
43875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen    /**
4411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * The content path for application component URIs.
4511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
4611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static final String APPLICATION_PATH = "applications";
4711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
4811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
4911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * The content path for application search.
5011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
5111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static final String SEARCH_PATH = "search";
5211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
5311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    private static final String APPLICATION_SUB_TYPE = "vnd.android.application";
5411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
5511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
5611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * The MIME type for a single application item.
5711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
5811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static final String APPLICATION_ITEM_TYPE =
5911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + APPLICATION_SUB_TYPE;
6011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
6111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
6211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * The MIME type for a list of application items.
6311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
6411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static final String APPLICATION_DIR_TYPE =
6511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + APPLICATION_SUB_TYPE;
6611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
6711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
68875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen     * no public constructor since this is a utility class
69875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen     */
70875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen    private Applications() {}
7111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
7211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
7311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * Gets a cursor with application search results.
7411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * See {@link ApplicationColumns} for the columns available in the returned cursor.
7511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
7611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static Cursor search(ContentResolver resolver, String query) {
7711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        Uri searchUri = CONTENT_URI.buildUpon().appendPath(SEARCH_PATH).appendPath(query).build();
7811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        return resolver.query(searchUri, null, null, null, null);
7911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    }
8011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
8111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
8211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * Gets the application component name from an application URI.
8311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     *
8411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * @param appUri A URI of the form
8511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * "content://applications/applications/<packageName>/<className>".
8611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * @return The component name for the application, or
8711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * <code>null</code> if the given URI was <code>null</code>
8811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * or malformed.
8911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
9011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static ComponentName uriToComponentName(Uri appUri) {
9111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        if (appUri == null) return null;
9211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        if (!ContentResolver.SCHEME_CONTENT.equals(appUri.getScheme())) return null;
9311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        if (!AUTHORITY.equals(appUri.getAuthority())) return null;
9411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        List<String> pathSegments = appUri.getPathSegments();
9511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        if (pathSegments.size() != 3) return null;
9611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        if (!APPLICATION_PATH.equals(pathSegments.get(0))) return null;
9711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        String packageName = pathSegments.get(1);
9811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        String name = pathSegments.get(2);
9911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        return new ComponentName(packageName, name);
10011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    }
10111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
10211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
10311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * Gets the URI for an application component.
10411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     *
10511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * @param packageName The name of the application's package.
10611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * @param className The class name of the application.
10711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * @return A URI of the form
10811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * "content://applications/applications/&lt;packageName&gt;/&lt;className&gt;".
10911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
11011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public static Uri componentNameToUri(String packageName, String className) {
11111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        return Applications.CONTENT_URI.buildUpon()
11211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert                .appendEncodedPath(APPLICATION_PATH)
11311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert                .appendPath(packageName)
11411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert                .appendPath(className)
11511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert                .build();
11611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    }
11711a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert
11811a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    /**
11911a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * The columns in application cursors, like those returned by
12011a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     * {@link Applications#search(ContentResolver, String)}.
12111a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert     */
12211a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    public interface ApplicationColumns extends BaseColumns {
12311a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        public static final String NAME = "name";
12411a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        public static final String ICON = "icon";
12511a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert        public static final String URI = "uri";
12611a7ba367f39175f41300f384ec6e984bcc08d53Bjorn Bringert    }
127875d50a4b9294b2be33cff6493cae7acd1d07ea7Karl Rosaen}
128