Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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; |
| 18 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 19 | import static com.android.systemui.statusbar.StatusBarIconView.STATE_DOT; |
| 20 | import static com.android.systemui.statusbar.StatusBarIconView.STATE_HIDDEN; |
| 21 | import static com.android.systemui.statusbar.StatusBarIconView.STATE_ICON; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 22 | import static com.android.systemui.statusbar.policy.DarkIconDispatcher.getTint; |
Evan Laird | dbeefe3 | 2018-04-03 16:52:41 -0400 | [diff] [blame] | 23 | import static com.android.systemui.statusbar.policy.DarkIconDispatcher.isInArea; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 24 | |
| 25 | import android.content.Context; |
| 26 | import android.content.res.ColorStateList; |
| 27 | import android.graphics.Rect; |
| 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.util.AttributeSet; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 30 | import android.util.Log; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 31 | import android.view.ContextThemeWrapper; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 32 | import android.view.Gravity; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
| 34 | import android.view.View; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 35 | import android.view.ViewGroup; |
| 36 | import android.view.ViewGroup.LayoutParams; |
| 37 | import android.widget.FrameLayout; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 38 | import android.widget.ImageView; |
| 39 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 40 | import android.widget.LinearLayout; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 41 | import com.android.keyguard.AlphaOptimizedLinearLayout; |
| 42 | import com.android.settingslib.Utils; |
| 43 | import com.android.systemui.R; |
| 44 | import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.WifiIconState; |
| 45 | import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; |
| 46 | |
| 47 | /** |
| 48 | * Start small: StatusBarWifiView will be able to layout from a WifiIconState |
| 49 | */ |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 50 | public class StatusBarWifiView extends FrameLayout implements DarkReceiver, |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 51 | StatusIconDisplayable { |
| 52 | private static final String TAG = "StatusBarWifiView"; |
| 53 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 54 | /// Used to show etc dots |
| 55 | private StatusBarIconView mDotView; |
| 56 | /// Contains the main icon layout |
| 57 | private LinearLayout mWifiGroup; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 58 | private ImageView mWifiIcon; |
| 59 | private ImageView mIn; |
| 60 | private ImageView mOut; |
| 61 | private View mInoutContainer; |
| 62 | private View mSignalSpacer; |
| 63 | private View mAirplaneSpacer; |
| 64 | private WifiIconState mState; |
| 65 | private String mSlot; |
| 66 | private float mDarkIntensity = 0; |
| 67 | |
| 68 | private ContextThemeWrapper mDarkContext; |
| 69 | private ContextThemeWrapper mLightContext; |
| 70 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 71 | public static StatusBarWifiView fromContext(Context context, String slot) { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 72 | LayoutInflater inflater = LayoutInflater.from(context); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 73 | StatusBarWifiView v = (StatusBarWifiView) inflater.inflate(R.layout.status_bar_wifi_group, null); |
| 74 | v.setSlot(slot); |
| 75 | v.init(); |
| 76 | return v; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | public StatusBarWifiView(Context context) { |
| 80 | super(context); |
| 81 | } |
| 82 | |
| 83 | public StatusBarWifiView(Context context, AttributeSet attrs) { |
| 84 | super(context, attrs); |
| 85 | } |
| 86 | |
| 87 | public StatusBarWifiView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 88 | super(context, attrs, defStyleAttr); |
| 89 | } |
| 90 | |
| 91 | public StatusBarWifiView(Context context, AttributeSet attrs, int defStyleAttr, |
| 92 | int defStyleRes) { |
| 93 | super(context, attrs, defStyleAttr, defStyleRes); |
| 94 | } |
| 95 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 96 | public void setSlot(String slot) { |
| 97 | mSlot = slot; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void setStaticDrawableColor(int color) { |
| 102 | ColorStateList list = ColorStateList.valueOf(color); |
| 103 | mWifiIcon.setImageTintList(list); |
| 104 | mIn.setImageTintList(list); |
| 105 | mOut.setImageTintList(list); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 106 | mDotView.setDecorColor(color); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void setDecorColor(int color) { |
| 111 | mDotView.setDecorColor(color); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String getSlot() { |
| 116 | return mSlot; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean isIconVisible() { |
| 121 | return mState != null && mState.visible; |
| 122 | } |
| 123 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 124 | @Override |
| 125 | public void setVisibleState(int state) { |
| 126 | switch (state) { |
| 127 | case STATE_ICON: |
| 128 | mWifiGroup.setVisibility(View.VISIBLE); |
| 129 | mDotView.setVisibility(View.GONE); |
| 130 | break; |
| 131 | case STATE_DOT: |
| 132 | mWifiGroup.setVisibility(View.GONE); |
| 133 | mDotView.setVisibility(View.VISIBLE); |
| 134 | break; |
| 135 | case STATE_HIDDEN: |
| 136 | default: |
| 137 | setVisibility(View.GONE); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 144 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 145 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 146 | } |
| 147 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 148 | private void init() { |
| 149 | int dualToneLightTheme = Utils.getThemeAttr(mContext, R.attr.lightIconTheme); |
| 150 | int dualToneDarkTheme = Utils.getThemeAttr(mContext, R.attr.darkIconTheme); |
| 151 | mLightContext = new ContextThemeWrapper(mContext, dualToneLightTheme); |
| 152 | mDarkContext = new ContextThemeWrapper(mContext, dualToneDarkTheme); |
| 153 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 154 | mWifiGroup = findViewById(R.id.wifi_group); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 155 | mWifiIcon = findViewById(R.id.wifi_signal); |
| 156 | mIn = findViewById(R.id.wifi_in); |
| 157 | mOut = findViewById(R.id.wifi_out); |
| 158 | mSignalSpacer = findViewById(R.id.wifi_signal_spacer); |
| 159 | mAirplaneSpacer = findViewById(R.id.wifi_airplane_spacer); |
| 160 | mInoutContainer = findViewById(R.id.inout_container); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 161 | |
| 162 | initDotView(); |
| 163 | } |
| 164 | |
| 165 | private void initDotView() { |
| 166 | mDotView = new StatusBarIconView(mContext, mSlot, null); |
| 167 | mDotView.setVisibleState(STATE_DOT); |
| 168 | |
| 169 | int width = mContext.getResources().getDimensionPixelSize(R.dimen.status_bar_icon_size); |
| 170 | LayoutParams lp = new LayoutParams(width, width); |
| 171 | lp.gravity = Gravity.CENTER_VERTICAL | Gravity.START; |
| 172 | addView(mDotView, lp); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void applyWifiState(WifiIconState state) { |
| 176 | if (state == null) { |
| 177 | setVisibility(View.GONE); |
| 178 | mState = null; |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (mState == null) { |
| 183 | mState = state; |
| 184 | initViewState(); |
| 185 | } |
| 186 | |
| 187 | if (!mState.equals(state)) { |
| 188 | updateState(state); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private void updateState(WifiIconState state) { |
| 193 | if (mState.resId != state.resId && state.resId >= 0) { |
| 194 | NeutralGoodDrawable drawable = NeutralGoodDrawable |
| 195 | .create(mLightContext, mDarkContext, state.resId); |
| 196 | drawable.setDarkIntensity(mDarkIntensity); |
| 197 | mWifiIcon.setImageDrawable(drawable); |
| 198 | } |
| 199 | |
| 200 | mIn.setVisibility(state.activityIn ? View.VISIBLE : View.GONE); |
| 201 | mOut.setVisibility(state.activityOut ? View.VISIBLE : View.GONE); |
| 202 | mInoutContainer.setVisibility( |
| 203 | (state.activityIn || state.activityOut) ? View.VISIBLE : View.GONE); |
| 204 | mAirplaneSpacer.setVisibility(state.airplaneSpacerVisible ? View.VISIBLE : View.GONE); |
| 205 | mSignalSpacer.setVisibility(state.signalSpacerVisible ? View.VISIBLE : View.GONE); |
| 206 | if (mState.visible != state.visible) { |
| 207 | setVisibility(state.visible ? View.VISIBLE : View.GONE); |
| 208 | } |
| 209 | |
| 210 | mState = state; |
| 211 | } |
| 212 | |
| 213 | private void initViewState() { |
| 214 | if (mState.resId >= 0) { |
| 215 | NeutralGoodDrawable drawable = NeutralGoodDrawable.create( |
| 216 | mLightContext, mDarkContext, mState.resId); |
| 217 | drawable.setDarkIntensity(mDarkIntensity); |
| 218 | mWifiIcon.setImageDrawable(drawable); |
| 219 | } |
| 220 | |
| 221 | mIn.setVisibility(mState.activityIn ? View.VISIBLE : View.GONE); |
| 222 | mOut.setVisibility(mState.activityOut ? View.VISIBLE : View.GONE); |
| 223 | mInoutContainer.setVisibility( |
| 224 | (mState.activityIn || mState.activityOut) ? View.VISIBLE : View.GONE); |
| 225 | mAirplaneSpacer.setVisibility(mState.airplaneSpacerVisible ? View.VISIBLE : View.GONE); |
| 226 | mSignalSpacer.setVisibility(mState.signalSpacerVisible ? View.VISIBLE : View.GONE); |
| 227 | setVisibility(mState.visible ? View.VISIBLE : View.GONE); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Evan Laird | dbeefe3 | 2018-04-03 16:52:41 -0400 | [diff] [blame] | 232 | if (!isInArea(area, this)) { |
| 233 | return; |
| 234 | } |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 235 | mDarkIntensity = darkIntensity; |
| 236 | Drawable d = mWifiIcon.getDrawable(); |
| 237 | if (d instanceof NeutralGoodDrawable) { |
| 238 | ((NeutralGoodDrawable)d).setDarkIntensity(darkIntensity); |
| 239 | } |
| 240 | mIn.setImageTintList(ColorStateList.valueOf(getTint(area, this, tint))); |
| 241 | mOut.setImageTintList(ColorStateList.valueOf(getTint(area, this, tint))); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 242 | mDotView.setDecorColor(tint); |
| 243 | mDotView.setIconColor(tint, false); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | @Override |
| 248 | public String toString() { |
| 249 | return "StatusBarWifiView(slot=" + mSlot + " state=" + mState + ")"; |
| 250 | } |
| 251 | } |