blob: 0041bb4d6332b0982869f2a9b79b4e0096756772 [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) {
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 Goyal658058b2017-01-21 01:33:02 -080096 public void onAddToDatabase(ContentWriter writer) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -080097 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
Jon Miranda608d0db2017-02-28 13:15:28 -0800117 public void prepareAutoUpdate() {
Jon Miranda44060942017-02-22 10:17:49 -0800118 for (int i = 0; i < listeners.size(); i++) {
Jon Miranda608d0db2017-02-28 13:15:28 -0800119 listeners.get(i).prepareAutoUpdate();
Jon Miranda44060942017-02-22 10:17:49 -0800120 }
121 }
122
Adam Cohenf9c184a2016-01-15 16:47:43 -0800123 public interface FolderListener {
Sunny Goyal90cb3e52016-05-17 21:25:48 +0000124 public void onAdd(ShortcutInfo item);
125 public void onRemove(ShortcutInfo item);
126 public void onTitleChanged(CharSequence title);
127 public void onItemsChanged(boolean animate);
Jon Miranda608d0db2017-02-28 13:15:28 -0800128 public void prepareAutoUpdate();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700129 }
Winson Chung64359a52013-07-08 17:17:08 -0700130
Sunny Goyal5d85c442015-03-10 13:14:47 -0700131 public boolean hasOption(int optionFlag) {
132 return (options & optionFlag) != 0;
133 }
134
135 /**
136 * @param option flag to set or clear
137 * @param isEnabled whether to set or clear the flag
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800138 * @param writer if not null, save changes to the db.
Sunny Goyal5d85c442015-03-10 13:14:47 -0700139 */
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800140 public void setOption(int option, boolean isEnabled, ModelWriter writer) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700141 int oldOptions = options;
142 if (isEnabled) {
143 options |= option;
144 } else {
145 options &= ~option;
146 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800147 if (writer != null && oldOptions != options) {
148 writer.updateItemInDatabase(this);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700149 }
150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151}