WindowInfo.java revision 1cf70bbf96930662cab0e699d70b62865766ff52
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;
49
50    public float compatibilityScale;
51
52    public boolean visible;
53
54    public int displayId;
55
56    private WindowInfo() {
57        /* do nothing - reduce visibility */
58    }
59
60    @Override
61    public int describeContents() {
62        return 0;
63    }
64
65    @Override
66    public void writeToParcel(Parcel parcel, int flags) {
67        parcel.writeStrongBinder(token);
68        parcel.writeParcelable(frame, 0);
69        parcel.writeParcelable(touchableRegion, 0);
70        parcel.writeInt(type);
71        parcel.writeFloat(compatibilityScale);
72        parcel.writeInt(visible ? 1 : 0);
73        parcel.writeInt(displayId);
74        recycle();
75    }
76
77    private void initFromParcel(Parcel parcel) {
78        token = parcel.readStrongBinder();
79        frame.set((Rect) parcel.readParcelable(null));
80        touchableRegion.set((Rect) parcel.readParcelable(null));
81        type = parcel.readInt();
82        compatibilityScale = parcel.readFloat();
83        visible = (parcel.readInt() == 1);
84        displayId = parcel.readInt();
85    }
86
87    public static WindowInfo obtain(WindowInfo other) {
88        WindowInfo info = obtain();
89        info.token = other.token;
90        info.frame.set(other.frame);
91        info.touchableRegion.set(other.touchableRegion);
92        info.type = other.type;
93        info.displayId = other.displayId;
94        info.compatibilityScale = other.compatibilityScale;
95        info.visible = other.visible;
96        return info;
97    }
98
99    public static WindowInfo obtain() {
100        synchronized (sPoolLock) {
101            if (sPoolSize > 0) {
102                WindowInfo info = sPool;
103                sPool = info.mNext;
104                info.mNext = null;
105                info.mInPool = false;
106                sPoolSize--;
107                return info;
108            } else {
109                return new WindowInfo();
110            }
111        }
112    }
113
114    public void recycle() {
115        if (mInPool) {
116            throw new IllegalStateException("Already recycled.");
117        }
118        clear();
119        synchronized (sPoolLock) {
120            if (sPoolSize < MAX_POOL_SIZE) {
121                mNext = sPool;
122                sPool = this;
123                mInPool = true;
124                sPoolSize++;
125            }
126        }
127    }
128
129    private void clear() {
130        token = null;
131        frame.setEmpty();
132        touchableRegion.setEmpty();
133        type = UNDEFINED;
134        displayId = UNDEFINED;
135        visible = false;
136    }
137
138    /**
139     * @see Parcelable.Creator
140     */
141    public static final Parcelable.Creator<WindowInfo> CREATOR =
142            new Parcelable.Creator<WindowInfo>() {
143        public WindowInfo createFromParcel(Parcel parcel) {
144            WindowInfo info = WindowInfo.obtain();
145            info.initFromParcel(parcel);
146            return info;
147        }
148
149        public WindowInfo[] newArray(int size) {
150            return new WindowInfo[size];
151        }
152    };
153}
154