Back to project page umpire_buddy.
The source code is released under:
Apache License
If you think the Android project umpire_buddy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bluefairyapps.java.android.umpirebuddy; /* w ww .ja v a 2 s . co m*/ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import com.bluefairyapps.java.android.umpirebuddy.enums.EventsEnum; import com.bluefairyapps.java.android.umpirebuddy.enums.GameStatsEnum; import com.bluefairyapps.java.android.umpirebuddy.event.Event; import com.bluefairyapps.java.android.umpirebuddy.event.EventListener; import com.bluefairyapps.java.android.umpirebuddy.event.game.GameStatUpdateEvent; import com.bluefairyapps.java.android.umpirebuddy.systems.AudioSystem; import com.bluefairyapps.java.android.umpirebuddy.systems.BaseballGameSystem; import com.bluefairyapps.java.android.umpirebuddy.systems.EventManager; public class MainActivity extends Activity implements EventListener { private static final String TAG = MainActivity.class.getSimpleName(); private static final String KEY_TOTAL_OUTS = "com.bluefairyapps.java.android.umpirebuddy.MainActivity.KEY_TOTAL_OUTS"; private EventManager mEventManager; private SharedPreferences mPreferences; @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); setContentView(R.layout.activity_main_updated); mPreferences = getSharedPreferences(getString(R.string.pref_filename), MODE_PRIVATE); mEventManager = EventManager.get(); BaseballGameSystem.init(); if(mPreferences.getBoolean(getString(R.string.pref_audio_key), Boolean.parseBoolean(getString(R.string.pref_audio_dflt)))) { AudioSystem.init(this); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.findItem(R.id.menu_audio).setChecked(mPreferences.getBoolean( getString(R.string.pref_audio_key), Boolean.parseBoolean(getString(R.string.pref_audio_dflt)) )); return super.onPrepareOptionsMenu(menu); } @Override protected void onStart() { super.onStart(); mEventManager.registerForEvent(this, EventsEnum.EVENT_BATTER_OUT); mEventManager.registerForEvent(this, EventsEnum.EVENT_BATTER_WALKS); mEventManager.registerForEvent(this, EventsEnum.EVENT_UPDATE_VIEWS); mEventManager.registerForEvent(this, EventsEnum.EVENT_GAME_OVER); ((TextView) findViewById(R.id.totalOuts)).setText( String.valueOf(mPreferences.getInt(KEY_TOTAL_OUTS, 0)) ); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if(!savedInstanceState.isEmpty()) { BaseballGameSystem.get().restore(savedInstanceState); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); BaseballGameSystem.get().persist(outState); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.menu_newGame: mEventManager.dispatch(new Event(EventsEnum.EVENT_NEW_GAME)); break; case R.id.menu_audio: toggleAudio(item); break; case R.id.menu_about: startActivity(new Intent(this, AboutActivity.class)); break; default: break; } return super.onOptionsItemSelected(item); } @Override public void onEvent(Event event) { Log.d(TAG, event.getType().name() + " received."); switch(event.getType()) { case EVENT_BATTER_OUT: showDialog(getString(R.string.message_out)); incrementTotalOuts(); break; case EVENT_BATTER_WALKS: showDialog(getString(R.string.message_walk)); break; case EVENT_UPDATE_VIEWS: updateViews((GameStatUpdateEvent) event); break; case EVENT_GAME_OVER: showDialog(getString(R.string.message_game_over)); break; default: Log.d(TAG, "Unable to handle received event."); } } @Override protected void onStop() { super.onStop(); mEventManager.unregister(this); } @Override protected void onDestroy() { super.onDestroy(); if(AudioSystem.active()) { AudioSystem.get().finish(); } } public void onClick(View view) { Log.d(TAG, "Processing button touch..."); switch(view.getId()) { case R.id.btn_strike: mEventManager.dispatch(new Event(EventsEnum.EVENT_STRIKE)); break; case R.id.btn_ball: mEventManager.dispatch(new Event(EventsEnum.EVENT_BALL)); break; } } private void showDialog(String message) { new AlertDialog.Builder(this) .setMessage(message) .setCancelable(false) .setPositiveButton(R.string.btn_ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create().show(); } private void toggleAudio(MenuItem checkbox) { checkbox.setChecked(!checkbox.isChecked()); if(checkbox.isChecked() && !AudioSystem.active()) { AudioSystem.init(this); } mPreferences.edit().putBoolean(getString(R.string.pref_audio_key), checkbox.isChecked()) .commit(); mEventManager.dispatch(new Event(EventsEnum.EVENT_TOGGLE_AUDIO)); } private void incrementTotalOuts() { int totalOuts = mPreferences.getInt(KEY_TOTAL_OUTS, 0) + 1; mPreferences.edit().putInt(KEY_TOTAL_OUTS, totalOuts).commit(); ((TextView) findViewById(R.id.totalOuts)).setText(String.valueOf(totalOuts)); } private void updateViews(GameStatUpdateEvent event) { Log.d(TAG, "Updating views..."); for(GameStatsEnum statType : event.getStatsTypeList()) { String statValue = String.valueOf(event.getStat(statType)); switch(statType) { case STRIKES: ((TextView) findViewById(R.id.numStrikes)).setText(statValue); break; case BALLS: ((TextView) findViewById(R.id.numBalls)).setText(statValue); break; case OUTS: ((TextView) findViewById(R.id.numOuts)).setText(statValue); break; case INNING: ((TextView) findViewById(R.id.currentInning)).setText(statValue); break; default: Log.d(TAG, "No view to update for game stat: " + statType.name()); } } } }