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