@@ -2349,164 +2349,31 @@ DECLARE_JNI_CLASS (AndroidDialogOnClickListener, "android/content/DialogInterfac
2349
2349
class DialogListener : public juce ::AndroidInterfaceImplementer
2350
2350
{
2351
2351
public:
2352
- DialogListener (std::shared_ptr<ModalComponentManager::Callback> callbackToUse, int resultToUse)
2353
- : callback (std::move (callbackToUse)), result (resultToUse)
2354
- {}
2352
+ explicit DialogListener (std::function<void ()> cb) : callback (std::move (cb)) {}
2355
2353
2356
- void onResult (jobject dialog)
2357
- {
2358
- auto * env = getEnv ();
2359
- env->CallVoidMethod (dialog, AndroidDialogInterface.dismiss );
2360
-
2361
- if (callback != nullptr )
2362
- callback->modalStateFinished (result);
2363
-
2364
- callback = nullptr ;
2365
- }
2366
-
2367
- private:
2368
2354
jobject invoke (jobject proxy, jobject method, jobjectArray args) override
2369
2355
{
2370
2356
auto * env = getEnv ();
2371
2357
auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName ));
2372
2358
2373
2359
if (methodName == " onCancel" || methodName == " onClick" )
2374
2360
{
2375
- onResult (env->GetObjectArrayElement (args, 0 ));
2361
+ auto * dialog = env->GetObjectArrayElement (args, 0 );
2362
+ env->CallVoidMethod (dialog, AndroidDialogInterface.dismiss );
2363
+
2364
+ NullCheckedInvocation::invoke (callback);
2365
+
2376
2366
return nullptr ;
2377
2367
}
2378
2368
2379
2369
// invoke base class
2380
2370
return AndroidInterfaceImplementer::invoke (proxy, method, args);
2381
2371
}
2382
2372
2383
- std::shared_ptr<ModalComponentManager::Callback> callback;
2384
- int result ;
2373
+ private:
2374
+ std::function< void ()> callback ;
2385
2375
};
2386
2376
2387
- // ==============================================================================
2388
- static void createAndroidDialog (const MessageBoxOptions& opts,
2389
- ModalComponentManager::Callback* callbackIn,
2390
- AlertWindowMappings::MapFn mapFn)
2391
- {
2392
- auto * env = getEnv ();
2393
-
2394
- LocalRef<jobject> builder (env->NewObject (AndroidAlertDialogBuilder, AndroidAlertDialogBuilder.construct , getMainActivity ().get ()));
2395
-
2396
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setTitle , javaString (opts.getTitle ()).get ()));
2397
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setMessage , javaString (opts.getMessage ()).get ()));
2398
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setCancelable , true ));
2399
-
2400
- std::shared_ptr<ModalComponentManager::Callback> sharedCallback (AlertWindowMappings::getWrappedCallback (callbackIn, mapFn));
2401
-
2402
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setOnCancelListener ,
2403
- CreateJavaInterface (new DialogListener (sharedCallback, 0 ),
2404
- " android/content/DialogInterface$OnCancelListener" ).get ()));
2405
-
2406
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setPositiveButton ,
2407
- javaString (opts.getButtonText (0 )).get (),
2408
- CreateJavaInterface (new DialogListener (sharedCallback, 0 ),
2409
- " android/content/DialogInterface$OnClickListener" ).get ()));
2410
-
2411
- if (opts.getButtonText (1 ).isNotEmpty ())
2412
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setNegativeButton ,
2413
- javaString (opts.getButtonText (1 )).get (),
2414
- CreateJavaInterface (new DialogListener (sharedCallback, 1 ),
2415
- " android/content/DialogInterface$OnClickListener" ).get ()));
2416
-
2417
- if (opts.getButtonText (2 ).isNotEmpty ())
2418
- builder = LocalRef<jobject> (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.setNeutralButton ,
2419
- javaString (opts.getButtonText (2 )).get (),
2420
- CreateJavaInterface (new DialogListener (sharedCallback, 2 ),
2421
- " android/content/DialogInterface$OnClickListener" ).get ()));
2422
-
2423
- LocalRef<jobject> dialog (env->CallObjectMethod (builder.get (), AndroidAlertDialogBuilder.create ));
2424
-
2425
- LocalRef<jobject> window (env->CallObjectMethod (dialog.get (), AndroidDialog.getWindow ));
2426
-
2427
- if (Desktop::getInstance ().getKioskModeComponent () != nullptr )
2428
- {
2429
- env->CallVoidMethod (window.get (), AndroidWindow.setFlags , FLAG_NOT_FOCUSABLE, FLAG_NOT_FOCUSABLE);
2430
- LocalRef<jobject> decorView (env->CallObjectMethod (window.get (), AndroidWindow.getDecorView ));
2431
- env->CallVoidMethod (decorView.get (), AndroidView.setSystemUiVisibility , fullScreenFlags);
2432
- }
2433
-
2434
- env->CallVoidMethod (dialog.get (), AndroidDialog.show );
2435
-
2436
- if (Desktop::getInstance ().getKioskModeComponent () != nullptr )
2437
- env->CallVoidMethod (window.get (), AndroidWindow.clearFlags , FLAG_NOT_FOCUSABLE);
2438
- }
2439
-
2440
- void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType /* iconType*/ ,
2441
- const String& title, const String& message,
2442
- Component* /* associatedComponent*/ ,
2443
- ModalComponentManager::Callback* callback)
2444
- {
2445
- createAndroidDialog (MessageBoxOptions ()
2446
- .withTitle (title)
2447
- .withMessage (message)
2448
- .withButton (TRANS (" OK" )),
2449
- callback, AlertWindowMappings::messageBox);
2450
- }
2451
-
2452
- bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType /* iconType*/ ,
2453
- const String& title, const String& message,
2454
- Component* /* associatedComponent*/ ,
2455
- ModalComponentManager::Callback* callback)
2456
- {
2457
- createAndroidDialog (MessageBoxOptions ()
2458
- .withTitle (title)
2459
- .withMessage (message)
2460
- .withButton (TRANS (" OK" ))
2461
- .withButton (TRANS (" Cancel" )),
2462
- callback, AlertWindowMappings::okCancel);
2463
-
2464
- return false ;
2465
- }
2466
-
2467
- int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType /* iconType*/ ,
2468
- const String& title, const String& message,
2469
- Component* /* associatedComponent*/ ,
2470
- ModalComponentManager::Callback* callback)
2471
- {
2472
- createAndroidDialog (MessageBoxOptions ()
2473
- .withTitle (title)
2474
- .withMessage (message)
2475
- .withButton (TRANS (" Yes" ))
2476
- .withButton (TRANS (" No" ))
2477
- .withButton (TRANS (" Cancel" )),
2478
- callback, AlertWindowMappings::yesNoCancel);
2479
-
2480
- return 0 ;
2481
- }
2482
-
2483
- int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType /* iconType*/ ,
2484
- const String& title, const String& message,
2485
- Component* /* associatedComponent*/ ,
2486
- ModalComponentManager::Callback* callback)
2487
- {
2488
- createAndroidDialog (MessageBoxOptions ()
2489
- .withTitle (title)
2490
- .withMessage (message)
2491
- .withButton (TRANS (" Yes" ))
2492
- .withButton (TRANS (" No" )),
2493
- callback, AlertWindowMappings::okCancel);
2494
-
2495
- return 0 ;
2496
- }
2497
-
2498
- void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
2499
- ModalComponentManager::Callback* callback)
2500
- {
2501
- createAndroidDialog (options, callback, AlertWindowMappings::noMapping);
2502
- }
2503
-
2504
- void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
2505
- std::function<void (int )> callback)
2506
- {
2507
- showAsync (options, ModalCallbackFunction::create (callback));
2508
- }
2509
-
2510
2377
// ==============================================================================
2511
2378
static bool androidScreenSaverEnabled = true ;
2512
2379
0 commit comments