Skip to content

Commit

Permalink
Merge branch 'fix-crash-on-wireguard-key-screen'
Browse files Browse the repository at this point in the history
  • Loading branch information
Janito Vaqueiro Ferreira Filho committed Apr 30, 2020
2 parents 9737bd1 + f02ebb5 commit bdae032
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Line wrap the file at 100 chars. Th
- Fixed bogus or absent update notifications on the desktop app due to incorrect deserialization of
a struct sent from the daemon.

### Fixed
#### Android
- Fix crash when leaving WireGuard Key screen while key is still verifying.


## [2020.4-beta3] - 2020-04-29
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre

private lateinit var timeAgoFormatter: TimeAgoFormatter

private var greenColor: Int = 0
private var redColor: Int = 0

private var tunnelStateListener: Int? = null
private var tunnelState: TunnelState = TunnelState.Disconnected()

Expand Down Expand Up @@ -96,7 +99,11 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre
override fun onAttach(context: Context) {
super.onAttach(context)

timeAgoFormatter = TimeAgoFormatter(context.resources)
val resources = context.resources

redColor = resources.getColor(R.color.red)
greenColor = resources.getColor(R.color.green)
timeAgoFormatter = TimeAgoFormatter(resources)
}

override fun onSafelyCreateView(
Expand Down Expand Up @@ -230,9 +237,9 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre

private fun updateOfflineStatus() {
if (reconnectionExpected) {
setStatusMessage(R.string.wireguard_key_reconnecting, R.color.green)
setStatusMessage(R.string.wireguard_key_reconnecting, greenColor)
} else {
setStatusMessage(R.string.wireguard_key_blocked_state_message, R.color.red)
setStatusMessage(R.string.wireguard_key_blocked_state_message, redColor)
}
}

Expand All @@ -241,7 +248,7 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre
val replacementFailure = keyStatus.replacementFailure

if (replacementFailure != null) {
setStatusMessage(failureMessage(replacementFailure), R.color.red)
setStatusMessage(failureMessage(replacementFailure), redColor)
} else {
updateKeyIsValid(verificationWasDone, keyStatus.verified)
}
Expand All @@ -252,11 +259,11 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre

private fun updateKeyIsValid(verificationWasDone: Boolean, verified: Boolean?) {
when (verified) {
true -> setStatusMessage(R.string.wireguard_key_valid, R.color.green)
false -> setStatusMessage(R.string.wireguard_key_invalid, R.color.red)
true -> setStatusMessage(R.string.wireguard_key_valid, greenColor)
false -> setStatusMessage(R.string.wireguard_key_invalid, redColor)
null -> {
if (verificationWasDone) {
setStatusMessage(R.string.wireguard_key_verification_failure, R.color.red)
setStatusMessage(R.string.wireguard_key_verification_failure, redColor)
} else {
statusMessage.visibility = View.GONE
}
Expand Down Expand Up @@ -296,7 +303,7 @@ class WireguardKeyFragment : ServiceDependentFragment(OnNoService.GoToLaunchScre

private fun setStatusMessage(message: Int, color: Int) {
statusMessage.setText(message)
statusMessage.setTextColor(resources.getColor(color))
statusMessage.setTextColor(color)
statusMessage.visibility = View.VISIBLE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.launch

class JobTracker {
private val jobs = HashMap<Long, Job>()
private val reaperJobs = HashMap<Long, Job>()
private val namedJobs = HashMap<String, Long>()

private var jobIdCounter = 0L
Expand All @@ -18,7 +19,9 @@ class JobTracker {

jobIdCounter += 1

jobs.put(jobId, GlobalScope.launch(Dispatchers.Default) {
jobs.put(jobId, job)

reaperJobs.put(jobId, GlobalScope.launch(Dispatchers.Default) {
job.join()

synchronized(jobs) {
Expand Down Expand Up @@ -69,6 +72,7 @@ class JobTracker {
fun cancelJob(jobId: Long) {
synchronized(jobs) {
jobs.remove(jobId)?.cancel()
reaperJobs.remove(jobId)?.cancel()
}
}

Expand All @@ -78,7 +82,13 @@ class JobTracker {
job.cancel()
}

for (job in reaperJobs.values) {
job.cancel()
}

jobs.clear()
reaperJobs.clear()
namedJobs.clear()
}
}
}

0 comments on commit bdae032

Please sign in to comment.