Skip to content

Commit 3adb7b8

Browse files
Minor bug fixes
git-svn-id: https://apps-for-android.googlecode.com/svn/trunk@158 4c97771b-2a47-0410-a901-cdf36a16b7d0
1 parent 9bc9ef0 commit 3adb7b8

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

BTClickLinkCompete/src/com/google/android/btclc/AirHockey.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.view.SurfaceView;
3131
import android.view.WindowManager;
3232
import android.view.SurfaceHolder.Callback;
33+
import android.view.WindowManager.BadTokenException;
3334
import android.widget.Toast;
3435

3536
import java.util.ArrayList;
@@ -119,7 +120,7 @@ public void OnIncomingConnection(String device) {
119120
int width = d.getWidth();
120121
int height = d.getHeight();
121122
mBall = new Demo_Ball(true, width, height - 60);
122-
mBall.putOnScreen(width/2, (height/2 + (int)(height*.05)), 0, 0, 0, 0, 0);
123+
mBall.putOnScreen(width / 2, (height / 2 + (int) (height * .05)), 0, 0, 0, 0, 0);
123124
}
124125
};
125126

@@ -130,16 +131,24 @@ public void run() {
130131
Builder connectionLostAlert = new Builder(self);
131132

132133
connectionLostAlert.setTitle("Connection lost");
133-
connectionLostAlert.setMessage("Your connection with the other player has been lost.");
134+
connectionLostAlert
135+
.setMessage("Your connection with the other player has been lost.");
134136

135137
connectionLostAlert.setPositiveButton("Ok", new OnClickListener() {
136138
public void onClick(DialogInterface dialog, int which) {
137139
finish();
138140
}
139141
});
140142
connectionLostAlert.setCancelable(false);
143+
try {
141144
connectionLostAlert.show();
142-
}
145+
} catch (BadTokenException e){
146+
// Something really bad happened here;
147+
// seems like the Activity itself went away before
148+
// the runnable finished.
149+
// Bail out gracefully here and do nothing.
150+
}
151+
}
143152
}
144153
self.runOnUiThread(new displayConnectionLostAlert());
145154
}
@@ -204,7 +213,7 @@ protected void onDestroy() {
204213
if (mConnection != null) {
205214
mConnection.shutdown();
206215
}
207-
if (mPlayer != null){
216+
if (mPlayer != null) {
208217
mPlayer.release();
209218
}
210219
super.onDestroy();
@@ -241,8 +250,9 @@ private void doDraw(Canvas c) {
241250
// Point debugPaddleCircle = getPaddleCenter();
242251
// c.drawCircle(debugPaddleCircle.x, debugPaddleCircle.y,
243252
// mPaddleRadius, ballPaint);
244-
245-
c.drawBitmap(mPaddleBmp, p.x - 60, p.y - 200, new Paint());
253+
if (p != null) {
254+
c.drawBitmap(mPaddleBmp, p.x - 60, p.y - 200, new Paint());
255+
}
246256
}
247257
if ((mBall == null) || !mBall.isOnScreen()) {
248258
return;
@@ -259,7 +269,8 @@ private void doDraw(Canvas c) {
259269

260270
// Debug circle
261271
Point debugBallCircle = getBallCenter();
262-
//c.drawCircle(debugBallCircle.x, debugBallCircle.y, mBallRadius, ballPaint);
272+
// c.drawCircle(debugBallCircle.x, debugBallCircle.y, mBallRadius,
273+
// ballPaint);
263274

264275
c.drawBitmap(bmp, x - 17, y - 23, new Paint());
265276
}
@@ -290,19 +301,21 @@ public void run() {
290301
if ((position == UP) && (rivalDevice.length() > 1)) {
291302
mConnection.sendMessage(rivalDevice, mBall.getState() + "|"
292303
+ FLIPTOP);
293-
} else if (position == DOWN){
294-
if (mType == 0){
295-
clientScore = clientScore + 1;
296-
} else {
297-
hostScore = hostScore + 1;
298-
}
299-
mConnection.sendMessage(rivalDevice, "SCORE:" + hostScore + ":" + clientScore);
300-
showScore();
301-
WindowManager w = getWindowManager();
302-
Display d = w.getDefaultDisplay();
303-
int width = d.getWidth();
304-
int height = d.getHeight();
305-
mBall.putOnScreen(width/2, (height/2 + (int)(height*.05)), 0, 0, 0, 0, 0);
304+
} else if (position == DOWN) {
305+
if (mType == 0) {
306+
clientScore = clientScore + 1;
307+
} else {
308+
hostScore = hostScore + 1;
309+
}
310+
mConnection.sendMessage(rivalDevice, "SCORE:" + hostScore + ":"
311+
+ clientScore);
312+
showScore();
313+
WindowManager w = getWindowManager();
314+
Display d = w.getDefaultDisplay();
315+
int width = d.getWidth();
316+
int height = d.getHeight();
317+
mBall.putOnScreen(width / 2, (height / 2 + (int) (height * .05)),
318+
0, 0, 0, 0, 0);
306319
} else {
307320
mBall.doRebound();
308321
}
@@ -381,18 +394,18 @@ public Point getPaddleCenter() {
381394
return new Point(-1, -1);
382395
}
383396
}
384-
385-
private void showScore(){
397+
398+
private void showScore() {
386399
class showScoreRunnable implements Runnable {
387400
public void run() {
388401
String scoreString = "";
389-
if (mType == 0){
402+
if (mType == 0) {
390403
scoreString = hostScore + " - " + clientScore;
391404
} else {
392405
scoreString = clientScore + " - " + hostScore;
393406
}
394407
Toast.makeText(self, scoreString, 0).show();
395-
}
408+
}
396409
}
397410
self.runOnUiThread(new showScoreRunnable());
398411
}

0 commit comments

Comments
 (0)