Android Open Source - android-nfl-passer-rating Passer Rating Activity






From Project

Back to project page android-nfl-passer-rating.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project android-nfl-passer-rating listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.pffy.qbrating;
/*ww  w.j  a va2s.com*/
/*
 * This is free and unencumbered software released into the public domain.
 * 
 * Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either
 * in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and
 * by any means.
 * 
 * In jurisdictions that recognize copyright laws, the author or authors of this software dedicate
 * any and all copyright interest in the software to the public domain. We make this dedication for
 * the benefit of the public at large and to the detriment of our heirs and successors. We intend
 * this dedication to be an overt act of relinquishment in perpetuity of all present and future
 * rights to this software under copyright law.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * For more information, please refer to <http://unlicense.org/>
 */


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.pffy.sports.NflPasserRating;

/*
 * PasserRatingActivity - Calculate the QB Passer Rating. Share with friends.
 * 
 * @author The Pffy Authors
 */


public class PasserRatingActivity extends Activity {

  private int passesCompleted = 0;
  private int passesAttempted = 0;
  private int yardsPassed = 0;
  private int touchdownPasses = 0;
  private int passesIntercepted = 0;

  private String passerRatingString = "";

  private LinearLayout linlay_lining;

  private Button btn_calculate;
  private Button btn_reset;
  private Button btn_share;

  private EditText et_completions;
  private EditText et_attempts;
  private EditText et_yards;
  private EditText et_touchdowns;
  private EditText et_interceptions;

