Java Code Examples for android.view.View#isAttachedToWindow()

The following examples show how to use android.view.View#isAttachedToWindow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActivityTransitionCoordinator.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Iterates over the shared elements and adds them to the members in order.
 * Shared elements that are nested in other shared elements are placed after the
 * elements that they are nested in. This means that layout ordering can be done
 * from first to last.
 *
 * @param sharedElements The map of transition names to shared elements to set into
 *                       the member fields.
 */
private void setSharedElements(ArrayMap<String, View> sharedElements) {
    boolean isFirstRun = true;
    while (!sharedElements.isEmpty()) {
        final int numSharedElements = sharedElements.size();
        for (int i = numSharedElements - 1; i >= 0; i--) {
            final View view = sharedElements.valueAt(i);
            final String name = sharedElements.keyAt(i);
            if (isFirstRun && (view == null || !view.isAttachedToWindow() || name == null)) {
                sharedElements.removeAt(i);
            } else if (!isNested(view, sharedElements)) {
                mSharedElementNames.add(name);
                mSharedElements.add(view);
                sharedElements.removeAt(i);
            }
        }
        isFirstRun = false;
    }
}
 
Example 2
Source File: EnterTransitionCoordinator.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public void viewInstancesReady(ArrayList<String> accepted, ArrayList<String> localNames,
        ArrayList<View> localViews) {
    boolean remap = false;
    for (int i = 0; i < localViews.size(); i++) {
        View view = localViews.get(i);
        if (!TextUtils.equals(view.getTransitionName(), localNames.get(i))
                || !view.isAttachedToWindow()) {
            remap = true;
            break;
        }
    }
    if (remap) {
        triggerViewsReady(mapNamedElements(accepted, localNames));
    } else {
        triggerViewsReady(mapSharedElements(accepted, localViews));
    }
}
 
Example 3
Source File: PopupWindow.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/** @hide */
protected void attachToAnchor(View anchor, int xoff, int yoff, int gravity) {
    detachFromAnchor();

    final ViewTreeObserver vto = anchor.getViewTreeObserver();
    if (vto != null) {
        vto.addOnScrollChangedListener(mOnScrollChangedListener);
    }
    anchor.addOnAttachStateChangeListener(mOnAnchorDetachedListener);

    final View anchorRoot = anchor.getRootView();
    anchorRoot.addOnAttachStateChangeListener(mOnAnchorRootDetachedListener);
    anchorRoot.addOnLayoutChangeListener(mOnLayoutChangeListener);

    mAnchor = new WeakReference<>(anchor);
    mAnchorRoot = new WeakReference<>(anchorRoot);
    mIsAnchorRootAttached = anchorRoot.isAttachedToWindow();
    mParentRootView = mAnchorRoot;

    mAnchorXoff = xoff;
    mAnchorYoff = yoff;
    mAnchoredGravity = gravity;
}
 
Example 4
Source File: SharedElementUtils.java    From scene with Apache License 2.0 6 votes vote down vote up
/**
 * Guarantee order: Parent -> Child
 * Make sure that Parent will not overwrite Child when adding Overlay
 */
public static List<NonNullPair<String, View>> sortSharedElementList(ArrayMap<String, View> sharedElements) {
    List<NonNullPair<String, View>> list = new ArrayList<>();
    boolean isFirstRun = true;
    while (!sharedElements.isEmpty()) {
        final int numSharedElements = sharedElements.size();
        for (int i = numSharedElements - 1; i >= 0; i--) {
            final View view = sharedElements.valueAt(i);
            final String name = sharedElements.keyAt(i);
            if (isFirstRun && (view == null || !view.isAttachedToWindow() || name == null)) {
                sharedElements.removeAt(i);
            } else if (!isNested(view, sharedElements)) {
                list.add(NonNullPair.create(name, view));
                sharedElements.removeAt(i);
            }
        }
        isFirstRun = false;
    }
    return list;
}
 
Example 5
Source File: ViewUtils.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isAttachedToWindow(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return view.isAttachedToWindow();
    } else {
        return view.getWindowToken() != null;
    }
}
 
