/packages/SystemUI/src/com/android/systemui/statusbar/phone/IconMerger.java

https://github.com/aizuzi/platform_frameworks_base · Java · 83 lines · 53 code · 13 blank · 17 comment · 10 complexity · 7ca464d91447240d2c8f295423c4c676 MD5 · raw file

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