1 /*
2  * Copyright (C) 2022 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.ViewGroup.LayoutParams.MATCH_PARENT;
20 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
21 
22 import android.app.WindowConfiguration.WindowingMode;
23 import android.graphics.Rect;
24 import android.view.WindowInsets.Type.InsetsType;
25 import android.window.ClientWindowFrames;
26 
27 /**
28  * Computes window frames for the windowless window.
29  *
30  * This can't be replaced with the regular WindowLayout because WindowLayout computes bounds
31  * with insets and cutout values. Since windowless windows aren't affected by insets and
32  * instead are bound by their parent, it will compute incorrect bounds for them if insets are used.
33  *
34  * @hide
35  */
36 public class WindowlessWindowLayout extends WindowLayout {
37 
38     @Override
computeFrames(WindowManager.LayoutParams attrs, InsetsState state, Rect displayCutoutSafe, Rect windowBounds, @WindowingMode int windowingMode, int requestedWidth, int requestedHeight, @InsetsType int requestedVisibleTypes, float compatScale, ClientWindowFrames frames)39     public void computeFrames(WindowManager.LayoutParams attrs, InsetsState state,
40             Rect displayCutoutSafe, Rect windowBounds, @WindowingMode int windowingMode,
41             int requestedWidth, int requestedHeight, @InsetsType int requestedVisibleTypes,
42             float compatScale, ClientWindowFrames frames) {
43         if (frames.attachedFrame == null) {
44             frames.frame.set(0, 0, attrs.width, attrs.height);
45             frames.parentFrame.set(frames.frame);
46             frames.displayFrame.set(frames.frame);
47             return;
48         }
49 
50         final int height = calculateLength(attrs.height, requestedHeight,
51                 frames.attachedFrame.height());
52         final int width = calculateLength(attrs.width, requestedWidth,
53                 frames.attachedFrame.width());
54         Gravity.apply(attrs.gravity, width, height, frames.attachedFrame,
55                 (int) (attrs.x + attrs.horizontalMargin),
56                 (int) (attrs.y + attrs.verticalMargin),
57                 frames.frame);
58         frames.displayFrame.set(frames.frame);
59         frames.parentFrame.set(frames.attachedFrame);
60     }
61 
calculateLength(int attrLength, int requestedLength, int parentLength)62     private static int calculateLength(int attrLength, int requestedLength, int parentLength) {
63         if (attrLength == MATCH_PARENT) {
64             return parentLength;
65         }
66         if (attrLength == WRAP_CONTENT) {
67             return requestedLength;
68         }
69         return attrLength;
70     }
71 }
72