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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 19 | import android.content.ContentValues; |
Winson Chung | be36516 | 2012-05-07 10:34:12 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
| 22 | import android.util.Log; |
| 23 | |
Winson Chung | be36516 | 2012-05-07 10:34:12 -0700 | [diff] [blame] | 24 | import java.io.ByteArrayOutputStream; |
| 25 | import java.io.IOException; |
| 26 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Represents an item in the launcher. |
| 29 | */ |
| 30 | class 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 Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 42 | * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 43 | * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | */ |
| 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 | */ |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 58 | long screenId = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 80 | /** |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 81 | * 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; |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 89 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 90 | /** |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 91 | * Indicates that this item needs to be updated in the db |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 92 | */ |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 93 | boolean requiresDbUpdate = false; |
| 94 | |
| 95 | /** |
| 96 | * Title of the item |
| 97 | */ |
| 98 | CharSequence title; |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 99 | |
Patrick Dubroy | bbaa75c | 2011-03-08 18:47:40 -0800 | [diff] [blame] | 100 | /** |
| 101 | * The position of the item in a drag-and-drop operation. |
| 102 | */ |
| 103 | int[] dropPos = null; |
| 104 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 105 | ItemInfo() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 108 | ItemInfo(ItemInfo info) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 109 | id = info.id; |
| 110 | cellX = info.cellX; |
| 111 | cellY = info.cellY; |
| 112 | spanX = info.spanX; |
| 113 | spanY = info.spanY; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 114 | screenId = info.screenId; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 115 | itemType = info.itemType; |
| 116 | container = info.container; |
Michael Jurka | 96610d5 | 2012-09-04 08:03:42 -0700 | [diff] [blame] | 117 | // tempdebug: |
| 118 | LauncherModel.checkItemInfo(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Write the fields of this item to the DB |
| 123 | * |
| 124 | * @param values |
| 125 | */ |
| 126 | void onAddToDatabase(ContentValues values) { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 127 | values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 128 | values.put(LauncherSettings.Favorites.CONTAINER, container); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 129 | values.put(LauncherSettings.Favorites.SCREEN, screenId); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 130 | values.put(LauncherSettings.Favorites.CELLX, cellX); |
| 131 | values.put(LauncherSettings.Favorites.CELLY, cellY); |
| 132 | values.put(LauncherSettings.Favorites.SPANX, spanX); |
| 133 | values.put(LauncherSettings.Favorites.SPANY, spanY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 136 | void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) { |
| 137 | values.put(LauncherSettings.Favorites.CELLX, cellX); |
| 138 | values.put(LauncherSettings.Favorites.CELLY, cellY); |
| 139 | } |
| 140 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 141 | static byte[] flattenBitmap(Bitmap bitmap) { |
| 142 | // Try go guesstimate how much space the icon will take when serialized |
| 143 | // to avoid unnecessary allocations/copies during the write. |
| 144 | int size = bitmap.getWidth() * bitmap.getHeight() * 4; |
| 145 | ByteArrayOutputStream out = new ByteArrayOutputStream(size); |
| 146 | try { |
| 147 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 148 | out.flush(); |
| 149 | out.close(); |
| 150 | return out.toByteArray(); |
| 151 | } catch (IOException e) { |
| 152 | Log.w("Favorite", "Could not write icon"); |
| 153 | return null; |
| 154 | } |
| 155 | } |
| 156 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 157 | static void writeBitmap(ContentValues values, Bitmap bitmap) { |
| 158 | if (bitmap != null) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 159 | byte[] data = flattenBitmap(bitmap); |
| 160 | values.put(LauncherSettings.Favorites.ICON, data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * It is very important that sub-classes implement this if they contain any references |
| 166 | * to the activity (anything in the view hierarchy etc.). If not, leaks can result since |
| 167 | * ItemInfo objects persist across rotation and can hence leak by holding stale references |
| 168 | * to the old view hierarchy / activity. |
| 169 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 170 | void unbind() { |
| 171 | } |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public String toString() { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 175 | return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 176 | + " screen=" + screenId + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 177 | + " spanY=" + spanY + " dropPos=" + dropPos + ")"; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 178 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 179 | } |