Skip to content

Commit a48ff74

Browse files
committed
Fixed various thread and dialog related bugs
1 parent 1285b4a commit a48ff74

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

app/src/main/com/henrykvdb/sttt/MainActivity.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import java.io.Closeable
6363
import java.util.*
6464
import java.util.concurrent.atomic.AtomicReference
6565

66-
fun log(text: String) = if (BuildConfig.DEBUG) Log.e("STTT",text) else 0
66+
fun log(text: String) = if (BuildConfig.DEBUG) Log.e("STTT", text) else 0
6767

6868
@SuppressLint("ShowToast")
6969
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
@@ -75,7 +75,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
7575
private var remoteServiceBound = false
7676
private var remoteServiceStarted = false
7777
private var remoteService: RemoteService? = null
78-
val remote get() = remoteService?.remoteGame()
78+
private val remote get() = remoteService?.remoteGame()
79+
private val remoteConnected get() = remote?.state == RemoteState.CONNECTED
7980

8081
//Bluetooth fields
8182
private var btAdapter = BluetoothAdapter.getDefaultAdapter()
@@ -170,7 +171,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
170171

171172
private var killService = false
172173
override fun onSaveInstanceState(outState: Bundle) {
173-
killService = !isChangingConfigurations && remote?.state != RemoteState.CONNECTED
174+
killService = !isChangingConfigurations && !remoteConnected
174175
outState.putBoolean(BTSERVICE_STARTED_KEY, remoteServiceStarted && !killService)
175176
outState.putBoolean(KEEP_BT_ON_KEY, keepBtOn)
176177
outState.putSerializable(GAMESTATE_KEY, gs)
@@ -181,7 +182,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
181182
isInBackground = true
182183

183184
//Notification telling the user that BtService is still open
184-
if (!killService && remote?.state == RemoteState.CONNECTED) remoteRunningNotification()
185+
if (!killService && remoteConnected) remoteRunningNotification()
185186

186187
//Unbind remoteService and stop if needed
187188
unbindRemoteService(killService)
@@ -262,12 +263,15 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
262263
}
263264

