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