Home | History | Annotate | Download | only in launcher2
      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.launcher2;
     18 
     19 import java.util.ArrayList;
     20 
     21 import android.content.ContentValues;
     22 
     23 /**
     24  * Represents a folder containing shortcuts or apps.
     25  */
     26 class FolderInfo extends ItemInfo {
     27 
     28     /**
     29      * Whether this folder has been opened
     30      */
     31     boolean opened;
     32 
     33     /**
     34      * The folder name.
     35      */
     36     CharSequence title;
     37 
     38     /**
     39      * The apps and shortcuts
     40      */
     41     ArrayList<ShortcutInfo> contents = new ArrayList<ShortcutInfo>();
     42 
     43     ArrayList<FolderListener> listeners = new ArrayList<FolderListener>();
     44 
     45     FolderInfo() {
     46         itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
     47     }
     48 
     49     /**
     50      * Add an app or shortcut
     51      *
     52      * @param item
     53      */
     54     public void add(ShortcutInfo item) {
     55         contents.add(item);
     56         for (int i = 0; i < listeners.size(); i++) {
     57             listeners.get(i).onAdd(item);
     58         }
     59         itemsChanged();
     60     }
     61 
     62     /**
     63      * Remove an app or shortcut. Does not change the DB.
     64      *
     65      * @param item
     66      */
     67     public void remove(ShortcutInfo item) {
     68         contents.remove(item);
     69         for (int i = 0; i < listeners.size(); i++) {
     70             listeners.get(i).onRemove(item);
     71         }
     72         itemsChanged();
     73     }
     74 
     75     public void setTitle(CharSequence title) {
     76         this.title = title;
     77         for (int i = 0; i < listeners.size(); i++) {
     78             listeners.get(i).onTitleChanged(title);
     79         }
     80     }
     81 
     82     @Override
     83     void onAddToDatabase(ContentValues values) {
     84         super.onAddToDatabase(values);
     85         values.put(LauncherSettings.Favorites.TITLE, title.toString());
     86     }
     87 
     88     void addListener(FolderListener listener) {
     89         listeners.add(listener);
     90     }
     91 
     92     void removeListener(FolderListener listener) {
     93         if (listeners.contains(listener)) {
     94             listeners.remove(listener);
     95         }
     96     }
     97 
     98     void itemsChanged() {
     99         for (int i = 0; i < listeners.size(); i++) {
    100             listeners.get(i).onItemsChanged();
    101         }
    102     }
    103 
    104     @Override
    105     void unbind() {
    106         super.unbind();
    107         listeners.clear();
    108     }
    109 
    110     interface FolderListener {
    111         public void onAdd(ShortcutInfo item);
    112         public void onRemove(ShortcutInfo item);
    113         public void onTitleChanged(CharSequence title);
    114         public void onItemsChanged();
    115     }
    116 }
    117