1 /*
2  * Copyright (C) 2016 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.perftests.utils;
18 
19 import android.view.View;
20 import android.view.ViewGroup;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 public class LayoutUtils {
26 
recursivelyGather(ViewGroup currentNode, List<View> nodeList)27     private static void recursivelyGather(ViewGroup currentNode, List<View> nodeList) {
28         nodeList.add(currentNode);
29         int count = currentNode.getChildCount();
30         for (int i = 0; i < count; i++) {
31             View view = currentNode.getChildAt(i);
32             if (view instanceof ViewGroup) {
33                 recursivelyGather((ViewGroup) view, nodeList);
34             } else {
35                 nodeList.add(view);
36             }
37         }
38     }
39 
40     /**
41      * Flattern the whole view tree into a list of View.
42      */
gatherViewTree(ViewGroup root)43     public static List<View> gatherViewTree(ViewGroup root) {
44         List<View> result = new ArrayList<View>();
45         recursivelyGather(root, result);
46         return result;
47     }
48 
49     /**
50      * For every node in the list, call requestLayout.
51      */
requestLayoutForAllNodes(List<View> nodeList)52     public static void requestLayoutForAllNodes(List<View> nodeList) {
53         int count = nodeList.size();
54         for (int i = 0; i < count; i++) {
55             nodeList.get(i).requestLayout();
56         }
57     }
58 }
59