• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.internal.widget.remotecompose.core.operations.layout.measure;
17 
18 import android.annotation.NonNull;
19 
20 import com.android.internal.widget.remotecompose.core.operations.layout.Component;
21 
22 import java.util.HashMap;
23 
24 /**
25  * Represents the result of a measure pass on the entire hierarchy TODO: optimize to use a flat
26  * array vs the current hashmap
27  */
28 public class MeasurePass {
29     @NonNull HashMap<Integer, ComponentMeasure> mList = new HashMap<>();
30 
31     /** Clear the MeasurePass */
clear()32     public void clear() {
33         mList.clear();
34     }
35 
36     /**
37      * Add a ComponentMeasure to the MeasurePass
38      *
39      * @param measure the ComponentMeasure to add
40      * @throws Exception
41      */
add(@onNull ComponentMeasure measure)42     public void add(@NonNull ComponentMeasure measure) throws Exception {
43         if (measure.mId == -1) {
44             throw new Exception("Component has no id!");
45         }
46         mList.put(measure.mId, measure);
47     }
48 
49     /**
50      * Returns true if the current MeasurePass already contains a ComponentMeasure for the given id.
51      *
52      * @param id
53      * @return
54      */
contains(int id)55     public boolean contains(int id) {
56         return mList.containsKey(id);
57     }
58 
59     /**
60      * return the ComponentMeasure associated with a given component
61      *
62      * @param c the Component
63      * @return the associated ComponentMeasure
64      */
get(@onNull Component c)65     public @NonNull ComponentMeasure get(@NonNull Component c) {
66         if (!mList.containsKey(c.getComponentId())) {
67             ComponentMeasure measure =
68                     new ComponentMeasure(
69                             c.getComponentId(), c.getX(), c.getY(), c.getWidth(), c.getHeight());
70             mList.put(c.getComponentId(), measure);
71             return measure;
72         }
73         return mList.get(c.getComponentId());
74     }
75 
76     /**
77      * Returns the ComponentMeasure associated with the id, creating one if none exists.
78      *
79      * @param id the component id
80      * @return the associated ComponentMeasure
81      */
get(int id)82     public @NonNull ComponentMeasure get(int id) {
83         if (!mList.containsKey(id)) {
84             ComponentMeasure measure =
85                     new ComponentMeasure(id, 0f, 0f, 0f, 0f, Component.Visibility.GONE);
86             mList.put(id, measure);
87             return measure;
88         }
89         return mList.get(id);
90     }
91 }
92