Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 22 | import android.util.Log; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 23 | |
| 24 | import android.view.View; |
| 25 | import com.android.keyguard.AlphaOptimizedLinearLayout; |
| 26 | import com.android.systemui.R; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 27 | import com.android.systemui.statusbar.StatusIconDisplayable; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.stack.ViewState; |
| 29 | |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 30 | /** |
| 31 | * A container for Status bar system icons. Limits the number of system icons and handles overflow |
| 32 | * similar to NotificationIconController. Can be used to layout nested StatusIconContainers |
| 33 | * |
| 34 | * Children are expected to be of type StatusBarIconView. |
| 35 | */ |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 36 | public class StatusIconContainer extends AlphaOptimizedLinearLayout { |
| 37 | |
| 38 | private static final String TAG = "StatusIconContainer"; |
Evan Laird | e0fbc3e | 2018-02-13 11:41:00 -0500 | [diff] [blame] | 39 | private static final boolean DEBUG = false; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 40 | private static final int MAX_ICONS = 5; |
| 41 | private static final int MAX_DOTS = 3; |
| 42 | |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 43 | public StatusIconContainer(Context context) { |
| 44 | this(context, null); |
| 45 | } |
| 46 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 47 | public StatusIconContainer(Context context, AttributeSet attrs) { |
| 48 | super(context, attrs); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 53 | float midY = getHeight() / 2.0f; |
| 54 | |
| 55 | // Layout all child views so that we can move them around later |
| 56 | for (int i = 0; i < getChildCount(); i++) { |
| 57 | View child = getChildAt(i); |
| 58 | int width = child.getMeasuredWidth(); |
| 59 | int height = child.getMeasuredHeight(); |
| 60 | int top = (int) (midY - height / 2.0f); |
| 61 | child.layout(0, top, width, top + height); |
| 62 | } |
| 63 | |
| 64 | resetViewStates(); |
| 65 | calculateIconTranslations(); |
| 66 | applyIconStates(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 71 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Evan Laird | 264275e | 2018-03-01 19:39:39 -0500 | [diff] [blame] | 72 | |
| 73 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 74 | int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED); |
| 75 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 76 | final int count = getChildCount(); |
| 77 | // Measure all children so that they report the correct width |
| 78 | for (int i = 0; i < count; i++) { |
Evan Laird | 264275e | 2018-03-01 19:39:39 -0500 | [diff] [blame] | 79 | measureChild(getChildAt(i), widthSpec, heightMeasureSpec); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onViewAdded(View child) { |
| 85 | super.onViewAdded(child); |
| 86 | ViewState vs = new ViewState(); |
| 87 | child.setTag(R.id.status_bar_view_state_tag, vs); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onViewRemoved(View child) { |
| 92 | super.onViewRemoved(child); |
| 93 | child.setTag(R.id.status_bar_view_state_tag, null); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Layout is happening from end -> start |
| 98 | */ |
| 99 | private void calculateIconTranslations() { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 100 | float width = getWidth() - getPaddingEnd(); |
Evan Laird | 6303613 | 2018-02-02 16:30:41 -0500 | [diff] [blame] | 101 | float translationX = width; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 102 | float contentStart = getPaddingStart(); |
| 103 | int childCount = getChildCount(); |
| 104 | // Underflow === don't show content until that index |
| 105 | int firstUnderflowIndex = -1; |
Evan Laird | 264275e | 2018-03-01 19:39:39 -0500 | [diff] [blame] | 106 | if (DEBUG) android.util.Log.d(TAG, "calculateIconTransitions: start=" + translationX |
| 107 | + " width=" + width); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 108 | |
| 109 | //TODO: Dots |
| 110 | for (int i = childCount - 1; i >= 0; i--) { |
| 111 | View child = getChildAt(i); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 112 | if (!(child instanceof StatusIconDisplayable)) { |
| 113 | if (DEBUG) Log.d(TAG, "skipping child (wrong type)"); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 114 | continue; |
| 115 | } |
| 116 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 117 | StatusIconDisplayable iconView = (StatusIconDisplayable) child; |
| 118 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 119 | ViewState childState = getViewStateFromChild(child); |
| 120 | if (childState == null ) { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 121 | if (DEBUG) Log.d(TAG, "skipping child (" + iconView.getSlot() + ") no ViewState"); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 122 | continue; |
| 123 | } |
| 124 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 125 | if (!iconView.isIconVisible() || iconView.isIconBlocked()) { |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 126 | childState.hidden = true; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 127 | if (DEBUG) Log.d(TAG, "skipping child (" + iconView.getSlot() + ") not visible"); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
| 131 | childState.xTranslation = translationX - child.getWidth(); |
| 132 | |
| 133 | if (childState.xTranslation < contentStart) { |
| 134 | if (firstUnderflowIndex == -1) { |
| 135 | firstUnderflowIndex = i; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | translationX -= child.getWidth(); |
| 140 | } |
| 141 | |
| 142 | if (firstUnderflowIndex != -1) { |
| 143 | for (int i = 0; i <= firstUnderflowIndex; i++) { |
| 144 | View child = getChildAt(i); |
| 145 | ViewState vs = getViewStateFromChild(child); |
| 146 | if (vs != null) { |
| 147 | vs.hidden = true; |
| 148 | } |
| 149 | } |
| 150 | } |
Evan Laird | 6303613 | 2018-02-02 16:30:41 -0500 | [diff] [blame] | 151 | |
| 152 | // Stole this from NotificationIconContainer. Not optimal but keeps the layout logic clean |
| 153 | if (isLayoutRtl()) { |
| 154 | for (int i = 0; i < childCount; i++) { |
| 155 | View child = getChildAt(i); |
| 156 | ViewState state = getViewStateFromChild(child); |
| 157 | state.xTranslation = width - state.xTranslation - child.getWidth(); |
| 158 | } |
| 159 | } |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private void applyIconStates() { |
| 163 | for (int i = 0; i < getChildCount(); i++) { |
| 164 | View child = getChildAt(i); |
| 165 | ViewState vs = getViewStateFromChild(child); |
| 166 | if (vs != null) { |
| 167 | vs.applyToView(child); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private void resetViewStates() { |
| 173 | for (int i = 0; i < getChildCount(); i++) { |
| 174 | View child = getChildAt(i); |
| 175 | ViewState vs = getViewStateFromChild(child); |
| 176 | if (vs == null) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | vs.initFrom(child); |
| 181 | vs.alpha = 1.0f; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 182 | if (child instanceof StatusIconDisplayable) { |
| 183 | vs.hidden = !((StatusIconDisplayable)child).isIconVisible(); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 184 | } else { |
| 185 | vs.hidden = false; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private static @Nullable ViewState getViewStateFromChild(View child) { |
| 191 | return (ViewState) child.getTag(R.id.status_bar_view_state_tag); |
| 192 | } |
| 193 | } |