WindowInfo.java revision 8e3feb15c5aec2c72b0ef120a1da325e1e8f0dda
1/*
2 * Copyright (C) 2014 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
19import android.graphics.Rect;
20import android.os.IBinder;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.Pools;
24
25import java.util.ArrayList;
26import java.util.List;
27
28/**
29 * This class represents information about a window from the
30 * window manager to another part of the system.
31 *
32 * @hide
33 */
34public class WindowInfo implements Parcelable {
35    private static final int MAX_POOL_SIZE = 10;
36
37    private static final Pools.SynchronizedPool<WindowInfo> sPool =
38            new Pools.SynchronizedPool<WindowInfo>(MAX_POOL_SIZE);
39
40    public int type;
41    public int layer;
42    public IBinder token;
43    public IBinder parentToken;
44    public boolean focused;
45    public final Rect boundsInScreen = new Rect();
46    public List<IBinder> childTokens;
47
48    private WindowInfo() {
49        /* do nothing - hide constructor */
50    }
51
52    public static WindowInfo obtain() {
53        WindowInfo window = sPool.acquire();
54        if (window == null) {
55            window = new WindowInfo();
56        }
57        return window;
58    }
59
60    public static WindowInfo obtain(WindowInfo other) {
61        WindowInfo window = obtain();
62        window.type = other.type;
63        window.layer = other.layer;
64        window.token = other.token;
65        window.parentToken = other.parentToken;
66        window.focused = other.focused;
67        window.boundsInScreen.set(other.boundsInScreen);
68
69        if (other.childTokens != null && !other.childTokens.isEmpty()) {
70            if (window.childTokens == null) {
71                window.childTokens = new ArrayList<IBinder>(other.childTokens);
72            } else {
73                window.childTokens.addAll(other.childTokens);
74            }
75        }
76
77        return window;
78    }
79
80    public void recycle() {
81        clear();
82        sPool.release(this);
83    }
84
85    @Override
86    public int describeContents() {
87        return 0;
88    }
89
90    @Override
91    public void writeToParcel(Parcel parcel, int flags) {
92        parcel.writeInt(type);
93        parcel.writeInt(layer);
94        parcel.writeStrongBinder(token);
95        parcel.writeStrongBinder(parentToken);
96        parcel.writeInt(focused ? 1 : 0);
97        boundsInScreen.writeToParcel(parcel, flags);
98
99        if (childTokens != null && !childTokens.isEmpty()) {
100            parcel.writeInt(1);
101            parcel.writeBinderList(childTokens);
102        } else {
103            parcel.writeInt(0);
104        }
105    }
106
107    @Override
108    public String toString() {
109        StringBuilder builder = new StringBuilder();
110        builder.append("WindowInfo[");
111        builder.append("type=").append(type);
112        builder.append(", layer=").append(layer);
113        builder.append(", token=").append(token);
114        builder.append(", parent=").append(parentToken);
115        builder.append(", focused=").append(focused);
116        builder.append(", children=").append(childTokens);
117        builder.append(']');
118        return builder.toString();
119    }
120
121    private void initFromParcel(Parcel parcel) {
122        type = parcel.readInt();
123        layer = parcel.readInt();
124        token = parcel.readStrongBinder();
125        parentToken = parcel.readStrongBinder();
126        focused = (parcel.readInt() == 1);
127        boundsInScreen.readFromParcel(parcel);
128
129        final boolean hasChildren = (parcel.readInt() == 1);
130        if (hasChildren) {
131            if (childTokens == null) {
132                childTokens = new ArrayList<IBinder>();
133            }
134            parcel.readBinderList(childTokens);
135        }
136    }
137
138    private void clear() {
139        type = 0;
140        layer = 0;
141        token = null;
142        parentToken = null;
143        focused = false;
144        boundsInScreen.setEmpty();
145        if (childTokens != null) {
146            childTokens.clear();
147        }
148    }
149
150    public static final Parcelable.Creator<WindowInfo> CREATOR =
151            new Creator<WindowInfo>() {
152        @Override
153        public WindowInfo createFromParcel(Parcel parcel) {
154            WindowInfo window = obtain();
155            window.initFromParcel(parcel);
156            return window;
157        }
158
159        @Override
160        public WindowInfo[] newArray(int size) {
161            return new WindowInfo[size];
162        }
163    };
164}
165