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