Ejemplo n.º 1
0
  public void init(final Context context, AttributeSet attrs) {
    // register our interest in hearing about changes to our surface
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    // make sure we get key events
    setFocusable(true);

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    // read attributes
    if (attrs != null) {
      try {
        // create configured widget
        String[] widgets = {
          "Tacho", "Kompass", "Bar", "BatteryBar", "Plotter", "Label", "Timeplot", "BarGraph"
        };
        TypedArray attributes =
            context.getTheme().obtainStyledAttributes(attrs, R.styleable.WidgetView, 0, 0);
        int widgetIndex = attributes.getInt(R.styleable.WidgetView_widget, 0);
        if (widgetIndex < widgets.length) {
          String widget = widgets[widgetIndex];
          // MainActivity.debug("WidgetView: I am a "+widget);
          Class clazz = Class.forName("lu.fisch.canze.widgets." + widget);
          Constructor<?> constructor = clazz.getConstructor(null);
          drawable = (Drawable) constructor.newInstance();
          drawable.setDrawSurface(WidgetView.this);
          // apply attributes
          drawable.setMin(
              Integer.valueOf(
                  extractCarValue(attributes.getString(R.styleable.WidgetView_min).split(","))));
          drawable.setMax(
              Integer.valueOf(
                  extractCarValue(attributes.getString(R.styleable.WidgetView_max).split(","))));
          // drawable.setMin(attributes.getInt(R.styleable.WidgetView_min, 0));
          // drawable.setMax(attributes.getInt(R.styleable.WidgetView_max, 0));
          drawable.setMajorTicks(
              Integer.valueOf(
                  extractCarValue(
                      attributes.getString(R.styleable.WidgetView_majorTicks).split(","))));
          drawable.setMinorTicks(
              Integer.valueOf(
                  extractCarValue(
                      attributes.getString(R.styleable.WidgetView_minorTicks).split(","))));
          // drawable.setMajorTicks(attributes.getInt(R.styleable.WidgetView_majorTicks, 0));
          // drawable.setMinorTicks(attributes.getInt(R.styleable.WidgetView_minorTicks, 0));
          drawable.setTitle(attributes.getString(R.styleable.WidgetView_text));
          drawable.setShowLabels(attributes.getBoolean(R.styleable.WidgetView_showLabels, true));
          drawable.setShowValue(attributes.getBoolean(R.styleable.WidgetView_showValue, true));
          drawable.setInverted(attributes.getBoolean(R.styleable.WidgetView_isInverted, false));

          String colorRangesJson = attributes.getString(R.styleable.WidgetView_colorRanges);
          if (colorRangesJson != null && !colorRangesJson.trim().isEmpty())
            drawable.setColorRanges(new ColorRanges(colorRangesJson.replace("'", "\"")));

          String foreground = attributes.getString(R.styleable.WidgetView_foregroundColor);
          if (foreground != null && !foreground.isEmpty())
            drawable.setForeground(Color.decode(foreground));

          String background = attributes.getString(R.styleable.WidgetView_backgroundColor);
          if (background != null && !background.isEmpty())
            drawable.setBackground(Color.decode(background));

          String intermediate = attributes.getString(R.styleable.WidgetView_intermediateColor);
          if (intermediate != null && !intermediate.isEmpty())
            drawable.setIntermediate(Color.decode(intermediate));

          String titleColor = attributes.getString(R.styleable.WidgetView_titleColor);
          if (titleColor != null && !titleColor.isEmpty())
            drawable.setTitleColor(Color.decode(titleColor));

          String intervalJson = attributes.getString(R.styleable.WidgetView_intervals);
          if (intervalJson != null && !intervalJson.trim().isEmpty())
            drawable.setIntervals(new Intervals(intervalJson.replace("'", "\"")));

          String optionsJson = attributes.getString(R.styleable.WidgetView_options);
          if (optionsJson != null && !optionsJson.trim().isEmpty())
            drawable.setOptions(new Options(optionsJson.replace("'", "\"")));

          // drawable.setMinAlt(attributes.getInt(R.styleable.WidgetView_minAlt, -1));
          // drawable.setMaxAlt(attributes.getInt(R.styleable.WidgetView_maxAlt, -1));

          String minAlt = attributes.getString(R.styleable.WidgetView_minAlt);
          if (minAlt != null && !minAlt.trim().isEmpty())
            drawable.setMinAlt(Integer.valueOf(extractCarValue(minAlt.split(","))));

          String maxAlt = attributes.getString(R.styleable.WidgetView_maxAlt);
          if (maxAlt != null && !maxAlt.trim().isEmpty())
            drawable.setMaxAlt(Integer.valueOf(extractCarValue(maxAlt.split(","))));

          drawable.setTimeScale(attributes.getInt(R.styleable.WidgetView_timeScale, 1));

          fieldSID = attributes.getString(R.styleable.WidgetView_fieldSID);
          if (fieldSID != null) {
            String[] sids = fieldSID.split(",");
            for (int s = 0; s < sids.length; s++) {
              Field field = MainActivity.fields.getBySID(sids[s]);
              if (field == null) {
                MainActivity.debug(
                    "WidgetView: init: Field with SID <"
                        + sids[s]
                        + "> (index <"
                        + s
                        + "> in <"
                        + R.styleable.WidgetView_text
                        + "> not found!");
              } else {
                // add field to list of registered sids for this widget
                drawable.addField(field.getSID());
                // add listener
                field.addListener(drawable);
                // add filter to reader
                int interval = drawable.getIntervals().getInterval(field.getSID());
                if (interval == -1) MainActivity.device.addActivityField(field);
                else MainActivity.device.addActivityField(field, interval);
              }
            }
          }
          // MainActivity.debug("WidgetView: My SID is "+fieldSID);

          if (MainActivity.milesMode) drawable.setTitle(drawable.getTitle().replace("km", "mi"));
        } else {
          MainActivity.debug(
              "WidgetView: init: WidgetIndex "
                  + widgetIndex
                  + " is wrong!? Not registered in <WidgetView>?");
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    // in case your application needs one or more timers,
    // you have to put them here
    /*Timer timer = new Timer();
          timer.schedule(new TimerTask() {
    	@Override
    	public void run() {
    		repaint();
    	}
    }, 100, 100);
    */
  }
Ejemplo n.º 2
0
 public void setColorRanges(ColorRanges colorRanges) {
   drawable.setColorRanges(colorRanges);
 }