Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | package android.view; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.graphics.Canvas; |
| 20 | import android.graphics.Rect; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | |
| 25 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 26 | * An overlay is an extra layer that sits on top of a View (the "host view") |
| 27 | * which is drawn after all other content in that view (including children, |
| 28 | * if the view is a ViewGroup). Interaction with the overlay layer is done |
| 29 | * by adding and removing drawables. |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 30 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 31 | * <p>An overlay requested from a ViewGroup is of type {@link ViewGroupOverlay}, |
| 32 | * which also supports adding and removing views.</p> |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 33 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 34 | * @see View#getOverlay() View.getOverlay() |
| 35 | * @see ViewGroup#getOverlay() ViewGroup.getOverlay() |
| 36 | * @see ViewGroupOverlay |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 37 | */ |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 38 | public class ViewOverlay { |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 41 | * The actual container for the drawables (and views, if it's a ViewGroupOverlay). |
| 42 | * All of the management and rendering details for the overlay are handled in |
| 43 | * OverlayViewGroup. |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 44 | */ |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 45 | OverlayViewGroup mOverlayViewGroup; |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 46 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 47 | ViewOverlay(Context context, View hostView) { |
| 48 | mOverlayViewGroup = new OverlayViewGroup(context, hostView); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Used internally by View and ViewGroup to handle drawing and invalidation |
| 53 | * of the overlay |
| 54 | * @return |
| 55 | */ |
| 56 | ViewGroup getOverlayView() { |
| 57 | return mOverlayViewGroup; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Adds a Drawable to the overlay. The bounds of the drawable should be relative to |
| 62 | * the host view. Any drawable added to the overlay should be removed when it is no longer |
| 63 | * needed or no longer visible. |
| 64 | * |
| 65 | * @param drawable The Drawable to be added to the overlay. This drawable will be |
| 66 | * drawn when the view redraws its overlay. |
| 67 | * @see #remove(Drawable) |
| 68 | */ |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 69 | public void add(Drawable drawable) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 70 | mOverlayViewGroup.add(drawable); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Removes the specified Drawable from the overlay. |
| 75 | * |
| 76 | * @param drawable The Drawable to be removed from the overlay. |
| 77 | * @see #add(Drawable) |
| 78 | */ |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 79 | public void remove(Drawable drawable) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 80 | mOverlayViewGroup.remove(drawable); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Removes all content from the overlay. |
| 85 | */ |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 86 | public void clear() { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 87 | mOverlayViewGroup.clear(); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | boolean isEmpty() { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 91 | return mOverlayViewGroup.isEmpty(); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 94 | /** |
| 95 | * OverlayViewGroup is a container that View and ViewGroup use to host |
| 96 | * drawables and views added to their overlays ({@link ViewOverlay} and |
| 97 | * {@link ViewGroupOverlay}, respectively). Drawables are added to the overlay |
| 98 | * via the add/remove methods in ViewOverlay, Views are added/removed via |
| 99 | * ViewGroupOverlay. These drawable and view objects are |
| 100 | * drawn whenever the view itself is drawn; first the view draws its own |
| 101 | * content (and children, if it is a ViewGroup), then it draws its overlay |
| 102 | * (if it has one). |
| 103 | * |
| 104 | * <p>Besides managing and drawing the list of drawables, this class serves |
| 105 | * two purposes: |
| 106 | * (1) it noops layout calls because children are absolutely positioned and |
| 107 | * (2) it forwards all invalidation calls to its host view. The invalidation |
| 108 | * redirect is necessary because the overlay is not a child of the host view |
| 109 | * and invalidation cannot therefore follow the normal path up through the |
| 110 | * parent hierarchy.</p> |
| 111 | * |
| 112 | * @see View#getOverlay() |
| 113 | * @see ViewGroup#getOverlay() |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 114 | */ |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 115 | static class OverlayViewGroup extends ViewGroup { |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 116 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 117 | /** |
| 118 | * The View for which this is an overlay. Invalidations of the overlay are redirected to |
| 119 | * this host view. |
| 120 | */ |
| 121 | View mHostView; |
| 122 | |
| 123 | /** |
| 124 | * The set of drawables to draw when the overlay is rendered. |
| 125 | */ |
| 126 | ArrayList<Drawable> mDrawables = null; |
| 127 | |
| 128 | OverlayViewGroup(Context context, View hostView) { |
| 129 | super(context); |
| 130 | mHostView = hostView; |
| 131 | mAttachInfo = mHostView.mAttachInfo; |
| 132 | mRight = hostView.getWidth(); |
| 133 | mBottom = hostView.getHeight(); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 134 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 135 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 136 | public void add(Drawable drawable) { |
| 137 | if (mDrawables == null) { |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 138 | |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 139 | mDrawables = new ArrayList<Drawable>(); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 140 | } |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 141 | if (!mDrawables.contains(drawable)) { |
| 142 | // Make each drawable unique in the overlay; can't add it more than once |
| 143 | mDrawables.add(drawable); |
| 144 | invalidate(drawable.getBounds()); |
| 145 | drawable.setCallback(this); |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 146 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 147 | } |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 148 | |
| 149 | public void remove(Drawable drawable) { |
| 150 | if (mDrawables != null) { |
| 151 | mDrawables.remove(drawable); |
| 152 | invalidate(drawable.getBounds()); |
| 153 | drawable.setCallback(null); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public void add(View child) { |
| 158 | if (child.getParent() instanceof ViewGroup) { |
| 159 | ViewGroup parent = (ViewGroup) child.getParent(); |
Chet Haase | face742 | 2013-04-15 15:15:59 -0700 | [diff] [blame^] | 160 | if (parent != mHostView && parent.getParent() != null && |
| 161 | parent.mAttachInfo != null) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 162 | // Moving to different container; figure out how to position child such that |
| 163 | // it is in the same location on the screen |
| 164 | int[] parentLocation = new int[2]; |
| 165 | int[] hostViewLocation = new int[2]; |
| 166 | parent.getLocationOnScreen(parentLocation); |
| 167 | mHostView.getLocationOnScreen(hostViewLocation); |
| 168 | child.offsetLeftAndRight(parentLocation[0] - hostViewLocation[0]); |
| 169 | child.offsetTopAndBottom(parentLocation[1] - hostViewLocation[1]); |
| 170 | } |
| 171 | parent.removeView(child); |
| 172 | } |
| 173 | super.addView(child); |
| 174 | } |
| 175 | |
| 176 | public void remove(View view) { |
| 177 | super.removeView(view); |
| 178 | } |
| 179 | |
| 180 | public void clear() { |
| 181 | removeAllViews(); |
| 182 | mDrawables.clear(); |
| 183 | } |
| 184 | |
| 185 | boolean isEmpty() { |
| 186 | if (getChildCount() == 0 && |
| 187 | (mDrawables == null || mDrawables.size() == 0)) { |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void invalidateDrawable(Drawable drawable) { |
| 195 | invalidate(drawable.getBounds()); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected void dispatchDraw(Canvas canvas) { |
| 200 | super.dispatchDraw(canvas); |
| 201 | final int numDrawables = (mDrawables == null) ? 0 : mDrawables.size(); |
| 202 | for (int i = 0; i < numDrawables; ++i) { |
| 203 | mDrawables.get(i).draw(canvas); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 209 | // Noop: children are positioned absolutely |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | The following invalidation overrides exist for the purpose of redirecting invalidation to |
| 214 | the host view. The overlay is not parented to the host view (since a View cannot be a |
| 215 | parent), so the invalidation cannot proceed through the normal parent hierarchy. |
| 216 | There is a built-in assumption that the overlay exactly covers the host view, therefore |
| 217 | the invalidation rectangles received do not need to be adjusted when forwarded to |
| 218 | the host view. |
| 219 | */ |
| 220 | |
| 221 | @Override |
| 222 | public void invalidate(Rect dirty) { |
| 223 | super.invalidate(dirty); |
| 224 | if (mHostView != null) { |
| 225 | mHostView.invalidate(dirty); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void invalidate(int l, int t, int r, int b) { |
| 231 | super.invalidate(l, t, r, b); |
| 232 | if (mHostView != null) { |
| 233 | mHostView.invalidate(l, t, r, b); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void invalidate() { |
| 239 | super.invalidate(); |
| 240 | if (mHostView != null) { |
| 241 | mHostView.invalidate(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | void invalidate(boolean invalidateCache) { |
| 247 | super.invalidate(invalidateCache); |
| 248 | if (mHostView != null) { |
| 249 | mHostView.invalidate(invalidateCache); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) { |
| 255 | super.invalidateViewProperty(invalidateParent, forceRedraw); |
| 256 | if (mHostView != null) { |
| 257 | mHostView.invalidateViewProperty(invalidateParent, forceRedraw); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | protected void invalidateParentCaches() { |
| 263 | super.invalidateParentCaches(); |
| 264 | if (mHostView != null) { |
| 265 | mHostView.invalidateParentCaches(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | protected void invalidateParentIfNeeded() { |
| 271 | super.invalidateParentIfNeeded(); |
| 272 | if (mHostView != null) { |
| 273 | mHostView.invalidateParentIfNeeded(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public void invalidateChildFast(View child, final Rect dirty) { |
| 278 | if (mHostView != null) { |
| 279 | // Note: This is not a "fast" invalidation. Would be nice to instead invalidate |
| 280 | // using DisplayList properties and a dirty rect instead of causing a real |
| 281 | // invalidation of the host view |
| 282 | int left = child.mLeft; |
| 283 | int top = child.mTop; |
| 284 | if (!child.getMatrix().isIdentity()) { |
| 285 | child.transformRect(dirty); |
| 286 | } |
| 287 | dirty.offset(left, top); |
| 288 | mHostView.invalidate(dirty); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public ViewParent invalidateChildInParent(int[] location, Rect dirty) { |
| 294 | if (mHostView != null) { |
| 295 | dirty.offset(location[0], location[1]); |
| 296 | if (mHostView instanceof ViewGroup) { |
| 297 | location[0] = 0; |
| 298 | location[1] = 0; |
| 299 | super.invalidateChildInParent(location, dirty); |
| 300 | return ((ViewGroup) mHostView).invalidateChildInParent(location, dirty); |
| 301 | } else { |
| 302 | invalidate(dirty); |
| 303 | } |
| 304 | } |
| 305 | return null; |
| 306 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 307 | } |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 308 | |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 309 | } |