Skip to content

Commit

Permalink
Only accept content URIs in share activity
Browse files Browse the repository at this point in the history
  • Loading branch information
abaker committed Oct 15, 2022
1 parent 07bf02f commit 23bf69d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 12.7.1 (2022-10-04)

* Bug fix

### 12.7 (2022-06-18)

* Android 13 themed icon - Thanks @hanthor!
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ android {
defaultConfig {
testApplicationId = "org.tasks.test"
applicationId = "org.tasks"
versionCode = 120700
versionName = "12.7"
versionCode = 120701
versionName = "12.7.1"
targetSdk = Versions.targetSdk
minSdk = Versions.minSdk
testInstrumentationRunner = "org.tasks.TestRunner"
Expand Down
42 changes: 18 additions & 24 deletions app/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.todoroo.astrid.activity

import android.content.ContentResolver
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.google.common.io.Files
import com.todoroo.astrid.data.Task
import com.todoroo.astrid.service.TaskCreator
import com.todoroo.astrid.utility.Constants
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.tasks.Strings.isNullOrEmpty
import org.tasks.analytics.Firebase
import org.tasks.data.TaskAttachment
import org.tasks.files.FileHelper
Expand All @@ -18,7 +18,6 @@ import org.tasks.intents.TaskIntents
import org.tasks.preferences.Preferences
import timber.log.Timber
import javax.inject.Inject
import kotlin.math.min

/**
* @author joshuagross
Expand Down Expand Up @@ -88,29 +87,24 @@ class ShareLinkActivity : InjectingAppCompatActivity() {
startActivity(intent)
}

private fun copyAttachment(intent: Intent): ArrayList<Uri> {
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM) ?: return ArrayList()
var filename = FileHelper.getFilename(this, uri)
if (isNullOrEmpty(filename)) {
filename = intent.getStringExtra(Intent.EXTRA_SUBJECT)
?.takeIf { it.isNotBlank() }
?.let { it.substring(0, min(it.length, FileHelper.MAX_FILENAME_LENGTH)) }
?: uri.lastPathSegment
}
val basename = Files.getNameWithoutExtension(filename!!)
return arrayListOf(FileHelper.copyToUri(this, preferences.attachmentsDirectory!!, uri, basename))
}
private fun copyAttachment(intent: Intent): ArrayList<Uri> =
intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
?.let { copyAttachments(listOf(it)) }
?: arrayListOf()

private fun copyMultipleAttachments(intent: Intent): ArrayList<Uri> =
intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
?.let { copyAttachments(it) }
?: arrayListOf()

private fun copyMultipleAttachments(intent: Intent): ArrayList<Uri> {
val result = ArrayList<Uri>()
val uris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
if (uris != null) {
for (uri in uris) {
result.add(FileHelper.copyToUri(this, preferences.attachmentsDirectory!!, uri))
private fun copyAttachments(uris: List<Uri>) =
uris
.filter {
it.scheme == ContentResolver.SCHEME_CONTENT
&& it.authority != Constants.FILE_PROVIDER_AUTHORITY
}
}
return result
}
.map { FileHelper.copyToUri(this, preferences.attachmentsDirectory!!, it) }
.let { ArrayList(it) }

private fun hasAttachments(intent: Intent) =
intent.type?.let { type -> ATTACHMENT_TYPES.any { type.startsWith(it) } } ?: false
Expand Down
21 changes: 10 additions & 11 deletions app/src/main/java/org/tasks/files/FileHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import java.io.IOException
import java.util.*

object FileHelper {
const val MAX_FILENAME_LENGTH = 40
fun newFilePickerIntent(activity: Activity?, initial: Uri?, vararg mimeTypes: String?): Intent =
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
putExtra("android.content.extra.SHOW_ADVANCED", true)
Expand Down Expand Up @@ -178,20 +177,20 @@ object FileHelper {

fun copyToUri(context: Context, destination: Uri, input: Uri): Uri {
val filename = getFilename(context, input)
return copyToUri(context, destination, input, Files.getNameWithoutExtension(filename!!))
}

fun copyToUri(context: Context, destination: Uri, input: Uri, basename: String): Uri = try {
val output = newFile(
val basename = Files.getNameWithoutExtension(filename!!)
try {
val output = newFile(
context,
destination,
getMimeType(context, input),
basename,
getExtension(context, input))
copyStream(context, input, output)
output
} catch (e: IOException) {
throw IllegalStateException(e)
getExtension(context, input)
)
copyStream(context, input, output)
return output
} catch (e: IOException) {
throw IllegalStateException(e)
}
}

fun copyStream(context: Context, input: Uri?, output: Uri?) {
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/120701.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Bug fix

0 comments on commit 23bf69d

Please sign in to comment.