  private TextView tv_rating;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_passer_rating);

    this.linlay_lining = (LinearLayout) findViewById(R.id.linlay_lining);
    this.linlay_lining.setFocusable(true);

    this.et_completions = (EditText) findViewById(R.id.et_completions);
    this.et_attempts = (EditText) findViewById(R.id.et_attempts);
    this.et_yards = (EditText) findViewById(R.id.et_yards);
    this.et_touchdowns = (EditText) findViewById(R.id.et_touchdowns);
    this.et_interceptions = (EditText) findViewById(R.id.et_interceptions);

    this.btn_calculate = (Button) findViewById(R.id.btn_calculate);
    this.btn_reset = (Button) findViewById(R.id.btn_reset);
    this.btn_share = (Button) findViewById(R.id.btn_share);

    this.tv_rating = (TextView) findViewById(R.id.tv_passer_rating);

    // restore persistent preferences
    SharedPreferences shpref = this.getPreferences(Context.MODE_PRIVATE);

    this.passesCompleted = shpref.getInt(getString(R.string.str_pref_completed), 0);
    this.passesAttempted = shpref.getInt(getString(R.string.str_pref_attempted), 0);
    this.yardsPassed = shpref.getInt(getString(R.string.str_pref_yards), 0);
    this.touchdownPasses = shpref.getInt(getString(R.string.str_pref_touchdowns), 0);
    this.passesIntercepted = shpref.getInt(getString(R.string.str_pref_interceptions), 0);

    this.updateByStats();
    this.calculateQbRating();    
    this.requestStartupFocus();

    // event listeners

    this.et_completions.setOnEditorActionListener(editHandler);
    this.et_attempts.setOnEditorActionListener(editHandler);
    this.et_yards.setOnEditorActionListener(editHandler);
    this.et_touchdowns.setOnEditorActionListener(editHandler);
    this.et_interceptions.setOnEditorActionListener(editHandler);

    this.et_completions.setOnFocusChangeListener(focusHandler);
    this.et_attempts.setOnFocusChangeListener(focusHandler);
    this.et_yards.setOnFocusChangeListener(focusHandler);
    this.et_touchdowns.setOnFocusChangeListener(focusHandler);
    this.et_interceptions.setOnFocusChangeListener(focusHandler);

    this.btn_calculate.setOnClickListener(clickHandler);
    this.btn_reset.setOnClickListener(clickHandler);
    this.btn_share.setOnClickListener(clickHandler);

  };


  // saves persistently, just before app quits
  @Override
  protected void onDestroy() {

    super.onDestroy();

    SharedPreferences shpref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = shpref.edit();

    editor.putInt(getString(R.string.str_pref_completed), this.passesCompleted);
    editor.putInt(getString(R.string.str_pref_attempted), this.passesAttempted);
    editor.putInt(getString(R.string.str_pref_yards), this.yardsPassed);
    editor.putInt(getString(R.string.str_pref_touchdowns), this.touchdownPasses);
    editor.putInt(getString(R.string.str_pref_interceptions), this.passesIntercepted);

    editor.commit();

  };


  // saves on screen rotation or similar event
  @Override
  protected void onSaveInstanceState(Bundle savedInstanceState) {

    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt("IntCompleted", this.passesCompleted);
    savedInstanceState.putInt("IntAttempted", this.passesAttempted);
    savedInstanceState.putInt("IntYardsPassed", this.yardsPassed);
    savedInstanceState.putInt("IntTouchdowns", this.touchdownPasses);
    savedInstanceState.putInt("IntInterceptions", this.passesIntercepted);

  };


  // restores after screen rotation or similar event
  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {

    super.onRestoreInstanceState(savedInstanceState);

    this.passesCompleted = savedInstanceState.getInt("IntCompleted");
    this.passesAttempted = savedInstanceState.getInt("IntAttempted");
    this.yardsPassed = savedInstanceState.getInt("IntYardsPassed");
    this.touchdownPasses = savedInstanceState.getInt("IntTouchdowns");
    this.passesIntercepted = savedInstanceState.getInt("IntInterceptions");

    this.updateByStats();
    this.calculateQbRating();
  };

  
  // calculates NFL (or other) passer rating
  private void calculateQbRating() {

    passesCompleted = this.getValueFromString(this.et_completions.getText().toString());
    passesAttempted = this.getValueFromString(this.et_attempts.getText().toString());
    yardsPassed = this.getValueFromString(this.et_yards.getText().toString());
    touchdownPasses = this.getValueFromString(this.et_touchdowns.getText().toString());
    passesIntercepted = this.getValueFromString(this.et_interceptions.getText().toString());

    // checks to avoid unnecessary calculation
    if (isAllZeros()) {

      resetAllStats();

    } else {

      NflPasserRating pr = new NflPasserRating();

      pr.setCompletedPasses(passesCompleted);
      pr.setAttemptedPasses(passesAttempted);
      pr.setYardsPassing(yardsPassed);
      pr.setTouchdownPasses(touchdownPasses);
      pr.setInterceptedPasses(passesIntercepted);

      this.tv_rating.setText("" + pr.getRating());
      this.passerRatingString = "Passer rating is " + pr.getRating();
    }
  };


  // shares current stats and passer rating with chooser
  private void sendByIntent() {

    // be sure to calculate, then share
    calculateQbRating();

    String str = "";

    if (passesAttempted > 0) {

      str = passesCompleted + " of " + passesAttempted + " for ";
      str += yardsPassed + " YD";

      if (yardsPassed != 1) {
        // yards
        str += "s";
      }

      str += ". ";
      str += touchdownPasses + " TD";

      if (touchdownPasses != 1) {
        // touchdowns
        str += "s";
      }

      str += ". ";
      str += passesIntercepted + " INT";

      if (passesIntercepted != 1) {
        // interceptions
        str += "s";
      }

      str += ". ";
      str += passerRatingString;

    } else {
      str += "No stats yet.";
    }

    // boilerplate Intent code found at:
    // http://developer.android.com/training/sharing/send.html
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, str);
    sendIntent.setType("text/plain");
    startActivity(Intent.createChooser(sendIntent, getResources()
        .getText(R.string.str_share_sendto)));

  };

  
  // updates all inputs based on current stats
  private void updateByStats() {

    this.et_completions.setText("" + passesCompleted);
    this.et_attempts.setText("" + passesAttempted);
    this.et_yards.setText("" + yardsPassed);
    this.et_touchdowns.setText("" + touchdownPasses);
    this.et_interceptions.setText("" + passesIntercepted);

  };

  
  // converts String input into integer
  private int getValueFromString(String str) {

    int value = 0;

    try {
      value = Integer.parseInt(str);
    } catch (Exception ex) {
      value = 0; // no problem. moving along.
    }

    return value;
  };

  
  // are all stats set to zero?
  private boolean isAllZeros() {

    if (passesCompleted != 0) {
      return false;
    }

    if (passesAttempted != 0) {
      return false;
    }

    if (yardsPassed != 0) {
      return false;
    }

    if (touchdownPasses != 0) {
      return false;
    }

    if (passesIntercepted != 0) {
      return false;
    }

    return true;
  };


  // focuses on first field entry
  private void requestStartupFocus() {
    this.et_completions.requestFocus();
    this.et_completions.selectAll();
  };

  
  // resets all stats
  private void resetAllStats() {

    this.passesCompleted = 0;
    this.passesAttempted = 0;
    this.yardsPassed = 0;
    this.touchdownPasses = 0;
    this.passesIntercepted = 0;

    this.tv_rating.setText(getResources().getString(R.string.str_blank));
    this.passerRatingString = "";

    this.updateByStats();
    this.requestStartupFocus();
  };


  /**
   * Event Listeners.
   */


  private View.OnFocusChangeListener focusHandler = new View.OnFocusChangeListener() {

    private int viewId = 0;
    private int value = 0;
    private EditText et = null;

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

      viewId = v.getId();
      et = (EditText) findViewById(viewId);

      if (!hasFocus) {

        value = getValueFromString(et.getText().toString());

        switch (viewId) {
          case R.id.et_completions:
            value = Math.max(0, value);
            passesCompleted = value;
            break;
          case R.id.et_attempts:
            value = Math.max(0, value);
            passesAttempted = value;
            break;
          case R.id.et_yards:
            yardsPassed = value;
            break;
          case R.id.et_touchdowns:
            value = Math.max(0, value);
            touchdownPasses = value;
            break;
          case R.id.et_interceptions:
            value = Math.max(0, value);
            passesIntercepted = value;
            break;
          default:
            // do nothing
            break;
        }

        // replace the current text value
        et.setText("" + value);

      } else {
        
        // selecting all text makes it easier to edit
        et.selectAll();
      }


    }
  };


  private TextView.OnEditorActionListener editHandler = new TextView.OnEditorActionListener() {

    private int viewId = 0;
    private int value = 0;
    private EditText et = null;

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

      viewId = v.getId();
      et = (EditText) findViewById(viewId);

      value = getValueFromString(et.getText().toString());

      switch (viewId) {
        case R.id.et_completions:
          passesCompleted = value;
          break;
        case R.id.et_attempts:
          passesAttempted = value;
          break;
        case R.id.et_yards:
          yardsPassed = value;
          break;
        case R.id.et_touchdowns:
          touchdownPasses = value;
          break;
        case R.id.et_interceptions:
          passesIntercepted = value;
          break;
        default:
          // do nothing
          break;
      }

      // replace current text value
      et.setText("" + value);

      return false;
    }
  };


  private View.OnClickListener clickHandler = new View.OnClickListener() {

    private int viewId = 0;

    @Override
    public void onClick(View v) {

      viewId = v.getId();

      switch (viewId) {
        case R.id.btn_calculate:
          calculateQbRating();
          break;
        case R.id.btn_reset:
          resetAllStats();
          break;
        case R.id.btn_share:
          sendByIntent();
          break;
        default:
          // do nothing here.
          break;
      }
    }
  };

};




Java Source Code List

com.github.pffy.qbrating.PasserRatingActivity.java
com.github.pffy.sports.AbstractPasserRating.java
com.github.pffy.sports.AflPasserRating.java
com.github.pffy.sports.NcaaPasserRating.java
com.github.pffy.sports.NflPasserRating.java