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<ShortcutInfo> contents = new ArrayList<ShortcutInfo>(); 54 55 ArrayList<FolderListener> listeners = new ArrayList<FolderListener>(); 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(ShortcutInfo item, boolean animate)67 public void add(ShortcutInfo item, boolean animate) { 68 contents.add(item); 69 for (int i = 0; i < listeners.size(); i++) { 70 listeners.get(i).onAdd(item); 71 } 72 itemsChanged(animate); 73 } 74 75 /** 76 * Remove an app or shortcut. Does not change the DB. 77 * 78 * @param item 79 */ remove(ShortcutInfo item, boolean animate)80 public void remove(ShortcutInfo item, boolean animate) { 81 contents.remove(item); 82 for (int i = 0; i < listeners.size(); i++) { 83 listeners.get(i).onRemove(item); 84 } 85 itemsChanged(animate); 86 } 87 setTitle(CharSequence title)88 public void setTitle(CharSequence title) { 89 this.title = title; 90 for (int i = 0; i < listeners.size(); i++) { 91 listeners.get(i).onTitleChanged(title); 92 } 93 } 94 95 @Override onAddToDatabase(ContentWriter writer)96 public void onAddToDatabase(ContentWriter writer) { 97 super.onAddToDatabase(writer); 98 writer.put(LauncherSettings.Favorites.TITLE, title) 99 .put(LauncherSettings.Favorites.OPTIONS, options); 100 101 } 102 addListener(FolderListener listener)103 public void addListener(FolderListener listener) { 104 listeners.add(listener); 105 } 106 removeListener(FolderListener listener)107 public void removeListener(FolderListener listener) { 108 listeners.remove(listener); 109 } 110 itemsChanged(boolean animate)111 public void itemsChanged(boolean animate) { 112 for (int i = 0; i < listeners.size(); i++) { 113 listeners.get(i).onItemsChanged(animate); 114 } 115 } 116 prepareAutoUpdate()117 public void prepareAutoUpdate() { 118 for (int i = 0; i < listeners.size(); i++) { 119 listeners.get(i).prepareAutoUpdate(); 120 } 121 } 122 123 public interface FolderListener { onAdd(ShortcutInfo item)124 public void onAdd(ShortcutInfo item); onRemove(ShortcutInfo item)125 public void onRemove(ShortcutInfo item); onTitleChanged(CharSequence title)126 public void onTitleChanged(CharSequence title); onItemsChanged(boolean animate)127 public void onItemsChanged(boolean animate); prepareAutoUpdate()128 public void prepareAutoUpdate(); 129 } 130 hasOption(int optionFlag)131 public boolean hasOption(int optionFlag) { 132 return (options & optionFlag) != 0; 133 } 134 135 /** 136 * @param option flag to set or clear 137 * @param isEnabled whether to set or clear the flag 138 * @param writer if not null, save changes to the db. 139 */ setOption(int option, boolean isEnabled, ModelWriter writer)140 public void setOption(int option, boolean isEnabled, ModelWriter writer) { 141 int oldOptions = options; 142 if (isEnabled) { 143 options |= option; 144 } else { 145 options &= ~option; 146 } 147 if (writer != null && oldOptions != options) { 148 writer.updateItemInDatabase(this); 149 } 150 } 151 } 152