WindowContainer.java revision d63594a8d116b9fb25a031cb489ca033f716226a
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
17package com.android.server.wm;
18
19import android.annotation.CallSuper;
20
21import java.util.Comparator;
22import java.util.LinkedList;
23
24/**
25 * Defines common functionality for classes that can hold windows directly or through their
26 * children.
27 * The test class is {@link WindowContainerTests} which must be kept up-to-date and ran anytime
28 * changes are made to this class.
29 */
30class WindowContainer {
31
32    // The parent of this window container.
33    private WindowContainer mParent = null;
34
35    // List of children for this window container. List is in z-order as the children appear on
36    // screen with the top-most window container at the tail of the list.
37    protected final LinkedList<WindowContainer> mChildren = new LinkedList();
38
39    protected WindowContainer getParent() {
40        return mParent;
41    }
42
43    /**
44     * Adds the input window container has a child of this container in order based on the input
45     * comparator.
46     * @param child The window container to add as a child of this window container.
47     * @param comparator Comparator to use in determining the position the child should be added to.
48     *                   If null, the child will be added to the top.
49     */
50    @CallSuper
51    protected void addChild(WindowContainer child, Comparator<WindowContainer> comparator) {
52        child.mParent = this;
53
54        if (mChildren.isEmpty() || comparator == null) {
55            mChildren.add(child);
56            return;
57        }
58
59        final int count = mChildren.size();
60        for (int i = 0; i < count; i++) {
61            if (comparator.compare(child, mChildren.get(i)) < 0) {
62                mChildren.add(i, child);
63                return;
64            }
65        }
66
67        mChildren.add(child);
68    }
69
70    /** Removes this window container and its children */
71    @CallSuper
72    void remove() {
73        while (!mChildren.isEmpty()) {
74            final WindowContainer child = mChildren.removeLast();
75            child.remove();
76        }
77
78        if (mParent != null) {
79            mParent.mChildren.remove(this);
80            mParent = null;
81        }
82    }
83
84    /** Returns true if this window container has the input child. */
85    boolean hasChild(WindowContainer child) {
86        for (int i = mChildren.size() - 1; i >= 0; --i) {
87            final WindowContainer current = mChildren.get(i);
88            if (current == child || current.hasChild(child)) {
89                return true;
90            }
91        }
92        return false;
93    }
94}
95