-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'filter-select-location-relay-list-on-ownership-and-sele…
…cted-droid-466'
- Loading branch information
Showing
21 changed files
with
345 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
android/app/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/Provider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package net.mullvad.mullvadvpn.relaylist | ||
|
||
data class Provider(val name: String, val mullvadOwned: Boolean) |
3 changes: 3 additions & 0 deletions
3
android/app/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package net.mullvad.mullvadvpn.relaylist | ||
|
||
data class RelayList(val country: List<RelayCountry>, val selectedItem: RelayItem?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 30 additions & 113 deletions
143
android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/RelayListListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,52 @@ | ||
package net.mullvad.mullvadvpn.ui.serviceconnection | ||
|
||
import android.os.Messenger | ||
import net.mullvad.mullvadvpn.lib.common.util.toGeographicLocationConstraint | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.stateIn | ||
import net.mullvad.mullvadvpn.lib.ipc.Event | ||
import net.mullvad.mullvadvpn.lib.ipc.EventDispatcher | ||
import net.mullvad.mullvadvpn.lib.ipc.Request | ||
import net.mullvad.mullvadvpn.model.Constraint | ||
import net.mullvad.mullvadvpn.model.GeographicLocationConstraint | ||
import net.mullvad.mullvadvpn.model.PortRange | ||
import net.mullvad.mullvadvpn.model.RelayConstraints | ||
import net.mullvad.mullvadvpn.model.RelaySettings | ||
import net.mullvad.mullvadvpn.model.Ownership | ||
import net.mullvad.mullvadvpn.model.Providers | ||
import net.mullvad.mullvadvpn.model.RelayList | ||
import net.mullvad.mullvadvpn.model.WireguardConstraints | ||
import net.mullvad.mullvadvpn.relaylist.RelayCountry | ||
import net.mullvad.mullvadvpn.relaylist.RelayItem | ||
import net.mullvad.mullvadvpn.relaylist.findItemForLocation | ||
import net.mullvad.mullvadvpn.relaylist.toRelayCountries | ||
import net.mullvad.mullvadvpn.model.WireguardEndpointData | ||
|
||
class RelayListListener( | ||
private val connection: Messenger, | ||
eventDispatcher: EventDispatcher, | ||
private val settingsListener: SettingsListener | ||
private val messageHandler: MessageHandler, | ||
dispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) { | ||
private var relayCountries: List<RelayCountry>? = null | ||
private var relaySettings: RelaySettings? = null | ||
private var portRanges: List<PortRange> = emptyList() | ||
|
||
var selectedRelayItem: RelayItem? = null | ||
private set | ||
val relayListEvents: StateFlow<RelayList> = | ||
messageHandler | ||
.events<Event.NewRelayList>() | ||
.map { it.relayList ?: defaultRelayList() } | ||
// This is added so that we always have a relay list. Otherwise sometimes there would | ||
// not be a relay list since the fetching of a relay list would be done before the | ||
// event stream is available. | ||
.onStart { messageHandler.trySendRequest(Request.FetchRelayList) } | ||
.stateIn(CoroutineScope(dispatcher), SharingStarted.Eagerly, defaultRelayList()) | ||
|
||
fun updateSelectedRelayLocation(value: GeographicLocationConstraint) { | ||
connection.send(Request.SetRelayLocation(value).message) | ||
messageHandler.trySendRequest(Request.SetRelayLocation(value)) | ||
} | ||
|
||
fun updateSelectedWireguardConstraints(value: WireguardConstraints) { | ||
connection.send(Request.SetWireguardConstraints(value).message) | ||
} | ||
|
||
var onRelayCountriesChange: ((List<RelayCountry>, RelayItem?) -> Unit)? = null | ||
set(value) { | ||
field = value | ||
|
||
synchronized(this) { | ||
val relayCountries = this.relayCountries | ||
|
||
if (relayCountries != null) { | ||
value?.invoke(relayCountries, selectedRelayItem) | ||
} | ||
} | ||
} | ||
|
||
var onPortRangesChange: ((List<PortRange>) -> Unit)? = null | ||
set(value) { | ||
field = value | ||
|
||
synchronized(this) { value?.invoke(portRanges) } | ||
} | ||
|
||
init { | ||
eventDispatcher.registerHandler(Event.NewRelayList::class) { event -> | ||
event.relayList?.let { relayLocations -> | ||
relayListChanged(relayLocations.toRelayCountries()) | ||
portRangesChanged(relayLocations.wireguardEndpointData.portRanges) | ||
} | ||
} | ||
|
||
settingsListener.relaySettingsNotifier.subscribe(this) { newRelaySettings -> | ||
relaySettingsChanged(newRelaySettings) | ||
} | ||
} | ||
|
||
fun onDestroy() { | ||
settingsListener.relaySettingsNotifier.unsubscribe(this) | ||
onRelayCountriesChange = null | ||
} | ||
|
||
private fun relaySettingsChanged(newRelaySettings: RelaySettings?) { | ||
synchronized(this) { | ||
val relayCountries = this.relayCountries | ||
val portRanges = this.portRanges | ||
|
||
relaySettings = | ||
newRelaySettings | ||
?: RelaySettings.Normal( | ||
RelayConstraints( | ||
location = Constraint.Any(), | ||
ownership = Constraint.Any(), | ||
wireguardConstraints = WireguardConstraints(Constraint.Any()), | ||
providers = Constraint.Any() | ||
) | ||
) | ||
|
||
if (relayCountries != null) { | ||
relayListChanged(relayCountries) | ||
} | ||
portRangesChanged(portRanges) | ||
} | ||
messageHandler.trySendRequest(Request.SetWireguardConstraints(value)) | ||
} | ||
|
||
private fun relayListChanged(newRelayCountries: List<RelayCountry>) { | ||
synchronized(this) { | ||
relayCountries = newRelayCountries | ||
selectedRelayItem = findSelectedRelayItem() | ||
|
||
onRelayCountriesChange?.invoke(newRelayCountries, selectedRelayItem) | ||
} | ||
fun updateSelectedOwnershipFilter(value: Constraint<Ownership>) { | ||
messageHandler.trySendRequest(Request.SetOwnership(value)) | ||
} | ||
|
||
private fun portRangesChanged(newPortRanges: List<PortRange>) { | ||
synchronized(this) { | ||
portRanges = newPortRanges | ||
|
||
onPortRangesChange?.invoke(portRanges) | ||
} | ||
fun updateSelectedProvidersFilter(value: Constraint<Providers>) { | ||
messageHandler.trySendRequest(Request.SetProviders(value)) | ||
} | ||
|
||
private fun findSelectedRelayItem(): RelayItem? { | ||
val relaySettings = this.relaySettings | ||
|
||
when (relaySettings) { | ||
is RelaySettings.CustomTunnelEndpoint -> return null | ||
is RelaySettings.Normal -> { | ||
val location = relaySettings.relayConstraints.location | ||
return relayCountries?.findItemForLocation( | ||
location.toGeographicLocationConstraint() | ||
) | ||
} | ||
else -> { | ||
/* NOOP */ | ||
} | ||
} | ||
|
||
return null | ||
} | ||
private fun defaultRelayList() = RelayList(ArrayList(), WireguardEndpointData(ArrayList())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/PortRangeUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.mullvad.mullvadvpn.usecase | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.map | ||
import net.mullvad.mullvadvpn.model.PortRange | ||
import net.mullvad.mullvadvpn.ui.serviceconnection.RelayListListener | ||
|
||
class PortRangeUseCase(private val relayListListener: RelayListListener) { | ||
fun portRanges(): Flow<List<PortRange>> = | ||
relayListListener.relayListEvents | ||
.map { it?.wireguardEndpointData?.portRanges ?: emptyList() } | ||
.distinctUntilChanged() | ||
} |
Oops, something went wrong.