• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.launcher3;
18 
19 import android.content.ComponentName;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.graphics.Bitmap;
24 
25 import com.android.launcher3.compat.UserHandleCompat;
26 import com.android.launcher3.compat.UserManagerCompat;
27 
28 /**
29  * Represents an item in the launcher.
30  */
31 public class ItemInfo {
32 
33     /**
34      * Intent extra to store the profile. Format: UserHandle
35      */
36     public static final String EXTRA_PROFILE = "profile";
37 
38     public static final int NO_ID = -1;
39 
40     /**
41      * The id in the settings database for this item
42      */
43     public long id = NO_ID;
44 
45     /**
46      * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
47      * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
48      * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER}, or
49      * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}.
50      */
51     public int itemType;
52 
53     /**
54      * The id of the container that holds this item. For the desktop, this will be
55      * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it
56      * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders
57      * it will be the id of the folder.
58      */
59     public long container = NO_ID;
60 
61     /**
62      * Iindicates the screen in which the shortcut appears.
63      */
64     public long screenId = -1;
65 
66     /**
67      * Indicates the X position of the associated cell.
68      */
69     public int cellX = -1;
70 
71     /**
72      * Indicates the Y position of the associated cell.
73      */
74     public int cellY = -1;
75 
76     /**
77      * Indicates the X cell span.
78      */
79     public int spanX = 1;
80 
81     /**
82      * Indicates the Y cell span.
83      */
84     public int spanY = 1;
85 
86     /**
87      * Indicates the minimum X cell span.
88      */
89     public int minSpanX = 1;
90 
91     /**
92      * Indicates the minimum Y cell span.
93      */
94     public int minSpanY = 1;
95 
96     /**
97      * Indicates the position in an ordered list.
98      */
99     public int rank = 0;
100 
101     /**
102      * Title of the item
103      */
104     public CharSequence title;
105 
106     /**
107      * Content description of the item.
108      */
109     public CharSequence contentDescription;
110 
111     public UserHandleCompat user;
112 
ItemInfo()113     public ItemInfo() {
114         user = UserHandleCompat.myUserHandle();
115     }
116 
ItemInfo(ItemInfo info)117     ItemInfo(ItemInfo info) {
118         copyFrom(info);
119         // tempdebug:
120         LauncherModel.checkItemInfo(this);
121     }
122 
copyFrom(ItemInfo info)123     public void copyFrom(ItemInfo info) {
124         id = info.id;
125         cellX = info.cellX;
126         cellY = info.cellY;
127         spanX = info.spanX;
128         spanY = info.spanY;
129         rank = info.rank;
130         screenId = info.screenId;
131         itemType = info.itemType;
132         container = info.container;
133         user = info.user;
134         contentDescription = info.contentDescription;
135     }
136 
getIntent()137     public Intent getIntent() {
138         return null;
139     }
140 
getTargetComponent()141     public ComponentName getTargetComponent() {
142         return getIntent() == null ? null : getIntent().getComponent();
143     }
144 
writeToValues(ContentValues values)145     public void writeToValues(ContentValues values) {
146         values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
147         values.put(LauncherSettings.Favorites.CONTAINER, container);
148         values.put(LauncherSettings.Favorites.SCREEN, screenId);
149         values.put(LauncherSettings.Favorites.CELLX, cellX);
150         values.put(LauncherSettings.Favorites.CELLY, cellY);
151         values.put(LauncherSettings.Favorites.SPANX, spanX);
152         values.put(LauncherSettings.Favorites.SPANY, spanY);
153         values.put(LauncherSettings.Favorites.RANK, rank);
154     }
155 
readFromValues(ContentValues values)156     public void readFromValues(ContentValues values) {
157         itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
158         container = values.getAsLong(LauncherSettings.Favorites.CONTAINER);
159         screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
160         cellX = values.getAsInteger(LauncherSettings.Favorites.CELLX);
161         cellY = values.getAsInteger(LauncherSettings.Favorites.CELLY);
162         spanX = values.getAsInteger(LauncherSettings.Favorites.SPANX);
163         spanY = values.getAsInteger(LauncherSettings.Favorites.SPANY);
164         rank = values.getAsInteger(LauncherSettings.Favorites.RANK);
165     }
166 
167     /**
168      * Write the fields of this item to the DB
169      *
170      * @param context A context object to use for getting UserManagerCompat
171      * @param values
172      */
onAddToDatabase(Context context, ContentValues values)173     void onAddToDatabase(Context context, ContentValues values) {
174         writeToValues(values);
175         long serialNumber = UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
176         values.put(LauncherSettings.Favorites.PROFILE_ID, serialNumber);
177 
178         if (screenId == Workspace.EXTRA_EMPTY_SCREEN_ID) {
179             // We should never persist an item on the extra empty screen.
180             throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
181         }
182     }
183 
writeBitmap(ContentValues values, Bitmap bitmap)184     static void writeBitmap(ContentValues values, Bitmap bitmap) {
185         if (bitmap != null) {
186             byte[] data = Utilities.flattenBitmap(bitmap);
187             values.put(LauncherSettings.Favorites.ICON, data);
188         }
189     }
190 
191     @Override
toString()192     public final String toString() {
193         return getClass().getSimpleName() + "(" + dumpProperties() + ")";
194     }
195 
dumpProperties()196     protected String dumpProperties() {
197         return "id=" + id
198                 + " type=" + itemType
199                 + " container=" + container
200                 + " screen=" + screenId
201                 + " cellX=" + cellX
202                 + " cellY=" + cellY
203                 + " spanX=" + spanX
204                 + " spanY=" + spanY
205                 + " minSpanX=" + minSpanX
206                 + " minSpanY=" + minSpanY
207                 + " rank=" + rank
208                 + " user=" + user
209                 + " title=" + title;
210     }
211 
212     /**
213      * Whether this item is disabled.
214      */
isDisabled()215     public boolean isDisabled() {
216         return false;
217     }
218 }
219