blob: 0548e698be3320a46de107cb4924db7467f803b3 [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;
Charles Hece2a7c02017-10-11 20:25:20 +010018
Rohan Shahd3cf7562018-02-23 11:12:28 -080019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Amin Shaikh0e003312018-03-08 11:39:01 -050021import android.annotation.ColorInt;
Lucas Dupin1f7374a2018-02-26 18:08:33 -080022import android.app.ActivityManager;
Rohan Shahd3cf7562018-02-23 11:12:28 -080023import android.app.AlarmManager;
Jason Monke5b770e2017-03-03 21:49:29 -050024import android.content.Context;
Evan Laird4ea2a492018-01-22 11:29:12 -050025import android.content.Intent;
Jason Monke5b770e2017-03-03 21:49:29 -050026import android.content.res.Configuration;
Jason Monke5b770e2017-03-03 21:49:29 -050027import android.graphics.Color;
28import android.graphics.Rect;
Rohan Shahd3cf7562018-02-23 11:12:28 -080029import android.os.Handler;
Evan Laird4ea2a492018-01-22 11:29:12 -050030import android.provider.AlarmClock;
Jason Monke5b770e2017-03-03 21:49:29 -050031import android.support.annotation.VisibleForTesting;
Rohan Shahd3cf7562018-02-23 11:12:28 -080032import android.text.TextUtils;
33import android.text.format.DateUtils;
Jason Monke5b770e2017-03-03 21:49:29 -050034import android.util.AttributeSet;
Jason Monk824ffff2017-04-11 15:49:06 -040035import android.view.View;
Jason Monke5b770e2017-03-03 21:49:29 -050036import android.widget.RelativeLayout;
Rohan Shahd3cf7562018-02-23 11:12:28 -080037import android.widget.TextView;
Jason Monke5b770e2017-03-03 21:49:29 -050038
39import com.android.settingslib.Utils;
40import com.android.systemui.BatteryMeterView;
41import com.android.systemui.Dependency;
Rohan Shahd3cf7562018-02-23 11:12:28 -080042import com.android.systemui.Prefs;
Jason Monke5b770e2017-03-03 21:49:29 -050043import com.android.systemui.R;
Jason Monk01df36f2017-06-07 13:02:47 -040044import com.android.systemui.R.id;
Charles Hece2a7c02017-10-11 20:25:20 +010045import com.android.systemui.SysUiServiceProvider;
Jason Monke5b770e2017-03-03 21:49:29 -050046import com.android.systemui.plugins.ActivityStarter;
47import com.android.systemui.qs.QSDetail.Callback;
Charles Hece2a7c02017-10-11 20:25:20 +010048import com.android.systemui.statusbar.CommandQueue;
Evan Laird95896952018-01-22 19:30:05 -050049import com.android.systemui.statusbar.phone.StatusBarIconController;
50import com.android.systemui.statusbar.phone.StatusBarIconController.TintedIconManager;
Evan Laird39254d42018-01-18 16:05:30 -050051import com.android.systemui.statusbar.policy.DarkIconDispatcher;
Jason Monk824ffff2017-04-11 15:49:06 -040052import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
Rohan Shahd3cf7562018-02-23 11:12:28 -080053import com.android.systemui.statusbar.policy.NextAlarmController;
Jason Monke5b770e2017-03-03 21:49:29 -050054
Lucas Dupin1f7374a2018-02-26 18:08:33 -080055import java.util.Locale;
56
Rohan Shahd3cf7562018-02-23 11:12:28 -080057/**
58 * View that contains the top-most bits of the screen (primarily the status bar with date, time, and
59 * battery) and also contains the {@link QuickQSPanel} along with some of the panel's inner
60 * contents.
61 */
62public class QuickStatusBarHeader extends RelativeLayout implements CommandQueue.Callbacks,
63 View.OnClickListener, NextAlarmController.NextAlarmChangeCallback {
Jason Monke5b770e2017-03-03 21:49:29 -050064
Rohan Shahd3cf7562018-02-23 11:12:28 -080065 /** Delay for auto fading out the long press tooltip after it's fully visible (in ms). */
66 private static final long AUTO_FADE_OUT_DELAY_MS = DateUtils.SECOND_IN_MILLIS * 6;
67 private static final int FADE_ANIMATION_DURATION_MS = 300;
68 private static final int TOOLTIP_NOT_YET_SHOWN_COUNT = 0;
Rohan Shahb6a915c2018-03-01 11:57:00 -080069 public static final int MAX_TOOLTIP_SHOWN_COUNT = 2;
Rohan Shahd3cf7562018-02-23 11:12:28 -080070
71 private final Handler mHandler = new Handler();
Jason Monke5b770e2017-03-03 21:49:29 -050072
73 private QSPanel mQsPanel;
74
75 private boolean mExpanded;
76 private boolean mListening;
Charles Hece2a7c02017-10-11 20:25:20 +010077 private boolean mQsDisabled;
Jason Monke5b770e2017-03-03 21:49:29 -050078
79 protected QuickQSPanel mHeaderQsPanel;
80 protected QSTileHost mHost;
Evan Laird95896952018-01-22 19:30:05 -050081 private TintedIconManager mIconManager;
Rohan Shahd3cf7562018-02-23 11:12:28 -080082 private TouchAnimator mStatusIconsAlphaAnimator;
83 private TouchAnimator mHeaderTextContainerAlphaAnimator;
Evan Laird95896952018-01-22 19:30:05 -050084
85 private View mQuickQsStatusIcons;
Evan Laird4ea2a492018-01-22 11:29:12 -050086 private View mDate;
Rohan Shahd3cf7562018-02-23 11:12:28 -080087 private View mHeaderTextContainerView;
88 /** View corresponding to the next alarm info (including the icon). */
89 private View mNextAlarmView;
90 /** Tooltip for educating users that they can long press on icons to see more details. */
91 private View mLongPressTooltipView;
92 /** {@link TextView} containing the actual text indicating when the next alarm will go off. */
93 private TextView mNextAlarmTextView;
94
95 private NextAlarmController mAlarmController;
96 private String mNextAlarmText;
97 /** Counts how many times the long press tooltip has been shown to the user. */
98 private int mShownCount;
99
100 /**
101 * Runnable for automatically fading out the long press tooltip (as if it were animating away).
102 */
103 private final Runnable mAutoFadeOutTooltipRunnable = () -> hideLongPressTooltip(false);
Evan Laird4ea2a492018-01-22 11:29:12 -0500104
Jason Monke5b770e2017-03-03 21:49:29 -0500105 public QuickStatusBarHeader(Context context, AttributeSet attrs) {
106 super(context, attrs);
Rohan Shahd3cf7562018-02-23 11:12:28 -0800107
108 mAlarmController = Dependency.get(NextAlarmController.class);
109 mShownCount = getStoredShownCount();
Jason Monke5b770e2017-03-03 21:49:29 -0500110 }
111
112 @Override
113 protected void onFinishInflate() {
114 super.onFinishInflate();
Jason Monke5b770e2017-03-03 21:49:29 -0500115
116 mHeaderQsPanel = findViewById(R.id.quick_qs_panel);
Evan Laird4ea2a492018-01-22 11:29:12 -0500117 mDate = findViewById(R.id.date);
118 mDate.setOnClickListener(this);
Evan Laird95896952018-01-22 19:30:05 -0500119 mQuickQsStatusIcons = findViewById(R.id.quick_qs_status_icons);
120 mIconManager = new TintedIconManager(findViewById(R.id.statusIcons));
Jason Monke5b770e2017-03-03 21:49:29 -0500121
Rohan Shahd3cf7562018-02-23 11:12:28 -0800122 // Views corresponding to the header info section (e.g. tooltip and next alarm).
123 mHeaderTextContainerView = findViewById(R.id.header_text_container);
124 mLongPressTooltipView = findViewById(R.id.long_press_tooltip);
125 mNextAlarmView = findViewById(R.id.next_alarm);
126 mNextAlarmTextView = findViewById(R.id.next_alarm_text);
Jason Monke5b770e2017-03-03 21:49:29 -0500127
128 updateResources();
129
Jason Monk824ffff2017-04-11 15:49:06 -0400130 Rect tintArea = new Rect(0, 0, 0, 0);
Evan Laird95896952018-01-22 19:30:05 -0500131 int colorForeground = Utils.getColorAttr(getContext(), android.R.attr.colorForeground);
Amin Shaikh0e003312018-03-08 11:39:01 -0500132 float intensity = getColorIntensity(colorForeground);
Evan Laird95896952018-01-22 19:30:05 -0500133 int fillColor = fillColorForIntensity(intensity, getContext());
134
135 // Set light text on the header icons because they will always be on a black background
Evan Laird39254d42018-01-18 16:05:30 -0500136 applyDarkness(R.id.clock, tintArea, 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Evan Laird95896952018-01-22 19:30:05 -0500137 applyDarkness(id.signal_cluster, tintArea, intensity, colorForeground);
138
139 // Set the correct tint for the status icons so they contrast
140 mIconManager.setTint(fillColor);
Jason Monke5b770e2017-03-03 21:49:29 -0500141
142 BatteryMeterView battery = findViewById(R.id.battery);
143 battery.setForceShowPercent(true);
Jason Monke5b770e2017-03-03 21:49:29 -0500144 }
145
Jason Monk824ffff2017-04-11 15:49:06 -0400146 private void applyDarkness(int id, Rect tintArea, float intensity, int color) {
147 View v = findViewById(id);
148 if (v instanceof DarkReceiver) {
149 ((DarkReceiver) v).onDarkChanged(tintArea, intensity, color);
150 }
151 }
152
Evan Laird95896952018-01-22 19:30:05 -0500153 private int fillColorForIntensity(float intensity, Context context) {
154 if (intensity == 0) {
155 return context.getColor(R.color.light_mode_icon_color_dual_tone_fill);
156 }
157 return context.getColor(R.color.dark_mode_icon_color_dual_tone_fill);
158 }
159
Jason Monke5b770e2017-03-03 21:49:29 -0500160 @Override
161 protected void onConfigurationChanged(Configuration newConfig) {
162 super.onConfigurationChanged(newConfig);
163 updateResources();
164 }
165
166 @Override
167 public void onRtlPropertiesChanged(int layoutDirection) {
168 super.onRtlPropertiesChanged(layoutDirection);
169 updateResources();
170 }
171
172 private void updateResources() {
Rohan Shahd3cf7562018-02-23 11:12:28 -0800173 // Update height, especially due to landscape mode restricting space.
174 mHeaderTextContainerView.getLayoutParams().height =
175 mContext.getResources().getDimensionPixelSize(R.dimen.qs_header_tooltip_height);
176 mHeaderTextContainerView.setLayoutParams(mHeaderTextContainerView.getLayoutParams());
177
178 updateStatusIconAlphaAnimator();
179 updateHeaderTextContainerAlphaAnimator();
Evan Laird95896952018-01-22 19:30:05 -0500180 }
181
Rohan Shahd3cf7562018-02-23 11:12:28 -0800182 private void updateStatusIconAlphaAnimator() {
183 mStatusIconsAlphaAnimator = new TouchAnimator.Builder()
Evan Laird95896952018-01-22 19:30:05 -0500184 .addFloat(mQuickQsStatusIcons, "alpha", 1, 0)
185 .build();
Jason Monke5b770e2017-03-03 21:49:29 -0500186 }
187
Rohan Shahd3cf7562018-02-23 11:12:28 -0800188 private void updateHeaderTextContainerAlphaAnimator() {
189 mHeaderTextContainerAlphaAnimator = new TouchAnimator.Builder()
190 .addFloat(mHeaderTextContainerView, "alpha", 0, 1)
191 .setStartDelay(.5f)
192 .build();
Jason Monke5b770e2017-03-03 21:49:29 -0500193 }
194
195 public void setExpanded(boolean expanded) {
196 if (mExpanded == expanded) return;
197 mExpanded = expanded;
198 mHeaderQsPanel.setExpanded(expanded);
199 updateEverything();
200 }
201
Rohan Shahd3cf7562018-02-23 11:12:28 -0800202 /**
203 * Animates the inner contents based on the given expansion details.
204 *
205 * @param isKeyguardShowing whether or not we're showing the keyguard (a.k.a. lockscreen)
206 * @param expansionFraction how much the QS panel is expanded/pulled out (up to 1f)
207 * @param panelTranslationY how much the panel has physically moved down vertically (required
208 * for keyguard animations only)
209 */
210 public void setExpansion(boolean isKeyguardShowing, float expansionFraction,
211 float panelTranslationY) {
212 final float keyguardExpansionFraction = isKeyguardShowing ? 1f : expansionFraction;
213 if (mStatusIconsAlphaAnimator != null) {
214 mStatusIconsAlphaAnimator.setPosition(keyguardExpansionFraction);
Evan Laird95896952018-01-22 19:30:05 -0500215 }
Rohan Shahd3cf7562018-02-23 11:12:28 -0800216
217 if (isKeyguardShowing) {
218 // If the keyguard is showing, we want to offset the text so that it comes in at the
219 // same time as the panel as it slides down.
220 mHeaderTextContainerView.setTranslationY(panelTranslationY);
221 } else {
222 mHeaderTextContainerView.setTranslationY(0f);
223 }
224
225 if (mHeaderTextContainerAlphaAnimator != null) {
226 mHeaderTextContainerAlphaAnimator.setPosition(keyguardExpansionFraction);
227 }
228
229 // Check the original expansion fraction - we don't want to show the tooltip until the
230 // panel is pulled all the way out.
231 if (expansionFraction == 1f) {
232 // QS is fully expanded, bring in the tooltip.
233 showLongPressTooltip();
234 }
235 }
236
237 /** Returns the latest stored tooltip shown count from SharedPreferences. */
238 private int getStoredShownCount() {
239 return Prefs.getInt(
240 mContext,
241 Prefs.Key.QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT,
242 TOOLTIP_NOT_YET_SHOWN_COUNT);
Jason Monke5b770e2017-03-03 21:49:29 -0500243 }
244
245 @Override
Charles Hece2a7c02017-10-11 20:25:20 +0100246 public void disable(int state1, int state2, boolean animate) {
247 final boolean disabled = (state2 & DISABLE2_QUICK_SETTINGS) != 0;
248 if (disabled == mQsDisabled) return;
249 mQsDisabled = disabled;
250 mHeaderQsPanel.setDisabledByPolicy(disabled);
Evan Laird19bf52c2018-01-24 19:54:58 -0500251 final int rawHeight = (int) getResources().getDimension(
252 com.android.internal.R.dimen.quick_qs_total_height);
Charles Hece2a7c02017-10-11 20:25:20 +0100253 getLayoutParams().height = disabled ? (rawHeight - mHeaderQsPanel.getHeight()) : rawHeight;
254 }
255
256 @Override
257 public void onAttachedToWindow() {
258 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).addCallbacks(this);
Evan Laird95896952018-01-22 19:30:05 -0500259 Dependency.get(StatusBarIconController.class).addIconGroup(mIconManager);
Charles Hece2a7c02017-10-11 20:25:20 +0100260 }
261
262 @Override
Jason Monke5b770e2017-03-03 21:49:29 -0500263 @VisibleForTesting
264 public void onDetachedFromWindow() {
265 setListening(false);
Charles Hece2a7c02017-10-11 20:25:20 +0100266 SysUiServiceProvider.getComponent(getContext(), CommandQueue.class).removeCallbacks(this);
Evan Laird95896952018-01-22 19:30:05 -0500267 Dependency.get(StatusBarIconController.class).removeIconGroup(mIconManager);
Jason Monke5b770e2017-03-03 21:49:29 -0500268 super.onDetachedFromWindow();
269 }
270
271 public void setListening(boolean listening) {
272 if (listening == mListening) {
273 return;
274 }
275 mHeaderQsPanel.setListening(listening);
276 mListening = listening;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800277
278 if (listening) {
279 mAlarmController.addCallback(this);
280 } else {
281 mAlarmController.removeCallback(this);
282 }
Jason Monke5b770e2017-03-03 21:49:29 -0500283 }
284
Evan Laird95896952018-01-22 19:30:05 -0500285 @Override
286 public void onClick(View v) {
287 if(v == mDate){
288 Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard(new Intent(
289 AlarmClock.ACTION_SHOW_ALARMS),0);
290 }
291 }
292
Rohan Shahd3cf7562018-02-23 11:12:28 -0800293 @Override
294 public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) {
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800295 mNextAlarmText = nextAlarm != null ? formatNextAlarm(nextAlarm) : null;
Rohan Shahd3cf7562018-02-23 11:12:28 -0800296 if (mNextAlarmText != null) {
297 hideLongPressTooltip(true /* shouldFadeInAlarmText */);
298 } else {
299 hideAlarmText();
300 }
301 updateHeaderTextContainerAlphaAnimator();
302 }
303
304 /**
305 * Animates in the long press tooltip (as long as the next alarm text isn't currently occupying
306 * the space).
307 */
308 public void showLongPressTooltip() {
309 // If we have alarm text to show, don't bother fading in the tooltip.
310 if (!TextUtils.isEmpty(mNextAlarmText)) {
311 return;
312 }
313
314 if (mShownCount < MAX_TOOLTIP_SHOWN_COUNT) {
315 mLongPressTooltipView.animate().cancel();
316 mLongPressTooltipView.setVisibility(View.VISIBLE);
317 mLongPressTooltipView.animate()
318 .alpha(1f)
319 .setDuration(FADE_ANIMATION_DURATION_MS)
320 .setListener(new AnimatorListenerAdapter() {
321 @Override
322 public void onAnimationEnd(Animator animation) {
323 mHandler.postDelayed(
324 mAutoFadeOutTooltipRunnable, AUTO_FADE_OUT_DELAY_MS);
325 }
326 })
327 .start();
328
329 // Increment and drop the shown count in prefs for the next time we're deciding to
330 // fade in the tooltip. We first sanity check that the tooltip count hasn't changed yet
331 // in prefs (say, from a long press).
332 if (getStoredShownCount() <= mShownCount) {
333 Prefs.putInt(mContext, Prefs.Key.QS_LONG_PRESS_TOOLTIP_SHOWN_COUNT, ++mShownCount);
334 }
335 }
336 }
337
338 /**
339 * Fades out the long press tooltip if it's partially visible - short circuits any running
340 * animation. Additionally has the ability to fade in the alarm info text.
341 *
342 * @param shouldShowAlarmText whether we should fade in the next alarm text
343 */
344 private void hideLongPressTooltip(boolean shouldShowAlarmText) {
345 mLongPressTooltipView.animate().cancel();
346 if (mLongPressTooltipView.getVisibility() == View.VISIBLE
347 && mLongPressTooltipView.getAlpha() != 0f) {
348 mHandler.removeCallbacks(mAutoFadeOutTooltipRunnable);
349 mLongPressTooltipView.animate()
350 .alpha(0f)
351 .setDuration(FADE_ANIMATION_DURATION_MS)
352 .setListener(new AnimatorListenerAdapter() {
353 @Override
354 public void onAnimationEnd(Animator animation) {
355 mLongPressTooltipView.setVisibility(View.INVISIBLE);
356
357 if (shouldShowAlarmText) {
358 showAlarmText();
359 }
360 }
361 })
362 .start();
363 } else {
364 mLongPressTooltipView.setVisibility(View.INVISIBLE);
365
366 if (shouldShowAlarmText) {
367 showAlarmText();
368 }
369 }
370 }
371
372 /**
373 * Fades in the updated alarm text. Note that if there's already an alarm showing, this will
374 * immediately hide it and fade in the updated time.
375 */
376 private void showAlarmText() {
377 mNextAlarmView.setAlpha(0f);
378 mNextAlarmView.setVisibility(View.VISIBLE);
379 mNextAlarmTextView.setText(mNextAlarmText);
380
381 mNextAlarmView.animate()
382 .alpha(1f)
383 .setDuration(FADE_ANIMATION_DURATION_MS)
384 .start();
385 }
386
387 /**
388 * Fades out and hides the next alarm text. This also resets the text contents to null in
389 * preparation for the next alarm update.
390 */
391 private void hideAlarmText() {
392 if (mNextAlarmView.getVisibility() == View.VISIBLE) {
393 mNextAlarmView.animate()
394 .alpha(0f)
395 .setListener(new AnimatorListenerAdapter() {
396 @Override
397 public void onAnimationEnd(Animator animation) {
398 // Reset the alpha regardless of how the animation ends for the next
399 // time we show this view/want to animate it.
400 mNextAlarmView.setVisibility(View.INVISIBLE);
401 mNextAlarmView.setAlpha(1f);
402 mNextAlarmTextView.setText(null);
403 }
404 })
405 .start();
406 } else {
407 // Next alarm view is already hidden, only need to clear the text.
408 mNextAlarmTextView.setText(null);
409 }
410 }
411
Jason Monke5b770e2017-03-03 21:49:29 -0500412 public void updateEverything() {
Jason Monk1fdde2d2017-03-08 09:39:21 -0500413 post(() -> setClickable(false));
Jason Monke5b770e2017-03-03 21:49:29 -0500414 }
415
416 public void setQSPanel(final QSPanel qsPanel) {
417 mQsPanel = qsPanel;
418 setupHost(qsPanel.getHost());
419 }
420
421 public void setupHost(final QSTileHost host) {
422 mHost = host;
423 //host.setHeaderView(mExpandIndicator);
424 mHeaderQsPanel.setQSPanelAndHeader(mQsPanel, this);
425 mHeaderQsPanel.setHost(host, null /* No customization in header */);
Evan Lairdef160f22018-01-29 14:08:45 -0500426
427 // Use SystemUI context to get battery meter colors, and let it use the default tint (white)
428 BatteryMeterView battery = findViewById(R.id.battery);
429 battery.setColorsFromContext(mHost.getContext());
430 battery.onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Jason Monke5b770e2017-03-03 21:49:29 -0500431 }
432
433 public void setCallback(Callback qsPanelCallback) {
434 mHeaderQsPanel.setCallback(qsPanelCallback);
435 }
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800436
437 private String formatNextAlarm(AlarmManager.AlarmClockInfo info) {
438 if (info == null) {
439 return "";
440 }
441 String skeleton = android.text.format.DateFormat
442 .is24HourFormat(mContext, ActivityManager.getCurrentUser()) ? "EHm" : "Ehma";
443 String pattern = android.text.format.DateFormat
444 .getBestDateTimePattern(Locale.getDefault(), skeleton);
445 return android.text.format.DateFormat.format(pattern, info.getTriggerTime()).toString();
446 }
Amin Shaikh0e003312018-03-08 11:39:01 -0500447
448 public static float getColorIntensity(@ColorInt int color) {
449 return color == Color.WHITE ? 0 : 1;
450 }
451
Jason Monke5b770e2017-03-03 21:49:29 -0500452}