Skip to content

Commit

Permalink
Catch NetworkOnMainThreadException when reading or saving files
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Sep 25, 2023
1 parent 07d5b1a commit 76c1dc5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/main/java/de/blau/android/util/SelectFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.NetworkOnMainThreadException;
import android.provider.DocumentsContract;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -233,30 +234,35 @@ public static void handleResult(int code, @NonNull Intent data) {
uri = Uri.fromFile(com.nononsenseapps.filepicker.Utils.getFileForUri(data.getData()));
}
ContentResolverUtil.persistPermissions(activity, data.getFlags(), uri);
if (code == SAVE_FILE) {
File file = new File(uri.getPath());
if (file.exists()) {
Snack.barWarning(activity, activity.getResources().getString(R.string.toast_file_exists, file.getName()), R.string.overwrite, v -> {
synchronized (saveCallbackLock) {
if (saveCallback != null) {
saveCallback.save(uri);
try {
if (code == SAVE_FILE) {
File file = new File(uri.getPath());
if (file.exists()) {
Snack.barWarning(activity, activity.getResources().getString(R.string.toast_file_exists, file.getName()), R.string.overwrite, v -> {
synchronized (saveCallbackLock) {
if (saveCallback != null) {
saveCallback.save(uri);
}
}
});
}
synchronized (saveCallbackLock) {
if (saveCallback != null) {
Log.d(DEBUG_TAG, "saving to " + uri);
saveCallback.save(uri);
}
});
}
synchronized (saveCallbackLock) {
if (saveCallback != null) {
Log.d(DEBUG_TAG, "saving to " + uri);
saveCallback.save(uri);
}
}
} else if (code == READ_FILE) {
synchronized (readCallbackLock) {
if (readCallback != null) {
Log.d(DEBUG_TAG, "reading " + uri);
readCallback.read(uri);
} else if (code == READ_FILE) {
synchronized (readCallbackLock) {
if (readCallback != null) {
Log.d(DEBUG_TAG, "reading " + uri);
readCallback.read(uri);
}
}
}
} catch (NetworkOnMainThreadException nex) {
Log.e(DEBUG_TAG, "Got exception for " + " uri " + nex.getMessage());
Snack.toastTopError(activity, activity.getString(R.string.toast_network_file_not_supported, nex.getMessage()));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@
<string name="toast_no_coverage">%1$s has no coverage here</string>
<string name="toast_tile_layer_errors">The imagery source \'%1$s\' is erroring</string>
<string name="toast_file_exists">%1$s file exists.</string>
<string name="toast_network_file_not_supported">Access to this file over the network is not supported, copy to local storage.\n%1$s</string>
<string name="toast_needs_resurvey">Object may be out of date</string>
<string name="toast_missing_key">%1$s key missing</string>
<string name="toast_invalid_object">Invalid object</string>
Expand Down

0 comments on commit 76c1dc5

Please sign in to comment.