Android Open Source - SensorReader Main Activity






From Project

Back to project page SensorReader.

License

The source code is released under:

Apache License

If you think the Android project SensorReader 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 shrine.sensorreader;
//from  www.j a  v a 2  s .  c om
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

public class MainActivity extends Activity implements Button.OnClickListener {
    protected static final String TAG = "SensorReader";
    private final int[] radioButtonIds = new int[]{R.id.radioSaveFile, R.id.radioSendToPc};
    private Context context;
    private SensorManager mSensorManager;
    private SensorReader sensorReader;
    private OutputStream outputStream;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "Activity created.");
        setContentView(R.layout.activity_main);
        context = getApplicationContext();

        super.onCreate(savedInstanceState);
    }

    private OutputStream createFileOutputStream() throws IOException {
        return context.openFileOutput(SensorReader.fileName, Context.MODE_PRIVATE);
    }

    private int getPortFromForm() {
        TextView v = (TextView) findViewById(R.id.editTextPort);
        String s = v.getText().toString();
        Integer i = Integer.parseInt(s);
        return i.intValue();
    }

    private String getIpAddressFromForm() {
        TextView ip1 = (TextView) findViewById(R.id.editTextIp);
        TextView ip2 = (TextView) findViewById(R.id.editTextIp2);
        TextView ip3 = (TextView) findViewById(R.id.editTextIp3);
        TextView ip4 = (TextView) findViewById(R.id.editTextIp4);

        return String.format("%s.%s.%s.%s", ip1.getText().toString(), ip2.getText().toString(), ip3.getText().toString(), ip4.getText().toString());
    }

    private OutputStream createNetworkOutputStream() throws IOException {
        // We are doing network on the Main Thread.
        // Usually this is not permitted but since this is just a simple Tool the restriction will
        // be overridden to keep the App simple.
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
        StrictMode.setThreadPolicy(policy);

        Socket socket = new Socket(getIpAddressFromForm(), getPortFromForm());
        return socket.getOutputStream();
    }

    private OutputStream createOutputStream() throws IOException {
        RadioGroup r = (RadioGroup) findViewById(R.id.radioGroup);
        OutputStream ret;
        if (R.id.radioSaveFile == r.getCheckedRadioButtonId()) {
            ret = createFileOutputStream();
        } else {
            ret = createNetworkOutputStream();
        }

        return ret;
    }

    private void closeOutputStream() throws IOException {
        if (null != outputStream) {
            outputStream.close();
        }
    }

    @Override
    public void onPause() {
        Log.d(TAG, "Stopping Recording");
        stopRecording();
        super.onPause();
    }

    private void startRecording() {
        // Update the UI
        setEnabledForViews(radioButtonIds, false);
        setIpInputEnabled(false);

        // TODO: Recreate the stream if it has been changed after stop
        if (null == mSensorManager) {
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);

            if (null != sensors && sensors.size() > 0) {
                try {
                    outputStream = createOutputStream();
                    sensorReader = new SensorReader(outputStream, mSensorManager, context);

                    for (Sensor s : sensors) {
                        mSensorManager.registerListener(sensorReader, s, SensorManager.SENSOR_DELAY_FASTEST);
                    }
                } catch (IOException e) {
                    Log.d(TAG, "Sensor Reader could not write to file");
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            } else {
                Log.d(TAG, "No Sensors found!");
            }
        }
    }

    private void stopRecording() {
        /* initially, I intended to collect ALL data at runtime and write it out at once to a file
         * in this onPause() method. Unfortunately, after collecting a lot of data, the application
         * crashes at this point. */
        if (null != mSensorManager) {
            mSensorManager.unregisterListener(sensorReader);
            mSensorManager = null;
        }

        try {
            closeOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Update the UI
        setEnabledForViews(radioButtonIds, true);
        RadioButton radioSendToPc = (RadioButton) findViewById(R.id.radioSendToPc);
        setIpInputEnabled(radioSendToPc.isChecked());
        ToggleButton b = (ToggleButton) findViewById(R.id.toggleButton);
        b.setChecked(false);
    }

    private void setEnabledForViews(int[] ids, boolean enabled) {
        for (int id : ids) {
            View v = findViewById(id);
            v.setEnabled(enabled);
        }
    }

    private void setIpInputEnabled(boolean b) {
        int[] ids = {R.id.editTextIp, R.id.editTextIp2, R.id.editTextIp3, R.id.editTextIp4,
                R.id.editTextPort};
        setEnabledForViews(ids, b);
    }

    @Override
    public void onClick(View view) {
        if (view == findViewById(R.id.toggleButton)) {
            ToggleButton b = (ToggleButton) view;
            if (b.isChecked()) {
                startRecording();
            } else {
                stopRecording();
            }
        } else if (view == findViewById(R.id.radioSaveFile) || view == findViewById(R.id.radioSendToPc)) {
            RadioButton radioSendToPc = (RadioButton) findViewById(R.id.radioSendToPc);
            setIpInputEnabled(radioSendToPc.isChecked());
        }
    }

}




Java Source Code List

shrine.sensorreader.ApplicationTest.java
shrine.sensorreader.MainActivity.java
shrine.sensorreader.SensorReader.java