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 17 package android.view; 18 19 import android.graphics.Region; 20 import android.os.IBinder; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.util.Pools; 24 import android.view.accessibility.AccessibilityNodeInfo; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** 30 * This class represents information about a window from the 31 * window manager to another part of the system. 32 * 33 * @hide 34 */ 35 public class WindowInfo implements Parcelable { 36 private static final int MAX_POOL_SIZE = 10; 37 38 private static final Pools.SynchronizedPool<WindowInfo> sPool = 39 new Pools.SynchronizedPool<WindowInfo>(MAX_POOL_SIZE); 40 41 public int type; 42 public int layer; 43 public IBinder token; 44 public IBinder parentToken; 45 public IBinder activityToken; 46 public boolean focused; 47 public Region regionInScreen = new Region(); 48 public List<IBinder> childTokens; 49 public CharSequence title; 50 public long accessibilityIdOfAnchor = AccessibilityNodeInfo.UNDEFINED_NODE_ID; 51 public boolean inPictureInPicture; 52 public boolean hasFlagWatchOutsideTouch; 53 public int displayId = Display.INVALID_DISPLAY; 54 WindowInfo()55 private WindowInfo() { 56 /* do nothing - hide constructor */ 57 } 58 obtain()59 public static WindowInfo obtain() { 60 WindowInfo window = sPool.acquire(); 61 if (window == null) { 62 window = new WindowInfo(); 63 } 64 return window; 65 } 66 obtain(WindowInfo other)67 public static WindowInfo obtain(WindowInfo other) { 68 WindowInfo window = obtain(); 69 window.displayId = other.displayId; 70 window.type = other.type; 71 window.layer = other.layer; 72 window.token = other.token; 73 window.parentToken = other.parentToken; 74 window.activityToken = other.activityToken; 75 window.focused = other.focused; 76 window.regionInScreen.set(other.regionInScreen); 77 window.title = other.title; 78 window.accessibilityIdOfAnchor = other.accessibilityIdOfAnchor; 79 window.inPictureInPicture = other.inPictureInPicture; 80 window.hasFlagWatchOutsideTouch = other.hasFlagWatchOutsideTouch; 81 82 if (other.childTokens != null && !other.childTokens.isEmpty()) { 83 if (window.childTokens == null) { 84 window.childTokens = new ArrayList<IBinder>(other.childTokens); 85 } else { 86 window.childTokens.addAll(other.childTokens); 87 } 88 } 89 90 return window; 91 } 92 recycle()93 public void recycle() { 94 clear(); 95 sPool.release(this); 96 } 97 98 @Override describeContents()99 public int describeContents() { 100 return 0; 101 } 102 103 @Override writeToParcel(Parcel parcel, int flags)104 public void writeToParcel(Parcel parcel, int flags) { 105 parcel.writeInt(displayId); 106 parcel.writeInt(type); 107 parcel.writeInt(layer); 108 parcel.writeStrongBinder(token); 109 parcel.writeStrongBinder(parentToken); 110 parcel.writeStrongBinder(activityToken); 111 parcel.writeInt(focused ? 1 : 0); 112 regionInScreen.writeToParcel(parcel, flags); 113 parcel.writeCharSequence(title); 114 parcel.writeLong(accessibilityIdOfAnchor); 115 parcel.writeInt(inPictureInPicture ? 1 : 0); 116 parcel.writeInt(hasFlagWatchOutsideTouch ? 1 : 0); 117 118 if (childTokens != null && !childTokens.isEmpty()) { 119 parcel.writeInt(1); 120 parcel.writeBinderList(childTokens); 121 } else { 122 parcel.writeInt(0); 123 } 124 } 125 126 @Override toString()127 public String toString() { 128 StringBuilder builder = new StringBuilder(); 129 builder.append("WindowInfo["); 130 builder.append("title=").append(title); 131 builder.append(", displayId=").append(displayId); 132 builder.append(", type=").append(type); 133 builder.append(", layer=").append(layer); 134 builder.append(", token=").append(token); 135 builder.append(", region=").append(regionInScreen); 136 builder.append(", bounds=").append(regionInScreen.getBounds()); 137 builder.append(", parent=").append(parentToken); 138 builder.append(", focused=").append(focused); 139 builder.append(", children=").append(childTokens); 140 builder.append(", accessibility anchor=").append(accessibilityIdOfAnchor); 141 builder.append(", pictureInPicture=").append(inPictureInPicture); 142 builder.append(", watchOutsideTouch=").append(hasFlagWatchOutsideTouch); 143 builder.append(']'); 144 return builder.toString(); 145 } 146 initFromParcel(Parcel parcel)147 private void initFromParcel(Parcel parcel) { 148 displayId = parcel.readInt(); 149 type = parcel.readInt(); 150 layer = parcel.readInt(); 151 token = parcel.readStrongBinder(); 152 parentToken = parcel.readStrongBinder(); 153 activityToken = parcel.readStrongBinder(); 154 focused = (parcel.readInt() == 1); 155 regionInScreen = Region.CREATOR.createFromParcel(parcel); 156 title = parcel.readCharSequence(); 157 accessibilityIdOfAnchor = parcel.readLong(); 158 inPictureInPicture = (parcel.readInt() == 1); 159 hasFlagWatchOutsideTouch = (parcel.readInt() == 1); 160 161 final boolean hasChildren = (parcel.readInt() == 1); 162 if (hasChildren) { 163 if (childTokens == null) { 164 childTokens = new ArrayList<IBinder>(); 165 } 166 parcel.readBinderList(childTokens); 167 } 168 } 169 clear()170 private void clear() { 171 displayId = Display.INVALID_DISPLAY; 172 type = 0; 173 layer = 0; 174 token = null; 175 parentToken = null; 176 activityToken = null; 177 focused = false; 178 regionInScreen.setEmpty(); 179 if (childTokens != null) { 180 childTokens.clear(); 181 } 182 inPictureInPicture = false; 183 hasFlagWatchOutsideTouch = false; 184 } 185 186 public static final @android.annotation.NonNull Parcelable.Creator<WindowInfo> CREATOR = 187 new Creator<WindowInfo>() { 188 @Override 189 public WindowInfo createFromParcel(Parcel parcel) { 190 WindowInfo window = obtain(); 191 window.initFromParcel(parcel); 192 return window; 193 } 194 195 @Override 196 public WindowInfo[] newArray(int size) { 197 return new WindowInfo[size]; 198 } 199 }; 200 } 201