1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

null checks and try with resources

This commit is contained in:
Ammar Githam 2021-05-25 00:43:20 +09:00
parent c18e1ddb93
commit bc6fdd03d9
3 changed files with 11 additions and 10 deletions

View File

@ -198,7 +198,7 @@ public final class InboxManager {
hasOlder = false; hasOlder = false;
return; return;
} }
if (!response.getStatus().equals("ok")) { if (!Objects.equals(response.getStatus(), "ok")) {
Log.e(TAG, "DM inbox fetch response: status not ok"); Log.e(TAG, "DM inbox fetch response: status not ok");
inbox.postValue(Resource.error(R.string.generic_not_ok_response, getCurrentDirectInbox())); inbox.postValue(Resource.error(R.string.generic_not_ok_response, getCurrentDirectInbox()));
hasOlder = false; hasOlder = false;
@ -209,12 +209,13 @@ public final class InboxManager {
viewer = response.getViewer(); viewer = response.getViewer();
} }
final DirectInbox inbox = response.getInbox(); final DirectInbox inbox = response.getInbox();
if (inbox == null) return;
if (!TextUtils.isEmpty(cursor)) { if (!TextUtils.isEmpty(cursor)) {
final DirectInbox currentDirectInbox = getCurrentDirectInbox(); final DirectInbox currentDirectInbox = getCurrentDirectInbox();
if (currentDirectInbox != null) { if (currentDirectInbox != null) {
List<DirectThread> threads = currentDirectInbox.getThreads(); List<DirectThread> threads = currentDirectInbox.getThreads();
threads = threads == null ? new LinkedList<>() : new LinkedList<>(threads); threads = threads == null ? new LinkedList<>() : new LinkedList<>(threads);
threads.addAll(inbox.getThreads()); threads.addAll(inbox.getThreads() == null ? Collections.emptyList() : inbox.getThreads());
inbox.setThreads(threads); inbox.setThreads(threads);
} }
} }

View File

@ -16,7 +16,6 @@ import com.google.common.collect.Iterables;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.ArrayList; import java.util.ArrayList;
@ -1008,7 +1007,7 @@ public final class ThreadManager {
return; return;
} }
sendPhoto(data, uri, dimensions.first, dimensions.second); sendPhoto(data, uri, dimensions.first, dimensions.second);
} catch (FileNotFoundException e) { } catch (IOException e) {
data.postValue(Resource.error(e.getMessage(), null)); data.postValue(Resource.error(e.getMessage(), null));
Log.e(TAG, "sendPhoto: ", e); Log.e(TAG, "sendPhoto: ", e);
} }

View File

@ -17,7 +17,6 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -243,13 +242,15 @@ public final class BitmapUtils {
* @return dimensions of the image * @return dimensions of the image
*/ */
public static Pair<Integer, Integer> decodeDimensions(@NonNull final ContentResolver contentResolver, public static Pair<Integer, Integer> decodeDimensions(@NonNull final ContentResolver contentResolver,
@NonNull final Uri uri) throws FileNotFoundException { @NonNull final Uri uri) throws IOException {
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options); try (final InputStream stream = contentResolver.openInputStream(uri)) {
return (options.outWidth == -1 || options.outHeight == -1) BitmapFactory.decodeStream(stream, null, options);
? null return (options.outWidth == -1 || options.outHeight == -1)
: new Pair<>(options.outWidth, options.outHeight); ? null
: new Pair<>(options.outWidth, options.outHeight);
}
} }
public static File convertToJpegAndSaveToFile(@NonNull final Bitmap bitmap, @Nullable final File file) throws IOException { public static File convertToJpegAndSaveToFile(@NonNull final Bitmap bitmap, @Nullable final File file) throws IOException {