blob: dedc0f4f32460ecda368a52624978ea4a787d864 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.ContentValues;
Winson Chungbe365162012-05-07 10:34:12 -070020import android.content.Intent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.graphics.Bitmap;
22import android.util.Log;
23
Winson Chungbe365162012-05-07 10:34:12 -070024import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027/**
28 * Represents an item in the launcher.
29 */
30class ItemInfo {
31
32 static final int NO_ID = -1;
33
34 /**
35 * The id in the settings database for this item
36 */
37 long id = NO_ID;
38
39 /**
40 * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
41 * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
Adam Cohendf2cc412011-04-27 16:56:57 -070042 * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or
The Android Open Source Project7376fae2009-03-11 12:11:58 -070043 * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044 */
45 int itemType;
46
47 /**
48 * The id of the container that holds this item. For the desktop, this will be
49 * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it
50 * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders
51 * it will be the id of the folder.
52 */
53 long container = NO_ID;
54
55 /**
56 * Iindicates the screen in which the shortcut appears.
57 */
58 int screen = -1;
59
60 /**
61 * Indicates the X position of the associated cell.
62 */
63 int cellX = -1;
64
65 /**
66 * Indicates the Y position of the associated cell.
67 */
68 int cellY = -1;
69
70 /**
71 * Indicates the X cell span.
72 */
73 int spanX = 1;
74
75 /**
76 * Indicates the Y cell span.
77 */
78 int spanY = 1;
79
Romain Guy73b979d2009-06-09 12:57:21 -070080 /**
Adam Cohend41fbf52012-02-16 23:53:59 -080081 * Indicates the minimum X cell span.
82 */
83 int minSpanX = 1;
84
85 /**
86 * Indicates the minimum Y cell span.
87 */
88 int minSpanY = 1;
89 /**
Romain Guy73b979d2009-06-09 12:57:21 -070090 * Indicates whether the item is a gesture.
91 */
92 boolean isGesture = false;
93
Patrick Dubroybbaa75c2011-03-08 18:47:40 -080094 /**
95 * The position of the item in a drag-and-drop operation.
96 */
97 int[] dropPos = null;
98
Michael Jurkac9d95c52011-08-29 14:03:34 -070099 ItemInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 }
101
Michael Jurkac9d95c52011-08-29 14:03:34 -0700102 ItemInfo(ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 id = info.id;
104 cellX = info.cellX;
105 cellY = info.cellY;
106 spanX = info.spanX;
107 spanY = info.spanY;
108 screen = info.screen;
109 itemType = info.itemType;
110 container = info.container;
111 }
112
Winson Chungbe365162012-05-07 10:34:12 -0700113 /** Returns the package name that the intent will resolve to, or an empty string if
114 * none exists. */
115 static String getPackageName(Intent intent) {
116 if (intent != null) {
117 String packageName = intent.getPackage();
Winson Chung33a0ad52012-05-08 10:00:31 -0700118 if (packageName == null && intent.getComponent() != null) {
Winson Chungbe365162012-05-07 10:34:12 -0700119 packageName = intent.getComponent().getPackageName();
120 }
121 if (packageName != null) {
122 return packageName;
123 }
124 }
125 return "";
126 }
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 /**
129 * Write the fields of this item to the DB
130 *
131 * @param values
132 */
133 void onAddToDatabase(ContentValues values) {
Romain Guy73b979d2009-06-09 12:57:21 -0700134 values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType);
135 if (!isGesture) {
136 values.put(LauncherSettings.Favorites.CONTAINER, container);
137 values.put(LauncherSettings.Favorites.SCREEN, screen);
138 values.put(LauncherSettings.Favorites.CELLX, cellX);
139 values.put(LauncherSettings.Favorites.CELLY, cellY);
140 values.put(LauncherSettings.Favorites.SPANX, spanX);
141 values.put(LauncherSettings.Favorites.SPANY, spanY);
142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144
Winson Chungaafa03c2010-06-11 17:34:16 -0700145 void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) {
146 values.put(LauncherSettings.Favorites.CELLX, cellX);
147 values.put(LauncherSettings.Favorites.CELLY, cellY);
148 }
149
Joe Onorato0589f0f2010-02-08 13:44:00 -0800150 static byte[] flattenBitmap(Bitmap bitmap) {
151 // Try go guesstimate how much space the icon will take when serialized
152 // to avoid unnecessary allocations/copies during the write.
153 int size = bitmap.getWidth() * bitmap.getHeight() * 4;
154 ByteArrayOutputStream out = new ByteArrayOutputStream(size);
155 try {
156 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
157 out.flush();
158 out.close();
159 return out.toByteArray();
160 } catch (IOException e) {
161 Log.w("Favorite", "Could not write icon");
162 return null;
163 }
164 }
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 static void writeBitmap(ContentValues values, Bitmap bitmap) {
167 if (bitmap != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800168 byte[] data = flattenBitmap(bitmap);
169 values.put(LauncherSettings.Favorites.ICON, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 }
171 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700172
173 /**
174 * It is very important that sub-classes implement this if they contain any references
175 * to the activity (anything in the view hierarchy etc.). If not, leaks can result since
176 * ItemInfo objects persist across rotation and can hence leak by holding stale references
177 * to the old view hierarchy / activity.
178 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 void unbind() {
180 }
Daniel Sandler8802e962010-05-26 16:28:16 -0400181
182 @Override
183 public String toString() {
Winson Chungc07918d2011-07-01 15:35:26 -0700184 return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container
185 + " screen=" + screen + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
186 + " spanY=" + spanY + " isGesture=" + isGesture + " dropPos=" + dropPos + ")";
Daniel Sandler8802e962010-05-26 16:28:16 -0400187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188}