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