private Intent createAlarmServiceIntent(KrollDict args) {
    String serviceName = args.getString("service");
    Intent intent =
        new Intent(TiApplication.getInstance().getApplicationContext(), AlarmServiceListener.class);
    intent.putExtra("alarm_service_name", serviceName);
    // Pass in flag if we need to restart the service on each call
    intent.putExtra("alarm_service_force_restart", (optionIsEnabled(args, "forceRestart")));
    // Check if the user has selected to use intervals
    boolean hasInterval = (args.containsKeyAndNotNull("interval"));
    long intervalValue = 0;
    if (hasInterval) {
      Object interval = args.get("interval");
      if (interval instanceof Number) {
        intervalValue = ((Number) interval).longValue();
      } else {
        hasInterval = false;
      }
    }
    intent.putExtra("alarm_service_has_interval", hasInterval);
    if (hasInterval) {
      intent.putExtra("alarm_service_interval", intervalValue);
    }

    utils.debugLog(
        "created alarm service intent for "
            + serviceName
            + "(forceRestart: "
            + (optionIsEnabled(args, "forceRestart") ? "true" : "false")
            + ", intervalValue: "
            + intervalValue
            + ")");

    return intent;
  }
 private boolean optionIsEnabled(KrollDict args, String paramName) {
   if (args.containsKeyAndNotNull(paramName)) {
     Object value = args.get(paramName);
     return TiConvert.toBoolean(value);
   } else {
     return false;
   }
 }
Ejemplo n.º 3
0
 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;
 }
  private Intent createAlarmNotifyIntent(KrollDict args, int requestCode) {
    int notificationIcon = 0;
    String contentTitle = "";
    String contentText = "";
    String notificationSound = "";
    boolean playSound = optionIsEnabled(args, "playSound");
    boolean doVibrate = optionIsEnabled(args, "vibrate");
    boolean showLights = optionIsEnabled(args, "showLights");
    if (args.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TITLE)
        || args.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TEXT)) {
      if (args.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TITLE)) {
        contentTitle = TiConvert.toString(args, TiC.PROPERTY_CONTENT_TITLE);
      }
      if (args.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TEXT)) {
        contentText = TiConvert.toString(args, TiC.PROPERTY_CONTENT_TEXT);
      }
      ;
    }

    if (args.containsKey(TiC.PROPERTY_ICON)) {
      Object icon = args.get(TiC.PROPERTY_ICON);
      if (icon instanceof Number) {
        notificationIcon = ((Number) icon).intValue();
      } else {
        String iconUrl = TiConvert.toString(icon);
        String iconFullUrl = resolveUrl(null, iconUrl);
        notificationIcon = TiUIHelper.getResourceId(iconFullUrl);
        if (notificationIcon == 0) {
          utils.debugLog("No image found for " + iconUrl);
          utils.debugLog("Default icon will be used");
        }
      }
    }

    if (args.containsKey(TiC.PROPERTY_SOUND)) {
      notificationSound = TiConvert.toString(args, TiC.PROPERTY_SOUND);
    }

    if (args.containsKey("rootActivity")) {
      rootActivityClassName = TiConvert.toString(args, "rootActivity");
    }

    Intent intent =
        new Intent(
            TiApplication.getInstance().getApplicationContext(), AlarmNotificationListener.class);
    // Add some extra information so when the alarm goes off we have enough to create the
    // notification
    intent.putExtra("notification_title", contentTitle);
    intent.putExtra("notification_msg", contentText);
    intent.putExtra("notification_has_icon", (notificationIcon != 0));
    intent.putExtra("notification_icon", notificationIcon);
    intent.putExtra("notification_sound", notificationSound);
    intent.putExtra("notification_play_sound", playSound);
    intent.putExtra("notification_vibrate", doVibrate);
    intent.putExtra("notification_show_lights", showLights);
    intent.putExtra("notification_requestcode", requestCode);
    intent.putExtra("notification_root_classname", rootActivityClassName);
    intent.putExtra("notification_request_code", requestCode);
    intent.setData(Uri.parse("alarmId://" + requestCode));
    return intent;
  }
  @Kroll.method
  public void addAlarmService(@SuppressWarnings("rawtypes") HashMap hm) {
    @SuppressWarnings("unchecked")
    KrollDict args = new KrollDict(hm);
    if (!args.containsKeyAndNotNull("service")) {
      throw new IllegalArgumentException("Service name (service) is required");
    }
    if (!args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("second")) {
      throw new IllegalArgumentException("The minute or second field is required");
    }
    Calendar calendar = null;
    boolean isRepeating = hasRepeating(args);
    long repeatingFrequency = 0;
    if (isRepeating) {
      repeatingFrequency = repeatingFrequency(args);
    }

    // If seconds are provided but not years, we just take the seconds to mean to add seconds until
    // fire
    boolean secondBased =
        (args.containsKeyAndNotNull("second") && !args.containsKeyAndNotNull("year"));

    // If minutes are provided but not years, we just take the minutes to mean to add minutes until
    // fire
    boolean minuteBased =
        (args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("year"));

    // Based on what kind of duration we build our calendar
    if (secondBased) {
      calendar = getSecondBasedCalendar(args);
    } else if (minuteBased) {
      calendar = getMinuteBasedCalendar(args);
    } else {
      calendar = getFullCalendar(args);
    }

    // Get the requestCode if provided, if none provided
    // we use 192837 for backwards compatibility
    int requestCode = args.optInt("requestCode", AlarmmanagerModule.DEFAULT_REQUEST_CODE);

    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    utils.debugLog("Creating Alarm Notification for: " + sdf.format(calendar.getTime()));

    AlarmManager am =
        (AlarmManager)
            TiApplication.getInstance()
                .getApplicationContext()
                .getSystemService(TiApplication.ALARM_SERVICE);
    Intent intent = createAlarmServiceIntent(args);

    if (isRepeating) {
      utils.debugLog("Setting Alarm to repeat at frequency " + repeatingFrequency);
      PendingIntent pendingIntent =
          PendingIntent.getBroadcast(
              TiApplication.getInstance().getApplicationContext(),
              requestCode,
              intent,
              PendingIntent.FLAG_UPDATE_CURRENT);
      am.setRepeating(
          AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), repeatingFrequency, pendingIntent);
    } else {
      PendingIntent sender =
          PendingIntent.getBroadcast(
              TiApplication.getInstance().getApplicationContext(),
              requestCode,
              intent,
              PendingIntent.FLAG_UPDATE_CURRENT);
      utils.debugLog("Setting Alarm for a single run");
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }

    utils.infoLog("Alarm Service Request Created");
  }
 private boolean hasRepeating(KrollDict args) {
   boolean results = (args.containsKeyAndNotNull("repeat"));
   utils.debugLog("Repeat Frequency enabled: " + results);
   return results;
 }
