fix(android): prevent errors on filesystem operations while using "content://" uris (#10461)

This commit is contained in:
farfromrefuge
2024-01-19 19:49:31 +01:00
committed by GitHub
parent 41759c189c
commit c15820b59c
2 changed files with 37 additions and 15 deletions

View File

@ -602,12 +602,17 @@ public class Async {
public static class File { public static class File {
static void updateValue(Context context, Uri uri) { static void updateValue(Context context, Uri uri) {
try {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.getContentResolver().update(uri, values, null); context.getContentResolver().update(uri, values, null);
} else { } else {
context.getContentResolver().update(uri, values, null, null); context.getContentResolver().update(uri, values, null, null);
} }
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
} }
public static void append(final String path, final byte[] content, final CompleteCallback callback, final Object context) { public static void append(final String path, final byte[] content, final CompleteCallback callback, final Object context) {

View File

@ -11,6 +11,7 @@ import android.os.Looper;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
@ -34,6 +35,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class FileHelper { public class FileHelper {
static final String TAG = "FileHelper";
private Uri uri; private Uri uri;
private final ExecutorService executor = Executors.newSingleThreadExecutor(); private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final Handler handler; private final Handler handler;
@ -566,7 +568,11 @@ public class FileHelper {
os.write(content, 0, content.length); os.write(content, 0, content.length);
os.flush(); os.flush();
os.close(); os.close();
try {
updateInternal(context); updateInternal(context);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
} }
private void writeBufferSyncInternal(Context context, ByteBuffer content) throws Exception { private void writeBufferSyncInternal(Context context, ByteBuffer content) throws Exception {
@ -579,7 +585,11 @@ public class FileHelper {
channel.write(content); channel.write(content);
os.flush(); os.flush();
os.close(); os.close();
try {
updateInternal(context); updateInternal(context);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
} }
public void writeSync(Context context, byte[] content, @Nullable Callback callback) { public void writeSync(Context context, byte[] content, @Nullable Callback callback) {
@ -638,7 +648,11 @@ public class FileHelper {
osw.write(content); osw.write(content);
osw.flush(); osw.flush();
osw.close(); osw.close();
try {
updateInternal(context); updateInternal(context);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
} }
public void writeTextSync(Context context, String content, @Nullable String encoding, @Nullable Callback callback) { public void writeTextSync(Context context, String content, @Nullable String encoding, @Nullable Callback callback) {
@ -764,14 +778,17 @@ public class FileHelper {
} }
} }
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.getContentResolver().update(uri, values, null); context.getContentResolver().update(uri, values, null);
} else { } else {
context.getContentResolver().update(uri, values, null, null); context.getContentResolver().update(uri, values, null, null);
} }
updateInternal(context, false); updateInternal(context, false);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
} }
public void renameSync(Context context, String newName, @Nullable Callback callback) { public void renameSync(Context context, String newName, @Nullable Callback callback) {