Code example for View

Methods: refreshDrawableState, setBackgroundDrawable, setFitsSystemWindows

0
        if (mContext != null) {
            p.packageName = mContext.getPackageName();
        } 
        if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
            mPopupView.setFitsSystemWindows(mLayoutInsetDecor);
        } 
        mWindowManager.addView(mPopupView, p);
    } 
 
    public boolean isAboveAnchor() { 
        return mAboveAnchor;
    } 
 
    public boolean isClippingEnabled() { 
        return mClippingEnabled;
    } 
 
    public boolean isFocusable() { 
        return mFocusable;
    } 
 
    public boolean isLayoutInScreenEnabled() { 
        return mLayoutInScreen;
    } 
 
    public boolean isOutsideTouchable() { 
        return mOutsideTouchable;
    } 
 
    public boolean isShowing() { 
        return mIsShowing;
    } 
 
    public boolean isSplitTouchEnabled() { 
        if (mSplitTouchEnabled < 0 && mContext != null) {
            return mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB;
        } 
        return mSplitTouchEnabled == 1;
    } 
 
    public boolean isTouchable() { 
        return mTouchable;
    } 
 
    @SuppressWarnings("deprecation") 
    private void preparePopup(WindowManager.LayoutParams p) {
        if (mContentView == null || mContext == null || mWindowManager == null) {
            throw new IllegalStateException("You must specify a valid content view by "
                    + "calling setContentView() before attempting to show the popup."); 
        } 
        if (mBackground != null) {
            final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            if (layoutParams != null &&
                    layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
                height = ViewGroup.LayoutParams.WRAP_CONTENT;
            } 
            PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
            PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, height
            ); 
            popupViewContainer.setBackgroundDrawable(mBackground);
            popupViewContainer.addView(mContentView, listParams);
            mPopupView = popupViewContainer;
        } else { 
            mPopupView = mContentView;
        } 
        mPopupWidth = p.width;
        mPopupHeight = p.height;
    } 
 
    private void registerForScrollChanged(View anchor, int xoff, int yoff) {
        unregisterForScrollChanged(); 
        mAnchor = new WeakReference<View>(anchor);
        ViewTreeObserver vto = anchor.getViewTreeObserver();
        if (vto != null) {
            vto.addOnScrollChangedListener(mOnScrollChangedListener);
        } 
        mAnchorXoff = xoff;
        mAnchorYoff = yoff;
    } 
 
    void setAllowScrollingAnchorParent(boolean enabled) {
        mAllowScrollingAnchorParent = enabled;
    } 
 
    public void setAnimationStyle(int animationStyle) {
        mAnimationStyle = animationStyle;
    } 
 
    public void setBackgroundDrawable(Drawable background) {
        mBackground = background;
    } 
 
    public void setClippingEnabled(boolean enabled) {
        mClippingEnabled = enabled;
    } 
 
    public void setClipToScreenEnabled(boolean enabled) {
        mClipToScreen = enabled;
        setClippingEnabled(!enabled);
    } 
 
    public void setContentView(View contentView) {
        if (isShowing()) { 
            return; 
        } 
        mContentView = contentView;
        if (mContext == null && mContentView != null) {
            mContext = mContentView.getContext();
        } 
        if (mWindowManager == null && mContext != null) {
            mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        } 
    } 
 
    public void setFocusable(boolean focusable) {
        mFocusable = focusable;
    } 
 
    public void setHeight(int height) {
        mHeight = height;
    } 
 
    public void setIgnoreCheekPress() { 
        mIgnoreCheekPress = true;
    } 
 
    public void setInputMethodMode(int mode) {
        mInputMethodMode = mode;
    } 
 
    public void setLayoutInScreenEnabled(boolean enabled) {
        mLayoutInScreen = enabled;
    } 
 
    public void setLayoutInsetDecor(boolean enabled) {
        mLayoutInsetDecor = enabled;
    } 
 
    public void setOnDismissListener(OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
    } 
 
    public void setOutsideTouchable(boolean touchable) {
        mOutsideTouchable = touchable;
    } 
 
    public void setSoftInputMode(int mode) {
        mSoftInputMode = mode;
    } 
 
    public void setSplitTouchEnabled(boolean enabled) {
        mSplitTouchEnabled = enabled ? 1 : 0;
    } 
 
    public void setTouchable(boolean touchable) {
        mTouchable = touchable;
    } 
 
    public void setTouchInterceptor(OnTouchListener l) {
        mTouchInterceptor = l;
    } 
 
    public void setTouchModal(boolean touchModal) {
        mNotTouchModal = !touchModal;
    } 
 
    public void setWidth(int width) {
        mWidth = width;
    } 
 
    public void setWindowLayoutMode(int widthSpec, int heightSpec) {
        mWidthMode = widthSpec;
        mHeightMode = heightSpec;
    } 
 
    public void setWindowLayoutType(int layoutType) {
        mWindowLayoutType = layoutType;
    } 
 
    public void showAsDropDown(View anchor) {
        showAsDropDown(anchor, 0, 0);
    } 
 
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (isShowing() || mContentView == null) {
            return; 
        } 
        registerForScrollChanged(anchor, xoff, yoff);
        mIsShowing = true;
        mIsDropdown = true;
        WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
        preparePopup(p);
        updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
        if (mHeightMode < 0) {
            p.height = mLastHeight = mHeightMode;
        } 
        if (mWidthMode < 0) {
            p.width = mLastWidth = mWidthMode;
        } 
        p.windowAnimations = computeAnimationResource();
        invokePopup(p);
    } 
 
    public void showAtLocation(IBinder token, int gravity, int x, int y) {
        if (isShowing() || mContentView == null) {
            return; 
        } 
        unregisterForScrollChanged(); 
        mIsShowing = true;
        mIsDropdown = false;
        WindowManager.LayoutParams p = createPopupLayout(token);
        p.windowAnimations = computeAnimationResource();
        preparePopup(p);
        if (gravity == Gravity.NO_GRAVITY) {
            gravity = Gravity.TOP | Gravity.LEFT;
        } 
        p.gravity = gravity;
        p.x = x;
        p.y = y;
        if (mHeightMode < 0) {
            p.height = mLastHeight = mHeightMode;
        } 
        if (mWidthMode < 0) {
            p.width = mLastWidth = mWidthMode;
        } 
        invokePopup(p);
    } 
 
    public void showAtLocation(View parent, int gravity, int x, int y) {
        showAtLocation(parent.getWindowToken(), gravity, x, y);
    } 
 
    private void unregisterForScrollChanged() { 
        WeakReference<View> anchorRef = mAnchor;
        View anchor = null;
        if (anchorRef != null) {
            anchor = anchorRef.get();
        } 
        if (anchor != null) {
            ViewTreeObserver vto = anchor.getViewTreeObserver();
            vto.removeOnScrollChangedListener(mOnScrollChangedListener);
        } 
        mAnchor = null;
    } 
 
    public void update() { 
        if (!isShowing() || mContentView == null) {
            return; 
        } 
        WindowManager.LayoutParams p = (WindowManager.LayoutParams)
                mPopupView.getLayoutParams();
        boolean update = false;
        final int newAnim = computeAnimationResource();
        if (newAnim != p.windowAnimations) {
            p.windowAnimations = newAnim;
            update = true;
        } 
        final int newFlags = computeFlags(p.flags);
        if (newFlags != p.flags) {
            p.flags = newFlags;
            update = true;
        } 
        if (update) {
            mWindowManager.updateViewLayout(mPopupView, p);
        } 
    } 
 
    public void update(int width, int height) {
        WindowManager.LayoutParams p = (WindowManager.LayoutParams)
                mPopupView.getLayoutParams();
        update(p.x, p.y, width, height, false);
    } 
 
    public void update(int x, int y, int width, int height) {
        update(x, y, width, height, false);
    } 
 
    public void update(int x, int y, int width, int height, boolean force) {
        if (width != -1) {
            mLastWidth = width;
            setWidth(width);
        } 
        if (height != -1) {
            mLastHeight = height;
            setHeight(height);
        } 
        if (!isShowing() || mContentView == null) {
            return; 
        } 
        WindowManager.LayoutParams p = (WindowManager.LayoutParams) mPopupView.getLayoutParams();
        boolean update = force;
        final int finalWidth = mWidthMode < 0 ? mWidthMode : mLastWidth;
        if (width != -1 && p.width != finalWidth) {
            p.width = mLastWidth = finalWidth;
            update = true;
        } 
        final int finalHeight = mHeightMode < 0 ? mHeightMode : mLastHeight;
        if (height != -1 && p.height != finalHeight) {
            p.height = mLastHeight = finalHeight;
            update = true;
        } 
        if (p.x != x) {
            p.x = x;
            update = true;
        } 
        if (p.y != y) {
            p.y = y;
            update = true;
        } 
        final int newAnim = computeAnimationResource();
        if (newAnim != p.windowAnimations) {
            p.windowAnimations = newAnim;
            update = true;
        } 
        final int newFlags = computeFlags(p.flags);
        if (newFlags != p.flags) {
            p.flags = newFlags;
            update = true;
        } 
        if (update) {
            mWindowManager.updateViewLayout(mPopupView, p);
        } 
    } 
 
    private void update(View anchor, boolean updateLocation, int xoff, int yoff,
                        boolean updateDimension, int width, int height) {
        if (!isShowing() || mContentView == null) {
            return; 
        } 
        WeakReference<View> oldAnchor = mAnchor;
        final boolean needsUpdate = updateLocation && (mAnchorXoff != xoff || mAnchorYoff != yoff);
        if (oldAnchor == null || oldAnchor.get() != anchor || needsUpdate && !mIsDropdown) {
            registerForScrollChanged(anchor, xoff, yoff);
        } else if (needsUpdate) {
            mAnchorXoff = xoff;
            mAnchorYoff = yoff;
        } 
        WindowManager.LayoutParams p = (WindowManager.LayoutParams) mPopupView.getLayoutParams();
        if (updateDimension) {
            if (width == -1) {
                width = mPopupWidth;
            } else { 
                mPopupWidth = width;
            } 
            if (height == -1) {
                height = mPopupHeight;
            } else { 
                mPopupHeight = height;
            } 
        } 
        int x = p.x;
        int y = p.y;
        if (updateLocation) {
            updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
        } else { 
            updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff));
        } 
        update(p.x, p.y, width, height, x != p.x || y != p.y);
    } 
 
    public void update(View anchor, int width, int height) {
        update(anchor, false, 0, 0, true, width, height);
    } 
 
    public void update(View anchor, int xoff, int yoff, int width, int height) {
        update(anchor, true, xoff, yoff, true, width, height);
    } 
 
    @SuppressWarnings("deprecation") 
    private void updateAboveAnchor(boolean aboveAnchor) {
        if (aboveAnchor != mAboveAnchor) {
            mAboveAnchor = aboveAnchor;
            if (mBackground != null) {
                if (mAboveAnchorBackgroundDrawable != null) {
                    if (mAboveAnchor) {
                        mPopupView.setBackgroundDrawable(mAboveAnchorBackgroundDrawable);
                    } else { 
                        mPopupView.setBackgroundDrawable(mBelowAnchorBackgroundDrawable);
                    } 
                } else { 
                    mPopupView.refreshDrawableState();
                } 
            } 
        } 
    } 
 
}