Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
The Tor Project
Applications
android-components
Commits
5597c3c0
Commit
5597c3c0
authored
Mar 25, 2020
by
Christian Sadilek
Browse files
Closes #6317: Prevent inserting duplicate record into content resolver
parent
d4d1449f
Changes
1
Hide whitespace changes
Inline
Side-by-side
components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt
View file @
5597c3c0
...
...
@@ -10,11 +10,13 @@ import android.app.DownloadManager.EXTRA_DOWNLOAD_ID
import
android.app.Service
import
android.content.ActivityNotFoundException
import
android.content.BroadcastReceiver
import
android.content.ContentUris
import
android.content.ContentValues
import
android.content.Context
import
android.content.Intent
import
android.content.Intent.ACTION_VIEW
import
android.content.IntentFilter
import
android.net.Uri
import
android.os.Build
import
android.os.Build.VERSION.SDK_INT
import
android.os.Environment
...
...
@@ -441,14 +443,39 @@ abstract class AbstractFetchDownloadService : Service() {
val
resolver
=
applicationContext
.
contentResolver
val
collection
=
MediaStore
.
Downloads
.
getContentUri
(
MediaStore
.
VOLUME_EXTERNAL_PRIMARY
)
val
item
=
resolver
.
insert
(
collection
,
values
)
val
pfd
=
resolver
.
openFileDescriptor
(
item
!!
,
"w"
)
ParcelFileDescriptor
.
AutoCloseOutputStream
(
pfd
).
use
(
block
)
// Query if we have a pending download with the same name. This can happen
// if a download was interrupted, failed or cancelled before the file was
// written to disk. Our logic above will have generated a unique file name
// based on existing files on the device, but we might already have a row
// for the download in the content resolver.
var
downloadUri
:
Uri
?
=
null
resolver
.
query
(
MediaStore
.
setIncludePending
(
collection
),
arrayOf
(
MediaStore
.
Downloads
.
_ID
),
"${MediaStore.Downloads.DISPLAY_NAME} = ?"
,
arrayOf
(
"${download.fileName}"
),
null
)
?.
use
{
if
(
it
.
count
>
0
)
{
val
idColumnIndex
=
it
.
getColumnIndex
(
MediaStore
.
Downloads
.
_ID
)
it
.
moveToFirst
()
downloadUri
=
ContentUris
.
withAppendedId
(
collection
,
it
.
getLong
(
idColumnIndex
))
}
}
if
(
downloadUri
==
null
)
{
downloadUri
=
resolver
.
insert
(
collection
,
values
)
}
downloadUri
?.
let
{
val
pfd
=
resolver
.
openFileDescriptor
(
it
,
"w"
)
ParcelFileDescriptor
.
AutoCloseOutputStream
(
pfd
).
use
(
block
)
values
.
clear
()
values
.
put
(
MediaStore
.
Downloads
.
IS_PENDING
,
0
)
resolver
.
update
(
item
,
values
,
null
,
null
)
values
.
clear
()
values
.
put
(
MediaStore
.
Downloads
.
IS_PENDING
,
0
)
resolver
.
update
(
it
,
values
,
null
,
null
)
}
?:
throw
IOException
(
"Failed to register download with content resolver"
)
}
@TargetApi
(
Build
.
VERSION_CODES
.
P
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment