Skip to content

Commit

Permalink
Make sure the activity status in UI is updated properly
Browse files Browse the repository at this point in the history
Make sure the local broadcast receiver in the activity is pased and
resumed properly, and additionally, read from the vpn service until a
broadcast is sent from the service.
  • Loading branch information
dbrodie committed Jun 1, 2016
1 parent d04483d commit ad8e602
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/app/adbuster/AdVpnService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const val VPN_UPDATE_STATUS_EXTRA = "VPN_STATUS"
class AdVpnService : VpnService(), Handler.Callback, Runnable {
companion object {
private val TAG = "VpnService"
// TODO: Temporary Hack til refactor is done
var vpnStatusTextId: Int = R.string.notification_stopped
}

// TODO: There must be a better way in kotlin to do this
Expand Down Expand Up @@ -99,6 +101,7 @@ class AdVpnService : VpnService(), Handler.Callback, Runnable {

startForeground(10, notification)

vpnStatusTextId = text_id
var intent = Intent(VPN_UPDATE_STATUS_INTENT)
intent.putExtra(VPN_UPDATE_STATUS_EXTRA, text_id)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/app/adbuster/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ class MainActivity : Activity() {
mVpnServiceBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val str_id = intent.getIntExtra(VPN_UPDATE_STATUS_EXTRA, R.string.notification_stopped)
text_status.text = getString(str_id)
updateStatus(str_id)
}
}

LocalBroadcastManager.getInstance(this)
.registerReceiver(mVpnServiceBroadcastReceiver, IntentFilter(VPN_UPDATE_STATUS_INTENT))
}

public override fun onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mVpnServiceBroadcastReceiver)
private fun updateStatus(textId: Int) {
text_status.text = getString(textId)
}

override fun onActivityResult(request: Int, result: Int, data: Intent?) {
Expand All @@ -68,8 +65,17 @@ class MainActivity : Activity() {
}
}

override fun onPause() {
super.onPause()
LocalBroadcastManager.getInstance(this).unregisterReceiver(mVpnServiceBroadcastReceiver)
}

override fun onResume() {
super.onResume()
CrashManager.register(this)

updateStatus(AdVpnService.vpnStatusTextId)
LocalBroadcastManager.getInstance(this)
.registerReceiver(mVpnServiceBroadcastReceiver, IntentFilter(VPN_UPDATE_STATUS_INTENT))
}
}

0 comments on commit ad8e602

Please sign in to comment.