package se.nekotronic.satelliterush; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class RushActivity extends Activity { private RushView the_view; private PowerManager the_power_manager; private WakeLock the_wake_lock; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); the_view = (RushView) findViewById(R.id.rushview); // 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()); } // onCreate @Override protected void onResume() { super.onResume(); /* When the activity is resumed, we acquire a wake-lock so that the * screen stays on, since the user will likely not be fiddling with the * screen or buttons. */ the_wake_lock.acquire(); // Start the simulation the_view.resume(); } @Override protected void onPause() { super.onPause(); the_view.pause(); the_wake_lock.release(); } 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; static final private int MENU_HELP = Menu.FIRST + 5; MenuItem item_left; MenuItem item_right; MenuItem item_start; MenuItem item_pause; MenuItem item_quit; MenuItem item_help; @Override public boolean onCreateOptionsMenu(Menu menu) { item_left = menu.add(Menu.NONE, MENU_LEFT, Menu.NONE, R.string.menutext_left); item_right = menu.add(Menu.NONE, MENU_RIGHT, Menu.NONE, R.string.menutext_right); item_start = menu.add(Menu.NONE, MENU_START, Menu.NONE, R.string.menutext_start); item_pause = menu.add(Menu.NONE, MENU_PAUSE, Menu.NONE, R.string.menutext_pause); item_quit = menu.add(Menu.NONE, MENU_QUIT, Menu.NONE, R.string.menutext_quit); item_help = menu.add(Menu.NONE, MENU_HELP, Menu.NONE, R.string.menutext_help); return true; } public void disable_corner_commands() { item_left.setEnabled(false); item_right.setEnabled(false); } @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.", this); the_view.mark_left_corner(); break; case MENU_RIGHT: // showWarning("Can't set right corner yet.", this); the_view.mark_right_corner(); break; case MENU_START: the_view.start_moving(); break; case MENU_PAUSE: the_view.stop_moving(); break; case MENU_QUIT: askQuit(); break; case MENU_HELP: Intent the_help_intent = new Intent(RushActivity.this, HelpActivity.class); startActivity(the_help_intent); break; default: showWarning("This can't happen. How strange.", this); break; } // switch return true; } // onOptionsItemSelected public static void showWarning(String message, Context c) { AlertDialog.Builder builder = new AlertDialog.Builder(c); builder.setTitle("Hi there!"); builder.setMessage(message); builder.setNeutralButton("Ok", null); builder.show(); } // showWarning private void askQuit() { final Activity the_activity = this; final 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(); } // askQuit } // class RushActivity