John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 19 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON; |
| 20 | |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 21 | import static com.android.systemui.util.Utils.isGesturalModeOnDefaultDisplay; |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 22 | |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 23 | import android.content.Context; |
Winson Chung | 775d287 | 2019-03-27 14:14:01 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.RemoteException; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 27 | import android.os.ServiceManager; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 28 | import android.util.SparseArray; |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 29 | import android.view.Display; |
| 30 | import android.view.IWallpaperVisibilityListener; |
| 31 | import android.view.IWindowManager; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 32 | import android.view.View; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.statusbar.IStatusBarService; |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 35 | import com.android.systemui.Dependency; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 36 | import com.android.systemui.R; |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 37 | import com.android.systemui.statusbar.CommandQueue; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 38 | |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
| 41 | |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 42 | public final class NavigationBarTransitions extends BarTransitions implements |
| 43 | LightBarTransitionsController.DarkIntensityApplier { |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 44 | |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 45 | public static final int MIN_COLOR_ADAPT_TRANSITION_TIME = 400; |
| 46 | public static final int DEFAULT_COLOR_ADAPT_TRANSITION_TIME = 1700; |
| 47 | |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 48 | /** |
| 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 Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 59 | private final NavigationBarView mView; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 60 | private final LightBarTransitionsController mLightTransitionsController; |
Matthew Ng | 3107d99 | 2017-11-21 15:56:19 -0800 | [diff] [blame] | 61 | private final boolean mAllowAutoDimWallpaperNotVisible; |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 62 | private boolean mWallpaperVisible; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 63 | |
| 64 | private boolean mLightsOut; |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 65 | private boolean mAutoDim; |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 66 | private View mNavButtons; |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 67 | private int mNavBarMode = NAV_BAR_MODE_3BUTTON; |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 68 | private List<DarkIntensityListener> mDarkIntensityListeners; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 69 | |
Winson Chung | b03d44d | 2018-09-18 12:42:45 -0700 | [diff] [blame] | 70 | 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 Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 81 | public NavigationBarTransitions(NavigationBarView view, CommandQueue commandQueue) { |
John Spurlock | 7057d2c | 2013-10-02 10:00:37 -0400 | [diff] [blame] | 82 | super(view, R.drawable.nav_background); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 83 | mView = view; |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 84 | mLightTransitionsController = new LightBarTransitionsController( |
| 85 | view.getContext(), this, commandQueue); |
Matthew Ng | 3107d99 | 2017-11-21 15:56:19 -0800 | [diff] [blame] | 86 | mAllowAutoDimWallpaperNotVisible = view.getContext().getResources() |
| 87 | .getBoolean(R.bool.config_navigation_bar_enable_auto_dim_no_visible_wallpaper); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 88 | mDarkIntensityListeners = new ArrayList(); |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 89 | |
| 90 | IWindowManager windowManagerService = Dependency.get(IWindowManager.class); |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 91 | try { |
| 92 | mWallpaperVisible = windowManagerService.registerWallpaperVisibilityListener( |
Winson Chung | b03d44d | 2018-09-18 12:42:45 -0700 | [diff] [blame] | 93 | mWallpaperVisibilityListener, Display.DEFAULT_DISPLAY); |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 94 | } catch (RemoteException e) { |
| 95 | } |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 96 | 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 Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Adrian Roos | 8a8ffd4 | 2015-05-26 18:30:37 -0700 | [diff] [blame] | 110 | public void init() { |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 111 | applyModeBackground(-1, getMode(), false /*animate*/); |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 112 | applyLightsOut(false /*animate*/, true /*force*/); |
| 113 | } |
| 114 | |
| 115 | @Override |
Winson Chung | b03d44d | 2018-09-18 12:42:45 -0700 | [diff] [blame] | 116 | 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 Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 126 | public void setAutoDim(boolean autoDim) { |
Winson Chung | ebeed02 | 2019-05-14 16:22:57 -0700 | [diff] [blame] | 127 | // Ensure we aren't in gestural nav if we are triggering auto dim |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 128 | if (autoDim && isGesturalModeOnDefaultDisplay(mView.getContext(), mNavBarMode)) return; |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 129 | if (mAutoDim == autoDim) return; |
| 130 | mAutoDim = autoDim; |
| 131 | applyLightsOut(true, false); |
| 132 | } |
| 133 | |
Winson Chung | 775d287 | 2019-03-27 14:14:01 -0700 | [diff] [blame] | 134 | void setBackgroundFrame(Rect frame) { |
| 135 | mBarBackground.setFrame(frame); |
| 136 | } |
| 137 | |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 138 | @Override |
| 139 | protected boolean isLightsOut(int mode) { |
Matthew Ng | 3107d99 | 2017-11-21 15:56:19 -0800 | [diff] [blame] | 140 | return super.isLightsOut(mode) || (mAllowAutoDimWallpaperNotVisible && mAutoDim |
| 141 | && !mWallpaperVisible && mode != MODE_WARNING); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 144 | public LightBarTransitionsController getLightTransitionsController() { |
| 145 | return mLightTransitionsController; |
| 146 | } |
| 147 | |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 148 | @Override |
| 149 | protected void onTransition(int oldMode, int newMode, boolean animate) { |
| 150 | super.onTransition(oldMode, newMode, animate); |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 151 | applyLightsOut(animate, false /*force*/); |
Winson Chung | 775d287 | 2019-03-27 14:14:01 -0700 | [diff] [blame] | 152 | mView.onBarTransition(newMode); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 155 | private void applyLightsOut(boolean animate, boolean force) { |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 156 | // apply to lights out |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 157 | applyLightsOut(isLightsOut(getMode()), animate, force); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 158 | } |
| 159 | |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 160 | private void applyLightsOut(boolean lightsOut, boolean animate, boolean force) { |
| 161 | if (!force && lightsOut == mLightsOut) return; |
| 162 | |
| 163 | mLightsOut = lightsOut; |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 164 | if (mNavButtons == null) return; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 165 | |
| 166 | // ok, everyone, stop it right there |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 167 | mNavButtons.animate().cancel(); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 168 | |
Jason Monk | cdec5ff | 2017-11-08 15:51:59 -0500 | [diff] [blame] | 169 | // Bump percentage by 10% if dark. |
| 170 | float darkBump = mLightTransitionsController.getCurrentDarkIntensity() / 10; |
| 171 | final float navButtonsAlpha = lightsOut ? 0.6f + darkBump : 1f; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 172 | |
| 173 | if (!animate) { |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 174 | mNavButtons.setAlpha(navButtonsAlpha); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 175 | } else { |
| 176 | final int duration = lightsOut ? LIGHTS_OUT_DURATION : LIGHTS_IN_DURATION; |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 177 | mNavButtons.animate() |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 178 | .alpha(navButtonsAlpha) |
| 179 | .setDuration(duration) |
| 180 | .start(); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 184 | public void reapplyDarkIntensity() { |
| 185 | applyDarkIntensity(mLightTransitionsController.getCurrentDarkIntensity()); |
| 186 | } |
| 187 | |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 188 | @Override |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 189 | 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 Zhou | 24fd028 | 2019-05-20 14:40:38 -0700 | [diff] [blame] | 194 | mView.getRotationButtonController().setDarkIntensity(darkIntensity); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 195 | for (DarkIntensityListener listener : mDarkIntensityListeners) { |
| 196 | listener.onDarkIntensity(darkIntensity); |
| 197 | } |
Jason Monk | cdec5ff | 2017-11-08 15:51:59 -0500 | [diff] [blame] | 198 | if (mAutoDim) { |
| 199 | applyLightsOut(false, true); |
| 200 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 201 | } |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 202 | |
| 203 | @Override |
| 204 | public int getTintAnimationDuration() { |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 205 | if (isGesturalModeOnDefaultDisplay(mView.getContext(), mNavBarMode)) { |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 206 | return Math.max(DEFAULT_COLOR_ADAPT_TRANSITION_TIME, MIN_COLOR_ADAPT_TRANSITION_TIME); |
| 207 | } |
| 208 | return LightBarTransitionsController.DEFAULT_TINT_ANIMATION_DURATION; |
| 209 | } |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 210 | |
| 211 | public void onNavigationModeChanged(int mode) { |
| 212 | mNavBarMode = mode; |
| 213 | } |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Register {@code listener} to be notified when the color of nav bar elements changes. |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 217 | * |
| 218 | * Returns the current nav bar color. |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 219 | */ |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 220 | public float addDarkIntensityListener(DarkIntensityListener listener) { |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 221 | mDarkIntensityListeners.add(listener); |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 222 | return mLightTransitionsController.getCurrentDarkIntensity(); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 223 | } |
| 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 Miller | af638c4 | 2013-09-20 01:33:28 +0000 | [diff] [blame] | 231 | } |