public List<String> getSensorMethods() { List<String> out = new LinkedList<String>(); for (AbstractSensor sensor : sensors) { if (sensor.isEnabled()) { String name = sensor.getClass().getName(); name = name.substring(name.lastIndexOf('.') + 1); Field[] fields = sensor.getClass().getDeclaredFields(); try { for (Field f : fields) { f.setAccessible(true); Object o = f.get(sensor); if (o instanceof SensorValue) { out.add(name + "." + f.getName()); } } } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (IllegalAccessException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } } } return out; }
public void registerSensor(AbstractSensor sensor) { for (AbstractSensor s : sensors) { if (s.getClass().equals(sensor.getClass())) { Log.d(SensorRegistry.TAG, "Sensor of this class already present, not registering."); return; } } sensors.add(sensor); }
private SensorValue invokeMethod(String classname, String methodname) { AbstractSensor sensor = getSensorForClassname(classname); if (sensor != null) { if (sensor.isEnabled()) { Field[] fields = sensor.getClass().getDeclaredFields(); try { for (Field f : fields) { f.setAccessible(true); Object o = f.get(sensor); if (f.getName().equals(methodname) && o instanceof SensorValue) { return (SensorValue) o; } } } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (IllegalAccessException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } } } return null; }
public void startup(Context context) { this.context = context; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); for (AbstractSensor sensor : sensors) { try { boolean savedstate = prefs.getBoolean(sensor.getClass().getName(), true); Log.d(SensorRegistry.TAG, sensor.getClass().getName() + ": " + savedstate); if (savedstate) sensor.enable(); } catch (Exception e) { sensor.disable(); Log.d(SensorRegistry.TAG, e.toString()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } } }
private AbstractSensor getSensorWithName(String classname) { for (AbstractSensor sensor : sensors) { String sensorname = sensor.getClass().getName(); sensorname = sensorname.substring(sensorname.lastIndexOf('.') + 1); if (sensorname.equals(classname) && sensor.isEnabled()) { return sensor; } } return null; }
public AbstractSensor getSensorForClassname(String classname) { for (AbstractSensor sensor : sensors) { String name = sensor.getClass().getName(); if (classname.lastIndexOf('.') > 0) { // we have a qualified name if (name.equals(classname)) return sensor; } else { name = name.substring(name.lastIndexOf('.') + 1); if (name.equals(classname)) { return sensor; } } } return null; }
public Object[] getSensorMethodSignature(String methodname) { List<String> signature = new LinkedList<String>(); if (methodname.lastIndexOf('.') > 0) { String classname = methodname.substring(0, methodname.lastIndexOf('.')); AbstractSensor sensor = getSensorForClassname(classname); if (sensor != null && sensor.isEnabled()) { Field[] fields = sensor.getClass().getDeclaredFields(); try { for (Field f : fields) { f.setAccessible(true); Object o = f.get(sensor); if (o instanceof SensorValue) { String fieldname = f.getName(); if (fieldname.equals(methodname.substring(methodname.lastIndexOf('.') + 1))) { signature.add(methodname); SensorValue sv = (SensorValue) o; String rettype = sv.getValue().getClass().toString(); // TODO: this will frequently be only String and // changing, // due to be set to "n/a" when no value is // present if (rettype.equals("class [Ljava.lang.Object;")) { signature.add("array"); } else if (rettype.equals("class java.lang.String")) { signature.add("string"); } else if (rettype.equals("class java.lang.Integer")) { signature.add("int"); } else if (rettype.equals("class java.lang.Boolean")) { signature.add("boolean"); } else if (rettype.equals("class java.lang.Double")) { signature.add("double"); } else if (rettype.equals("class java.lang.Float")) { signature.add("ex:float"); } else if (rettype.equals("class java.lang.Long")) { signature.add("ex:i8"); } else if (rettype.equals("class java.lang.Byte")) { signature.add("ex:i1"); } else if (rettype.equals("class java.lang.Short")) { signature.add("ex:i2"); } else { signature.add(rettype.toString()); } // add method parameters: always nil signature.add("ex:nil"); } } } } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (IllegalAccessException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } } if (!signature.isEmpty()) return signature.toArray(); } return null; }