1/*
2 * Copyright (C) 2007 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.internal.statusbar;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.io.PrintWriter;
23
24public class StatusBarIconList implements Parcelable {
25    private String[] mSlots;
26    private StatusBarIcon[] mIcons;
27
28    public StatusBarIconList() {
29    }
30
31    public StatusBarIconList(Parcel in) {
32        readFromParcel(in);
33    }
34
35    public void readFromParcel(Parcel in) {
36        this.mSlots = in.readStringArray();
37        final int N = in.readInt();
38        if (N < 0) {
39            mIcons = null;
40        } else {
41            mIcons = new StatusBarIcon[N];
42            for (int i=0; i<N; i++) {
43                if (in.readInt() != 0) {
44                    mIcons[i] = new StatusBarIcon(in);
45                }
46            }
47        }
48    }
49
50    public void writeToParcel(Parcel out, int flags) {
51        out.writeStringArray(mSlots);
52        if (mIcons == null) {
53            out.writeInt(-1);
54        } else {
55            final int N = mIcons.length;
56            out.writeInt(N);
57            for (int i=0; i<N; i++) {
58                StatusBarIcon ic = mIcons[i];
59                if (ic == null) {
60                    out.writeInt(0);
61                } else {
62                    out.writeInt(1);
63                    ic.writeToParcel(out, flags);
64                }
65            }
66        }
67    }
68
69    public int describeContents() {
70        return 0;
71    }
72
73    /**
74     * Parcelable.Creator that instantiates StatusBarIconList objects
75     */
76    public static final Parcelable.Creator<StatusBarIconList> CREATOR
77            = new Parcelable.Creator<StatusBarIconList>()
78    {
79        public StatusBarIconList createFromParcel(Parcel parcel)
80        {
81            return new StatusBarIconList(parcel);
82        }
83
84        public StatusBarIconList[] newArray(int size)
85        {
86            return new StatusBarIconList[size];
87        }
88    };
89
90    public void defineSlots(String[] slots) {
91        final int N = slots.length;
92        String[] s = mSlots = new String[N];
93        for (int i=0; i<N; i++) {
94            s[i] = slots[i];
95        }
96        mIcons = new StatusBarIcon[N];
97    }
98
99    public int getSlotIndex(String slot) {
100        final int N = mSlots.length;
101        for (int i=0; i<N; i++) {
102            if (slot.equals(mSlots[i])) {
103                return i;
104            }
105        }
106        return -1;
107    }
108
109    public int size() {
110        return mSlots.length;
111    }
112
113    public void setIcon(int index, StatusBarIcon icon) {
114        mIcons[index] = icon.clone();
115    }
116
117    public void removeIcon(int index) {
118        mIcons[index] = null;
119    }
120
121    public String getSlot(int index) {
122        return mSlots[index];
123    }
124
125    public StatusBarIcon getIcon(int index) {
126        return mIcons[index];
127    }
128
129    public int getViewIndex(int index) {
130        int count = 0;
131        for (int i=0; i<index; i++) {
132            if (mIcons[i] != null) {
133                count++;
134            }
135        }
136        return count;
137    }
138
139    public void copyFrom(StatusBarIconList that) {
140        if (that.mSlots == null) {
141            this.mSlots = null;
142            this.mIcons = null;
143        } else {
144            final int N = that.mSlots.length;
145            this.mSlots = new String[N];
146            this.mIcons = new StatusBarIcon[N];
147            for (int i=0; i<N; i++) {
148                this.mSlots[i] = that.mSlots[i];
149                this.mIcons[i] = that.mIcons[i] != null ? that.mIcons[i].clone() : null;
150            }
151        }
152    }
153
154    public void dump(PrintWriter pw) {
155        final int N = mSlots.length;
156        pw.println("Icon list:");
157        for (int i=0; i<N; i++) {
158            pw.printf("  %2d: (%s) %s\n", i, mSlots[i], mIcons[i]);
159        }
160    }
161}
162