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 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_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 long 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 long 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 111 public ItemInfo() { 112 user = Process.myUserHandle(); 113 } 114 115 ItemInfo(ItemInfo info) { 116 copyFrom(info); 117 // tempdebug: 118 LauncherModel.checkItemInfo(this); 119 } 120 121 public void copyFrom(ItemInfo info) { 122 id = info.id; 123 cellX = info.cellX; 124 cellY = info.cellY; 125 spanX = info.spanX; 126 spanY = info.spanY; 127 rank = info.rank; 128 screenId = info.screenId; 129 itemType = info.itemType; 130 container = info.container; 131 user = info.user; 132 contentDescription = info.contentDescription; 133 } 134 135 public Intent getIntent() { 136 return null; 137 } 138 139 public ComponentName getTargetComponent() { 140 Intent intent = getIntent(); 141 if (intent != null) { 142 return intent.getComponent(); 143 } else { 144 return null; 145 } 146 } 147 148 public void writeToValues(ContentWriter writer) { 149 writer.put(LauncherSettings.Favorites.ITEM_TYPE, itemType) 150 .put(LauncherSettings.Favorites.CONTAINER, container) 151 .put(LauncherSettings.Favorites.SCREEN, screenId) 152 .put(LauncherSettings.Favorites.CELLX, cellX) 153 .put(LauncherSettings.Favorites.CELLY, cellY) 154 .put(LauncherSettings.Favorites.SPANX, spanX) 155 .put(LauncherSettings.Favorites.SPANY, spanY) 156 .put(LauncherSettings.Favorites.RANK, rank); 157 } 158 159 public void readFromValues(ContentValues values) { 160 itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE); 161 container = values.getAsLong(LauncherSettings.Favorites.CONTAINER); 162 screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN); 163 cellX = values.getAsInteger(LauncherSettings.Favorites.CELLX); 164 cellY = values.getAsInteger(LauncherSettings.Favorites.CELLY); 165 spanX = values.getAsInteger(LauncherSettings.Favorites.SPANX); 166 spanY = values.getAsInteger(LauncherSettings.Favorites.SPANY); 167 rank = values.getAsInteger(LauncherSettings.Favorites.RANK); 168 } 169 170 /** 171 * Write the fields of this item to the DB 172 */ 173 public void onAddToDatabase(ContentWriter writer) { 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 } 178 179 writeToValues(writer); 180 writer.put(LauncherSettings.Favorites.PROFILE_ID, user); 181 } 182 183 @Override 184 public final String toString() { 185 return getClass().getSimpleName() + "(" + dumpProperties() + ")"; 186 } 187 188 protected String dumpProperties() { 189 return "id=" + id 190 + " type=" + LauncherSettings.Favorites.itemTypeToString(itemType) 191 + " container=" + LauncherSettings.Favorites.containerToString((int)container) 192 + " screen=" + screenId 193 + " cell(" + cellX + "," + cellY + ")" 194 + " span(" + spanX + "," + spanY + ")" 195 + " minSpan(" + minSpanX + "," + minSpanY + ")" 196 + " rank=" + rank 197 + " user=" + user 198 + " title=" + title; 199 } 200 201 /** 202 * Whether this item is disabled. 203 */ 204 public boolean isDisabled() { 205 return false; 206 } 207 } 208