blob: aff8323c2a59d56a3d1a6ab3fc6d953c44facbd0 [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;
Kenny Guyed131872014-04-30 03:02:21 +010020import android.content.Context;
Winson Chungbe365162012-05-07 10:34:12 -070021import android.content.Intent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.graphics.Bitmap;
23import android.util.Log;
24
Kenny Guyed131872014-04-30 03:02:21 +010025import com.android.launcher3.compat.UserHandleCompat;
26import com.android.launcher3.compat.UserManagerCompat;
27
Winson Chungbe365162012-05-07 10:34:12 -070028import java.io.ByteArrayOutputStream;
29import java.io.IOException;
Sameer Padalabe3e4102014-04-21 19:36:14 -070030import java.util.Arrays;
Winson Chungbe365162012-05-07 10:34:12 -070031
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
33 * Represents an item in the launcher.
34 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000035public class ItemInfo {
Kenny Guyed131872014-04-30 03:02:21 +010036
37 /**
38 * Intent extra to store the profile. Format: UserHandle
39 */
40 static final String EXTRA_PROFILE = "profile";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041
42 static final int NO_ID = -1;
43
44 /**
45 * The id in the settings database for this item
46 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070047 public long id = NO_ID;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
49 /**
50 * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
51 * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
Adam Cohendf2cc412011-04-27 16:56:57 -070052 * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or
The Android Open Source Project7376fae2009-03-11 12:11:58 -070053 * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070055 public int itemType;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056
57 /**
58 * The id of the container that holds this item. For the desktop, this will be
59 * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it
60 * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders
61 * it will be the id of the folder.
62 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070063 public long container = NO_ID;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
65 /**
66 * Iindicates the screen in which the shortcut appears.
67 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070068 public long screenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70 /**
71 * Indicates the X position of the associated cell.
72 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070073 public int cellX = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75 /**
76 * Indicates the Y position of the associated cell.
77 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070078 public int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80 /**
81 * Indicates the X cell span.
82 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070083 public int spanX = 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
85 /**
86 * Indicates the Y cell span.
87 */
88 int spanY = 1;
89
Romain Guy73b979d2009-06-09 12:57:21 -070090 /**
Adam Cohend41fbf52012-02-16 23:53:59 -080091 * Indicates the minimum X cell span.
92 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070093 public int minSpanX = 1;
Adam Cohend41fbf52012-02-16 23:53:59 -080094
95 /**
96 * Indicates the minimum Y cell span.
97 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070098 public int minSpanY = 1;
Adam Cohen487f7dd2012-06-28 18:12:10 -070099
Adam Cohend41fbf52012-02-16 23:53:59 -0800100 /**
Sunny Goyal08f72612015-01-05 13:41:43 -0800101 * Indicates the position in an ordered list.
102 */
103 public int rank = 0;
104
105 /**
Adam Cohen487f7dd2012-06-28 18:12:10 -0700106 * Indicates that this item needs to be updated in the db
Romain Guy73b979d2009-06-09 12:57:21 -0700107 */
Anjali Koppale3e646e2014-03-17 09:34:39 -0700108 public boolean requiresDbUpdate = false;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700109
110 /**
111 * Title of the item
112 */
113 CharSequence title;
Romain Guy73b979d2009-06-09 12:57:21 -0700114
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800115 /**
Kenny Guyc2bd8102014-06-30 12:30:31 +0100116 * Content description of the item.
117 */
Kenny Guyd6fe5262014-07-21 17:11:41 +0100118 CharSequence contentDescription;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100119
120 /**
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800121 * The position of the item in a drag-and-drop operation.
122 */
123 int[] dropPos = null;
124
Kenny Guyed131872014-04-30 03:02:21 +0100125 UserHandleCompat user;
126
Michael Jurkac9d95c52011-08-29 14:03:34 -0700127 ItemInfo() {
Kenny Guyc2bd8102014-06-30 12:30:31 +0100128 user = UserHandleCompat.myUserHandle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 }
130
Michael Jurkac9d95c52011-08-29 14:03:34 -0700131 ItemInfo(ItemInfo info) {
Sunny Goyalff572272014-07-23 13:58:07 -0700132 copyFrom(info);
133 // tempdebug:
134 LauncherModel.checkItemInfo(this);
135 }
136
137 public void copyFrom(ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 id = info.id;
139 cellX = info.cellX;
140 cellY = info.cellY;
141 spanX = info.spanX;
142 spanY = info.spanY;
Sunny Goyal08f72612015-01-05 13:41:43 -0800143 rank = info.rank;
Adam Cohendcd297f2013-06-18 13:13:40 -0700144 screenId = info.screenId;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 itemType = info.itemType;
146 container = info.container;
Kenny Guyed131872014-04-30 03:02:21 +0100147 user = info.user;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100148 contentDescription = info.contentDescription;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
Anjali Koppale3e646e2014-03-17 09:34:39 -0700151 public Intent getIntent() {
Winson Chung997a9232013-07-24 15:33:46 -0700152 throw new RuntimeException("Unexpected Intent");
153 }
154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 /**
156 * Write the fields of this item to the DB
157 *
Kenny Guyed131872014-04-30 03:02:21 +0100158 * @param context A context object to use for getting UserManagerCompat
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 * @param values
160 */
Kenny Guyed131872014-04-30 03:02:21 +0100161
162 void onAddToDatabase(Context context, ContentValues values) {
Romain Guy73b979d2009-06-09 12:57:21 -0700163 values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 values.put(LauncherSettings.Favorites.CONTAINER, container);
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 values.put(LauncherSettings.Favorites.SCREEN, screenId);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700166 values.put(LauncherSettings.Favorites.CELLX, cellX);
167 values.put(LauncherSettings.Favorites.CELLY, cellY);
168 values.put(LauncherSettings.Favorites.SPANX, spanX);
169 values.put(LauncherSettings.Favorites.SPANY, spanY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800170 values.put(LauncherSettings.Favorites.RANK, rank);
Kenny Guyed131872014-04-30 03:02:21 +0100171 long serialNumber = UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
172 values.put(LauncherSettings.Favorites.PROFILE_ID, serialNumber);
Adrian Roos8f3f6832014-04-28 15:45:52 +0200173
174 if (screenId == Workspace.EXTRA_EMPTY_SCREEN_ID) {
175 // We should never persist an item on the extra empty screen.
176 throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 }
179
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180 static byte[] flattenBitmap(Bitmap bitmap) {
181 // Try go guesstimate how much space the icon will take when serialized
182 // to avoid unnecessary allocations/copies during the write.
183 int size = bitmap.getWidth() * bitmap.getHeight() * 4;
184 ByteArrayOutputStream out = new ByteArrayOutputStream(size);
185 try {
186 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
187 out.flush();
188 out.close();
189 return out.toByteArray();
190 } catch (IOException e) {
191 Log.w("Favorite", "Could not write icon");
192 return null;
193 }
194 }
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 static void writeBitmap(ContentValues values, Bitmap bitmap) {
197 if (bitmap != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 byte[] data = flattenBitmap(bitmap);
199 values.put(LauncherSettings.Favorites.ICON, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 }
201 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700202
203 /**
204 * It is very important that sub-classes implement this if they contain any references
205 * to the activity (anything in the view hierarchy etc.). If not, leaks can result since
206 * ItemInfo objects persist across rotation and can hence leak by holding stale references
207 * to the old view hierarchy / activity.
208 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 void unbind() {
210 }
Daniel Sandler8802e962010-05-26 16:28:16 -0400211
212 @Override
213 public String toString() {
Winson Chungc07918d2011-07-01 15:35:26 -0700214 return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container
Adam Cohendcd297f2013-06-18 13:13:40 -0700215 + " screen=" + screenId + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Kenny Guyed131872014-04-30 03:02:21 +0100216 + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos)
217 + " user=" + user + ")";
Daniel Sandler8802e962010-05-26 16:28:16 -0400218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219}