ApplicationInfo.java revision fca7c88f9dba895d9f6cf52c6f0e656445063e3e
1/*
2 * Copyright (C) 2008 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.launcher;
18
19import android.content.ComponentName;
20import android.content.ContentValues;
21import android.content.Intent;
22import android.graphics.Bitmap;
23import android.graphics.drawable.Drawable;
24
25/**
26 * Represents a launchable application. An application is made of a name (or title),
27 * an intent and an icon.
28 */
29class ApplicationInfo extends ItemInfo {
30
31    /**
32     * The application name.
33     */
34    CharSequence title;
35
36    /**
37     * The intent used to start the application.
38     */
39    Intent intent;
40
41    /**
42     * The application icon.
43     */
44    Drawable icon;
45
46    /**
47     * When set to true, indicates that the icon has been resized.
48     */
49    boolean filtered;
50
51    /**
52     * Indicates whether the icon comes from an application's resource (if false)
53     * or from a custom Bitmap (if true.)
54     */
55    boolean customIcon;
56
57    /**
58     * If isShortcut=true and customIcon=false, this contains a reference to the
59     * shortcut icon as an application's resource.
60     */
61    Intent.ShortcutIconResource iconResource;
62
63    ApplicationInfo() {
64        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
65    }
66
67    public ApplicationInfo(ApplicationInfo info) {
68        super(info);
69        title = info.title.toString();
70        intent = new Intent(info.intent);
71        if (info.iconResource != null) {
72            iconResource = new Intent.ShortcutIconResource();
73            iconResource.packageName = info.iconResource.packageName;
74            iconResource.resourceName = info.iconResource.resourceName;
75        }
76        icon = info.icon;
77        filtered = info.filtered;
78        customIcon = info.customIcon;
79    }
80
81    /**
82     * Creates the application intent based on a component name and various launch flags.
83     * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
84     *
85     * @param className the class name of the component representing the intent
86     * @param launchFlags the launch flags
87     */
88    final void setActivity(ComponentName className, int launchFlags) {
89        intent = new Intent(Intent.ACTION_MAIN);
90        intent.addCategory(Intent.CATEGORY_LAUNCHER);
91        intent.setComponent(className);
92        intent.setFlags(launchFlags);
93        itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
94    }
95
96    @Override
97    void onAddToDatabase(ContentValues values) {
98        super.onAddToDatabase(values);
99
100        String titleStr = title != null ? title.toString() : null;
101        values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
102
103        String uri = intent != null ? intent.toUri(0) : null;
104        values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
105
106        if (customIcon) {
107            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
108                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
109            Bitmap bitmap = ((FastBitmapDrawable) icon).getBitmap();
110            writeBitmap(values, bitmap);
111        } else {
112            values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
113                    LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
114            if (iconResource != null) {
115                values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
116                        iconResource.packageName);
117                values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
118                        iconResource.resourceName);
119            }
120        }
121    }
122
123    @Override
124    public String toString() {
125        return title.toString();
126    }
127}
128