blob: 12bbee78001e35406f3115b43f3d7b95472ce32c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.ContentValues;
Winson Chungbe365162012-05-07 10:34:12 -070020import android.content.Intent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.graphics.Bitmap;
22import android.util.Log;
23
Winson Chungbe365162012-05-07 10:34:12 -070024import java.io.ByteArrayOutputStream;
25import java.io.IOException;
Sameer Padalabe3e4102014-04-21 19:36:14 -070026import java.util.Arrays;
Winson Chungbe365162012-05-07 10:34:12 -070027
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028/**
29 * Represents an item in the launcher.
30 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000031public class ItemInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032
33 static final int NO_ID = -1;
34
35 /**
36 * The id in the settings database for this item
37 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070038 public long id = NO_ID;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039
40 /**
41 * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
42 * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
Adam Cohendf2cc412011-04-27 16:56:57 -070043 * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or
The Android Open Source Project7376fae2009-03-11 12:11:58 -070044 * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070046 public int itemType;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
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 Koppale3e646e2014-03-17 09:34:39 -070054 public long container = NO_ID;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
56 /**
57 * Iindicates the screen in which the shortcut appears.
58 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070059 public long screenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
61 /**
62 * Indicates the X position of the associated cell.
63 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070064 public int cellX = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 /**
67 * Indicates the Y position of the associated cell.
68 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070069 public int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
71 /**
72 * Indicates the X cell span.
73 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070074 public int spanX = 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 /**
77 * Indicates the Y cell span.
78 */
79 int spanY = 1;
80
Romain Guy73b979d2009-06-09 12:57:21 -070081 /**
Adam Cohend41fbf52012-02-16 23:53:59 -080082 * Indicates the minimum X cell span.
83 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070084 public int minSpanX = 1;
Adam Cohend41fbf52012-02-16 23:53:59 -080085
86 /**
87 * Indicates the minimum Y cell span.
88 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070089 public int minSpanY = 1;
Adam Cohen487f7dd2012-06-28 18:12:10 -070090
Adam Cohend41fbf52012-02-16 23:53:59 -080091 /**
Adam Cohen487f7dd2012-06-28 18:12:10 -070092 * Indicates that this item needs to be updated in the db
Romain Guy73b979d2009-06-09 12:57:21 -070093 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070094 public boolean requiresDbUpdate = false;
Adam Cohen487f7dd2012-06-28 18:12:10 -070095
96 /**
97 * Title of the item
98 */
99 CharSequence title;
Romain Guy73b979d2009-06-09 12:57:21 -0700100
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800101 /**
102 * The position of the item in a drag-and-drop operation.
103 */
104 int[] dropPos = null;
105
Michael Jurkac9d95c52011-08-29 14:03:34 -0700106 ItemInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
108
Michael Jurkac9d95c52011-08-29 14:03:34 -0700109 ItemInfo(ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 id = info.id;
111 cellX = info.cellX;
112 cellY = info.cellY;
113 spanX = info.spanX;
114 spanY = info.spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -0700115 screenId = info.screenId;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 itemType = info.itemType;
117 container = info.container;
Michael Jurka96610d52012-09-04 08:03:42 -0700118 // tempdebug:
119 LauncherModel.checkItemInfo(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 }
121
Anjali Koppale3e646e2014-03-17 09:34:39 -0700122 public Intent getIntent() {
Winson Chung997a9232013-07-24 15:33:46 -0700123 throw new RuntimeException("Unexpected Intent");
124 }
125
Chris Wrenb6d4c282014-01-27 14:17:08 -0500126 protected Intent getRestoredIntent() {
127 throw new RuntimeException("Unexpected Intent");
128 }
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 /**
131 * Write the fields of this item to the DB
132 *
133 * @param values
134 */
Adrian Roos8f3f6832014-04-28 15:45:52 +0200135 void onAddToDatabase(ContentValues values) {
Romain Guy73b979d2009-06-09 12:57:21 -0700136 values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 values.put(LauncherSettings.Favorites.CONTAINER, container);
Adam Cohendcd297f2013-06-18 13:13:40 -0700138 values.put(LauncherSettings.Favorites.SCREEN, screenId);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 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 Roos8f3f6832014-04-28 15:45:52 +0200143
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 Project31dd5032009-03-03 19:32:27 -0800148 }
149
Winson Chungaafa03c2010-06-11 17:34:16 -0700150 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 Onorato0589f0f2010-02-08 13:44:00 -0800155 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 Project31dd5032009-03-03 19:32:27 -0800171 static void writeBitmap(ContentValues values, Bitmap bitmap) {
172 if (bitmap != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173 byte[] data = flattenBitmap(bitmap);
174 values.put(LauncherSettings.Favorites.ICON, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
176 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700177
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 Onorato9c1289c2009-08-17 11:03:03 -0400184 void unbind() {
185 }
Daniel Sandler8802e962010-05-26 16:28:16 -0400186
187 @Override
188 public String toString() {
Winson Chungc07918d2011-07-01 15:35:26 -0700189 return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container
Adam Cohendcd297f2013-06-18 13:13:40 -0700190 + " screen=" + screenId + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Sameer Padalabe3e4102014-04-21 19:36:14 -0700191 + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos) + ")";
Daniel Sandler8802e962010-05-26 16:28:16 -0400192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193}