Example #1
0
  private void initializeBorder(KrollDict d, Integer bgColor) {
    if (d.containsKey(TiC.PROPERTY_BORDER_RADIUS)
        || d.containsKey(TiC.PROPERTY_BORDER_COLOR)
        || d.containsKey(TiC.PROPERTY_BORDER_WIDTH)) {

      if (nativeView != null) {
        if (background == null) {
          applyCustomBackground();
        }

        if (background.getBorder() == null) {
          background.setBorder(new TiBackgroundDrawable.Border());
        }

        TiBackgroundDrawable.Border border = background.getBorder();

        if (d.containsKey(TiC.PROPERTY_BORDER_RADIUS)) {
          border.setRadius(TiConvert.toFloat(d, TiC.PROPERTY_BORDER_RADIUS));
        }
        if (d.containsKey(TiC.PROPERTY_BORDER_COLOR) || d.containsKey(TiC.PROPERTY_BORDER_WIDTH)) {
          if (d.containsKey(TiC.PROPERTY_BORDER_COLOR)) {
            border.setColor(TiConvert.toColor(d, TiC.PROPERTY_BORDER_COLOR));
          } else {
            if (bgColor != null) {
              border.setColor(bgColor);
            }
          }
          if (d.containsKey(TiC.PROPERTY_BORDER_WIDTH)) {
            border.setWidth(TiConvert.toFloat(d, TiC.PROPERTY_BORDER_WIDTH));
          }
        }
        // applyCustomBackground();
      }
    }
  }
Example #2
0
  private void handleBorderProperty(String property, Object value) {
    if (background.getBorder() == null) {
      background.setBorder(new TiBackgroundDrawable.Border());
    }
    TiBackgroundDrawable.Border border = background.getBorder();

    if (property.equals(TiC.PROPERTY_BORDER_COLOR)) {
      border.setColor(TiConvert.toColor(value.toString()));
    } else if (property.equals(TiC.PROPERTY_BORDER_RADIUS)) {
      border.setRadius(TiConvert.toFloat(value));
    } else if (property.equals(TiC.PROPERTY_BORDER_WIDTH)) {
      border.setWidth(TiConvert.toFloat(value));
    }
    applyCustomBackground();
  }
  public void setBackgroundDrawable(KrollDict d, Drawable drawable) {
    StateListDrawable stateDrawable = new StateListDrawable();

    // use transparent highlight so the selector drawable is visible over background
    ColorDrawable transparent = new ColorDrawable(Color.TRANSPARENT);
    stateDrawable.addState(
        new int[] {
          android.R.attr.state_window_focused,
          android.R.attr.state_enabled,
          android.R.attr.state_pressed
        },
        transparent);
    stateDrawable.addState(new int[] {android.R.attr.state_selected}, transparent);

    stateDrawable.addState(
        new int[] {
          android.R.attr.state_focused,
          android.R.attr.state_window_focused,
          android.R.attr.state_enabled
        },
        drawable);
    stateDrawable.addState(new int[0], drawable);

    if (d.containsKey(TiC.PROPERTY_OPACITY)) {
      stateDrawable.setAlpha(Math.round(TiConvert.toFloat(d, TiC.PROPERTY_OPACITY) * 255));
    }
    setBackgroundDrawable(stateDrawable);
  }
  private void loadColors(Object[] colors) {
    this.colors = new int[colors.length];
    int offsetCount = 0;
    for (int i = 0; i < colors.length; i++) {
      Object color = colors[i];
      if (color instanceof HashMap) {
        HashMap<String, Object> colorRefObject = (HashMap) color;
        this.colors[i] = TiConvert.toColor(colorRefObject, "color");

        if (offsets == null) {
          offsets = new float[colors.length];
        }

        float offset = TiConvert.toFloat(colorRefObject, "offset", -1);
        if (offset >= 0.0f && offset <= 1.0f) {
          offsets[offsetCount++] = offset;
        }

      } else {
        this.colors[i] = TiConvert.toColor(color.toString());
      }
    }

    // If the number of offsets doesn't match the number of colors,
    // just distribute the colors evenly along the gradient line.
    if (offsetCount != this.colors.length) {
      offsets = null;
    }
  }
 @Override
 public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) {
   if ("volume".equals(key)) {
     setVolume(TiConvert.toFloat(newValue));
   } else if ("time".equals(key)) {
     setTime(TiConvert.toInt(newValue));
   }
 }
Example #6
0
  public float optFloat(String key, float defaultValue) {
    float result = defaultValue;

    if (containsKey(key)) {
      result = TiConvert.toFloat(get(key), defaultValue);
    }
    return result;
  }
