blob: 1f509549efd185f65a0c5bd6f86715fa603d961b [file] [log] [blame]
John Spurlock7edfbca2013-09-14 11:58:55 -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
Winson Chung1aa24b92019-04-24 15:17:33 -070019import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
20
Vinit Nayak3f35db52019-08-08 17:31:48 -070021import static com.android.systemui.util.Utils.isGesturalModeOnDefaultDisplay;
Winson Chunge89f4b52019-03-29 15:01:49 -070022
John Spurlock7edfbca2013-09-14 11:58:55 -040023import android.content.Context;
Winson Chung775d2872019-03-27 14:14:01 -070024import android.graphics.Rect;
Jason Monk2044e6e2017-10-13 10:34:40 -040025import android.os.Handler;
26import android.os.RemoteException;
John Spurlock7edfbca2013-09-14 11:58:55 -040027import android.os.ServiceManager;
Jorim Jaggi40db0292016-06-27 17:58:03 -070028import android.util.SparseArray;
Jason Monk2044e6e2017-10-13 10:34:40 -040029import android.view.Display;
30import android.view.IWallpaperVisibilityListener;
31import android.view.IWindowManager;
John Spurlock7edfbca2013-09-14 11:58:55 -040032import android.view.View;
John Spurlock7edfbca2013-09-14 11:58:55 -040033
34import com.android.internal.statusbar.IStatusBarService;
Jason Monk2044e6e2017-10-13 10:34:40 -040035import com.android.systemui.Dependency;
John Spurlock7edfbca2013-09-14 11:58:55 -040036import com.android.systemui.R;
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040037import com.android.systemui.statusbar.CommandQueue;
John Spurlock7edfbca2013-09-14 11:58:55 -040038
James O'Leary4335c702019-05-29 12:38:51 -040039import java.util.ArrayList;
40import java.util.List;
41
Winson Chunge89f4b52019-03-29 15:01:49 -070042public final class NavigationBarTransitions extends BarTransitions implements
43 LightBarTransitionsController.DarkIntensityApplier {
John Spurlock7edfbca2013-09-14 11:58:55 -040044
Vinit Nayak3f35db52019-08-08 17:31:48 -070045 public static final int MIN_COLOR_ADAPT_TRANSITION_TIME = 400;
46 public static final int DEFAULT_COLOR_ADAPT_TRANSITION_TIME = 1700;
47
James O'Leary4335c702019-05-29 12:38:51 -040048 /**
49 * Notified when the color of nav bar elements changes.
50 */
51 public interface DarkIntensityListener {
52 /**
53 * Called when the color of nav bar elements changes.
54 * @param darkIntensity 0 is the lightest color, 1 is the darkest.
55 */
56 void onDarkIntensity(float darkIntensity);
57 }
58
John Spurlock7edfbca2013-09-14 11:58:55 -040059 private final NavigationBarView mView;
Jorim Jaggi40db0292016-06-27 17:58:03 -070060 private final LightBarTransitionsController mLightTransitionsController;
Matthew Ng3107d992017-11-21 15:56:19 -080061 private final boolean mAllowAutoDimWallpaperNotVisible;
Jason Monk2044e6e2017-10-13 10:34:40 -040062 private boolean mWallpaperVisible;
John Spurlock7edfbca2013-09-14 11:58:55 -040063
64 private boolean mLightsOut;
Jason Monkc2beef52017-08-29 18:58:00 -040065 private boolean mAutoDim;
Jason Monk0e3aab22018-01-18 19:05:52 -050066 private View mNavButtons;
Winson Chung1aa24b92019-04-24 15:17:33 -070067 private int mNavBarMode = NAV_BAR_MODE_3BUTTON;
James O'Leary4335c702019-05-29 12:38:51 -040068 private List<DarkIntensityListener> mDarkIntensityListeners;
John Spurlock7edfbca2013-09-14 11:58:55 -040069
Winson Chungb03d44d2018-09-18 12:42:45 -070070 private final Handler mHandler = Handler.getMain();
71 private final IWallpaperVisibilityListener mWallpaperVisibilityListener =
72 new IWallpaperVisibilityListener.Stub() {
73 @Override
74 public void onWallpaperVisibilityChanged(boolean newVisibility,
75 int displayId) throws RemoteException {
76 mWallpaperVisible = newVisibility;
77 mHandler.post(() -> applyLightsOut(true, false));
78 }
79 };
80
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040081 public NavigationBarTransitions(NavigationBarView view, CommandQueue commandQueue) {
John Spurlock7057d2c2013-10-02 10:00:37 -040082 super(view, R.drawable.nav_background);
John Spurlock7edfbca2013-09-14 11:58:55 -040083 mView = view;
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040084 mLightTransitionsController = new LightBarTransitionsController(
85 view.getContext(), this, commandQueue);
Matthew Ng3107d992017-11-21 15:56:19 -080086 mAllowAutoDimWallpaperNotVisible = view.getContext().getResources()
87 .getBoolean(R.bool.config_navigation_bar_enable_auto_dim_no_visible_wallpaper);
James O'Leary4335c702019-05-29 12:38:51 -040088 mDarkIntensityListeners = new ArrayList();
Jason Monk2044e6e2017-10-13 10:34:40 -040089
90 IWindowManager windowManagerService = Dependency.get(IWindowManager.class);
Jason Monk2044e6e2017-10-13 10:34:40 -040091 try {
92 mWallpaperVisible = windowManagerService.registerWallpaperVisibilityListener(
Winson Chungb03d44d2018-09-18 12:42:45 -070093 mWallpaperVisibilityListener, Display.DEFAULT_DISPLAY);
Jason Monk2044e6e2017-10-13 10:34:40 -040094 } catch (RemoteException e) {
95 }
Jason Monk0e3aab22018-01-18 19:05:52 -050096 mView.addOnLayoutChangeListener(
97 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
98 View currentView = mView.getCurrentView();
99 if (currentView != null) {
100 mNavButtons = currentView.findViewById(R.id.nav_buttons);
101 applyLightsOut(false, true);
102 }
103 });
104 View currentView = mView.getCurrentView();
105 if (currentView != null) {
106 mNavButtons = currentView.findViewById(R.id.nav_buttons);
107 }
John Spurlock7edfbca2013-09-14 11:58:55 -0400108 }
109
Adrian Roos8a8ffd42015-05-26 18:30:37 -0700110 public void init() {
John Spurlocke631b412013-09-18 16:33:57 -0400111 applyModeBackground(-1, getMode(), false /*animate*/);
Jason Monkc2beef52017-08-29 18:58:00 -0400112 applyLightsOut(false /*animate*/, true /*force*/);
113 }
114
115 @Override
Winson Chungb03d44d2018-09-18 12:42:45 -0700116 public void destroy() {
117 IWindowManager windowManagerService = Dependency.get(IWindowManager.class);
118 try {
119 windowManagerService.unregisterWallpaperVisibilityListener(mWallpaperVisibilityListener,
120 Display.DEFAULT_DISPLAY);
121 } catch (RemoteException e) {
122 }
123 }
124
125 @Override
Jason Monkc2beef52017-08-29 18:58:00 -0400126 public void setAutoDim(boolean autoDim) {
Winson Chungebeed022019-05-14 16:22:57 -0700127 // Ensure we aren't in gestural nav if we are triggering auto dim
Vinit Nayak3f35db52019-08-08 17:31:48 -0700128 if (autoDim && isGesturalModeOnDefaultDisplay(mView.getContext(), mNavBarMode)) return;
Jason Monkc2beef52017-08-29 18:58:00 -0400129 if (mAutoDim == autoDim) return;
130 mAutoDim = autoDim;
131 applyLightsOut(true, false);
132 }
133
Winson Chung775d2872019-03-27 14:14:01 -0700134 void setBackgroundFrame(Rect frame) {
135 mBarBackground.setFrame(frame);
136 }
137
Jason Monkc2beef52017-08-29 18:58:00 -0400138 @Override
139 protected boolean isLightsOut(int mode) {
Matthew Ng3107d992017-11-21 15:56:19 -0800140 return super.isLightsOut(mode) || (mAllowAutoDimWallpaperNotVisible && mAutoDim
141 && !mWallpaperVisible && mode != MODE_WARNING);
John Spurlock7edfbca2013-09-14 11:58:55 -0400142 }
143
Jorim Jaggi40db0292016-06-27 17:58:03 -0700144 public LightBarTransitionsController getLightTransitionsController() {
145 return mLightTransitionsController;
146 }
147
John Spurlock7edfbca2013-09-14 11:58:55 -0400148 @Override
149 protected void onTransition(int oldMode, int newMode, boolean animate) {
150 super.onTransition(oldMode, newMode, animate);
Jason Monkc2beef52017-08-29 18:58:00 -0400151 applyLightsOut(animate, false /*force*/);
Winson Chung775d2872019-03-27 14:14:01 -0700152 mView.onBarTransition(newMode);
John Spurlock7edfbca2013-09-14 11:58:55 -0400153 }
154
Jason Monkc2beef52017-08-29 18:58:00 -0400155 private void applyLightsOut(boolean animate, boolean force) {
John Spurlock7edfbca2013-09-14 11:58:55 -0400156 // apply to lights out
Jason Monkc2beef52017-08-29 18:58:00 -0400157 applyLightsOut(isLightsOut(getMode()), animate, force);
John Spurlock7edfbca2013-09-14 11:58:55 -0400158 }
159
John Spurlock7edfbca2013-09-14 11:58:55 -0400160 private void applyLightsOut(boolean lightsOut, boolean animate, boolean force) {
161 if (!force && lightsOut == mLightsOut) return;
162
163 mLightsOut = lightsOut;
Jason Monk0e3aab22018-01-18 19:05:52 -0500164 if (mNavButtons == null) return;
John Spurlock7edfbca2013-09-14 11:58:55 -0400165
166 // ok, everyone, stop it right there
Jason Monk0e3aab22018-01-18 19:05:52 -0500167 mNavButtons.animate().cancel();
John Spurlock7edfbca2013-09-14 11:58:55 -0400168
Jason Monkcdec5ff2017-11-08 15:51:59 -0500169 // Bump percentage by 10% if dark.
170 float darkBump = mLightTransitionsController.getCurrentDarkIntensity() / 10;
171 final float navButtonsAlpha = lightsOut ? 0.6f + darkBump : 1f;
John Spurlock7edfbca2013-09-14 11:58:55 -0400172
173 if (!animate) {
Jason Monk0e3aab22018-01-18 19:05:52 -0500174 mNavButtons.setAlpha(navButtonsAlpha);
John Spurlock7edfbca2013-09-14 11:58:55 -0400175 } else {
176 final int duration = lightsOut ? LIGHTS_OUT_DURATION : LIGHTS_IN_DURATION;
Jason Monk0e3aab22018-01-18 19:05:52 -0500177 mNavButtons.animate()
John Spurlock7edfbca2013-09-14 11:58:55 -0400178 .alpha(navButtonsAlpha)
179 .setDuration(duration)
180 .start();
John Spurlock7edfbca2013-09-14 11:58:55 -0400181 }
182 }
183
Jorim Jaggi40db0292016-06-27 17:58:03 -0700184 public void reapplyDarkIntensity() {
185 applyDarkIntensity(mLightTransitionsController.getCurrentDarkIntensity());
186 }
187
James O'Leary4335c702019-05-29 12:38:51 -0400188 @Override
Jorim Jaggi40db0292016-06-27 17:58:03 -0700189 public void applyDarkIntensity(float darkIntensity) {
190 SparseArray<ButtonDispatcher> buttonDispatchers = mView.getButtonDispatchers();
191 for (int i = buttonDispatchers.size() - 1; i >= 0; i--) {
192 buttonDispatchers.valueAt(i).setDarkIntensity(darkIntensity);
193 }
Tracy Zhou24fd0282019-05-20 14:40:38 -0700194 mView.getRotationButtonController().setDarkIntensity(darkIntensity);
James O'Leary4335c702019-05-29 12:38:51 -0400195 for (DarkIntensityListener listener : mDarkIntensityListeners) {
196 listener.onDarkIntensity(darkIntensity);
197 }
Jason Monkcdec5ff2017-11-08 15:51:59 -0500198 if (mAutoDim) {
199 applyLightsOut(false, true);
200 }
Jorim Jaggi40db0292016-06-27 17:58:03 -0700201 }
Winson Chunge89f4b52019-03-29 15:01:49 -0700202
203 @Override
204 public int getTintAnimationDuration() {
Vinit Nayak3f35db52019-08-08 17:31:48 -0700205 if (isGesturalModeOnDefaultDisplay(mView.getContext(), mNavBarMode)) {
Winson Chunge89f4b52019-03-29 15:01:49 -0700206 return Math.max(DEFAULT_COLOR_ADAPT_TRANSITION_TIME, MIN_COLOR_ADAPT_TRANSITION_TIME);
207 }
208 return LightBarTransitionsController.DEFAULT_TINT_ANIMATION_DURATION;
209 }
Winson Chung1aa24b92019-04-24 15:17:33 -0700210
211 public void onNavigationModeChanged(int mode) {
212 mNavBarMode = mode;
213 }
James O'Leary4335c702019-05-29 12:38:51 -0400214
215 /**
216 * Register {@code listener} to be notified when the color of nav bar elements changes.
Miranda Kephart2c464b82019-06-05 16:09:15 -0400217 *
218 * Returns the current nav bar color.
James O'Leary4335c702019-05-29 12:38:51 -0400219 */
Miranda Kephart2c464b82019-06-05 16:09:15 -0400220 public float addDarkIntensityListener(DarkIntensityListener listener) {
James O'Leary4335c702019-05-29 12:38:51 -0400221 mDarkIntensityListeners.add(listener);
Miranda Kephart2c464b82019-06-05 16:09:15 -0400222 return mLightTransitionsController.getCurrentDarkIntensity();
James O'Leary4335c702019-05-29 12:38:51 -0400223 }
224
225 /**
226 * Remove {@code listener} from being notified when the color of nav bar elements changes.
227 */
228 public void removeDarkIntensityListener(DarkIntensityListener listener) {
229 mDarkIntensityListeners.remove(listener);
230 }
Jim Milleraf638c42013-09-20 01:33:28 +0000231}