WindowInfo.java revision 9b4125e435b6bc8f8bd2f6e569d9b0b296ab16ef
1/*
2 * Copyright (C) 2012 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;
23
24/**
25 * Information the state of a window.
26 *
27 * @hide
28 */
29public class WindowInfo implements Parcelable {
30
31    private static final int MAX_POOL_SIZE = 20;
32
33    private static int UNDEFINED = -1;
34
35    private static Object sPoolLock = new Object();
36    private static WindowInfo sPool;
37    private static int sPoolSize;
38
39    private WindowInfo mNext;
40    private boolean mInPool;
41
42    public IBinder token;
43
44    public final Rect frame = new Rect();
45
46    public final Rect touchableRegion = new Rect();
47
48    public int type = UNDEFINED;
49
50    public float compatibilityScale = UNDEFINED;
51
52    public boolean visible;
53
54    public int displayId = UNDEFINED;
55
56    public int layer = UNDEFINED;
57
58    private WindowInfo() {
59        /* do nothing - reduce visibility */
60    }
61
62    @Override
63    public int describeContents() {
64        return 0;
65    }
66
67    @Override
68    public void writeToParcel(Parcel parcel, int flags) {
69        parcel.writeStrongBinder(token);
70        parcel.writeParcelable(frame, 0);
71        parcel.writeParcelable(touchableRegion, 0);
72        parcel.writeInt(type);
73        parcel.writeFloat(compatibilityScale);
74        parcel.writeInt(visible ? 1 : 0);
75        parcel.writeInt(displayId);
76        parcel.writeInt(layer);
77        recycle();
78    }
79
80    private void initFromParcel(Parcel parcel) {
81        token = parcel.readStrongBinder();
82        frame.set((Rect) parcel.readParcelable(null));
83        touchableRegion.set((Rect) parcel.readParcelable(null));
84        type = parcel.readInt();
85        compatibilityScale = parcel.readFloat();
86        visible = (parcel.readInt() == 1);
87        displayId = parcel.readInt();
88        layer = parcel.readInt();
89    }
90
91    public static WindowInfo obtain(WindowInfo other) {
92        WindowInfo info = obtain();
93        info.token = other.token;
94        info.frame.set(other.frame);
95        info.touchableRegion.set(other.touchableRegion);
96        info.type = other.type;
97        info.compatibilityScale = other.compatibilityScale;
98        info.visible = other.visible;
99        info.displayId = other.displayId;
100        info.layer = other.layer;
101        return info;
102    }
103
104    public static WindowInfo obtain() {
105        synchronized (sPoolLock) {
106            if (sPoolSize > 0) {
107                WindowInfo info = sPool;
108                sPool = info.mNext;
109                info.mNext = null;
110                info.mInPool = false;
111                sPoolSize--;
112                return info;
113            } else {
114                return new WindowInfo();
115            }
116        }
117    }
118
119    public void recycle() {
120        if (mInPool) {
121            throw new IllegalStateException("Already recycled.");
122        }
123        clear();
124        synchronized (sPoolLock) {
125            if (sPoolSize < MAX_POOL_SIZE) {
126                mNext = sPool;
127                sPool = this;
128                mInPool = true;
129                sPoolSize++;
130            }
131        }
132    }
133
134    private void clear() {
135        token = null;
136        frame.setEmpty();
137        touchableRegion.setEmpty();
138        type = UNDEFINED;
139        compatibilityScale = UNDEFINED;
140        visible = false;
141        displayId = UNDEFINED;
142        layer = UNDEFINED;
143    }
144
145    @Override
146    public String toString() {
147        StringBuilder builder = new StringBuilder();
148        builder.append("Window [token:").append((token != null) ? token.hashCode() : null);
149        builder.append(", displayId:").append(displayId);
150        builder.append(", type:").append(type);
151        builder.append(", visible:").append(visible);
152        builder.append(", layer:").append(layer);
153        builder.append(", compatibilityScale:").append(compatibilityScale);
154        builder.append(", frame:").append(frame);
155        builder.append(", touchableRegion:").append(touchableRegion);
156        builder.append("]");
157        return builder.toString();
158    }
159
160    /**
161     * @see Parcelable.Creator
162     */
163    public static final Parcelable.Creator<WindowInfo> CREATOR =
164            new Parcelable.Creator<WindowInfo>() {
165        public WindowInfo createFromParcel(Parcel parcel) {
166            WindowInfo info = WindowInfo.obtain();
167            info.initFromParcel(parcel);
168            return info;
169        }
170
171        public WindowInfo[] newArray(int size) {
172            return new WindowInfo[size];
173        }
174    };
175}
176