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 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 19 | import android.os.Process; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 20 | |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 21 | import com.android.launcher3.model.ModelWriter; |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 22 | import com.android.launcher3.util.ContentWriter; |
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 | |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 48 | public int options; |
| 49 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | /** |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 51 | * The apps and shortcuts |
| 52 | */ |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 53 | public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 54 | |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 55 | ArrayList<FolderListener> listeners = new ArrayList<FolderListener>(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 57 | public FolderInfo() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 58 | itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 59 | user = Process.myUserHandle(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add an app or shortcut |
| 64 | * |
| 65 | * @param item |
| 66 | */ |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 67 | public void add(ShortcutInfo item, boolean animate) { |
Jon Miranda | c476d6e | 2017-05-04 16:30:01 -0700 | [diff] [blame] | 68 | add(item, contents.size(), animate); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Add an app or shortcut for a specified rank. |
| 73 | */ |
| 74 | public void add(ShortcutInfo item, int rank, boolean animate) { |
| 75 | rank = Utilities.boundToRange(rank, 0, contents.size()); |
| 76 | contents.add(rank, item); |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 77 | for (int i = 0; i < listeners.size(); i++) { |
Jon Miranda | c476d6e | 2017-05-04 16:30:01 -0700 | [diff] [blame] | 78 | listeners.get(i).onAdd(item, rank); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 79 | } |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 80 | itemsChanged(animate); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Remove an app or shortcut. Does not change the DB. |
| 85 | * |
| 86 | * @param item |
| 87 | */ |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 88 | public void remove(ShortcutInfo item, boolean animate) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 89 | contents.remove(item); |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 90 | for (int i = 0; i < listeners.size(); i++) { |
| 91 | listeners.get(i).onRemove(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 92 | } |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 93 | itemsChanged(animate); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 96 | public void setTitle(CharSequence title) { |
| 97 | this.title = title; |
| 98 | for (int i = 0; i < listeners.size(); i++) { |
| 99 | listeners.get(i).onTitleChanged(title); |
| 100 | } |
| 101 | } |
| 102 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 103 | @Override |
Sunny Goyal | 658058b | 2017-01-21 01:33:02 -0800 | [diff] [blame] | 104 | public void onAddToDatabase(ContentWriter writer) { |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 105 | super.onAddToDatabase(writer); |
| 106 | writer.put(LauncherSettings.Favorites.TITLE, title) |
| 107 | .put(LauncherSettings.Favorites.OPTIONS, options); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 108 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 109 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 110 | |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 111 | public void addListener(FolderListener listener) { |
| 112 | listeners.add(listener); |
| 113 | } |
| 114 | |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 115 | public void removeListener(FolderListener listener) { |
| 116 | listeners.remove(listener); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 119 | public void itemsChanged(boolean animate) { |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 120 | for (int i = 0; i < listeners.size(); i++) { |
| 121 | listeners.get(i).onItemsChanged(animate); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Jon Miranda | 608d0db | 2017-02-28 13:15:28 -0800 | [diff] [blame] | 125 | public void prepareAutoUpdate() { |
Jon Miranda | 4406094 | 2017-02-22 10:17:49 -0800 | [diff] [blame] | 126 | for (int i = 0; i < listeners.size(); i++) { |
Jon Miranda | 608d0db | 2017-02-28 13:15:28 -0800 | [diff] [blame] | 127 | listeners.get(i).prepareAutoUpdate(); |
Jon Miranda | 4406094 | 2017-02-22 10:17:49 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 131 | public interface FolderListener { |
Jon Miranda | c476d6e | 2017-05-04 16:30:01 -0700 | [diff] [blame] | 132 | public void onAdd(ShortcutInfo item, int rank); |
Sunny Goyal | 90cb3e5 | 2016-05-17 21:25:48 +0000 | [diff] [blame] | 133 | public void onRemove(ShortcutInfo item); |
| 134 | public void onTitleChanged(CharSequence title); |
| 135 | public void onItemsChanged(boolean animate); |
Jon Miranda | 608d0db | 2017-02-28 13:15:28 -0800 | [diff] [blame] | 136 | public void prepareAutoUpdate(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 137 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 138 | |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 139 | public boolean hasOption(int optionFlag) { |
| 140 | return (options & optionFlag) != 0; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param option flag to set or clear |
| 145 | * @param isEnabled whether to set or clear the flag |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 146 | * @param writer if not null, save changes to the db. |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 147 | */ |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 148 | public void setOption(int option, boolean isEnabled, ModelWriter writer) { |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 149 | int oldOptions = options; |
| 150 | if (isEnabled) { |
| 151 | options |= option; |
| 152 | } else { |
| 153 | options &= ~option; |
| 154 | } |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 155 | if (writer != null && oldOptions != options) { |
| 156 | writer.updateItemInDatabase(this); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 159 | } |