blob: 80b156413be323940317e44ad9bb5692a6061e98 [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 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 * Whether this folder has been opened
46 */
47 boolean opened;
48
Sunny Goyal5d85c442015-03-10 13:14:47 -070049 public int options;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 /**
Adam Cohendf2cc412011-04-27 16:56:57 -070052 * The apps and shortcuts
53 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -070054 public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
Adam Cohendf2cc412011-04-27 16:56:57 -070055
Adam Cohena9cf38f2011-05-02 15:36:58 -070056 ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
57
Sunny Goyal18bf8e22015-04-08 18:13:46 -070058 public FolderInfo() {
Adam Cohendf2cc412011-04-27 16:56:57 -070059 itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
Kenny Guyed131872014-04-30 03:02:21 +010060 user = UserHandleCompat.myUserHandle();
Adam Cohendf2cc412011-04-27 16:56:57 -070061 }
62
63 /**
64 * Add an app or shortcut
65 *
66 * @param item
67 */
68 public void add(ShortcutInfo item) {
69 contents.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070070 for (int i = 0; i < listeners.size(); i++) {
71 listeners.get(i).onAdd(item);
72 }
Adam Cohenac56cff2011-09-28 20:45:37 -070073 itemsChanged();
Adam Cohendf2cc412011-04-27 16:56:57 -070074 }
75
76 /**
77 * Remove an app or shortcut. Does not change the DB.
78 *
79 * @param item
80 */
81 public void remove(ShortcutInfo item) {
82 contents.remove(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070083 for (int i = 0; i < listeners.size(); i++) {
84 listeners.get(i).onRemove(item);
85 }
Adam Cohenac56cff2011-09-28 20:45:37 -070086 itemsChanged();
Adam Cohendf2cc412011-04-27 16:56:57 -070087 }
88
Adam Cohen76fc0852011-06-17 13:26:23 -070089 public void setTitle(CharSequence title) {
90 this.title = title;
91 for (int i = 0; i < listeners.size(); i++) {
92 listeners.get(i).onTitleChanged(title);
93 }
94 }
95
Adam Cohendf2cc412011-04-27 16:56:57 -070096 @Override
Kenny Guyed131872014-04-30 03:02:21 +010097 void onAddToDatabase(Context context, ContentValues values) {
98 super.onAddToDatabase(context, values);
Adam Cohendf2cc412011-04-27 16:56:57 -070099 values.put(LauncherSettings.Favorites.TITLE, title.toString());
Sunny Goyal5d85c442015-03-10 13:14:47 -0700100 values.put(LauncherSettings.Favorites.OPTIONS, options);
101
Adam Cohendf2cc412011-04-27 16:56:57 -0700102 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700103
104 void addListener(FolderListener listener) {
105 listeners.add(listener);
106 }
107
108 void removeListener(FolderListener listener) {
109 if (listeners.contains(listener)) {
110 listeners.remove(listener);
111 }
112 }
113
Adam Cohen76078c42011-06-09 15:06:52 -0700114 void itemsChanged() {
115 for (int i = 0; i < listeners.size(); i++) {
116 listeners.get(i).onItemsChanged();
117 }
118 }
119
Adam Cohen4eac29a2011-07-11 17:53:37 -0700120 @Override
121 void unbind() {
122 super.unbind();
123 listeners.clear();
124 }
125
Adam Cohena9cf38f2011-05-02 15:36:58 -0700126 interface FolderListener {
127 public void onAdd(ShortcutInfo item);
128 public void onRemove(ShortcutInfo item);
Adam Cohen76fc0852011-06-17 13:26:23 -0700129 public void onTitleChanged(CharSequence title);
Adam Cohen76078c42011-06-09 15:06:52 -0700130 public void onItemsChanged();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700131 }
Winson Chung64359a52013-07-08 17:17:08 -0700132
133 @Override
134 public String toString() {
135 return "FolderInfo(id=" + this.id + " type=" + this.itemType
136 + " container=" + this.container + " screen=" + screenId
137 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
Sameer Padalabe3e4102014-04-21 19:36:14 -0700138 + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos) + ")";
Winson Chung64359a52013-07-08 17:17:08 -0700139 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700140
141 public boolean hasOption(int optionFlag) {
142 return (options & optionFlag) != 0;
143 }
144
145 /**
146 * @param option flag to set or clear
147 * @param isEnabled whether to set or clear the flag
148 * @param context if not null, save changes to the db.
149 */
150 public void setOption(int option, boolean isEnabled, Context context) {
151 int oldOptions = options;
152 if (isEnabled) {
153 options |= option;
154 } else {
155 options &= ~option;
156 }
157 if (context != null && oldOptions != options) {
158 LauncherModel.updateItemInDatabase(context, this);
159 }
160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161}