Back to project page StrTrainer_android.
The source code is released under:
GNU General Public License
If you think the Android project StrTrainer_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** *// w w w . j a va2s . c o m * StrTrainer /com/davefarinelli/strtrainer/CalculatorActivity.java * * Copyright (c) 2011 Dave Farinelli * * LICENSE: * * This file is part of StrTrainer, a companion Android app for the Starting Strength weight * training program (http://davefarinelli.com/developer/android). * * StrTrainer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any * later version. * * StrTrainer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with StrTrainer. If not, see * <http://www.gnu.org/licenses/>. * * @author Dave Farinelli <davefarinelli[at]gmail[dot]com> * @license http://www.gnu.org/licenses/gpl.html * @copyright 2011 Dave Farinelli */ package com.davefarinelli.strtrainer; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class ComputeTab extends Activity implements OnClickListener { /** Declaration of Shared Preference name */ public static final String PREFS_NAME = "MyPrefsFile"; // integer value used to determine core lift int CoreLift; // constants used to differentiate core lifts final int SQUAT = 0; final int BENCH = 1; final int PRESS = 2; final int DEADLIFT = 3; final int CLEAN = 4; // work set weight to be used float workSetWeight; // warmup factor values for calculating warmup sets float WARMUP_FACTOR_1; float WARMUP_FACTOR_2; float WARMUP_FACTOR_3; float INCREMENT_VALUE; // Create handles for screen widgets EditText etWorkSetWeight; TextView tvWarmup1; TextView tvWarmup2; TextView tvWarmup3; TextView tvWarmup4; TextView tvWorkSet; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.compute_tab); // Grab the Intent object used to find which core lift to compute from Intent sender = getIntent(); CoreLift = sender.getIntExtra("corelift_num", 0); setWarmupFactors(sender); restorePreferences(); //Sets click listener for increment button Button buttonIncrement = (Button)findViewById(R.id.b_increment); buttonIncrement.setOnClickListener(this); //Set increment button to increment value buttonIncrement.setText("+" + Double.toString(INCREMENT_VALUE)); // Initialize handles etWorkSetWeight = (EditText)findViewById(R.id.et_worksetweight); tvWarmup1 = (TextView)findViewById(R.id.tv_warmup1); tvWarmup2 = (TextView)findViewById(R.id.tv_warmup2); tvWarmup3 = (TextView)findViewById(R.id.tv_warmup3); tvWarmup4 = (TextView)findViewById(R.id.tv_warmup4); tvWorkSet = (TextView)findViewById(R.id.tv_worksets); //If applicable, set work set weight to saved value if (workSetWeight != 0) { etWorkSetWeight.setText(Float.toString(workSetWeight)); calculateWarmupValues(); } // Set KeyListener to the Bill EditText widget etWorkSetWeight.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int arg1, KeyEvent arg2) { calculateWarmupValues(); return false; } }); } private void restorePreferences() { SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); switch (CoreLift) { case SQUAT: workSetWeight = settings.getFloat("squatWorkSetValue", 0); INCREMENT_VALUE = settings.getFloat("squatIncrementValue", 5); break; case BENCH: workSetWeight = settings.getFloat("benchWorkSetValue", 0); INCREMENT_VALUE = settings.getFloat("benchIncrementValue", 5); break; case PRESS: workSetWeight = settings.getFloat("pressWorkSetValue", 0); INCREMENT_VALUE = settings.getFloat("pressIncrementValue", 5); break; case DEADLIFT: workSetWeight = settings.getFloat("deadliftWorkSetValue", 0); INCREMENT_VALUE = settings.getFloat("deadliftIncrementValue", 10); break; case CLEAN: workSetWeight = settings.getFloat("cleanWorkSetValue", 0); INCREMENT_VALUE = settings.getFloat("cleanIncrementValue", 5); break; } } private void setWarmupFactors(Intent sender) { switch (CoreLift) { case SQUAT: WARMUP_FACTOR_1 = (float) 0.4; WARMUP_FACTOR_2 = (float) 0.6; WARMUP_FACTOR_3 = (float) 0.8; break; case BENCH: WARMUP_FACTOR_1 = (float) 0.5; WARMUP_FACTOR_2 = (float) 0.7; WARMUP_FACTOR_3 = (float) 0.9; break; case PRESS: WARMUP_FACTOR_1 = (float) 0.55; WARMUP_FACTOR_2 = (float) 0.7; WARMUP_FACTOR_3 = (float) 0.85; break; case DEADLIFT: WARMUP_FACTOR_1 = (float) 0.4; WARMUP_FACTOR_2 = (float) 0.6; WARMUP_FACTOR_3 = (float) 0.8; break; case CLEAN: WARMUP_FACTOR_1 = (float) 0.55; WARMUP_FACTOR_2 = (float) 0.7; WARMUP_FACTOR_3 = (float) 0.85; break; } } private void calculateWarmupValues() { float workSetWeight = 0; float warmup1; float warmup2; float warmup3; int warmup1Int; int warmup2Int; int warmup3Int; /** * Attempts to set workSetWeight as value in et_worksetweight, if empty or non-integer, set all values to zero */ try { //Sets float value to value of et_text workSetWeight = Float.parseFloat(etWorkSetWeight.getText().toString()); // find correct warmup values based off spreadsheet warmup1 = workSetWeight * WARMUP_FACTOR_1; warmup2 = workSetWeight * WARMUP_FACTOR_2; warmup3 = workSetWeight * WARMUP_FACTOR_3; } catch (NumberFormatException x) { warmup1 = 0; warmup2 = 0; warmup3 = 0; } saveWorkSetValueWeight(); // Set integer values of warm up weights warmup1Int = (int)warmup1; warmup2Int = (int)warmup2; warmup3Int = (int)warmup3; // Round integer values to multiple of 5 warmup1Int = Math.round(warmup1Int/5)*5; warmup2Int = Math.round(warmup2Int/5)*5; warmup3Int = Math.round(warmup3Int/5)*5; // Create strings String warmup1String = Integer.toString(warmup1Int); String warmup2String = Integer.toString(warmup2Int); String warmup3String = Integer.toString(warmup3Int); String workSetString = Double.toString(workSetWeight); setWorkSetTextViews(warmup1String, warmup2String, warmup3String, workSetString); } private void setWorkSetTextViews(String warmup1String, String warmup2String, String warmup3String, String workSetString) { // TextView #1 if (CoreLift == DEADLIFT) tvWarmup1.setText("2 x 5 x 135"); else tvWarmup1.setText("2 x 5 x 45"); // TextView #2 if (CoreLift != DEADLIFT) tvWarmup2.setText("1 x 5 x " + warmup1String); // TextView #3 tvWarmup3.setText("1 x 3 x " + warmup2String); // TextView #4 tvWarmup4.setText("1 x 2 x " + warmup3String); // TextView #5 if (CoreLift == DEADLIFT) tvWorkSet.setText("1 x 5 x " + workSetString); else if (CoreLift == CLEAN) tvWorkSet.setText("5 x 3 x " + workSetString); else tvWorkSet.setText("3 x 5 x " + workSetString); } private void saveWorkSetValueWeight() { SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putFloat("squatWorkSetValue", workSetWeight); switch (CoreLift) { case SQUAT: editor.putFloat("squatWorkSetValue", workSetWeight); break; case BENCH: editor.putFloat("benchWorkSetValue", workSetWeight); break; case PRESS: editor.putFloat("pressWorkSetValue", workSetWeight); break; case DEADLIFT: editor.putFloat("deadliftWorkSetValue", workSetWeight); break; case CLEAN: editor.putFloat("cleanWorkSetValue", workSetWeight); break; } // Commit the edits! editor.commit(); } @Override public void onClick(View arg0) { switch(arg0.getId()) { case R.id.b_increment: incrementWeight(); calculateWarmupValues(); break; /* case R.id.b_reset: //Resets value in EditText, set values in TextViews to "-" etWorkSetWeight.setText(""); // Set TextViews tvWarmup1.setText("-"); tvWarmup2.setText("-"); tvWarmup3.setText("-"); tvWarmup4.setText("-"); tvWorkSet.setText("-"); break; */ } } private void incrementWeight() { double workSetWeight = 0.0; /** * Attempts to set workSetWeight as value in et_worksetweight, if empty or non-integer, set all values to zero */ try { //Sets double value to value of et_text workSetWeight = Double.parseDouble(etWorkSetWeight.getText().toString()); } catch (NumberFormatException x) { } workSetWeight = workSetWeight + INCREMENT_VALUE; etWorkSetWeight.setText(Double.toString(workSetWeight)); } }