Skip to content
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

Allow sharing files via Android's share sheet #100

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>

<service
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/onionshare/android/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.onionshare.android.ui

import android.Manifest.permission.POST_NOTIFICATIONS
import android.content.Intent
import android.content.Intent.EXTRA_STREAM
import android.net.Uri
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
Expand All @@ -25,6 +29,33 @@ class MainActivity : ComponentActivity() {
setContent {
MainUi(viewModel)
}
onNewIntent(intent)
}

override fun onNewIntent(intent: Intent?) {
when (intent?.action) {
Intent.ACTION_SEND -> {
handleSend(intent.getParcelableExtra<Parcelable>(EXTRA_STREAM) as? Uri)
}

Intent.ACTION_SEND_MULTIPLE -> {
handleSendMultiple(intent.getParcelableArrayListExtra(EXTRA_STREAM))
}

else -> super.onNewIntent(intent)
}
}

private fun handleSend(uri: Uri?) {
if (uri == null) return
viewModel.onUrisReceived(listOf(uri), false)
}

private fun handleSendMultiple(parcelables: ArrayList<Parcelable>?) {
if (parcelables == null) return
val uris = parcelables.mapNotNull { it as? Uri }
if (uris.isEmpty()) return
viewModel.onUrisReceived(uris, false)
}

override fun onStart() {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/org/onionshare/android/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package org.onionshare.android.ui

import android.app.Application
import android.net.Uri
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.briarproject.android.dontkillmelib.DozeUtils.needsDozeWhitelisting
import org.onionshare.android.R
import org.onionshare.android.ShareManager
import org.onionshare.android.files.FileManager
import org.onionshare.android.files.FilesState
Expand All @@ -32,8 +35,10 @@ class MainViewModel @Inject constructor(
fun onUrisReceived(uris: List<Uri>, takePermission: Boolean) {
require(uris.isNotEmpty()) { "Call this only for non-empty list of Uris" }

viewModelScope.launch {
if (shareState.value.allowsModifyingFiles) viewModelScope.launch {
fileManager.addFiles(uris, takePermission)
} else {
Toast.makeText(getApplication(), R.string.share_error_not_allowed, LENGTH_LONG).show()
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<string name="share_error_snackbar_text">Could not start OnionShare</string>
<string name="share_error_file_snackbar_text">Could not open %s. Removed.</string>
<string name="share_error_snackbar_action">Retry</string>
<string name="share_error_not_allowed">Sharing new files is only possible when not already sharing.</string>

<string name="about_title">About</string>
<string name="about_app_version">App Version %s</string>
Expand Down
Loading