WindowInfo.java revision 396d549113bc633f719acc643c7dfc5f2a8fae4e
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    public CharSequence title;
48    public int accessibilityIdOfAnchor = View.NO_ID;
49
50    private WindowInfo() {
51        /* do nothing - hide constructor */
52    }
53
54    public static WindowInfo obtain() {
55        WindowInfo window = sPool.acquire();
56        if (window == null) {
57            window = new WindowInfo();
58        }
59        return window;
60    }
61
62    public static WindowInfo obtain(WindowInfo other) {
63        WindowInfo window = obtain();
64        window.type = other.type;
65        window.layer = other.layer;
66        window.token = other.token;
67        window.parentToken = other.parentToken;
68        window.focused = other.focused;
69        window.boundsInScreen.set(other.boundsInScreen);
70        window.title = other.title;
71        window.accessibilityIdOfAnchor = other.accessibilityIdOfAnchor;
72
73        if (other.childTokens != null && !other.childTokens.isEmpty()) {
74            if (window.childTokens == null) {
75                window.childTokens = new ArrayList<IBinder>(other.childTokens);
76            } else {
77                window.childTokens.addAll(other.childTokens);
78            }
79        }
80
81        return window;
82    }
83
84    public void recycle() {
85        clear();
86        sPool.release(this);
87    }
88
89    @Override
90    public int describeContents() {
91        return 0;
92    }
93
94    @Override
95    public void writeToParcel(Parcel parcel, int flags) {
96        parcel.writeInt(type);
97        parcel.writeInt(layer);
98        parcel.writeStrongBinder(token);
99        parcel.writeStrongBinder(parentToken);
100        parcel.writeInt(focused ? 1 : 0);
101        boundsInScreen.writeToParcel(parcel, flags);
102        parcel.writeCharSequence(title);
103        parcel.writeInt(accessibilityIdOfAnchor);
104
105        if (childTokens != null && !childTokens.isEmpty()) {
106            parcel.writeInt(1);
107            parcel.writeBinderList(childTokens);
108        } else {
109            parcel.writeInt(0);
110        }
111    }
112
113    @Override
114    public String toString() {
115        StringBuilder builder = new StringBuilder();
116        builder.append("WindowInfo[");
117        builder.append("title=").append(title);
118        builder.append(", type=").append(type);
119        builder.append(", layer=").append(layer);
120        builder.append(", token=").append(token);
121        builder.append(", bounds=").append(boundsInScreen);
122        builder.append(", parent=").append(parentToken);
123        builder.append(", focused=").append(focused);
124        builder.append(", children=").append(childTokens);
125        builder.append(", accessibility anchor=").append(accessibilityIdOfAnchor);
126        builder.append(']');
127        return builder.toString();
128    }
129
130    private void initFromParcel(Parcel parcel) {
131        type = parcel.readInt();
132        layer = parcel.readInt();
133        token = parcel.readStrongBinder();
134        parentToken = parcel.readStrongBinder();
135        focused = (parcel.readInt() == 1);
136        boundsInScreen.readFromParcel(parcel);
137        title = parcel.readCharSequence();
138        accessibilityIdOfAnchor = parcel.readInt();
139
140        final boolean hasChildren = (parcel.readInt() == 1);
141        if (hasChildren) {
142            if (childTokens == null) {
143                childTokens = new ArrayList<IBinder>();
144            }
145            parcel.readBinderList(childTokens);
146        }
147    }
148
149    private void clear() {
150        type = 0;
151        layer = 0;
152        token = null;
153        parentToken = null;
154        focused = false;
155        boundsInScreen.setEmpty();
156        if (childTokens != null) {
157            childTokens.clear();
158        }
159    }
160
161    public static final Parcelable.Creator<WindowInfo> CREATOR =
162            new Creator<WindowInfo>() {
163        @Override
164        public WindowInfo createFromParcel(Parcel parcel) {
165            WindowInfo window = obtain();
166            window.initFromParcel(parcel);
167            return window;
168        }
169
170        @Override
171        public WindowInfo[] newArray(int size) {
172            return new WindowInfo[size];
173        }
174    };
175}
176