Skip to content

Commit a0007d0

Browse files
committed
Merge branch 'release/4.2.0'
2 parents 27ea82a + f17993d commit a0007d0

File tree

6 files changed

+97
-37
lines changed

6 files changed

+97
-37
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. Dates are d
44

55
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
66

7+
#### [Unreleased](https://github.com/bsorrentino/cordova-broadcaster/compare/v4.1.0...HEAD)
8+
9+
- merge PR #56 [`0264cbd`](https://github.com/bsorrentino/cordova-broadcaster/commit/0264cbdb345b2bf1b1c041c6be425020078341c2)
10+
- update doc [`5477ed1`](https://github.com/bsorrentino/cordova-broadcaster/commit/5477ed14b1138f21f1645c8197835f68d14a53b3)
11+
- update typescript declarations [`b627c71`](https://github.com/bsorrentino/cordova-broadcaster/commit/b627c7188aeac336d56407b7aa4d932a51959c90)
12+
713
#### [v4.1.0](https://github.com/bsorrentino/cordova-broadcaster/compare/v4.0.0...v4.1.0)
814

915
> 20 March 2020

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $ cordova plugin add cordova-plugin-broadcaster
4747

4848
window.broadcaster.addEventListener( "didShow", listener);
4949
```
50-
### From ANDROID to Javascript - Extra ANDROID features
50+
### From ANDROID to Javascript - Extra ANDROID features
5151

5252
#### Listen for global message
5353
```javascript
@@ -90,7 +90,7 @@ LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);
9090
@"valid": [NSNumber numberWithBool:YES],
9191
@"child": @{ @"name": @"joker" }
9292
};
93-
93+
9494
[[NSNotificationCenter defaultCenter] postNotificationName:@"TEST.EVENT"
9595
object:nil
9696
userInfo:payload];
@@ -127,14 +127,14 @@ document.dispatchEvent( event )
127127
} );
128128
```
129129

130-
### From Javascript to ANDROID - Extra ANDROID features
130+
### From Javascript to ANDROID - Extra ANDROID features
131131

132132
#### Send a message with "flags" and "category"
133133
```javascript
134134
if( cordova.platformId === "android" ) {
135135

136136
// send a message with "flags" and "category"
137-
window.broadcaster.fireNativeEvent( "message", { extras:{ item:'test data' }, flags:0, category:'android.intent.category.INFO'}, function() {
137+
window.broadcaster.fireNativeEvent( "message", { extras:{ item:'test data' }, flags:0, category:'android.intent.category.INFO', packageName:'org.bsc'}, function() {
138138
console.log( "event fired!" );
139139
});
140140
}
@@ -159,7 +159,7 @@ if( cordova.platformId === "android" ) {
159159

160160
// send a global message with "flags" and "category"
161161
var isGlobal = true
162-
window.broadcaster.fireNativeEvent( "GLOBAL_ACTION", isGlobal, { extras:{ item:'test data' }, flags:0, category:'android.intent.category.INFO'}, function() {
162+
window.broadcaster.fireNativeEvent( "GLOBAL_ACTION", isGlobal, { extras:{ item:'test data' }, flags:0, category:'android.intent.category.INFO', packageName:'org.bsc'}, function() {
163163
console.log( "event fired!" );
164164
});
165165

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-broadcaster",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "Allow send message from Javascript to Native",
55
"cordova": {
66
"id": "cordova-plugin-broadcaster",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<plugin id="cordova-plugin-broadcaster"
3-
version="4.1.0"
3+
version="4.2.0"
44
xmlns="http://apache.org/cordova/ns/plugins/1.0"
55
xmlns:android="http://schemas.android.com/apk/res/android">
66
<engines>

src/android/CDVBroadcaster.java

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,43 @@
1919
import java.util.Iterator;
2020

2121
import 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

www/broadcaster.d.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
declare type Listener = (event: Event) => void;
2-
declare type AndroidData = {
2+
declare type AndroidData = { // to be an AndroidData one between flags,category or packageName must be set
33
extras: object;
4-
flags: number;
5-
category: string;
4+
flags?: number;
5+
category?: string;
6+
packageName?: string;
67
};
78

89
interface CordovaBroadcaster {
@@ -16,14 +17,14 @@ interface CordovaBroadcaster {
1617
*/
1718
fireNativeEvent(type: string, data: object | AndroidData | null, success?: () => void, error?: (message: string) => void): void;
1819
/**
19-
* fire global native evet (valid only for android)
20-
* @param type
21-
* @param isGlobal
22-
* @param data
23-
* @param success
24-
* @param error
25-
*/
26-
fireNativeEvent(type: string, isGlobal:boolean, data: object | AndroidData | null, success?: () => void, error?: (message: string) => void): void;
20+
* fire global native evet (valid only for android)
21+
* @param type
22+
* @param isGlobal
23+
* @param data
24+
* @param success
25+
* @param error
26+
*/
27+
fireNativeEvent(type: string, isGlobal:boolean, data: object | AndroidData | null, success?: () => void, error?: (message: string) => void): void;
2728
/**
2829
* add a listener
2930
*
@@ -33,10 +34,10 @@ interface CordovaBroadcaster {
3334
addEventListener(eventname: string, listener: Listener): void;
3435
/**
3536
* add a global listener (valid only for android)
36-
*
37-
* @param eventname
38-
* @param isGlobal
39-
* @param listener
37+
*
38+
* @param eventname
39+
* @param isGlobal
40+
* @param listener
4041
*/
4142
addEventListener(eventname: string, isGlobal: boolean, listener: Listener): void;
4243
/**
@@ -47,9 +48,9 @@ interface CordovaBroadcaster {
4748
*/
4849
removeEventListener(eventname: string, listener: Listener): void;
4950
}
50-
51+
5152
interface Window {
5253
broadcaster: CordovaBroadcaster;
5354
}
54-
55-
declare var broadcaster: CordovaBroadcaster;
55+
56+
declare var broadcaster: CordovaBroadcaster;

0 commit comments

Comments
 (0)