OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / services / java / com / android / server / InputWindowList.java
1 /*
2  * Copyright (C) 2010 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
17 package com.android.server;
18
19
20 /**
21  * A specialized list of window information objects backed by an array.
22  * 
23  * This class is part of an InputManager optimization to avoid allocating objects and arrays
24  * unnecessarily.  Internally, it keeps an array full of demand-allocated objects that it
25  * recycles each time the list is cleared.  The used portion of the array is padded with a null.
26  * 
27  * The contents of the list are intended to be Z-ordered from top to bottom.
28  * 
29  * @hide
30  */
31 public final class InputWindowList {
32     private InputWindow[] mArray;
33     private int mCount;
34     
35     /**
36      * Creates an empty list.
37      */
38     public InputWindowList() {
39         mArray = new InputWindow[8];
40     }
41     
42     /**
43      * Clears the list.
44      */
45     public void clear() {
46         if (mCount == 0) {
47             return;
48         }
49         
50         int count = mCount;
51         mCount = 0;
52         mArray[count] = mArray[0];
53         while (count > 0) {
54             count -= 1;
55             mArray[count].recycle();
56         }
57         mArray[0] = null;
58     }
59     
60     /**
61      * Adds an uninitialized input window object to the list and returns it.
62      */
63     public InputWindow add() {
64         if (mCount + 1 == mArray.length) {
65             InputWindow[] oldArray = mArray;
66             mArray = new InputWindow[oldArray.length * 2];
67             System.arraycopy(oldArray, 0, mArray, 0, mCount);
68         }
69         
70         // Grab object from tail (after used section) if available.
71         InputWindow item = mArray[mCount + 1];
72         if (item == null) {
73             item = new InputWindow();
74         }
75         
76         mArray[mCount] = item;
77         mCount += 1;
78         mArray[mCount] = null;
79         return item;
80     }
81     
82     /**
83      * Gets the input window objects as a null-terminated array.
84      * @return The input window array.
85      */
86     public InputWindow[] toNullTerminatedArray() {
87         return mArray;
88     }
89 }