30
30
import android .view .SurfaceView ;
31
31
import android .view .WindowManager ;
32
32
import android .view .SurfaceHolder .Callback ;
33
+ import android .view .WindowManager .BadTokenException ;
33
34
import android .widget .Toast ;
34
35
35
36
import java .util .ArrayList ;
@@ -119,7 +120,7 @@ public void OnIncomingConnection(String device) {
119
120
int width = d .getWidth ();
120
121
int height = d .getHeight ();
121
122
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 );
123
124
}
124
125
};
125
126
@@ -130,16 +131,24 @@ public void run() {
130
131
Builder connectionLostAlert = new Builder (self );
131
132
132
133
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." );
134
136
135
137
connectionLostAlert .setPositiveButton ("Ok" , new OnClickListener () {
136
138
public void onClick (DialogInterface dialog , int which ) {
137
139
finish ();
138
140
}
139
141
});
140
142
connectionLostAlert .setCancelable (false );
143
+ try {
141
144
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
+ }
143
152
}
144
153
self .runOnUiThread (new displayConnectionLostAlert ());
145
154
}
@@ -204,7 +213,7 @@ protected void onDestroy() {
204
213
if (mConnection != null ) {
205
214
mConnection .shutdown ();
206
215
}
207
- if (mPlayer != null ){
216
+ if (mPlayer != null ) {
208
217
mPlayer .release ();
209
218
}
210
219
super .onDestroy ();
@@ -241,8 +250,9 @@ private void doDraw(Canvas c) {
241
250
// Point debugPaddleCircle = getPaddleCenter();
242
251
// c.drawCircle(debugPaddleCircle.x, debugPaddleCircle.y,
243
252
// 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
+ }
246
256
}
247
257
if ((mBall == null ) || !mBall .isOnScreen ()) {
248
258
return ;
@@ -259,7 +269,8 @@ private void doDraw(Canvas c) {
259
269
260
270
// Debug circle
261
271
Point debugBallCircle = getBallCenter ();
262
- //c.drawCircle(debugBallCircle.x, debugBallCircle.y, mBallRadius, ballPaint);
272
+ // c.drawCircle(debugBallCircle.x, debugBallCircle.y, mBallRadius,
273
+ // ballPaint);
263
274
264
275
c .drawBitmap (bmp , x - 17 , y - 23 , new Paint ());
265
276
}
@@ -290,19 +301,21 @@ public void run() {
290
301
if ((position == UP ) && (rivalDevice .length () > 1 )) {
291
302
mConnection .sendMessage (rivalDevice , mBall .getState () + "|"
292
303
+ 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 );
306
319
} else {
307
320
mBall .doRebound ();
308
321
}
@@ -381,18 +394,18 @@ public Point getPaddleCenter() {
381
394
return new Point (-1 , -1 );
382
395
}
383
396
}
384
-
385
- private void showScore (){
397
+
398
+ private void showScore () {
386
399
class showScoreRunnable implements Runnable {
387
400
public void run () {
388
401
String scoreString = "" ;
389
- if (mType == 0 ){
402
+ if (mType == 0 ) {
390
403
scoreString = hostScore + " - " + clientScore ;
391
404
} else {
392
405
scoreString = clientScore + " - " + hostScore ;
393
406
}
394
407
Toast .makeText (self , scoreString , 0 ).show ();
395
- }
408
+ }
396
409
}
397
410
self .runOnUiThread (new showScoreRunnable ());
398
411
}
0 commit comments