/src/edu/neu/madcourse/francescoliuzzi/mboggle/Boggle.java

https://bitbucket.org/liuzzi/demo-android-app · Java · 143 lines · 96 code · 33 blank · 14 comment · 2 complexity · 324e05c7fb72c6db4c1ee44b9a47eb30 MD5 · raw file

  1. /***
  2. * Excerpted from "Hello, Android",
  3. * published by The Pragmatic Bookshelf.
  4. * Copyrights apply to this code. It may not be used to create training material,
  5. * courses, books, articles, and the like. Contact us if you are in doubt.
  6. * We make no guarantees that this code is fit for any purpose.
  7. * Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
  8. ***/
  9. package edu.neu.madcourse.francescoliuzzi.mboggle;
  10. import edu.neu.madcourse.francescoliuzzi.R;
  11. import android.app.Activity;
  12. import android.app.AlarmManager;
  13. import android.app.PendingIntent;
  14. import android.content.Context;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. import android.os.Bundle;
  18. import android.os.SystemClock;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.view.View.OnClickListener;
  22. public class Boggle extends Activity implements OnClickListener {
  23. public static final String TAG = "MBoggle";
  24. public static String username = "mboggle_liuzzi";
  25. public static String password = "mboggle";
  26. public static final int MINUTE_INTERVAL = 10;
  27. private SharedPreferences sharedPrefs;
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.mboggle_main);
  32. //set up polling service
  33. Log.d(TAG, "Setting up polling service...");
  34. String alarm = Context.ALARM_SERVICE;
  35. AlarmManager am = ( AlarmManager ) getSystemService( alarm );
  36. Intent intent = new Intent( "REFRESH_THIS" );
  37. PendingIntent pi = PendingIntent.getBroadcast( this, 0, intent, 0 );
  38. int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
  39. long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
  40. long triggerTime = SystemClock.elapsedRealtime() + interval;
  41. am.setInexactRepeating( type, triggerTime, interval, pi );
  42. // Set up click listeners for all the buttons
  43. View continueButton = findViewById(R.id.continue_button);
  44. continueButton.setOnClickListener(this);
  45. View newButton = findViewById(R.id.new_button);
  46. newButton.setOnClickListener(this);
  47. View aboutButton = findViewById(R.id.about_button);
  48. aboutButton.setOnClickListener(this);
  49. View exitButton = findViewById(R.id.exit_button);
  50. exitButton.setOnClickListener(this);
  51. View topScoresButton = findViewById(R.id.topscores_button);
  52. topScoresButton.setOnClickListener(this);
  53. View multiplayerButton = findViewById(R.id.multiplayer_button);
  54. multiplayerButton.setOnClickListener(this);
  55. this.sharedPrefs =
  56. getApplicationContext().getSharedPreferences(BoggleGame.class.getSimpleName(),
  57. Activity.MODE_PRIVATE);
  58. //if there is a saved game state...
  59. if(sharedPrefs.getBoolean("continue",false)){
  60. continueButton.setVisibility(View.VISIBLE);
  61. }
  62. }
  63. @Override
  64. protected void onResume() {
  65. super.onResume();
  66. onCreate(null);
  67. }
  68. @Override
  69. protected void onPause() {
  70. super.onPause();
  71. BoggleMusic.stop(this);
  72. }
  73. public void onClick(View v) {
  74. Intent i;
  75. Log.d(TAG,"clicked on main");
  76. switch (v.getId()) {
  77. case R.id.continue_button:
  78. startGame(BoggleGame.CONTINUE);
  79. break;
  80. case R.id.multiplayer_button:
  81. i = new Intent(this, Multiplayer.class);
  82. startActivity(i);
  83. break;
  84. case R.id.topscores_button:
  85. i = new Intent(this, BoggleTopScores.class);
  86. startActivity(i);
  87. break;
  88. // ...
  89. case R.id.about_button:
  90. i = new Intent(this, BoggleAbout.class);
  91. startActivity(i);
  92. break;
  93. // More buttons go here (if any) ...
  94. case R.id.new_button:
  95. Log.d(TAG,"pressed new game but");
  96. startGame(BoggleGame.NEWGAME);
  97. break;
  98. case R.id.exit_button:
  99. finish();
  100. break;
  101. }
  102. }
  103. /** Start a new game with the given difficulty level */
  104. private void startGame(int i) {
  105. Log.d(TAG, "clicked on " + i);
  106. Intent intent = new Intent(this, BoggleGame.class);
  107. intent.putExtra(BoggleGame.OPTION, i);
  108. startActivity(intent);
  109. }
  110. }