Skip to content

Commit

Permalink
Merge pull request #387 from mysteriumnetwork/bugfix/Crashes_and_fixes
Browse files Browse the repository at this point in the history
Bugfix/crashes and fixes
  • Loading branch information
ArtemHryhorovGeniusee authored Jun 14, 2021
2 parents 43db9a3 + 7469633 commit ad8ab47
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package updated.mysterium.vpn.core

import android.util.Log
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Semaphore
import mysterium.MobileNode

Expand All @@ -30,15 +27,14 @@ class DeferredNode {
if (!lock.tryAcquire()) {
Log.i(TAG, "Node is already started or starting, skipping")
} else {
val startJob = CoroutineScope(Dispatchers.Main).launch {
try {
val node = service.startNode()
deferredNode.complete(node)
done?.invoke(null)
} catch (err: Exception) {
Log.e(TAG, "Failed to start node", err)
done?.invoke(err)
}
val handler = CoroutineExceptionHandler { _, exception ->
Log.e(TAG, exception.localizedMessage ?: exception.toString())
done?.invoke(exception as Exception)
}
val startJob = CoroutineScope(Dispatchers.Main + handler).launch {
val node = service.startNode()
deferredNode.complete(node)
done?.invoke(null)
}
startJob.invokeOnCompletion {
lock.release()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.os.Bundle
import android.os.IBinder
import android.util.Log
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mysterium.MobileNode
Expand Down Expand Up @@ -84,20 +85,17 @@ class MysteriumAndroidCoreService : VpnService(), KoinComponent {
}

private fun startMobileNode(filesPath: String): MobileNode {
if (mobileNode != null) {
return mobileNode!!
mobileNode?.let {
return it
}

val wireguardBridge = WireguardAndroidTunnelSetup(this)
val options = Mysterium.defaultNodeOptions()
// Testing payment, should be deleted after testing
mobileNode = Mysterium.newNode(filesPath, options)
mobileNode?.overrideWireguardConnection(wireguardBridge)
mobileNode = Mysterium.newNode(filesPath, Mysterium.defaultNodeOptions())
mobileNode?.overrideWireguardConnection(WireguardAndroidTunnelSetup(this))

Log.i(TAG, "Node started")
initBalanceListener()
initConnectionListener()
return mobileNode!!
return mobileNode ?: MobileNode()
}

private fun stopMobileNode() {
Expand All @@ -118,7 +116,7 @@ class MysteriumAndroidCoreService : VpnService(), KoinComponent {
}

private fun initBalanceListener() {
GlobalScope.launch {
GlobalScope.launch(Dispatchers.IO) {
balanceUseCase.initBalanceListener {
if (it < BALANCE_LIMIT && it > 0.0 && !balanceUseCase.isBalancePushShown()) {
makeBalancePushNotification()
Expand All @@ -133,7 +131,7 @@ class MysteriumAndroidCoreService : VpnService(), KoinComponent {
}

private fun initConnectionListener() {
GlobalScope.launch {
GlobalScope.launch(Dispatchers.IO) {
connectionUseCase.connectionStatusCallback {
val state = ConnectionState.from(it)
val previousState = currentState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ class NodeRepository(var deferredNode: DeferredNode) {
deferredNode.await().listProposalFilterPresets()
}

suspend fun getProposalsByFilterId(filterId: Long) = withContext(Dispatchers.IO) {
val bytesProposals = deferredNode.await().getProposalsByPreset(filterId)
suspend fun getProposalsByFilterId(getProposalRequest: GetProposalsRequest) = withContext(Dispatchers.IO) {
val bytesProposals = deferredNode.await().getProposals(getProposalRequest)
val proposalsResponse = parseProposals(bytesProposals)
if (proposalsResponse?.proposals == null) {
listOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package updated.mysterium.vpn.network.usecase

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import mysterium.GetProposalsRequest
import network.mysterium.vpn.R
import updated.mysterium.vpn.core.NodeRepository
import updated.mysterium.vpn.database.entity.NodeEntity
Expand Down Expand Up @@ -55,8 +56,13 @@ class FilterUseCase(

suspend fun getProposalsByFilterId(filterId: Int): List<NodeEntity>? {
return if (filterId != ALL_NODES_FILTER_ID) {
nodeRepository.getProposalsByFilterId(filterId.toLong())
.map { NodeEntity(it) }
val proposalRequest = GetProposalsRequest().apply {
presetID = filterId.toLong()
refresh = true
}
nodeRepository.getProposalsByFilterId(proposalRequest).map {
NodeEntity(it)
}
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package updated.mysterium.vpn.notification
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -12,6 +14,10 @@ import updated.mysterium.vpn.ui.connection.ConnectionViewModel

class AppBroadcastReceiver : BroadcastReceiver(), KoinComponent {

private companion object {
const val TAG = "AppBroadcastReceiver"
}

private val viewModel: ConnectionViewModel by inject()
private val appNotificationManager: AppNotificationManager by inject()

Expand All @@ -22,7 +28,10 @@ class AppBroadcastReceiver : BroadcastReceiver(), KoinComponent {
}

private fun handleDisconnect() {
CoroutineScope(Dispatchers.Main).launch {
val handler = CoroutineExceptionHandler { _, exception ->
Log.e(TAG, exception.localizedMessage ?: exception.toString())
}
CoroutineScope(Dispatchers.Main + handler).launch {
appNotificationManager.hideStatisticsNotification()
viewModel.disconnectFromNotification()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class SplashActivity : BaseActivity() {
establishConnectionListeners()
navigateForward()
})
viewModel.preloadFinished.observe(this, {
binding.onceAnimationView.playAnimation()
viewModel.initRepository()
})
}

private fun applyDarkMode() {
Expand Down Expand Up @@ -240,12 +244,7 @@ class SplashActivity : BaseActivity() {
private fun init() {
val deferredMysteriumCoreService = App.getInstance(this).deferredMysteriumCoreService
balanceViewModel.initDeferredNode(deferredMysteriumCoreService)
viewModel.startLoading(deferredMysteriumCoreService).observe(this) { result ->
result.onSuccess {
binding.onceAnimationView.playAnimation()
viewModel.initRepository()
}
}
viewModel.startLoading(deferredMysteriumCoreService)
}

private fun openPlayMarket() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package updated.mysterium.vpn.ui.splash

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import updated.mysterium.vpn.common.livedata.SingleLiveEvent
import updated.mysterium.vpn.core.DeferredNode
import updated.mysterium.vpn.core.MysteriumCoreService
import updated.mysterium.vpn.common.extensions.liveDataResult
import updated.mysterium.vpn.network.provider.usecase.UseCaseProvider

class SplashViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {

private companion object {
const val TAG = "SplashViewModel"
}

val navigateForward: LiveData<Unit>
get() = _navigateForward

val preloadFinished: LiveData<Unit>
get() = _preloadFinished

private val _preloadFinished = SingleLiveEvent<Unit>()
private val _navigateForward = MutableLiveData<Unit>()
private val balanceUseCase = useCaseProvider.balance()
private val connectionUseCase = useCaseProvider.connection()
Expand All @@ -29,18 +40,23 @@ class SplashViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {

fun startLoading(
deferredMysteriumCoreService: CompletableDeferred<MysteriumCoreService>
) = liveDataResult {
val service = deferredMysteriumCoreService.await()
if (service.getDeferredNode() != null) {
service.getDeferredNode()?.let {
deferredNode = it
}
} else {
if (!deferredNode.startedOrStarting()) {
deferredNode.start(service)
) {
val handler = CoroutineExceptionHandler { _, exception ->
Log.e(TAG, exception.localizedMessage ?: exception.toString())
}
viewModelScope.launch(Dispatchers.IO + handler) {
val service = deferredMysteriumCoreService.await()
if (service.getDeferredNode() != null) {
service.getDeferredNode()?.let {
deferredNode = it
}
} else {
if (!deferredNode.startedOrStarting()) {
deferredNode.start(service)
}
}
_preloadFinished.postValue(Unit)
}
deferredNode
}

fun isUserAlreadyLogin() = loginUseCase.isAlreadyLogin()
Expand All @@ -62,7 +78,10 @@ class SplashViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {
}

fun initRepository() {
viewModelScope.launch {
val handler = CoroutineExceptionHandler { _, exception ->
Log.e(TAG, exception.localizedMessage ?: exception.toString())
}
viewModelScope.launch(Dispatchers.IO + handler) {
balanceUseCase.initDeferredNode(deferredNode)
connectionUseCase.initDeferredNode(deferredNode)
if (isAnimationLoaded) {
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
android:text="@string/settings_description"
android:textAlignment="center"
android:textColor="@color/manual_connect_value_white"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dnsTitle" />
Expand Down Expand Up @@ -160,6 +161,7 @@
android:text="@string/settings_description"
android:textAlignment="center"
android:textColor="@color/manual_connect_value_white"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/residentTitle" />
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<string name="filter_price_high">Any</string>
<string name="filter_price_medium">Medium</string>
<string name="filter_price_low">Low</string>
<string name="filter_quality_high">Hight</string>
<string name="filter_quality_high">High</string>
<string name="filter_quality_medium">Medium +</string>
<string name="filter_quality_low">Any</string>

Expand Down

0 comments on commit ad8ab47

Please sign in to comment.