blob: 50ead3d1783375df1696470e2d9d56f236b71ceb [file] [log] [blame]
Joe Onorato503007d2010-04-16 09:20:55 -07001/*
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
Joe Onoratofd52b182010-11-10 18:00:52 -080017package com.android.systemui.statusbar.phone;
Joe Onorato503007d2010-04-16 09:20:55 -070018
19import android.content.Context;
Joe Onorato503007d2010-04-16 09:20:55 -070020import android.util.AttributeSet;
21import android.view.View;
22import android.widget.LinearLayout;
23
Joe Onoratob77f53b2010-05-28 19:59:51 -040024import com.android.systemui.R;
Joe Onoratob77f53b2010-05-28 19:59:51 -040025
Joe Onorato503007d2010-04-16 09:20:55 -070026public class IconMerger extends LinearLayout {
Joe Onoratob77f53b2010-05-28 19:59:51 -040027 private static final String TAG = "IconMerger";
Daniel Sandler05e24142011-11-10 11:56:49 -050028 private static final boolean DEBUG = false;
Joe Onoratob77f53b2010-05-28 19:59:51 -040029
Joe Onorato6c01a112010-10-04 17:38:47 -040030 private int mIconSize;
Daniel Sandler05e24142011-11-10 11:56:49 -050031 private View mMoreView;
Joe Onorato503007d2010-04-16 09:20:55 -070032
33 public IconMerger(Context context, AttributeSet attrs) {
34 super(context, attrs);
Joe Onorato6c01a112010-10-04 17:38:47 -040035
36 mIconSize = context.getResources().getDimensionPixelSize(
Daniel Sandler05e24142011-11-10 11:56:49 -050037 R.dimen.status_bar_icon_size);
Joe Onorato6c01a112010-10-04 17:38:47 -040038
Daniel Sandler05e24142011-11-10 11:56:49 -050039 if (DEBUG) {
40 setBackgroundColor(0x800099FF);
41 }
Daniel Sandler7c3e39d2011-07-29 16:30:49 -040042 }
43
Daniel Sandler05e24142011-11-10 11:56:49 -050044 public void setOverflowIndicator(View v) {
45 mMoreView = v;
Joe Onorato503007d2010-04-16 09:20:55 -070046 }
47
Daniel Sandler05e24142011-11-10 11:56:49 -050048 @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());
Joe Onoratob77f53b2010-05-28 19:59:51 -040054 }
55
Joe Onorato503007d2010-04-16 09:20:55 -070056 @Override
57 protected void onLayout(boolean changed, int l, int t, int r, int b) {
58 super.onLayout(changed, l, t, r, b);
Daniel Sandler05e24142011-11-10 11:56:49 -050059 checkOverflow(r - l);
60 }
Joe Onorato503007d2010-04-16 09:20:55 -070061
Daniel Sandler05e24142011-11-10 11:56:49 -050062 private void checkOverflow(int width) {
63 if (mMoreView == null) return;
64
Joe Onorato503007d2010-04-16 09:20:55 -070065 final int N = getChildCount();
Daniel Sandler05e24142011-11-10 11:56:49 -050066 int visibleChildren = 0;
67 for (int i=0; i<N; i++) {
68 if (getChildAt(i).getVisibility() != GONE) visibleChildren++;
Joe Onorato503007d2010-04-16 09:20:55 -070069 }
Daniel Sandler05e24142011-11-10 11:56:49 -050070 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);
Joe Onorato503007d2010-04-16 09:20:55 -070079 }
Daniel Sandler05e24142011-11-10 11:56:49 -050080 });
Joe Onorato503007d2010-04-16 09:20:55 -070081 }
Joe Onorato6c01a112010-10-04 17:38:47 -040082 }
Joe Onorato503007d2010-04-16 09:20:55 -070083}