blob: aea21c95b51a30d5649013512a6ded844af0420e [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
Winson Chung72b520c2013-12-10 01:17:03 +000019import android.content.ContentValues;
Kenny Guyed131872014-04-30 03:02:21 +010020import android.content.Context;
21
22import com.android.launcher3.compat.UserHandleCompat;
Winson Chung72b520c2013-12-10 01:17:03 +000023
Winson Chung33231f52013-12-09 16:57:45 -080024import java.util.ArrayList;
Sameer Padalabe3e4102014-04-21 19:36:14 -070025import java.util.Arrays;
Winson Chung33231f52013-12-09 16:57:45 -080026
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027/**
28 * Represents a folder containing shortcuts or apps.
29 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080030public class FolderInfo extends ItemInfo {
Adam Cohendf2cc412011-04-27 16:56:57 -070031
Sunny Goyal5d85c442015-03-10 13:14:47 -070032 public static final int NO_FLAGS = 0x00000000;
33
34 /**
35 * The folder is locked in sorted mode
36 */
37 public static final int FLAG_ITEMS_SORTED = 0x00000001;
38
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 /**
Sunny Goyal18bf8e22015-04-08 18:13:46 -070040 * It is a work folder
41 */
42 public static final int FLAG_WORK_FOLDER = 0x00000002;
43
44 /**
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070045 * The multi-page animation has run for this folder
46 */
47 public static final int FLAG_MULTI_PAGE_ANIMATION = 0x00000004;
48
49 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 * Whether this folder has been opened
51 */
52 boolean opened;
53
Sunny Goyal5d85c442015-03-10 13:14:47 -070054 public int options;
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 /**
Adam Cohendf2cc412011-04-27 16:56:57 -070057 * The apps and shortcuts
58 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -070059 public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
Adam Cohendf2cc412011-04-27 16:56:57 -070060
Adam Cohena9cf38f2011-05-02 15:36:58 -070061 ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
62
Sunny Goyal18bf8e22015-04-08 18:13:46 -070063 public FolderInfo() {
Adam Cohendf2cc412011-04-27 16:56:57 -070064 itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
Kenny Guyed131872014-04-30 03:02:21 +010065 user = UserHandleCompat.myUserHandle();
Adam Cohendf2cc412011-04-27 16:56:57 -070066 }
67
68 /**
69 * Add an app or shortcut
70 *
71 * @param item
72 */
73 public void add(ShortcutInfo item) {
74 contents.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070075 for (int i = 0; i < listeners.size(); i++) {
76 listeners.get(i).onAdd(item);
77 }
Adam Cohenac56cff2011-09-28 20:45:37 -070078 itemsChanged();
Adam Cohendf2cc412011-04-27 16:56:57 -070079 }
80
81 /**
82 * Remove an app or shortcut. Does not change the DB.
83 *
84 * @param item
85 */
86 public void remove(ShortcutInfo item) {
87 contents.remove(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070088 for (int i = 0; i < listeners.size(); i++) {
89 listeners.get(i).onRemove(item);
90 }
Adam Cohenac56cff2011-09-28 20:45:37 -070091 itemsChanged();
Adam Cohendf2cc412011-04-27 16:56:57 -070092 }
93
Adam Cohen76fc0852011-06-17 13:26:23 -070094 public void setTitle(CharSequence title) {
95 this.title = title;
96 for (int i = 0; i < listeners.size(); i++) {
97 listeners.get(i).onTitleChanged(title);
98 }
99 }
100
Adam Cohendf2cc412011-04-27 16:56:57 -0700101 @Override
Kenny Guyed131872014-04-30 03:02:21 +0100102 void onAddToDatabase(Context context, ContentValues values) {
103 super.onAddToDatabase(context, values);
Adam Cohendf2cc412011-04-27 16:56:57 -0700104 values.put(LauncherSettings.Favorites.TITLE, title.toString());
Sunny Goyal5d85c442015-03-10 13:14:47 -0700105 values.put(LauncherSettings.Favorites.OPTIONS, options);
106
Adam Cohendf2cc412011-04-27 16:56:57 -0700107 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700108
109 void addListener(FolderListener listener) {
110 listeners.add(listener);
111 }
112
113 void removeListener(FolderListener listener) {
114 if (listeners.contains(listener)) {
115 listeners.remove(listener);
116 }
117 }
118
Adam Cohen76078c42011-06-09 15:06:52 -0700119 void itemsChanged() {
120 for (int i = 0; i < listeners.size(); i++) {
121 listeners.get(i).onItemsChanged();
122 }
123 }
124
Adam Cohen4eac29a2011-07-11 17:53:37 -0700125 @Override
126 void unbind() {
127 super.unbind();
128 listeners.clear();
129 }
130
Adam Cohena9cf38f2011-05-02 15:36:58 -0700131 interface FolderListener {
132 public void onAdd(ShortcutInfo item);
133 public void onRemove(ShortcutInfo item);
Adam Cohen76fc0852011-06-17 13:26:23 -0700134 public void onTitleChanged(CharSequence title);
Adam Cohen76078c42011-06-09 15:06:52 -0700135 public void onItemsChanged();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700136 }
Winson Chung64359a52013-07-08 17:17:08 -0700137
138 @Override
139 public String toString() {
140 return "FolderInfo(id=" + this.id + " type=" + this.itemType
141 + " container=" + this.container + " screen=" + screenId
142 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Sameer Padalabe3e4102014-04-21 19:36:14 -0700143 + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos) + ")";
Winson Chung64359a52013-07-08 17:17:08 -0700144 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700145
146 public boolean hasOption(int optionFlag) {
147 return (options & optionFlag) != 0;
148 }
149
150 /**
151 * @param option flag to set or clear
152 * @param isEnabled whether to set or clear the flag
153 * @param context if not null, save changes to the db.
154 */
155 public void setOption(int option, boolean isEnabled, Context context) {
156 int oldOptions = options;
157 if (isEnabled) {
158 options |= option;
159 } else {
160 options &= ~option;
161 }
162 if (context != null && oldOptions != options) {
163 LauncherModel.updateItemInDatabase(context, this);
164 }
165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166}