Example 6
Source File: JumpingBeansSpan.java    From AutoLoadListView with Apache License 2.0 5 votes vote down vote up
private boolean isAttachedToHierarchy(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return v.isAttachedToWindow();
    } else {
        return v.getParent() != null;   // Best-effort fallback
    }
}
 
Example 7
Source File: Utilities.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isViewAttachedToWindow(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return v.isAttachedToWindow();
    } else {
        // A proxy call which returns null, if the view is not attached to the window.
        return v.getKeyDispatcherState() != null;
    }
}
 
Example 8
Source File: Coordinators.java    From coordinators with Apache License 2.0 5 votes vote down vote up
private static boolean isAttachedToWindow(View view) {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    return view.getWindowToken() != null;
  } else {
    return view.isAttachedToWindow();
  }
}
 
Example 9
Source File: HolyGraphActivity.java    From intra42 with Apache License 2.0 5 votes vote down vote up
public void animate(View action, View view) {

        view.bringToFront();
        view.setVisibility(View.VISIBLE);

        if (action == null)
            return;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

            if (!action.isAttachedToWindow() || !view.isAttachedToWindow())
                return;

            // finding X and Y co-ordinates
            int[] coordinateAction = {0, 0};
            int[] coordinateView = {0, 0};
            action.getLocationInWindow(coordinateAction);
            view.getLocationInWindow(coordinateView);
            int cx = (coordinateAction[0] + action.getWidth() / 2);
            int cy = (0 - coordinateView[1] + coordinateAction[1] + action.getHeight() / 2);

            // to find  radius when icon is tapped for showing layout
            int startRadius = 0;
            int endRadius = Math.max(view.getWidth() + cx, view.getHeight() + cy);

            // performing circular reveal when icon will be tapped
            Animator animator = ViewAnimationUtils.createCircularReveal(view, cx, cy, startRadius, endRadius);
            animator.setInterpolator(new AccelerateDecelerateInterpolator());
            animator.setDuration(350);

            // to show the layout when icon is tapped
            animator.start();
        }
    }
 
Example 10
Source File: JumpingBeansSpan.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private boolean isAttachedToHierarchy(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return v.isAttachedToWindow();
    } else {
        return v.getParent() != null;   // Best-effort fallback
    }
}
 
Example 11
Source File: JumpingBeansSpan.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private boolean isAttachedToHierarchy(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return v.isAttachedToWindow();
    } else {
        return v.getParent() != null;   // Best-effort fallback
    }
}
 
Example 12
Source File: PopupWindow.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void alignToAnchor() {
    final View anchor = mAnchor != null ? mAnchor.get() : null;
    if (anchor != null && anchor.isAttachedToWindow() && hasDecorView()) {
        final WindowManager.LayoutParams p = getDecorViewLayoutParams();

        updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff,
                p.width, p.height, mAnchoredGravity, false));
        update(p.x, p.y, -1, -1, true);
    }
}
 
Example 13
Source File: ActivityTransitionCoordinator.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
protected void moveSharedElementsToOverlay() {
    if (mWindow == null || !mWindow.getSharedElementsUseOverlay()) {
        return;
    }
    setSharedElementMatrices();
    int numSharedElements = mSharedElements.size();
    ViewGroup decor = getDecor();
    if (decor != null) {
        boolean moveWithParent = moveSharedElementWithParent();
        Matrix tempMatrix = new Matrix();
        for (int i = 0; i < numSharedElements; i++) {
            View view = mSharedElements.get(i);
            if (view.isAttachedToWindow()) {
                tempMatrix.reset();
                mSharedElementParentMatrices.get(i).invert(tempMatrix);
                GhostView.addGhost(view, decor, tempMatrix);
                ViewGroup parent = (ViewGroup) view.getParent();
                if (moveWithParent && !isInTransitionGroup(parent, decor)) {
                    GhostViewListeners listener = new GhostViewListeners(view, parent, decor);
                    parent.getViewTreeObserver().addOnPreDrawListener(listener);
                    parent.addOnAttachStateChangeListener(listener);
                    mGhostViewListeners.add(listener);
                }
            }
        }
    }
}
 
