The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import java.io.ByteArrayOutputStream; |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | import android.content.ContentValues; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | /** |
| 27 | * Represents an item in the launcher. |
| 28 | */ |
| 29 | class ItemInfo { |
| 30 | |
| 31 | static final int NO_ID = -1; |
| 32 | |
| 33 | /** |
| 34 | * The id in the settings database for this item |
| 35 | */ |
| 36 | long id = NO_ID; |
| 37 | |
| 38 | /** |
| 39 | * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION}, |
| 40 | * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT}, |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 41 | * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 42 | * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | */ |
| 44 | int itemType; |
| 45 | |
| 46 | /** |
| 47 | * The id of the container that holds this item. For the desktop, this will be |
| 48 | * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it |
| 49 | * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders |
| 50 | * it will be the id of the folder. |
| 51 | */ |
| 52 | long container = NO_ID; |
| 53 | |
| 54 | /** |
| 55 | * Iindicates the screen in which the shortcut appears. |
| 56 | */ |
| 57 | int screen = -1; |
| 58 | |
| 59 | /** |
| 60 | * Indicates the X position of the associated cell. |
| 61 | */ |
| 62 | int cellX = -1; |
| 63 | |
| 64 | /** |
| 65 | * Indicates the Y position of the associated cell. |
| 66 | */ |
| 67 | int cellY = -1; |
| 68 | |
| 69 | /** |
| 70 | * Indicates the X cell span. |
| 71 | */ |
| 72 | int spanX = 1; |
| 73 | |
| 74 | /** |
| 75 | * Indicates the Y cell span. |
| 76 | */ |
| 77 | int spanY = 1; |
| 78 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 79 | /** |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 80 | * Indicates the minimum X cell span. |
| 81 | */ |
| 82 | int minSpanX = 1; |
| 83 | |
| 84 | /** |
| 85 | * Indicates the minimum Y cell span. |
| 86 | */ |
| 87 | int minSpanY = 1; |
| 88 | /** |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 89 | * Indicates whether the item is a gesture. |
| 90 | */ |
| 91 | boolean isGesture = false; |
| 92 | |
Patrick Dubroy | bbaa75c | 2011-03-08 18:47:40 -0800 | [diff] [blame] | 93 | /** |
| 94 | * The position of the item in a drag-and-drop operation. |
| 95 | */ |
| 96 | int[] dropPos = null; |
| 97 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 98 | ItemInfo() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 101 | ItemInfo(ItemInfo info) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | id = info.id; |
| 103 | cellX = info.cellX; |
| 104 | cellY = info.cellY; |
| 105 | spanX = info.spanX; |
| 106 | spanY = info.spanY; |
| 107 | screen = info.screen; |
| 108 | itemType = info.itemType; |
| 109 | container = info.container; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Write the fields of this item to the DB |
| 114 | * |
| 115 | * @param values |
| 116 | */ |
| 117 | void onAddToDatabase(ContentValues values) { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 118 | values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType); |
| 119 | if (!isGesture) { |
| 120 | values.put(LauncherSettings.Favorites.CONTAINER, container); |
| 121 | values.put(LauncherSettings.Favorites.SCREEN, screen); |
| 122 | values.put(LauncherSettings.Favorites.CELLX, cellX); |
| 123 | values.put(LauncherSettings.Favorites.CELLY, cellY); |
| 124 | values.put(LauncherSettings.Favorites.SPANX, spanX); |
| 125 | values.put(LauncherSettings.Favorites.SPANY, spanY); |
| 126 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 129 | void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) { |
| 130 | values.put(LauncherSettings.Favorites.CELLX, cellX); |
| 131 | values.put(LauncherSettings.Favorites.CELLY, cellY); |
| 132 | } |
| 133 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 134 | static byte[] flattenBitmap(Bitmap bitmap) { |
| 135 | // Try go guesstimate how much space the icon will take when serialized |
| 136 | // to avoid unnecessary allocations/copies during the write. |
| 137 | int size = bitmap.getWidth() * bitmap.getHeight() * 4; |
| 138 | ByteArrayOutputStream out = new ByteArrayOutputStream(size); |
| 139 | try { |
| 140 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 141 | out.flush(); |
| 142 | out.close(); |
| 143 | return out.toByteArray(); |
| 144 | } catch (IOException e) { |
| 145 | Log.w("Favorite", "Could not write icon"); |
| 146 | return null; |
| 147 | } |
| 148 | } |
| 149 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 150 | static void writeBitmap(ContentValues values, Bitmap bitmap) { |
| 151 | if (bitmap != null) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 152 | byte[] data = flattenBitmap(bitmap); |
| 153 | values.put(LauncherSettings.Favorites.ICON, data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * It is very important that sub-classes implement this if they contain any references |
| 159 | * to the activity (anything in the view hierarchy etc.). If not, leaks can result since |
| 160 | * ItemInfo objects persist across rotation and can hence leak by holding stale references |
| 161 | * to the old view hierarchy / activity. |
| 162 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 163 | void unbind() { |
| 164 | } |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | public String toString() { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 168 | return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container |
| 169 | + " screen=" + screen + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX |
| 170 | + " spanY=" + spanY + " isGesture=" + isGesture + " dropPos=" + dropPos + ")"; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 171 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | } |