blob: 0dfe525b752ca192d29cb0f296a2c8d9c2c7beb4 [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
Sunny Goyale48644a2016-05-16 16:16:14 -070024import java.lang.ref.WeakReference;
Winson Chung33231f52013-12-09 16:57:45 -080025import java.util.ArrayList;
26
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 */
Adam Cohenf9c184a2016-01-15 16:47:43 -080052 public boolean opened;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
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
Sunny Goyale48644a2016-05-16 16:16:14 -070061 /**
62 * A collection of listeners for folder info changes. Since this listeners are implemented by
63 * the UI objects, using a WeakReference prevents context leaks.
64 */
65 private WeakReference<FolderListener> mListener;
Adam Cohena9cf38f2011-05-02 15:36:58 -070066
Sunny Goyal18bf8e22015-04-08 18:13:46 -070067 public FolderInfo() {
Adam Cohendf2cc412011-04-27 16:56:57 -070068 itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
Kenny Guyed131872014-04-30 03:02:21 +010069 user = UserHandleCompat.myUserHandle();
Adam Cohendf2cc412011-04-27 16:56:57 -070070 }
71
72 /**
73 * Add an app or shortcut
74 *
75 * @param item
76 */
Sunny Goyalc52ba712016-04-05 15:59:05 -070077 public void add(ShortcutInfo item, boolean animate) {
Adam Cohendf2cc412011-04-27 16:56:57 -070078 contents.add(item);
Sunny Goyale48644a2016-05-16 16:16:14 -070079 FolderListener listener = mListener == null ? null : mListener.get();
80 if (listener != null) {
81 listener.onAdd(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070082 }
Sunny Goyalc52ba712016-04-05 15:59:05 -070083 itemsChanged(animate);
Adam Cohendf2cc412011-04-27 16:56:57 -070084 }
85
86 /**
87 * Remove an app or shortcut. Does not change the DB.
88 *
89 * @param item
90 */
Sunny Goyalc52ba712016-04-05 15:59:05 -070091 public void remove(ShortcutInfo item, boolean animate) {
Adam Cohendf2cc412011-04-27 16:56:57 -070092 contents.remove(item);
Sunny Goyale48644a2016-05-16 16:16:14 -070093 FolderListener listener = mListener == null ? null : mListener.get();
94 if (listener != null) {
95 listener.onRemove(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070096 }
Sunny Goyalc52ba712016-04-05 15:59:05 -070097 itemsChanged(animate);
Adam Cohendf2cc412011-04-27 16:56:57 -070098 }
99
100 @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
Sunny Goyale48644a2016-05-16 16:16:14 -0700108 /**
109 * Registers a listener for info change events.
110 */
111 public void setListener(FolderListener listener) {
112 mListener = new WeakReference<>(listener);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700113 }
114
Sunny Goyalc52ba712016-04-05 15:59:05 -0700115 public void itemsChanged(boolean animate) {
Sunny Goyale48644a2016-05-16 16:16:14 -0700116 FolderListener listener = mListener == null ? null : mListener.get();
117 if (listener != null) {
118 listener.onItemsChanged(animate);
Adam Cohen76078c42011-06-09 15:06:52 -0700119 }
120 }
121
Adam Cohen4eac29a2011-07-11 17:53:37 -0700122 @Override
123 void unbind() {
124 super.unbind();
Sunny Goyale48644a2016-05-16 16:16:14 -0700125 mListener = null;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700126 }
127
Adam Cohenf9c184a2016-01-15 16:47:43 -0800128 public interface FolderListener {
Sunny Goyale48644a2016-05-16 16:16:14 -0700129 void onAdd(ShortcutInfo item);
130 void onRemove(ShortcutInfo item);
131 void onItemsChanged(boolean animate);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700132 }
Winson Chung64359a52013-07-08 17:17:08 -0700133
134 @Override
135 public String toString() {
136 return "FolderInfo(id=" + this.id + " type=" + this.itemType
137 + " container=" + this.container + " screen=" + screenId
138 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700139 + " spanY=" + spanY + ")";
Winson Chung64359a52013-07-08 17:17:08 -0700140 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700141
142 public boolean hasOption(int optionFlag) {
143 return (options & optionFlag) != 0;
144 }
145
146 /**
147 * @param option flag to set or clear
148 * @param isEnabled whether to set or clear the flag
149 * @param context if not null, save changes to the db.
150 */
151 public void setOption(int option, boolean isEnabled, Context context) {
152 int oldOptions = options;
153 if (isEnabled) {
154 options |= option;
155 } else {
156 options &= ~option;
157 }
158 if (context != null && oldOptions != options) {
159 LauncherModel.updateItemInDatabase(context, this);
160 }
161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162}