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 | 78a3d469 | 2018-11-13 18:06:56 -0800 | [diff] [blame^] | 129 | final DimAnimatable dimAnimatable = new DimAnimatable(dimLayer); |
| 130 | mSurfaceAnimator = new SurfaceAnimator(dimAnimatable, () -> { |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 131 | if (!mDimming) { |
chaviw | 78a3d469 | 2018-11-13 18:06:56 -0800 | [diff] [blame^] | 132 | dimAnimatable.getPendingTransaction().destroy(mDimLayer); |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 133 | } |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 134 | }, mHost.mService); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 135 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 136 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 137 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 138 | /** |
| 139 | * The {@link WindowContainer} that our Dim's are bounded to. We may be dimming on behalf of the |
| 140 | * host, some controller of it, or one of the hosts children. |
| 141 | */ |
| 142 | private WindowContainer mHost; |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 143 | private WindowContainer mLastRequestedDimContainer; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 144 | @VisibleForTesting |
| 145 | DimState mDimState; |
| 146 | |
| 147 | private final SurfaceAnimatorStarter mSurfaceAnimatorStarter; |
| 148 | |
| 149 | @VisibleForTesting |
| 150 | interface SurfaceAnimatorStarter { |
| 151 | void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t, |
| 152 | AnimationAdapter anim, boolean hidden); |
| 153 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 154 | |
| 155 | Dimmer(WindowContainer host) { |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 156 | this(host, SurfaceAnimator::startAnimation); |
| 157 | } |
| 158 | |
| 159 | Dimmer(WindowContainer host, SurfaceAnimatorStarter surfaceAnimatorStarter) { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 160 | mHost = host; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 161 | mSurfaceAnimatorStarter = surfaceAnimatorStarter; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 164 | private SurfaceControl makeDimLayer() { |
| 165 | return mHost.makeChildSurface(null) |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 166 | .setParent(mHost.getSurfaceControl()) |
| 167 | .setColorLayer(true) |
| 168 | .setName("Dim Layer for - " + mHost.getName()) |
| 169 | .build(); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 173 | * Retrieve the DimState, creating one if it doesn't exist. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 174 | */ |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 175 | private DimState getDimState(WindowContainer container) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 176 | if (mDimState == null) { |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 177 | try { |
| 178 | final SurfaceControl ctl = makeDimLayer(); |
| 179 | mDimState = new DimState(ctl); |
| 180 | /** |
| 181 | * See documentation on {@link #dimAbove} to understand lifecycle management of |
| 182 | * Dim's via state resetting for Dim's with containers. |
| 183 | */ |
| 184 | if (container == null) { |
| 185 | mDimState.mDontReset = true; |
| 186 | } |
| 187 | } catch (Surface.OutOfResourcesException e) { |
| 188 | Log.w(TAG, "OutOfResourcesException creating dim surface"); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 189 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 190 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 191 | |
| 192 | mLastRequestedDimContainer = container; |
| 193 | return mDimState; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | private void dim(SurfaceControl.Transaction t, WindowContainer container, int relativeLayer, |
| 197 | float alpha) { |
| 198 | final DimState d = getDimState(container); |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 199 | |
| 200 | if (d == null) { |
| 201 | return; |
| 202 | } |
| 203 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 204 | if (container != null) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 205 | // The dim method is called from WindowState.prepareSurfaces(), which is always called |
| 206 | // in the correct Z from lowest Z to highest. This ensures that the dim layer is always |
| 207 | // relative to the highest Z layer with a dim. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 208 | t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 209 | } else { |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 210 | t.setLayer(d.mDimLayer, Integer.MAX_VALUE); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 211 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 212 | t.setAlpha(d.mDimLayer, alpha); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 213 | |
| 214 | d.mDimming = true; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Finish a dim started by dimAbove in the case there was no call to dimAbove. |
| 219 | * |
| 220 | * @param t A Transaction in which to finish the dim. |
| 221 | */ |
| 222 | void stopDim(SurfaceControl.Transaction t) { |
chaviw | c5eb816 | 2018-05-25 16:18:42 -0700 | [diff] [blame] | 223 | if (mDimState != null) { |
| 224 | t.hide(mDimState.mDimLayer); |
| 225 | mDimState.isVisible = false; |
| 226 | mDimState.mDontReset = false; |
| 227 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 228 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 229 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Place a Dim above the entire host container. The caller is responsible for calling stopDim to |
| 232 | * remove this effect. If the Dim can be assosciated with a particular child of the host |
| 233 | * consider using the other variant of dimAbove which ties the Dim lifetime to the child |
| 234 | * lifetime more explicitly. |
| 235 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 236 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 237 | * @param alpha The alpha at which to Dim. |
| 238 | */ |
| 239 | void dimAbove(SurfaceControl.Transaction t, float alpha) { |
| 240 | dim(t, null, 1, alpha); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Place a dim above the given container, which should be a child of the host container. |
| 245 | * for each call to {@link WindowContainer#prepareSurfaces} the Dim state will be reset |
| 246 | * and the child should call dimAbove again to request the Dim to continue. |
| 247 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 248 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 249 | * @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] | 250 | * @param alpha The alpha at which to Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 251 | */ |
| 252 | void dimAbove(SurfaceControl.Transaction t, WindowContainer container, float alpha) { |
| 253 | dim(t, container, 1, alpha); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Like {@link #dimAbove} but places the dim below the given container. |
| 258 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 259 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 260 | * @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] | 261 | * @param alpha The alpha at which to Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 262 | */ |
| 263 | |
| 264 | void dimBelow(SurfaceControl.Transaction t, WindowContainer container, float alpha) { |
| 265 | dim(t, container, -1, alpha); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Mark all dims as pending completion on the next call to {@link #updateDims} |
| 270 | * |
| 271 | * This is intended for us by the host container, to be called at the beginning of |
| 272 | * {@link WindowContainer#prepareSurfaces}. After calling this, the container should |
| 273 | * chain {@link WindowContainer#prepareSurfaces} down to it's children to give them |
| 274 | * a chance to request dims to continue. |
| 275 | */ |
| 276 | void resetDimStates() { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 277 | if (mDimState != null && !mDimState.mDontReset) { |
| 278 | mDimState.mDimming = false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
chaviw | 87ca63a | 2018-03-26 14:06:17 -0700 | [diff] [blame] | 282 | void dontAnimateExit() { |
| 283 | if (mDimState != null) { |
| 284 | mDimState.mAnimateExit = false; |
| 285 | } |
| 286 | } |
| 287 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Call after invoking {@link WindowContainer#prepareSurfaces} on children as |
| 290 | * described in {@link #resetDimStates}. |
| 291 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 292 | * @param t A transaction in which to update the dims. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 293 | * @param bounds The bounds at which to dim. |
| 294 | * @return true if any Dims were updated. |
| 295 | */ |
| 296 | boolean updateDims(SurfaceControl.Transaction t, Rect bounds) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 297 | if (mDimState == null) { |
| 298 | return false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 299 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 300 | |
| 301 | if (!mDimState.mDimming) { |
chaviw | 87ca63a | 2018-03-26 14:06:17 -0700 | [diff] [blame] | 302 | if (!mDimState.mAnimateExit) { |
| 303 | t.destroy(mDimState.mDimLayer); |
| 304 | } else { |
| 305 | startDimExit(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t); |
| 306 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 307 | mDimState = null; |
| 308 | return false; |
| 309 | } else { |
| 310 | // TODO: Once we use geometry from hierarchy this falls away. |
| 311 | t.setSize(mDimState.mDimLayer, bounds.width(), bounds.height()); |
| 312 | t.setPosition(mDimState.mDimLayer, bounds.left, bounds.top); |
| 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 | } |