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.launcher3;
18 
19 import android.os.Process;
20 
21 import com.android.launcher3.model.ModelWriter;
22 import com.android.launcher3.util.ContentWriter;
23 
24 import java.util.ArrayList;
25 
26 /**
27  * Represents a folder containing shortcuts or apps.
28  */
29 public class FolderInfo extends ItemInfo {
30 
31     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 
38     /**
39      * It is a work folder
40      */
41     public static final int FLAG_WORK_FOLDER = 0x00000002;
42 
43     /**
44      * The multi-page animation has run for this folder
45      */
46     public static final int FLAG_MULTI_PAGE_ANIMATION = 0x00000004;
47 
48     public int options;
49 
50     /**
51      * The apps and shortcuts
52      */
53     public ArrayList<WorkspaceItemInfo> contents = new ArrayList<>();
54 
55     private ArrayList<FolderListener> mListeners = new ArrayList<>();
56 
FolderInfo()57     public FolderInfo() {
58         itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
59         user = Process.myUserHandle();
60     }
61 
62     /**
63      * Add an app or shortcut
64      *
65      * @param item
66      */
add(WorkspaceItemInfo item, boolean animate)67     public void add(WorkspaceItemInfo item, boolean animate) {
68         add(item, contents.size(), animate);
69     }
70 
71     /**
72      * Add an app or shortcut for a specified rank.
73      */
add(WorkspaceItemInfo item, int rank, boolean animate)74     public void add(WorkspaceItemInfo item, int rank, boolean animate) {
75         rank = Utilities.boundToRange(rank, 0, contents.size() + 1);
76         contents.add(rank, item);
77         for (int i = 0; i < mListeners.size(); i++) {
78             mListeners.get(i).onAdd(item, rank);
79         }
80         itemsChanged(animate);
81     }
82 
83     /**
84      * Remove an app or shortcut. Does not change the DB.
85      *
86      * @param item
87      */
remove(WorkspaceItemInfo item, boolean animate)88     public void remove(WorkspaceItemInfo item, boolean animate) {
89         contents.remove(item);
90         for (int i = 0; i < mListeners.size(); i++) {
91             mListeners.get(i).onRemove(item);
92         }
93         itemsChanged(animate);
94     }
95 
96     @Override
onAddToDatabase(ContentWriter writer)97     public void onAddToDatabase(ContentWriter writer) {
98         super.onAddToDatabase(writer);
99         writer.put(LauncherSettings.Favorites.TITLE, title)
100                 .put(LauncherSettings.Favorites.OPTIONS, options);
101     }
102 
addListener(FolderListener listener)103     public void addListener(FolderListener listener) {
104         mListeners.add(listener);
105     }
106 
removeListener(FolderListener listener)107     public void removeListener(FolderListener listener) {
108         mListeners.remove(listener);
109     }
110 
itemsChanged(boolean animate)111     public void itemsChanged(boolean animate) {
112         for (int i = 0; i < mListeners.size(); i++) {
113             mListeners.get(i).onItemsChanged(animate);
114         }
115     }
116 
117     public interface FolderListener {
onAdd(WorkspaceItemInfo item, int rank)118         public void onAdd(WorkspaceItemInfo item, int rank);
onRemove(WorkspaceItemInfo item)119         public void onRemove(WorkspaceItemInfo item);
onItemsChanged(boolean animate)120         public void onItemsChanged(boolean animate);
121     }
122 
hasOption(int optionFlag)123     public boolean hasOption(int optionFlag) {
124         return (options & optionFlag) != 0;
125     }
126 
127     /**
128      * @param option flag to set or clear
129      * @param isEnabled whether to set or clear the flag
130      * @param writer if not null, save changes to the db.
131      */
setOption(int option, boolean isEnabled, ModelWriter writer)132     public void setOption(int option, boolean isEnabled, ModelWriter writer) {
133         int oldOptions = options;
134         if (isEnabled) {
135             options |= option;
136         } else {
137             options &= ~option;
138         }
139         if (writer != null && oldOptions != options) {
140             writer.updateItemInDatabase(this);
141         }
142     }
143 }
144