ItemInfo.java revision 374753cabf05cde1ad669d07bde47e34fdcbe499
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.launcher2;
18
19import android.content.ContentValues;
20import android.content.Context;
21import android.content.Intent;
22import android.graphics.Bitmap;
23import android.os.UserHandle;
24import android.os.UserManager;
25import android.util.Log;
26
27import java.io.ByteArrayOutputStream;
28import java.io.IOException;
29
30/**
31 * Represents an item in the launcher.
32 */
33class ItemInfo {
34
35    /**
36     * Intent extra to store the profile. Format: UserHandle
37     */
38    static final String EXTRA_PROFILE = "profile";
39
40    static final int NO_ID = -1;
41
42    /**
43     * The id in the settings database for this item
44     */
45    long id = NO_ID;
46
47    /**
48     * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
49     * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
50     * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or
51     * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}.
52     */
53    int itemType;
54
55    /**
56     * The id of the container that holds this item. For the desktop, this will be
57     * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it
58     * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders
59     * it will be the id of the folder.
60     */
61    long container = NO_ID;
62
63    /**
64     * Indicates the screen in which the shortcut appears.
65     */
66    int screen = -1;
67
68    /**
69     * Indicates the X position of the associated cell.
70     */
71    int cellX = -1;
72
73    /**
74     * Indicates the Y position of the associated cell.
75     */
76    int cellY = -1;
77
78    /**
79     * Indicates the X cell span.
80     */
81    int spanX = 1;
82
83    /**
84     * Indicates the Y cell span.
85     */
86    int spanY = 1;
87
88    /**
89     * Indicates the minimum X cell span.
90     */
91    int minSpanX = 1;
92
93    /**
94     * Indicates the minimum Y cell span.
95     */
96    int minSpanY = 1;
97
98    /**
99     * Indicates that this item needs to be updated in the db
100     */
101    boolean requiresDbUpdate = false;
102
103    /**
104     * Title of the item
105     */
106    CharSequence title;
107
108    /**
109     * The position of the item in a drag-and-drop operation.
110     */
111    int[] dropPos = null;
112
113    UserHandle user;
114
115    ItemInfo() {
116        user = android.os.Process.myUserHandle();
117    }
118
119    ItemInfo(ItemInfo info) {
120        id = info.id;
121        cellX = info.cellX;
122        cellY = info.cellY;
123        spanX = info.spanX;
124        spanY = info.spanY;
125        screen = info.screen;
126        itemType = info.itemType;
127        container = info.container;
128        user = info.user;
129        // tempdebug:
130        LauncherModel.checkItemInfo(this);
131    }
132
133    /** Returns the package name that the intent will resolve to, or an empty string if
134     *  none exists. */
135    static String getPackageName(Intent intent) {
136        if (intent != null) {
137            String packageName = intent.getPackage();
138            if (packageName == null && intent.getComponent() != null) {
139                packageName = intent.getComponent().getPackageName();
140            }
141            if (packageName != null) {
142                return packageName;
143            }
144        }
145        return "";
146    }
147
148    protected void updateUser(Intent intent) {
149        if (intent != null && intent.hasExtra(EXTRA_PROFILE)) {
150            user = (UserHandle) intent.getParcelableExtra(EXTRA_PROFILE);
151        }
152    }
153
154    /**
155     * Write the fields of this item to the DB
156     *
157     * @param context A context object to use for getting a UserManager
158     *            instance.
159     * @param values
160     */
161    void onAddToDatabase(Context context, ContentValues values) {
162        values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType);
163        values.put(LauncherSettings.Favorites.CONTAINER, container);
164        values.put(LauncherSettings.Favorites.SCREEN, screen);
165        values.put(LauncherSettings.Favorites.CELLX, cellX);
166        values.put(LauncherSettings.Favorites.CELLY, cellY);
167        values.put(LauncherSettings.Favorites.SPANX, spanX);
168        values.put(LauncherSettings.Favorites.SPANY, spanY);
169        long serialNumber = ((UserManager) context.getSystemService(Context.USER_SERVICE))
170                .getSerialNumberForUser(user);
171        values.put(LauncherSettings.Favorites.PROFILE_ID, serialNumber);
172    }
173
174    void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) {
175        values.put(LauncherSettings.Favorites.CELLX, cellX);
176        values.put(LauncherSettings.Favorites.CELLY, cellY);
177    }
178
179    static byte[] flattenBitmap(Bitmap bitmap) {
180        // Try go guesstimate how much space the icon will take when serialized
181        // to avoid unnecessary allocations/copies during the write.
182        int size = bitmap.getWidth() * bitmap.getHeight() * 4;
183        ByteArrayOutputStream out = new ByteArrayOutputStream(size);
184        try {
185            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
186            out.flush();
187            out.close();
188            return out.toByteArray();
189        } catch (IOException e) {
190            Log.w("Favorite", "Could not write icon");
191            return null;
192        }
193    }
194
195    static void writeBitmap(ContentValues values, Bitmap bitmap) {
196        if (bitmap != null) {
197            byte[] data = flattenBitmap(bitmap);
198            values.put(LauncherSettings.Favorites.ICON, data);
199        }
200    }
201
202    /**
203     * It is very important that sub-classes implement this if they contain any references
204     * to the activity (anything in the view hierarchy etc.). If not, leaks can result since
205     * ItemInfo objects persist across rotation and can hence leak by holding stale references
206     * to the old view hierarchy / activity.
207     */
208    void unbind() {
209    }
210
211    @Override
212    public String toString() {
213        return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container
214            + " screen=" + screen + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
215                + " spanY=" + spanY + " dropPos=" + dropPos + " user=" + user
216                + ")";
217    }
218}
219