11package com .molpay .molpayxdk ;
22
33import android .Manifest ;
4+ import android .annotation .SuppressLint ;
45import android .annotation .TargetApi ;
56import android .content .ActivityNotFoundException ;
67import android .content .Intent ;
1314import android .os .Bundle ;
1415import android .os .Environment ;
1516import android .os .Message ;
17+
18+ import androidx .annotation .NonNull ;
1619import androidx .annotation .RequiresApi ;
1720import androidx .core .app .ActivityCompat ;
1821import androidx .appcompat .app .AppCompatActivity ;
3841import java .io .File ;
3942import java .io .FileOutputStream ;
4043import java .util .HashMap ;
44+ import java .util .Objects ;
4145
4246public class MOLPayActivity extends AppCompatActivity {
4347
@@ -142,6 +146,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
142146 return super .onOptionsItemSelected (item );
143147 }
144148
149+ @ SuppressLint ("SetJavaScriptEnabled" )
145150 @ Override
146151 protected void onCreate (Bundle savedInstanceState ) {
147152 super .onCreate (savedInstanceState );
@@ -151,18 +156,19 @@ protected void onCreate(Bundle savedInstanceState) {
151156
152157 // For submodule wrappers
153158 boolean is_submodule = false ;
159+ assert paymentDetails != null ;
154160 if (paymentDetails .containsKey ("is_submodule" )) {
155- is_submodule = Boolean .valueOf ( paymentDetails .get ("is_submodule" ).toString ());
161+ is_submodule = Boolean .parseBoolean ( Objects . requireNonNull ( paymentDetails .get ("is_submodule" ) ).toString ());
156162 }
157163 String submodule_module_id = null ;
158164 if (paymentDetails .containsKey ("module_id" )) {
159- submodule_module_id = paymentDetails .get ("module_id" ).toString ();
165+ submodule_module_id = Objects . requireNonNull ( paymentDetails .get ("module_id" ) ).toString ();
160166 }
161167 String submodule_wrapper_version = null ;
162168 if (paymentDetails .containsKey ("wrapper_version" )) {
163- submodule_wrapper_version = paymentDetails .get ("wrapper_version" ).toString ();
169+ submodule_wrapper_version = Objects . requireNonNull ( paymentDetails .get ("wrapper_version" ) ).toString ();
164170 }
165- if (is_submodule && submodule_module_id != "" && submodule_wrapper_version != "" ) {
171+ if (is_submodule && ! Objects . equals ( submodule_module_id , "" ) && ! Objects . equals ( submodule_wrapper_version , "" ) ) {
166172 paymentDetails .put (module_id , submodule_module_id );
167173 paymentDetails .put (wrapper_version , wrapperVersion +"." +submodule_wrapper_version );
168174 } else {
@@ -194,7 +200,7 @@ protected void onCreate(Bundle savedInstanceState) {
194200 mpMainUI .loadUrl ("file:///android_asset/molpay-mobile-xdk-www/index.html" );
195201
196202 // Configure MOLPay ui
197- mpMOLPayUI .getSettings ().setCacheMode (WebSettings .LOAD_CACHE_ELSE_NETWORK );
203+ mpMOLPayUI .getSettings ().setCacheMode (WebSettings .LOAD_DEFAULT );
198204 mpMOLPayUI .getSettings ().setAllowUniversalAccessFromFileURLs (true );
199205 mpMOLPayUI .getSettings ().setJavaScriptCanOpenWindowsAutomatically (true );
200206 mpMOLPayUI .getSettings ().setSupportMultipleWindows (true );
@@ -207,29 +213,26 @@ protected void onCreate(Bundle savedInstanceState) {
207213 CookieManager .getInstance ().setAcceptCookie (true );
208214
209215 mpMOLPayUI .setLongClickable (true );
210- mpMOLPayUI .setOnLongClickListener (new View .OnLongClickListener () {
211- @ Override
212- public boolean onLongClick (View view ) {
213- Log .d (MOLPAY , "Long press fired!" );
214- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
215- mpMOLPayUI .evaluateJavascript ("document.getElementById(\" qrcode_img\" ).src" , new ValueCallback <String >() {
216- @ Override
217- public void onReceiveValue (String qrdata ) {
218- Log .d (MOLPAY , "QR data = " + qrdata );
219- if (qrdata != null && !qrdata .equals ("null" )) {
220- String imageQrCode = qrdata .replaceAll ("data:image/png;base64," , "" );
221- Log .d (MOLPAY , "imageQrCode = " + imageQrCode );
222- byte [] decodedBytes = Base64 .decode (imageQrCode , 0 );
223- imgBitmap = BitmapFactory .decodeByteArray (decodedBytes , 0 , decodedBytes .length );
224- filename = paymentDetails .get ("mp_order_ID" ).toString () + ".png" ;
225-
226- isStoragePermissionGranted ();
227- }
216+ mpMOLPayUI .setOnLongClickListener (view -> {
217+ Log .d (MOLPAY , "Long press fired!" );
218+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
219+ mpMOLPayUI .evaluateJavascript ("document.getElementById(\" qrcode_img\" ).src" , new ValueCallback <String >() {
220+ @ Override
221+ public void onReceiveValue (String qrdata ) {
222+ Log .d (MOLPAY , "QR data = " + qrdata );
223+ if (qrdata != null && !qrdata .equals ("null" )) {
224+ String imageQrCode = qrdata .replaceAll ("data:image/png;base64," , "" );
225+ Log .d (MOLPAY , "imageQrCode = " + imageQrCode );
226+ byte [] decodedBytes = Base64 .decode (imageQrCode , 0 );
227+ imgBitmap = BitmapFactory .decodeByteArray (decodedBytes , 0 , decodedBytes .length );
228+ filename = Objects .requireNonNull (paymentDetails .get ("mp_order_ID" )) + ".png" ;
229+
230+ isStoragePermissionGranted ();
228231 }
229- });
230- }
231- return false ;
232+ }
233+ });
232234 }
235+ return false ;
233236 });
234237
235238 }
@@ -343,32 +346,29 @@ public void onPageFinished (final WebView view, String url) {
343346
344347 Log .d (MOLPAY , "contains url" );
345348
346- view .evaluateJavascript ("document.getElementById(\" systembrowserurl\" ).innerHTML" , new ValueCallback <String >() {
347- @ Override
348- public void onReceiveValue (String s ) {
349- String base64String = s ;
350- Log .d (MOLPAY , "MPMOLPayUIWebClient base64String = " + base64String );
351-
352- // // Decode base64
353- byte [] data = Base64 .decode (base64String , Base64 .DEFAULT );
354- String dataString = new String (data );
355- Log .d (MOLPAY , "MPBankUIWebClient dataString = " + dataString );
356-
357- if (base64String .length () > 0 ) {
358- Log .d (MOLPAY , "MPMOLPayUIWebClient success" );
359- Intent intent = new Intent (Intent .ACTION_VIEW , Uri .parse (dataString ));
360- startActivity (intent );
361- } else {
362- Log .d (MOLPAY , "MPMOLPayUIWebClient empty dataString" );
363- }
364- }
365- });
349+ view .evaluateJavascript ("document.getElementById(\" systembrowserurl\" ).innerHTML" , s -> {
350+ Log .d (MOLPAY , "MPMOLPayUIWebClient base64String = " + s );
351+
352+ // // Decode base64
353+ byte [] data = Base64 .decode (s , Base64 .DEFAULT );
354+ String dataString = new String (data );
355+ Log .d (MOLPAY , "MPBankUIWebClient dataString = " + dataString );
356+
357+ if (s .length () > 0 ) {
358+ Log .d (MOLPAY , "MPMOLPayUIWebClient success" );
359+ Intent intent = new Intent (Intent .ACTION_VIEW , Uri .parse (dataString ));
360+ startActivity (intent );
361+ } else {
362+ Log .d (MOLPAY , "MPMOLPayUIWebClient empty dataString" );
363+ }
364+ });
366365
367366 }
368367 }
369368 }
370369
371370 private class MPMOLPayUIWebChromeClient extends WebChromeClient {
371+ @ SuppressLint ("SetJavaScriptEnabled" )
372372 @ Override
373373 public boolean onCreateWindow (WebView view , boolean dialog , boolean userGesture , Message resultMsg ) {
374374
@@ -379,7 +379,7 @@ public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture,
379379 mpBankUI = new WebView (MOLPayActivity .this );
380380
381381 mpBankUI .getSettings ().setJavaScriptEnabled (true );
382- mpBankUI .getSettings ().setCacheMode (WebSettings .LOAD_CACHE_ELSE_NETWORK );
382+ mpBankUI .getSettings ().setCacheMode (WebSettings .LOAD_DEFAULT );
383383 mpBankUI .getSettings ().setAllowUniversalAccessFromFileURLs (true );
384384 mpBankUI .getSettings ().setJavaScriptCanOpenWindowsAutomatically (true );
385385 mpBankUI .getSettings ().setSupportMultipleWindows (true );
@@ -589,21 +589,19 @@ public boolean isStoragePermissionGranted() {
589589
590590 @ TargetApi (23 )
591591 @ Override
592- public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
592+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
593593 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
594594
595- switch (requestCode ) {
596- case REQUEST_EXTERNAL_STORAGE : {
597- if (grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
598- Log .d (MOLPAY , "onRequestPermissionsResult Permission: " + permissions [0 ] + "was " + grantResults [0 ]);
599- //resume tasks needing this permission
595+ if (requestCode == REQUEST_EXTERNAL_STORAGE ) {
596+ if (grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
597+ Log .d (MOLPAY , "onRequestPermissionsResult Permission: " + permissions [0 ] + "was " + grantResults [0 ]);
598+ //resume tasks needing this permission
600599
601- storeImage (imgBitmap );
600+ storeImage (imgBitmap );
602601
603- } else {
604- Log .d (MOLPAY , "onRequestPermissionsResult EXTERNAL_STORAGE permission was NOT granted." );
605- Toast .makeText (this , "Image not saved" , Toast .LENGTH_LONG ).show ();
606- }
602+ } else {
603+ Log .d (MOLPAY , "onRequestPermissionsResult EXTERNAL_STORAGE permission was NOT granted." );
604+ Toast .makeText (this , "Image not saved" , Toast .LENGTH_LONG ).show ();
607605 }
608606 }
609607 }
0 commit comments