Example 14
Source File: ChatAttachAlertPhotoLayout.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void checkCameraViewPosition() {
    if (Build.VERSION.SDK_INT >= 21) {
        if (cameraView != null) {
            cameraView.invalidateOutline();
        }
        RecyclerView.ViewHolder holder = gridView.findViewHolderForAdapterPosition(itemsPerRow - 1);
        if (holder != null) {
            holder.itemView.invalidateOutline();
        }
        if (!adapter.needCamera || !deviceHasGoodCamera || selectedAlbumEntry != galleryAlbumEntry) {
            holder = gridView.findViewHolderForAdapterPosition(0);
            if (holder != null) {
                holder.itemView.invalidateOutline();
            }
        }
    }
    if (!deviceHasGoodCamera) {
        return;
    }
    int count = gridView.getChildCount();
    for (int a = 0; a < count; a++) {
        View child = gridView.getChildAt(a);
        if (child instanceof PhotoAttachCameraCell) {
            if (Build.VERSION.SDK_INT >= 19) {
                if (!child.isAttachedToWindow()) {
                    break;
                }
            }
            child.getLocationInWindow(cameraViewLocation);
            cameraViewLocation[0] -= parentAlert.getLeftInset();
            float listViewX = gridView.getX() - parentAlert.getLeftInset();
            if (cameraViewLocation[0] < listViewX) {
                cameraViewOffsetX = (int) (listViewX - cameraViewLocation[0]);
                if (cameraViewOffsetX >= itemSize) {
                    cameraViewOffsetX = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-400);
                    cameraViewLocation[1] = 0;
                } else {
                    cameraViewLocation[0] += cameraViewOffsetX;
                }
            } else {
                cameraViewOffsetX = 0;
            }
            int maxY = (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0) + ActionBar.getCurrentActionBarHeight();
            if (cameraViewLocation[1] < maxY) {
                cameraViewOffsetY = maxY - cameraViewLocation[1];
                if (cameraViewOffsetY >= itemSize) {
                    cameraViewOffsetY = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-400);
                    cameraViewLocation[1] = 0;
                } else {
                    cameraViewLocation[1] += cameraViewOffsetY;
                }
            } else {
                cameraViewOffsetY = 0;
            }
            int containerHeight = parentAlert.getSheetContainer().getMeasuredHeight();
            int keyboardSize = parentAlert.sizeNotifierFrameLayout.getKeyboardHeight();
            if (!AndroidUtilities.isInMultiwindow && keyboardSize <= AndroidUtilities.dp(20)) {
                containerHeight -= parentAlert.commentTextView.getEmojiPadding();
            }
            maxY = (int) (containerHeight - parentAlert.buttonsRecyclerView.getMeasuredHeight() + parentAlert.buttonsRecyclerView.getTranslationY() + parentAlert.getSheetContainer().getTranslationY());
            if (cameraViewLocation[1] + itemSize > maxY) {
                cameraViewOffsetBottomY = cameraViewLocation[1] + itemSize - maxY;
                if (cameraViewOffsetBottomY >= itemSize) {
                    cameraViewOffsetBottomY = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-400);
                    cameraViewLocation[1] = 0;
                }
            } else {
                cameraViewOffsetBottomY = 0;
            }
            applyCameraViewPosition();
            return;
        }
    }
    cameraViewOffsetX = 0;
    cameraViewOffsetY = 0;
    cameraViewLocation[0] = AndroidUtilities.dp(-400);
    cameraViewLocation[1] = 0;
    applyCameraViewPosition();
}
 
Example 15
Source File: JumpingBeansSpan.java    From stynico with MIT License 4 votes vote down vote up
private static boolean isAttachedToHierarchy(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return v.isAttachedToWindow();
    }
    return v.getParent() != null;   // Best-effort fallback (without adding support-v4 just for this...)
}
 
