Craig Mautner | c2c0a61 | 2014-02-20 20:25:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | */ |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DIM_LAYER; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 20 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 25 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 26 | import android.graphics.PixelFormat; |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 28 | import android.os.SystemClock; |
| 29 | import android.util.Slog; |
| 30 | import android.view.DisplayInfo; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 31 | import android.view.SurfaceControl; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 32 | |
| 33 | import java.io.PrintWriter; |
| 34 | |
| 35 | public class DimLayer { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 36 | private static final String TAG = TAG_WITH_CLASS_NAME ? "DimLayer" : TAG_WM; |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 37 | private final WindowManagerService mService; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 38 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 39 | /** Actual surface that dims */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 40 | private SurfaceControl mDimSurface; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 41 | |
| 42 | /** Last value passed to mDimSurface.setAlpha() */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 43 | private float mAlpha = 0; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 44 | |
| 45 | /** Last value passed to mDimSurface.setLayer() */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 46 | private int mLayer = -1; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 47 | |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 48 | /** Next values to pass to mDimSurface.setPosition() and mDimSurface.setSize() */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 49 | private final Rect mBounds = new Rect(); |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 50 | |
| 51 | /** Last values passed to mDimSurface.setPosition() and mDimSurface.setSize() */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 52 | private final Rect mLastBounds = new Rect(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 53 | |
| 54 | /** True after mDimSurface.show() has been called, false after mDimSurface.hide(). */ |
| 55 | private boolean mShowing = false; |
| 56 | |
| 57 | /** Value of mAlpha when beginning transition to mTargetAlpha */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 58 | private float mStartAlpha = 0; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 59 | |
| 60 | /** Final value of mAlpha following transition */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 61 | private float mTargetAlpha = 0; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 62 | |
| 63 | /** Time in units of SystemClock.uptimeMillis() at which the current transition started */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 64 | private long mStartTime; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 65 | |
| 66 | /** Time in milliseconds to take to transition from mStartAlpha to mTargetAlpha */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 67 | private long mDuration; |
| 68 | |
| 69 | private boolean mDestroyed = false; |
| 70 | |
| 71 | private final int mDisplayId; |
| 72 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 73 | |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 74 | /** Interface implemented by users of the dim layer */ |
| 75 | interface DimLayerUser { |
Wale Ogunwale | 29bfbb8 | 2016-05-12 15:13:52 -0700 | [diff] [blame] | 76 | /** Returns true if the dim should be fullscreen. */ |
| 77 | boolean dimFullscreen(); |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 78 | /** Returns the display info. of the dim layer user. */ |
| 79 | DisplayInfo getDisplayInfo(); |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 80 | /** Gets the bounds of the dim layer user. */ |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 81 | void getDimBounds(Rect outBounds); |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 82 | String toShortString(); |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 83 | } |
| 84 | /** The user of this dim layer. */ |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 85 | private final DimLayerUser mUser; |
Craig Mautner | c7b8a10 | 2013-10-02 16:49:52 -0700 | [diff] [blame] | 86 | |
Jorim Jaggi | bc5425c | 2016-03-01 13:51:16 +0100 | [diff] [blame] | 87 | private final String mName; |
| 88 | |
| 89 | DimLayer(WindowManagerService service, DimLayerUser user, int displayId, String name) { |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 90 | mUser = user; |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 91 | mDisplayId = displayId; |
| 92 | mService = service; |
Jorim Jaggi | bc5425c | 2016-03-01 13:51:16 +0100 | [diff] [blame] | 93 | mName = name; |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 94 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "Ctor: displayId=" + displayId); |
| 95 | } |
| 96 | |
| 97 | private void constructSurface(WindowManagerService service) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 98 | SurfaceControl.openTransaction(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 99 | try { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 100 | if (DEBUG_SURFACE_TRACE) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 101 | mDimSurface = new WindowSurfaceController.SurfaceTrace(service.mFxSession, |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 102 | "DimSurface", |
| 103 | 16, 16, PixelFormat.OPAQUE, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 104 | SurfaceControl.FX_SURFACE_DIM | SurfaceControl.HIDDEN); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 105 | } else { |
Jorim Jaggi | bc5425c | 2016-03-01 13:51:16 +0100 | [diff] [blame] | 106 | mDimSurface = new SurfaceControl(service.mFxSession, mName, |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 107 | 16, 16, PixelFormat.OPAQUE, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 108 | SurfaceControl.FX_SURFACE_DIM | SurfaceControl.HIDDEN); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 109 | } |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 110 | if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG, |
| 111 | " DIM " + mDimSurface + ": CREATE"); |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 112 | mDimSurface.setLayerStack(mDisplayId); |
| 113 | adjustBounds(); |
| 114 | adjustAlpha(mAlpha); |
| 115 | adjustLayer(mLayer); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 116 | } catch (Exception e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 117 | Slog.e(TAG_WM, "Exception creating Dim surface", e); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 118 | } finally { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 119 | SurfaceControl.closeTransaction(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /** Return true if dim layer is showing */ |
| 124 | boolean isDimming() { |
| 125 | return mTargetAlpha != 0; |
| 126 | } |
| 127 | |
| 128 | /** Return true if in a transition period */ |
| 129 | boolean isAnimating() { |
| 130 | return mTargetAlpha != mAlpha; |
| 131 | } |
| 132 | |
| 133 | float getTargetAlpha() { |
| 134 | return mTargetAlpha; |
| 135 | } |
| 136 | |
Craig Mautner | 13131e7 | 2013-01-11 11:03:33 -0800 | [diff] [blame] | 137 | void setLayer(int layer) { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 138 | if (mLayer == layer) { |
| 139 | return; |
| 140 | } |
| 141 | mLayer = layer; |
| 142 | adjustLayer(layer); |
| 143 | } |
| 144 | |
| 145 | private void adjustLayer(int layer) { |
| 146 | if (mDimSurface != null) { |
Craig Mautner | 13131e7 | 2013-01-11 11:03:33 -0800 | [diff] [blame] | 147 | mDimSurface.setLayer(layer); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int getLayer() { |
| 152 | return mLayer; |
| 153 | } |
| 154 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 155 | private void setAlpha(float alpha) { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 156 | if (mAlpha == alpha) { |
| 157 | return; |
| 158 | } |
| 159 | mAlpha = alpha; |
| 160 | adjustAlpha(alpha); |
| 161 | } |
| 162 | |
| 163 | private void adjustAlpha(float alpha) { |
| 164 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "setAlpha alpha=" + alpha); |
| 165 | try { |
| 166 | if (mDimSurface != null) { |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 167 | mDimSurface.setAlpha(alpha); |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 168 | } |
| 169 | if (alpha == 0 && mShowing) { |
| 170 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "setAlpha hiding"); |
| 171 | if (mDimSurface != null) { |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 172 | mDimSurface.hide(); |
| 173 | mShowing = false; |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 174 | } |
| 175 | } else if (alpha > 0 && !mShowing) { |
| 176 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "setAlpha showing"); |
| 177 | if (mDimSurface != null) { |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 178 | mDimSurface.show(); |
| 179 | mShowing = true; |
| 180 | } |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 181 | } |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 182 | } catch (RuntimeException e) { |
| 183 | Slog.w(TAG, "Failure setting alpha immediately", e); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 187 | /** |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 188 | * NOTE: Must be called with Surface transaction open. |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 189 | */ |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 190 | private void adjustBounds() { |
Wale Ogunwale | 29bfbb8 | 2016-05-12 15:13:52 -0700 | [diff] [blame] | 191 | if (mUser.dimFullscreen()) { |
| 192 | getBoundsForFullscreen(mBounds); |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 195 | if (mDimSurface != null) { |
Wale Ogunwale | 29bfbb8 | 2016-05-12 15:13:52 -0700 | [diff] [blame] | 196 | mDimSurface.setPosition(mBounds.left, mBounds.top); |
| 197 | mDimSurface.setSize(mBounds.width(), mBounds.height()); |
| 198 | if (DEBUG_DIM_LAYER) Slog.v(TAG, |
| 199 | "adjustBounds user=" + mUser.toShortString() + " mBounds=" + mBounds); |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 200 | } |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 201 | |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 202 | mLastBounds.set(mBounds); |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Wale Ogunwale | 29bfbb8 | 2016-05-12 15:13:52 -0700 | [diff] [blame] | 205 | private void getBoundsForFullscreen(Rect outBounds) { |
| 206 | final int dw, dh; |
| 207 | final float xPos, yPos; |
| 208 | // Set surface size to screen size. |
| 209 | final DisplayInfo info = mUser.getDisplayInfo(); |
| 210 | // Multiply by 1.5 so that rotating a frozen surface that includes this does not expose |
| 211 | // a corner. |
| 212 | dw = (int) (info.logicalWidth * 1.5); |
| 213 | dh = (int) (info.logicalHeight * 1.5); |
| 214 | // back off position so 1/4 of Surface is before and 1/4 is after. |
| 215 | xPos = -1 * dw / 6; |
| 216 | yPos = -1 * dh / 6; |
| 217 | outBounds.set((int) xPos, (int) yPos, (int) xPos + dw, (int) yPos + dh); |
| 218 | } |
| 219 | |
| 220 | void setBoundsForFullscreen() { |
| 221 | getBoundsForFullscreen(mBounds); |
| 222 | setBounds(mBounds); |
| 223 | } |
| 224 | |
Wale Ogunwale | 0ade61b | 2015-03-24 10:40:32 -0700 | [diff] [blame] | 225 | /** @param bounds The new bounds to set */ |
| 226 | void setBounds(Rect bounds) { |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 227 | mBounds.set(bounds); |
Craig Mautner | b660b9d | 2014-02-13 10:59:16 -0800 | [diff] [blame] | 228 | if (isDimming() && !mLastBounds.equals(bounds)) { |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 229 | try { |
Wale Ogunwale | 0ade61b | 2015-03-24 10:40:32 -0700 | [diff] [blame] | 230 | SurfaceControl.openTransaction(); |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 231 | adjustBounds(); |
| 232 | } catch (RuntimeException e) { |
| 233 | Slog.w(TAG, "Failure setting size", e); |
| 234 | } finally { |
Wale Ogunwale | 0ade61b | 2015-03-24 10:40:32 -0700 | [diff] [blame] | 235 | SurfaceControl.closeTransaction(); |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 236 | } |
Craig Mautner | b660b9d | 2014-02-13 10:59:16 -0800 | [diff] [blame] | 237 | } |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 240 | /** |
| 241 | * @param duration The time to test. |
| 242 | * @return True if the duration would lead to an earlier end to the current animation. |
| 243 | */ |
| 244 | private boolean durationEndsEarlier(long duration) { |
| 245 | return SystemClock.uptimeMillis() + duration < mStartTime + mDuration; |
| 246 | } |
| 247 | |
| 248 | /** Jump to the end of the animation. |
| 249 | * NOTE: Must be called with Surface transaction open. */ |
| 250 | void show() { |
| 251 | if (isAnimating()) { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 252 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "show: immediate"); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 253 | show(mLayer, mTargetAlpha, 0); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Begin an animation to a new dim value. |
| 259 | * NOTE: Must be called with Surface transaction open. |
| 260 | * |
| 261 | * @param layer The layer to set the surface to. |
| 262 | * @param alpha The dim value to end at. |
| 263 | * @param duration How long to take to get there in milliseconds. |
| 264 | */ |
| 265 | void show(int layer, float alpha, long duration) { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 266 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "show: layer=" + layer + " alpha=" + alpha |
| 267 | + " duration=" + duration + ", mDestroyed=" + mDestroyed); |
| 268 | if (mDestroyed) { |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 269 | Slog.e(TAG, "show: no Surface"); |
| 270 | // Make sure isAnimating() returns false. |
| 271 | mTargetAlpha = mAlpha = 0; |
| 272 | return; |
| 273 | } |
| 274 | |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 275 | if (mDimSurface == null) { |
| 276 | constructSurface(mService); |
| 277 | } |
| 278 | |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 279 | if (!mLastBounds.equals(mBounds)) { |
| 280 | adjustBounds(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 281 | } |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 282 | setLayer(layer); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 283 | |
| 284 | long curTime = SystemClock.uptimeMillis(); |
| 285 | final boolean animating = isAnimating(); |
| 286 | if ((animating && (mTargetAlpha != alpha || durationEndsEarlier(duration))) |
| 287 | || (!animating && mAlpha != alpha)) { |
| 288 | if (duration <= 0) { |
| 289 | // No animation required, just set values. |
| 290 | setAlpha(alpha); |
| 291 | } else { |
| 292 | // Start or continue animation with new parameters. |
| 293 | mStartAlpha = mAlpha; |
| 294 | mStartTime = curTime; |
| 295 | mDuration = duration; |
| 296 | } |
| 297 | } |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 298 | mTargetAlpha = alpha; |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 299 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "show: mStartAlpha=" + mStartAlpha + " mStartTime=" |
| 300 | + mStartTime + " mTargetAlpha=" + mTargetAlpha); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** Immediate hide. |
| 304 | * NOTE: Must be called with Surface transaction open. */ |
| 305 | void hide() { |
| 306 | if (mShowing) { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 307 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "hide: immediate"); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 308 | hide(0); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Gradually fade to transparent. |
| 314 | * NOTE: Must be called with Surface transaction open. |
| 315 | * |
| 316 | * @param duration Time to fade in milliseconds. |
| 317 | */ |
| 318 | void hide(long duration) { |
| 319 | if (mShowing && (mTargetAlpha != 0 || durationEndsEarlier(duration))) { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 320 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "hide: duration=" + duration); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 321 | show(mLayer, 0, duration); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Advance the dimming per the last #show(int, float, long) call. |
| 327 | * NOTE: Must be called with Surface transaction open. |
| 328 | * |
| 329 | * @return True if animation is still required after this step. |
| 330 | */ |
| 331 | boolean stepAnimation() { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 332 | if (mDestroyed) { |
| 333 | Slog.e(TAG, "stepAnimation: surface destroyed"); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 334 | // Ensure that isAnimating() returns false; |
| 335 | mTargetAlpha = mAlpha = 0; |
| 336 | return false; |
| 337 | } |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 338 | if (isAnimating()) { |
| 339 | final long curTime = SystemClock.uptimeMillis(); |
| 340 | final float alphaDelta = mTargetAlpha - mStartAlpha; |
| 341 | float alpha = mStartAlpha + alphaDelta * (curTime - mStartTime) / mDuration; |
| 342 | if (alphaDelta > 0 && alpha > mTargetAlpha || |
| 343 | alphaDelta < 0 && alpha < mTargetAlpha) { |
| 344 | // Don't exceed limits. |
| 345 | alpha = mTargetAlpha; |
| 346 | } |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 347 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "stepAnimation: curTime=" + curTime + " alpha=" + alpha); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 348 | setAlpha(alpha); |
| 349 | } |
| 350 | |
| 351 | return isAnimating(); |
| 352 | } |
| 353 | |
| 354 | /** Cleanup */ |
| 355 | void destroySurface() { |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 356 | if (DEBUG_DIM_LAYER) Slog.v(TAG, "destroySurface."); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 357 | if (mDimSurface != null) { |
| 358 | mDimSurface.destroy(); |
| 359 | mDimSurface = null; |
| 360 | } |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 361 | mDestroyed = true; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | public void printTo(String prefix, PrintWriter pw) { |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 365 | pw.print(prefix); pw.print("mDimSurface="); pw.print(mDimSurface); |
| 366 | pw.print(" mLayer="); pw.print(mLayer); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 367 | pw.print(" mAlpha="); pw.println(mAlpha); |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 368 | pw.print(prefix); pw.print("mLastBounds="); pw.print(mLastBounds.toShortString()); |
| 369 | pw.print(" mBounds="); pw.println(mBounds.toShortString()); |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 370 | pw.print(prefix); pw.print("Last animation: "); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 371 | pw.print(" mDuration="); pw.print(mDuration); |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 372 | pw.print(" mStartTime="); pw.print(mStartTime); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 373 | pw.print(" curTime="); pw.println(SystemClock.uptimeMillis()); |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 374 | pw.print(prefix); pw.print(" mStartAlpha="); pw.print(mStartAlpha); |
| 375 | pw.print(" mTargetAlpha="); pw.println(mTargetAlpha); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 376 | } |
| 377 | } |