The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [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 | |
| 17 | package com.android.launcher; |
| 18 | |
| 19 | import java.io.ByteArrayOutputStream; |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | import android.content.ContentValues; |
| 23 | import android.graphics.Bitmap; |
| 24 | import com.android.internal.provider.Settings; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Represents an item in the launcher. |
| 30 | */ |
| 31 | class ItemInfo { |
| 32 | |
| 33 | static final int NO_ID = -1; |
| 34 | |
| 35 | /** |
| 36 | * The id in the settings database for this item |
| 37 | */ |
| 38 | long id = NO_ID; |
| 39 | |
| 40 | /** |
| 41 | * One of {@link Settings.Favorites#ITEM_TYPE_APPLICATION}, |
| 42 | * {@link Settings.Favorites#ITEM_TYPE_SHORTCUT}, |
| 43 | * {@link Settings.Favorites#ITEM_TYPE_USER_FOLDER}, |
| 44 | * {@link Settings.Favorites#ITEM_TYPE_WIDGET_CLOCK}, |
| 45 | * {@link Settings.Favorites#ITEM_TYPE_WIDGET_SEARCH} or |
| 46 | * {@link Settings.Favorites#ITEM_TYPE_WIDGET_PHOTO_FRAME}, |
| 47 | */ |
| 48 | int itemType; |
| 49 | |
| 50 | /** |
| 51 | * The id of the container that holds this item. For the desktop, this will be |
| 52 | * {@link Settings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it |
| 53 | * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders |
| 54 | * it will be the id of the folder. |
| 55 | */ |
| 56 | long container = NO_ID; |
| 57 | |
| 58 | /** |
| 59 | * Iindicates the screen in which the shortcut appears. |
| 60 | */ |
| 61 | int screen = -1; |
| 62 | |
| 63 | /** |
| 64 | * Indicates the X position of the associated cell. |
| 65 | */ |
| 66 | int cellX = -1; |
| 67 | |
| 68 | /** |
| 69 | * Indicates the Y position of the associated cell. |
| 70 | */ |
| 71 | int cellY = -1; |
| 72 | |
| 73 | /** |
| 74 | * Indicates the X cell span. |
| 75 | */ |
| 76 | int spanX = 1; |
| 77 | |
| 78 | /** |
| 79 | * Indicates the Y cell span. |
| 80 | */ |
| 81 | int spanY = 1; |
| 82 | |
| 83 | ItemInfo() { |
| 84 | } |
| 85 | |
| 86 | ItemInfo(ItemInfo info) { |
| 87 | id = info.id; |
| 88 | cellX = info.cellX; |
| 89 | cellY = info.cellY; |
| 90 | spanX = info.spanX; |
| 91 | spanY = info.spanY; |
| 92 | screen = info.screen; |
| 93 | itemType = info.itemType; |
| 94 | container = info.container; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Write the fields of this item to the DB |
| 99 | * |
| 100 | * @param values |
| 101 | */ |
| 102 | void onAddToDatabase(ContentValues values) { |
| 103 | values.put(Settings.Favorites.ITEM_TYPE, itemType); |
| 104 | values.put(Settings.Favorites.CONTAINER, container); |
| 105 | values.put(Settings.Favorites.SCREEN, screen); |
| 106 | values.put(Settings.Favorites.CELLX, cellX); |
| 107 | values.put(Settings.Favorites.CELLY, cellY); |
| 108 | values.put(Settings.Favorites.SPANX, spanX); |
| 109 | values.put(Settings.Favorites.SPANY, spanY); |
| 110 | } |
| 111 | |
| 112 | static void writeBitmap(ContentValues values, Bitmap bitmap) { |
| 113 | if (bitmap != null) { |
| 114 | // Try go guesstimate how much space the icon will take when serialized |
| 115 | // to avoid unnecessary allocations/copies during the write. |
| 116 | int size = bitmap.getWidth() * bitmap.getHeight() * 4; |
| 117 | ByteArrayOutputStream out = new ByteArrayOutputStream(size); |
| 118 | try { |
| 119 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 120 | out.flush(); |
| 121 | out.close(); |
| 122 | |
| 123 | values.put(Settings.Favorites.ICON, out.toByteArray()); |
| 124 | } catch (IOException e) { |
| 125 | Log.w("Favorite", "Could not write icon"); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |