package se.nekotronic.satelliterushtest; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.util.DisplayMetrics; import android.view.Display; import android.view.Menu; import android.view.MenuItem; import android.view.Surface; import android.view.WindowManager; public class SatelliteRushTestActivity extends Activity implements SensorEventListener { private RushView the_view; private SensorManager the_sensor_manager; private Sensor the_accelerometer; private Display the_display; private PowerManager the_power_manager; private WindowManager the_window_manager; private WakeLock the_wake_lock; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get an instance of the WindowManager the_window_manager = (WindowManager) getSystemService(WINDOW_SERVICE); the_display = the_window_manager.getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); the_display.getMetrics(dm); setContentView(R.layout.main); the_view = (RushView) findViewById(R.id.rushview); // Get an instance of the SensorManager the_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE); the_accelerometer = the_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // Get an instance of the PowerManager the_power_manager = (PowerManager) getSystemService(POWER_SERVICE); // Create a bright wake lock the_wake_lock = the_power_manager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName()); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); the_view.pause(); // the_sensor_manager.unregisterListener(this); // the_wake_lock.release(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); the_view.resume(); //the_wake_lock.acquire(); /* * It is not necessary to get accelerometer events at a very high * rate, by using a slower rate (SENSOR_DELAY_UI), we get an * automatic low-pass filter, which "extracts" the gravity component * of the acceleration. As an added benefit, we use less power and * CPU resources. */ //the_sensor_manager.registerListener(this, the_accelerometer, SensorManager.SENSOR_DELAY_UI); } static final private int MENU_LEFT = Menu.FIRST; static final private int MENU_RIGHT = Menu.FIRST + 1; static final private int MENU_START = Menu.FIRST + 2; static final private int MENU_PAUSE = Menu.FIRST + 3; static final private int MENU_QUIT = Menu.FIRST + 4; @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub // return super.onCreateOptionsMenu(menu); MenuItem item_left = menu.add(Menu.NONE, MENU_LEFT, Menu.NONE, R.string.menutext_left); MenuItem item_right = menu.add(Menu.NONE, MENU_RIGHT, Menu.NONE, R.string.menutext_right); MenuItem item_start = menu.add(Menu.NONE, MENU_START, Menu.NONE, R.string.menutext_start); MenuItem item_pause = menu.add(Menu.NONE, MENU_PAUSE, Menu.NONE, R.string.menutext_pause); MenuItem item_quit = menu.add(Menu.NONE, MENU_QUIT, Menu.NONE, R.string.menutext_quit); return true; } void showWarning(String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Hi there!"); builder.setMessage(message); DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }; builder.setNeutralButton("Ok", listener); builder.show(); } void askYesNo(String question) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(question); DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: // Yes button clicked break; case DialogInterface.BUTTON_NEGATIVE: // No button clicked break; } } // OnClickListener }; builder.setPositiveButton("Yes", listener); builder.setNegativeButton("No", listener); builder.show(); } final private Activity the_activity = this; private void quit_dialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Really quit?"); DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: // "Yes" button clicked the_activity.finish(); break; case DialogInterface.BUTTON_NEGATIVE: // "No" button clicked break; } } // OnClickListener }; builder.setPositiveButton("Yes, quit", listener); builder.setNegativeButton("No", listener); builder.show(); } // quit_dialog @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub // return super.onOptionsItemSelected(item); int menyvalet = item.getItemId(); switch (menyvalet) { case MENU_LEFT: showWarning("Can't set left corner yet"); break; case MENU_RIGHT: showWarning("Can't set right corner yet"); break; case MENU_START: the_view.start(); break; case MENU_PAUSE: the_view.stop(); break; case MENU_QUIT: quit_dialog(); break; default: break; } // switch return true; } // onOptionsItemSelected @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } private float mSensorX; private float mSensorY; @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { switch (the_display.getRotation()) { case Surface.ROTATION_0: mSensorX = event.values[0]; mSensorY = event.values[1]; break; case Surface.ROTATION_90: mSensorX = -event.values[1]; mSensorY = event.values[0]; break; case Surface.ROTATION_180: mSensorX = -event.values[0]; mSensorY = -event.values[1]; break; case Surface.ROTATION_270: mSensorX = event.values[1]; mSensorY = -event.values[0]; break; } the_view.set_gravity(mSensorX, mSensorY); } } // onSensorChanged } // class SatelliteRushTestActivity