Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.wm; |
| 18 | |
Kweku Adams | 21b8d26 | 2018-03-30 12:19:58 -0700 | [diff] [blame] | 19 | import static com.android.server.wm.AlphaAnimationSpecProto.DURATION_MS; |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 20 | import static com.android.server.wm.AlphaAnimationSpecProto.FROM; |
| 21 | import static com.android.server.wm.AlphaAnimationSpecProto.TO; |
| 22 | import static com.android.server.wm.AnimationSpecProto.ALPHA; |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 23 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 25 | import android.util.Log; |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 26 | import android.util.proto.ProtoOutputStream; |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 27 | import android.view.Surface; |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 28 | import android.view.SurfaceControl; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 29 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
| 31 | |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 32 | import java.io.PrintWriter; |
| 33 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Utility class for use by a WindowContainer implementation to add "DimLayer" support, that is |
| 36 | * black layers of varying opacity at various Z-levels which create the effect of a Dim. |
| 37 | */ |
| 38 | class Dimmer { |
| 39 | private static final String TAG = "WindowManager"; |
Kweku Adams | 21b8d26 | 2018-03-30 12:19:58 -0700 | [diff] [blame] | 40 | // This is in milliseconds. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 41 | private static final int DEFAULT_DIM_ANIM_DURATION = 200; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 42 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 43 | private class DimAnimatable implements SurfaceAnimator.Animatable { |
| 44 | private final SurfaceControl mDimLayer; |
| 45 | |
| 46 | private DimAnimatable(SurfaceControl dimLayer) { |
| 47 | mDimLayer = dimLayer; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public SurfaceControl.Transaction getPendingTransaction() { |
| 52 | return mHost.getPendingTransaction(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void commitPendingTransaction() { |
| 57 | mHost.commitPendingTransaction(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onAnimationLeashCreated(SurfaceControl.Transaction t, SurfaceControl leash) { |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onAnimationLeashDestroyed(SurfaceControl.Transaction t) { |
| 66 | } |
| 67 | |
| 68 | @Override |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 69 | public SurfaceControl.Builder makeAnimationLeash() { |
| 70 | return mHost.makeAnimationLeash(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public SurfaceControl getAnimationLeashParent() { |
| 75 | return mHost.getSurfaceControl(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public SurfaceControl getSurfaceControl() { |
| 80 | return mDimLayer; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public SurfaceControl getParentSurfaceControl() { |
| 85 | return mHost.getSurfaceControl(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int getSurfaceWidth() { |
| 90 | // This will determine the size of the leash created. This should be the size of the |
| 91 | // host and not the dim layer since the dim layer may get bigger during animation. If |
| 92 | // that occurs, the leash size cannot change so we need to ensure the leash is big |
| 93 | // enough that the dim layer can grow. |
| 94 | // This works because the mHost will be a Task which has the display bounds. |
| 95 | return mHost.getSurfaceWidth(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int getSurfaceHeight() { |
| 100 | // See getSurfaceWidth() above for explanation. |
| 101 | return mHost.getSurfaceHeight(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @VisibleForTesting |
| 106 | class DimState { |
| 107 | /** |
| 108 | * The layer where property changes should be invoked on. |
| 109 | */ |
| 110 | SurfaceControl mDimLayer; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 111 | boolean mDimming; |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 112 | boolean isVisible; |
| 113 | SurfaceAnimator mSurfaceAnimator; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
chaviw | 87ca63a | 2018-03-26 14:06:17 -0700 | [diff] [blame] | 116 | * Determines whether the dim layer should animate before destroying. |
| 117 | */ |
| 118 | boolean mAnimateExit = true; |
| 119 | |
| 120 | /** |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 121 | * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 122 | * details on Dim lifecycle. |
| 123 | */ |
| 124 | boolean mDontReset; |
| 125 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 126 | DimState(SurfaceControl dimLayer) { |
| 127 | mDimLayer = dimLayer; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 128 | mDimming = true; |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 129 | mSurfaceAnimator = new SurfaceAnimator(new DimAnimatable(dimLayer), () -> { |
| 130 | if (!mDimming) { |
| 131 | mDimLayer.destroy(); |
| 132 | } |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 133 | }, mHost.mService); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 134 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 135 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 136 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 137 | /** |
| 138 | * The {@link WindowContainer} that our Dim's are bounded to. We may be dimming on behalf of the |
| 139 | * host, some controller of it, or one of the hosts children. |
| 140 | */ |
| 141 | private WindowContainer mHost; |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 142 | private WindowContainer mLastRequestedDimContainer; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 143 | @VisibleForTesting |
| 144 | DimState mDimState; |
| 145 | |
| 146 | private final SurfaceAnimatorStarter mSurfaceAnimatorStarter; |
| 147 | |
| 148 | @VisibleForTesting |
| 149 | interface SurfaceAnimatorStarter { |
| 150 | void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t, |
| 151 | AnimationAdapter anim, boolean hidden); |
| 152 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 153 | |
| 154 | Dimmer(WindowContainer host) { |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 155 | this(host, SurfaceAnimator::startAnimation); |
| 156 | } |
| 157 | |
| 158 | Dimmer(WindowContainer host, SurfaceAnimatorStarter surfaceAnimatorStarter) { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 159 | mHost = host; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 160 | mSurfaceAnimatorStarter = surfaceAnimatorStarter; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 163 | private SurfaceControl makeDimLayer() { |
| 164 | return mHost.makeChildSurface(null) |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 165 | .setParent(mHost.getSurfaceControl()) |
| 166 | .setColorLayer(true) |
| 167 | .setName("Dim Layer for - " + mHost.getName()) |
| 168 | .build(); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 172 | * Retrieve the DimState, creating one if it doesn't exist. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 173 | */ |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 174 | private DimState getDimState(WindowContainer container) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 175 | if (mDimState == null) { |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 176 | try { |
| 177 | final SurfaceControl ctl = makeDimLayer(); |
| 178 | mDimState = new DimState(ctl); |
| 179 | /** |
| 180 | * See documentation on {@link #dimAbove} to understand lifecycle management of |
| 181 | * Dim's via state resetting for Dim's with containers. |
| 182 | */ |
| 183 | if (container == null) { |
| 184 | mDimState.mDontReset = true; |
| 185 | } |
| 186 | } catch (Surface.OutOfResourcesException e) { |
| 187 | Log.w(TAG, "OutOfResourcesException creating dim surface"); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 188 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 189 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 190 | |
| 191 | mLastRequestedDimContainer = container; |
| 192 | return mDimState; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private void dim(SurfaceControl.Transaction t, WindowContainer container, int relativeLayer, |
| 196 | float alpha) { |
| 197 | final DimState d = getDimState(container); |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 198 | |
| 199 | if (d == null) { |
| 200 | return; |
| 201 | } |
| 202 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 203 | if (container != null) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 204 | // The dim method is called from WindowState.prepareSurfaces(), which is always called |
| 205 | // in the correct Z from lowest Z to highest. This ensures that the dim layer is always |
| 206 | // relative to the highest Z layer with a dim. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 207 | t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 208 | } else { |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 209 | t.setLayer(d.mDimLayer, Integer.MAX_VALUE); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 210 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 211 | t.setAlpha(d.mDimLayer, alpha); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 212 | |
| 213 | d.mDimming = true; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Finish a dim started by dimAbove in the case there was no call to dimAbove. |
| 218 | * |
| 219 | * @param t A Transaction in which to finish the dim. |
| 220 | */ |
| 221 | void stopDim(SurfaceControl.Transaction t) { |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 222 | if (mDimState != null) { |
| 223 | t.hide(mDimState.mDimLayer); |
| 224 | mDimState.isVisible = false; |
| 225 | mDimState.mDontReset = false; |
| 226 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 227 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 228 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Place a Dim above the entire host container. The caller is responsible for calling stopDim to |
| 231 | * remove this effect. If the Dim can be assosciated with a particular child of the host |
| 232 | * consider using the other variant of dimAbove which ties the Dim lifetime to the child |
| 233 | * lifetime more explicitly. |
| 234 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 235 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 236 | * @param alpha The alpha at which to Dim. |
| 237 | */ |
| 238 | void dimAbove(SurfaceControl.Transaction t, float alpha) { |
| 239 | dim(t, null, 1, alpha); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Place a dim above the given container, which should be a child of the host container. |
| 244 | * for each call to {@link WindowContainer#prepareSurfaces} the Dim state will be reset |
| 245 | * and the child should call dimAbove again to request the Dim to continue. |
| 246 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 247 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 248 | * @param container The container which to dim above. Should be a child of our host. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 249 | * @param alpha The alpha at which to Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 250 | */ |
| 251 | void dimAbove(SurfaceControl.Transaction t, WindowContainer container, float alpha) { |
| 252 | dim(t, container, 1, alpha); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Like {@link #dimAbove} but places the dim below the given container. |
| 257 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 258 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 259 | * @param container The container which to dim below. Should be a child of our host. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 260 | * @param alpha The alpha at which to Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 261 | */ |
| 262 | |
| 263 | void dimBelow(SurfaceControl.Transaction t, WindowContainer container, float alpha) { |
| 264 | dim(t, container, -1, alpha); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Mark all dims as pending completion on the next call to {@link #updateDims} |
| 269 | * |
| 270 | * This is intended for us by the host container, to be called at the beginning of |
| 271 | * {@link WindowContainer#prepareSurfaces}. After calling this, the container should |
| 272 | * chain {@link WindowContainer#prepareSurfaces} down to it's children to give them |
| 273 | * a chance to request dims to continue. |
| 274 | */ |
| 275 | void resetDimStates() { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 276 | if (mDimState != null && !mDimState.mDontReset) { |
| 277 | mDimState.mDimming = false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
chaviw | 87ca63a | 2018-03-26 14:06:17 -0700 | [diff] [blame] | 281 | void dontAnimateExit() { |
| 282 | if (mDimState != null) { |
| 283 | mDimState.mAnimateExit = false; |
| 284 | } |
| 285 | } |
| 286 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Call after invoking {@link WindowContainer#prepareSurfaces} on children as |
| 289 | * described in {@link #resetDimStates}. |
| 290 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 291 | * @param t A transaction in which to update the dims. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 292 | * @param bounds The bounds at which to dim. |
| 293 | * @return true if any Dims were updated. |
| 294 | */ |
| 295 | boolean updateDims(SurfaceControl.Transaction t, Rect bounds) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 296 | if (mDimState == null) { |
| 297 | return false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 298 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 299 | |
| 300 | if (!mDimState.mDimming) { |
chaviw | 87ca63a | 2018-03-26 14:06:17 -0700 | [diff] [blame] | 301 | if (!mDimState.mAnimateExit) { |
| 302 | t.destroy(mDimState.mDimLayer); |
| 303 | } else { |
| 304 | startDimExit(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t); |
| 305 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 306 | mDimState = null; |
| 307 | return false; |
| 308 | } else { |
| 309 | // TODO: Once we use geometry from hierarchy this falls away. |
| 310 | t.setSize(mDimState.mDimLayer, bounds.width(), bounds.height()); |
| 311 | t.setPosition(mDimState.mDimLayer, bounds.left, bounds.top); |
Vishnu Nair | d454442d | 2018-11-13 13:51:01 -0800 | [diff] [blame^] | 312 | t.setWindowCrop(mDimState.mDimLayer, bounds.width(), bounds.height()); |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 313 | if (!mDimState.isVisible) { |
| 314 | mDimState.isVisible = true; |
| 315 | t.show(mDimState.mDimLayer); |
| 316 | startDimEnter(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t); |
| 317 | } |
| 318 | return true; |
| 319 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 320 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 321 | |
| 322 | private void startDimEnter(WindowContainer container, SurfaceAnimator animator, |
| 323 | SurfaceControl.Transaction t) { |
| 324 | startAnim(container, animator, t, 0 /* startAlpha */, 1 /* endAlpha */); |
| 325 | } |
| 326 | |
| 327 | private void startDimExit(WindowContainer container, SurfaceAnimator animator, |
| 328 | SurfaceControl.Transaction t) { |
| 329 | startAnim(container, animator, t, 1 /* startAlpha */, 0 /* endAlpha */); |
| 330 | } |
| 331 | |
| 332 | private void startAnim(WindowContainer container, SurfaceAnimator animator, |
| 333 | SurfaceControl.Transaction t, float startAlpha, float endAlpha) { |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 334 | mSurfaceAnimatorStarter.startAnimation(animator, t, new LocalAnimationAdapter( |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 335 | new AlphaAnimationSpec(startAlpha, endAlpha, getDimDuration(container)), |
| 336 | mHost.mService.mSurfaceAnimationRunner), false /* hidden */); |
| 337 | } |
| 338 | |
| 339 | private long getDimDuration(WindowContainer container) { |
| 340 | // If there's no container, then there isn't an animation occurring while dimming. Set the |
| 341 | // duration to 0 so it immediately dims to the set alpha. |
| 342 | if (container == null) { |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | // Otherwise use the same duration as the animation on the WindowContainer |
| 347 | AnimationAdapter animationAdapter = container.mSurfaceAnimator.getAnimation(); |
| 348 | return animationAdapter == null ? DEFAULT_DIM_ANIM_DURATION |
| 349 | : animationAdapter.getDurationHint(); |
| 350 | } |
| 351 | |
| 352 | private static class AlphaAnimationSpec implements LocalAnimationAdapter.AnimationSpec { |
| 353 | private final long mDuration; |
| 354 | private final float mFromAlpha; |
| 355 | private final float mToAlpha; |
| 356 | |
| 357 | AlphaAnimationSpec(float fromAlpha, float toAlpha, long duration) { |
| 358 | mFromAlpha = fromAlpha; |
| 359 | mToAlpha = toAlpha; |
| 360 | mDuration = duration; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public long getDuration() { |
| 365 | return mDuration; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public void apply(SurfaceControl.Transaction t, SurfaceControl sc, long currentPlayTime) { |
| 370 | float alpha = ((float) currentPlayTime / getDuration()) * (mToAlpha - mFromAlpha) |
| 371 | + mFromAlpha; |
| 372 | t.setAlpha(sc, alpha); |
| 373 | } |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 374 | |
| 375 | @Override |
| 376 | public void dump(PrintWriter pw, String prefix) { |
| 377 | pw.print(prefix); pw.print("from="); pw.print(mFromAlpha); |
| 378 | pw.print(" to="); pw.print(mToAlpha); |
| 379 | pw.print(" duration="); pw.println(mDuration); |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public void writeToProtoInner(ProtoOutputStream proto) { |
| 384 | final long token = proto.start(ALPHA); |
| 385 | proto.write(FROM, mFromAlpha); |
| 386 | proto.write(TO, mToAlpha); |
Kweku Adams | 21b8d26 | 2018-03-30 12:19:58 -0700 | [diff] [blame] | 387 | proto.write(DURATION_MS, mDuration); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 388 | proto.end(token); |
| 389 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 390 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 391 | } |