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.content.ContentValues; 20 import android.content.Context; 21 22 import com.android.launcher3.compat.UserHandleCompat; 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 /** 49 * Whether this folder has been opened 50 */ 51 public boolean opened; 52 53 public int options; 54 55 /** 56 * The apps and shortcuts 57 */ 58 public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>(); 59 60 ArrayList<FolderListener> listeners = new ArrayList<FolderListener>(); 61 FolderInfo()62 public FolderInfo() { 63 itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER; 64 user = UserHandleCompat.myUserHandle(); 65 } 66 67 /** 68 * Add an app or shortcut 69 * 70 * @param item 71 */ add(ShortcutInfo item, boolean animate)72 public void add(ShortcutInfo item, boolean animate) { 73 contents.add(item); 74 for (int i = 0; i < listeners.size(); i++) { 75 listeners.get(i).onAdd(item); 76 } 77 itemsChanged(animate); 78 } 79 80 /** 81 * Remove an app or shortcut. Does not change the DB. 82 * 83 * @param item 84 */ remove(ShortcutInfo item, boolean animate)85 public void remove(ShortcutInfo item, boolean animate) { 86 contents.remove(item); 87 for (int i = 0; i < listeners.size(); i++) { 88 listeners.get(i).onRemove(item); 89 } 90 itemsChanged(animate); 91 } 92 setTitle(CharSequence title)93 public void setTitle(CharSequence title) { 94 this.title = title; 95 for (int i = 0; i < listeners.size(); i++) { 96 listeners.get(i).onTitleChanged(title); 97 } 98 } 99 100 @Override onAddToDatabase(Context context, ContentValues values)101 void onAddToDatabase(Context context, ContentValues values) { 102 super.onAddToDatabase(context, values); 103 values.put(LauncherSettings.Favorites.TITLE, title.toString()); 104 values.put(LauncherSettings.Favorites.OPTIONS, options); 105 106 } 107 addListener(FolderListener listener)108 public void addListener(FolderListener listener) { 109 listeners.add(listener); 110 } 111 removeListener(FolderListener listener)112 public void removeListener(FolderListener listener) { 113 listeners.remove(listener); 114 } 115 itemsChanged(boolean animate)116 public void itemsChanged(boolean animate) { 117 for (int i = 0; i < listeners.size(); i++) { 118 listeners.get(i).onItemsChanged(animate); 119 } 120 } 121 122 public interface FolderListener { onAdd(ShortcutInfo item)123 public void onAdd(ShortcutInfo item); onRemove(ShortcutInfo item)124 public void onRemove(ShortcutInfo item); onTitleChanged(CharSequence title)125 public void onTitleChanged(CharSequence title); onItemsChanged(boolean animate)126 public void onItemsChanged(boolean animate); 127 } 128 hasOption(int optionFlag)129 public boolean hasOption(int optionFlag) { 130 return (options & optionFlag) != 0; 131 } 132 133 /** 134 * @param option flag to set or clear 135 * @param isEnabled whether to set or clear the flag 136 * @param context if not null, save changes to the db. 137 */ setOption(int option, boolean isEnabled, Context context)138 public void setOption(int option, boolean isEnabled, Context context) { 139 int oldOptions = options; 140 if (isEnabled) { 141 options |= option; 142 } else { 143 options &= ~option; 144 } 145 if (context != null && oldOptions != options) { 146 LauncherModel.updateItemInDatabase(context, this); 147 } 148 } 149 } 150