1/*
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
17package com.android.launcher2;
18
19import java.util.ArrayList;
20
21import android.content.ContentValues;
22
23/**
24 * Represents a folder containing shortcuts or apps.
25 */
26class FolderInfo extends ItemInfo {
27
28    /**
29     * Whether this folder has been opened
30     */
31    boolean opened;
32
33    /**
34     * The apps and shortcuts
35     */
36    ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
37
38    ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
39
40    FolderInfo() {
41        itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
42    }
43
44    /**
45     * Add an app or shortcut
46     *
47     * @param item
48     */
49    public void add(ShortcutInfo item) {
50        contents.add(item);
51        for (int i = 0; i < listeners.size(); i++) {
52            listeners.get(i).onAdd(item);
53        }
54        itemsChanged();
55    }
56
57    /**
58     * Remove an app or shortcut. Does not change the DB.
59     *
60     * @param item
61     */
62    public void remove(ShortcutInfo item) {
63        contents.remove(item);
64        for (int i = 0; i < listeners.size(); i++) {
65            listeners.get(i).onRemove(item);
66        }
67        itemsChanged();
68    }
69
70    public void setTitle(CharSequence title) {
71        this.title = title;
72        for (int i = 0; i < listeners.size(); i++) {
73            listeners.get(i).onTitleChanged(title);
74        }
75    }
76
77    @Override
78    void onAddToDatabase(ContentValues values) {
79        super.onAddToDatabase(values);
80        values.put(LauncherSettings.Favorites.TITLE, title.toString());
81    }
82
83    void addListener(FolderListener listener) {
84        listeners.add(listener);
85    }
86
87    void removeListener(FolderListener listener) {
88        if (listeners.contains(listener)) {
89            listeners.remove(listener);
90        }
91    }
92
93    void itemsChanged() {
94        for (int i = 0; i < listeners.size(); i++) {
95            listeners.get(i).onItemsChanged();
96        }
97    }
98
99    @Override
100    void unbind() {
101        super.unbind();
102        listeners.clear();
103    }
104
105    interface FolderListener {
106        public void onAdd(ShortcutInfo item);
107        public void onRemove(ShortcutInfo item);
108        public void onTitleChanged(CharSequence title);
109        public void onItemsChanged();
110    }
111}
112