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
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
20import android.os.Handler;
21import android.util.AttributeSet;
22import android.util.Slog;
23import android.view.View;
24import android.widget.LinearLayout;
25
26import com.android.internal.statusbar.StatusBarIcon;
27
28import com.android.systemui.R;
29import com.android.systemui.statusbar.StatusBarIconView;
30
31public class IconMerger extends LinearLayout {
32    private static final String TAG = "IconMerger";
33    private static final boolean DEBUG = false;
34
35    private int mIconSize;
36    private View mMoreView;
37
38    public IconMerger(Context context, AttributeSet attrs) {
39        super(context, attrs);
40
41        mIconSize = context.getResources().getDimensionPixelSize(
42                R.dimen.status_bar_icon_size);
43
44        if (DEBUG) {
45            setBackgroundColor(0x800099FF);
46        }
47    }
48
49    public void setOverflowIndicator(View v) {
50        mMoreView = v;
51    }
52
53    @Override
54    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
55        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
56        // we need to constrain this to an integral multiple of our children
57        int width = getMeasuredWidth();
58        setMeasuredDimension(width - (width % mIconSize), getMeasuredHeight());
59    }
60
61    @Override
62    protected void onLayout(boolean changed, int l, int t, int r, int b) {
63        super.onLayout(changed, l, t, r, b);
64        checkOverflow(r - l);
65    }
66
67    private void checkOverflow(int width) {
68        if (mMoreView == null) return;
69
70        final int N = getChildCount();
71        int visibleChildren = 0;
72        for (int i=0; i<N; i++) {
73            if (getChildAt(i).getVisibility() != GONE) visibleChildren++;
74        }
75        final boolean overflowShown = (mMoreView.getVisibility() == View.VISIBLE);
76        // let's assume we have one more slot if the more icon is already showing
77        if (overflowShown) visibleChildren --;
78        final boolean moreRequired = visibleChildren * mIconSize > width;
79        if (moreRequired != overflowShown) {
80            post(new Runnable() {
81                @Override
82                public void run() {
83                    mMoreView.setVisibility(moreRequired ? View.VISIBLE : View.GONE);
84                }
85            });
86        }
87    }
88}
89