Home | History | Annotate | Download | only in launcher3
      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 import java.util.Arrays;
     26 
     27 /**
     28  * Represents a folder containing shortcuts or apps.
     29  */
     30 public class FolderInfo extends ItemInfo {
     31 
     32     public static final int NO_FLAGS = 0x00000000;
     33 
     34     /**
     35      * The folder is locked in sorted mode
     36      */
     37     public static final int FLAG_ITEMS_SORTED = 0x00000001;
     38 
     39     /**
     40      * It is a work folder
     41      */
     42     public static final int FLAG_WORK_FOLDER = 0x00000002;
     43 
     44     /**
     45      * The multi-page animation has run for this folder
     46      */
     47     public static final int FLAG_MULTI_PAGE_ANIMATION = 0x00000004;
     48 
     49     /**
     50      * Whether this folder has been opened
     51      */
     52     boolean opened;
     53 
     54     public int options;
     55 
     56     /**
     57      * The apps and shortcuts
     58      */
     59     public ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
     60 
     61     ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
     62 
     63     public FolderInfo() {
     64         itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
     65         user = UserHandleCompat.myUserHandle();
     66     }
     67 
     68     /**
     69      * Add an app or shortcut
     70      *
     71      * @param item
     72      */
     73     public void add(ShortcutInfo item) {
     74         contents.add(item);
     75         for (int i = 0; i < listeners.size(); i++) {
     76             listeners.get(i).onAdd(item);
     77         }
     78         itemsChanged();
     79     }
     80 
     81     /**
     82      * Remove an app or shortcut. Does not change the DB.
     83      *
     84      * @param item
     85      */
     86     public void remove(ShortcutInfo item) {
     87         contents.remove(item);
     88         for (int i = 0; i < listeners.size(); i++) {
     89             listeners.get(i).onRemove(item);
     90         }
     91         itemsChanged();
     92     }
     93 
     94     public void setTitle(CharSequence title) {
     95         this.title = title;
     96         for (int i = 0; i < listeners.size(); i++) {
     97             listeners.get(i).onTitleChanged(title);
     98         }
     99     }
    100 
    101     @Override
    102     void onAddToDatabase(Context context, ContentValues values) {
    103         super.onAddToDatabase(context, values);
    104         values.put(LauncherSettings.Favorites.TITLE, title.toString());
    105         values.put(LauncherSettings.Favorites.OPTIONS, options);
    106 
    107     }
    108 
    109     void addListener(FolderListener listener) {
    110         listeners.add(listener);
    111     }
    112 
    113     void removeListener(FolderListener listener) {
    114         if (listeners.contains(listener)) {
    115             listeners.remove(listener);
    116         }
    117     }
    118 
    119     void itemsChanged() {
    120         for (int i = 0; i < listeners.size(); i++) {
    121             listeners.get(i).onItemsChanged();
    122         }
    123     }
    124 
    125     @Override
    126     void unbind() {
    127         super.unbind();
    128         listeners.clear();
    129     }
    130 
    131     interface FolderListener {
    132         public void onAdd(ShortcutInfo item);
    133         public void onRemove(ShortcutInfo item);
    134         public void onTitleChanged(CharSequence title);
    135         public void onItemsChanged();
    136     }
    137 
    138     @Override
    139     public String toString() {
    140         return "FolderInfo(id=" + this.id + " type=" + this.itemType
    141                 + " container=" + this.container + " screen=" + screenId
    142                 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX
    143                 + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos) + ")";
    144     }
    145 
    146     public boolean hasOption(int optionFlag) {
    147         return (options & optionFlag) != 0;
    148     }
    149 
    150     /**
    151      * @param option flag to set or clear
    152      * @param isEnabled whether to set or clear the flag
    153      * @param context if not null, save changes to the db.
    154      */
    155     public void setOption(int option, boolean isEnabled, Context context) {
    156         int oldOptions = options;
    157         if (isEnabled) {
    158             options |= option;
    159         } else {
    160             options &= ~option;
    161         }
    162         if (context != null && oldOptions != options) {
    163             LauncherModel.updateItemInDatabase(context, this);
    164         }
    165     }
    166 }
    167