Skip to content

Commit

Permalink
Extract constant strings to resources for later localization.
Browse files Browse the repository at this point in the history
Signed-off-by: Fredric Silberberg <fred@silberberg.xyz>
  • Loading branch information
333fred committed Jun 11, 2024
1 parent b98486b commit c9443c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
21 changes: 12 additions & 9 deletions android/src/main/java/com/tailscale/ipn/UseExitNodeWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,35 @@ class UseExitNodeWorker(
workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
val app = UninitializedApp.get()
suspend fun runAndGetResult(): String? {
val exitNodeName = inputData.getString(EXIT_NODE_NAME)

val exitNodeId = if (exitNodeName.isNullOrEmpty()) {
null
} else {
if (!UninitializedApp.get().isAbleToStartVPN()) {
return "VPN is not ready to start"
if (!app.isAbleToStartVPN()) {
return app.getString(R.string.vpn_is_not_ready_to_start)
}

val peers =
(Notifier.netmap.value
?: run { return@runAndGetResult "Tailscale is not setup" })
.Peers ?: run { return@runAndGetResult "No peers found" }
?: run { return@runAndGetResult app.getString(R.string.tailscale_is_not_setup) })
.Peers ?: run { return@runAndGetResult app.getString(R.string.no_peers_found) }

val filteredPeers = peers.filter {
it.displayName == exitNodeName
}.toList()

if (filteredPeers.isEmpty()) {
return "No peers with name $exitNodeName found"
return app.getString(R.string.no_peers_with_name_found, exitNodeName)
} else if (filteredPeers.size > 1) {
return "Multiple peers with name $exitNodeName found"
return app.getString(R.string.multiple_peers_with_name_found, exitNodeName)
} else if (!filteredPeers[0].isExitNode) {
return "Peer with name $exitNodeName is not an exit node"
return app.getString(
R.string.peer_with_name_is_not_an_exit_node,
exitNodeName
)
}

filteredPeers[0].StableID
Expand Down Expand Up @@ -76,7 +80,6 @@ class UseExitNodeWorker(
val result = runAndGetResult()

return if (result != null) {
val app = UninitializedApp.get()
val intent =
Intent(app, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
Expand All @@ -87,7 +90,7 @@ class UseExitNodeWorker(

val notification = NotificationCompat.Builder(app, STATUS_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Use Exit Node Intent Failed")
.setContentTitle(app.getString(R.string.use_exit_node_intent_failed))
.setContentText(result)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
Expand Down
9 changes: 9 additions & 0 deletions android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@
<string name="welcome2">All connections are device-to-device, so we never see your data. We collect and use your email address and name, as well as your device name, OS version, and IP address in order to help you to connect your devices and manage your settings. We log when you are connected to your network.</string>
<string name="scan_to_connect_to_your_tailnet">Scan this QR code to log in to your tailnet</string>

<!-- Strings for intent handling -->
<string name="vpn_is_not_ready_to_start">VPN is not ready to start</string>
<string name="tailscale_is_not_setup">Tailscale is not setup</string>
<string name="no_peers_found">No peers found</string>
<string name="no_peers_with_name_found">No peers with name %1$s found</string>
<string name="multiple_peers_with_name_found">Multiple peers with name %1$s found</string>
<string name="peer_with_name_is_not_an_exit_node">Peer with name %1$s is not an exit node</string>
<string name="use_exit_node_intent_failed">Use Exit Node Intent Failed</string>

<!-- Strings for the IPNReceiver -->
<string name="title_connection_failed">Tailscale Connection Failed</string>
<string name="body_open_tailscale">Tap here to open Tailscale.</string>
Expand Down

0 comments on commit c9443c7

Please sign in to comment.