The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Winson Chung | 72b520c | 2013-12-10 01:17:03 +0000 | [diff] [blame] | 19 | import android.content.ContentValues; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | |
| 22 | import com.android.launcher3.compat.UserHandleCompat; |
Winson Chung | 72b520c | 2013-12-10 01:17:03 +0000 | [diff] [blame] | 23 | |
Winson Chung | 33231f5 | 2013-12-09 16:57:45 -0800 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Represents a folder containing shortcuts or apps. |
| 28 | */ |
Anjali Koppal | 7b168a1 | 2014-03-04 17:16:11 -0800 | [diff] [blame] | 29 | public class FolderInfo extends ItemInfo { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 30 | |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 31 | public static final int NO_FLAGS = 0x00000000; |
| 32 | |
| 33 | /** |
| 34 | * The folder is locked in sorted mode |
| 35 | */ |
| 36 | public static final int FLAG_ITEMS_SORTED = 0x00000001; |
| 37 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | /** |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 39 | * It is a work folder |
| 40 | */ |
| 41 | public static final int FLAG_WORK_FOLDER = 0x00000002; |
| 42 | |
| 43 | /** |
Sunny Goyal | b7e15ad | 2015-05-07 14:51:31 -0700 | [diff] [blame] | 44 | * The multi-page animation has run for this folder |
| 45 | */ |
| 46 | public static final int FLAG_MULTI_PAGE_ANIMATION = 0x00000004; |
| 47 | |
| 48 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | * Whether this folder has been opened |
| 50 | */ |
| 51 | boolean opened; |
| 52 | |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 53 | public int options; |
| 54 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | /** |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 56 | * The apps and shortcuts |
| 57 | */ |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 58 | public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 59 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 60 | ArrayList<FolderListener> listeners = new ArrayList<FolderListener>(); |
| 61 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 62 | public FolderInfo() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 63 | itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 64 | user = UserHandleCompat.myUserHandle(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Add an app or shortcut |
| 69 | * |
| 70 | * @param item |
| 71 | */ |
| 72 | public void add(ShortcutInfo item) { |
| 73 | contents.add(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 74 | for (int i = 0; i < listeners.size(); i++) { |
| 75 | listeners.get(i).onAdd(item); |
| 76 | } |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 77 | itemsChanged(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Remove an app or shortcut. Does not change the DB. |
| 82 | * |
| 83 | * @param item |
| 84 | */ |
| 85 | public void remove(ShortcutInfo item) { |
| 86 | contents.remove(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 87 | for (int i = 0; i < listeners.size(); i++) { |
| 88 | listeners.get(i).onRemove(item); |
| 89 | } |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 90 | itemsChanged(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 93 | public void setTitle(CharSequence title) { |
Sunny Goyal | a508e4f | 2015-05-21 09:33:57 -0700 | [diff] [blame] | 94 | this.title = title; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 95 | for (int i = 0; i < listeners.size(); i++) { |
| 96 | listeners.get(i).onTitleChanged(title); |
| 97 | } |
| 98 | } |
| 99 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 100 | @Override |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 101 | void onAddToDatabase(Context context, ContentValues values) { |
| 102 | super.onAddToDatabase(context, values); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 103 | values.put(LauncherSettings.Favorites.TITLE, title.toString()); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 104 | values.put(LauncherSettings.Favorites.OPTIONS, options); |
| 105 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 106 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 107 | |
| 108 | void addListener(FolderListener listener) { |
| 109 | listeners.add(listener); |
| 110 | } |
| 111 | |
| 112 | void removeListener(FolderListener listener) { |
| 113 | if (listeners.contains(listener)) { |
| 114 | listeners.remove(listener); |
| 115 | } |
| 116 | } |
| 117 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 118 | void itemsChanged() { |
| 119 | for (int i = 0; i < listeners.size(); i++) { |
| 120 | listeners.get(i).onItemsChanged(); |
| 121 | } |
| 122 | } |
| 123 | |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 124 | @Override |
| 125 | void unbind() { |
| 126 | super.unbind(); |
| 127 | listeners.clear(); |
| 128 | } |
| 129 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 130 | interface FolderListener { |
| 131 | public void onAdd(ShortcutInfo item); |
| 132 | public void onRemove(ShortcutInfo item); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 133 | public void onTitleChanged(CharSequence title); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 134 | public void onItemsChanged(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 135 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public String toString() { |
| 139 | return "FolderInfo(id=" + this.id + " type=" + this.itemType |
| 140 | + " container=" + this.container + " screen=" + screenId |
| 141 | + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 142 | + " spanY=" + spanY + ")"; |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 143 | } |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 144 | |
| 145 | public boolean hasOption(int optionFlag) { |
| 146 | return (options & optionFlag) != 0; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param option flag to set or clear |
| 151 | * @param isEnabled whether to set or clear the flag |
| 152 | * @param context if not null, save changes to the db. |
| 153 | */ |
| 154 | public void setOption(int option, boolean isEnabled, Context context) { |
| 155 | int oldOptions = options; |
| 156 | if (isEnabled) { |
| 157 | options |= option; |
| 158 | } else { |
| 159 | options &= ~option; |
| 160 | } |
| 161 | if (context != null && oldOptions != options) { |
| 162 | LauncherModel.updateItemInDatabase(context, this); |
| 163 | } |
| 164 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | } |