Ejemplo n.º 1
0
    /**
     * Set the position of this window in absolute pixels. The window's top-left corner will be
     * positioned at the given x and y, unless you've set a different anchor point with {@link
     * #setAnchorPoint(float, float)}.
     *
     * <p>Changes will not applied until you {@link #commit()}.
     *
     * @param x
     * @param y
     * @param skip Don't call {@link #setPosition(int, int)} and {@link #setSize(int, int)} to avoid
     *     stack overflow.
     * @return The same Editor, useful for method chaining.
     */
    private Editor setPosition(int x, int y, boolean skip) {
      if (mParams != null) {
        if (anchorX < 0 || anchorX > 1 || anchorY < 0 || anchorY > 1) {
          throw new IllegalStateException("Anchor point must be between 0 and 1, inclusive.");
        }

        // sets the x and y correctly according to anchorX and
        // anchorY
        if (x != UNCHANGED) {
          mParams.x = (int) (x - mParams.width * anchorX);
        }
        if (y != UNCHANGED) {
          mParams.y = (int) (y - mParams.height * anchorY);
        }

        if (Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE)) {
          // if gravity is not TOP|LEFT throw exception
          if (mParams.gravity != (Gravity.TOP | Gravity.LEFT)) {
            throw new IllegalStateException(
                "The window "
                    + id
                    + " gravity must be TOP|LEFT if FLAG_WINDOW_EDGE_LIMITS_ENABLE or FLAG_WINDOW_EDGE_TILE_ENABLE is set.");
          }

          // keep window inside edges
          //					mParams.x = Math.min(Math.max(mParams.x, 0), displayWidth - mParams.width);
          //					mParams.y = Math.min(Math.max(mParams.y, 0), displayHeight - mParams.height);
        }
      }

      return this;
    }
Ejemplo n.º 2
0
  /**
   * Request or remove the focus from this window.
   *
   * @param focus Whether we want to gain or lose focus.
   * @return True if focus changed successfully, false if it failed.
   */
  public boolean onFocus(boolean focus) {
    if (!Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE)) {
      // window is focusable

      if (focus == focused) {
        // window already focused/unfocused
        return false;
      }

      focused = focus;

      // alert callbacks and cancel if instructed
      if (mContext.onFocusChange(id, this, focus)) {
        Log.d(
            TAG,
            "Window "
                + id
                + " focus change "
                + (focus ? "(true)" : "(false)")
                + " cancelled by implementation.");
        focused = !focus;
        return false;
      }

      if (!Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_FOCUS_INDICATOR_DISABLE)) {
        // change visual state
        View content = findViewById(R.id.content);
        if (focus) {
          // gaining focus
          content.setBackgroundResource(R.drawable.border_focused);
        } else {
          // losing focus
          if (Utils.isSet(flags, StandOutFlags.FLAG_DECORATION_SYSTEM)) {
            // system decorations
            content.setBackgroundResource(R.drawable.border);
          } else {
            // no decorations
            content.setBackgroundResource(0);
          }
        }
      }

      // set window manager params
      StandOutLayoutParams params = getLayoutParams();
      params.setFocusFlag(focus);
      mContext.updateViewLayout(id, params);

      if (focus) {
        mContext.setFocusedWindow(this);
      } else {
        if (mContext.getFocusedWindow() == this) {
          mContext.setFocusedWindow(null);
        }
      }

      return true;
    }
    return false;
  }
Ejemplo n.º 3
0
    /**
     * Set the size of this window in absolute pixels. The window will expand and shrink around the
     * top-left corner, unless you've set a different anchor point with {@link
     * #setAnchorPoint(float, float)}.
     *
     * <p>Changes will not applied until you {@link #commit()}.
     *
     * @param width
     * @param height
     * @param skip Don't call {@link #setPosition(int, int)} to avoid stack overflow.
     * @return The same Editor, useful for method chaining.
     */
    private Editor setSize(int width, int height, boolean skip) {
      if (mParams != null) {
        if (anchorX < 0 || anchorX > 1 || anchorY < 0 || anchorY > 1) {
          throw new IllegalStateException("Anchor point must be between 0 and 1, inclusive.");
        }

        int lastWidth = mParams.width;
        int lastHeight = mParams.height;

        if (width != UNCHANGED) {
          mParams.width = width;
        }
        if (height != UNCHANGED) {
          mParams.height = height;
        }

        // set max width/height
        int maxWidth = mParams.maxWidth;
        int maxHeight = mParams.maxHeight;

        if (Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE)) {
          maxWidth = (int) Math.min(maxWidth, displayWidth);
          maxHeight = (int) Math.min(maxHeight, displayHeight);
        }

        // keep window between min and max
        mParams.width = Math.min(Math.max(mParams.width, mParams.minWidth), maxWidth);
        mParams.height = Math.min(Math.max(mParams.height, mParams.minHeight), maxHeight);

        // keep window in aspect ratio
        if (Utils.isSet(flags, StandOutFlags.FLAG_WINDOW_ASPECT_RATIO_ENABLE)) {
          int ratioWidth = (int) (mParams.height * touchInfo.ratio);
          int ratioHeight = (int) (mParams.width / touchInfo.ratio);
          if (ratioHeight >= mParams.minHeight && ratioHeight <= mParams.maxHeight) {
            // width good adjust height
            mParams.height = ratioHeight;
          } else {
            // height good adjust width
            mParams.width = ratioWidth;
          }
        }

        if (!skip) {
          // set position based on anchor point
          setPosition(
              (int) (mParams.x + lastWidth * anchorX), (int) (mParams.y + lastHeight * anchorY));
        }
      }

      return this;
    }