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