Ejemplo n.º 1
0
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    // react on touch events
    // get pointer index from the event object
    int pointerIndex = event.getActionIndex();

    // get pointer ID
    int pointerId = event.getPointerId(pointerIndex);

    // get masked (not specific to a pointer) action
    int maskedAction = event.getActionMasked();

    MainActivity.debug("WidgetView: maskedAction = " + maskedAction);

    switch (maskedAction) {
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_POINTER_DOWN:
        {
          motionDown = true;
          downX = event.getX();
          downY = event.getY();
          break;
        }
      case MotionEvent.ACTION_MOVE:
        {
          if (Math.abs(downX - event.getX()) + Math.abs(downY - event.getY()) > 20) {
            motionMove = true;
          }

          break;
        }

      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
        {
          if (!motionMove && clickable && MainActivity.isSafe()) {
            Intent intent = new Intent(this.getContext(), WidgetActivity.class);
            selectedDrawable = this.getDrawable();
            this.getContext().startActivity(intent);
          }

          motionDown = false;
          motionMove = false;
          break;
        }
      case MotionEvent.ACTION_CANCEL:
        {
          break;
        }
    }

    invalidate();

    return true;
  }
Ejemplo n.º 2
0
 private void registerApplicationFields() {
   if (safeDrivingMode) {
     // speed
     Field field = fields.getBySID("5d7.0");
     field.addListener(MainActivity.getInstance());
     if (device != null) device.addApplicationField(field, 1000); // query every second
   } else {
     Field field = fields.getBySID("5d7.0");
     field.removeListener(MainActivity.getInstance());
     if (device != null) device.removeApplicationField(field);
   }
 }
Ejemplo n.º 3
0
 public static void toast(final String message) {
   if (instance != null)
     instance.runOnUiThread(
         new Runnable() {
           @Override
           public void run() {
             Toast.makeText(instance, message, Toast.LENGTH_SHORT).show();
           }
         });
 }
 private void addListener(String sid, int intervalMs) {
   Field field;
   field = MainActivity.fields.getBySID(sid);
   if (field != null) {
     field.addListener(this);
     MainActivity.device.addActivityField(field, intervalMs);
     subscribedFields.add(field);
   } else {
     MainActivity.toast("sid " + sid + " does not exist in class Fields");
   }
 }
Ejemplo n.º 5
0
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    MainActivity.debug("MainActivity: onActivityResult");
    MainActivity.debug("MainActivity: onActivityResult > requestCode = " + requestCode);
    MainActivity.debug("MainActivity: onActivityResult > resultCode = " + resultCode);

    // this must be set in any case
    leaveBluetoothOn = false;

    if (requestCode == SETTINGS_ACTIVITY) {
      // load settings
      loadSettings();
    } else if (requestCode == LEAVE_BLUETOOTH_ON) {
      MainActivity.debug("MainActivity: onActivityResult > " + LEAVE_BLUETOOTH_ON);
      returnFromWidget = true;
      // register fields this activity needs
      /*
      registerFields();
       */
    } else super.onActivityResult(requestCode, resultCode, data);
  }
Ejemplo n.º 6
0
  // @Override
  public boolean onTouchEvent__disabled(MotionEvent event) {
    // react on touch events
    // get pointer index from the event object
    int pointerIndex = event.getActionIndex();

    // get pointer ID
    int pointerId = event.getPointerId(pointerIndex);

    // get masked (not specific to a pointer) action
    int maskedAction = event.getActionMasked();

    switch (maskedAction) {
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_POINTER_DOWN:
        {
          if (clickable && MainActivity.isSafe()) {
            Intent intent = new Intent(this.getContext(), WidgetActivity.class);
            selectedDrawable = this.getDrawable();
            this.getContext().startActivity(intent);
          }
          break;
        }
      case MotionEvent.ACTION_MOVE:
        {
          break;
        }
        /*case MotionEvent.ACTION_MOVE: { // a pointer was moved

         break;
        }*/
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
      case MotionEvent.ACTION_CANCEL:
        {
          break;
        }
    }

    invalidate();

    return true;
  }
Ejemplo n.º 7
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.º 8
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(this);
          // apply attributes
          setMin(attributes.getInt(R.styleable.WidgetView_min, 0));
          setMax(attributes.getInt(R.styleable.WidgetView_max, 0));
          setMajorTicks(attributes.getInt(R.styleable.WidgetView_majorTicks, 0));
          setMinorTicks(attributes.getInt(R.styleable.WidgetView_minorTicks, 0));
          setTitle(attributes.getString(R.styleable.WidgetView_text));
          setShowLabels(attributes.getBoolean(R.styleable.WidgetView_showLabels, true));
          setShowValue(attributes.getBoolean(R.styleable.WidgetView_showValue, true));
          setInverted(attributes.getBoolean(R.styleable.WidgetView_isInverted, false));
          fieldSID = attributes.getString(R.styleable.WidgetView_fieldSID);

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

          // MainActivity.debug("WidgetView: My SID is "+fieldSID);

          if (MainActivity.milesMode) setTitle(drawable.getTitle().replace("km", "mi"));

          repaint();
        } else {
          MainActivity.debug(
              "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);
    */
  }