blob: 0731a568ae7df2eb6f36ee20aa9fabfb92ea14c5 [file] [log] [blame]
John Spurlock3c875662013-08-31 15:07:25 -04001/*
2 * Copyright (C) 2013 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
17package com.android.systemui.statusbar.phone;
18
Evan Laird937d9fa2018-02-08 10:12:16 -080019import android.graphics.Rect;
Dan Sandlerd63f9322015-05-06 15:18:49 -040020import android.graphics.drawable.Icon;
John Spurlock3c875662013-08-31 15:07:25 -040021import android.os.Bundle;
22import android.os.UserHandle;
Evan Lairde1d13c92018-03-20 16:58:01 -040023import android.util.Log;
John Spurlock3c875662013-08-31 15:07:25 -040024import android.view.Gravity;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.LinearLayout;
28
29import com.android.internal.statusbar.StatusBarIcon;
30import com.android.systemui.DemoMode;
31import com.android.systemui.R;
Beverly1be62f42018-12-19 17:17:48 -050032import com.android.systemui.plugins.DarkIconDispatcher;
33import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
John Spurlock3c875662013-08-31 15:07:25 -040034import com.android.systemui.statusbar.StatusBarIconView;
Evan Lairde1d13c92018-03-20 16:58:01 -040035import com.android.systemui.statusbar.StatusBarMobileView;
36import com.android.systemui.statusbar.StatusBarWifiView;
Gus Prevasab336792018-11-14 13:52:20 -050037import com.android.systemui.statusbar.StatusIconDisplayable;
Evan Lairde1d13c92018-03-20 16:58:01 -040038import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.MobileIconState;
39import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.WifiIconState;
Gus Prevasab336792018-11-14 13:52:20 -050040
Evan Lairde1d13c92018-03-20 16:58:01 -040041import java.util.ArrayList;
John Spurlock3c875662013-08-31 15:07:25 -040042
Evan Laird937d9fa2018-02-08 10:12:16 -080043public class DemoStatusIcons extends StatusIconContainer implements DemoMode, DarkReceiver {
Evan Lairde1d13c92018-03-20 16:58:01 -040044 private static final String TAG = "DemoStatusIcons";
45
John Spurlock3c875662013-08-31 15:07:25 -040046 private final LinearLayout mStatusIcons;
Evan Lairde1d13c92018-03-20 16:58:01 -040047 private final ArrayList<StatusBarMobileView> mMobileViews = new ArrayList<>();
John Spurlock3c875662013-08-31 15:07:25 -040048 private final int mIconSize;
49
Evan Lairde1d13c92018-03-20 16:58:01 -040050 private StatusBarWifiView mWifiView;
John Spurlock3c875662013-08-31 15:07:25 -040051 private boolean mDemoMode;
Evan Laird937d9fa2018-02-08 10:12:16 -080052 private int mColor;
John Spurlock3c875662013-08-31 15:07:25 -040053
54 public DemoStatusIcons(LinearLayout statusIcons, int iconSize) {
55 super(statusIcons.getContext());
56 mStatusIcons = statusIcons;
57 mIconSize = iconSize;
Evan Laird937d9fa2018-02-08 10:12:16 -080058 mColor = DarkIconDispatcher.DEFAULT_ICON_TINT;
John Spurlock3c875662013-08-31 15:07:25 -040059
Evan Lairdfeec2ab2018-05-02 12:47:26 -040060 if (statusIcons instanceof StatusIconContainer) {
61 setShouldRestrictIcons(((StatusIconContainer) statusIcons).isRestrictingIcons());
62 } else {
63 setShouldRestrictIcons(false);
64 }
John Spurlock3c875662013-08-31 15:07:25 -040065 setLayoutParams(mStatusIcons.getLayoutParams());
Evan Lairdfeec2ab2018-05-02 12:47:26 -040066 setPadding(mStatusIcons.getPaddingLeft(),mStatusIcons.getPaddingTop(),
67 mStatusIcons.getPaddingRight(), mStatusIcons.getPaddingBottom());
John Spurlock3c875662013-08-31 15:07:25 -040068 setOrientation(mStatusIcons.getOrientation());
69 setGravity(Gravity.CENTER_VERTICAL); // no LL.getGravity()
70 ViewGroup p = (ViewGroup) mStatusIcons.getParent();
71 p.addView(this, p.indexOfChild(mStatusIcons));
72 }
73
Evan Laird937d9fa2018-02-08 10:12:16 -080074 public void remove() {
Evan Lairde1d13c92018-03-20 16:58:01 -040075 mMobileViews.clear();
Evan Laird937d9fa2018-02-08 10:12:16 -080076 ((ViewGroup) getParent()).removeView(this);
77 }
78
79 public void setColor(int color) {
80 mColor = color;
81 updateColors();
82 }
83
84 private void updateColors() {
85 for (int i = 0; i < getChildCount(); i++) {
Evan Lairde1d13c92018-03-20 16:58:01 -040086 StatusIconDisplayable child = (StatusIconDisplayable) getChildAt(i);
Evan Laird937d9fa2018-02-08 10:12:16 -080087 child.setStaticDrawableColor(mColor);
Evan Lairdfeec2ab2018-05-02 12:47:26 -040088 child.setDecorColor(mColor);
Evan Laird937d9fa2018-02-08 10:12:16 -080089 }
90 }
91
John Spurlock3c875662013-08-31 15:07:25 -040092 @Override
93 public void dispatchDemoCommand(String command, Bundle args) {
94 if (!mDemoMode && command.equals(COMMAND_ENTER)) {
95 mDemoMode = true;
96 mStatusIcons.setVisibility(View.GONE);
97 setVisibility(View.VISIBLE);
98 } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
99 mDemoMode = false;
100 mStatusIcons.setVisibility(View.VISIBLE);
101 setVisibility(View.GONE);
102 } else if (mDemoMode && command.equals(COMMAND_STATUS)) {
103 String volume = args.getString("volume");
104 if (volume != null) {
John Spurlockad494bc2014-07-19 15:56:19 -0400105 int iconId = volume.equals("vibrate") ? R.drawable.stat_sys_ringer_vibrate
John Spurlock3c875662013-08-31 15:07:25 -0400106 : 0;
107 updateSlot("volume", null, iconId);
108 }
John Spurlockad494bc2014-07-19 15:56:19 -0400109 String zen = args.getString("zen");
110 if (zen != null) {
Amin Shaikh833bb0f2019-03-28 14:03:48 -0400111 int iconId = zen.equals("dnd") ? R.drawable.stat_sys_dnd : 0;
John Spurlockad494bc2014-07-19 15:56:19 -0400112 updateSlot("zen", null, iconId);
113 }
John Spurlock3c875662013-08-31 15:07:25 -0400114 String bt = args.getString("bluetooth");
115 if (bt != null) {
Amin Shaikhad29e932019-03-08 11:26:12 -0500116 int iconId = bt.equals("connected")
117 ? R.drawable.stat_sys_data_bluetooth_connected : 0;
John Spurlock3c875662013-08-31 15:07:25 -0400118 updateSlot("bluetooth", null, iconId);
119 }
120 String location = args.getString("location");
121 if (location != null) {
Jason Monk359bb742017-04-13 10:40:40 -0400122 int iconId = location.equals("show") ? PhoneStatusBarPolicy.LOCATION_STATUS_ICON_ID
John Spurlock3c875662013-08-31 15:07:25 -0400123 : 0;
Jason Monk3e189872016-01-12 09:10:34 -0500124 updateSlot("location", null, iconId);
John Spurlock3c875662013-08-31 15:07:25 -0400125 }
126 String alarm = args.getString("alarm");
127 if (alarm != null) {
128 int iconId = alarm.equals("show") ? R.drawable.stat_sys_alarm
129 : 0;
130 updateSlot("alarm_clock", null, iconId);
131 }
John Spurlock3c875662013-08-31 15:07:25 -0400132 String tty = args.getString("tty");
133 if (tty != null) {
134 int iconId = tty.equals("show") ? R.drawable.stat_sys_tty_mode
135 : 0;
136 updateSlot("tty", null, iconId);
137 }
John Spurlock3c875662013-08-31 15:07:25 -0400138 String mute = args.getString("mute");
139 if (mute != null) {
140 int iconId = mute.equals("show") ? android.R.drawable.stat_notify_call_mute
141 : 0;
142 updateSlot("mute", null, iconId);
143 }
144 String speakerphone = args.getString("speakerphone");
145 if (speakerphone != null) {
146 int iconId = speakerphone.equals("show") ? android.R.drawable.stat_sys_speakerphone
147 : 0;
148 updateSlot("speakerphone", null, iconId);
149 }
John Spurlock96fe98d2014-08-27 09:36:02 -0400150 String cast = args.getString("cast");
151 if (cast != null) {
Jason Monk952d5d82014-10-27 15:28:22 -0400152 int iconId = cast.equals("show") ? R.drawable.stat_sys_cast : 0;
John Spurlock96fe98d2014-08-27 09:36:02 -0400153 updateSlot("cast", null, iconId);
154 }
Jason Monk952d5d82014-10-27 15:28:22 -0400155 String hotspot = args.getString("hotspot");
156 if (hotspot != null) {
157 int iconId = hotspot.equals("show") ? R.drawable.stat_sys_hotspot : 0;
158 updateSlot("hotspot", null, iconId);
159 }
John Spurlock3c875662013-08-31 15:07:25 -0400160 }
161 }
162
Evan Lairde1d13c92018-03-20 16:58:01 -0400163 /// Can only be used to update non-signal related slots
John Spurlock3c875662013-08-31 15:07:25 -0400164 private void updateSlot(String slot, String iconPkg, int iconId) {
165 if (!mDemoMode) return;
Jason Monk259e06f2015-06-18 14:07:26 -0400166 if (iconPkg == null) {
167 iconPkg = mContext.getPackageName();
168 }
John Spurlock3c875662013-08-31 15:07:25 -0400169 int removeIndex = -1;
170 for (int i = 0; i < getChildCount(); i++) {
Evan Lairde1d13c92018-03-20 16:58:01 -0400171 View child = getChildAt(i);
172 if (!(child instanceof StatusBarIconView)) {
173 continue;
174 }
175 StatusBarIconView v = (StatusBarIconView) child;
John Spurlock3c875662013-08-31 15:07:25 -0400176 if (slot.equals(v.getTag())) {
177 if (iconId == 0) {
178 removeIndex = i;
179 break;
180 } else {
181 StatusBarIcon icon = v.getStatusBarIcon();
Evan Laird937d9fa2018-02-08 10:12:16 -0800182 icon.visible = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -0400183 icon.icon = Icon.createWithResource(icon.icon.getResPackage(), iconId);
John Spurlock3c875662013-08-31 15:07:25 -0400184 v.set(icon);
185 v.updateDrawable();
186 return;
187 }
188 }
189 }
190 if (iconId == 0) {
191 if (removeIndex != -1) {
192 removeViewAt(removeIndex);
John Spurlock3c875662013-08-31 15:07:25 -0400193 }
Jason Monk259e06f2015-06-18 14:07:26 -0400194 return;
John Spurlock3c875662013-08-31 15:07:25 -0400195 }
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700196 StatusBarIcon icon = new StatusBarIcon(iconPkg, UserHandle.SYSTEM, iconId, 0, 0, "Demo");
Evan Laird937d9fa2018-02-08 10:12:16 -0800197 icon.visible = true;
Evan Lairdfeec2ab2018-05-02 12:47:26 -0400198 StatusBarIconView v = new StatusBarIconView(getContext(), slot, null, false);
John Spurlock3c875662013-08-31 15:07:25 -0400199 v.setTag(slot);
200 v.set(icon);
Evan Laird937d9fa2018-02-08 10:12:16 -0800201 v.setStaticDrawableColor(mColor);
Evan Lairdfeec2ab2018-05-02 12:47:26 -0400202 v.setDecorColor(mColor);
Evan Lairdf303d952018-06-21 14:07:10 -0400203 addView(v, 0, createLayoutParams());
John Spurlock3c875662013-08-31 15:07:25 -0400204 }
Evan Laird937d9fa2018-02-08 10:12:16 -0800205
Evan Lairde1d13c92018-03-20 16:58:01 -0400206 public void addDemoWifiView(WifiIconState state) {
207 Log.d(TAG, "addDemoWifiView: ");
Evan Laird20b87bf2018-04-12 09:54:11 -0400208 StatusBarWifiView view = StatusBarWifiView.fromContext(mContext, state.slot);
Evan Lairde1d13c92018-03-20 16:58:01 -0400209
210 int viewIndex = getChildCount();
211 // If we have mobile views, put wifi before them
212 for (int i = 0; i < getChildCount(); i++) {
213 View child = getChildAt(i);
214 if (child instanceof StatusBarMobileView) {
215 viewIndex = i;
216 break;
217 }
218 }
219
220 mWifiView = view;
221 mWifiView.applyWifiState(state);
222 mWifiView.setStaticDrawableColor(mColor);
Evan Lairdf303d952018-06-21 14:07:10 -0400223 addView(view, viewIndex, createLayoutParams());
Evan Lairde1d13c92018-03-20 16:58:01 -0400224 }
225
226 public void updateWifiState(WifiIconState state) {
227 Log.d(TAG, "updateWifiState: ");
228 if (mWifiView == null) {
229 addDemoWifiView(state);
230 } else {
231 mWifiView.applyWifiState(state);
232 }
233 }
234
235 public void addMobileView(MobileIconState state) {
236 Log.d(TAG, "addMobileView: ");
Evan Laird97ae20c2018-05-10 13:13:27 -0400237 StatusBarMobileView view = StatusBarMobileView.fromContext(mContext, state.slot);
Evan Lairde1d13c92018-03-20 16:58:01 -0400238
Evan Lairde1d13c92018-03-20 16:58:01 -0400239 view.applyMobileState(state);
240 view.setStaticDrawableColor(mColor);
241
242 // mobile always goes at the end
243 mMobileViews.add(view);
Evan Lairdf303d952018-06-21 14:07:10 -0400244 addView(view, getChildCount(), createLayoutParams());
Evan Lairde1d13c92018-03-20 16:58:01 -0400245 }
246
247 public void updateMobileState(MobileIconState state) {
248 Log.d(TAG, "updateMobileState: ");
249 // If the view for this subId exists already, use it
250 for (int i = 0; i < mMobileViews.size(); i++) {
251 StatusBarMobileView view = mMobileViews.get(i);
252 if (view.getState().subId == state.subId) {
253 view.applyMobileState(state);
254 return;
255 }
256 }
257
258 // Else we have to add it
259 addMobileView(state);
260 }
261
262 public void onRemoveIcon(StatusIconDisplayable view) {
263 if (view.getSlot().equals("wifi")) {
264 removeView(mWifiView);
265 mWifiView = null;
266 } else {
267 StatusBarMobileView mobileView = matchingMobileView(view);
268 if (mobileView != null) {
269 removeView(mobileView);
270 mMobileViews.remove(mobileView);
271 }
272 }
273 }
274
275 private StatusBarMobileView matchingMobileView(StatusIconDisplayable otherView) {
276 if (!(otherView instanceof StatusBarMobileView)) {
277 return null;
278 }
279
280 StatusBarMobileView v = (StatusBarMobileView) otherView;
281 for (StatusBarMobileView view : mMobileViews) {
282 if (view.getState().subId == v.getState().subId) {
283 return view;
284 }
285 }
286
287 return null;
288 }
289
Evan Lairdf303d952018-06-21 14:07:10 -0400290 private LayoutParams createLayoutParams() {
291 return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, mIconSize);
292 }
293
Evan Laird937d9fa2018-02-08 10:12:16 -0800294 @Override
295 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
296 setColor(DarkIconDispatcher.getTint(area, mStatusIcons, tint));
Evan Lairde1d13c92018-03-20 16:58:01 -0400297
298 if (mWifiView != null) {
299 mWifiView.onDarkChanged(area, darkIntensity, tint);
300 }
301 for (StatusBarMobileView view : mMobileViews) {
302 view.onDarkChanged(area, darkIntensity, tint);
303 }
Evan Laird937d9fa2018-02-08 10:12:16 -0800304 }
Dan Sandlerd63f9322015-05-06 15:18:49 -0400305}