Android Open Source - SensOSC Startup Activity






From Project

Back to project page SensOSC.

License

The source code is released under:

Author: Thomas Mayer <thomas@residuum.org> Copyright (c) 2014 Thomas Mayer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation ...

If you think the Android project SensOSC 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 org.residuum.sensosc;
//from   w w  w .  j  av  a2 s  .  c  o  m
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.List;

public class StartupActivity extends FragmentActivity implements SensorEventListener {
  private SensorManager sensorManager;
  private List<SensorFragment> controllers;
  private CompoundButton activeButton;
  private List<Sensor> sensors;
  private int sensorRate;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startup);

    this.activeButton = (CompoundButton) this.findViewById(R.id.active);

    this.sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    this.controllers = new ArrayList<SensorFragment>();
    this.sensors = GetSensors();

    for (Sensor sensor : this.sensors) {
      this.CreateSensorFragments(sensor);
    }
    this.loadPreferences();
  }

  public void addSensorFragment(SensorFragment controller) {
    this.controllers.add(controller);
  }

  private List<Sensor> GetSensors() {
    List<Sensor> sensors = new ArrayList<Sensor>();
    sensors.add(new Sensor(getString(R.string.sensor_accelerator), "accelerator/", android.hardware.Sensor.TYPE_ACCELEROMETER, 3));
    sensors.add(new Sensor(getString(R.string.sensor_gyroscope), "gyroscope/", android.hardware.Sensor.TYPE_GYROSCOPE, 3));
    sensors.add(new Sensor(getString(R.string.sensor_gravity), "gravity/", android.hardware.Sensor.TYPE_GRAVITY, 3));
    sensors.add(new Sensor(getString(R.string.sensor_orientation), "orientation/", android.hardware.Sensor.TYPE_ORIENTATION, 3));

    sensors.add(new Sensor(getString(R.string.sensor_light), "light", android.hardware.Sensor.TYPE_LIGHT, 1));
    sensors.add(new Sensor(getString(R.string.sensor_proximity), "proximity", android.hardware.Sensor.TYPE_PROXIMITY, 1));
    sensors.add(new Sensor(getString(R.string.sensor_temperature), "temperature", android.hardware.Sensor.TYPE_AMBIENT_TEMPERATURE, 1));
    return sensors;
  }

  @Override
  protected void onPause() {
    super.onPause();
    this.sensorManager.unregisterListener(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    for (Sensor sensor : this.sensors) {
      this.RegisterSensor(sensor);
    }
    this.loadPreferences();
  }

  private void RegisterSensor(Sensor sensor) {
    if (this.sensorManager.getDefaultSensor(sensor.getSensorType()) != null) {
      this.sensorManager.registerListener(this,
          this.sensorManager.getDefaultSensor(sensor.getSensorType()),
          this.sensorRate);
    }
  }

  private void CreateSensorFragments(Sensor sensor) {
    if (this.sensorManager.getDefaultSensor(sensor.getSensorType()) != null) {
      FragmentManager manager = this.getSupportFragmentManager();
      SensorGroupFragment groupFragment = (SensorGroupFragment) manager.findFragmentByTag(sensor.getName());
      if (groupFragment == null) {
        CreateFragment(sensor, manager);
      }
    }
  }

  private void CreateFragment(Sensor sensor, FragmentManager manager) {
    FragmentTransaction transaction = manager.beginTransaction();
    SensorGroupFragment groupFragment = new SensorGroupFragment();
    Bundle args = new Bundle();
    args.putInt("dimensions", sensor.getDimensions());
    args.putInt("sensorType", sensor.getSensorType());
    args.putString("oscPrefix", sensor.getOscPrefix());
    args.putString("name", sensor.getName());
    groupFragment.setArguments(args);
    transaction.add(R.id.sensor_group, groupFragment, sensor.getName());
    transaction.commit();
  }

  public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {
    // We do not care about that
  }

  public void onSensorChanged(SensorEvent event) {
    if (!activeButton.isChecked()) {
      return;
    }
    this.sendData(event.sensor.getType(), event.values);
  }

  private void sendData(int sensorType, float[] values) {
    for (int i = 0; i < values.length; i++) {
      for (SensorFragment controller : this.controllers) {
        if (controller.getSensorType() == sensorType && controller.getIndex() == i) {
          controller.setValue(values[i]);
          break;
        }
      }
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.startup, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      Intent intent = new Intent(this, SettingsActivity.class);
      startActivity(intent);
      return true;
    }
    if (id == R.id.action_about) {
      Intent intent = new Intent(this, AboutActivity.class);
      startActivity(intent);
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  private void loadPreferences() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    this.setHost(preferences);
    this.setPort(preferences);
    this.setSensitivity(preferences, this.controllers);
    this.setSensorRate(preferences);
  }

  private void setHost(SharedPreferences preferences) {
    OscConfiguration osc = OscConfiguration.getInstance();
    osc.setHost(preferences.getString("pref_comm_host", "localhost"));
  }

  private void setPort(SharedPreferences preferences) {
    OscConfiguration osc = OscConfiguration.getInstance();
    osc.setPort(Integer.valueOf(preferences.getString("pref_comm_port", "9000")));
  }

  private void setSensitivity(SharedPreferences preferences, List<SensorFragment> controllers) {
    float sensitivity = Float.valueOf(preferences.getString("pref_general_sensitivity", "0"));
    for (SensorFragment controller : controllers) {
      controller.setSensitivity(sensitivity);
    }
  }

  public void setSensorRate(SharedPreferences preferences) {
    this.sensorRate = Integer.valueOf(preferences.getString("pref_general_sensor_rate", "1"));
  }
}




Java Source Code List

org.residuum.sensosc.AboutActivity.java
org.residuum.sensosc.ApplicationTest.java
org.residuum.sensosc.ExtendedEditTextPreference.java
org.residuum.sensosc.OscCommunication.java
org.residuum.sensosc.OscConfiguration.java
org.residuum.sensosc.SensorFragment.java
org.residuum.sensosc.SensorGroupFragment.java
org.residuum.sensosc.Sensor.java
org.residuum.sensosc.SettingsActivity.java
org.residuum.sensosc.StartupActivity.java