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