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