Skip to content

Commit

Permalink
Do not remove downloads on main thread
Browse files Browse the repository at this point in the history
Removing a download involves on disk reading/writing operations.
Those are blocking operations that should not happen in the main
thread. Use the recently added disk executor to carry on the task.

This fixes this report:
D  StrictMode policy violation; ~duration=102 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1659)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:131)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7795)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:751)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:742)
at android.app.ContextImpl.getFilesDir(ContextImpl.java:787)
at com.android.providers.downloads.Helpers.isFilenameValid(Helpers.java:665)
at com.android.providers.downloads.Helpers.isFilenameValid(Helpers.java:527)
at com.android.providers.downloads.DownloadProvider.delete(DownloadProvider.java:1629)
at android.content.ContentProvider.delete(ContentProvider.java:1785)
at android.content.ContentProvider$Transport.delete(ContentProvider.java:430)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:215)
at android.os.Binder.execTransactInternal(Binder.java:1179)
at android.os.Binder.execTransact(Binder.java:1143)
at android.os.StrictMode.readAndHandleBinderCallViolations(StrictMode.java:2497)
at android.os.Parcel.readExceptionCode(Parcel.java:2371)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:139)
at android.content.ContentProviderProxy.delete(ContentProviderNative.java:629)
at android.content.ContentResolver.delete(ContentResolver.java:2333)
at android.content.ContentResolver.delete(ContentResolver.java:2299)
at android.app.DownloadManager.markRowDeleted(DownloadManager.java:1138)
at android.app.DownloadManager.remove(DownloadManager.java:1150)
at com.igalia.wolvic.downloads.DownloadsManager.removeDownload(DownloadsManager.java:250)
at com.igalia.wolvic.utils.EnvironmentsManager$1.onUnzipFinish(EnvironmentsManager.java:193)
at com.igalia.wolvic.utils.zip.UnzipResultReceiver.lambda$onReceiveResult$0(UnzipResultReceiver.java:59)
at com.igalia.wolvic.utils.zip.UnzipResultReceiver$$ExternalSyntheticLambda0.accept(D8$$SyntheticClass:0)
at java.util.ArrayList.forEach(ArrayList.java:1262)
at com.igalia.wolvic.utils.zip.UnzipResultReceiver.onReceiveResult(UnzipResultReceiver.java:50)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:50)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:214)
at android.os.Looper.loop(Looper.java:304)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1010)
  • Loading branch information
svillar committed Nov 19, 2024
1 parent d98d576 commit 13eb314
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public DownloadsManager(@NonNull Context context) {
mDiskExecutor = ((VRBrowserApplication) mContext.getApplicationContext()).getExecutors().diskIO();
}

private void removeDownloadOnDiskIO(long id) {
mDiskExecutor.execute(() -> { mDownloadManager.remove(id); });
}

public void init() {
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Expand All @@ -75,7 +79,7 @@ public void init() {
downloads.forEach(download -> {
File downloadedFile = download.getOutputFile();
if (mDownloadManager != null && (downloadedFile == null || !downloadedFile.exists())) {
mDownloadManager.remove(download.getId());
removeDownloadOnDiskIO(download.getId());
}
});
}
Expand Down Expand Up @@ -235,19 +239,19 @@ public void removeDownload(long downloadId, boolean deleteFiles) {
File newFile = new File(file.getAbsolutePath().concat(".bak"));
file.renameTo(newFile);
if (mDownloadManager != null) {
mDownloadManager.remove(downloadId);
removeDownloadOnDiskIO(downloadId);
}
newFile.renameTo(file);

} else {
if (mDownloadManager != null) {
mDownloadManager.remove(downloadId);
removeDownloadOnDiskIO(downloadId);
}
}

} else {
if (mDownloadManager != null) {
mDownloadManager.remove(downloadId);
removeDownloadOnDiskIO(downloadId);
}
}
}
Expand Down

0 comments on commit 13eb314

Please sign in to comment.