Example usage for android.view View hasWindowFocus

List of usage examples for android.view View hasWindowFocus

Introduction

In this page you can find the example usage for android.view View hasWindowFocus.

Prototype

public boolean hasWindowFocus() 

Source Link

Document

Returns true if this view is in a window that currently has window focus.

Usage

From source file:io.selendroid.server.model.AndroidNativeElement.java

public boolean isDisplayed() {
    View view = getView();
    boolean hasWindowFocus = view.hasWindowFocus();
    int width = view.getWidth();
    int height = view.getHeight();
    int visibility = view.getVisibility();
    boolean isVisible = (View.VISIBLE == visibility);

    // Check visibility of the view and its parents as well.
    // This is more reliable when transitions between activities are in progress.
    boolean isShown = view.isShown();

    boolean isDisplayed = hasWindowFocus && isVisible && isShown && (width > 0) && (height > 0);

    if (!isDisplayed) {
        Activity activity = instrumentation.getCurrentActivity();
        View focusedView = activity.getCurrentFocus();
        String displayCheckFailureMessage = String.format(
                "Display check failed\n" + "for view: %s\n"
                        + "isVisible: %b\nvisibility: %d\nisShown: %b\nhasWindowFocus: %b\n"
                        + "width: %d\nheight: %d\ncurrent activity: %s\nfocused view: %s",
                view, isVisible, visibility, isShown, hasWindowFocus, width, height, activity, focusedView);
        SelendroidLogger.debug(displayCheckFailureMessage);
        if (!isShown) {
            logIsShownCheckFailure(view);
        }/*from   w w w . j a v a2 s. c o m*/
        // Check the view belongs to the same view hierarchy as the view with current window focus.
        // If true, this usually means a system alert dialog is rendered on top of the view
        // (typically this is an app crash dialog).
        if (!hasWindowFocus) {
            if (activity != null && focusedView != null) {
                if (view.getRootView() == focusedView.getRootView()) {
                    SelendroidLogger.debug("hasWindowFocus() check failed. "
                            + "This usually means the view is covered by a system dialog.");
                }
            }
        }
    }

    return isDisplayed;
}

From source file:com.app.blockydemo.ui.adapter.BrickAdapter.java

private void animateSelectedBricks() {
    if (!animatedBricks.isEmpty()) {

        for (final Brick animationBrick : animatedBricks) {
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.blink);

            animation.setAnimationListener(new AnimationListener() {

                @Override/*ww w .  j  av a 2 s. c  om*/
                public void onAnimationStart(Animation animation) {
                    animationBrick.setAnimationState(true);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    animationBrick.setAnimationState(false);
                }
            });
            int position = animatedBricks.indexOf(animationBrick);
            animationBrick.setAnimationState(true);
            View view = animationBrick.getView(context, position, this);

            if (view.hasWindowFocus()) {
                view.startAnimation(animation);
            }
        }

    }
    animatedBricks.clear();
}

From source file:android.app.Activity.java

/**
 * Returns true if this activity's <em>main</em> window currently has window focus.
 * Note that this is not the same as the view itself having focus.
 * /*from w w w.j ava 2s  .c  om*/
 * @return True if this activity's main window currently has window focus.
 * 
 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
 */
public boolean hasWindowFocus() {
    Window w = getWindow();
    if (w != null) {
        View d = w.getDecorView();
        if (d != null) {
            return d.hasWindowFocus();
        }
    }
    return false;
}