Example #7
0
  public void processProperties(KrollDict d) {
    if (d.containsKey(TiC.PROPERTY_LAYOUT)) {
      String layout = TiConvert.toString(d, TiC.PROPERTY_LAYOUT);
      if (nativeView instanceof TiCompositeLayout) {
        ((TiCompositeLayout) nativeView).setLayoutArrangement(layout);
      }
    }
    if (TiConvert.fillLayout(d, layoutParams)) {
      if (nativeView != null) {
        nativeView.requestLayout();
      }
    }

    Integer bgColor = null;

    // Default background processing.
    // Prefer image to color.
    if (hasImage(d) || hasColorState(d) || hasBorder(d)) {
      handleBackgroundImage(d);
    } else if (d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR)) {
      bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR);
      nativeView.setBackgroundColor(bgColor);
    }
    if (d.containsKey(TiC.PROPERTY_OPACITY)) {
      if (nativeView != null) {
        setOpacity(TiConvert.toFloat(d, TiC.PROPERTY_OPACITY));
      }
    }

    if (d.containsKey(TiC.PROPERTY_VISIBLE)) {
      nativeView.setVisibility(
          TiConvert.toBoolean(d, TiC.PROPERTY_VISIBLE) ? View.VISIBLE : View.INVISIBLE);
    }
    if (d.containsKey(TiC.PROPERTY_ENABLED)) {
      nativeView.setEnabled(TiConvert.toBoolean(d, TiC.PROPERTY_ENABLED));
    }

    if (d.containsKey(TiC.PROPERTY_FOCUSABLE)) {
      boolean focusable = TiConvert.toBoolean(d, TiC.PROPERTY_FOCUSABLE);
      nativeView.setFocusable(focusable);
      if (focusable) {
        registerForKeyClick(nativeView);
      } else {
        nativeView.setOnClickListener(null);
      }
    }

    initializeBorder(d, bgColor);

    if (d.containsKey(TiC.PROPERTY_TRANSFORM)) {
      Ti2DMatrix matrix = (Ti2DMatrix) d.get(TiC.PROPERTY_TRANSFORM);
      if (matrix != null) {
        applyTransform(matrix);
      }
    }
  }
Example #8
0
  private void handleBorderProperty(String property, Object value) {
    if (background.getBorder() == null) {
      background.setBorder(new TiBackgroundDrawable.Border());
    }
    TiBackgroundDrawable.Border border = background.getBorder();

    if (property.equals(TiC.PROPERTY_BORDER_COLOR)) {
      border.setColor(TiConvert.toColor(value.toString()));
    } else if (property.equals(TiC.PROPERTY_BORDER_RADIUS)) {
      float radius = TiConvert.toFloat(value);
      if (radius > 0f && HONEYCOMB_OR_GREATER) {
        disableHWAcceleration();
      }
      border.setRadius(radius);
    } else if (property.equals(TiC.PROPERTY_BORDER_WIDTH)) {
      border.setWidth(TiConvert.toFloat(value));
    }
    applyCustomBackground();
  }
  @Override
  public void processProperties(KrollDict d) {
    if (d.containsKey("volume")) {
      setVolume(TiConvert.toFloat(d, "volume"));
    } else {
      setVolume(0.5f);
    }

    if (d.containsKey("time")) {
      setTime(TiConvert.toInt(d, "time"));
    }
  }
  @Override
  public void applyOptions() {
    super.applyOptions();
    HashMap options = getOptions();

    if (options == null) {
      return;
    }

    if (options.containsKey(TiC.PROPERTY_ANCHOR_POINT)) {
      Object anchorPoint = options.get(TiC.PROPERTY_ANCHOR_POINT);
      if (anchorPoint instanceof HashMap) {
        HashMap point = (HashMap) anchorPoint;
        anchorX = TiConvert.toFloat(point, TiC.PROPERTY_X);
        anchorY = TiConvert.toFloat(point, TiC.PROPERTY_Y);
      } else {
        Log.e(TAG, "Invalid argument type for anchorPoint property. Ignoring");
      }
    }
    this.options = options;
  }
  @Override
  public void propertySet(String key, Object newValue, Object oldValue, boolean changedProperty) {
    switch (key) {
      case TiC.PROPERTY_MESSAGE:
        message = TiConvert.toString(newValue);
        break;

      case TiC.PROPERTY_DURATION:
        toast.setDuration(TiConvert.toInt(newValue));
        break;

      case "verticalMargin":
        verticalMargin = TiConvert.toFloat(newValue, verticalMargin);
        toast.setMargin(horizontalMargin, verticalMargin);
        break;

      case "horizontalMargin":
        horizontalMargin = TiConvert.toFloat(newValue, horizontalMargin);
        toast.setMargin(horizontalMargin, verticalMargin);
        break;
      case "offsetX":
        offsetX = TiConvert.toInt(newValue, offsetX);
        toast.setGravity(gravity, offsetX, offsetY);
        break;
      case "offsetY":
        offsetY = TiConvert.toInt(newValue, offsetY);
        toast.setGravity(gravity, offsetX, offsetY);
        break;
      case "gravity":
        gravity = TiConvert.toInt(newValue, gravity);
        toast.setGravity(gravity, offsetX, offsetY);
        break;
      default:
        super.propertySet(key, newValue, oldValue, changedProperty);
        break;
    }
  }
