Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame^] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 21 | import android.graphics.Rect; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame^] | 22 | import android.util.ArraySet; |
| 23 | import android.util.SparseArray; |
| 24 | import android.view.SurfaceControl.Transaction; |
| 25 | import android.view.WindowInsets.Type.InsetType; |
| 26 | import android.view.InsetsState.InternalInsetType; |
| 27 | |
| 28 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 29 | |
| 30 | import java.io.PrintWriter; |
| 31 | |
| 32 | /** |
| 33 | * Implements {@link WindowInsetsController} on the client. |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame^] | 34 | * @hide |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 35 | */ |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame^] | 36 | public class InsetsController implements WindowInsetsController { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 37 | |
| 38 | private final InsetsState mState = new InsetsState(); |
| 39 | private final Rect mFrame = new Rect(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame^] | 40 | private final SparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>(); |
| 41 | |
| 42 | private final SparseArray<InsetsSourceControl> mTmpControlArray = new SparseArray<>(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 43 | |
| 44 | void onFrameChanged(Rect frame) { |
| 45 | mFrame.set(frame); |
| 46 | } |
| 47 | |
| 48 | public InsetsState getState() { |
| 49 | return mState; |
| 50 | } |
| 51 | |
| 52 | public void setState(InsetsState state) { |
| 53 | mState.set(state); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @see InsetsState#calculateInsets |
| 58 | */ |
| 59 | WindowInsets calculateInsets(boolean isScreenRound, |
| 60 | boolean alwaysConsumeNavBar, DisplayCutout cutout) { |
| 61 | return mState.calculateInsets(mFrame, isScreenRound, alwaysConsumeNavBar, cutout); |
| 62 | } |
| 63 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame^] | 64 | /** |
| 65 | * Called when the server has dispatched us a new set of inset controls. |
| 66 | */ |
| 67 | public void onControlsChanged(InsetsSourceControl[] activeControls) { |
| 68 | if (activeControls != null) { |
| 69 | for (InsetsSourceControl activeControl : activeControls) { |
| 70 | mTmpControlArray.put(activeControl.getType(), activeControl); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Ensure to update all existing source consumers |
| 75 | for (int i = mSourceConsumers.size() - 1; i >= 0; i--) { |
| 76 | final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i); |
| 77 | final InsetsSourceControl control = mTmpControlArray.get(consumer.getType()); |
| 78 | |
| 79 | // control may be null, but we still need to update the control to null if it got |
| 80 | // revoked. |
| 81 | consumer.setControl(control); |
| 82 | } |
| 83 | |
| 84 | // Ensure to create source consumers if not available yet. |
| 85 | for (int i = mTmpControlArray.size() - 1; i >= 0; i--) { |
| 86 | final InsetsSourceControl control = mTmpControlArray.valueAt(i); |
| 87 | getSourceConsumer(control.getType()).setControl(control); |
| 88 | } |
| 89 | mTmpControlArray.clear(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void show(@InsetType int types) { |
| 94 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 95 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
| 96 | getSourceConsumer(internalTypes.valueAt(i)).show(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void hide(@InsetType int types) { |
| 102 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 103 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
| 104 | getSourceConsumer(internalTypes.valueAt(i)).hide(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @VisibleForTesting |
| 109 | public @NonNull InsetsSourceConsumer getSourceConsumer(@InternalInsetType int type) { |
| 110 | InsetsSourceConsumer controller = mSourceConsumers.get(type); |
| 111 | if (controller != null) { |
| 112 | return controller; |
| 113 | } |
| 114 | controller = new InsetsSourceConsumer(type, mState, Transaction::new); |
| 115 | mSourceConsumers.put(type, controller); |
| 116 | return controller; |
| 117 | } |
| 118 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 119 | void dump(String prefix, PrintWriter pw) { |
| 120 | pw.println(prefix); pw.println("InsetsController:"); |
| 121 | mState.dump(prefix + " ", pw); |
| 122 | } |
| 123 | } |