1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Don't call mediastore on file changes on android >=10 (fixes #1801) (#1803)

This commit is contained in:
Simon Frei 2022-07-11 13:16:29 +02:00 committed by GitHub
parent 135584b3a2
commit 5817ed4e07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.provider.MediaStore; import android.provider.MediaStore;
@ -134,11 +135,23 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener
MediaScannerConnection.scanFile(mContext, new String[]{updatedFile.getPath()}, MediaScannerConnection.scanFile(mContext, new String[]{updatedFile.getPath()},
null, null); null, null);
} else { } else {
// Starting with Android 10/Q and targeting API level 29/removing legacy storage flag,
// reports of files being spuriously deleted came up.
// Best guess is that Syncthing directly interacted with the filesystem before,
// and there's a virtualisation layer there now. Also there's reports this API
// changed behaviour with scoped storage. In any case it now does not only
// update the media db, but actually delete the file on disk. Which is bad,
// as it can race with the creation of the same file and thus delete it. See:
// https://github.com/syncthing/syncthing-android/issues/1801
// https://github.com/syncthing/syncthing/issues/7974
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
break;
}
// https://stackoverflow.com/a/29881556/1837158 // https://stackoverflow.com/a/29881556/1837158
Log.i(TAG, "Deleted file from MediaStore: " + updatedFile.toString()); Log.i(TAG, "Deleted file from MediaStore: " + updatedFile.toString());
Uri contentUri = MediaStore.Files.getContentUri("external"); Uri contentUri = MediaStore.Files.getContentUri("external");
ContentResolver resolver = mContext.getContentResolver(); ContentResolver resolver = mContext.getContentResolver();
resolver.delete(contentUri, MediaStore.Images.ImageColumns.DATA + " LIKE ?", resolver.delete(contentUri, MediaStore.Images.ImageColumns.DATA + " = ?",
new String[]{updatedFile.getPath()}); new String[]{updatedFile.getPath()});
} }
break; break;