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 | |
| 19 | import static com.android.systemui.statusbar.policy.DarkIconDispatcher.getTint; |
Evan Laird | dbeefe3 | 2018-04-03 16:52:41 -0400 | [diff] [blame] | 20 | import static com.android.systemui.statusbar.policy.DarkIconDispatcher.isInArea; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 21 | |
| 22 | import android.content.Context; |
| 23 | import android.content.res.ColorStateList; |
| 24 | import android.graphics.Rect; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.view.ContextThemeWrapper; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.widget.ImageView; |
| 31 | |
| 32 | import com.android.keyguard.AlphaOptimizedLinearLayout; |
| 33 | import com.android.settingslib.Utils; |
| 34 | import com.android.systemui.R; |
| 35 | import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.WifiIconState; |
| 36 | import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; |
| 37 | |
| 38 | /** |
| 39 | * Start small: StatusBarWifiView will be able to layout from a WifiIconState |
| 40 | */ |
| 41 | public class StatusBarWifiView extends AlphaOptimizedLinearLayout implements DarkReceiver, |
| 42 | StatusIconDisplayable { |
| 43 | private static final String TAG = "StatusBarWifiView"; |
| 44 | |
| 45 | private ImageView mWifiIcon; |
| 46 | private ImageView mIn; |
| 47 | private ImageView mOut; |
| 48 | private View mInoutContainer; |
| 49 | private View mSignalSpacer; |
| 50 | private View mAirplaneSpacer; |
| 51 | private WifiIconState mState; |
| 52 | private String mSlot; |
| 53 | private float mDarkIntensity = 0; |
| 54 | |
| 55 | private ContextThemeWrapper mDarkContext; |
| 56 | private ContextThemeWrapper mLightContext; |
| 57 | |
| 58 | public static StatusBarWifiView fromContext(Context context) { |
| 59 | LayoutInflater inflater = LayoutInflater.from(context); |
| 60 | return (StatusBarWifiView) inflater.inflate(R.layout.status_bar_wifi_group, null); |
| 61 | } |
| 62 | |
| 63 | public StatusBarWifiView(Context context) { |
| 64 | super(context); |
| 65 | } |
| 66 | |
| 67 | public StatusBarWifiView(Context context, AttributeSet attrs) { |
| 68 | super(context, attrs); |
| 69 | } |
| 70 | |
| 71 | public StatusBarWifiView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 72 | super(context, attrs, defStyleAttr); |
| 73 | } |
| 74 | |
| 75 | public StatusBarWifiView(Context context, AttributeSet attrs, int defStyleAttr, |
| 76 | int defStyleRes) { |
| 77 | super(context, attrs, defStyleAttr, defStyleRes); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void onFinishInflate() { |
| 82 | super.onFinishInflate(); |
| 83 | init(); |
| 84 | } |
| 85 | |
| 86 | public void setSlot(String slot) { |
| 87 | mSlot = slot; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void setStaticDrawableColor(int color) { |
| 92 | ColorStateList list = ColorStateList.valueOf(color); |
| 93 | mWifiIcon.setImageTintList(list); |
| 94 | mIn.setImageTintList(list); |
| 95 | mOut.setImageTintList(list); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String getSlot() { |
| 100 | return mSlot; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean isIconVisible() { |
| 105 | return mState != null && mState.visible; |
| 106 | } |
| 107 | |
| 108 | private void init() { |
| 109 | int dualToneLightTheme = Utils.getThemeAttr(mContext, R.attr.lightIconTheme); |
| 110 | int dualToneDarkTheme = Utils.getThemeAttr(mContext, R.attr.darkIconTheme); |
| 111 | mLightContext = new ContextThemeWrapper(mContext, dualToneLightTheme); |
| 112 | mDarkContext = new ContextThemeWrapper(mContext, dualToneDarkTheme); |
| 113 | |
| 114 | mWifiIcon = findViewById(R.id.wifi_signal); |
| 115 | mIn = findViewById(R.id.wifi_in); |
| 116 | mOut = findViewById(R.id.wifi_out); |
| 117 | mSignalSpacer = findViewById(R.id.wifi_signal_spacer); |
| 118 | mAirplaneSpacer = findViewById(R.id.wifi_airplane_spacer); |
| 119 | mInoutContainer = findViewById(R.id.inout_container); |
| 120 | } |
| 121 | |
| 122 | public void applyWifiState(WifiIconState state) { |
| 123 | if (state == null) { |
| 124 | setVisibility(View.GONE); |
| 125 | mState = null; |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (mState == null) { |
| 130 | mState = state; |
| 131 | initViewState(); |
| 132 | } |
| 133 | |
| 134 | if (!mState.equals(state)) { |
| 135 | updateState(state); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private void updateState(WifiIconState state) { |
| 140 | if (mState.resId != state.resId && state.resId >= 0) { |
| 141 | NeutralGoodDrawable drawable = NeutralGoodDrawable |
| 142 | .create(mLightContext, mDarkContext, state.resId); |
| 143 | drawable.setDarkIntensity(mDarkIntensity); |
| 144 | mWifiIcon.setImageDrawable(drawable); |
| 145 | } |
| 146 | |
| 147 | mIn.setVisibility(state.activityIn ? View.VISIBLE : View.GONE); |
| 148 | mOut.setVisibility(state.activityOut ? View.VISIBLE : View.GONE); |
| 149 | mInoutContainer.setVisibility( |
| 150 | (state.activityIn || state.activityOut) ? View.VISIBLE : View.GONE); |
| 151 | mAirplaneSpacer.setVisibility(state.airplaneSpacerVisible ? View.VISIBLE : View.GONE); |
| 152 | mSignalSpacer.setVisibility(state.signalSpacerVisible ? View.VISIBLE : View.GONE); |
| 153 | if (mState.visible != state.visible) { |
| 154 | setVisibility(state.visible ? View.VISIBLE : View.GONE); |
| 155 | } |
| 156 | |
| 157 | mState = state; |
| 158 | } |
| 159 | |
| 160 | private void initViewState() { |
| 161 | if (mState.resId >= 0) { |
| 162 | NeutralGoodDrawable drawable = NeutralGoodDrawable.create( |
| 163 | mLightContext, mDarkContext, mState.resId); |
| 164 | drawable.setDarkIntensity(mDarkIntensity); |
| 165 | mWifiIcon.setImageDrawable(drawable); |
| 166 | } |
| 167 | |
| 168 | mIn.setVisibility(mState.activityIn ? View.VISIBLE : View.GONE); |
| 169 | mOut.setVisibility(mState.activityOut ? View.VISIBLE : View.GONE); |
| 170 | mInoutContainer.setVisibility( |
| 171 | (mState.activityIn || mState.activityOut) ? View.VISIBLE : View.GONE); |
| 172 | mAirplaneSpacer.setVisibility(mState.airplaneSpacerVisible ? View.VISIBLE : View.GONE); |
| 173 | mSignalSpacer.setVisibility(mState.signalSpacerVisible ? View.VISIBLE : View.GONE); |
| 174 | setVisibility(mState.visible ? View.VISIBLE : View.GONE); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Evan Laird | dbeefe3 | 2018-04-03 16:52:41 -0400 | [diff] [blame] | 179 | if (!isInArea(area, this)) { |
| 180 | return; |
| 181 | } |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 182 | mDarkIntensity = darkIntensity; |
| 183 | Drawable d = mWifiIcon.getDrawable(); |
| 184 | if (d instanceof NeutralGoodDrawable) { |
| 185 | ((NeutralGoodDrawable)d).setDarkIntensity(darkIntensity); |
| 186 | } |
| 187 | mIn.setImageTintList(ColorStateList.valueOf(getTint(area, this, tint))); |
| 188 | mOut.setImageTintList(ColorStateList.valueOf(getTint(area, this, tint))); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | @Override |
| 193 | public String toString() { |
| 194 | return "StatusBarWifiView(slot=" + mSlot + " state=" + mState + ")"; |
| 195 | } |
| 196 | } |