1/**
2 * Copyright (C) 2015 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.settingslib.drawer;
18
19import android.content.ComponentName;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.text.TextUtils;
23import android.util.Log;
24
25import java.util.ArrayList;
26import java.util.List;
27
28public class DashboardCategory implements Parcelable {
29
30    private static final String TAG = "DashboardCategory";
31    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
32
33    /**
34     * Title of the category that is shown to the user.
35     */
36    public CharSequence title;
37
38    /**
39     * Key used for placing external tiles.
40     */
41    public String key;
42
43    /**
44     * Used to control display order.
45     */
46    public int priority;
47
48    /**
49     * List of the category's children
50     */
51    public List<Tile> tiles = new ArrayList<>();
52
53
54    public DashboardCategory() {
55        // Empty
56    }
57
58    public void addTile(Tile tile) {
59        tiles.add(tile);
60    }
61
62    public void addTile(int n, Tile tile) {
63        tiles.add(n, tile);
64    }
65
66    public void removeTile(Tile tile) {
67        tiles.remove(tile);
68    }
69
70    public void removeTile(int n) {
71        tiles.remove(n);
72    }
73
74    public int getTilesCount() {
75        return tiles.size();
76    }
77
78    public Tile getTile(int n) {
79        return tiles.get(n);
80    }
81
82    public boolean containsComponent(ComponentName component) {
83        for (Tile tile : tiles) {
84            if (TextUtils.equals(tile.intent.getComponent().getClassName(),
85                    component.getClassName())) {
86                if (DEBUG) {
87                    Log.d(TAG,  "category " + key + "contains component" + component);
88                }
89                return true;
90            }
91        }
92        if (DEBUG) {
93            Log.d(TAG,  "category " + key + " does not contain component" + component);
94        }
95        return false;
96    }
97
98    @Override
99    public int describeContents() {
100        return 0;
101    }
102
103    @Override
104    public void writeToParcel(Parcel dest, int flags) {
105        TextUtils.writeToParcel(title, dest, flags);
106        dest.writeString(key);
107        dest.writeInt(priority);
108
109        final int count = tiles.size();
110        dest.writeInt(count);
111
112        for (int n = 0; n < count; n++) {
113            Tile tile = tiles.get(n);
114            tile.writeToParcel(dest, flags);
115        }
116    }
117
118    public void readFromParcel(Parcel in) {
119        title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
120        key = in.readString();
121        priority = in.readInt();
122
123        final int count = in.readInt();
124
125        for (int n = 0; n < count; n++) {
126            Tile tile = Tile.CREATOR.createFromParcel(in);
127            tiles.add(tile);
128        }
129    }
130
131    DashboardCategory(Parcel in) {
132        readFromParcel(in);
133    }
134
135    public static final Creator<DashboardCategory> CREATOR = new Creator<DashboardCategory>() {
136        public DashboardCategory createFromParcel(Parcel source) {
137            return new DashboardCategory(source);
138        }
139
140        public DashboardCategory[] newArray(int size) {
141            return new DashboardCategory[size];
142        }
143    };
144}
145