From bc6fdd03d9da1c6b4ebdb4a0c74b3cc04c942957 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 25 May 2021 00:43:20 +0900 Subject: [PATCH] null checks and try with resources --- .../awais/instagrabber/managers/InboxManager.java | 5 +++-- .../awais/instagrabber/managers/ThreadManager.java | 3 +-- .../java/awais/instagrabber/utils/BitmapUtils.java | 13 +++++++------ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/managers/InboxManager.java b/app/src/main/java/awais/instagrabber/managers/InboxManager.java index 073343cd..82019161 100644 --- a/app/src/main/java/awais/instagrabber/managers/InboxManager.java +++ b/app/src/main/java/awais/instagrabber/managers/InboxManager.java @@ -198,7 +198,7 @@ public final class InboxManager { hasOlder = false; return; } - if (!response.getStatus().equals("ok")) { + if (!Objects.equals(response.getStatus(), "ok")) { Log.e(TAG, "DM inbox fetch response: status not ok"); inbox.postValue(Resource.error(R.string.generic_not_ok_response, getCurrentDirectInbox())); hasOlder = false; @@ -209,12 +209,13 @@ public final class InboxManager { viewer = response.getViewer(); } final DirectInbox inbox = response.getInbox(); + if (inbox == null) return; if (!TextUtils.isEmpty(cursor)) { final DirectInbox currentDirectInbox = getCurrentDirectInbox(); if (currentDirectInbox != null) { List threads = currentDirectInbox.getThreads(); threads = threads == null ? new LinkedList<>() : new LinkedList<>(threads); - threads.addAll(inbox.getThreads()); + threads.addAll(inbox.getThreads() == null ? Collections.emptyList() : inbox.getThreads()); inbox.setThreads(threads); } } diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java index 780c1897..21c50a83 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java @@ -16,7 +16,6 @@ import com.google.common.collect.Iterables; import org.json.JSONObject; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.net.HttpURLConnection; import java.util.ArrayList; @@ -1008,7 +1007,7 @@ public final class ThreadManager { return; } sendPhoto(data, uri, dimensions.first, dimensions.second); - } catch (FileNotFoundException e) { + } catch (IOException e) { data.postValue(Resource.error(e.getMessage(), null)); Log.e(TAG, "sendPhoto: ", e); } diff --git a/app/src/main/java/awais/instagrabber/utils/BitmapUtils.java b/app/src/main/java/awais/instagrabber/utils/BitmapUtils.java index 5a4783d0..49137b71 100644 --- a/app/src/main/java/awais/instagrabber/utils/BitmapUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/BitmapUtils.java @@ -17,7 +17,6 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -243,13 +242,15 @@ public final class BitmapUtils { * @return dimensions of the image */ public static Pair decodeDimensions(@NonNull final ContentResolver contentResolver, - @NonNull final Uri uri) throws FileNotFoundException { + @NonNull final Uri uri) throws IOException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options); - return (options.outWidth == -1 || options.outHeight == -1) - ? null - : new Pair<>(options.outWidth, options.outHeight); + try (final InputStream stream = contentResolver.openInputStream(uri)) { + BitmapFactory.decodeStream(stream, null, options); + return (options.outWidth == -1 || options.outHeight == -1) + ? null + : new Pair<>(options.outWidth, options.outHeight); + } } public static File convertToJpegAndSaveToFile(@NonNull final Bitmap bitmap, @Nullable final File file) throws IOException {