mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-27 02:55:50 +01:00
Made UriHelper a bit more failure resistant
This commit is contained in:
parent
1c2cd99efd
commit
709fb0a82b
@ -22,15 +22,18 @@ import android.database.Cursor;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
public class UriHelper {
|
public class UriHelper {
|
||||||
@NonNull
|
@NonNull
|
||||||
private final Uri uri;
|
private final Uri uri;
|
||||||
@ -41,6 +44,8 @@ public class UriHelper {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private File file;
|
private File file;
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(UriHelper.class);
|
||||||
|
|
||||||
private UriHelper(@NonNull Uri uri, @NonNull Context context) {
|
private UriHelper(@NonNull Uri uri, @NonNull Context context) {
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@ -118,55 +123,76 @@ public class UriHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void resolveMetadata() throws IOException {
|
private void resolveMetadata() throws IOException {
|
||||||
|
if (uri == null) {
|
||||||
|
throw new IOException("URI was null, can't query metadata");
|
||||||
|
}
|
||||||
|
|
||||||
String uriScheme = uri.getScheme();
|
String uriScheme = uri.getScheme();
|
||||||
if (ContentResolver.SCHEME_CONTENT.equals(uriScheme)) {
|
|
||||||
Cursor cursor = context.getContentResolver().query(
|
if (uriScheme == null) {
|
||||||
uri,
|
throw new IOException("URI scheme was null, can't query metadata");
|
||||||
new String[] {
|
}
|
||||||
MediaStore.MediaColumns.DISPLAY_NAME,
|
|
||||||
MediaStore.MediaColumns.SIZE
|
switch (uriScheme) {
|
||||||
}, null, null, null);
|
case ContentResolver.SCHEME_CONTENT:
|
||||||
if (cursor == null) {
|
Cursor cursor;
|
||||||
throw new IOException("Unable to query metadata for: " + uri);
|
try {
|
||||||
}
|
ContentResolver resolver = context.getContentResolver();
|
||||||
try {
|
cursor = resolver.query(
|
||||||
if (cursor.moveToFirst()) {
|
uri,
|
||||||
int name_index = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
|
new String[]{
|
||||||
if (name_index == -1) {
|
MediaStore.MediaColumns.DISPLAY_NAME,
|
||||||
throw new IOException("Unable to retrieve name for: " + uri);
|
MediaStore.MediaColumns.SIZE
|
||||||
}
|
}, null, null, null);
|
||||||
int size_index = cursor.getColumnIndex(MediaStore.MediaColumns.SIZE);
|
} catch (IllegalStateException e) {
|
||||||
if (size_index == -1) {
|
LOG.error(e.toString());
|
||||||
throw new IOException("Unable to retrieve size for: " + uri);
|
throw new IOException("IllegalStateException when trying to query metadata for: " + uri);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
fileName = cursor.getString(name_index);
|
if (cursor == null) {
|
||||||
if (fileName == null) {
|
throw new IOException("Unable to query metadata for: " + uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
int name_index = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
|
||||||
|
if (name_index == -1) {
|
||||||
throw new IOException("Unable to retrieve name for: " + uri);
|
throw new IOException("Unable to retrieve name for: " + uri);
|
||||||
}
|
}
|
||||||
fileSize = cursor.getLong(size_index);
|
int size_index = cursor.getColumnIndex(MediaStore.MediaColumns.SIZE);
|
||||||
if (fileSize < 0) {
|
if (size_index == -1) {
|
||||||
throw new IOException("Unable to retrieve size for: " + uri);
|
throw new IOException("Unable to retrieve size for: " + uri);
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
try {
|
||||||
throw new IOException("Unable to retrieve metadata for: " + uri + ": " + ex.getMessage());
|
fileName = cursor.getString(name_index);
|
||||||
|
if (fileName == null) {
|
||||||
|
throw new IOException("Unable to retrieve name for: " + uri);
|
||||||
|
}
|
||||||
|
fileSize = cursor.getLong(size_index);
|
||||||
|
if (fileSize < 0) {
|
||||||
|
throw new IOException("Unable to retrieve size for: " + uri);
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new IOException("Unable to retrieve metadata for: " + uri + ": " + ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
cursor.close();
|
||||||
}
|
}
|
||||||
} finally {
|
break;
|
||||||
cursor.close();
|
case ContentResolver.SCHEME_FILE:
|
||||||
}
|
file = new File(uri.getPath());
|
||||||
} else if (ContentResolver.SCHEME_FILE.equals(uriScheme)) {
|
if (!file.exists()) {
|
||||||
file = new File(uri.getPath());
|
throw new FileNotFoundException("Does not exist: " + file);
|
||||||
if (!file.exists()) {
|
}
|
||||||
throw new FileNotFoundException("Does not exist: " + file);
|
fileName = file.getName();
|
||||||
}
|
fileSize = file.length();
|
||||||
fileName = file.getName();
|
break;
|
||||||
fileSize = file.length();
|
case ContentResolver.SCHEME_ANDROID_RESOURCE:
|
||||||
} else if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uriScheme)) {
|
// we could actually read it, but I don't see how we can determine the file size
|
||||||
// we could actually read it, but I don't see how we can determine the file size
|
throw new IOException("Unsupported scheme for uri: " + uri);
|
||||||
throw new IOException("Unsupported scheme for uri: " + uri);
|
default:
|
||||||
} else {
|
throw new IOException("Unsupported scheme for uri: " + uri);
|
||||||
throw new IOException("Unsupported scheme for uri: " + uri);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user