Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 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 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.phone; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 20 | import android.util.AttributeSet; |
| 21 | import android.view.View; |
| 22 | import android.widget.LinearLayout; |
| 23 | |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 24 | import com.android.systemui.R; |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 25 | |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 26 | public class IconMerger extends LinearLayout { |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 27 | private static final String TAG = "IconMerger"; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 28 | private static final boolean DEBUG = false; |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 29 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 30 | private int mIconSize; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 31 | private View mMoreView; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 32 | |
| 33 | public IconMerger(Context context, AttributeSet attrs) { |
| 34 | super(context, attrs); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 35 | |
| 36 | mIconSize = context.getResources().getDimensionPixelSize( |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 37 | R.dimen.status_bar_icon_size); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 38 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 39 | if (DEBUG) { |
| 40 | setBackgroundColor(0x800099FF); |
| 41 | } |
Daniel Sandler | 7c3e39d | 2011-07-29 16:30:49 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 44 | public void setOverflowIndicator(View v) { |
| 45 | mMoreView = v; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 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()); |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 56 | @Override |
| 57 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 58 | super.onLayout(changed, l, t, r, b); |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 59 | checkOverflow(r - l); |
| 60 | } |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 61 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 62 | private void checkOverflow(int width) { |
| 63 | if (mMoreView == null) return; |
| 64 | |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 65 | final int N = getChildCount(); |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 66 | int visibleChildren = 0; |
| 67 | for (int i=0; i<N; i++) { |
| 68 | if (getChildAt(i).getVisibility() != GONE) visibleChildren++; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 69 | } |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 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); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 79 | } |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 80 | }); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 81 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 82 | } |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 83 | } |