blob: aa5a18d3c4065f69f29b077de4a7cb52fb8ea5e6 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023
Kenny Guyed131872014-04-30 03:02:21 +010024import com.android.launcher3.compat.UserHandleCompat;
25import com.android.launcher3.compat.UserManagerCompat;
26
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027/**
28 * Represents an item in the launcher.
29 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000030public class ItemInfo {
Kenny Guyed131872014-04-30 03:02:21 +010031
32 /**
33 * Intent extra to store the profile. Format: UserHandle
34 */
35 static final String EXTRA_PROFILE = "profile";
Sunny Goyalaa8ef112015-06-12 20:04:41 -070036
Hyunyoung Song3f471442015-04-08 19:01:34 -070037 public static final int NO_ID = -1;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070038
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 /**
40 * The id in the settings database for this item
41 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070042 public long id = NO_ID;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070043
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044 /**
45 * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
46 * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
Adam Cohendf2cc412011-04-27 16:56:57 -070047 * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or
The Android Open Source Project7376fae2009-03-11 12:11:58 -070048 * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070050 public int itemType;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070051
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 /**
Sunny Goyalaa8ef112015-06-12 20:04:41 -070053 * The id of the container that holds this item. For the desktop, this will be
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it
55 * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders
56 * it will be the id of the folder.
57 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070058 public long container = NO_ID;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070059
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 /**
61 * Iindicates the screen in which the shortcut appears.
62 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070063 public long screenId = -1;
Sunny Goyalaa8ef112015-06-12 20:04:41 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 /**
66 * Indicates the X position of the associated cell.
67 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070068 public int cellX = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70 /**
71 * Indicates the Y position of the associated cell.
72 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070073 public int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75 /**
76 * Indicates the X cell span.
77 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070078 public int spanX = 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80 /**
81 * Indicates the Y cell span.
82 */
Hyunyoung Song3f471442015-04-08 19:01:34 -070083 public int spanY = 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Romain Guy73b979d2009-06-09 12:57:21 -070085 /**
Adam Cohend41fbf52012-02-16 23:53:59 -080086 * Indicates the minimum X cell span.
87 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070088 public int minSpanX = 1;
Adam Cohend41fbf52012-02-16 23:53:59 -080089
90 /**
91 * Indicates the minimum Y cell span.
92 */
Anjali Koppale3e646e2014-03-17 09:34:39 -070093 public int minSpanY = 1;
Adam Cohen487f7dd2012-06-28 18:12:10 -070094
Adam Cohend41fbf52012-02-16 23:53:59 -080095 /**
Sunny Goyal08f72612015-01-05 13:41:43 -080096 * Indicates the position in an ordered list.
97 */
98 public int rank = 0;
99
100 /**
Adam Cohen487f7dd2012-06-28 18:12:10 -0700101 * Title of the item
102 */
Hyunyoung Song3f471442015-04-08 19:01:34 -0700103 public CharSequence title;
Romain Guy73b979d2009-06-09 12:57:21 -0700104
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800105 /**
Kenny Guyc2bd8102014-06-30 12:30:31 +0100106 * Content description of the item.
107 */
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108 public CharSequence contentDescription;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100109
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110 public UserHandleCompat user;
Kenny Guyed131872014-04-30 03:02:21 +0100111
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112 public ItemInfo() {
Kenny Guyc2bd8102014-06-30 12:30:31 +0100113 user = UserHandleCompat.myUserHandle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
115
Michael Jurkac9d95c52011-08-29 14:03:34 -0700116 ItemInfo(ItemInfo info) {
Sunny Goyalff572272014-07-23 13:58:07 -0700117 copyFrom(info);
118 // tempdebug:
119 LauncherModel.checkItemInfo(this);
120 }
121
122 public void copyFrom(ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 id = info.id;
124 cellX = info.cellX;
125 cellY = info.cellY;
126 spanX = info.spanX;
127 spanY = info.spanY;
Sunny Goyal08f72612015-01-05 13:41:43 -0800128 rank = info.rank;
Adam Cohendcd297f2013-06-18 13:13:40 -0700129 screenId = info.screenId;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 itemType = info.itemType;
131 container = info.container;
Kenny Guyed131872014-04-30 03:02:21 +0100132 user = info.user;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100133 contentDescription = info.contentDescription;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
Anjali Koppale3e646e2014-03-17 09:34:39 -0700136 public Intent getIntent() {
Vadim Tryshevd6c8e7e2015-07-08 13:40:14 -0700137 return null;
Winson Chung997a9232013-07-24 15:33:46 -0700138 }
139
Sunny Goyal756cd262015-08-20 12:33:21 -0700140 public void writeToValues(ContentValues values) {
141 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700142 values.put(LauncherSettings.Favorites.CONTAINER, container);
Adam Cohendcd297f2013-06-18 13:13:40 -0700143 values.put(LauncherSettings.Favorites.SCREEN, screenId);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700144 values.put(LauncherSettings.Favorites.CELLX, cellX);
145 values.put(LauncherSettings.Favorites.CELLY, cellY);
146 values.put(LauncherSettings.Favorites.SPANX, spanX);
147 values.put(LauncherSettings.Favorites.SPANY, spanY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800148 values.put(LauncherSettings.Favorites.RANK, rank);
Sunny Goyal756cd262015-08-20 12:33:21 -0700149 }
150
151 public void readFromValues(ContentValues values) {
152 itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
153 container = values.getAsLong(LauncherSettings.Favorites.CONTAINER);
154 screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
Sunny Goyalde51d1d2015-08-31 11:47:03 -0700155 cellX = values.getAsInteger(LauncherSettings.Favorites.CELLX);
156 cellY = values.getAsInteger(LauncherSettings.Favorites.CELLY);
157 spanX = values.getAsInteger(LauncherSettings.Favorites.SPANX);
158 spanY = values.getAsInteger(LauncherSettings.Favorites.SPANY);
159 rank = values.getAsInteger(LauncherSettings.Favorites.RANK);
Sunny Goyal756cd262015-08-20 12:33:21 -0700160 }
161
162 /**
163 * Write the fields of this item to the DB
164 *
165 * @param context A context object to use for getting UserManagerCompat
166 * @param values
167 */
168 void onAddToDatabase(Context context, ContentValues values) {
169 writeToValues(values);
Kenny Guyed131872014-04-30 03:02:21 +0100170 long serialNumber = UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
171 values.put(LauncherSettings.Favorites.PROFILE_ID, serialNumber);
Adrian Roos8f3f6832014-04-28 15:45:52 +0200172
173 if (screenId == Workspace.EXTRA_EMPTY_SCREEN_ID) {
174 // We should never persist an item on the extra empty screen.
175 throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 }
178
179 static void writeBitmap(ContentValues values, Bitmap bitmap) {
180 if (bitmap != null) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700181 byte[] data = Utilities.flattenBitmap(bitmap);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800182 values.put(LauncherSettings.Favorites.ICON, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700185
186 /**
187 * It is very important that sub-classes implement this if they contain any references
188 * to the activity (anything in the view hierarchy etc.). If not, leaks can result since
189 * ItemInfo objects persist across rotation and can hence leak by holding stale references
190 * to the old view hierarchy / activity.
191 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 void unbind() {
193 }
Daniel Sandler8802e962010-05-26 16:28:16 -0400194
195 @Override
196 public String toString() {
Winson Chungc07918d2011-07-01 15:35:26 -0700197 return "Item(id=" + this.id + " type=" + this.itemType + " container=" + this.container
Adam Cohendcd297f2013-06-18 13:13:40 -0700198 + " screen=" + screenId + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700199 + " spanY=" + spanY + " user=" + user + ")";
Daniel Sandler8802e962010-05-26 16:28:16 -0400200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201}