Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 android.view; |
| 18 | |
| 19 | import static android.view.SyncRtSurfaceTransactionApplier.applyParams; |
| 20 | |
| 21 | import android.annotation.UiThread; |
| 22 | import android.graphics.Rect; |
| 23 | import android.os.Handler; |
| 24 | import android.util.SparseArray; |
| 25 | import android.view.InsetsController.AnimationType; |
| 26 | import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; |
| 27 | import android.view.WindowInsets.Type.InsetsType; |
| 28 | import android.view.WindowInsetsAnimation.Bounds; |
| 29 | import android.view.animation.Interpolator; |
| 30 | |
| 31 | /** |
| 32 | * Insets animation runner that uses {@link InsetsAnimationThread} to run the animation off from the |
| 33 | * main thread. |
| 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | public class InsetsAnimationThreadControlRunner implements InsetsAnimationControlRunner { |
| 38 | |
| 39 | private final InsetsAnimationControlImpl mControl; |
| 40 | private final InsetsAnimationControlCallbacks mOuterCallbacks; |
| 41 | private final Handler mMainThreadHandler; |
| 42 | private final InsetsState mState = new InsetsState(); |
| 43 | private final InsetsAnimationControlCallbacks mCallbacks = |
| 44 | new InsetsAnimationControlCallbacks() { |
| 45 | |
| 46 | private final float[] mTmpFloat9 = new float[9]; |
| 47 | |
| 48 | @Override |
| 49 | @UiThread |
| 50 | public void startAnimation(InsetsAnimationControlImpl controller, |
| 51 | WindowInsetsAnimationControlListener listener, int types, |
| 52 | WindowInsetsAnimation animation, Bounds bounds) { |
| 53 | // Animation will be started in constructor already. |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void scheduleApplyChangeInsets(InsetsAnimationControlRunner runner) { |
| 58 | mControl.applyChangeInsets(mState); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) { |
| 63 | releaseControls(mControl.getControls()); |
| 64 | mMainThreadHandler.post(() -> |
| 65 | mOuterCallbacks.notifyFinished(InsetsAnimationThreadControlRunner.this, shown)); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void applySurfaceParams(SurfaceParams... params) { |
| 70 | SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 71 | for (int i = params.length - 1; i >= 0; i--) { |
| 72 | SyncRtSurfaceTransactionApplier.SurfaceParams surfaceParams = params[i]; |
| 73 | applyParams(t, surfaceParams, mTmpFloat9); |
| 74 | } |
| 75 | t.apply(); |
| 76 | t.close(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void releaseSurfaceControlFromRt(SurfaceControl sc) { |
| 81 | // Since we don't push the SurfaceParams to the RT we can release directly |
| 82 | sc.release(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | @UiThread |
| 87 | public InsetsAnimationThreadControlRunner(SparseArray<InsetsSourceControl> controls, Rect frame, |
| 88 | InsetsState state, WindowInsetsAnimationControlListener listener, |
| 89 | @InsetsType int types, |
| 90 | InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator, |
| 91 | @AnimationType int animationType, Handler mainThreadHandler) { |
| 92 | mMainThreadHandler = mainThreadHandler; |
| 93 | mOuterCallbacks = controller; |
| 94 | mControl = new InsetsAnimationControlImpl(controls, frame, state, listener, |
| 95 | types, mCallbacks, durationMs, interpolator, animationType); |
| 96 | InsetsAnimationThread.getHandler().post(() -> listener.onReady(mControl, types)); |
| 97 | } |
| 98 | |
| 99 | private void releaseControls(SparseArray<InsetsSourceControl> controls) { |
| 100 | for (int i = controls.size() - 1; i >= 0; i--) { |
| 101 | controls.valueAt(i).release(SurfaceControl::release); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private SparseArray<InsetsSourceControl> copyControls( |
| 106 | SparseArray<InsetsSourceControl> controls) { |
| 107 | SparseArray<InsetsSourceControl> copy = new SparseArray<>(controls.size()); |
| 108 | for (int i = 0; i < controls.size(); i++) { |
| 109 | copy.append(controls.keyAt(i), new InsetsSourceControl(controls.valueAt(i))); |
| 110 | } |
| 111 | return copy; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | @UiThread |
| 116 | public int getTypes() { |
| 117 | return mControl.getTypes(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | @UiThread |
| 122 | public void cancel() { |
| 123 | InsetsAnimationThread.getHandler().post(mControl::cancel); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | @UiThread |
| 128 | public WindowInsetsAnimation getAnimation() { |
| 129 | return mControl.getAnimation(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public int getAnimationType() { |
| 134 | return mControl.getAnimationType(); |
| 135 | } |
| 136 | } |