package net.jacekk.bridge.android;
|
|
import android.app.Activity;
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.text.Editable;
|
import android.text.TextWatcher;
|
import android.view.Menu;
|
import android.view.MenuItem;
|
import android.widget.CompoundButton;
|
import android.widget.EditText;
|
import android.widget.SeekBar;
|
import android.widget.TextView;
|
import android.widget.ToggleButton;
|
|
import net.jacekk.bridge.BridgeCompute;
|
import net.jacekk.bridge.BridgeInput;
|
import net.jacekk.bridge.BridgeInputException;
|
import net.jacekk.bridge.BridgeResult;
|
import net.jacekk.bridge.R;
|
|
public class MainActivity extends Activity {
|
private SeekBar BidSlider;
|
private TextView BidText;
|
|
private ToggleButton MinorSuitsToggle;
|
private ToggleButton MajorSuitsToggle;
|
private ToggleButton NoTrumpToggle;
|
|
private ToggleButton ContractToggle;
|
private ToggleButton DoubleToggle;
|
private ToggleButton RedoubleToggle;
|
|
private ToggleButton NNToggle;
|
private ToggleButton NVToggle;
|
private ToggleButton VNToggle;
|
private ToggleButton VVToggle;
|
|
private EditText PCInput;
|
private EditText TricksInput;
|
|
private TextView ResultsText;
|
|
public BridgeCompute bridgeCompute;
|
|
/**
|
* Called when the activity is first created.
|
*/
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.main);
|
|
InitializeApp();
|
}
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
getMenuInflater().inflate(R.menu.main, menu);
|
return super.onCreateOptionsMenu(menu);
|
}
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
switch (item.getItemId()) {
|
case R.id.main_action_about:
|
Intent about = new Intent(getApplicationContext(), AboutActivity.class);
|
startActivity(about);
|
return true;
|
case R.id.main_action_reset:
|
resetInputs();
|
return true;
|
}
|
|
return super.onOptionsItemSelected(item);
|
}
|
|
private void resetInputs() {
|
BidSlider.setProgress(0);
|
MinorSuitsToggle.setChecked(true);
|
ContractToggle.setChecked(true);
|
NNToggle.setChecked(true);
|
PCInput.setText("");
|
TricksInput.setText("");
|
ResultsText.setText(getString(R.string.enter_data));
|
}
|
|
protected void InitializeApp() {
|
bridgeCompute = new BridgeCompute();
|
|
BidSlider = (SeekBar) findViewById(R.id.BidSlider);
|
BidText = (TextView) findViewById(R.id.BidText);
|
|
MinorSuitsToggle = (ToggleButton) findViewById(R.id.MinorSuitsToggle);
|
MajorSuitsToggle = (ToggleButton) findViewById(R.id.MajorSuitsToggle);
|
NoTrumpToggle = (ToggleButton) findViewById(R.id.NoTrumpToggle);
|
|
ContractToggle = (ToggleButton) findViewById(R.id.ContractToggle);
|
DoubleToggle = (ToggleButton) findViewById(R.id.DoubleToggle);
|
RedoubleToggle = (ToggleButton) findViewById(R.id.RedoubleToggle);
|
|
NNToggle = (ToggleButton) findViewById(R.id.NNToggle);
|
NVToggle = (ToggleButton) findViewById(R.id.NVToggle);
|
VNToggle = (ToggleButton) findViewById(R.id.VNToggle);
|
VVToggle = (ToggleButton) findViewById(R.id.VVToggle);
|
|
PCInput = (EditText) findViewById(R.id.PCInput);
|
TricksInput = (EditText) findViewById(R.id.TricksInput);
|
|
ResultsText = (TextView) findViewById(R.id.ResultsText);
|
|
CompoundButton.OnCheckedChangeListener changeListener;
|
|
BidSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
@Override
|
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
|
BidText.setText(Integer.toString(seekBar.getProgress()));
|
updateResults();
|
}
|
|
@Override
|
public void onStartTrackingTouch(SeekBar seekBar) {
|
}
|
|
@Override
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
}
|
});
|
|
changeListener = new CompoundButton.OnCheckedChangeListener() {
|
@Override
|
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
|
if (!isChecked) {
|
if (!MinorSuitsToggle.isChecked() && !MajorSuitsToggle.isChecked()
|
&& !NoTrumpToggle.isChecked()) {
|
MinorSuitsToggle.setChecked(true);
|
}
|
return;
|
}
|
|
if (!button.equals(MinorSuitsToggle))
|
MinorSuitsToggle.setChecked(false);
|
if (!button.equals(MajorSuitsToggle))
|
MajorSuitsToggle.setChecked(false);
|
if (!button.equals(NoTrumpToggle))
|
NoTrumpToggle.setChecked(false);
|
|
updateResults();
|
}
|
};
|
|
MinorSuitsToggle.setOnCheckedChangeListener(changeListener);
|
MajorSuitsToggle.setOnCheckedChangeListener(changeListener);
|
NoTrumpToggle.setOnCheckedChangeListener(changeListener);
|
|
changeListener = new CompoundButton.OnCheckedChangeListener() {
|
@Override
|
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
|
if (!isChecked) {
|
if (!ContractToggle.isChecked() && !DoubleToggle.isChecked()
|
&& !RedoubleToggle.isChecked()) {
|
ContractToggle.setChecked(true);
|
}
|
return;
|
}
|
|
if (!button.equals(ContractToggle))
|
ContractToggle.setChecked(false);
|
if (!button.equals(DoubleToggle))
|
DoubleToggle.setChecked(false);
|
if (!button.equals(RedoubleToggle))
|
RedoubleToggle.setChecked(false);
|
|
updateResults();
|
}
|
};
|
|
ContractToggle.setOnCheckedChangeListener(changeListener);
|
DoubleToggle.setOnCheckedChangeListener(changeListener);
|
RedoubleToggle.setOnCheckedChangeListener(changeListener);
|
|
changeListener = new CompoundButton.OnCheckedChangeListener() {
|
@Override
|
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
|
if (!isChecked) {
|
if (!VVToggle.isChecked() && !VNToggle.isChecked()
|
&& !NVToggle.isChecked() && !NNToggle.isChecked()) {
|
NNToggle.setChecked(true);
|
}
|
return;
|
}
|
|
if (!button.equals(NNToggle))
|
NNToggle.setChecked(false);
|
if (!button.equals(NVToggle))
|
NVToggle.setChecked(false);
|
if (!button.equals(VNToggle))
|
VNToggle.setChecked(false);
|
if (!button.equals(VVToggle))
|
VVToggle.setChecked(false);
|
|
updateResults();
|
}
|
};
|
|
VVToggle.setOnCheckedChangeListener(changeListener);
|
VNToggle.setOnCheckedChangeListener(changeListener);
|
NVToggle.setOnCheckedChangeListener(changeListener);
|
NNToggle.setOnCheckedChangeListener(changeListener);
|
|
TextWatcher textWatcher = new TextWatcher() {
|
@Override
|
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
}
|
|
@Override
|
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
}
|
|
@Override
|
public void afterTextChanged(Editable editable) {
|
updateResults();
|
}
|
};
|
|
PCInput.addTextChangedListener(textWatcher);
|
TricksInput.addTextChangedListener(textWatcher);
|
}
|
|
public void updateResults() {
|
BridgeInput input = getBridgeInput();
|
|
BridgeResult result;
|
try {
|
result = bridgeCompute.getResultForInput(input);
|
} catch (BridgeInputException e) {
|
int errorStringId = BridgeInputExceptionMapper.map(e);
|
ResultsText.setText(getString(errorStringId));
|
return;
|
}
|
|
ResultsText.setText(
|
(result.winners == BridgeResult.Side.We ? getString(R.string.result_for_us)
|
: getString(R.string.result_for_them)) +
|
"\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore +
|
"\n" + getString(R.string.result_pts) + " " + result.points +
|
"\n" + getString(R.string.result_imps) + " " + result.IMPs +
|
"\n" + getString(R.string.result_percent) + " " + result.percent);
|
}
|
|
private BridgeInput getBridgeInput() {
|
int bid = BidSlider.getProgress();
|
|
int color = 0;
|
if (MajorSuitsToggle.isChecked())
|
color = 1;
|
if (NoTrumpToggle.isChecked())
|
color = 2;
|
|
int dbl = 0;
|
if (DoubleToggle.isChecked())
|
dbl = 1;
|
if (RedoubleToggle.isChecked())
|
dbl = 2;
|
|
int vulnerability = 0;
|
if (NVToggle.isChecked())
|
vulnerability = 1;
|
if (VNToggle.isChecked())
|
vulnerability = 2;
|
if (VVToggle.isChecked())
|
vulnerability = 3;
|
|
int PC = -1;
|
try {
|
PC = Integer.parseInt(PCInput.getText().toString());
|
} catch (NumberFormatException ignored) {
|
}
|
|
int tricks = -1;
|
try {
|
tricks = Integer.parseInt(TricksInput.getText().toString());
|
} catch (NumberFormatException ignored) {
|
}
|
|
BridgeInput input = new BridgeInput();
|
input.bid = bid;
|
input.suit = BridgeInput.Suit.values()[color];
|
input.contract = BridgeInput.Contract.values()[dbl];
|
input.weVulnerable = vulnerability / 2 >= 1;
|
input.theyVulnerable = vulnerability % 2 >= 1;
|
input.PC = PC;
|
input.tricks = tricks;
|
return input;
|
}
|
}
|