blob: 75ab5dfd2977db51eaa2f967b3be8c8420d82707 [file] [log] [blame]
Jason Monke5b770e2017-03-03 21:49:29 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.qs;
16
Charles Hece2a7c02017-10-11 20:25:20 +010017import static android.app.StatusBarManager.DISABLE2_QUICK_SETTINGS;
Evan Laird4bf21df2018-10-22 14:24:32 -040018import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
Charles Hece2a7c02017-10-11 20:25:20 +010019
Jason Monkea54e8a2018-12-20 10:01:48 -050020import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT;
21
Rohan Shahd3cf7562018-02-23 11:12:28 -080022import android.animation.Animator;
23import android.animation.AnimatorListenerAdapter;
Amin Shaikh0e003312018-03-08 11:39:01 -050024import android.annotation.ColorInt;
Lucas Dupin1f7374a2018-02-26 18:08:33 -080025import android.app.ActivityManager;
Rohan Shahd3cf7562018-02-23 11:12:28 -080026import android.app.AlarmManager;
Fabian Kozynski12638242018-10-12 15:33:41 -040027import android.app.Dialog;
Amin Shaikh761c5662018-03-27 16:55:43 -040028import android.content.BroadcastReceiver;
Jason Monke5b770e2017-03-03 21:49:29 -050029import android.content.Context;
Evan Laird4ea2a492018-01-22 11:29:12 -050030import android.content.Intent;
Amin Shaikh761c5662018-03-27 16:55:43 -040031import android.content.IntentFilter;
Jason Monke5b770e2017-03-03 21:49:29 -050032import android.content.res.Configuration;
Rohan Shahcc3d1d82018-03-30 21:24:17 +000033import android.content.res.Resources;
Evan Laird4bf21df2018-10-22 14:24:32 -040034import android.database.ContentObserver;
Jason Monke5b770e2017-03-03 21:49:29 -050035import android.graphics.Color;
36import android.graphics.Rect;
Amin Shaikh761c5662018-03-27 16:55:43 -040037import android.media.AudioManager;
Evan Laird4bf21df2018-10-22 14:24:32 -040038import android.net.Uri;
Rohan Shahd3cf7562018-02-23 11:12:28 -080039import android.os.Handler;
Fabian Kozynski12638242018-10-12 15:33:41 -040040import android.os.Looper;
Evan Laird4ea2a492018-01-22 11:29:12 -050041import android.provider.AlarmClock;
Evan Laird4bf21df2018-10-22 14:24:32 -040042import android.provider.Settings;
Beverlyb9826dd2018-04-12 15:02:45 -040043import android.service.notification.ZenModeConfig;
Rohan Shahd3cf7562018-02-23 11:12:28 -080044import android.text.format.DateUtils;
Jason Monke5b770e2017-03-03 21:49:29 -050045import android.util.AttributeSet;
Rohan Shaha160ab12018-03-09 15:47:36 -080046import android.util.Log;
Adrian Roos13836052018-03-15 21:06:37 +010047import android.util.Pair;
Fabian Kozynski8d06c712018-11-07 10:33:02 -050048import android.view.DisplayCutout;
Jason Monk824ffff2017-04-11 15:49:06 -040049import android.view.View;
Adrian Roos13836052018-03-15 21:06:37 +010050import android.view.WindowInsets;
Fabian Kozynski12638242018-10-12 15:33:41 -040051import android.view.WindowManager;
Beverlyff2df9b2018-10-10 16:54:10 -040052import android.widget.FrameLayout;
Amin Shaikh761c5662018-03-27 16:55:43 -040053import android.widget.ImageView;
Fabian Kozynski8d06c712018-11-07 10:33:02 -050054import android.widget.LinearLayout;
Jason Monke5b770e2017-03-03 21:49:29 -050055import android.widget.RelativeLayout;
Fabian Kozynski8d06c712018-11-07 10:33:02 -050056import android.widget.Space;
Rohan Shahd3cf7562018-02-23 11:12:28 -080057import android.widget.TextView;
Jason Monke5b770e2017-03-03 21:49:29 -050058
Beverlyff2df9b2018-10-10 16:54:10 -040059import androidx.annotation.VisibleForTesting;
60
Jason Monke5b770e2017-03-03 21:49:29 -050061import com.android.settingslib.Utils;
62import com.android.systemui.BatteryMeterView;
Rohan Shahd3cf7562018-02-23 11:12:28 -080063import com.android.systemui.Prefs;
Jason Monke5b770e2017-03-03 21:49:29 -050064import com.android.systemui.R;
65import com.android.systemui.plugins.ActivityStarter;
Beverly1be62f42018-12-19 17:17:48 -050066import com.android.systemui.plugins.DarkIconDispatcher;
67import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
Fabian Kozynski12638242018-10-12 15:33:41 -040068import com.android.systemui.privacy.OngoingPrivacyChip;
69import com.android.systemui.privacy.OngoingPrivacyDialog;
Fabian Kozynski8d06c712018-11-07 10:33:02 -050070import com.android.systemui.privacy.PrivacyItem;
71import com.android.systemui.privacy.PrivacyItemController;
Jason Monke5b770e2017-03-03 21:49:29 -050072import com.android.systemui.qs.QSDetail.Callback;
Adrian Roos13836052018-03-15 21:06:37 +010073import com.android.systemui.statusbar.phone.PhoneStatusBarView;
Evan Laird95896952018-01-22 19:30:05 -050074import com.android.systemui.statusbar.phone.StatusBarIconController;
75import com.android.systemui.statusbar.phone.StatusBarIconController.TintedIconManager;
Evan Laird20b87bf2018-04-12 09:54:11 -040076import com.android.systemui.statusbar.phone.StatusIconContainer;
Fabian Kozynski12638242018-10-12 15:33:41 -040077import com.android.systemui.statusbar.phone.SystemUIDialog;
Evan Laird4bf21df2018-10-22 14:24:32 -040078import com.android.systemui.statusbar.policy.BatteryController;
Beverlyff2df9b2018-10-10 16:54:10 -040079import com.android.systemui.statusbar.policy.Clock;
Rohan Shahcc3d1d82018-03-30 21:24:17 +000080import com.android.systemui.statusbar.policy.DateView;
Rohan Shahd3cf7562018-02-23 11:12:28 -080081import com.android.systemui.statusbar.policy.NextAlarmController;
Beverlyb9826dd2018-04-12 15:02:45 -040082import com.android.systemui.statusbar.policy.ZenModeController;
Jason Monke5b770e2017-03-03 21:49:29 -050083
Fabian Kozynski8d06c712018-11-07 10:33:02 -050084import java.util.List;
Lucas Dupin1f7374a2018-02-26 18:08:33 -080085import java.util.Locale;
Beverlyb9826dd2018-04-12 15:02:45 -040086import java.util.Objects;
Lucas Dupin1f7374a2018-02-26 18:08:33 -080087
Jason Monkea54e8a2018-12-20 10:01:48 -050088import javax.inject.Inject;
89import javax.inject.Named;
90
Rohan Shahd3cf7562018-02-23 11:12:28 -080091/**
92 * View that contains the top-most bits of the screen (primarily the status bar with date, time, and
93 * battery) and also contains the {@link QuickQSPanel} along with some of the panel's inner
94 * contents.
95 */
Amin Shaikhf09450b2018-04-06 17:32:45 -040096public class QuickStatusBarHeader extends RelativeLayout implements
Beverlyb9826dd2018-04-12 15:02:45 -040097 View.OnClickListener, NextAlarmController.NextAlarmChangeCallback,
98 ZenModeController.Callback {
Rohan Shaha160ab12018-03-09 15:47:36 -080099 private static final String TAG = "QuickStatusBarHeader";
100 private static final boolean DEBUG = false;
Jason Monke5b770e2017-03-03 21:49:29 -0500101
Rohan Shahd3cf7562018-02-23 11:12:28 -0800102 /** Delay for auto fading out the long press tooltip after it's fully visible (in ms). */
103 private static final long AUTO_FADE_OUT_DELAY_MS = DateUtils.SECOND_IN_MILLIS * 6;
104 private static final int FADE_ANIMATION_DURATION_MS = 300;
105 private static final int TOOLTIP_NOT_YET_SHOWN_COUNT = 0;
Rohan Shahb6a915c2018-03-01 11:57:00 -0800106 public static final int MAX_TOOLTIP_SHOWN_COUNT = 2;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800107
108 private final Handler mHandler = new Handler();
Jason Monkea54e8a2018-12-20 10:01:48 -0500109 private final BatteryController mBatteryController;
110 private final NextAlarmController mAlarmController;
111 private final ZenModeController mZenController;
112 private final StatusBarIconController mStatusBarIconController;
113 private final ActivityStarter mActivityStarter;
Jason Monke5b770e2017-03-03 21:49:29 -0500114
115 private QSPanel mQsPanel;
116
117 private boolean mExpanded;
118 private boolean mListening;
Charles Hece2a7c02017-10-11 20:25:20 +0100119 private boolean mQsDisabled;
Jason Monke5b770e2017-03-03 21:49:29 -0500120
121 protected QuickQSPanel mHeaderQsPanel;
122 protected QSTileHost mHost;
Evan Laird95896952018-01-22 19:30:05 -0500123 private TintedIconManager mIconManager;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800124 private TouchAnimator mStatusIconsAlphaAnimator;
125 private TouchAnimator mHeaderTextContainerAlphaAnimator;
Evan Laird95896952018-01-22 19:30:05 -0500126
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000127 private View mSystemIconsView;
Evan Laird95896952018-01-22 19:30:05 -0500128 private View mQuickQsStatusIcons;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800129 private View mHeaderTextContainerView;
Amin Shaikh761c5662018-03-27 16:55:43 -0400130 /** View containing the next alarm and ringer mode info. */
131 private View mStatusContainer;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800132 /** Tooltip for educating users that they can long press on icons to see more details. */
133 private View mLongPressTooltipView;
Amin Shaikh761c5662018-03-27 16:55:43 -0400134
135 private int mRingerMode = AudioManager.RINGER_MODE_NORMAL;
136 private AlarmManager.AlarmClockInfo mNextAlarm;
137
138 private ImageView mNextAlarmIcon;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800139 /** {@link TextView} containing the actual text indicating when the next alarm will go off. */
140 private TextView mNextAlarmTextView;
Amin Shaikh761c5662018-03-27 16:55:43 -0400141 private View mStatusSeparator;
142 private ImageView mRingerModeIcon;
143 private TextView mRingerModeTextView;
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000144 private Clock mClockView;
145 private DateView mDateView;
Fabian Kozynski12638242018-10-12 15:33:41 -0400146 private OngoingPrivacyChip mPrivacyChip;
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500147 private Space mSpace;
Evan Laird4bf21df2018-10-22 14:24:32 -0400148 private BatteryMeterView mBatteryRemainingIcon;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800149
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500150 private PrivacyItemController mPrivacyItemController;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800151 /** Counts how many times the long press tooltip has been shown to the user. */
152 private int mShownCount;
153
Amin Shaikh761c5662018-03-27 16:55:43 -0400154 private final BroadcastReceiver mRingerReceiver = new BroadcastReceiver() {
155 @Override
156 public void onReceive(Context context, Intent intent) {
157 mRingerMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1);
158 updateStatusText();
159 }
160 };
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500161 private boolean mHasTopCutout = false;
Amin Shaikh761c5662018-03-27 16:55:43 -0400162
Evan Laird4bf21df2018-10-22 14:24:32 -0400163 private final PercentSettingObserver mPercentSettingObserver =
164 new PercentSettingObserver(new Handler(mContext.getMainLooper()));
165
Rohan Shahd3cf7562018-02-23 11:12:28 -0800166 /**
167 * Runnable for automatically fading out the long press tooltip (as if it were animating away).
168 */
169 private final Runnable mAutoFadeOutTooltipRunnable = () -> hideLongPressTooltip(false);
Evan Laird4ea2a492018-01-22 11:29:12 -0500170
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500171 private PrivacyItemController.Callback mPICCallback = new PrivacyItemController.Callback() {
172 @Override
173 public void privacyChanged(List<PrivacyItem> privacyItems) {
174 mPrivacyChip.setPrivacyList(privacyItems);
175 setChipVisibility(!privacyItems.isEmpty());
176 }
177 };
178
Jason Monkea54e8a2018-12-20 10:01:48 -0500179 @Inject
180 public QuickStatusBarHeader(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs,
181 NextAlarmController nextAlarmController, ZenModeController zenModeController,
182 BatteryController batteryController, StatusBarIconController statusBarIconController,
183 ActivityStarter activityStarter) {
Jason Monke5b770e2017-03-03 21:49:29 -0500184 super(context, attrs);
Jason Monkea54e8a2018-12-20 10:01:48 -0500185 mAlarmController = nextAlarmController;
186 mZenController = zenModeController;
187 mBatteryController = batteryController;
188 mStatusBarIconController = statusBarIconController;
189 mActivityStarter = activityStarter;
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500190 mPrivacyItemController = new PrivacyItemController(context, mPICCallback);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800191 mShownCount = getStoredShownCount();
Jason Monke5b770e2017-03-03 21:49:29 -0500192 }
193
194 @Override
195 protected void onFinishInflate() {
196 super.onFinishInflate();
Jason Monke5b770e2017-03-03 21:49:29 -0500197
198 mHeaderQsPanel = findViewById(R.id.quick_qs_panel);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000199 mSystemIconsView = findViewById(R.id.quick_status_bar_system_icons);
Evan Laird95896952018-01-22 19:30:05 -0500200 mQuickQsStatusIcons = findViewById(R.id.quick_qs_status_icons);
Evan Laird20b87bf2018-04-12 09:54:11 -0400201 StatusIconContainer iconContainer = findViewById(R.id.statusIcons);
202 iconContainer.setShouldRestrictIcons(false);
203 mIconManager = new TintedIconManager(iconContainer);
Jason Monke5b770e2017-03-03 21:49:29 -0500204
Rohan Shahd3cf7562018-02-23 11:12:28 -0800205 // Views corresponding to the header info section (e.g. tooltip and next alarm).
206 mHeaderTextContainerView = findViewById(R.id.header_text_container);
207 mLongPressTooltipView = findViewById(R.id.long_press_tooltip);
Amin Shaikh761c5662018-03-27 16:55:43 -0400208 mStatusContainer = findViewById(R.id.status_container);
209 mStatusSeparator = findViewById(R.id.status_separator);
210 mNextAlarmIcon = findViewById(R.id.next_alarm_icon);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800211 mNextAlarmTextView = findViewById(R.id.next_alarm_text);
Amin Shaikh761c5662018-03-27 16:55:43 -0400212 mRingerModeIcon = findViewById(R.id.ringer_mode_icon);
213 mRingerModeTextView = findViewById(R.id.ringer_mode_text);
Jason Monke5b770e2017-03-03 21:49:29 -0500214
215 updateResources();
216
Jason Monk824ffff2017-04-11 15:49:06 -0400217 Rect tintArea = new Rect(0, 0, 0, 0);
Jason Changb4e879d2018-04-11 11:17:58 +0800218 int colorForeground = Utils.getColorAttrDefaultColor(getContext(),
219 android.R.attr.colorForeground);
Amin Shaikh0e003312018-03-08 11:39:01 -0500220 float intensity = getColorIntensity(colorForeground);
Evan Laird95896952018-01-22 19:30:05 -0500221 int fillColor = fillColorForIntensity(intensity, getContext());
222
223 // Set light text on the header icons because they will always be on a black background
Evan Laird39254d42018-01-18 16:05:30 -0500224 applyDarkness(R.id.clock, tintArea, 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Evan Laird95896952018-01-22 19:30:05 -0500225
226 // Set the correct tint for the status icons so they contrast
227 mIconManager.setTint(fillColor);
Jason Monke5b770e2017-03-03 21:49:29 -0500228
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000229 mClockView = findViewById(R.id.clock);
Amin Shaikh39bdc502018-05-15 23:43:34 -0400230 mClockView.setOnClickListener(this);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000231 mDateView = findViewById(R.id.date);
Fabian Kozynski12638242018-10-12 15:33:41 -0400232 mPrivacyChip = findViewById(R.id.privacy_chip);
233 mPrivacyChip.setOnClickListener(this);
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500234 mSpace = findViewById(R.id.space);
Evan Laird4bf21df2018-10-22 14:24:32 -0400235
236 // Tint for the battery icons are handled in setupHost()
237 mBatteryRemainingIcon = findViewById(R.id.batteryRemainingIcon);
Evan Laird698839b2018-12-05 15:49:12 -0500238 // Don't need to worry about tuner settings for this icon
239 mBatteryRemainingIcon.setIgnoreTunerUpdates(true);
Evan Laird4bf21df2018-10-22 14:24:32 -0400240 updateShowPercent();
Jason Monke5b770e2017-03-03 21:49:29 -0500241 }
242
Amin Shaikh761c5662018-03-27 16:55:43 -0400243 private void updateStatusText() {
Beverlyb9826dd2018-04-12 15:02:45 -0400244 boolean changed = updateRingerStatus() || updateAlarmStatus();
245
246 if (changed) {
247 boolean alarmVisible = mNextAlarmTextView.getVisibility() == View.VISIBLE;
248 boolean ringerVisible = mRingerModeTextView.getVisibility() == View.VISIBLE;
249 mStatusSeparator.setVisibility(alarmVisible && ringerVisible ? View.VISIBLE
250 : View.GONE);
251 updateTooltipShow();
252 }
253 }
254
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500255 private void setChipVisibility(boolean chipVisible) {
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500256 if (chipVisible) {
257 mPrivacyChip.setVisibility(View.VISIBLE);
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500258 } else {
259 mPrivacyChip.setVisibility(View.GONE);
260 }
261 }
262
Beverlyb9826dd2018-04-12 15:02:45 -0400263 private boolean updateRingerStatus() {
264 boolean isOriginalVisible = mRingerModeTextView.getVisibility() == View.VISIBLE;
265 CharSequence originalRingerText = mRingerModeTextView.getText();
266
Amin Shaikh761c5662018-03-27 16:55:43 -0400267 boolean ringerVisible = false;
Beverlyb9826dd2018-04-12 15:02:45 -0400268 if (!ZenModeConfig.isZenOverridingRinger(mZenController.getZen(),
Beverlyff2df9b2018-10-10 16:54:10 -0400269 mZenController.getConsolidatedPolicy())) {
Beverlyb9826dd2018-04-12 15:02:45 -0400270 if (mRingerMode == AudioManager.RINGER_MODE_VIBRATE) {
271 mRingerModeIcon.setImageResource(R.drawable.stat_sys_ringer_vibrate);
272 mRingerModeTextView.setText(R.string.qs_status_phone_vibrate);
273 ringerVisible = true;
274 } else if (mRingerMode == AudioManager.RINGER_MODE_SILENT) {
275 mRingerModeIcon.setImageResource(R.drawable.stat_sys_ringer_silent);
276 mRingerModeTextView.setText(R.string.qs_status_phone_muted);
277 ringerVisible = true;
278 }
Amin Shaikh761c5662018-03-27 16:55:43 -0400279 }
280 mRingerModeIcon.setVisibility(ringerVisible ? View.VISIBLE : View.GONE);
281 mRingerModeTextView.setVisibility(ringerVisible ? View.VISIBLE : View.GONE);
282
Beverlyb9826dd2018-04-12 15:02:45 -0400283 return isOriginalVisible != ringerVisible ||
284 !Objects.equals(originalRingerText, mRingerModeTextView.getText());
285 }
286
287 private boolean updateAlarmStatus() {
288 boolean isOriginalVisible = mNextAlarmTextView.getVisibility() == View.VISIBLE;
289 CharSequence originalAlarmText = mNextAlarmTextView.getText();
290
Amin Shaikh761c5662018-03-27 16:55:43 -0400291 boolean alarmVisible = false;
292 if (mNextAlarm != null) {
293 alarmVisible = true;
294 mNextAlarmTextView.setText(formatNextAlarm(mNextAlarm));
295 }
296 mNextAlarmIcon.setVisibility(alarmVisible ? View.VISIBLE : View.GONE);
297 mNextAlarmTextView.setVisibility(alarmVisible ? View.VISIBLE : View.GONE);
Amin Shaikh761c5662018-03-27 16:55:43 -0400298
Beverlyb9826dd2018-04-12 15:02:45 -0400299 return isOriginalVisible != alarmVisible ||
300 !Objects.equals(originalAlarmText, mNextAlarmTextView.getText());
301 }
Amin Shaikh761c5662018-03-27 16:55:43 -0400302
Jason Monk824ffff2017-04-11 15:49:06 -0400303 private void applyDarkness(int id, Rect tintArea, float intensity, int color) {
304 View v = findViewById(id);
305 if (v instanceof DarkReceiver) {
306 ((DarkReceiver) v).onDarkChanged(tintArea, intensity, color);
307 }
308 }
309
Evan Laird95896952018-01-22 19:30:05 -0500310 private int fillColorForIntensity(float intensity, Context context) {
311 if (intensity == 0) {
Amin Shaikh9cac3382018-04-11 17:10:07 -0400312 return context.getColor(R.color.light_mode_icon_color_single_tone);
Evan Laird95896952018-01-22 19:30:05 -0500313 }
Amin Shaikh9cac3382018-04-11 17:10:07 -0400314 return context.getColor(R.color.dark_mode_icon_color_single_tone);
Evan Laird95896952018-01-22 19:30:05 -0500315 }
316
Jason Monke5b770e2017-03-03 21:49:29 -0500317 @Override
318 protected void onConfigurationChanged(Configuration newConfig) {
319 super.onConfigurationChanged(newConfig);
320 updateResources();
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000321
322 // Update color schemes in landscape to use wallpaperTextColor
323 boolean shouldUseWallpaperTextColor =
324 newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000325 mClockView.useWallpaperTextColor(shouldUseWallpaperTextColor);
Jason Monke5b770e2017-03-03 21:49:29 -0500326 }
327
Fabian Kozynskief124492018-11-02 11:02:11 -0400328
329
Jason Monke5b770e2017-03-03 21:49:29 -0500330 @Override
331 public void onRtlPropertiesChanged(int layoutDirection) {
332 super.onRtlPropertiesChanged(layoutDirection);
333 updateResources();
334 }
335
Evan Laird84b98952018-07-02 17:43:59 -0400336 /**
337 * The height of QQS should always be the status bar height + 128dp. This is normally easy, but
338 * when there is a notch involved the status bar can remain a fixed pixel size.
339 */
340 private void updateMinimumHeight() {
341 int sbHeight = mContext.getResources().getDimensionPixelSize(
342 com.android.internal.R.dimen.status_bar_height);
343 int qqsHeight = mContext.getResources().getDimensionPixelSize(
344 R.dimen.qs_quick_header_panel_height);
345
346 setMinimumHeight(sbHeight + qqsHeight);
347 }
348
Jason Monke5b770e2017-03-03 21:49:29 -0500349 private void updateResources() {
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000350 Resources resources = mContext.getResources();
Evan Laird84b98952018-07-02 17:43:59 -0400351 updateMinimumHeight();
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000352
353 // Update height for a few views, especially due to landscape mode restricting space.
Rohan Shahd3cf7562018-02-23 11:12:28 -0800354 mHeaderTextContainerView.getLayoutParams().height =
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000355 resources.getDimensionPixelSize(R.dimen.qs_header_tooltip_height);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800356 mHeaderTextContainerView.setLayoutParams(mHeaderTextContainerView.getLayoutParams());
357
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000358 mSystemIconsView.getLayoutParams().height = resources.getDimensionPixelSize(
359 com.android.internal.R.dimen.quick_qs_offset_height);
360 mSystemIconsView.setLayoutParams(mSystemIconsView.getLayoutParams());
361
Evan Laird84b98952018-07-02 17:43:59 -0400362 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
363 if (mQsDisabled) {
364 lp.height = resources.getDimensionPixelSize(
365 com.android.internal.R.dimen.quick_qs_offset_height);
366 } else {
367 lp.height = Math.max(getMinimumHeight(),
368 resources.getDimensionPixelSize(
Evan Lairdbf3794a2018-07-17 15:55:40 -0400369 com.android.internal.R.dimen.quick_qs_total_height));
Evan Laird84b98952018-07-02 17:43:59 -0400370 }
371
372 setLayoutParams(lp);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000373
Fabian Kozynskief124492018-11-02 11:02:11 -0400374 if (mPrivacyChip != null) {
375 MarginLayoutParams lm = (MarginLayoutParams) mPrivacyChip.getLayoutParams();
376 int sideMargins = lm.leftMargin;
377 int topBottomMargins = resources.getDimensionPixelSize(
378 R.dimen.ongoing_appops_top_chip_margin);
379 lm.setMargins(sideMargins, topBottomMargins, sideMargins, topBottomMargins);
380 mPrivacyChip.setLayoutParams(lm);
381 }
382
Rohan Shahd3cf7562018-02-23 11:12:28 -0800383 updateStatusIconAlphaAnimator();
384 updateHeaderTextContainerAlphaAnimator();
Evan Laird95896952018-01-22 19:30:05 -0500385 }
386
Rohan Shahd3cf7562018-02-23 11:12:28 -0800387 private void updateStatusIconAlphaAnimator() {
388 mStatusIconsAlphaAnimator = new TouchAnimator.Builder()
Evan Laird95896952018-01-22 19:30:05 -0500389 .addFloat(mQuickQsStatusIcons, "alpha", 1, 0)
390 .build();
Jason Monke5b770e2017-03-03 21:49:29 -0500391 }
392
Rohan Shahd3cf7562018-02-23 11:12:28 -0800393 private void updateHeaderTextContainerAlphaAnimator() {
394 mHeaderTextContainerAlphaAnimator = new TouchAnimator.Builder()
395 .addFloat(mHeaderTextContainerView, "alpha", 0, 1)
396 .setStartDelay(.5f)
397 .build();
Jason Monke5b770e2017-03-03 21:49:29 -0500398 }
399
400 public void setExpanded(boolean expanded) {
401 if (mExpanded == expanded) return;
402 mExpanded = expanded;
403 mHeaderQsPanel.setExpanded(expanded);
Fabian Kozynskif3a77012019-01-09 15:37:30 +0000404 updateEverything();
Jason Monke5b770e2017-03-03 21:49:29 -0500405 }
406
Rohan Shahd3cf7562018-02-23 11:12:28 -0800407 /**
408 * Animates the inner contents based on the given expansion details.
409 *
410 * @param isKeyguardShowing whether or not we're showing the keyguard (a.k.a. lockscreen)
411 * @param expansionFraction how much the QS panel is expanded/pulled out (up to 1f)
412 * @param panelTranslationY how much the panel has physically moved down vertically (required
413 * for keyguard animations only)
414 */
415 public void setExpansion(boolean isKeyguardShowing, float expansionFraction,
416 float panelTranslationY) {
417 final float keyguardExpansionFraction = isKeyguardShowing ? 1f : expansionFraction;
418 if (mStatusIconsAlphaAnimator != null) {
419 mStatusIconsAlphaAnimator.setPosition(keyguardExpansionFraction);
Evan Laird95896952018-01-22 19:30:05 -0500420 }
Rohan Shahd3cf7562018-02-23 11:12:28 -0800421
422 if (isKeyguardShowing) {
423 // If the keyguard is showing, we want to offset the text so that it comes in at the
424 // same time as the panel as it slides down.
425 mHeaderTextContainerView.setTranslationY(panelTranslationY);
426 } else {
427 mHeaderTextContainerView.setTranslationY(0f);
428 }
429
430 if (mHeaderTextContainerAlphaAnimator != null) {
431 mHeaderTextContainerAlphaAnimator.setPosition(keyguardExpansionFraction);
432 }
433
434 // Check the original expansion fraction - we don't want to show the tooltip until the
435 // panel is pulled all the way out.
436 if (expansionFraction == 1f) {
437 // QS is fully expanded, bring in the tooltip.
438 showLongPressTooltip();
439 }
440 }
441
442 /** Returns the latest stored tooltip shown count from SharedPreferences. */
443 private int getStoredShownCount() {
444 return Prefs.getInt(
445 mContext,
446 Prefs.Key.QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT,
447 TOOLTIP_NOT_YET_SHOWN_COUNT);
Jason Monke5b770e2017-03-03 21:49:29 -0500448 }
449
Charles Hece2a7c02017-10-11 20:25:20 +0100450 public void disable(int state1, int state2, boolean animate) {
451 final boolean disabled = (state2 & DISABLE2_QUICK_SETTINGS) != 0;
452 if (disabled == mQsDisabled) return;
453 mQsDisabled = disabled;
454 mHeaderQsPanel.setDisabledByPolicy(disabled);
Amin Shaikhf09450b2018-04-06 17:32:45 -0400455 mHeaderTextContainerView.setVisibility(mQsDisabled ? View.GONE : View.VISIBLE);
456 mQuickQsStatusIcons.setVisibility(mQsDisabled ? View.GONE : View.VISIBLE);
457 updateResources();
Charles Hece2a7c02017-10-11 20:25:20 +0100458 }
459
460 @Override
461 public void onAttachedToWindow() {
Amin Shaikha59215a2018-04-23 08:52:38 -0400462 super.onAttachedToWindow();
Jason Monkea54e8a2018-12-20 10:01:48 -0500463 mStatusBarIconController.addIconGroup(mIconManager);
Adrian Roos13836052018-03-15 21:06:37 +0100464 requestApplyInsets();
Evan Laird4bf21df2018-10-22 14:24:32 -0400465 mContext.getContentResolver().registerContentObserver(
466 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mPercentSettingObserver,
467 ActivityManager.getCurrentUser());
Adrian Roos13836052018-03-15 21:06:37 +0100468 }
469
470 @Override
471 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500472 DisplayCutout cutout = insets.getDisplayCutout();
Adrian Roos13836052018-03-15 21:06:37 +0100473 Pair<Integer, Integer> padding = PhoneStatusBarView.cornerCutoutMargins(
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500474 cutout, getDisplay());
Adrian Roos13836052018-03-15 21:06:37 +0100475 if (padding == null) {
Amin Shaikh7d86b042018-04-16 16:00:12 -0400476 mSystemIconsView.setPaddingRelative(
477 getResources().getDimensionPixelSize(R.dimen.status_bar_padding_start), 0,
478 getResources().getDimensionPixelSize(R.dimen.status_bar_padding_end), 0);
Adrian Roos13836052018-03-15 21:06:37 +0100479 } else {
Amin Shaikh7d86b042018-04-16 16:00:12 -0400480 mSystemIconsView.setPadding(padding.first, 0, padding.second, 0);
481
Adrian Roos13836052018-03-15 21:06:37 +0100482 }
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500483 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mSpace.getLayoutParams();
484 if (cutout != null) {
485 Rect topCutout = cutout.getBoundingRectTop();
486 if (topCutout.isEmpty()) {
487 mHasTopCutout = false;
488 lp.width = 0;
489 mSpace.setVisibility(View.GONE);
490 } else {
491 mHasTopCutout = true;
492 lp.width = topCutout.width();
493 mSpace.setVisibility(View.VISIBLE);
494 }
495 }
496 mSpace.setLayoutParams(lp);
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500497 setChipVisibility(mPrivacyChip.getVisibility() == View.VISIBLE);
Adrian Roos13836052018-03-15 21:06:37 +0100498 return super.onApplyWindowInsets(insets);
Charles Hece2a7c02017-10-11 20:25:20 +0100499 }
500
501 @Override
Jason Monke5b770e2017-03-03 21:49:29 -0500502 @VisibleForTesting
503 public void onDetachedFromWindow() {
504 setListening(false);
Jason Monkea54e8a2018-12-20 10:01:48 -0500505 mStatusBarIconController.removeIconGroup(mIconManager);
Evan Laird4bf21df2018-10-22 14:24:32 -0400506 mContext.getContentResolver().unregisterContentObserver(mPercentSettingObserver);
Jason Monke5b770e2017-03-03 21:49:29 -0500507 super.onDetachedFromWindow();
508 }
509
510 public void setListening(boolean listening) {
511 if (listening == mListening) {
512 return;
513 }
514 mHeaderQsPanel.setListening(listening);
Fabian Kozynski8d06c712018-11-07 10:33:02 -0500515 mPrivacyItemController.setListening(listening);
Jason Monke5b770e2017-03-03 21:49:29 -0500516 mListening = listening;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800517
518 if (listening) {
Beverlyb9826dd2018-04-12 15:02:45 -0400519 mZenController.addCallback(this);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800520 mAlarmController.addCallback(this);
Amin Shaikh761c5662018-03-27 16:55:43 -0400521 mContext.registerReceiver(mRingerReceiver,
522 new IntentFilter(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION));
Rohan Shahd3cf7562018-02-23 11:12:28 -0800523 } else {
Beverlyb9826dd2018-04-12 15:02:45 -0400524 mZenController.removeCallback(this);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800525 mAlarmController.removeCallback(this);
Amin Shaikh761c5662018-03-27 16:55:43 -0400526 mContext.unregisterReceiver(mRingerReceiver);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800527 }
Jason Monke5b770e2017-03-03 21:49:29 -0500528 }
529
Evan Laird95896952018-01-22 19:30:05 -0500530 @Override
531 public void onClick(View v) {
Amin Shaikh39bdc502018-05-15 23:43:34 -0400532 if (v == mClockView) {
Jason Monkea54e8a2018-12-20 10:01:48 -0500533 mActivityStarter.postStartActivityDismissingKeyguard(new Intent(
Evan Laird95896952018-01-22 19:30:05 -0500534 AlarmClock.ACTION_SHOW_ALARMS),0);
Fabian Kozynski12638242018-10-12 15:33:41 -0400535 } else if (v == mPrivacyChip) {
536 Handler mUiHandler = new Handler(Looper.getMainLooper());
537 mUiHandler.post(() -> {
538 Dialog mDialog = new OngoingPrivacyDialog(mContext,
539 mPrivacyChip.getBuilder()).createDialog();
540 mDialog.getWindow().setType(
541 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
542 SystemUIDialog.setShowForAllUsers(mDialog, true);
543 SystemUIDialog.registerDismissListener(mDialog);
544 SystemUIDialog.setWindowOnTop(mDialog);
545 mUiHandler.post(() -> mDialog.show());
546 mHost.collapsePanels();
547 });
Evan Laird95896952018-01-22 19:30:05 -0500548 }
549 }
550
Rohan Shahd3cf7562018-02-23 11:12:28 -0800551 @Override
552 public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) {
Amin Shaikh761c5662018-03-27 16:55:43 -0400553 mNextAlarm = nextAlarm;
554 updateStatusText();
555 }
Rohan Shaha160ab12018-03-09 15:47:36 -0800556
Beverlyb9826dd2018-04-12 15:02:45 -0400557 @Override
558 public void onZenChanged(int zen) {
559 updateStatusText();
560
561 }
562
563 @Override
564 public void onConfigChanged(ZenModeConfig config) {
565 updateStatusText();
566 }
567
Amin Shaikh761c5662018-03-27 16:55:43 -0400568 private void updateTooltipShow() {
569 if (hasStatusText()) {
570 hideLongPressTooltip(true /* shouldShowStatusText */);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800571 } else {
Amin Shaikh761c5662018-03-27 16:55:43 -0400572 hideStatusText();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800573 }
574 updateHeaderTextContainerAlphaAnimator();
575 }
576
Amin Shaikh761c5662018-03-27 16:55:43 -0400577 private boolean hasStatusText() {
578 return mNextAlarmTextView.getVisibility() == View.VISIBLE
579 || mRingerModeTextView.getVisibility() == View.VISIBLE;
580 }
581
Rohan Shahd3cf7562018-02-23 11:12:28 -0800582 /**
583 * Animates in the long press tooltip (as long as the next alarm text isn't currently occupying
584 * the space).
585 */
586 public void showLongPressTooltip() {
Amin Shaikh761c5662018-03-27 16:55:43 -0400587 // If we have status text to show, don't bother fading in the tooltip.
588 if (hasStatusText()) {
Rohan Shahd3cf7562018-02-23 11:12:28 -0800589 return;
590 }
591
592 if (mShownCount < MAX_TOOLTIP_SHOWN_COUNT) {
593 mLongPressTooltipView.animate().cancel();
594 mLongPressTooltipView.setVisibility(View.VISIBLE);
595 mLongPressTooltipView.animate()
596 .alpha(1f)
597 .setDuration(FADE_ANIMATION_DURATION_MS)
598 .setListener(new AnimatorListenerAdapter() {
599 @Override
600 public void onAnimationEnd(Animator animation) {
601 mHandler.postDelayed(
602 mAutoFadeOutTooltipRunnable, AUTO_FADE_OUT_DELAY_MS);
603 }
604 })
605 .start();
606
607 // Increment and drop the shown count in prefs for the next time we're deciding to
608 // fade in the tooltip. We first sanity check that the tooltip count hasn't changed yet
609 // in prefs (say, from a long press).
610 if (getStoredShownCount() <= mShownCount) {
611 Prefs.putInt(mContext, Prefs.Key.QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT, ++mShownCount);
612 }
613 }
614 }
615
616 /**
617 * Fades out the long press tooltip if it's partially visible - short circuits any running
Amin Shaikh761c5662018-03-27 16:55:43 -0400618 * animation. Additionally has the ability to fade in the status info text.
Rohan Shahd3cf7562018-02-23 11:12:28 -0800619 *
Amin Shaikh761c5662018-03-27 16:55:43 -0400620 * @param shouldShowStatusText whether we should fade in the status text
Rohan Shahd3cf7562018-02-23 11:12:28 -0800621 */
Amin Shaikh761c5662018-03-27 16:55:43 -0400622 private void hideLongPressTooltip(boolean shouldShowStatusText) {
Rohan Shahd3cf7562018-02-23 11:12:28 -0800623 mLongPressTooltipView.animate().cancel();
624 if (mLongPressTooltipView.getVisibility() == View.VISIBLE
625 && mLongPressTooltipView.getAlpha() != 0f) {
626 mHandler.removeCallbacks(mAutoFadeOutTooltipRunnable);
627 mLongPressTooltipView.animate()
628 .alpha(0f)
629 .setDuration(FADE_ANIMATION_DURATION_MS)
630 .setListener(new AnimatorListenerAdapter() {
631 @Override
632 public void onAnimationEnd(Animator animation) {
Rohan Shaha160ab12018-03-09 15:47:36 -0800633 if (DEBUG) Log.d(TAG, "hideLongPressTooltip: Hid long press tip");
Rohan Shahd3cf7562018-02-23 11:12:28 -0800634 mLongPressTooltipView.setVisibility(View.INVISIBLE);
635
Amin Shaikh761c5662018-03-27 16:55:43 -0400636 if (shouldShowStatusText) {
637 showStatus();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800638 }
639 }
640 })
641 .start();
642 } else {
643 mLongPressTooltipView.setVisibility(View.INVISIBLE);
Amin Shaikh761c5662018-03-27 16:55:43 -0400644 if (shouldShowStatusText) {
645 showStatus();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800646 }
647 }
648 }
649
650 /**
Amin Shaikh761c5662018-03-27 16:55:43 -0400651 * Fades in the updated status text. Note that if there's already a status showing, this will
Fabian Kozynski3f789332018-09-04 16:42:43 -0400652 * immediately fade it out and fade in the updated status.
Rohan Shahd3cf7562018-02-23 11:12:28 -0800653 */
Amin Shaikh761c5662018-03-27 16:55:43 -0400654 private void showStatus() {
655 mStatusContainer.setAlpha(0f);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800656
Amin Shaikh761c5662018-03-27 16:55:43 -0400657 mStatusContainer.animate()
Rohan Shahd3cf7562018-02-23 11:12:28 -0800658 .alpha(1f)
659 .setDuration(FADE_ANIMATION_DURATION_MS)
660 .start();
661 }
662
Fabian Kozynski3f789332018-09-04 16:42:43 -0400663 /** Fades out the status text. */
Amin Shaikh761c5662018-03-27 16:55:43 -0400664 private void hideStatusText() {
Fabian Kozynski3f789332018-09-04 16:42:43 -0400665 mStatusContainer.animate()
666 .alpha(0f)
667 .setDuration(FADE_ANIMATION_DURATION_MS)
668 .start();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800669 }
670
Fabian Kozynskif3a77012019-01-09 15:37:30 +0000671 public void updateEverything() {
Fabian Kozynski9a15b7c2019-01-10 11:13:27 -0500672 post(() -> setClickable(!mExpanded));
Fabian Kozynskif3a77012019-01-09 15:37:30 +0000673 }
674
Jason Monke5b770e2017-03-03 21:49:29 -0500675 public void setQSPanel(final QSPanel qsPanel) {
676 mQsPanel = qsPanel;
677 setupHost(qsPanel.getHost());
678 }
679
680 public void setupHost(final QSTileHost host) {
681 mHost = host;
682 //host.setHeaderView(mExpandIndicator);
683 mHeaderQsPanel.setQSPanelAndHeader(mQsPanel, this);
684 mHeaderQsPanel.setHost(host, null /* No customization in header */);
Evan Lairdef160f22018-01-29 14:08:45 -0500685
Evan Laird4bf21df2018-10-22 14:24:32 -0400686
687 Rect tintArea = new Rect(0, 0, 0, 0);
688 int colorForeground = Utils.getColorAttrDefaultColor(getContext(),
689 android.R.attr.colorForeground);
690 float intensity = getColorIntensity(colorForeground);
691 int fillColor = fillColorForIntensity(intensity, getContext());
692 mBatteryRemainingIcon.setColorsFromContext(mHost.getContext());
693 mBatteryRemainingIcon.onDarkChanged(tintArea, intensity, fillColor);
Jason Monke5b770e2017-03-03 21:49:29 -0500694 }
695
696 public void setCallback(Callback qsPanelCallback) {
697 mHeaderQsPanel.setCallback(qsPanelCallback);
698 }
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800699
700 private String formatNextAlarm(AlarmManager.AlarmClockInfo info) {
701 if (info == null) {
702 return "";
703 }
704 String skeleton = android.text.format.DateFormat
705 .is24HourFormat(mContext, ActivityManager.getCurrentUser()) ? "EHm" : "Ehma";
706 String pattern = android.text.format.DateFormat
707 .getBestDateTimePattern(Locale.getDefault(), skeleton);
708 return android.text.format.DateFormat.format(pattern, info.getTriggerTime()).toString();
709 }
Amin Shaikh0e003312018-03-08 11:39:01 -0500710
711 public static float getColorIntensity(@ColorInt int color) {
712 return color == Color.WHITE ? 0 : 1;
713 }
Amin Shaikh50d2d1e2018-04-25 14:48:13 -0400714
715 public void setMargins(int sideMargins) {
716 for (int i = 0; i < getChildCount(); i++) {
717 View v = getChildAt(i);
Fabian Kozynskief124492018-11-02 11:02:11 -0400718 if (v == mSystemIconsView || v == mQuickQsStatusIcons || v == mHeaderQsPanel
719 || v == mPrivacyChip) {
Amin Shaikh50d2d1e2018-04-25 14:48:13 -0400720 continue;
721 }
722 RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();
723 lp.leftMargin = sideMargins;
724 lp.rightMargin = sideMargins;
725 }
726 }
Evan Laird4bf21df2018-10-22 14:24:32 -0400727
728 private void updateShowPercent() {
729 final boolean systemSetting = 0 != Settings.System
730 .getIntForUser(getContext().getContentResolver(),
731 SHOW_BATTERY_PERCENT, 0, ActivityManager.getCurrentUser());
732
Evan Lairda5a73c52019-01-11 13:36:32 -0500733 mBatteryRemainingIcon.setPercentShowMode(systemSetting
734 ? BatteryMeterView.MODE_ESTIMATE : BatteryMeterView.MODE_ON);
Evan Laird4bf21df2018-10-22 14:24:32 -0400735 }
736
737 private final class PercentSettingObserver extends ContentObserver {
738 PercentSettingObserver(Handler handler) {
739 super(handler);
740 }
741
742 @Override
743 public void onChange(boolean selfChange, Uri uri) {
744 super.onChange(selfChange, uri);
745 updateShowPercent();
746 }
747 }
Jason Monke5b770e2017-03-03 21:49:29 -0500748}