WindowInfo.java revision f7174e87b6007000777b0124de9cef70d8618788
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(", bounds=").append(boundsInScreen);
115        builder.append(", parent=").append(parentToken);
116        builder.append(", focused=").append(focused);
117        builder.append(", children=").append(childTokens);
118        builder.append(']');
119        return builder.toString();
120    }
121
122    private void initFromParcel(Parcel parcel) {
123        type = parcel.readInt();
124        layer = parcel.readInt();
125        token = parcel.readStrongBinder();
126        parentToken = parcel.readStrongBinder();
127        focused = (parcel.readInt() == 1);
128        boundsInScreen.readFromParcel(parcel);
129
130        final boolean hasChildren = (parcel.readInt() == 1);
131        if (hasChildren) {
132            if (childTokens == null) {
133                childTokens = new ArrayList<IBinder>();
134            }
135            parcel.readBinderList(childTokens);
136        }
137    }
138
139    private void clear() {
140        type = 0;
141        layer = 0;
142        token = null;
143        parentToken = null;
144        focused = false;
145        boundsInScreen.setEmpty();
146        if (childTokens != null) {
147            childTokens.clear();
148        }
149    }
150
151    public static final Parcelable.Creator<WindowInfo> CREATOR =
152            new Creator<WindowInfo>() {
153        @Override
154        public WindowInfo createFromParcel(Parcel parcel) {
155            WindowInfo window = obtain();
156            window.initFromParcel(parcel);
157            return window;
158        }
159
160        @Override
161        public WindowInfo[] newArray(int size) {
162            return new WindowInfo[size];
163        }
164    };
165}
166