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 java.io.ByteArrayOutputStream; 20import java.io.IOException; 21 22import android.content.ContentValues; 23import android.graphics.Bitmap; 24import android.util.Log; 25 26/** 27 * Represents an item in the launcher. 28 */ 29class 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}, 41 * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or 42 * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}. 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 79 /** 80 * Indicates whether the item is a gesture. 81 */ 82 boolean isGesture = false; 83 84 /** 85 * The position of the item in a drag-and-drop operation. 86 */ 87 int[] dropPos = null; 88 89 ItemInfo() { 90 } 91 92 ItemInfo(ItemInfo info) { 93 id = info.id; 94 cellX = info.cellX; 95 cellY = info.cellY; 96 spanX = info.spanX; 97 spanY = info.spanY; 98 screen = info.screen; 99 itemType = info.itemType; 100 container = info.container; 101 } 102 103 /** 104 * Write the fields of this item to the DB 105 * 106 * @param values 107 */ 108 void onAddToDatabase(ContentValues values) { 109 values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType); 110 if (!isGesture) { 111 values.put(LauncherSettings.Favorites.CONTAINER, container); 112 values.put(LauncherSettings.Favorites.SCREEN, screen); 113 values.put(LauncherSettings.Favorites.CELLX, cellX); 114 values.put(LauncherSettings.Favorites.CELLY, cellY); 115 values.put(LauncherSettings.Favorites.SPANX, spanX); 116 values.put(LauncherSettings.Favorites.SPANY, spanY); 117 } 118 } 119 120 void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) { 121 values.put(LauncherSettings.Favorites.CELLX, cellX); 122 values.put(LauncherSettings.Favorites.CELLY, cellY); 123 } 124 125 static byte[] flattenBitmap(Bitmap bitmap) { 126 // Try go guesstimate how much space the icon will take when serialized 127 // to avoid unnecessary allocations/copies during the write. 128 int size = bitmap.getWidth() * bitmap.getHeight() * 4; 129 ByteArrayOutputStream out = new ByteArrayOutputStream(size); 130 try { 131 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 132 out.flush(); 133 out.close(); 134 return out.toByteArray(); 135 } catch (IOException e) { 136 Log.w("Favorite", "Could not write icon"); 137 return null; 138 } 139 } 140 141 static void writeBitmap(ContentValues values, Bitmap bitmap) { 142 if (bitmap != null) { 143 byte[] data = flattenBitmap(bitmap); 144 values.put(LauncherSettings.Favorites.ICON, data); 145 } 146 } 147 148 /** 149 * It is very important that sub-classes implement this if they contain any references 150 * to the activity (anything in the view hierarchy etc.). If not, leaks can result since 151 * ItemInfo objects persist across rotation and can hence leak by holding stale references 152 * to the old view hierarchy / activity. 153 */ 154 void unbind() { 155 } 156 157 @Override 158 public String toString() { 159 return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container 160 + " screen=" + screen + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX 161 + " spanY=" + spanY + " isGesture=" + isGesture + " dropPos=" + dropPos + ")"; 162 } 163} 164