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; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 60 | private final IStatusBarService mBarService; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 61 | private final LightBarTransitionsController mLightTransitionsController; |
Matthew Ng | 3107d99 | 2017-11-21 15:56:19 -0800 | [diff] [blame] | 62 | private final boolean mAllowAutoDimWallpaperNotVisible; |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 63 | private boolean mWallpaperVisible; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 64 | |
| 65 | private boolean mLightsOut; |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 66 | private boolean mAutoDim; |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 67 | private View mNavButtons; |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 68 | private int mNavBarMode = NAV_BAR_MODE_3BUTTON; |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 69 | private List<DarkIntensityListener> mDarkIntensityListeners; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 70 | |
Winson Chung | b03d44d | 2018-09-18 12:42:45 -0700 | [diff] [blame] | 71 | private final Handler mHandler = Handler.getMain(); |
| 72 | private final IWallpaperVisibilityListener mWallpaperVisibilityListener = |
| 73 | new IWallpaperVisibilityListener.Stub() { |
| 74 | @Override |
| 75 | public void onWallpaperVisibilityChanged(boolean newVisibility, |
| 76 | int displayId) throws RemoteException { |
| 77 | mWallpaperVisible = newVisibility; |
| 78 | mHandler.post(() -> applyLightsOut(true, false)); |
| 79 | } |
| 80 | }; |
| 81 | |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 82 | public NavigationBarTransitions(NavigationBarView view, CommandQueue commandQueue) { |
John Spurlock | 7057d2c | 2013-10-02 10:00:37 -0400 | [diff] [blame] | 83 | super(view, R.drawable.nav_background); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 84 | mView = view; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 85 | mBarService = IStatusBarService.Stub.asInterface( |
| 86 | ServiceManager.getService(Context.STATUS_BAR_SERVICE)); |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 87 | mLightTransitionsController = new LightBarTransitionsController( |
| 88 | view.getContext(), this, commandQueue); |
Matthew Ng | 3107d99 | 2017-11-21 15:56:19 -0800 | [diff] [blame] | 89 | mAllowAutoDimWallpaperNotVisible = view.getContext().getResources() |
| 90 | .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] | 91 | mDarkIntensityListeners = new ArrayList(); |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 92 | |
| 93 | IWindowManager windowManagerService = Dependency.get(IWindowManager.class); |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 94 | try { |
| 95 | mWallpaperVisible = windowManagerService.registerWallpaperVisibilityListener( |
Winson Chung | b03d44d | 2018-09-18 12:42:45 -0700 | [diff] [blame] | 96 | mWallpaperVisibilityListener, Display.DEFAULT_DISPLAY); |
Jason Monk | 2044e6e | 2017-10-13 10:34:40 -0400 | [diff] [blame] | 97 | } catch (RemoteException e) { |
| 98 | } |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 99 | mView.addOnLayoutChangeListener( |
| 100 | (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { |
| 101 | View currentView = mView.getCurrentView(); |
| 102 | if (currentView != null) { |
| 103 | mNavButtons = currentView.findViewById(R.id.nav_buttons); |
| 104 | applyLightsOut(false, true); |
| 105 | } |
| 106 | }); |
| 107 | View currentView = mView.getCurrentView(); |
| 108 | if (currentView != null) { |
| 109 | mNavButtons = currentView.findViewById(R.id.nav_buttons); |
| 110 | } |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Adrian Roos | 8a8ffd4 | 2015-05-26 18:30:37 -0700 | [diff] [blame] | 113 | public void init() { |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 114 | applyModeBackground(-1, getMode(), false /*animate*/); |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 115 | applyLightsOut(false /*animate*/, true /*force*/); |
| 116 | } |
| 117 | |
| 118 | @Override |
Winson Chung | b03d44d | 2018-09-18 12:42:45 -0700 | [diff] [blame] | 119 | public void destroy() { |
| 120 | IWindowManager windowManagerService = Dependency.get(IWindowManager.class); |
| 121 | try { |
| 122 | windowManagerService.unregisterWallpaperVisibilityListener(mWallpaperVisibilityListener, |
| 123 | Display.DEFAULT_DISPLAY); |
| 124 | } catch (RemoteException e) { |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 129 | public void setAutoDim(boolean autoDim) { |
Winson Chung | ebeed02 | 2019-05-14 16:22:57 -0700 | [diff] [blame] | 130 | // 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] | 131 | if (autoDim && isGesturalModeOnDefaultDisplay(mView.getContext(), mNavBarMode)) return; |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 132 | if (mAutoDim == autoDim) return; |
| 133 | mAutoDim = autoDim; |
| 134 | applyLightsOut(true, false); |
| 135 | } |
| 136 | |
Winson Chung | 775d287 | 2019-03-27 14:14:01 -0700 | [diff] [blame] | 137 | void setBackgroundFrame(Rect frame) { |
| 138 | mBarBackground.setFrame(frame); |
| 139 | } |
| 140 | |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 141 | @Override |
| 142 | protected boolean isLightsOut(int mode) { |
Matthew Ng | 3107d99 | 2017-11-21 15:56:19 -0800 | [diff] [blame] | 143 | return super.isLightsOut(mode) || (mAllowAutoDimWallpaperNotVisible && mAutoDim |
| 144 | && !mWallpaperVisible && mode != MODE_WARNING); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 147 | public LightBarTransitionsController getLightTransitionsController() { |
| 148 | return mLightTransitionsController; |
| 149 | } |
| 150 | |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 151 | @Override |
| 152 | protected void onTransition(int oldMode, int newMode, boolean animate) { |
| 153 | super.onTransition(oldMode, newMode, animate); |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 154 | applyLightsOut(animate, false /*force*/); |
Winson Chung | 775d287 | 2019-03-27 14:14:01 -0700 | [diff] [blame] | 155 | mView.onBarTransition(newMode); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 158 | private void applyLightsOut(boolean animate, boolean force) { |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 159 | // apply to lights out |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 160 | applyLightsOut(isLightsOut(getMode()), animate, force); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 161 | } |
| 162 | |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 163 | private void applyLightsOut(boolean lightsOut, boolean animate, boolean force) { |
| 164 | if (!force && lightsOut == mLightsOut) return; |
| 165 | |
| 166 | mLightsOut = lightsOut; |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 167 | if (mNavButtons == null) return; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 168 | |
| 169 | // ok, everyone, stop it right there |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 170 | mNavButtons.animate().cancel(); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 171 | |
Jason Monk | cdec5ff | 2017-11-08 15:51:59 -0500 | [diff] [blame] | 172 | // Bump percentage by 10% if dark. |
| 173 | float darkBump = mLightTransitionsController.getCurrentDarkIntensity() / 10; |
| 174 | final float navButtonsAlpha = lightsOut ? 0.6f + darkBump : 1f; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 175 | |
| 176 | if (!animate) { |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 177 | mNavButtons.setAlpha(navButtonsAlpha); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 178 | } else { |
| 179 | final int duration = lightsOut ? LIGHTS_OUT_DURATION : LIGHTS_IN_DURATION; |
Jason Monk | 0e3aab2 | 2018-01-18 19:05:52 -0500 | [diff] [blame] | 180 | mNavButtons.animate() |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 181 | .alpha(navButtonsAlpha) |
| 182 | .setDuration(duration) |
| 183 | .start(); |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 187 | public void reapplyDarkIntensity() { |
| 188 | applyDarkIntensity(mLightTransitionsController.getCurrentDarkIntensity()); |
| 189 | } |
| 190 | |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 191 | @Override |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 192 | public void applyDarkIntensity(float darkIntensity) { |
| 193 | SparseArray<ButtonDispatcher> buttonDispatchers = mView.getButtonDispatchers(); |
| 194 | for (int i = buttonDispatchers.size() - 1; i >= 0; i--) { |
| 195 | buttonDispatchers.valueAt(i).setDarkIntensity(darkIntensity); |
| 196 | } |
Tracy Zhou | 24fd028 | 2019-05-20 14:40:38 -0700 | [diff] [blame] | 197 | mView.getRotationButtonController().setDarkIntensity(darkIntensity); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 198 | for (DarkIntensityListener listener : mDarkIntensityListeners) { |
| 199 | listener.onDarkIntensity(darkIntensity); |
| 200 | } |
Jason Monk | cdec5ff | 2017-11-08 15:51:59 -0500 | [diff] [blame] | 201 | if (mAutoDim) { |
| 202 | applyLightsOut(false, true); |
| 203 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 204 | } |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 205 | |
| 206 | @Override |
| 207 | public int getTintAnimationDuration() { |
Vinit Nayak | 3f35db5 | 2019-08-08 17:31:48 -0700 | [diff] [blame] | 208 | if (isGesturalModeOnDefaultDisplay(mView.getContext(), mNavBarMode)) { |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 209 | return Math.max(DEFAULT_COLOR_ADAPT_TRANSITION_TIME, MIN_COLOR_ADAPT_TRANSITION_TIME); |
| 210 | } |
| 211 | return LightBarTransitionsController.DEFAULT_TINT_ANIMATION_DURATION; |
| 212 | } |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 213 | |
| 214 | public void onNavigationModeChanged(int mode) { |
| 215 | mNavBarMode = mode; |
| 216 | } |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * 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] | 220 | * |
| 221 | * Returns the current nav bar color. |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 222 | */ |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 223 | public float addDarkIntensityListener(DarkIntensityListener listener) { |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 224 | mDarkIntensityListeners.add(listener); |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 225 | return mLightTransitionsController.getCurrentDarkIntensity(); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Remove {@code listener} from being notified when the color of nav bar elements changes. |
| 230 | */ |
| 231 | public void removeDarkIntensityListener(DarkIntensityListener listener) { |
| 232 | mDarkIntensityListeners.remove(listener); |
| 233 | } |
Jim Miller | af638c4 | 2013-09-20 01:33:28 +0000 | [diff] [blame] | 234 | } |