summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorDavid Göransson <david.goransson90@gmail.com>2023-11-22 17:16:17 +0100
committerJonatan Rhodin <jonatan.rhodin@mullvad.net>2023-11-27 16:16:21 +0100
commit9fd6dbd468e5be0339b21d6aa52f6d95f7697814 (patch)
treead1e9e26619322971e0d71be6442f4b9192e7483 /android
parenta062df8ccda8ae473fc4bbf0f85840f8b872643c (diff)
downloadmullvadvpn-9fd6dbd468e5be0339b21d6aa52f6d95f7697814.tar.xz
mullvadvpn-9fd6dbd468e5be0339b21d6aa52f6d95f7697814.zip
Add file sharer
Diffstat (limited to 'android')
-rw-r--r--android/app/src/main/AndroidManifest.xml10
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ViewLogsScreen.kt19
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/provider/MullvadFileProvider.kt59
-rw-r--r--android/app/src/main/res/xml/provider_paths.xml8
4 files changed, 86 insertions, 10 deletions
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 10b40b9347..1df73d76e6 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -90,5 +90,15 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
+
+ <provider
+ android:name="net.mullvad.mullvadvpn.provider.MullvadFileProvider"
+ android:authorities="net.mullvad.mullvadvpn.FileProvider"
+ android:exported="false"
+ android:grantUriPermissions="true">
+ <meta-data
+ android:name="android.support.FILE_PROVIDER_PATHS"
+ android:resource="@xml/provider_paths" />
+ </provider>
</application>
</manifest>
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ViewLogsScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ViewLogsScreen.kt
index cbe1f6d0b3..06fa5442ec 100644
--- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ViewLogsScreen.kt
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ViewLogsScreen.kt
@@ -2,6 +2,8 @@ package net.mullvad.mullvadvpn.compose.screen
import android.content.Context
import android.content.Intent
+import android.net.Uri
+import android.util.Log
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
@@ -17,11 +19,13 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
+import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorMedium
import net.mullvad.mullvadvpn.compose.component.MullvadMediumTopBar
@@ -30,6 +34,7 @@ import net.mullvad.mullvadvpn.compose.component.drawVerticalScrollbar
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.AlphaScrollbar
+import net.mullvad.mullvadvpn.provider.getLogsShareIntent
import net.mullvad.mullvadvpn.viewmodel.ViewLogsUiState
@Preview
@@ -52,13 +57,14 @@ fun ViewLogsScreen(
) {
val context = LocalContext.current
+ val scope = rememberCoroutineScope()
Scaffold(
topBar = {
MullvadMediumTopBar(
title = stringResource(id = R.string.view_logs),
navigationIcon = { NavigateBackIconButton(onBackClick) },
actions = {
- IconButton(onClick = { shareText(context, uiState.text()) }) {
+ IconButton(onClick = { scope.launch { shareText(context, uiState.text()) } }) {
Icon(imageVector = Icons.Default.Share, contentDescription = null)
}
}
@@ -101,14 +107,7 @@ fun ViewLogsScreen(
}
}
-private fun shareText(context: Context, text: String) {
- val sendIntent: Intent =
- Intent().apply {
- action = Intent.ACTION_SEND
- putExtra(Intent.EXTRA_TEXT, text)
- type = "text/plain"
- }
- val shareIntent = Intent.createChooser(sendIntent, null)
-
+private fun shareText(context: Context, logContent: String) {
+ val shareIntent = context.getLogsShareIntent("Share logs", logContent)
context.startActivity(shareIntent)
}
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/provider/MullvadFileProvider.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/provider/MullvadFileProvider.kt
new file mode 100644
index 0000000000..c9e7bbe214
--- /dev/null
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/provider/MullvadFileProvider.kt
@@ -0,0 +1,59 @@
+package net.mullvad.mullvadvpn.provider
+
+import android.content.Context
+import android.content.Intent
+import android.net.Uri
+import androidx.core.content.FileProvider
+import java.io.File
+import net.mullvad.mullvadvpn.R
+import org.joda.time.DateTime
+import org.joda.time.format.ISODateTimeFormat
+
+// https://developer.android.com/reference/androidx/core/content/FileProvider
+// From link: It is possible to use FileProvider directly instead of extending it. However, this is
+// not reliable and will causes crashes on some devices.
+class MullvadFileProvider : FileProvider(R.xml.provider_paths) {
+ companion object {
+ fun uriForFile(context: Context, file: File): Uri {
+ return getUriForFile(context, "net.mullvad.mullvadvpn.FileProvider", file)
+ }
+ }
+}
+
+enum class ProviderCacheDirectory(val directoryName: String) {
+ LOGS("logs")
+}
+
+fun Context.getLogsShareIntent(shareTitle: String, logContent: String): Intent {
+ val fileName = createShareLogFileName()
+ val cacheFile = createCacheFile(ProviderCacheDirectory.LOGS, fileName)
+ cacheFile.writeText(logContent)
+ val logsUri = MullvadFileProvider.uriForFile(this, cacheFile)
+
+ val sendIntent: Intent =
+ Intent().apply {
+ action = Intent.ACTION_SEND
+ type = "text/plain"
+ putExtra(Intent.EXTRA_STREAM, logsUri)
+ addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ }
+ return Intent.createChooser(sendIntent, null)
+}
+
+fun Context.createCacheFile(
+ directory: ProviderCacheDirectory,
+ fileName: String,
+): File {
+ // Path to log file
+ val logsPath = File(cacheDir, directory.directoryName)
+
+ // Ensure path is created
+ logsPath.mkdirs()
+
+ return File(logsPath, fileName)
+}
+
+fun createShareLogFileName(): String {
+ val datetime = ISODateTimeFormat.basicOrdinalDateTimeNoMillis().print(DateTime.now())
+ return "mullvad_log-${datetime}.txt"
+}
diff --git a/android/app/src/main/res/xml/provider_paths.xml b/android/app/src/main/res/xml/provider_paths.xml
new file mode 100644
index 0000000000..921d92f8dd
--- /dev/null
+++ b/android/app/src/main/res/xml/provider_paths.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<paths>
+ <cache-path
+ name="logs"
+ path="logs/" />
+</paths>
+
+