ItemInfo.java revision 487f7dd3059621527eb439d7d51d34e00293f9b1
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 android.content.ContentValues; 20import android.content.Intent; 21import android.graphics.Bitmap; 22import android.util.Log; 23 24import java.io.ByteArrayOutputStream; 25import java.io.IOException; 26 27/** 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}, 42 * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or 43 * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}. 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 */ 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 80 /** 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; 89 90 /** 91 * Indicates that this item needs to be updated in the db 92 */ 93 boolean requiresDbUpdate = false; 94 95 /** 96 * Title of the item 97 */ 98 CharSequence title; 99 100 /** 101 * The position of the item in a drag-and-drop operation. 102 */ 103 int[] dropPos = null; 104 105 ItemInfo() { 106 } 107 108 ItemInfo(ItemInfo info) { 109 id = info.id; 110 cellX = info.cellX; 111 cellY = info.cellY; 112 spanX = info.spanX; 113 spanY = info.spanY; 114 screen = info.screen; 115 itemType = info.itemType; 116 container = info.container; 117 } 118 119 /** Returns the package name that the intent will resolve to, or an empty string if 120 * none exists. */ 121 static String getPackageName(Intent intent) { 122 if (intent != null) { 123 String packageName = intent.getPackage(); 124 if (packageName == null && intent.getComponent() != null) { 125 packageName = intent.getComponent().getPackageName(); 126 } 127 if (packageName != null) { 128 return packageName; 129 } 130 } 131 return ""; 132 } 133 134 /** 135 * Write the fields of this item to the DB 136 * 137 * @param values 138 */ 139 void onAddToDatabase(ContentValues values) { 140 values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType); 141 values.put(LauncherSettings.Favorites.CONTAINER, container); 142 values.put(LauncherSettings.Favorites.SCREEN, screen); 143 values.put(LauncherSettings.Favorites.CELLX, cellX); 144 values.put(LauncherSettings.Favorites.CELLY, cellY); 145 values.put(LauncherSettings.Favorites.SPANX, spanX); 146 values.put(LauncherSettings.Favorites.SPANY, spanY); 147 } 148 149 void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) { 150 values.put(LauncherSettings.Favorites.CELLX, cellX); 151 values.put(LauncherSettings.Favorites.CELLY, cellY); 152 } 153 154 static byte[] flattenBitmap(Bitmap bitmap) { 155 // Try go guesstimate how much space the icon will take when serialized 156 // to avoid unnecessary allocations/copies during the write. 157 int size = bitmap.getWidth() * bitmap.getHeight() * 4; 158 ByteArrayOutputStream out = new ByteArrayOutputStream(size); 159 try { 160 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 161 out.flush(); 162 out.close(); 163 return out.toByteArray(); 164 } catch (IOException e) { 165 Log.w("Favorite", "Could not write icon"); 166 return null; 167 } 168 } 169 170 static void writeBitmap(ContentValues values, Bitmap bitmap) { 171 if (bitmap != null) { 172 byte[] data = flattenBitmap(bitmap); 173 values.put(LauncherSettings.Favorites.ICON, data); 174 } 175 } 176 177 /** 178 * It is very important that sub-classes implement this if they contain any references 179 * to the activity (anything in the view hierarchy etc.). If not, leaks can result since 180 * ItemInfo objects persist across rotation and can hence leak by holding stale references 181 * to the old view hierarchy / activity. 182 */ 183 void unbind() { 184 } 185 186 @Override 187 public String toString() { 188 return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container 189 + " screen=" + screen + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX 190 + " spanY=" + spanY + " dropPos=" + dropPos + ")"; 191 } 192} 193