commit | author | age
|
7a519e
|
1 |
package net.jacekk.bridge.android; |
b20e6d
|
2 |
|
JK |
3 |
import android.app.Activity; |
0d7d16
|
4 |
import android.content.Intent; |
b20e6d
|
5 |
import android.os.Bundle; |
JK |
6 |
import android.text.Editable; |
|
7 |
import android.text.TextWatcher; |
0d7d16
|
8 |
import android.view.Menu; |
JK |
9 |
import android.view.MenuItem; |
2d8187
|
10 |
import android.widget.CompoundButton; |
b20e6d
|
11 |
import android.widget.EditText; |
JK |
12 |
import android.widget.SeekBar; |
|
13 |
import android.widget.TextView; |
|
14 |
import android.widget.ToggleButton; |
7a519e
|
15 |
|
JK |
16 |
import net.jacekk.bridge.BridgeCompute; |
ae5f07
|
17 |
import net.jacekk.bridge.BridgeInput; |
JK |
18 |
import net.jacekk.bridge.BridgeInputException; |
7a519e
|
19 |
import net.jacekk.bridge.BridgeResult; |
ae5f07
|
20 |
import net.jacekk.bridge.R; |
b20e6d
|
21 |
|
JK |
22 |
public class MainActivity extends Activity { |
|
23 |
private SeekBar BidSlider; |
|
24 |
private TextView BidText; |
|
25 |
|
7a519e
|
26 |
private ToggleButton MinorSuitsToggle; |
JK |
27 |
private ToggleButton MajorSuitsToggle; |
|
28 |
private ToggleButton NoTrumpToggle; |
b20e6d
|
29 |
|
7a519e
|
30 |
private ToggleButton ContractToggle; |
JK |
31 |
private ToggleButton DoubleToggle; |
|
32 |
private ToggleButton RedoubleToggle; |
b20e6d
|
33 |
|
7a519e
|
34 |
private ToggleButton NNToggle; |
JK |
35 |
private ToggleButton NVToggle; |
|
36 |
private ToggleButton VNToggle; |
|
37 |
private ToggleButton VVToggle; |
b20e6d
|
38 |
|
JK |
39 |
private EditText PCInput; |
|
40 |
private EditText TricksInput; |
|
41 |
|
ae5f07
|
42 |
private TextView ResultsText; |
b20e6d
|
43 |
|
JK |
44 |
public BridgeCompute bridgeCompute; |
|
45 |
|
|
46 |
/** |
|
47 |
* Called when the activity is first created. |
|
48 |
*/ |
|
49 |
@Override |
|
50 |
public void onCreate(Bundle savedInstanceState) { |
|
51 |
super.onCreate(savedInstanceState); |
|
52 |
setContentView(R.layout.main); |
|
53 |
|
|
54 |
InitializeApp(); |
|
55 |
} |
|
56 |
|
0d7d16
|
57 |
public boolean onCreateOptionsMenu(Menu menu) { |
JK |
58 |
getMenuInflater().inflate(R.menu.main, menu); |
|
59 |
return super.onCreateOptionsMenu(menu); |
|
60 |
} |
|
61 |
|
|
62 |
public boolean onOptionsItemSelected(MenuItem item) { |
|
63 |
switch (item.getItemId()) { |
|
64 |
case R.id.main_action_about: |
|
65 |
Intent about = new Intent(getApplicationContext(), AboutActivity.class); |
|
66 |
startActivity(about); |
|
67 |
return true; |
|
68 |
case R.id.main_action_reset: |
|
69 |
resetInputs(); |
|
70 |
return true; |
|
71 |
} |
|
72 |
|
|
73 |
return super.onOptionsItemSelected(item); |
|
74 |
} |
|
75 |
|
ae5f07
|
76 |
private void resetInputs() { |
2d8187
|
77 |
BidSlider.setProgress(0); |
JK |
78 |
MinorSuitsToggle.setChecked(true); |
|
79 |
ContractToggle.setChecked(true); |
|
80 |
NNToggle.setChecked(true); |
|
81 |
PCInput.setText(""); |
|
82 |
TricksInput.setText(""); |
ae5f07
|
83 |
ResultsText.setText(getString(R.string.enter_data)); |
2d8187
|
84 |
} |
JK |
85 |
|
0d7d16
|
86 |
protected void InitializeApp() { |
b20e6d
|
87 |
bridgeCompute = new BridgeCompute(); |
JK |
88 |
|
|
89 |
BidSlider = (SeekBar) findViewById(R.id.BidSlider); |
|
90 |
BidText = (TextView) findViewById(R.id.BidText); |
|
91 |
|
7a519e
|
92 |
MinorSuitsToggle = (ToggleButton) findViewById(R.id.MinorSuitsToggle); |
JK |
93 |
MajorSuitsToggle = (ToggleButton) findViewById(R.id.MajorSuitsToggle); |
|
94 |
NoTrumpToggle = (ToggleButton) findViewById(R.id.NoTrumpToggle); |
b20e6d
|
95 |
|
7a519e
|
96 |
ContractToggle = (ToggleButton) findViewById(R.id.ContractToggle); |
JK |
97 |
DoubleToggle = (ToggleButton) findViewById(R.id.DoubleToggle); |
|
98 |
RedoubleToggle = (ToggleButton) findViewById(R.id.RedoubleToggle); |
b20e6d
|
99 |
|
7a519e
|
100 |
NNToggle = (ToggleButton) findViewById(R.id.NNToggle); |
JK |
101 |
NVToggle = (ToggleButton) findViewById(R.id.NVToggle); |
|
102 |
VNToggle = (ToggleButton) findViewById(R.id.VNToggle); |
|
103 |
VVToggle = (ToggleButton) findViewById(R.id.VVToggle); |
b20e6d
|
104 |
|
JK |
105 |
PCInput = (EditText) findViewById(R.id.PCInput); |
|
106 |
TricksInput = (EditText) findViewById(R.id.TricksInput); |
|
107 |
|
ae5f07
|
108 |
ResultsText = (TextView) findViewById(R.id.ResultsText); |
b20e6d
|
109 |
|
2d8187
|
110 |
CompoundButton.OnCheckedChangeListener changeListener; |
JK |
111 |
|
b20e6d
|
112 |
BidSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
JK |
113 |
@Override |
|
114 |
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { |
|
115 |
BidText.setText(Integer.toString(seekBar.getProgress())); |
|
116 |
updateResults(); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public void onStartTrackingTouch(SeekBar seekBar) { |
|
121 |
} |
|
122 |
|
|
123 |
@Override |
|
124 |
public void onStopTrackingTouch(SeekBar seekBar) { |
|
125 |
} |
|
126 |
}); |
|
127 |
|
2d8187
|
128 |
changeListener = new CompoundButton.OnCheckedChangeListener() { |
b20e6d
|
129 |
@Override |
2d8187
|
130 |
public void onCheckedChanged(CompoundButton button, boolean isChecked) { |
JK |
131 |
if (!isChecked) { |
|
132 |
if (!MinorSuitsToggle.isChecked() && !MajorSuitsToggle.isChecked() |
|
133 |
&& !NoTrumpToggle.isChecked()) { |
|
134 |
MinorSuitsToggle.setChecked(true); |
|
135 |
} |
|
136 |
return; |
|
137 |
} |
b20e6d
|
138 |
|
2d8187
|
139 |
if (!button.equals(MinorSuitsToggle)) |
7a519e
|
140 |
MinorSuitsToggle.setChecked(false); |
2d8187
|
141 |
if (!button.equals(MajorSuitsToggle)) |
7a519e
|
142 |
MajorSuitsToggle.setChecked(false); |
2d8187
|
143 |
if (!button.equals(NoTrumpToggle)) |
7a519e
|
144 |
NoTrumpToggle.setChecked(false); |
b20e6d
|
145 |
|
JK |
146 |
updateResults(); |
|
147 |
} |
|
148 |
}; |
|
149 |
|
2d8187
|
150 |
MinorSuitsToggle.setOnCheckedChangeListener(changeListener); |
JK |
151 |
MajorSuitsToggle.setOnCheckedChangeListener(changeListener); |
|
152 |
NoTrumpToggle.setOnCheckedChangeListener(changeListener); |
b20e6d
|
153 |
|
2d8187
|
154 |
changeListener = new CompoundButton.OnCheckedChangeListener() { |
b20e6d
|
155 |
@Override |
2d8187
|
156 |
public void onCheckedChanged(CompoundButton button, boolean isChecked) { |
JK |
157 |
if (!isChecked) { |
|
158 |
if (!ContractToggle.isChecked() && !DoubleToggle.isChecked() |
|
159 |
&& !RedoubleToggle.isChecked()) { |
|
160 |
ContractToggle.setChecked(true); |
|
161 |
} |
|
162 |
return; |
|
163 |
} |
b20e6d
|
164 |
|
2d8187
|
165 |
if (!button.equals(ContractToggle)) |
7a519e
|
166 |
ContractToggle.setChecked(false); |
2d8187
|
167 |
if (!button.equals(DoubleToggle)) |
7a519e
|
168 |
DoubleToggle.setChecked(false); |
2d8187
|
169 |
if (!button.equals(RedoubleToggle)) |
7a519e
|
170 |
RedoubleToggle.setChecked(false); |
b20e6d
|
171 |
|
JK |
172 |
updateResults(); |
|
173 |
} |
|
174 |
}; |
|
175 |
|
2d8187
|
176 |
ContractToggle.setOnCheckedChangeListener(changeListener); |
JK |
177 |
DoubleToggle.setOnCheckedChangeListener(changeListener); |
|
178 |
RedoubleToggle.setOnCheckedChangeListener(changeListener); |
b20e6d
|
179 |
|
2d8187
|
180 |
changeListener = new CompoundButton.OnCheckedChangeListener() { |
JK |
181 |
@Override |
|
182 |
public void onCheckedChanged(CompoundButton button, boolean isChecked) { |
|
183 |
if (!isChecked) { |
|
184 |
if (!VVToggle.isChecked() && !VNToggle.isChecked() |
|
185 |
&& !NVToggle.isChecked() && !NNToggle.isChecked()) { |
|
186 |
NNToggle.setChecked(true); |
|
187 |
} |
|
188 |
return; |
|
189 |
} |
b20e6d
|
190 |
|
2d8187
|
191 |
if (!button.equals(NNToggle)) |
7a519e
|
192 |
NNToggle.setChecked(false); |
2d8187
|
193 |
if (!button.equals(NVToggle)) |
JK |
194 |
NVToggle.setChecked(false); |
|
195 |
if (!button.equals(VNToggle)) |
|
196 |
VNToggle.setChecked(false); |
|
197 |
if (!button.equals(VVToggle)) |
|
198 |
VVToggle.setChecked(false); |
b20e6d
|
199 |
|
JK |
200 |
updateResults(); |
|
201 |
} |
|
202 |
}; |
|
203 |
|
2d8187
|
204 |
VVToggle.setOnCheckedChangeListener(changeListener); |
JK |
205 |
VNToggle.setOnCheckedChangeListener(changeListener); |
|
206 |
NVToggle.setOnCheckedChangeListener(changeListener); |
|
207 |
NNToggle.setOnCheckedChangeListener(changeListener); |
b20e6d
|
208 |
|
80f235
|
209 |
TextWatcher textWatcher = new TextWatcher() { |
JK |
210 |
@Override |
|
211 |
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
|
212 |
} |
b20e6d
|
213 |
|
80f235
|
214 |
@Override |
JK |
215 |
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
|
216 |
} |
b20e6d
|
217 |
|
80f235
|
218 |
@Override |
JK |
219 |
public void afterTextChanged(Editable editable) { |
|
220 |
updateResults(); |
|
221 |
} |
|
222 |
}; |
b20e6d
|
223 |
|
80f235
|
224 |
PCInput.addTextChangedListener(textWatcher); |
JK |
225 |
TricksInput.addTextChangedListener(textWatcher); |
b20e6d
|
226 |
} |
JK |
227 |
|
|
228 |
public void updateResults() { |
ae5f07
|
229 |
BridgeInput input = getBridgeInput(); |
JK |
230 |
|
|
231 |
BridgeResult result; |
|
232 |
try { |
|
233 |
result = bridgeCompute.getResultForInput(input); |
|
234 |
} catch (BridgeInputException e) { |
|
235 |
int errorStringId = BridgeInputExceptionMapper.map(e); |
|
236 |
ResultsText.setText(getString(errorStringId)); |
|
237 |
return; |
|
238 |
} |
|
239 |
|
|
240 |
ResultsText.setText( |
|
241 |
(result.winners == BridgeResult.Side.We ? getString(R.string.result_for_us) |
|
242 |
: getString(R.string.result_for_them)) + |
|
243 |
"\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore + |
|
244 |
"\n" + getString(R.string.result_pts) + " " + result.points + |
|
245 |
"\n" + getString(R.string.result_imps) + " " + result.IMPs + |
|
246 |
"\n" + getString(R.string.result_percent) + " " + result.percent); |
|
247 |
} |
|
248 |
|
|
249 |
private BridgeInput getBridgeInput() { |
b20e6d
|
250 |
int bid = BidSlider.getProgress(); |
JK |
251 |
|
|
252 |
int color = 0; |
7a519e
|
253 |
if (MajorSuitsToggle.isChecked()) |
b20e6d
|
254 |
color = 1; |
7a519e
|
255 |
if (NoTrumpToggle.isChecked()) |
b20e6d
|
256 |
color = 2; |
JK |
257 |
|
|
258 |
int dbl = 0; |
7a519e
|
259 |
if (DoubleToggle.isChecked()) |
b20e6d
|
260 |
dbl = 1; |
7a519e
|
261 |
if (RedoubleToggle.isChecked()) |
b20e6d
|
262 |
dbl = 2; |
JK |
263 |
|
7a519e
|
264 |
int vulnerability = 0; |
JK |
265 |
if (NVToggle.isChecked()) |
|
266 |
vulnerability = 1; |
|
267 |
if (VNToggle.isChecked()) |
|
268 |
vulnerability = 2; |
|
269 |
if (VVToggle.isChecked()) |
|
270 |
vulnerability = 3; |
b20e6d
|
271 |
|
JK |
272 |
int PC = -1; |
|
273 |
try { |
|
274 |
PC = Integer.parseInt(PCInput.getText().toString()); |
7a519e
|
275 |
} catch (NumberFormatException ignored) { |
b20e6d
|
276 |
} |
JK |
277 |
|
|
278 |
int tricks = -1; |
|
279 |
try { |
|
280 |
tricks = Integer.parseInt(TricksInput.getText().toString()); |
7a519e
|
281 |
} catch (NumberFormatException ignored) { |
b20e6d
|
282 |
} |
JK |
283 |
|
ae5f07
|
284 |
BridgeInput input = new BridgeInput(); |
JK |
285 |
input.bid = bid; |
|
286 |
input.suit = BridgeInput.Suit.values()[color]; |
|
287 |
input.contract = BridgeInput.Contract.values()[dbl]; |
|
288 |
input.weVulnerable = vulnerability / 2 >= 1; |
|
289 |
input.theyVulnerable = vulnerability % 2 >= 1; |
|
290 |
input.PC = PC; |
|
291 |
input.tricks = tricks; |
|
292 |
return input; |
b20e6d
|
293 |
} |
JK |
294 |
} |