• 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.Intent;
22 import android.os.Process;
23 import android.os.UserHandle;
24 
25 import com.android.launcher3.util.ContentWriter;
26 
27 /**
28  * Represents an item in the launcher.
29  */
30 public class ItemInfo {
31 
32     public static final int NO_ID = -1;
33 
34     /**
35      * The id in the settings database for this item
36      */
37     public int id = NO_ID;
38 
39     /**
40      * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION},
41      * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT},
42      * {@link LauncherSettings.Favorites#ITEM_TYPE_DEEP_SHORTCUT}
43      * {@link LauncherSettings.Favorites#ITEM_TYPE_FOLDER},
44      * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET} or
45      * {@link LauncherSettings.Favorites#ITEM_TYPE_CUSTOM_APPWIDGET}.
46      */
47     public int itemType;
48 
49     /**
50      * The id of the container that holds this item. For the desktop, this will be
51      * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it
52      * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders
53      * it will be the id of the folder.
54      */
55     public int container = NO_ID;
56 
57     /**
58      * Indicates the screen in which the shortcut appears if the container types is
59      * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. (i.e., ignore if the container type is
60      * {@link LauncherSettings.Favorites#CONTAINER_HOTSEAT})
61      */
62     public int screenId = -1;
63 
64     /**
65      * Indicates the X position of the associated cell.
66      */
67     public int cellX = -1;
68 
69     /**
70      * Indicates the Y position of the associated cell.
71      */
72     public int cellY = -1;
73 
74     /**
75      * Indicates the X cell span.
76      */
77     public int spanX = 1;
78 
79     /**
80      * Indicates the Y cell span.
81      */
82     public int spanY = 1;
83 
84     /**
85      * Indicates the minimum X cell span.
86      */
87     public int minSpanX = 1;
88 
89     /**
90      * Indicates the minimum Y cell span.
91      */
92     public int minSpanY = 1;
93 
94     /**
95      * Indicates the position in an ordered list.
96      */
97     public int rank = 0;
98 
99     /**
100      * Title of the item
101      */
102     public CharSequence title;
103 
104     /**
105      * Content description of the item.
106      */
107     public CharSequence contentDescription;
108 
109     public UserHandle user;
110 
ItemInfo()111     public ItemInfo() {
112         user = Process.myUserHandle();
113     }
114 
ItemInfo(ItemInfo info)115     ItemInfo(ItemInfo info) {
116         copyFrom(info);
117     }
118 
copyFrom(ItemInfo info)119     public void copyFrom(ItemInfo info) {
120         id = info.id;
121         cellX = info.cellX;
122         cellY = info.cellY;
123         spanX = info.spanX;
124         spanY = info.spanY;
125         rank = info.rank;
126         screenId = info.screenId;
127         itemType = info.itemType;
128         container = info.container;
129         user = info.user;
130         contentDescription = info.contentDescription;
131     }
132 
getIntent()133     public Intent getIntent() {
134         return null;
135     }
136 
getTargetComponent()137     public ComponentName getTargetComponent() {
138         Intent intent = getIntent();
139         if (intent != null) {
140             return intent.getComponent();
141         } else {
142             return null;
143         }
144     }
145 
writeToValues(ContentWriter writer)146     public void writeToValues(ContentWriter writer) {
147         writer.put(LauncherSettings.Favorites.ITEM_TYPE, itemType)
148                 .put(LauncherSettings.Favorites.CONTAINER, container)
149                 .put(LauncherSettings.Favorites.SCREEN, screenId)
150                 .put(LauncherSettings.Favorites.CELLX, cellX)
151                 .put(LauncherSettings.Favorites.CELLY, cellY)
152                 .put(LauncherSettings.Favorites.SPANX, spanX)
153                 .put(LauncherSettings.Favorites.SPANY, spanY)
154                 .put(LauncherSettings.Favorites.RANK, rank);
155     }
156 
readFromValues(ContentValues values)157     public void readFromValues(ContentValues values) {
158         itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
159         container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
160         screenId = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
161         cellX = values.getAsInteger(LauncherSettings.Favorites.CELLX);
162         cellY = values.getAsInteger(LauncherSettings.Favorites.CELLY);
163         spanX = values.getAsInteger(LauncherSettings.Favorites.SPANX);
164         spanY = values.getAsInteger(LauncherSettings.Favorites.SPANY);
165         rank = values.getAsInteger(LauncherSettings.Favorites.RANK);
166     }
167 
168     /**
169      * Write the fields of this item to the DB
170      */
onAddToDatabase(ContentWriter writer)171     public void onAddToDatabase(ContentWriter writer) {
172         if (screenId == Workspace.EXTRA_EMPTY_SCREEN_ID) {
173             // We should never persist an item on the extra empty screen.
174             throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
175         }
176 
177         writeToValues(writer);
178         writer.put(LauncherSettings.Favorites.PROFILE_ID, user);
179     }
180 
181     @Override
toString()182     public final String toString() {
183         return getClass().getSimpleName() + "(" + dumpProperties() + ")";
184     }
185 
dumpProperties()186     protected String dumpProperties() {
187         return "id=" + id
188                 + " type=" + LauncherSettings.Favorites.itemTypeToString(itemType)
189                 + " container=" + LauncherSettings.Favorites.containerToString((int)container)
190                 + " screen=" + screenId
191                 + " cell(" + cellX + "," + cellY + ")"
192                 + " span(" + spanX + "," + spanY + ")"
193                 + " minSpan(" + minSpanX + "," + minSpanY + ")"
194                 + " rank=" + rank
195                 + " user=" + user
196                 + " title=" + title;
197     }
198 
199     /**
200      * Whether this item is disabled.
201      */
isDisabled()202     public boolean isDisabled() {
203         return false;
204     }
205 
getViewId()206     public int getViewId() {
207         // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
208         // This cast is safe as long as the id < 0x00FFFFFF
209         // Since we jail all the dynamically generated views, there should be no clashes
210         // with any other views.
211         return id;
212     }
213 
214 }
215