blob: c24423589db2ea539a31af6a9ad612573a79ac7e [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
Kenny Guyed131872014-04-30 03:02:21 +010019import android.content.Context;
20
21import com.android.launcher3.compat.UserHandleCompat;
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;
Kenny Guyed131872014-04-30 03:02:21 +010059 user = UserHandleCompat.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) {
Adam Cohendf2cc412011-04-27 16:56:57 -070068 contents.add(item);
Sunny Goyal90cb3e52016-05-17 21:25:48 +000069 for (int i = 0; i < listeners.size(); i++) {
70 listeners.get(i).onAdd(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070071 }
Sunny Goyalc52ba712016-04-05 15:59:05 -070072 itemsChanged(animate);
Adam Cohendf2cc412011-04-27 16:56:57 -070073 }
74
75 /**
76 * Remove an app or shortcut. Does not change the DB.
77 *
78 * @param item
79 */
Sunny Goyalc52ba712016-04-05 15:59:05 -070080 public void remove(ShortcutInfo item, boolean animate) {
Adam Cohendf2cc412011-04-27 16:56:57 -070081 contents.remove(item);
Sunny Goyal90cb3e52016-05-17 21:25:48 +000082 for (int i = 0; i < listeners.size(); i++) {
83 listeners.get(i).onRemove(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070084 }
Sunny Goyalc52ba712016-04-05 15:59:05 -070085 itemsChanged(animate);
Adam Cohendf2cc412011-04-27 16:56:57 -070086 }
87
Sunny Goyal90cb3e52016-05-17 21:25:48 +000088 public void setTitle(CharSequence title) {
89 this.title = title;
90 for (int i = 0; i < listeners.size(); i++) {
91 listeners.get(i).onTitleChanged(title);
92 }
93 }
94
Adam Cohendf2cc412011-04-27 16:56:57 -070095 @Override
Sunny Goyal32f3dda2016-11-11 11:45:00 -080096 void onAddToDatabase(ContentWriter writer) {
97 super.onAddToDatabase(writer);
98 writer.put(LauncherSettings.Favorites.TITLE, title)
99 .put(LauncherSettings.Favorites.OPTIONS, options);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700100
Adam Cohendf2cc412011-04-27 16:56:57 -0700101 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700102
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000103 public void addListener(FolderListener listener) {
104 listeners.add(listener);
105 }
106
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700107 public void removeListener(FolderListener listener) {
108 listeners.remove(listener);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700109 }
110
Sunny Goyalc52ba712016-04-05 15:59:05 -0700111 public void itemsChanged(boolean animate) {
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000112 for (int i = 0; i < listeners.size(); i++) {
113 listeners.get(i).onItemsChanged(animate);
Adam Cohen76078c42011-06-09 15:06:52 -0700114 }
115 }
116
Adam Cohenf9c184a2016-01-15 16:47:43 -0800117 public interface FolderListener {
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000118 public void onAdd(ShortcutInfo item);
119 public void onRemove(ShortcutInfo item);
120 public void onTitleChanged(CharSequence title);
121 public void onItemsChanged(boolean animate);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700122 }
Winson Chung64359a52013-07-08 17:17:08 -0700123
Sunny Goyal5d85c442015-03-10 13:14:47 -0700124 public boolean hasOption(int optionFlag) {
125 return (options & optionFlag) != 0;
126 }
127
128 /**
129 * @param option flag to set or clear
130 * @param isEnabled whether to set or clear the flag
131 * @param context if not null, save changes to the db.
132 */
133 public void setOption(int option, boolean isEnabled, Context context) {
134 int oldOptions = options;
135 if (isEnabled) {
136 options |= option;
137 } else {
138 options &= ~option;
139 }
140 if (context != null && oldOptions != options) {
141 LauncherModel.updateItemInDatabase(context, this);
142 }
143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144}