Skip to content

Commit

Permalink
(#662) Make Reply.file nullable and add null-checks everywhere around it
Browse files Browse the repository at this point in the history
  • Loading branch information
K1rakishou committed Feb 16, 2020
1 parent 47c33de commit d0edf28
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public void onDestroy() {

public void loadImagePreview() {
Reply reply = replyManager.getReply(loadable);
if (reply.file == null) {
showToast(context, R.string.no_file_is_selected);
return;
}

Point displaySize = getDisplaySize();
ImageDecoder.decodeFileOnBackgroundThread(reply.file,
//decode to the device width/height, whatever is smaller
Expand All @@ -110,6 +115,10 @@ public boolean hasAttachedFile() {
public Bitmap.CompressFormat getImageFormat() {
try {
Reply reply = replyManager.getReply(loadable);
if (reply.file == null) {
return null;
}

return BitmapUtils.getImageFormat(reply.file);
} catch (Exception e) {
Logger.e(TAG, "Error while trying to get image format", e);
Expand All @@ -121,6 +130,10 @@ public Bitmap.CompressFormat getImageFormat() {
public Pair<Integer, Integer> getImageDims() {
try {
Reply reply = replyManager.getReply(loadable);
if (reply.file == null) {
return null;
}

return BitmapUtils.getImageDims(reply.file);
} catch (Exception e) {
Logger.e(TAG, "Error while trying to get image dimensions", e);
Expand Down Expand Up @@ -152,12 +165,12 @@ public void changeImageChecksum(boolean isChecked) {
imageOptions.setChangeImageChecksum(isChecked);
}

public void applyImageOptions() {
public boolean applyImageOptions() {
Reply reply;

synchronized (this) {
if (cancelable != null) {
return;
return true;
}

reply = replyManager.getReply(loadable);
Expand All @@ -170,15 +183,20 @@ public void applyImageOptions() {
if (!imageOptions.getRemoveFilename() && !imageOptions.getFixExif() && !imageOptions.getRemoveMetadata()
&& !imageOptions.getChangeImageChecksum() && imageOptions.getReencodeSettings() == null) {
callback.onImageOptionsApplied(reply, false);
return;
return true;
}

//only the "remove filename" option is selected
if (imageOptions.getRemoveFilename() && !imageOptions.getFixExif() && !imageOptions.getRemoveMetadata()
&& !imageOptions.getChangeImageChecksum() && imageOptions.getReencodeSettings() == null) {
reply.fileName = getNewImageName(reply.fileName, AS_IS);
callback.onImageOptionsApplied(reply, true);
return;
return true;
}

if (reply.file == null) {
// File is not valid
return false;
}

//one of the options that affects the image is selected (reencode/remove metadata/change checksum)
Expand Down Expand Up @@ -218,6 +236,8 @@ public void applyImageOptions() {
synchronized (this) {
cancelable = localCancelable;
}

return true;
}

private String getNewImageName(String currentFileName, ReencodeType newType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.github.adamantcheese.chan.core.site.http;

import androidx.annotation.Nullable;

import com.github.adamantcheese.chan.core.model.orm.Loadable;

import java.io.File;
Expand All @@ -36,6 +38,7 @@ public class Reply {

public Loadable loadable;

@Nullable
public File file;
public String fileName = "";
public String name = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ private void attachFile(
) {
RequestBody requestBody;

if (reply.file == null) {
throw new NullPointerException("reply.file is null!");
}

if (progressListener == null) {
requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), reply.file);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ private void attachFile(
) {
RequestBody requestBody;

if (reply.file == null) {
throw new NullPointerException("reply.file is null!");
}

if (progressListener == null) {
requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), reply.file);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
*/
package com.github.adamantcheese.chan.ui.controller;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.util.Pair;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -52,6 +50,7 @@
import static com.github.adamantcheese.chan.utils.AndroidUtils.getString;
import static com.github.adamantcheese.chan.utils.AndroidUtils.getWindow;
import static com.github.adamantcheese.chan.utils.AndroidUtils.inflate;
import static com.github.adamantcheese.chan.utils.AndroidUtils.showToast;
import static com.github.adamantcheese.chan.utils.AnimationUtils.animateStatusBar;

public class ImageOptionsController
Expand Down Expand Up @@ -211,7 +210,14 @@ public void onClick(View v) {
if (v == cancel) {
imageReencodingHelper.pop();
} else if (v == ok) {
presenter.applyImageOptions();
if (!presenter.applyImageOptions()) {
// For now we only return false when reply.file == null
showToast(
context,
getString(R.string.could_not_apply_image_options,
context.getString(R.string.reply_file_is_null))
);
}
} else if (v == viewHolder) {
imageReencodingHelper.pop();
}
Expand Down
2 changes: 2 additions & 0 deletions Kuroba/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,6 @@ Don't have a 4chan Pass?<br>
<string name="settings_report_description">Report a problem/crash</string>
<string name="settings_auto_crash_report">Automatic crash reporting</string>
<string name="settings_auto_crash_report_description">By enabling this setting, all collected crash logs will be uploaded automatically on every app restart. Crash reports only collect the crash log itself, app version and basic OS information.</string>
<string name="no_file_is_selected">No file is selected</string>
<string name="reply_file_is_null">reply.file is null</string>
</resources>

0 comments on commit d0edf28

Please sign in to comment.