264265
private val remoteReceiver = object : BroadcastReceiver() {
265-
override fun onReceive(context: Context, intent: Intent) = when (intent.action) {
266+
override fun onReceive(context: Context, intent: Intent): Unit = when (intent.action) {
266267
INTENT_MOVE -> gameThread.play(Source.REMOTE, intent.getSerializableExtra(INTENT_DATA) as Byte)
267-
INTENT_NEWGAME -> newGame(intent.getSerializableExtra(INTENT_DATA) as GameState)
268268
INTENT_UNDO -> undo(intent.getBooleanExtra(INTENT_DATA, false))
269269
INTENT_TOAST -> toast(intent.getStringExtra(INTENT_DATA))
270270
INTENT_TURNLOCAL -> turnLocal()
271+
INTENT_NEWGAME -> {
272+
btDialog?.apply { setOnDismissListener { };dismiss() }
273+
newGame(intent.getSerializableExtra(INTENT_DATA) as GameState)
274+
}
271275
else -> throw IllegalStateException(intent.action)
272276
}
273277
}
@@ -281,7 +285,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
281285

282286
if (isInBackground) unbindRemoteService(killService)
283287
else if (gs.type == Source.REMOTE) {
284-
if (remote?.state == RemoteState.CONNECTED) {
288+
if (remoteConnected) {
285289
//Fetch latest board
286290
val newBoard = remote!!.lastBoard
287291
if (newBoard != gs.board()) newBoard.lastMove()?.let { gameThread.play(Source.REMOTE, it) }
@@ -329,7 +333,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
329333
})
330334

331335
if (gs.type == Source.REMOTE) {
332-
btDialog?.dismiss()
333336
val remoteName = remote?.remoteName
334337
if (remoteName != null) setSubTitle(getString(R.string.connected_to, remoteName))
335338
else setSubTitle(getString(R.string.connected))
@@ -439,29 +442,29 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
439442
override fun onOptionsItemSelected(item: MenuItem): Boolean {
440443
if (item.itemId != R.id.action_undo) return false
441444

442-
if (gs.boards.size == 1) {
445+
if (gs.boards.size == 1 || (gs.boards.size == 2 && gs.otherSource() == Source.AI)) {
443446
toast(getString(R.string.no_prev_moves))
444447
return true
445448
}
446449

447-
if (gs.type == Source.REMOTE && remote?.state == RemoteState.CONNECTED)
450+
if (gs.type == Source.REMOTE && remoteConnected)
448451
remote?.sendUndo(false)
449-
else undo(false)
452+
else if (gs.otherSource() == Source.AI)
453+
repeat(2) { undo(true) }
454+
else undo(true)
450455

451456
return true
452457
}
453458

454459
fun undo(force: Boolean) {
455-
if (!force && remote?.state == RemoteState.CONNECTED && gs.type == Source.REMOTE) {
460+
if (!force && remoteConnected && gs.type == Source.REMOTE) {
456461
askUser(getString(R.string.undo_request, remote?.remoteName), { allow ->
457462
if (allow) {
458463
undo(true)
459464
remote?.sendUndo(true)
460465
}
461466
})
462-
} else newGame(GameState.Builder().gs(gs).build().apply {
463-
repeat(if (Source.AI == gs.otherSource() && boards.size > 1) 2 else 1) { popBoard() }
464-
})
467+
} else newGame(GameState.Builder().gs(gs).build().apply { popBoard() })
465468
}
466469

467470
override fun onNavigationItemSelected(item: MenuItem): Boolean {
@@ -519,7 +522,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
519522
.setView(layout)
520523
.setCustomTitle(newTitle(getString(R.string.host_bluetooth_game)))
521524
.setNegativeButton(getString(R.string.close)) { _, _ -> btDialog?.dismiss() }
522-
.setOnDismissListener({ remote?.close() })
525+
.setOnDismissListener({ remote?.close() }) //TODO fix, onconnect dialog gets dismissed lmao
523526
.show())
524527
} else {
525528
val discoverableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)

app/src/main/com/henrykvdb/sttt/remote/BtGame.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,18 @@ class BtGame(val callback: RemoteCallback, val res: Resources) : RemoteGame {
103103
}
104104

105105
override fun run() {
106-
log("BEGIN ListenThread" + this)
107-
106+
log("BEGIN ListenThread $this")
108107
while (this@BtGame.state != RemoteState.CONNECTED && !isInterrupted) {
109108
try {
109+
log("reeeeee $this")
110110
socket = serverSocket?.accept() //Blocking call
111111
} catch (e: IOException) {
112112
log(e.toString())
113113
interrupt()
114114
}
115115

116116
if (!isInterrupted) socket?.let { connected(it, true) } //Manage connection, blocking call
117+
else interrupt()
117118
}
118119

119120
try {
@@ -123,39 +124,35 @@ class BtGame(val callback: RemoteCallback, val res: Resources) : RemoteGame {
123124
} catch (e: IOException) {
124125
log("Could not close sockets when closing ListenThread")
125126
}
126-
127127
log("END ListenThread $this")
128128
}
129129

130130
override fun close() {
131+
interrupt()
131132
serverSocket?.close()
132133
socket?.close()
133134
}
134135
}
135136

136137
private inner class ConnectingThread(device: BluetoothDevice) : CloseableThread() {
137138
private var socket: BluetoothSocket? = null
138-
override fun close() = socket?.close() ?: Unit
139139

140140
init {
141141
try {
142142
this@BtGame.state = RemoteState.CONNECTING
143143
btAdapter.cancelDiscovery()
144144
socket = device.createRfcommSocketToServiceRecord(UUID)
145145
} catch (e: IOException) {
146-
throw e
146+
log(e.toString())
147+
interrupt()
147148
}
148149
}
149150

150151
override fun run() {
151152
log("BEGIN connectingThread" + this)
152-
153153
try {
154154
socket?.connect() //Blocking call
155-
156-
if (!isInterrupted) {
157-
socket?.let { connected(it, false) } //Manage connection, blocking call
158-
}
155+
if (!isInterrupted) socket?.let { connected(it, false) } //Manage connection, blocking call
159156
} catch (e: IOException) {
160157
callback.toast(res.getString(R.string.unable_to_connect))
161158
log(e.toString())
@@ -167,9 +164,13 @@ class BtGame(val callback: RemoteCallback, val res: Resources) : RemoteGame {
167164
} catch (e2: IOException) {
168165
log(e2.toString())
169166
}
170-
171167
log("END connectingThread" + this)
172168
}
169+
170+
override fun close() {
171+
interrupt()
172+
socket?.close()
173+
}
173174
}
174175

175176
private fun connected(socket: BluetoothSocket, isHost: Boolean) {

app/src/main/com/henrykvdb/sttt/remote/RemoteService.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class RemoteService : Service() {
4040
}
4141

4242
fun remoteGame() = remoteGame
43-
4443
fun getType() = type
4544
fun setType(type: RemoteType) {
4645
remoteGame.close()
@@ -52,6 +51,11 @@ class RemoteService : Service() {
5251
}
5352
}
5453

54+
override fun onDestroy() {
55+
super.onDestroy()
56+
remoteGame.close()
57+
}
58+
5559
private val callback = object : RemoteCallback {
5660
override fun newGame(gs: GameState) = this@RemoteService.sendBroadcast(Intent(INTENT_NEWGAME).putExtra(INTENT_DATA, gs))
5761
override fun undo(force: Boolean) = this@RemoteService.sendBroadcast(Intent(INTENT_UNDO).putExtra(INTENT_DATA, force))

0 commit comments

Comments
 (0)