Skip to content

Commit

Permalink
fix(android): prevent errors on filesystem operations while using "co…
Browse files Browse the repository at this point in the history
…ntent://" uris (#10461)
  • Loading branch information
farfromrefug committed Jan 19, 2024
1 parent 41759c1 commit c15820b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
Expand Up @@ -602,12 +602,17 @@ private void closeOpenedStreams(Stack<Closeable> streams) throws IOException {
public static class File {

static void updateValue(Context context, Uri uri) {
ContentValues values = new ContentValues();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.getContentResolver().update(uri, values, null);
} else {
context.getContentResolver().update(uri, values, null, null);
try {
ContentValues values = new ContentValues();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.getContentResolver().update(uri, values, null);
} else {
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) {
Expand Down
Expand Up @@ -11,6 +11,7 @@
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.webkit.MimeTypeMap;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.documentfile.provider.DocumentFile;
Expand All @@ -34,6 +35,7 @@
import java.util.concurrent.Executors;

public class FileHelper {
static final String TAG = "FileHelper";
private Uri uri;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final Handler handler;
Expand Down Expand Up @@ -566,7 +568,11 @@ private void writeSyncInternal(Context context, byte[] content, boolean append)
os.write(content, 0, content.length);
os.flush();
os.close();
updateInternal(context);
try {
updateInternal(context);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
}

private void writeBufferSyncInternal(Context context, ByteBuffer content) throws Exception {
Expand All @@ -579,7 +585,11 @@ private void writeBufferSyncInternal(Context context, ByteBuffer content, boolea
channel.write(content);
os.flush();
os.close();
updateInternal(context);
try {
updateInternal(context);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}
}

public void writeSync(Context context, byte[] content, @Nullable Callback callback) {
Expand Down Expand Up @@ -638,7 +648,11 @@ private void writeTextSyncInternal(Context context, String content, @Nullable St
osw.write(content);
osw.flush();
osw.close();
updateInternal(context);
try {
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) {
Expand Down Expand Up @@ -764,14 +778,17 @@ private void renameInternal(Context context, String newName) throws Exception {
}

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.getContentResolver().update(uri, values, null);
} else {
context.getContentResolver().update(uri, values, null, null);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.getContentResolver().update(uri, values, null);
} else {
context.getContentResolver().update(uri, values, null, null);
}
updateInternal(context, false);
} catch (Exception exception){
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
}

updateInternal(context, false);

}

public void renameSync(Context context, String newName, @Nullable Callback callback) {
Expand Down

0 comments on commit c15820b

Please sign in to comment.