blob: ba5340c826d37788e2e250ff469974b16d75d4dd [file] [log] [blame]
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001/*
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
17package android.view;
18
Jorim Jaggib6030952018-10-23 18:31:52 +020019import android.annotation.NonNull;
20import android.annotation.Nullable;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020021import android.graphics.Rect;
Jorim Jaggib6030952018-10-23 18:31:52 +020022import android.util.ArraySet;
23import android.util.SparseArray;
24import android.view.SurfaceControl.Transaction;
25import android.view.WindowInsets.Type.InsetType;
26import android.view.InsetsState.InternalInsetType;
27
28import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020029
30import java.io.PrintWriter;
31
32/**
33 * Implements {@link WindowInsetsController} on the client.
Jorim Jaggib6030952018-10-23 18:31:52 +020034 * @hide
Jorim Jaggif96c90a2018-09-26 16:55:15 +020035 */
Jorim Jaggib6030952018-10-23 18:31:52 +020036public class InsetsController implements WindowInsetsController {
Jorim Jaggif96c90a2018-09-26 16:55:15 +020037
38 private final InsetsState mState = new InsetsState();
39 private final Rect mFrame = new Rect();
Jorim Jaggib6030952018-10-23 18:31:52 +020040 private final SparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>();
41
42 private final SparseArray<InsetsSourceControl> mTmpControlArray = new SparseArray<>();
Jorim Jaggif96c90a2018-09-26 16:55:15 +020043
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 Jaggib6030952018-10-23 18:31:52 +020064 /**
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 Jaggif96c90a2018-09-26 16:55:15 +0200119 void dump(String prefix, PrintWriter pw) {
120 pw.println(prefix); pw.println("InsetsController:");
121 mState.dump(prefix + " ", pw);
122 }
123}