Example 16
Source File: ChatAttachAlert.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void checkCameraViewPosition() {
    if (!deviceHasGoodCamera) {
        return;
    }
    int count = attachPhotoRecyclerView.getChildCount();
    for (int a = 0; a < count; a++) {
        View child = attachPhotoRecyclerView.getChildAt(a);
        if (child instanceof PhotoAttachCameraCell) {
            if (Build.VERSION.SDK_INT >= 19) {
                if (!child.isAttachedToWindow()) {
                    break;
                }
            }
            child.getLocationInWindow(cameraViewLocation);
            cameraViewLocation[0] -= getLeftInset();
            float listViewX = listView.getX() + backgroundPaddingLeft - getLeftInset();
            if (cameraViewLocation[0] < listViewX) {
                cameraViewOffsetX = (int) (listViewX - cameraViewLocation[0]);
                if (cameraViewOffsetX >= AndroidUtilities.dp(80)) {
                    cameraViewOffsetX = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-150);
                    cameraViewLocation[1] = 0;
                } else {
                    cameraViewLocation[0] += cameraViewOffsetX;
                }
            } else {
                cameraViewOffsetX = 0;
            }
            if (Build.VERSION.SDK_INT >= 21 && cameraViewLocation[1] < AndroidUtilities.statusBarHeight) {
                cameraViewOffsetY = AndroidUtilities.statusBarHeight - cameraViewLocation[1];
                if (cameraViewOffsetY >= AndroidUtilities.dp(80)) {
                    cameraViewOffsetY = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-150);
                    cameraViewLocation[1] = 0;
                } else {
                    cameraViewLocation[1] += cameraViewOffsetY;
                }
            } else {
                cameraViewOffsetY = 0;
            }
            applyCameraViewPosition();
            return;
        }
    }
    cameraViewOffsetX = 0;
    cameraViewOffsetY = 0;
    cameraViewLocation[0] = AndroidUtilities.dp(-150);
    cameraViewLocation[1] = 0;
    applyCameraViewPosition();
}
 
Example 17
Source File: JumpingBeansSpan.java    From BookLoadingView with Apache License 2.0 4 votes vote down vote up
private static boolean isAttachedToHierarchy(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return v.isAttachedToWindow();
    }
    return v.getParent() != null;   // Best-effort fallback
}
 
Example 18
Source File: MaterialTapTargetPrompt.java    From MaterialTapTargetPrompt with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 *
 * @param promptOptions The options used to create the prompt.
 */
MaterialTapTargetPrompt(final PromptOptions promptOptions)
{
    final ResourceFinder resourceFinder = promptOptions.getResourceFinder();
    mView = new PromptView(resourceFinder.getContext());
    mView.mPrompt = this;
    mView.mPromptOptions = promptOptions;
    mView.mPromptTouchedListener = new PromptView.PromptTouchedListener()
    {
        @Override
        public void onFocalPressed()
        {
            if (!isDismissing())
            {
                onPromptStateChanged(STATE_FOCAL_PRESSED);
                if (mView.mPromptOptions.getAutoFinish())
                {
                    finish();
                }
            }
        }

        @Override
        public void onNonFocalPressed()
        {
            if (!isDismissing())
            {
                onPromptStateChanged(STATE_NON_FOCAL_PRESSED);
                if (mView.mPromptOptions.getAutoDismiss())
                {
                    dismiss();
                }
            }
        }

        @Override
        public void onBackButtonPressed()
        {
            if (!isDismissing())
            {
                onPromptStateChanged(STATE_BACK_BUTTON_PRESSED);
                onPromptStateChanged(STATE_NON_FOCAL_PRESSED);
                if (mView.mPromptOptions.getAutoDismiss())
                {
                    dismiss();
                }
            }
        }
    };

    Rect rect = new Rect();
    resourceFinder.getPromptParentView().getWindowVisibleDisplayFrame(rect);
    mStatusBarHeight = rect.top;

    mGlobalLayoutListener = () -> {
        final View targetView = mView.mPromptOptions.getTargetView();
        if (targetView != null)
        {
            final boolean isTargetAttachedToWindow;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                isTargetAttachedToWindow = targetView.isAttachedToWindow();
            }
            else
            {
                isTargetAttachedToWindow = targetView.getWindowToken() != null;
            }

            if (!isTargetAttachedToWindow)
            {
                return;
            }
        }
        prepare();

        if (mAnimationCurrent == null)
        {
            // Force a relayout to update the view's location
            updateAnimation(1, 1);
        }
    };
}
 
