SlidingPaneLayout.java revision d0cb2111677748ec19a72e5fe18c8c64a359a751
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.support.v4.widget; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.Bitmap; 22import android.graphics.Canvas; 23import android.graphics.Paint; 24import android.graphics.PixelFormat; 25import android.graphics.PorterDuff; 26import android.graphics.PorterDuffColorFilter; 27import android.graphics.Rect; 28import android.graphics.drawable.Drawable; 29import android.os.Build; 30import android.os.Parcel; 31import android.os.Parcelable; 32import android.support.v4.view.AccessibilityDelegateCompat; 33import android.support.v4.view.MotionEventCompat; 34import android.support.v4.view.ViewCompat; 35import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; 36import android.util.AttributeSet; 37import android.util.Log; 38import android.view.MotionEvent; 39import android.view.View; 40import android.view.ViewConfiguration; 41import android.view.ViewGroup; 42import android.view.ViewParent; 43import android.view.accessibility.AccessibilityEvent; 44 45import java.lang.reflect.Field; 46import java.lang.reflect.Method; 47import java.util.ArrayList; 48 49/** 50 * SlidingPaneLayout provides a horizontal, multi-pane layout for use at the top level 51 * of a UI. A left (or first) pane is treated as a content list or browser, subordinate to a 52 * primary detail view for displaying content. 53 * 54 * <p>Child views may overlap if their combined width exceeds the available width 55 * in the SlidingPaneLayout. When this occurs the user may slide the topmost view out of the way 56 * by dragging it, or by navigating in the direction of the overlapped view using a keyboard. 57 * If the content of the dragged child view is itself horizontally scrollable, the user may 58 * grab it by the very edge.</p> 59 * 60 * <p>Thanks to this sliding behavior, SlidingPaneLayout may be suitable for creating layouts 61 * that can smoothly adapt across many different screen sizes, expanding out fully on larger 62 * screens and collapsing on smaller screens.</p> 63 * 64 * <p>SlidingPaneLayout is distinct from a navigation drawer as described in the design 65 * guide and should not be used in the same scenarios. SlidingPaneLayout should be thought 66 * of only as a way to allow a two-pane layout normally used on larger screens to adapt to smaller 67 * screens in a natural way. The interaction patterns expressed by SlidingPaneLayout imply 68 * a physicality and direct information hierarchy between panes that does not necessarily exist 69 * in a scenario where a navigation drawer should be used instead.</p> 70 * 71 * <p>Appropriate uses of SlidingPaneLayout include pairings of panes such as a contact list and 72 * subordinate interactions with those contacts, or an email thread list with the content pane 73 * displaying the contents of the selected thread. Inappropriate uses of SlidingPaneLayout include 74 * switching between disparate functions of your app, such as jumping from a social stream view 75 * to a view of your personal profile - cases such as this should use the navigation drawer 76 * pattern instead. ({@link DrawerLayout DrawerLayout} implements this pattern.)</p> 77 * 78 * <p>Like {@link android.widget.LinearLayout LinearLayout}, SlidingPaneLayout supports 79 * the use of the layout parameter <code>layout_weight</code> on child views to determine 80 * how to divide leftover space after measurement is complete. It is only relevant for width. 81 * When views do not overlap weight behaves as it does in a LinearLayout.</p> 82 * 83 * <p>When views do overlap, weight on a slideable pane indicates that the pane should be 84 * sized to fill all available space in the closed state. Weight on a pane that becomes covered 85 * indicates that the pane should be sized to fill all available space except a small minimum strip 86 * that the user may use to grab the slideable view and pull it back over into a closed state.</p> 87 * 88 * <p>Experimental. This class may be removed.</p> 89 */ 90public class SlidingPaneLayout extends ViewGroup { 91 private static final String TAG = "SlidingPaneLayout"; 92 93 /** 94 * Default size of the overhang for a pane in the open state. 95 * At least this much of a sliding pane will remain visible. 96 * This indicates that there is more content available and provides 97 * a "physical" edge to grab to pull it closed. 98 */ 99 private static final int DEFAULT_OVERHANG_SIZE = 32; // dp; 100 101 /** 102 * If no fade color is given by default it will fade to 80% gray. 103 */ 104 private static final int DEFAULT_FADE_COLOR = 0xcccccccc; 105 106 /** 107 * The fade color used for the sliding panel. 0 = no fading. 108 */ 109 private int mSliderFadeColor = DEFAULT_FADE_COLOR; 110 111 /** 112 * Minimum velocity that will be detected as a fling 113 */ 114 private static final int MIN_FLING_VELOCITY = 400; // dips per second 115 116 /** 117 * The fade color used for the panel covered by the slider. 0 = no fading. 118 */ 119 private int mCoveredFadeColor; 120 121 /** 122 * Drawable used to draw the shadow between panes. 123 */ 124 private Drawable mShadowDrawable; 125 126 /** 127 * The size of the overhang in pixels. 128 * This is the minimum section of the sliding panel that will 129 * be visible in the open state to allow for a closing drag. 130 */ 131 private final int mOverhangSize; 132 133 /** 134 * True if a panel can slide with the current measurements 135 */ 136 private boolean mCanSlide; 137 138 /** 139 * The child view that can slide, if any. 140 */ 141 private View mSlideableView; 142 143 /** 144 * How far the panel is offset from its closed position. 145 * range [0, 1] where 0 = closed, 1 = open. 146 */ 147 private float mSlideOffset; 148 149 /** 150 * How far the non-sliding panel is parallaxed from its usual position when open. 151 * range [0, 1] 152 */ 153 private float mParallaxOffset; 154 155 /** 156 * How far in pixels the slideable panel may move. 157 */ 158 private int mSlideRange; 159 160 /** 161 * A panel view is locked into internal scrolling or another condition that 162 * is preventing a drag. 163 */ 164 private boolean mIsUnableToDrag; 165 166 /** 167 * Distance in pixels to parallax the fixed pane by when fully closed 168 */ 169 private int mParallaxBy; 170 171 private float mInitialMotionX; 172 private float mInitialMotionY; 173 174 private PanelSlideListener mPanelSlideListener; 175 176 private final ViewDragHelper mDragHelper; 177 178 /** 179 * Stores whether or not the pane was open the last time it was slideable. 180 * If open/close operations are invoked this state is modified. Used by 181 * instance state save/restore. 182 */ 183 private boolean mPreservedOpenState; 184 private boolean mFirstLayout = true; 185 186 private final Rect mTmpRect = new Rect(); 187 188 private final ArrayList<DisableLayerRunnable> mPostedRunnables = 189 new ArrayList<DisableLayerRunnable>(); 190 191 static final SlidingPanelLayoutImpl IMPL; 192 193 static { 194 final int deviceVersion = Build.VERSION.SDK_INT; 195 if (deviceVersion >= 17) { 196 IMPL = new SlidingPanelLayoutImplJBMR1(); 197 } else if (deviceVersion >= 16) { 198 IMPL = new SlidingPanelLayoutImplJB(); 199 } else { 200 IMPL = new SlidingPanelLayoutImplBase(); 201 } 202 } 203 204 /** 205 * Listener for monitoring events about sliding panes. 206 */ 207 public interface PanelSlideListener { 208 /** 209 * Called when a sliding pane's position changes. 210 * @param panel The child view that was moved 211 * @param slideOffset The new offset of this sliding pane within its range, from 0-1 212 */ 213 public void onPanelSlide(View panel, float slideOffset); 214 /** 215 * Called when a sliding pane becomes slid completely open. The pane may or may not 216 * be interactive at this point depending on how much of the pane is visible. 217 * @param panel The child view that was slid to an open position, revealing other panes 218 */ 219 public void onPanelOpened(View panel); 220 221 /** 222 * Called when a sliding pane becomes slid completely closed. The pane is now guaranteed 223 * to be interactive. It may now obscure other views in the layout. 224 * @param panel The child view that was slid to a closed position 225 */ 226 public void onPanelClosed(View panel); 227 } 228 229 /** 230 * No-op stubs for {@link PanelSlideListener}. If you only want to implement a subset 231 * of the listener methods you can extend this instead of implement the full interface. 232 */ 233 public static class SimplePanelSlideListener implements PanelSlideListener { 234 @Override 235 public void onPanelSlide(View panel, float slideOffset) { 236 } 237 @Override 238 public void onPanelOpened(View panel) { 239 } 240 @Override 241 public void onPanelClosed(View panel) { 242 } 243 } 244 245 public SlidingPaneLayout(Context context) { 246 this(context, null); 247 } 248 249 public SlidingPaneLayout(Context context, AttributeSet attrs) { 250 this(context, attrs, 0); 251 } 252 253 public SlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) { 254 super(context, attrs, defStyle); 255 256 final float density = context.getResources().getDisplayMetrics().density; 257 mOverhangSize = (int) (DEFAULT_OVERHANG_SIZE * density + 0.5f); 258 259 final ViewConfiguration viewConfig = ViewConfiguration.get(context); 260 261 setWillNotDraw(false); 262 263 ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate()); 264 ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES); 265 266 mDragHelper = ViewDragHelper.create(this, 0.5f, new DragHelperCallback()); 267 mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT); 268 mDragHelper.setMinVelocity(MIN_FLING_VELOCITY * density); 269 } 270 271 /** 272 * Set a distance to parallax the lower pane by when the upper pane is in its 273 * fully closed state. The lower pane will scroll between this position and 274 * its fully open state. 275 * 276 * @param parallaxBy Distance to parallax by in pixels 277 */ 278 public void setParallaxDistance(int parallaxBy) { 279 mParallaxBy = parallaxBy; 280 requestLayout(); 281 } 282 283 /** 284 * @return The distance the lower pane will parallax by when the upper pane is fully closed. 285 * 286 * @see #setParallaxDistance(int) 287 */ 288 public int getParallaxDistance() { 289 return mParallaxBy; 290 } 291 292 /** 293 * Set the color used to fade the sliding pane out when it is slid most of the way offscreen. 294 * 295 * @param color An ARGB-packed color value 296 */ 297 public void setSliderFadeColor(int color) { 298 mSliderFadeColor = color; 299 } 300 301 /** 302 * @return The ARGB-packed color value used to fade the sliding pane 303 */ 304 public int getSliderFadeColor() { 305 return mSliderFadeColor; 306 } 307 308 /** 309 * Set the color used to fade the pane covered by the sliding pane out when the pane 310 * will become fully covered in the closed state. 311 * 312 * @param color An ARGB-packed color value 313 */ 314 public void setCoveredFadeColor(int color) { 315 mCoveredFadeColor = color; 316 } 317 318 /** 319 * @return The ARGB-packed color value used to fade the fixed pane 320 */ 321 public int getCoveredFadeColor() { 322 return mCoveredFadeColor; 323 } 324 325 public void setPanelSlideListener(PanelSlideListener listener) { 326 mPanelSlideListener = listener; 327 } 328 329 void dispatchOnPanelSlide(View panel) { 330 if (mPanelSlideListener != null) { 331 mPanelSlideListener.onPanelSlide(panel, mSlideOffset); 332 } 333 } 334 335 void dispatchOnPanelOpened(View panel) { 336 if (mPanelSlideListener != null) { 337 mPanelSlideListener.onPanelOpened(panel); 338 } 339 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 340 } 341 342 void dispatchOnPanelClosed(View panel) { 343 if (mPanelSlideListener != null) { 344 mPanelSlideListener.onPanelClosed(panel); 345 } 346 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 347 } 348 349 void updateObscuredViewsVisibility(View panel) { 350 final int leftBound = getPaddingLeft(); 351 final int rightBound = getWidth() - getPaddingRight(); 352 final int topBound = getPaddingTop(); 353 final int bottomBound = getHeight() - getPaddingBottom(); 354 final int left; 355 final int right; 356 final int top; 357 final int bottom; 358 if (panel != null && hasOpaqueBackground(panel)) { 359 left = panel.getLeft(); 360 right = panel.getRight(); 361 top = panel.getTop(); 362 bottom = panel.getBottom(); 363 } else { 364 left = right = top = bottom = 0; 365 } 366 367 for (int i = 0, childCount = getChildCount(); i < childCount; i++) { 368 final View child = getChildAt(i); 369 370 if (child == panel) { 371 // There are still more children above the panel but they won't be affected. 372 break; 373 } 374 375 final int clampedChildLeft = Math.max(leftBound, child.getLeft()); 376 final int clampedChildTop = Math.max(topBound, child.getTop()); 377 final int clampedChildRight = Math.min(rightBound, child.getRight()); 378 final int clampedChildBottom = Math.min(bottomBound, child.getBottom()); 379 final int vis; 380 if (clampedChildLeft >= left && clampedChildTop >= top && 381 clampedChildRight <= right && clampedChildBottom <= bottom) { 382 vis = INVISIBLE; 383 } else { 384 vis = VISIBLE; 385 } 386 child.setVisibility(vis); 387 } 388 } 389 390 void setAllChildrenVisible() { 391 for (int i = 0, childCount = getChildCount(); i < childCount; i++) { 392 final View child = getChildAt(i); 393 if (child.getVisibility() == INVISIBLE) { 394 child.setVisibility(VISIBLE); 395 } 396 } 397 } 398 399 private static boolean hasOpaqueBackground(View v) { 400 final Drawable bg = v.getBackground(); 401 if (bg != null) { 402 return bg.getOpacity() == PixelFormat.OPAQUE; 403 } 404 return false; 405 } 406 407 @Override 408 protected void onAttachedToWindow() { 409 super.onAttachedToWindow(); 410 mFirstLayout = true; 411 } 412 413 @Override 414 protected void onDetachedFromWindow() { 415 super.onDetachedFromWindow(); 416 mFirstLayout = true; 417 418 for (int i = 0, count = mPostedRunnables.size(); i < count; i++) { 419 final DisableLayerRunnable dlr = mPostedRunnables.get(i); 420 dlr.run(); 421 } 422 mPostedRunnables.clear(); 423 } 424 425 @Override 426 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 427 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 428 final int widthSize = MeasureSpec.getSize(widthMeasureSpec); 429 final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 430 final int heightSize = MeasureSpec.getSize(heightMeasureSpec); 431 432 if (widthMode != MeasureSpec.EXACTLY) { 433 throw new IllegalStateException("Width must have an exact value or MATCH_PARENT"); 434 } else if (heightMode == MeasureSpec.UNSPECIFIED) { 435 throw new IllegalStateException("Height must not be UNSPECIFIED"); 436 } 437 438 int layoutHeight = 0; 439 int maxLayoutHeight = -1; 440 switch (heightMode) { 441 case MeasureSpec.EXACTLY: 442 layoutHeight = maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom(); 443 break; 444 case MeasureSpec.AT_MOST: 445 maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom(); 446 break; 447 } 448 449 float weightSum = 0; 450 boolean canSlide = false; 451 int widthRemaining = widthSize - getPaddingLeft() - getPaddingRight(); 452 final int childCount = getChildCount(); 453 454 if (childCount > 2) { 455 Log.e(TAG, "onMeasure: More than two child views are not supported."); 456 } 457 458 // We'll find the current one below. 459 mSlideableView = null; 460 461 // First pass. Measure based on child LayoutParams width/height. 462 // Weight will incur a second pass. 463 for (int i = 0; i < childCount; i++) { 464 final View child = getChildAt(i); 465 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 466 467 if (child.getVisibility() == GONE) { 468 lp.dimWhenOffset = false; 469 continue; 470 } 471 472 if (lp.weight > 0) { 473 weightSum += lp.weight; 474 475 // If we have no width, weight is the only contributor to the final size. 476 // Measure this view on the weight pass only. 477 if (lp.width == 0) continue; 478 } 479 480 int childWidthSpec; 481 final int horizontalMargin = lp.leftMargin + lp.rightMargin; 482 if (lp.width == LayoutParams.WRAP_CONTENT) { 483 childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, 484 MeasureSpec.AT_MOST); 485 } else if (lp.width == LayoutParams.FILL_PARENT) { 486 childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, 487 MeasureSpec.EXACTLY); 488 } else { 489 childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); 490 } 491 492 int childHeightSpec; 493 if (lp.height == LayoutParams.WRAP_CONTENT) { 494 childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); 495 } else if (lp.height == LayoutParams.FILL_PARENT) { 496 childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); 497 } else { 498 childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); 499 } 500 501 child.measure(childWidthSpec, childHeightSpec); 502 final int childWidth = child.getMeasuredWidth(); 503 final int childHeight = child.getMeasuredHeight(); 504 505 if (heightMode == MeasureSpec.AT_MOST && childHeight > layoutHeight) { 506 layoutHeight = Math.min(childHeight, maxLayoutHeight); 507 } 508 509 widthRemaining -= childWidth; 510 canSlide |= lp.slideable = widthRemaining < 0; 511 if (lp.slideable) { 512 mSlideableView = child; 513 } 514 } 515 516 // Resolve weight and make sure non-sliding panels are smaller than the full screen. 517 if (canSlide || weightSum > 0) { 518 final int fixedPanelWidthLimit = widthSize - mOverhangSize; 519 520 for (int i = 0; i < childCount; i++) { 521 final View child = getChildAt(i); 522 523 if (child.getVisibility() == GONE) { 524 continue; 525 } 526 527 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 528 529 final boolean skippedFirstPass = lp.width == 0 && lp.weight > 0; 530 final int measuredWidth = skippedFirstPass ? 0 : child.getMeasuredWidth(); 531 if (canSlide && child != mSlideableView) { 532 if (lp.width < 0 && (measuredWidth > fixedPanelWidthLimit || lp.weight > 0)) { 533 // Fixed panels in a sliding configuration should 534 // be clamped to the fixed panel limit. 535 final int childHeightSpec; 536 if (skippedFirstPass) { 537 // Do initial height measurement if we skipped measuring this view 538 // the first time around. 539 if (lp.height == LayoutParams.WRAP_CONTENT) { 540 childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, 541 MeasureSpec.AT_MOST); 542 } else if (lp.height == LayoutParams.FILL_PARENT) { 543 childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, 544 MeasureSpec.EXACTLY); 545 } else { 546 childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, 547 MeasureSpec.EXACTLY); 548 } 549 } else { 550 childHeightSpec = MeasureSpec.makeMeasureSpec( 551 child.getMeasuredHeight(), MeasureSpec.EXACTLY); 552 } 553 final int childWidthSpec = MeasureSpec.makeMeasureSpec( 554 fixedPanelWidthLimit, MeasureSpec.EXACTLY); 555 child.measure(childWidthSpec, childHeightSpec); 556 } 557 } else if (lp.weight > 0) { 558 int childHeightSpec; 559 if (lp.width == 0) { 560 // This was skipped the first time; figure out a real height spec. 561 if (lp.height == LayoutParams.WRAP_CONTENT) { 562 childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, 563 MeasureSpec.AT_MOST); 564 } else if (lp.height == LayoutParams.FILL_PARENT) { 565 childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, 566 MeasureSpec.EXACTLY); 567 } else { 568 childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, 569 MeasureSpec.EXACTLY); 570 } 571 } else { 572 childHeightSpec = MeasureSpec.makeMeasureSpec( 573 child.getMeasuredHeight(), MeasureSpec.EXACTLY); 574 } 575 576 if (canSlide) { 577 // Consume available space 578 final int horizontalMargin = lp.leftMargin + lp.rightMargin; 579 final int newWidth = widthSize - horizontalMargin; 580 final int childWidthSpec = MeasureSpec.makeMeasureSpec( 581 newWidth, MeasureSpec.EXACTLY); 582 if (measuredWidth != newWidth) { 583 child.measure(childWidthSpec, childHeightSpec); 584 } 585 } else { 586 // Distribute the extra width proportionally similar to LinearLayout 587 final int widthToDistribute = Math.max(0, widthRemaining); 588 final int addedWidth = (int) (lp.weight * widthToDistribute / weightSum); 589 final int childWidthSpec = MeasureSpec.makeMeasureSpec( 590 measuredWidth + addedWidth, MeasureSpec.EXACTLY); 591 child.measure(childWidthSpec, childHeightSpec); 592 } 593 } 594 } 595 } 596 597 setMeasuredDimension(widthSize, layoutHeight); 598 mCanSlide = canSlide; 599 if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && !canSlide) { 600 // Cancel scrolling in progress, it's no longer relevant. 601 mDragHelper.abort(); 602 } 603 } 604 605 @Override 606 protected void onLayout(boolean changed, int l, int t, int r, int b) { 607 final int width = r - l; 608 final int paddingLeft = getPaddingLeft(); 609 final int paddingRight = getPaddingRight(); 610 final int paddingTop = getPaddingTop(); 611 612 final int childCount = getChildCount(); 613 int xStart = paddingLeft; 614 int nextXStart = xStart; 615 616 if (mFirstLayout) { 617 mSlideOffset = mCanSlide && mPreservedOpenState ? 1.f : 0.f; 618 } 619 620 for (int i = 0; i < childCount; i++) { 621 final View child = getChildAt(i); 622 623 if (child.getVisibility() == GONE) { 624 continue; 625 } 626 627 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 628 629 final int childWidth = child.getMeasuredWidth(); 630 int offset = 0; 631 632 if (lp.slideable) { 633 final int margin = lp.leftMargin + lp.rightMargin; 634 final int range = Math.min(nextXStart, 635 width - paddingRight - mOverhangSize) - xStart - margin; 636 mSlideRange = range; 637 lp.dimWhenOffset = xStart + lp.leftMargin + range + childWidth / 2 > 638 width - paddingRight; 639 xStart += (int) (range * mSlideOffset) + lp.leftMargin; 640 } else if (mCanSlide && mParallaxBy != 0) { 641 offset = (int) ((1 - mSlideOffset) * mParallaxBy); 642 xStart = nextXStart; 643 } else { 644 xStart = nextXStart; 645 } 646 647 final int childLeft = xStart - offset; 648 final int childRight = childLeft + childWidth; 649 final int childTop = paddingTop; 650 final int childBottom = childTop + child.getMeasuredHeight(); 651 child.layout(childLeft, paddingTop, childRight, childBottom); 652 653 nextXStart += child.getWidth(); 654 } 655 656 if (mFirstLayout) { 657 if (mCanSlide) { 658 if (mParallaxBy != 0) { 659 parallaxOtherViews(mSlideOffset); 660 } 661 if (((LayoutParams) mSlideableView.getLayoutParams()).dimWhenOffset) { 662 dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor); 663 } 664 } else { 665 // Reset the dim level of all children; it's irrelevant when nothing moves. 666 for (int i = 0; i < childCount; i++) { 667 dimChildView(getChildAt(i), 0, mSliderFadeColor); 668 } 669 } 670 updateObscuredViewsVisibility(mSlideableView); 671 } 672 673 mFirstLayout = false; 674 } 675 676 @Override 677 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 678 super.onSizeChanged(w, h, oldw, oldh); 679 // Recalculate sliding panes and their details 680 if (w != oldw) { 681 mFirstLayout = true; 682 } 683 } 684 685 @Override 686 public void requestChildFocus(View child, View focused) { 687 super.requestChildFocus(child, focused); 688 if (!isInTouchMode() && !mCanSlide) { 689 mPreservedOpenState = child == mSlideableView; 690 } 691 } 692 693 @Override 694 public boolean onInterceptTouchEvent(MotionEvent ev) { 695 final int action = MotionEventCompat.getActionMasked(ev); 696 697 // Preserve the open state based on the last view that was touched. 698 if (!mCanSlide && action == MotionEvent.ACTION_DOWN && getChildCount() > 1) { 699 // After the first things will be slideable. 700 final View secondChild = getChildAt(1); 701 if (secondChild != null) { 702 mPreservedOpenState = !mDragHelper.isViewUnder(secondChild, 703 (int) ev.getX(), (int) ev.getY()); 704 } 705 } 706 707 if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) { 708 mDragHelper.cancel(); 709 return super.onInterceptTouchEvent(ev); 710 } 711 712 if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { 713 mDragHelper.cancel(); 714 return false; 715 } 716 717 boolean interceptTap = false; 718 719 switch (action) { 720 case MotionEvent.ACTION_DOWN: { 721 mIsUnableToDrag = false; 722 final float x = ev.getX(); 723 final float y = ev.getY(); 724 mInitialMotionX = x; 725 mInitialMotionY = y; 726 727 if (mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y) && 728 isDimmed(mSlideableView)) { 729 interceptTap = true; 730 } 731 break; 732 } 733 734 case MotionEvent.ACTION_MOVE: { 735 final float x = ev.getX(); 736 final float y = ev.getY(); 737 final float adx = Math.abs(x - mInitialMotionX); 738 final float ady = Math.abs(y - mInitialMotionY); 739 final int slop = mDragHelper.getTouchSlop(); 740 if (adx > slop && ady > adx) { 741 mDragHelper.cancel(); 742 mIsUnableToDrag = true; 743 return false; 744 } 745 } 746 } 747 748 final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev); 749 750 return interceptForDrag || interceptTap; 751 } 752 753 @Override 754 public boolean onTouchEvent(MotionEvent ev) { 755 if (!mCanSlide) { 756 return super.onTouchEvent(ev); 757 } 758 759 mDragHelper.processTouchEvent(ev); 760 761 final int action = ev.getAction(); 762 boolean wantTouchEvents = true; 763 764 switch (action & MotionEventCompat.ACTION_MASK) { 765 case MotionEvent.ACTION_DOWN: { 766 final float x = ev.getX(); 767 final float y = ev.getY(); 768 mInitialMotionX = x; 769 mInitialMotionY = y; 770 break; 771 } 772 773 case MotionEvent.ACTION_UP: { 774 if (isDimmed(mSlideableView)) { 775 final float x = ev.getX(); 776 final float y = ev.getY(); 777 final float dx = x - mInitialMotionX; 778 final float dy = y - mInitialMotionY; 779 final int slop = mDragHelper.getTouchSlop(); 780 if (dx * dx + dy * dy < slop * slop && 781 mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y)) { 782 // Taps close a dimmed open pane. 783 closePane(mSlideableView, 0); 784 break; 785 } 786 } 787 break; 788 } 789 } 790 791 return wantTouchEvents; 792 } 793 794 private boolean closePane(View pane, int initialVelocity) { 795 if (mFirstLayout || smoothSlideTo(0.f, initialVelocity)) { 796 mPreservedOpenState = false; 797 return true; 798 } 799 return false; 800 } 801 802 private boolean openPane(View pane, int initialVelocity) { 803 if (mFirstLayout || smoothSlideTo(1.f, initialVelocity)) { 804 mPreservedOpenState = true; 805 return true; 806 } 807 return false; 808 } 809 810 /** 811 * @deprecated Renamed to {@link #openPane()} - this method is going away soon! 812 */ 813 @Deprecated 814 public void smoothSlideOpen() { 815 openPane(); 816 } 817 818 /** 819 * Open the sliding pane if it is currently slideable. If first layout 820 * has already completed this will animate. 821 * 822 * @return true if the pane was slideable and is now open/in the process of opening 823 */ 824 public boolean openPane() { 825 return openPane(mSlideableView, 0); 826 } 827 828 /** 829 * @deprecated Renamed to {@link #closePane()} - this method is going away soon! 830 */ 831 @Deprecated 832 public void smoothSlideClosed() { 833 closePane(); 834 } 835 836 /** 837 * Close the sliding pane if it is currently slideable. If first layout 838 * has already completed this will animate. 839 * 840 * @return true if the pane was slideable and is now closed/in the process of closing 841 */ 842 public boolean closePane() { 843 return closePane(mSlideableView, 0); 844 } 845 846 /** 847 * Check if the layout is completely open. It can be open either because the slider 848 * itself is open revealing the left pane, or if all content fits without sliding. 849 * 850 * @return true if sliding panels are completely open 851 */ 852 public boolean isOpen() { 853 return !mCanSlide || mSlideOffset == 1; 854 } 855 856 /** 857 * @return true if content in this layout can be slid open and closed 858 * @deprecated Renamed to {@link #isSlideable()} - this method is going away soon! 859 */ 860 @Deprecated 861 public boolean canSlide() { 862 return mCanSlide; 863 } 864 865 /** 866 * Check if the content in this layout cannot fully fit side by side and therefore 867 * the content pane can be slid back and forth. 868 * 869 * @return true if content in this layout can be slid open and closed 870 */ 871 public boolean isSlideable() { 872 return mCanSlide; 873 } 874 875 private void onPanelDragged(int newLeft) { 876 final LayoutParams lp = (LayoutParams) mSlideableView.getLayoutParams(); 877 final int leftBound = getPaddingLeft() + lp.leftMargin; 878 879 mSlideOffset = (float) (newLeft - leftBound) / mSlideRange; 880 881 if (mParallaxBy != 0) { 882 parallaxOtherViews(mSlideOffset); 883 } 884 885 if (lp.dimWhenOffset) { 886 dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor); 887 } 888 dispatchOnPanelSlide(mSlideableView); 889 } 890 891 private void dimChildView(View v, float mag, int fadeColor) { 892 final LayoutParams lp = (LayoutParams) v.getLayoutParams(); 893 894 if (mag > 0 && fadeColor != 0) { 895 final int baseAlpha = (fadeColor & 0xff000000) >>> 24; 896 int imag = (int) (baseAlpha * mag); 897 int color = imag << 24 | (fadeColor & 0xffffff); 898 if (lp.dimPaint == null) { 899 lp.dimPaint = new Paint(); 900 } 901 lp.dimPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_OVER)); 902 if (ViewCompat.getLayerType(v) != ViewCompat.LAYER_TYPE_HARDWARE) { 903 ViewCompat.setLayerType(v, ViewCompat.LAYER_TYPE_HARDWARE, lp.dimPaint); 904 } 905 invalidateChildRegion(v); 906 } else if (ViewCompat.getLayerType(v) != ViewCompat.LAYER_TYPE_NONE) { 907 if (lp.dimPaint != null) { 908 lp.dimPaint.setColorFilter(null); 909 } 910 final DisableLayerRunnable dlr = new DisableLayerRunnable(v); 911 mPostedRunnables.add(dlr); 912 ViewCompat.postOnAnimation(this, dlr); 913 } 914 } 915 916 @Override 917 protected boolean drawChild(Canvas canvas, View child, long drawingTime) { 918 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 919 boolean result; 920 final int save = canvas.save(Canvas.CLIP_SAVE_FLAG); 921 922 if (mCanSlide && !lp.slideable && mSlideableView != null) { 923 // Clip against the slider; no sense drawing what will immediately be covered. 924 canvas.getClipBounds(mTmpRect); 925 mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft()); 926 canvas.clipRect(mTmpRect); 927 } 928 929 if (Build.VERSION.SDK_INT >= 11) { // HC 930 result = super.drawChild(canvas, child, drawingTime); 931 } else { 932 if (lp.dimWhenOffset && mSlideOffset > 0) { 933 if (!child.isDrawingCacheEnabled()) { 934 child.setDrawingCacheEnabled(true); 935 } 936 final Bitmap cache = child.getDrawingCache(); 937 if (cache != null) { 938 canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint); 939 result = false; 940 } else { 941 Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache"); 942 result = super.drawChild(canvas, child, drawingTime); 943 } 944 } else { 945 if (child.isDrawingCacheEnabled()) { 946 child.setDrawingCacheEnabled(false); 947 } 948 result = super.drawChild(canvas, child, drawingTime); 949 } 950 } 951 952 canvas.restoreToCount(save); 953 954 return result; 955 } 956 957 private void invalidateChildRegion(View v) { 958 IMPL.invalidateChildRegion(this, v); 959 } 960 961 /** 962 * Smoothly animate mDraggingPane to the target X position within its range. 963 * 964 * @param slideOffset position to animate to 965 * @param velocity initial velocity in case of fling, or 0. 966 */ 967 boolean smoothSlideTo(float slideOffset, int velocity) { 968 if (!mCanSlide) { 969 // Nothing to do. 970 return false; 971 } 972 973 final LayoutParams lp = (LayoutParams) mSlideableView.getLayoutParams(); 974 975 final int leftBound = getPaddingLeft() + lp.leftMargin; 976 int x = (int) (leftBound + slideOffset * mSlideRange); 977 978 if (mDragHelper.smoothSlideViewTo(mSlideableView, x, mSlideableView.getTop())) { 979 setAllChildrenVisible(); 980 ViewCompat.postInvalidateOnAnimation(this); 981 return true; 982 } 983 return false; 984 } 985 986 @Override 987 public void computeScroll() { 988 if (mDragHelper.continueSettling(true)) { 989 if (!mCanSlide) { 990 mDragHelper.abort(); 991 return; 992 } 993 994 ViewCompat.postInvalidateOnAnimation(this); 995 } 996 } 997 998 /** 999 * Set a drawable to use as a shadow cast by the right pane onto the left pane 1000 * during opening/closing. 1001 * 1002 * @param d drawable to use as a shadow 1003 */ 1004 public void setShadowDrawable(Drawable d) { 1005 mShadowDrawable = d; 1006 } 1007 1008 /** 1009 * Set a drawable to use as a shadow cast by the right pane onto the left pane 1010 * during opening/closing. 1011 * 1012 * @param resId Resource ID of a drawable to use 1013 */ 1014 public void setShadowResource(int resId) { 1015 setShadowDrawable(getResources().getDrawable(resId)); 1016 } 1017 1018 @Override 1019 public void draw(Canvas c) { 1020 super.draw(c); 1021 1022 final View shadowView = getChildCount() > 1 ? getChildAt(1) : null; 1023 if (shadowView == null || mShadowDrawable == null) { 1024 // No need to draw a shadow if we don't have one. 1025 return; 1026 } 1027 1028 final int shadowWidth = mShadowDrawable.getIntrinsicWidth(); 1029 final int right = shadowView.getLeft(); 1030 final int top = shadowView.getTop(); 1031 final int bottom = shadowView.getBottom(); 1032 final int left = right - shadowWidth; 1033 mShadowDrawable.setBounds(left, top, right, bottom); 1034 mShadowDrawable.draw(c); 1035 } 1036 1037 private void parallaxOtherViews(float slideOffset) { 1038 final LayoutParams slideLp = (LayoutParams) mSlideableView.getLayoutParams(); 1039 final boolean dimViews = slideLp.dimWhenOffset && slideLp.leftMargin <= 0; 1040 final int childCount = getChildCount(); 1041 for (int i = 0; i < childCount; i++) { 1042 final View v = getChildAt(i); 1043 if (v == mSlideableView) continue; 1044 1045 final int oldOffset = (int) ((1 - mParallaxOffset) * mParallaxBy); 1046 mParallaxOffset = slideOffset; 1047 final int newOffset = (int) ((1 - slideOffset) * mParallaxBy); 1048 final int dx = oldOffset - newOffset; 1049 1050 v.offsetLeftAndRight(dx); 1051 1052 if (dimViews) { 1053 dimChildView(v, 1 - mParallaxOffset, mCoveredFadeColor); 1054 } 1055 } 1056 } 1057 1058 /** 1059 * Tests scrollability within child views of v given a delta of dx. 1060 * 1061 * @param v View to test for horizontal scrollability 1062 * @param checkV Whether the view v passed should itself be checked for scrollability (true), 1063 * or just its children (false). 1064 * @param dx Delta scrolled in pixels 1065 * @param x X coordinate of the active touch point 1066 * @param y Y coordinate of the active touch point 1067 * @return true if child views of v can be scrolled by delta of dx. 1068 */ 1069 protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { 1070 if (v instanceof ViewGroup) { 1071 final ViewGroup group = (ViewGroup) v; 1072 final int scrollX = v.getScrollX(); 1073 final int scrollY = v.getScrollY(); 1074 final int count = group.getChildCount(); 1075 // Count backwards - let topmost views consume scroll distance first. 1076 for (int i = count - 1; i >= 0; i--) { 1077 // TODO: Add versioned support here for transformed views. 1078 // This will not work for transformed views in Honeycomb+ 1079 final View child = group.getChildAt(i); 1080 if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() && 1081 y + scrollY >= child.getTop() && y + scrollY < child.getBottom() && 1082 canScroll(child, true, dx, x + scrollX - child.getLeft(), 1083 y + scrollY - child.getTop())) { 1084 return true; 1085 } 1086 } 1087 } 1088 1089 return checkV && ViewCompat.canScrollHorizontally(v, -dx); 1090 } 1091 1092 boolean isDimmed(View child) { 1093 if (child == null) { 1094 return false; 1095 } 1096 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 1097 return mCanSlide && lp.dimWhenOffset && mSlideOffset > 0; 1098 } 1099 1100 @Override 1101 protected ViewGroup.LayoutParams generateDefaultLayoutParams() { 1102 return new LayoutParams(); 1103 } 1104 1105 @Override 1106 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 1107 return p instanceof MarginLayoutParams 1108 ? new LayoutParams((MarginLayoutParams) p) 1109 : new LayoutParams(p); 1110 } 1111 1112 @Override 1113 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 1114 return p instanceof LayoutParams && super.checkLayoutParams(p); 1115 } 1116 1117 @Override 1118 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { 1119 return new LayoutParams(getContext(), attrs); 1120 } 1121 1122 @Override 1123 protected Parcelable onSaveInstanceState() { 1124 Parcelable superState = super.onSaveInstanceState(); 1125 1126 SavedState ss = new SavedState(superState); 1127 ss.isOpen = isSlideable() ? isOpen() : mPreservedOpenState; 1128 1129 return ss; 1130 } 1131 1132 @Override 1133 protected void onRestoreInstanceState(Parcelable state) { 1134 SavedState ss = (SavedState) state; 1135 super.onRestoreInstanceState(ss.getSuperState()); 1136 1137 if (ss.isOpen) { 1138 openPane(); 1139 } else { 1140 closePane(); 1141 } 1142 mPreservedOpenState = ss.isOpen; 1143 } 1144 1145 private class DragHelperCallback extends ViewDragHelper.Callback { 1146 1147 @Override 1148 public boolean tryCaptureView(View child, int pointerId) { 1149 if (mIsUnableToDrag) { 1150 return false; 1151 } 1152 1153 return ((LayoutParams) child.getLayoutParams()).slideable; 1154 } 1155 1156 @Override 1157 public void onViewDragStateChanged(int state) { 1158 if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) { 1159 if (mSlideOffset == 0) { 1160 updateObscuredViewsVisibility(mSlideableView); 1161 dispatchOnPanelClosed(mSlideableView); 1162 mPreservedOpenState = false; 1163 } else { 1164 dispatchOnPanelOpened(mSlideableView); 1165 mPreservedOpenState = true; 1166 } 1167 } 1168 } 1169 1170 @Override 1171 public void onViewCaptured(View capturedChild, int activePointerId) { 1172 // Make all child views visible in preparation for sliding things around 1173 setAllChildrenVisible(); 1174 } 1175 1176 @Override 1177 public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { 1178 onPanelDragged(left); 1179 invalidate(); 1180 } 1181 1182 @Override 1183 public void onViewReleased(View releasedChild, float xvel, float yvel) { 1184 final LayoutParams lp = (LayoutParams) releasedChild.getLayoutParams(); 1185 int left = getPaddingLeft() + lp.leftMargin; 1186 if (xvel > 0 || (xvel == 0 && mSlideOffset > 0.5f)) { 1187 left += mSlideRange; 1188 } 1189 mDragHelper.settleCapturedViewAt(left, releasedChild.getTop()); 1190 invalidate(); 1191 } 1192 1193 @Override 1194 public int getViewHorizontalDragRange(View child) { 1195 return mSlideRange; 1196 } 1197 1198 @Override 1199 public int clampViewPositionHorizontal(View child, int left, int dx) { 1200 final LayoutParams lp = (LayoutParams) mSlideableView.getLayoutParams(); 1201 final int leftBound = getPaddingLeft() + lp.leftMargin; 1202 final int rightBound = leftBound + mSlideRange; 1203 1204 final int newLeft = Math.min(Math.max(left, leftBound), rightBound); 1205 1206 return newLeft; 1207 } 1208 1209 @Override 1210 public void onEdgeDragStarted(int edgeFlags, int pointerId) { 1211 mDragHelper.captureChildView(mSlideableView, pointerId); 1212 } 1213 } 1214 1215 public static class LayoutParams extends ViewGroup.MarginLayoutParams { 1216 private static final int[] ATTRS = new int[] { 1217 android.R.attr.layout_weight 1218 }; 1219 1220 /** 1221 * The weighted proportion of how much of the leftover space 1222 * this child should consume after measurement. 1223 */ 1224 public float weight = 0; 1225 1226 /** 1227 * True if this pane is the slideable pane in the layout. 1228 */ 1229 boolean slideable; 1230 1231 /** 1232 * True if this view should be drawn dimmed 1233 * when it's been offset from its default position. 1234 */ 1235 boolean dimWhenOffset; 1236 1237 Paint dimPaint; 1238 1239 public LayoutParams() { 1240 super(FILL_PARENT, FILL_PARENT); 1241 } 1242 1243 public LayoutParams(int width, int height) { 1244 super(width, height); 1245 } 1246 1247 public LayoutParams(android.view.ViewGroup.LayoutParams source) { 1248 super(source); 1249 } 1250 1251 public LayoutParams(MarginLayoutParams source) { 1252 super(source); 1253 } 1254 1255 public LayoutParams(LayoutParams source) { 1256 super(source); 1257 this.weight = source.weight; 1258 } 1259 1260 public LayoutParams(Context c, AttributeSet attrs) { 1261 super(c, attrs); 1262 1263 final TypedArray a = c.obtainStyledAttributes(attrs, ATTRS); 1264 this.weight = a.getFloat(0, 0); 1265 a.recycle(); 1266 } 1267 1268 } 1269 1270 static class SavedState extends BaseSavedState { 1271 boolean isOpen; 1272 1273 SavedState(Parcelable superState) { 1274 super(superState); 1275 } 1276 1277 private SavedState(Parcel in) { 1278 super(in); 1279 isOpen = in.readInt() != 0; 1280 } 1281 1282 @Override 1283 public void writeToParcel(Parcel out, int flags) { 1284 super.writeToParcel(out, flags); 1285 out.writeInt(isOpen ? 1 : 0); 1286 } 1287 1288 public static final Parcelable.Creator<SavedState> CREATOR = 1289 new Parcelable.Creator<SavedState>() { 1290 public SavedState createFromParcel(Parcel in) { 1291 return new SavedState(in); 1292 } 1293 1294 public SavedState[] newArray(int size) { 1295 return new SavedState[size]; 1296 } 1297 }; 1298 } 1299 1300 interface SlidingPanelLayoutImpl { 1301 void invalidateChildRegion(SlidingPaneLayout parent, View child); 1302 } 1303 1304 static class SlidingPanelLayoutImplBase implements SlidingPanelLayoutImpl { 1305 public void invalidateChildRegion(SlidingPaneLayout parent, View child) { 1306 ViewCompat.postInvalidateOnAnimation(parent, child.getLeft(), child.getTop(), 1307 child.getRight(), child.getBottom()); 1308 } 1309 } 1310 1311 static class SlidingPanelLayoutImplJB extends SlidingPanelLayoutImplBase { 1312 /* 1313 * Private API hacks! Nasty! Bad! 1314 * 1315 * In Jellybean, some optimizations in the hardware UI renderer 1316 * prevent a changed Paint on a View using a hardware layer from having 1317 * the intended effect. This twiddles some internal bits on the view to force 1318 * it to recreate the display list. 1319 */ 1320 private Method mGetDisplayList; 1321 private Field mRecreateDisplayList; 1322 1323 SlidingPanelLayoutImplJB() { 1324 try { 1325 mGetDisplayList = View.class.getDeclaredMethod("getDisplayList", (Class[]) null); 1326 } catch (NoSuchMethodException e) { 1327 Log.e(TAG, "Couldn't fetch getDisplayList method; dimming won't work right.", e); 1328 } 1329 try { 1330 mRecreateDisplayList = View.class.getDeclaredField("mRecreateDisplayList"); 1331 mRecreateDisplayList.setAccessible(true); 1332 } catch (NoSuchFieldException e) { 1333 Log.e(TAG, "Couldn't fetch mRecreateDisplayList field; dimming will be slow.", e); 1334 } 1335 } 1336 1337 @Override 1338 public void invalidateChildRegion(SlidingPaneLayout parent, View child) { 1339 if (mGetDisplayList != null && mRecreateDisplayList != null) { 1340 try { 1341 mRecreateDisplayList.setBoolean(child, true); 1342 mGetDisplayList.invoke(child, (Object[]) null); 1343 } catch (Exception e) { 1344 Log.e(TAG, "Error refreshing display list state", e); 1345 } 1346 } else { 1347 // Slow path. REALLY slow path. Let's hope we don't get here. 1348 child.invalidate(); 1349 return; 1350 } 1351 super.invalidateChildRegion(parent, child); 1352 } 1353 } 1354 1355 static class SlidingPanelLayoutImplJBMR1 extends SlidingPanelLayoutImplBase { 1356 @Override 1357 public void invalidateChildRegion(SlidingPaneLayout parent, View child) { 1358 ViewCompat.setLayerPaint(child, ((LayoutParams) child.getLayoutParams()).dimPaint); 1359 } 1360 } 1361 1362 class AccessibilityDelegate extends AccessibilityDelegateCompat { 1363 private final Rect mTmpRect = new Rect(); 1364 1365 @Override 1366 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { 1367 final AccessibilityNodeInfoCompat superNode = AccessibilityNodeInfoCompat.obtain(info); 1368 super.onInitializeAccessibilityNodeInfo(host, superNode); 1369 1370 info.setSource(host); 1371 final ViewParent parent = ViewCompat.getParentForAccessibility(host); 1372 if (parent instanceof View) { 1373 info.setParent((View) parent); 1374 } 1375 copyNodeInfoNoChildren(info, superNode); 1376 1377 superNode.recycle(); 1378 1379 final int childCount = getChildCount(); 1380 for (int i = 0; i < childCount; i++) { 1381 final View child = getChildAt(i); 1382 if (!filter(child)) { 1383 info.addChild(child); 1384 } 1385 } 1386 } 1387 1388 @Override 1389 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child, 1390 AccessibilityEvent event) { 1391 if (!filter(child)) { 1392 return super.onRequestSendAccessibilityEvent(host, child, event); 1393 } 1394 return false; 1395 } 1396 1397 public boolean filter(View child) { 1398 return isDimmed(child); 1399 } 1400 1401 /** 1402 * This should really be in AccessibilityNodeInfoCompat, but there unfortunately 1403 * seem to be a few elements that are not easily cloneable using the underlying API. 1404 * Leave it private here as it's not general-purpose useful. 1405 */ 1406 private void copyNodeInfoNoChildren(AccessibilityNodeInfoCompat dest, 1407 AccessibilityNodeInfoCompat src) { 1408 final Rect rect = mTmpRect; 1409 1410 src.getBoundsInParent(rect); 1411 dest.setBoundsInParent(rect); 1412 1413 src.getBoundsInScreen(rect); 1414 dest.setBoundsInScreen(rect); 1415 1416 dest.setVisibleToUser(src.isVisibleToUser()); 1417 dest.setPackageName(src.getPackageName()); 1418 dest.setClassName(src.getClassName()); 1419 dest.setContentDescription(src.getContentDescription()); 1420 1421 dest.setEnabled(src.isEnabled()); 1422 dest.setClickable(src.isClickable()); 1423 dest.setFocusable(src.isFocusable()); 1424 dest.setFocused(src.isFocused()); 1425 dest.setAccessibilityFocused(src.isAccessibilityFocused()); 1426 dest.setSelected(src.isSelected()); 1427 dest.setLongClickable(src.isLongClickable()); 1428 1429 dest.addAction(src.getActions()); 1430 } 1431 } 1432 1433 private class DisableLayerRunnable implements Runnable { 1434 final View mChildView; 1435 1436 DisableLayerRunnable(View childView) { 1437 mChildView = childView; 1438 } 1439 1440 @Override 1441 public void run() { 1442 if (mChildView.getParent() == SlidingPaneLayout.this) { 1443 ViewCompat.setLayerType(mChildView, ViewCompat.LAYER_TYPE_NONE, null); 1444 invalidateChildRegion(mChildView); 1445 } 1446 mPostedRunnables.remove(this); 1447 } 1448 } 1449} 1450