Example #12
0
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
      long eventTimestamp = event.timestamp / 1000000;

      if (eventTimestamp - lastEventInUpdate > 250) {
        long actualTimestamp = baseTime.getTimeInMillis() + (eventTimestamp - sensorTimerStart);

        lastEventInUpdate = eventTimestamp;

        Object filter = geolocationModule.getProperty(TiC.PROPERTY_HEADING_FILTER);
        if (filter != null) {
          float headingFilter = TiConvert.toFloat(filter);

          if (Math.abs(event.values[0] - lastHeading) < headingFilter) {
            return;
          }

          lastHeading = event.values[0];
        }

        geolocationModule.fireEvent(TiC.EVENT_HEADING, eventToHashMap(event, actualTimestamp));
      }
    }
  }
 private void setPaintOptions() {
   tiPaint = new Paint();
   tiPaint.setAntiAlias(true);
   tiPaint.setDither(true);
   tiPaint.setColor(
       (props.containsKeyAndNotNull("strokeColor"))
           ? TiConvert.toColor(props, "strokeColor")
           : TiConvert.toColor("black"));
   tiPaint.setStyle(Paint.Style.STROKE);
   tiPaint.setStrokeJoin(Paint.Join.ROUND);
   tiPaint.setStrokeCap(Paint.Cap.ROUND);
   tiPaint.setStrokeWidth(
       (props.containsKeyAndNotNull("strokeWidth"))
           ? TiConvert.toFloat(props.get("strokeWidth"))
           : 12);
   tiPaint.setAlpha(
       (props.containsKeyAndNotNull("strokeAlpha"))
           ? TiConvert.toInt(props.get("strokeAlpha"))
           : 255);
   alphaState =
       (props.containsKeyAndNotNull("strokeAlpha"))
           ? TiConvert.toInt(props.get("strokeAlpha"))
           : 255;
 }