Example 19
Source File: ChatAttachAlertPhotoLayout.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private void checkCameraViewPosition() {
    if (Build.VERSION.SDK_INT >= 21) {
        if (cameraView != null) {
            cameraView.invalidateOutline();
        }
        RecyclerView.ViewHolder holder = gridView.findViewHolderForAdapterPosition(itemsPerRow - 1);
        if (holder != null) {
            holder.itemView.invalidateOutline();
        }
        if (!adapter.needCamera || !deviceHasGoodCamera || selectedAlbumEntry != galleryAlbumEntry) {
            holder = gridView.findViewHolderForAdapterPosition(0);
            if (holder != null) {
                holder.itemView.invalidateOutline();
            }
        }
    }
    if (!deviceHasGoodCamera) {
        return;
    }
    int count = gridView.getChildCount();
    for (int a = 0; a < count; a++) {
        View child = gridView.getChildAt(a);
        if (child instanceof PhotoAttachCameraCell) {
            if (Build.VERSION.SDK_INT >= 19) {
                if (!child.isAttachedToWindow()) {
                    break;
                }
            }
            child.getLocationInWindow(cameraViewLocation);
            cameraViewLocation[0] -= parentAlert.getLeftInset();
            float listViewX = gridView.getX() - parentAlert.getLeftInset();
            if (cameraViewLocation[0] < listViewX) {
                cameraViewOffsetX = (int) (listViewX - cameraViewLocation[0]);
                if (cameraViewOffsetX >= itemSize) {
                    cameraViewOffsetX = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-400);
                    cameraViewLocation[1] = 0;
                } else {
                    cameraViewLocation[0] += cameraViewOffsetX;
                }
            } else {
                cameraViewOffsetX = 0;
            }
            int maxY = (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0) + ActionBar.getCurrentActionBarHeight();
            if (cameraViewLocation[1] < maxY) {
                cameraViewOffsetY = maxY - cameraViewLocation[1];
                if (cameraViewOffsetY >= itemSize) {
                    cameraViewOffsetY = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-400);
                    cameraViewLocation[1] = 0;
                } else {
                    cameraViewLocation[1] += cameraViewOffsetY;
                }
            } else {
                cameraViewOffsetY = 0;
            }
            int containerHeight = parentAlert.getSheetContainer().getMeasuredHeight();
            int keyboardSize = parentAlert.sizeNotifierFrameLayout.getKeyboardHeight();
            if (!AndroidUtilities.isInMultiwindow && keyboardSize <= AndroidUtilities.dp(20)) {
                containerHeight -= parentAlert.commentTextView.getEmojiPadding();
            }
            maxY = (int) (containerHeight - parentAlert.buttonsRecyclerView.getMeasuredHeight() + parentAlert.buttonsRecyclerView.getTranslationY() + parentAlert.getSheetContainer().getTranslationY());
            if (cameraViewLocation[1] + itemSize > maxY) {
                cameraViewOffsetBottomY = cameraViewLocation[1] + itemSize - maxY;
                if (cameraViewOffsetBottomY >= itemSize) {
                    cameraViewOffsetBottomY = 0;
                    cameraViewLocation[0] = AndroidUtilities.dp(-400);
                    cameraViewLocation[1] = 0;
                }
            } else {
                cameraViewOffsetBottomY = 0;
            }
            applyCameraViewPosition();
            return;
        }
    }
    cameraViewOffsetX = 0;
    cameraViewOffsetY = 0;
    cameraViewLocation[0] = AndroidUtilities.dp(-400);
    cameraViewLocation[1] = 0;
    applyCameraViewPosition();
}
 
Example 20
Source File: AccessibilityRequestPreparer.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * @param view The view whose requests need preparation. It must be attached to a
 * window. This object will retain a weak reference to this view, and will unregister itself
 * from AccessibilityManager if the view is detached from a window. It will not re-register
 * itself.
 * @param requestTypes The types of requests that require preparation. Different types may
 * be ORed together.
 *
 * @throws IllegalStateException if the view is not attached to a window.
 */
public AccessibilityRequestPreparer(View view, @RequestTypes int requestTypes) {
    if (!view.isAttachedToWindow()) {
        throw new IllegalStateException("View must be attached to a window");
    }
    mViewRef = new WeakReference<>(view);
    mRequestTypes = requestTypes;
    view.addOnAttachStateChangeListener(new ViewAttachStateListener());
}