Search in sources :

Example 36 with Sensor

use of android.hardware.Sensor in project aware-client by denzilferreira.

the class Aware method sensorDiff.

/**
 * This function returns a list of sensors to enable and one to disable because of server side configuration changes
 *
 * @param server
 * @param local
 * @return
 * @throws JSONException
 */
private static ArrayList<JSONArray> sensorDiff(Context context, JSONArray server, JSONArray local) throws JSONException {
    SensorManager manager = (SensorManager) context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> sensors = manager.getSensorList(Sensor.TYPE_ALL);
    Hashtable<Integer, Boolean> listSensorType = new Hashtable<>();
    for (int i = 0; i < sensors.size(); i++) {
        listSensorType.put(sensors.get(i).getType(), true);
    }
    Hashtable<String, Integer> optionalSensors = new Hashtable<>();
    optionalSensors.put(Aware_Preferences.STATUS_ACCELEROMETER, Sensor.TYPE_ACCELEROMETER);
    optionalSensors.put(Aware_Preferences.STATUS_SIGNIFICANT_MOTION, Sensor.TYPE_ACCELEROMETER);
    optionalSensors.put(Aware_Preferences.STATUS_BAROMETER, Sensor.TYPE_PRESSURE);
    optionalSensors.put(Aware_Preferences.STATUS_GRAVITY, Sensor.TYPE_GRAVITY);
    optionalSensors.put(Aware_Preferences.STATUS_GYROSCOPE, Sensor.TYPE_GYROSCOPE);
    optionalSensors.put(Aware_Preferences.STATUS_LIGHT, Sensor.TYPE_LIGHT);
    optionalSensors.put(Aware_Preferences.STATUS_LINEAR_ACCELEROMETER, Sensor.TYPE_LINEAR_ACCELERATION);
    optionalSensors.put(Aware_Preferences.STATUS_MAGNETOMETER, Sensor.TYPE_MAGNETIC_FIELD);
    optionalSensors.put(Aware_Preferences.STATUS_PROXIMITY, Sensor.TYPE_PROXIMITY);
    optionalSensors.put(Aware_Preferences.STATUS_ROTATION, Sensor.TYPE_ROTATION_VECTOR);
    optionalSensors.put(Aware_Preferences.STATUS_TEMPERATURE, Sensor.TYPE_AMBIENT_TEMPERATURE);
    Set<String> keys = optionalSensors.keySet();
    JSONArray to_enable = new JSONArray();
    JSONArray to_disable = new JSONArray();
    ArrayList<String> immutable_settings = new ArrayList<>();
    immutable_settings.add("status_mqtt");
    immutable_settings.add("mqtt_server");
    immutable_settings.add("mqtt_port");
    immutable_settings.add("mqtt_keep_alive");
    immutable_settings.add("mqtt_qos");
    immutable_settings.add("mqtt_username");
    immutable_settings.add("mqtt_password");
    immutable_settings.add("status_esm");
    immutable_settings.add("study_id");
    immutable_settings.add("study_start");
    immutable_settings.add("webservice_server");
    immutable_settings.add("status_webservice");
    // enable new sensors from the server
    for (int i = 0; i < server.length(); i++) {
        JSONObject server_sensor = server.getJSONObject(i);
        for (String optionalSensor : keys) {
            if (server_sensor.getString("setting").equalsIgnoreCase(optionalSensor) && !listSensorType.containsKey(optionalSensors.get(optionalSensor)))
                continue;
        }
        if (immutable_settings.contains(server_sensor.getString("setting"))) {
            // don't do anything
            continue;
        }
        boolean is_present = false;
        for (int j = 0; j < local.length(); j++) {
            JSONObject local_sensor = local.getJSONObject(j);
            if (local_sensor.getString("setting").equalsIgnoreCase(server_sensor.getString("setting")) && local_sensor.getString("value").equals(server_sensor.getString("value"))) {
                is_present = true;
                break;
            }
        }
        if (!is_present)
            to_enable.put(server_sensor);
    }
    // disable local sensors that are no longer in the server
    for (int j = 0; j < local.length(); j++) {
        JSONObject local_sensor = local.getJSONObject(j);
        if (immutable_settings.contains(local_sensor.getString("setting"))) {
            // don't do anything
            continue;
        }
        for (String optionalSensor : keys) {
            if (local_sensor.getString("setting").equalsIgnoreCase(optionalSensor) && !listSensorType.containsKey(optionalSensors.get(optionalSensor)))
                continue;
        }
        boolean remove = true;
        for (int i = 0; i < server.length(); i++) {
            JSONObject server_sensor = server.getJSONObject(i);
            if (local_sensor.getString("setting").equalsIgnoreCase(server_sensor.getString("setting"))) {
                remove = false;
                break;
            }
        }
        if (remove)
            to_disable.put(local_sensor);
    }
    ArrayList<JSONArray> output = new ArrayList<>();
    output.add(to_enable);
    output.add(to_disable);
    return output;
}
Also used : Hashtable(java.util.Hashtable) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) SensorManager(android.hardware.SensorManager) JSONObject(org.json.JSONObject) Sensor(android.hardware.Sensor)

Aggregations

Sensor (android.hardware.Sensor)36 SensorManager (android.hardware.SensorManager)7 SensorEventListener (android.hardware.SensorEventListener)5 BatteryStats (android.os.BatteryStats)5 SensorEvent (android.hardware.SensorEvent)4 Intent (android.content.Intent)2 Handler (android.os.Handler)2 ArrayList (java.util.ArrayList)2 NotificationChannel (android.app.NotificationChannel)1 NotificationManager (android.app.NotificationManager)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 ViewPager (android.support.v4.view.ViewPager)1 View (android.view.View)1 ViewTreeObserver (android.view.ViewTreeObserver)1 Window (android.view.Window)1 AdapterView (android.widget.AdapterView)1 TextView (android.widget.TextView)1 InjectView (butterknife.InjectView)1 BmobUser (cn.bmob.v3.BmobUser)1