@@ -153,7 +153,9 @@ public void sendDownloadRequest(String url, String fileName, String description)
153153 .setDescription (description )
154154 .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE );
155155
156- File file = fileManager .getFile (fileName );
156+ // Download to a temp file which will be renamed later
157+ // See: https://stackoverflow.com/a/48128014
158+ File file = fileManager .getFile (fileName + "~" );
157159 Uri destination = Uri .fromFile (file );
158160 request .setDestinationUri (destination );
159161
@@ -184,12 +186,21 @@ public void onReceive(@NonNull Context context, @NonNull Intent intent) {
184186 DownloadManager .Query query = new DownloadManager .Query ();
185187 query .setFilterById (receivedID );
186188 Cursor cur = mgr .query (query );
187- int index = cur .getColumnIndex (DownloadManager .COLUMN_STATUS );
189+ int statusIndex = cur .getColumnIndex (DownloadManager .COLUMN_STATUS );
188190 if (cur .moveToFirst ()) {
189- if (cur .getInt (index ) == DownloadManager .STATUS_SUCCESSFUL ) {
191+ int status = cur .getInt (statusIndex );
192+ if (status == DownloadManager .STATUS_SUCCESSFUL ) {
193+ // Rename temp file by removing the tilde
194+ // See: https://stackoverflow.com/a/48128014
195+ int titleIndex = cur .getColumnIndex (DownloadManager .COLUMN_TITLE );
196+ String fileName = cur .getString (titleIndex );
197+ File temp = fileManager .getFile (fileName + "~" );
198+ File file = fileManager .getFile (fileName );
199+ temp .renameTo (file );
200+
190201 pendingDownloads --;
191202 }
192- if (cur . getInt ( index ) == DownloadManager .STATUS_FAILED ) {
203+ if (status == DownloadManager .STATUS_FAILED ) {
193204 isDownloadingSpawn = false ;
194205 isDownloadingFonts = false ;
195206 isDownloadingTranslation = false ;
0 commit comments