-
Notifications
You must be signed in to change notification settings - Fork 658
android: use SAF for storing Taildropped files #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) Tailscale Inc & AUTHORS | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| package com.tailscale.ipn.ui.util | ||
|
|
||
| import com.tailscale.ipn.util.TSLog | ||
| import java.io.OutputStream | ||
|
|
||
| // This class adapts a Java OutputStream to the libtailscale.OutputStream interface. | ||
| class OutputStreamAdapter(private val outputStream: OutputStream) : libtailscale.OutputStream { | ||
| // writes data to the outputStream in its entirety. Returns -1 on error. | ||
| override fun write(data: ByteArray): Long { | ||
|
kari-ts marked this conversation as resolved.
|
||
| return try { | ||
| outputStream.write(data) | ||
| outputStream.flush() | ||
| data.size.toLong() | ||
| } catch (e: Exception) { | ||
|
Comment on lines
+12
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the OutputStreamAdapter.write method, the return value is always data.size.toLong() regardless of how many bytes were actually written. Java's OutputStream.write might write fewer bytes than requested. Consider capturing the actual bytes written or ensuring the entire buffer is written (possibly with multiple calls). |
||
| TSLog.d("OutputStreamAdapter", "write exception: $e") | ||
| -1L | ||
| } | ||
|
barnstar marked this conversation as resolved.
|
||
| } | ||
|
|
||
| override fun close() { | ||
| outputStream.close() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,15 @@ | |
| package com.tailscale.ipn.ui.viewModel | ||
|
|
||
| import android.content.Intent | ||
| import android.net.Uri | ||
| import android.net.VpnService | ||
| import androidx.activity.result.ActivityResultLauncher | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.platform.ClipboardManager | ||
| import androidx.compose.ui.text.AnnotatedString | ||
| import androidx.documentfile.provider.DocumentFile | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.viewModelScope | ||
|
|
@@ -25,6 +27,7 @@ import com.tailscale.ipn.ui.util.PeerCategorizer | |
| import com.tailscale.ipn.ui.util.PeerSet | ||
| import com.tailscale.ipn.ui.util.TimeUtil | ||
| import com.tailscale.ipn.ui.util.set | ||
| import com.tailscale.ipn.util.TSLog | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.FlowPreview | ||
| import kotlinx.coroutines.Job | ||
|
|
@@ -63,6 +66,9 @@ class MainViewModel(private val vpnViewModel: VpnViewModel) : IpnViewModel() { | |
| private val _requestVpnPermission = MutableStateFlow(false) | ||
| val requestVpnPermission: StateFlow<Boolean> = _requestVpnPermission | ||
|
|
||
| // Select Taildrop directory | ||
| private var directoryPickerLauncher: ActivityResultLauncher<Uri?>? = null | ||
|
|
||
| // The list of peers | ||
| private val _peers = MutableStateFlow<List<PeerSet>>(emptyList()) | ||
| val peers: StateFlow<List<PeerSet>> = _peers | ||
|
|
@@ -204,13 +210,34 @@ class MainViewModel(private val vpnViewModel: VpnViewModel) : IpnViewModel() { | |
| _requestVpnPermission.value = false // reset | ||
| } | ||
|
|
||
| fun showDirectoryPickerLauncher() { | ||
| val app = App.get() | ||
| val storedUri = app.getStoredDirectoryUri() | ||
| if (storedUri == null) { | ||
| // No stored URI, so launch the directory picker. | ||
| directoryPickerLauncher?.launch(null) | ||
| return | ||
|
Comment on lines
+216
to
+219
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After launching the directory picker, the function returns immediately. However, there's no indication that the function will be called again after the picker completes. Consider adding a callback mechanism to ensure the flow continues appropriately after directory selection. |
||
| } | ||
|
|
||
| val documentFile = DocumentFile.fromTreeUri(app, storedUri) | ||
| if (documentFile == null || !documentFile.exists() || !documentFile.canWrite()) { | ||
| TSLog.d( | ||
| "MainViewModel", | ||
| "Stored directory URI is invalid or inaccessible; launching directory picker.") | ||
| directoryPickerLauncher?.launch(null) | ||
| } else { | ||
| TSLog.d("MainViewModel", "Using stored directory URI: $storedUri") | ||
| } | ||
| } | ||
|
|
||
| fun toggleVpn(desiredState: Boolean) { | ||
| if (isToggleInProgress.value) { | ||
| // Prevent toggling while a previous toggle is in progress | ||
| return | ||
| } | ||
|
|
||
| viewModelScope.launch { | ||
| showDirectoryPickerLauncher() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The showDirectoryPickerLauncher() call is made inside a viewModelScope.launch block during toggleVpn. Was this intentional? It could cause the directory picker to appear unexpectedly during VPN toggling, which might confuse users.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kind of agree with Skynet here. An actionable error message might be a less disruptive. Perhaps modelled after health warnings. Along with knowing where Taildrop files go (or don't go) and being able to change that in Settings after the fact. ...but this will do for now.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100% |
||
| isToggleInProgress.value = true | ||
| try { | ||
| val currentState = Notifier.state.value | ||
|
|
@@ -250,6 +277,10 @@ class MainViewModel(private val vpnViewModel: VpnViewModel) : IpnViewModel() { | |
| // No intent means we're already authorized | ||
| vpnPermissionLauncher = launcher | ||
| } | ||
|
|
||
| fun setDirectoryPickerLauncher(launcher: ActivityResultLauncher<Uri?>) { | ||
| directoryPickerLauncher = launcher | ||
| } | ||
| } | ||
|
|
||
| private fun userStringRes(currentState: State?, previousState: State?, vpnActive: Boolean): Int { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are incoming Taildrop files relevant on Android TV? They are not on AppleTV. If the user can actually pick a directory and use the files - all good - otherwise we should skip the dialog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call. gating it with an AndroidTV check