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