1919import java .util .Iterator ;
2020
2121import static java .lang .String .format ;
22+ import static java .lang .String .valueOf ;
23+
24+
25+ // replace with java.util.function.Consumer when moving on min version 24
26+ interface Consumer <T > {
27+ void accept ( T value );
28+ }
29+
30+ // replace with java.util.Optional when moving on min version 24
31+ class Optional <T > {
32+
33+ static <T > Optional <T > ofNullable ( T value ) {
34+ return new Optional <T >( value );
35+ }
36+
37+ static <T > Optional <T > empty ( ) {
38+ return new Optional <T >( null );
39+ }
40+
41+ private final T ref ;
42+
43+ private Optional (T ref ) {
44+ this .ref = ref ;
45+ }
46+
47+ public void ifPresent (Consumer <? super T > consumer ) {
48+ if ( consumer !=null && isPresent () ) consumer .accept ( ref );
49+ }
50+ public boolean isPresent () {
51+ return (ref !=null );
52+ }
53+
54+ public T orElse (T other ) {
55+ return isPresent () ? ref : other ;
56+ }
57+
58+ }
2259
2360/**
2461 * This class echoes a string called from JavaScript.
@@ -28,22 +65,30 @@ public class CDVBroadcaster extends CordovaPlugin {
2865 static class Data {
2966
3067 final JSONObject extras ;
31- final Integer flags ;
32- final String category ;
68+ final Optional <Integer > flags ;
69+ final Optional <String > category ;
70+ final Optional <String > packageName ;
3371
3472 final boolean isAndroidSpecific ;
3573
3674 Data (final JSONObject userData ) {
37- if (userData .has ("extras" ) && userData .has ("flags" ) && userData .has ("category" )) {
75+ boolean hasFlags = userData .has ("flags" );
76+ if (userData .has ("extras" ) && ( hasFlags || userData .has ("category" ) || userData .has ("packageName" )) ) {
77+
3878 extras = userData .optJSONObject ("extras" );
39- flags = userData .optInt ("flags" );
40- category = userData .optString ("category" );
79+ flags = ( hasFlags ) ? Optional .ofNullable ( userData .optInt ("flags" ) ) : Optional .empty ();
80+ category = Optional .ofNullable (userData .optString ("category" , null ));
81+ packageName = Optional .ofNullable ( userData .optString ("packageName" , null ));
4182 isAndroidSpecific = true ;
83+
4284 } else {
85+
4386 extras = userData ;
44- flags = null ;
45- category = null ;
87+ flags = Optional .empty ();
88+ category = Optional .empty ();
89+ packageName = Optional .empty ();
4690 isAndroidSpecific = false ;
91+
4792 }
4893 }
4994
@@ -168,14 +213,22 @@ private void fireNativeEvent(final String eventNameOrAction, Data userData, bool
168213 final Intent intent = new Intent (eventNameOrAction );
169214
170215 if (userData .isAndroidSpecific ) {
171- intent .addFlags (userData .flags );
172- intent .addCategory (userData .category );
216+ userData .flags .ifPresent ( (flags ) -> {
217+ Log .d (TAG , format ( "set intent flags: '%s'" , valueOf (flags )) );
218+ intent .addFlags (flags );
219+ });
220+ userData .category .ifPresent ( (category ) -> {
221+ Log .d (TAG , format ( "set intent category: '%s'" , category ) );
222+ intent .addCategory (category );
223+ });
224+ userData .packageName .ifPresent ( (packageName ) -> {
225+ Log .d (TAG , format ( "set intent package: '%s'" , packageName ) );
226+ intent .setPackage (packageName );
227+ });
173228 }
174229
175230 final Bundle bundle = (userData == null ) ? new Bundle () : toBundle (userData .extras );
176-
177231 intent .putExtras (bundle );
178-
179232 sendBroadcast (intent , isGlobal );
180233 }
181234
@@ -352,7 +405,7 @@ else if (
352405 }
353406 // Other(s)
354407 else {
355- return String . valueOf (value );
408+ return valueOf (value );
356409 }
357410 }
358411
0 commit comments