Skip to content

Commit b2dfd90

Browse files
author
Ashraf Amin
committed
Fixed close button crash for express mode
1 parent 2d8c75c commit b2dfd90

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

molpayxdk/src/main/java/com/molpay/molpayxdk/MOLPayActivity.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import android.widget.RelativeLayout;
3737
import android.widget.Toast;
3838

39+
import org.json.JSONArray;
40+
import org.json.JSONException;
3941
import org.json.JSONObject;
4042

4143
import java.io.File;
@@ -94,15 +96,15 @@ public class MOLPayActivity extends AppCompatActivity {
9496
public final static String mp_dpa_id = "mp_dpa_id";
9597
public final static String mp_company = "mp_company";
9698

97-
public final static String MOLPAY = "MOLPAY";
99+
public final static String MOLPAY = "logMOLPAY";
98100
private final static String mpopenmolpaywindow = "mpopenmolpaywindow://";
99101
private final static String mpcloseallwindows = "mpcloseallwindows://";
100102
private final static String mptransactionresults = "mptransactionresults://";
101103
private final static String mprunscriptonpopup = "mprunscriptonpopup://";
102104
private final static String mppinstructioncapture = "mppinstructioncapture://";
103105
private final static String module_id = "module_id";
104106
private final static String wrapper_version = "wrapper_version";
105-
private final static String wrapperVersion = "0";
107+
private final static String wrapperVersion = "9";
106108

107109
private String base64Img;
108110
private String filename;
@@ -418,8 +420,13 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
418420

419421
if (dataString.length() > 0) {
420422
Log.d(MOLPAY, "MPMainUIWebClient mpopenmolpaywindow success");
421-
mpMOLPayUI.loadDataWithBaseURL("", dataString, "text/html", "UTF-8", "");
422-
mpMOLPayUI.setVisibility(View.VISIBLE);
423+
if (mpMOLPayUI != null) {
424+
Log.d(MOLPAY, "mpMOLPayUI not NULL update UI");
425+
mpMOLPayUI.loadDataWithBaseURL("", dataString, "text/html", "UTF-8", "");
426+
mpMOLPayUI.setVisibility(View.VISIBLE);
427+
} else {
428+
Log.d(MOLPAY, "mpMOLPayUI NULL avoid crash");
429+
}
423430
} else {
424431
Log.d(MOLPAY, "MPMainUIWebClient mpopenmolpaywindow empty dataString");
425432
}
@@ -454,23 +461,31 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
454461

455462
Intent result = new Intent();
456463
result.putExtra(MOLPayTransactionResult, dataString);
457-
setResult(RESULT_OK, result);
458464

459-
// Check if mp_request_type is "Receipt", if it is, don't finish()
460-
try {
461-
JSONObject jsonResult = new JSONObject(dataString);
465+
if (isJSONValid(dataString)){
466+
Log.d(MOLPAY, "isJSONValid setResult");
467+
setResult(RESULT_OK, result);
462468

463-
Log.d(MOLPAY, "MPMainUIWebClient jsonResult = " + jsonResult);
469+
// Check if mp_request_type is "Receipt", if it is, don't finish()
470+
try {
471+
JSONObject jsonResult = new JSONObject(dataString);
472+
473+
Log.d(MOLPAY, "MPMainUIWebClient jsonResult = " + jsonResult);
464474

465-
if (!jsonResult.has("mp_request_type") || !jsonResult.getString("mp_request_type").equals("Receipt") || jsonResult.has("error_code")) {
475+
if (!jsonResult.has("mp_request_type") || !jsonResult.getString("mp_request_type").equals("Receipt") || jsonResult.has("error_code")) {
476+
finish();
477+
} else {
478+
// Next close button click will finish() the activity
479+
isClosingReceipt = true;
480+
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
481+
}
482+
} catch (Throwable t) {
466483
finish();
467-
} else {
468-
// Next close button click will finish() the activity
469-
isClosingReceipt = true;
470-
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
471484
}
472-
} catch (Throwable t) {
473-
finish();
485+
}
486+
else {
487+
Log.d(MOLPAY, "json not valid dont setResult");
488+
// setResult(RESULT_CANCELED, result);
474489
}
475490

476491
} else if (url != null && url.startsWith(mprunscriptonpopup)) {
@@ -535,6 +550,20 @@ public void onPageFinished(WebView view, String url) {
535550

536551
}
537552

553+
public boolean isJSONValid(String test) {
554+
try {
555+
new JSONObject(test);
556+
} catch (JSONException ex) {
557+
// in case JSONArray is valid as well
558+
try {
559+
new JSONArray(test);
560+
} catch (JSONException ex1) {
561+
return false;
562+
}
563+
}
564+
return true;
565+
}
566+
538567
private boolean storeImage(Bitmap image) {
539568
String fullPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
540569

@@ -568,7 +597,7 @@ private boolean storeImage(Bitmap image) {
568597
@TargetApi(23)
569598
public boolean isStoragePermissionGranted() {
570599
if (Build.VERSION.SDK_INT >= 23) {
571-
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
600+
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
572601
Log.d(MOLPAY, "isStoragePermissionGranted Permission granted");
573602
storeImage(imgBitmap);
574603
return true;

0 commit comments

Comments
 (0)