blob: 6e0dcd421b568a84fe72d4540083cb772caa9391 [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;
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
48 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 * Whether this folder has been opened
50 */
51 boolean opened;
52
Sunny Goyal5d85c442015-03-10 13:14:47 -070053 public int options;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 /**
Adam Cohendf2cc412011-04-27 16:56:57 -070056 * The apps and shortcuts
57 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -070058 public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
Adam Cohendf2cc412011-04-27 16:56:57 -070059
Adam Cohena9cf38f2011-05-02 15:36:58 -070060 ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
61
Sunny Goyal18bf8e22015-04-08 18:13:46 -070062 public FolderInfo() {
Adam Cohendf2cc412011-04-27 16:56:57 -070063 itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
Kenny Guyed131872014-04-30 03:02:21 +010064 user = UserHandleCompat.myUserHandle();
Adam Cohendf2cc412011-04-27 16:56:57 -070065 }
66
67 /**
68 * Add an app or shortcut
69 *
70 * @param item
71 */
72 public void add(ShortcutInfo item) {
73 contents.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070074 for (int i = 0; i < listeners.size(); i++) {
75 listeners.get(i).onAdd(item);
76 }
Adam Cohenac56cff2011-09-28 20:45:37 -070077 itemsChanged();
Adam Cohendf2cc412011-04-27 16:56:57 -070078 }
79
80 /**
81 * Remove an app or shortcut. Does not change the DB.
82 *
83 * @param item
84 */
85 public void remove(ShortcutInfo item) {
86 contents.remove(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070087 for (int i = 0; i < listeners.size(); i++) {
88 listeners.get(i).onRemove(item);
89 }
Adam Cohenac56cff2011-09-28 20:45:37 -070090 itemsChanged();
Adam Cohendf2cc412011-04-27 16:56:57 -070091 }
92
Adam Cohen76fc0852011-06-17 13:26:23 -070093 public void setTitle(CharSequence title) {
Sunny Goyala508e4f2015-05-21 09:33:57 -070094 this.title = title;
Adam Cohen76fc0852011-06-17 13:26:23 -070095 for (int i = 0; i < listeners.size(); i++) {
96 listeners.get(i).onTitleChanged(title);
97 }
98 }
99
Adam Cohendf2cc412011-04-27 16:56:57 -0700100 @Override
Kenny Guyed131872014-04-30 03:02:21 +0100101 void onAddToDatabase(Context context, ContentValues values) {
102 super.onAddToDatabase(context, values);
Adam Cohendf2cc412011-04-27 16:56:57 -0700103 values.put(LauncherSettings.Favorites.TITLE, title.toString());
Sunny Goyal5d85c442015-03-10 13:14:47 -0700104 values.put(LauncherSettings.Favorites.OPTIONS, options);
105
Adam Cohendf2cc412011-04-27 16:56:57 -0700106 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700107
108 void addListener(FolderListener listener) {
109 listeners.add(listener);
110 }
111
112 void removeListener(FolderListener listener) {
113 if (listeners.contains(listener)) {
114 listeners.remove(listener);
115 }
116 }
117
Adam Cohen76078c42011-06-09 15:06:52 -0700118 void itemsChanged() {
119 for (int i = 0; i < listeners.size(); i++) {
120 listeners.get(i).onItemsChanged();
121 }
122 }
123
Adam Cohen4eac29a2011-07-11 17:53:37 -0700124 @Override
125 void unbind() {
126 super.unbind();
127 listeners.clear();
128 }
129
Adam Cohena9cf38f2011-05-02 15:36:58 -0700130 interface FolderListener {
131 public void onAdd(ShortcutInfo item);
132 public void onRemove(ShortcutInfo item);
Adam Cohen76fc0852011-06-17 13:26:23 -0700133 public void onTitleChanged(CharSequence title);
Adam Cohen76078c42011-06-09 15:06:52 -0700134 public void onItemsChanged();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700135 }
Winson Chung64359a52013-07-08 17:17:08 -0700136
137 @Override
138 public String toString() {
139 return "FolderInfo(id=" + this.id + " type=" + this.itemType
140 + " container=" + this.container + " screen=" + screenId
141 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700142 + " spanY=" + spanY + ")";
Winson Chung64359a52013-07-08 17:17:08 -0700143 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700144
145 public boolean hasOption(int optionFlag) {
146 return (options & optionFlag) != 0;
147 }
148
149 /**
150 * @param option flag to set or clear
151 * @param isEnabled whether to set or clear the flag
152 * @param context if not null, save changes to the db.
153 */
154 public void setOption(int option, boolean isEnabled, Context context) {
155 int oldOptions = options;
156 if (isEnabled) {
157 options |= option;
158 } else {
159 options &= ~option;
160 }
161 if (context != null && oldOptions != options) {
162 LauncherModel.updateItemInDatabase(context, this);
163 }
164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165}