Example #14
0
  public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) {
    if (key.equals(TiC.PROPERTY_LEFT)) {
      if (newValue != null) {
        layoutParams.optionLeft =
            TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_LEFT);
      } else {
        layoutParams.optionLeft = null;
      }
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_TOP)) {
      if (newValue != null) {
        layoutParams.optionTop =
            TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_TOP);
      } else {
        layoutParams.optionTop = null;
      }
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_CENTER)) {
      TiConvert.updateLayoutCenter(newValue, layoutParams);
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_RIGHT)) {
      if (newValue != null) {
        layoutParams.optionRight =
            TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_RIGHT);
      } else {
        layoutParams.optionRight = null;
      }
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_BOTTOM)) {
      if (newValue != null) {
        layoutParams.optionBottom =
            TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_BOTTOM);
      } else {
        layoutParams.optionBottom = null;
      }
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_SIZE)) {
      if (newValue instanceof HashMap) {
        HashMap<String, Object> d = (HashMap) newValue;
        propertyChanged(TiC.PROPERTY_WIDTH, oldValue, d.get(TiC.PROPERTY_WIDTH), proxy);
        propertyChanged(TiC.PROPERTY_HEIGHT, oldValue, d.get(TiC.PROPERTY_HEIGHT), proxy);
      } else if (newValue != null) {
        Log.w(
            LCAT,
            "Unsupported property type ("
                + (newValue.getClass().getSimpleName())
                + ") for key: "
                + key
                + ". Must be an object/dictionary");
      }
    } else if (key.equals(TiC.PROPERTY_HEIGHT)) {
      if (newValue != null) {
        if (!newValue.equals(TiC.SIZE_AUTO)) {
          layoutParams.optionHeight =
              TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_HEIGHT);
          layoutParams.autoHeight = false;
        } else {
          layoutParams.optionHeight = null;
          layoutParams.autoHeight = true;
        }
      } else {
        layoutParams.optionHeight = null;
      }
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_WIDTH)) {
      if (newValue != null) {
        if (!newValue.equals(TiC.SIZE_AUTO)) {
          layoutParams.optionWidth =
              TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_WIDTH);
          layoutParams.autoWidth = false;
        } else {
          layoutParams.optionWidth = null;
          layoutParams.autoWidth = true;
        }
      } else {
        layoutParams.optionWidth = null;
      }
      layoutNativeView();
    } else if (key.equals(TiC.PROPERTY_ZINDEX)) {
      if (newValue != null) {
        layoutParams.optionZIndex = TiConvert.toInt(newValue);
      } else {
        layoutParams.optionZIndex = 0;
      }
      layoutNativeView(true);
    } else if (key.equals(TiC.PROPERTY_FOCUSABLE)) {
      boolean focusable = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_FOCUSABLE));
      nativeView.setFocusable(focusable);
      if (focusable) {
        registerForKeyClick(nativeView);
      } else {
        // nativeView.setOnClickListener(null); // ? mistake? I assume OnKeyListener was meant
        nativeView.setOnKeyListener(null);
      }
    } else if (key.equals(TiC.PROPERTY_TOUCH_ENABLED)) {
      doSetClickable(TiConvert.toBoolean(newValue));
    } else if (key.equals(TiC.PROPERTY_VISIBLE)) {
      nativeView.setVisibility(TiConvert.toBoolean(newValue) ? View.VISIBLE : View.INVISIBLE);
    } else if (key.equals(TiC.PROPERTY_ENABLED)) {
      nativeView.setEnabled(TiConvert.toBoolean(newValue));
    } else if (key.startsWith(TiC.PROPERTY_BACKGROUND_PADDING)) {
      Log.i(LCAT, key + " not yet implemented.");
    } else if (key.equals(TiC.PROPERTY_OPACITY)
        || key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX)
        || key.startsWith(TiC.PROPERTY_BORDER_PREFIX)) {
      // Update first before querying.
      proxy.setProperty(key, newValue);

      KrollDict d = proxy.getProperties();

      boolean hasImage = hasImage(d);
      boolean hasColorState = hasColorState(d);
      boolean hasBorder = hasBorder(d);

      boolean requiresCustomBackground = hasImage || hasColorState || hasBorder;

      if (!requiresCustomBackground) {
        if (background != null) {
          background.releaseDelegate();
          background.setCallback(null);
          background = null;
        }

        if (d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_COLOR)) {
          Integer bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR);
          if (nativeView != null) {
            nativeView.setBackgroundColor(bgColor);
            nativeView.postInvalidate();
          }
        } else {
          if (key.equals(TiC.PROPERTY_OPACITY)) {
            setOpacity(TiConvert.toFloat(newValue));
          }
          if (nativeView != null) {
            nativeView.setBackgroundDrawable(null);
            nativeView.postInvalidate();
          }
        }
      } else {
        boolean newBackground = background == null;
        if (newBackground) {
          background = new TiBackgroundDrawable();
        }

        Integer bgColor = null;

        if (!hasColorState) {
          if (d.get(TiC.PROPERTY_BACKGROUND_COLOR) != null) {
            bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR);
            if (newBackground
                || (key.equals(TiC.PROPERTY_OPACITY)
                    || key.equals(TiC.PROPERTY_BACKGROUND_COLOR))) {
              background.setBackgroundColor(bgColor);
            }
          }
        }

        if (hasImage || hasColorState) {
          if (newBackground || key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX)) {
            handleBackgroundImage(d);
          }
        }

        if (hasBorder) {
          if (newBackground) {
            initializeBorder(d, bgColor);
          } else if (key.startsWith(TiC.PROPERTY_BORDER_PREFIX)) {
            handleBorderProperty(key, newValue);
          }
        }
        applyCustomBackground();
      }
      if (nativeView != null) {
        nativeView.postInvalidate();
      }
    } else if (key.equals(TiC.PROPERTY_SOFT_KEYBOARD_ON_FOCUS)) {
      Log.w(
          LCAT,
          "Focus state changed to "
              + TiConvert.toString(newValue)
              + " not honored until next focus event.");
    } else if (key.equals(TiC.PROPERTY_TRANSFORM)) {
      if (nativeView != null) {
        applyTransform((Ti2DMatrix) newValue);
      }
    } else if (key.equals(TiC.PROPERTY_KEEP_SCREEN_ON)) {
      if (nativeView != null) {
        nativeView.setKeepScreenOn(TiConvert.toBoolean(newValue));
      }
    } else {
      TiViewProxy viewProxy = getProxy();
      if (viewProxy != null && viewProxy.isLocalizedTextId(key)) {
        viewProxy.setLocalizedText(key, TiConvert.toString(newValue));
      } else {
        if (DBG) {
          Log.d(LCAT, "Unhandled property key: " + key);
        }
      }
    }
  }
Example #15
0
 public float getFloat(String key) {
   return TiConvert.toFloat(get(key));
 }