blob: 21254ab2925e85eebd9a24eefbcf2671b707e240 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080019import android.os.Process;
Kenny Guyed131872014-04-30 03:02:21 +010020
Sunny Goyal43bf11d2017-02-02 13:52:53 -080021import com.android.launcher3.model.ModelWriter;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080022import com.android.launcher3.util.ContentWriter;
Winson Chung72b520c2013-12-10 01:17:03 +000023
Winson Chung33231f52013-12-09 16:57:45 -080024import java.util.ArrayList;
25
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026/**
27 * Represents a folder containing shortcuts or apps.
28 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080029public class FolderInfo extends ItemInfo {
Adam Cohendf2cc412011-04-27 16:56:57 -070030
Sunny Goyal5d85c442015-03-10 13:14:47 -070031 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 Project31dd5032009-03-03 19:32:27 -080038 /**
Sunny Goyal18bf8e22015-04-08 18:13:46 -070039 * It is a work folder
40 */
41 public static final int FLAG_WORK_FOLDER = 0x00000002;
42
43 /**
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070044 * The multi-page animation has run for this folder
45 */
46 public static final int FLAG_MULTI_PAGE_ANIMATION = 0x00000004;
47
Sunny Goyal5d85c442015-03-10 13:14:47 -070048 public int options;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 /**
Adam Cohendf2cc412011-04-27 16:56:57 -070051 * The apps and shortcuts
52 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -070053 public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
Adam Cohendf2cc412011-04-27 16:56:57 -070054
Sunny Goyal90cb3e52016-05-17 21:25:48 +000055 ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
Adam Cohena9cf38f2011-05-02 15:36:58 -070056
Sunny Goyal18bf8e22015-04-08 18:13:46 -070057 public FolderInfo() {
Adam Cohendf2cc412011-04-27 16:56:57 -070058 itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080059 user = Process.myUserHandle();
Adam Cohendf2cc412011-04-27 16:56:57 -070060 }
61
62 /**
63 * Add an app or shortcut
64 *
65 * @param item
66 */
Sunny Goyalc52ba712016-04-05 15:59:05 -070067 public void add(ShortcutInfo item, boolean animate) {
Jon Mirandac476d6e2017-05-04 16:30:01 -070068 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 Goyal90cb3e52016-05-17 21:25:48 +000077 for (int i = 0; i < listeners.size(); i++) {
Jon Mirandac476d6e2017-05-04 16:30:01 -070078 listeners.get(i).onAdd(item, rank);
Adam Cohena9cf38f2011-05-02 15:36:58 -070079 }
Sunny Goyalc52ba712016-04-05 15:59:05 -070080 itemsChanged(animate);
Adam Cohendf2cc412011-04-27 16:56:57 -070081 }
82
83 /**
84 * Remove an app or shortcut. Does not change the DB.
85 *
86 * @param item
87 */
Sunny Goyalc52ba712016-04-05 15:59:05 -070088 public void remove(ShortcutInfo item, boolean animate) {
Adam Cohendf2cc412011-04-27 16:56:57 -070089 contents.remove(item);
Sunny Goyal90cb3e52016-05-17 21:25:48 +000090 for (int i = 0; i < listeners.size(); i++) {
91 listeners.get(i).onRemove(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070092 }
Sunny Goyalc52ba712016-04-05 15:59:05 -070093 itemsChanged(animate);
Adam Cohendf2cc412011-04-27 16:56:57 -070094 }
95
Sunny Goyal90cb3e52016-05-17 21:25:48 +000096 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 Cohendf2cc412011-04-27 16:56:57 -0700103 @Override
Sunny Goyal658058b2017-01-21 01:33:02 -0800104 public void onAddToDatabase(ContentWriter writer) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800105 super.onAddToDatabase(writer);
106 writer.put(LauncherSettings.Favorites.TITLE, title)
107 .put(LauncherSettings.Favorites.OPTIONS, options);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700108
Adam Cohendf2cc412011-04-27 16:56:57 -0700109 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700110
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000111 public void addListener(FolderListener listener) {
112 listeners.add(listener);
113 }
114
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700115 public void removeListener(FolderListener listener) {
116 listeners.remove(listener);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700117 }
118
Sunny Goyalc52ba712016-04-05 15:59:05 -0700119 public void itemsChanged(boolean animate) {
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000120 for (int i = 0; i < listeners.size(); i++) {
121 listeners.get(i).onItemsChanged(animate);
Adam Cohen76078c42011-06-09 15:06:52 -0700122 }
123 }
124
Jon Miranda608d0db2017-02-28 13:15:28 -0800125 public void prepareAutoUpdate() {
Jon Miranda44060942017-02-22 10:17:49 -0800126 for (int i = 0; i < listeners.size(); i++) {
Jon Miranda608d0db2017-02-28 13:15:28 -0800127 listeners.get(i).prepareAutoUpdate();
Jon Miranda44060942017-02-22 10:17:49 -0800128 }
129 }
130
Adam Cohenf9c184a2016-01-15 16:47:43 -0800131 public interface FolderListener {
Jon Mirandac476d6e2017-05-04 16:30:01 -0700132 public void onAdd(ShortcutInfo item, int rank);
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000133 public void onRemove(ShortcutInfo item);
134 public void onTitleChanged(CharSequence title);
135 public void onItemsChanged(boolean animate);
Jon Miranda608d0db2017-02-28 13:15:28 -0800136 public void prepareAutoUpdate();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700137 }
Winson Chung64359a52013-07-08 17:17:08 -0700138
Sunny Goyal5d85c442015-03-10 13:14:47 -0700139 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 Goyal43bf11d2017-02-02 13:52:53 -0800146 * @param writer if not null, save changes to the db.
Sunny Goyal5d85c442015-03-10 13:14:47 -0700147 */
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800148 public void setOption(int option, boolean isEnabled, ModelWriter writer) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700149 int oldOptions = options;
150 if (isEnabled) {
151 options |= option;
152 } else {
153 options &= ~option;
154 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800155 if (writer != null && oldOptions != options) {
156 writer.updateItemInDatabase(this);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700157 }
158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159}