Android Open Source - OHC-android Field






From Project

Back to project page OHC-android.

License

The source code is released under:

MIT License

If you think the Android project OHC-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.openhc.ohc.basestation.device;
//  w w  w . j a  va 2s.com
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import io.openhc.ohc.OHC;

public class Field implements TextWatcher, CompoundButton.OnCheckedChangeListener,
    SeekBar.OnSeekBarChangeListener
{
  private OHC ohc;
  private String device_id;
  private int field_id;
  private Type type;
  private String name;
  private double min_value;
  private double max_value;
  private boolean writable;
  private boolean accessible = true;

  private Object value;

  public Field()
  {
    this.accessible = false;
  }

  public Field(OHC ohc, String devie_id, int field_id, Type type, String name, double min_value, double max_value, boolean writable)
  {
    this(ohc, devie_id, field_id, type, name, min_value, max_value, writable, null);
    switch(type)
    {
      case SLIDER:
      case INT:
        this.value = new Integer((int)this.min_value);
        break;
      case FLOAT:
        this.value = new Double((int)this.max_value);
        break;
      case BOOL:
      case ONOFF:
        this.value = new Boolean(false);
        break;
      case STRING:
        String s = "";
        for(int i = 0; i < Math.ceil(this.min_value); i++)
          s += "-";
        this.value = s;
    }
  }

  public Field(OHC ohc, String devie_id, int field_id, Type type, String name, double min_value, double max_value, boolean writable, Object value)
  {
    this.ohc = ohc;
    this.device_id = devie_id;
    this.field_id = field_id;
    this.type = type;
    this.name = name;
    this.min_value = min_value;
    this.max_value = max_value;
    this.writable = writable;
    this.value = value;
  }

  /*Sets a new value for this field. Throws a ClassCastException if type of
  * supplied value is incompatible with the fields type*/
  public void set_value(Object value) throws ClassCastException
  {
    this.value = this.type.get_data_type().cast(value);
    this.ohc.get_basestation().device_set_field_value(this.device_id, this.field_id, this.value);
  }

  public Type get_type()
  {
    return this.type;
  }

  public String get_name()
  {
    return this.name;
  }

  public boolean is_writable()
  {
    return this.writable;
  }

  public double get_min()
  {
    return this.min_value;
  }

  public double get_max()
  {
    return this.max_value;
  }

  public Object get_value()
  {
    return this.value;
  }

  @Override
  public void beforeTextChanged(CharSequence cs, int start, int count, int after)
  {

  }

  @Override
  public void onTextChanged(CharSequence cs, int start, int before, int count)
  {

  }

  @Override
  public void afterTextChanged(Editable e)
  {
    String str = e.toString();
    switch(this.type)
    {
      case INT:
        this.set_value(Integer.parseInt(str));
        break;
      case FLOAT:
        this.set_value(Double.parseDouble(str));
        break;
      case STRING:
        this.set_value(str);
        break;
    }
  }

  @Override
  public void onCheckedChanged(CompoundButton bt, boolean state)
  {
    this.set_value(state);
  }

  @Override
  public void onStartTrackingTouch(SeekBar sb)
  {

  }

  @Override
  public void onStopTrackingTouch(SeekBar sb)
  {

  }

  @Override
  public void onProgressChanged(SeekBar sb, int progress, boolean from_user)
  {
    this.set_value(progress);
  }

  public boolean is_accessible()
  {
    return this.accessible;
  }

  public enum Type
  {
    INT(Integer.class, "int"),
    FLOAT(Double.class, "float"),
    STRING(String.class, "string"),
    ONOFF(Boolean.class, "onoff"),
    BOOL(Boolean.class, "bool"),
    SLIDER(Integer.class, "slider");

    private Class c;

    Type(Class c, String text_rep)
    {
      this.c = c;
    }

    public Class get_data_type()
    {
      return this.c;
    }
  }
}




Java Source Code List

io.openhc.ohc.ApplicationTest.java
io.openhc.ohc.OHC.java
io.openhc.ohc.OHC_ui.java
io.openhc.ohc.basestation.Basestation.java
io.openhc.ohc.basestation.device.Device.java
io.openhc.ohc.basestation.device.Field.java
io.openhc.ohc.basestation.rpc.Base_rpc.java
io.openhc.ohc.logging.OHC_Logger.java
io.openhc.ohc.skynet.Broadcaster.java
io.openhc.ohc.skynet.Network.java
io.openhc.ohc.skynet.Receiver.java
io.openhc.ohc.skynet.Sender.java
io.openhc.ohc.skynet.Socket_timeout.java
io.openhc.ohc.skynet.transaction.Transaction_generator.java
io.openhc.ohc.ui.Field_adapter.java
io.openhc.ohc.ui.input.filter.Input_filter_float.java
io.openhc.ohc.ui.input.filter.Input_filter_int.java
io.openhc.ohc.ui.input.filter.Input_filter_string.java