Quickly switch between the code for the emulator to the code for the real device

In a previous post I explained the differences between the code for the emulator and the code for the real device, but the switch from one to another and vice versa is very cumbersome.
In this post I propose a way to switch quickly from one situation to another with few changes.
The specific code for the emulator has moved to a class that extends Activity (SimulatorActivity), and similarly, the specific code for the real device is moved to a class that extends Activity (DeviceActivity); both classes implement SensorEventListener.
The specific code for your app is contained in a class that extends SimulatorActivity or DeviceActivity; you override the methods onCreate, onResume and onPause and their first line of code must be a call to super.onCreate, super.onResume and super.onPause.
To quickly switch between the code for the emulator to the code for the real device you can simply change what class extends and change the import.

  • SimulatorActivity.java
    package eu.lucazanini.sensor;
    
    import org.openintents.sensorsimulator.hardware.Sensor;
    import org.openintents.sensorsimulator.hardware.SensorEvent;
    import org.openintents.sensorsimulator.hardware.SensorEventListener;
    import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.StrictMode;
    
    public abstract class SimulatorActivity extends Activity implements
    	SensorEventListener {
    
        protected SensorManagerSimulator mSensorManager;
    
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    
        public void onSensorChanged(SensorEvent event) {
    
    	synchronized (this) {
    
    	    int sensor = getSensorType(event);
    
    	    float[] values = event.values;
    
    	    switch (sensor) {
    	    case Sensor.TYPE_ACCELEROMETER:
    
    		break;
    	    case Sensor.TYPE_MAGNETIC_FIELD:
    
    		break;
    	    case Sensor.TYPE_GYROSCOPE:
    
    		break;
    	    default:
    		break;
    	    }
    	}
        }
    
        protected int getSensorType(SensorEvent event) {
    	return event.type;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    
    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    		.permitAll().build();
    	StrictMode.setThreadPolicy(policy);
    
    	mSensorManager = SensorManagerSimulator.getSystemService(this,
    		SENSOR_SERVICE);
    	mSensorManager.connectSimulator();
        }
    
        @Override
        protected void onPause() {
    	super.onPause();
    	mSensorManager.unregisterListener(this);
        }
    
        @Override
        protected void onResume() {
    	super.onResume();
    
    	mSensorManager.registerListener(this,
    		mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    		SensorManagerSimulator.SENSOR_DELAY_NORMAL);
    
    	mSensorManager.registerListener(this,
    		mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
    		SensorManagerSimulator.SENSOR_DELAY_NORMAL);
    
    	mSensorManager.registerListener(this,
    		mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
    		SensorManagerSimulator.SENSOR_DELAY_NORMAL);
        }
    }
    
  • DeviceActivity.java
    package eu.lucazanini.sensor;
    
    import android.app.Activity;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    
    public abstract class DeviceActivity extends Activity implements
    	SensorEventListener {
    
        protected SensorManager mSensorManager;
    
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    
        public void onSensorChanged(SensorEvent event) {
    
    	synchronized (this) {
    
    	    int sensor = getSensorType(event);
    
    	    float[] values = event.values;
    
    	    switch (sensor) {
    	    case Sensor.TYPE_ACCELEROMETER:
    
    		break;
    	    case Sensor.TYPE_MAGNETIC_FIELD:
    
    		break;
    	    case Sensor.TYPE_GYROSCOPE:
    
    		break;
    	    default:
    		break;
    	    }
    	}
        }
    
        protected int getSensorType(SensorEvent event) {
    	Sensor sensor = event.sensor;
    	return sensor.getType();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    
    	mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    
        }
    
        @Override
        protected void onPause() {
    	super.onPause();
    	mSensorManager.unregisterListener(this);
        }
    
        @Override
        protected void onResume() {
    	super.onResume();
    
    	mSensorManager.registerListener(this,
    		mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    		SensorManager.SENSOR_DELAY_NORMAL);
    
    	mSensorManager.registerListener(this,
    		mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
    		SensorManager.SENSOR_DELAY_FASTEST);
    
    	mSensorManager.registerListener(this,
    		mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
    		SensorManager.SENSOR_DELAY_FASTEST);
    
        }
    }
    
  • MainActivity.java
    package eu.lucazanini.sensorsimulator;
    
    // IMPORT FOR REAL DEVICE
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import eu.lucazanini.sensor.DeviceActivity;
    
    // IMPORT FOR EMULATOR
    //import org.openintents.sensorsimulator.hardware.Sensor;
    //import org.openintents.sensorsimulator.hardware.SensorEvent;
    //import eu.lucazanini.sensor.SimulatorActivity;
    
    // PROBABLY YOU NEEED THIS IMPORT
    import android.hardware.SensorManager;
    
    import android.os.Bundle;
    
    // MAINACTIVITY EXTENDS DEVICEACTIVITY OR SIMULATORACTIVITY
    public class MainActivity extends DeviceActivity {
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    	super.onAccuracyChanged(sensor, accuracy);
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
    
    	// YOUR CODE GOES HERE
    	// THIS IS A STARTING PPOINT EXAMPLE
    
    	synchronized (this) {
    
    	    int sensor = getSensorType(event);
    
    	    float[] values = event.values;
    
    	    switch (sensor) {
    	    case Sensor.TYPE_ACCELEROMETER:
    
    		break;
    	    case Sensor.TYPE_MAGNETIC_FIELD:
    
    		break;
    	    case Sensor.TYPE_GYROSCOPE:
    
    		break;
    	    default:
    		break;
    	    }
    	}
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    
    	// YOUR CODE GOES HERE
    
        }
    
        @Override
        protected void onPause() {
    	super.onPause();
    
    	// YOUR CODE GOES HERE
    
        }
    
        @Override
        protected void onResume() {
    	super.onResume();
    
    	// YOUR CODE GOES HERE
    
        }
    }
    

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.