1/*
2 * Copyright (C) 2014 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
17package com.android.settings.dashboard;
18
19import android.content.res.Resources;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.os.Parcelable.Creator;
23import android.text.TextUtils;
24
25import java.util.ArrayList;
26import java.util.List;
27
28public class DashboardCategory implements Parcelable {
29
30    /**
31     * Default value for {@link com.android.settings.dashboard.DashboardCategory#id DashboardCategory.id}
32     * indicating that no identifier value is set.  All other values (including those below -1)
33     * are valid.
34     */
35    public static final long CAT_ID_UNDEFINED = -1;
36
37    /**
38     * Identifier for this tile, to correlate with a new list when
39     * it is updated.  The default value is
40     * {@link com.android.settings.dashboard.DashboardTile#TILE_ID_UNDEFINED}, meaning no id.
41     * @attr ref android.R.styleable#PreferenceHeader_id
42     */
43    public long id = CAT_ID_UNDEFINED;
44
45    /**
46     * Resource ID of title of the category that is shown to the user.
47     */
48    public int titleRes;
49
50    /**
51     * Title of the category that is shown to the user.
52     */
53    public CharSequence title;
54
55    /**
56     * List of the category's children
57     */
58    public List<DashboardTile> tiles = new ArrayList<DashboardTile>();
59
60
61    public DashboardCategory() {
62        // Empty
63    }
64
65    public void addTile(DashboardTile tile) {
66        tiles.add(tile);
67    }
68
69    public void addTile(int n, DashboardTile tile) {
70        tiles.add(n, tile);
71    }
72
73    public void removeTile(DashboardTile tile) {
74        tiles.remove(tile);
75    }
76
77    public void removeTile(int n) {
78        tiles.remove(n);
79    }
80
81    public int getTilesCount() {
82        return tiles.size();
83    }
84
85    public DashboardTile getTile(int n) {
86        return tiles.get(n);
87    }
88
89    /**
90     * Return the currently set title.  If {@link #titleRes} is set,
91     * this resource is loaded from <var>res</var> and returned.  Otherwise
92     * {@link #title} is returned.
93     */
94    public CharSequence getTitle(Resources res) {
95        if (titleRes != 0) {
96            return res.getText(titleRes);
97        }
98        return title;
99    }
100
101    @Override
102    public int describeContents() {
103        return 0;
104    }
105
106    @Override
107    public void writeToParcel(Parcel dest, int flags) {
108        dest.writeInt(titleRes);
109        TextUtils.writeToParcel(title, dest, flags);
110
111        final int count = tiles.size();
112        dest.writeInt(count);
113
114        for (int n = 0; n < count; n++) {
115            DashboardTile tile = tiles.get(n);
116            tile.writeToParcel(dest, flags);
117        }
118    }
119
120    public void readFromParcel(Parcel in) {
121        titleRes = in.readInt();
122        title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
123
124        final int count = in.readInt();
125
126        for (int n = 0; n < count; n++) {
127            DashboardTile tile = DashboardTile.CREATOR.createFromParcel(in);
128            tiles.add(tile);
129        }
130    }
131
132    DashboardCategory(Parcel in) {
133        readFromParcel(in);
134    }
135
136    public static final Creator<DashboardCategory> CREATOR = new Creator<DashboardCategory>() {
137        public DashboardCategory createFromParcel(Parcel source) {
138            return new DashboardCategory(source);
139        }
140
141        public DashboardCategory[] newArray(int size) {
142            return new DashboardCategory[size];
143        }
144    };
145}
146