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