Ejemplo n.º 7
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);
        }
      }
    }
  }
Ejemplo n.º 8
0
 private boolean hasColorState(KrollDict d) {
   return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_FOCUSED_COLOR)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_FOCUSED_COLOR);
 }
Ejemplo n.º 9
0
 private boolean hasBorder(KrollDict d) {
   return d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_COLOR)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_RADIUS)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_WIDTH);
 }
Ejemplo n.º 10
0
 private boolean hasImage(KrollDict d) {
   return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_IMAGE)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_IMAGE)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_FOCUSED_IMAGE)
       || d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_DISABLED_IMAGE);
 }
Ejemplo n.º 11
0
 private boolean hasRepeat(KrollDict d) {
   return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_REPEAT);
 }
Ejemplo n.º 12
0
  public static TiAnnotation fromDict(final KrollDict dict) {
    TiAnnotation a = null;
    if (dict.containsKeyAndNotNull(TiC.PROPERTY_ID)) {
      a = new TiAnnotation(dict.getString(TiC.PROPERTY_ID));
    } else {
      a = new TiAnnotation();
      dict.put(TiC.PROPERTY_ID, a.getId());
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_TITLE)) {
      a.setTitle(dict.getString(TiC.PROPERTY_TITLE));
    }
    if (dict.containsKeyAndNotNull(TiC.PROPERTY_SUBTITLE)) {
      a.setSubtitle(dict.getString(TiC.PROPERTY_SUBTITLE));
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_LATITUDE)) {
      a.setLatitude(dict.getDouble(TiC.PROPERTY_LATITUDE));
    }
    if (dict.containsKeyAndNotNull(TiC.PROPERTY_LONGITUDE)) {
      a.setLongitude(dict.getDouble(TiC.PROPERTY_LONGITUDE));
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_IMAGE)
        || dict.containsKeyAndNotNull(TiC.PROPERTY_PIN_IMAGE)) {
      a.setImage(dict.getString(TiC.PROPERTY_IMAGE));
      if (a.getImage() == null) {
        a.setImage(dict.getString(TiC.PROPERTY_PIN_IMAGE));
      }
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_PINCOLOR)) {
      Object pinColor = dict.get(TiC.PROPERTY_PINCOLOR);
      if (pinColor instanceof String) {
        a.setPinColor((String) pinColor);
      } else {
        a.setPinColor(dict.getInt(TiC.PROPERTY_PINCOLOR));
      }
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_LEFT_BUTTON)) {
      a.setLeftButton(TiConvert.toString(dict, TiC.PROPERTY_LEFT_BUTTON));
    }
    if (dict.containsKeyAndNotNull(TiC.PROPERTY_RIGHT_BUTTON)) {
      a.setRightButton(TiConvert.toString(dict, TiC.PROPERTY_RIGHT_BUTTON));
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_LEFT_VIEW)) {
      Object leftView = dict.get(TiC.PROPERTY_LEFT_VIEW);
      if (leftView instanceof TiViewProxy) {
        a.setLeftView((TiViewProxy) leftView);
      }
    }
    if (dict.containsKeyAndNotNull(TiC.PROPERTY_RIGHT_VIEW)) {
      Object rightView = dict.get(TiC.PROPERTY_RIGHT_VIEW);
      if (rightView instanceof TiViewProxy) {
        a.setRightView((TiViewProxy) rightView);
      }
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_ANIMATE)) {
      a.setAnimate(dict.getBoolean(TiC.PROPERTY_ANIMATE));
    } else {
      a.setAnimate(Boolean.FALSE);
    }

    if (dict.containsKeyAndNotNull(TiC.PROPERTY_CENTER)) {
      a.setCenter(dict.getBoolean(TiC.PROPERTY_CENTER));
    } else {
      a.setCenter(Boolean.TRUE);
    }
    return a;
  }