Skip to content

Commit

Permalink
Merge branch 'release/4.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 18, 2020
2 parents 27ea82a + f17993d commit a0007d0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. Dates are d

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

#### [Unreleased](https://github.com/bsorrentino/cordova-broadcaster/compare/v4.1.0...HEAD)

- merge PR #56 [`0264cbd`](https://github.com/bsorrentino/cordova-broadcaster/commit/0264cbdb345b2bf1b1c041c6be425020078341c2)
- update doc [`5477ed1`](https://github.com/bsorrentino/cordova-broadcaster/commit/5477ed14b1138f21f1645c8197835f68d14a53b3)
- update typescript declarations [`b627c71`](https://github.com/bsorrentino/cordova-broadcaster/commit/b627c7188aeac336d56407b7aa4d932a51959c90)

#### [v4.1.0](https://github.com/bsorrentino/cordova-broadcaster/compare/v4.0.0...v4.1.0)

> 20 March 2020
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $ cordova plugin add cordova-plugin-broadcaster

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

#### Listen for global message
```javascript
Expand Down Expand Up @@ -90,7 +90,7 @@ LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);
@"valid": [NSNumber numberWithBool:YES],
@"child": @{ @"name": @"joker" }
};

[[NSNotificationCenter defaultCenter] postNotificationName:@"TEST.EVENT"
object:nil
userInfo:payload];
Expand Down Expand Up @@ -127,14 +127,14 @@ document.dispatchEvent( event )
} );
```

### From Javascript to ANDROID - Extra ANDROID features
### From Javascript to ANDROID - Extra ANDROID features

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-broadcaster",
"version": "4.1.0",
"version": "4.2.0",
"description": "Allow send message from Javascript to Native",
"cordova": {
"id": "cordova-plugin-broadcaster",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin id="cordova-plugin-broadcaster"
version="4.1.0"
version="4.2.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<engines>
Expand Down
77 changes: 65 additions & 12 deletions src/android/CDVBroadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@
import java.util.Iterator;

import static java.lang.String.format;
import static java.lang.String.valueOf;


// replace with java.util.function.Consumer when moving on min version 24
interface Consumer<T> {
void accept( T value );
}

// replace with java.util.Optional when moving on min version 24
class Optional<T> {

static <T> Optional<T> ofNullable( T value ) {
return new Optional<T>( value );
}

static <T> Optional<T> empty( ) {
return new Optional<T>( null );
}

private final T ref;

private Optional(T ref) {
this.ref = ref;
}

public void ifPresent(Consumer<? super T> consumer) {
if( consumer!=null && isPresent() ) consumer.accept( ref );
}
public boolean isPresent() {
return (ref!=null);
}

public T orElse(T other) {
return isPresent() ? ref : other;
}

}

/**
* This class echoes a string called from JavaScript.
Expand All @@ -28,22 +65,30 @@ public class CDVBroadcaster extends CordovaPlugin {
static class Data {

final JSONObject extras;
final Integer flags;
final String category;
final Optional<Integer> flags;
final Optional<String> category;
final Optional<String> packageName;

final boolean isAndroidSpecific;

Data(final JSONObject userData) {
if (userData.has("extras") && userData.has("flags") && userData.has("category")) {
boolean hasFlags = userData.has("flags");
if (userData.has("extras") && ( hasFlags || userData.has("category") || userData.has("packageName")) ) {

extras = userData.optJSONObject("extras");
flags = userData.optInt("flags");
category = userData.optString("category");
flags = ( hasFlags ) ? Optional.ofNullable( userData.optInt("flags") ) : Optional.empty();
category = Optional.ofNullable(userData.optString("category", null));
packageName = Optional.ofNullable( userData.optString("packageName", null));
isAndroidSpecific = true;

} else {

extras = userData;
flags = null;
category = null;
flags = Optional.empty();
category = Optional.empty();
packageName = Optional.empty();
isAndroidSpecific = false;

}
}

Expand Down Expand Up @@ -168,14 +213,22 @@ private void fireNativeEvent(final String eventNameOrAction, Data userData, bool
final Intent intent = new Intent(eventNameOrAction);

if (userData.isAndroidSpecific) {
intent.addFlags(userData.flags);
intent.addCategory(userData.category);
userData.flags.ifPresent( (flags) -> {
Log.d(TAG, format( "set intent flags: '%s'", valueOf(flags)) );
intent.addFlags(flags);
});
userData.category.ifPresent( (category) -> {
Log.d(TAG, format( "set intent category: '%s'", category) );
intent.addCategory(category);
});
userData.packageName.ifPresent( (packageName) -> {
Log.d(TAG, format( "set intent package: '%s'", packageName) );
intent.setPackage(packageName);
});
}

final Bundle bundle = (userData == null) ? new Bundle() : toBundle(userData.extras);

intent.putExtras(bundle);

sendBroadcast(intent, isGlobal);
}

Expand Down Expand Up @@ -352,7 +405,7 @@ else if (
}
// Other(s)
else {
return String.valueOf(value);
return valueOf(value);
}
}

Expand Down
37 changes: 19 additions & 18 deletions www/broadcaster.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
declare type Listener = (event: Event) => void;
declare type AndroidData = {
declare type AndroidData = { // to be an AndroidData one between flags,category or packageName must be set
extras: object;
flags: number;
category: string;
flags?: number;
category?: string;
packageName?: string;
};

interface CordovaBroadcaster {
Expand All @@ -16,14 +17,14 @@ interface CordovaBroadcaster {
*/
fireNativeEvent(type: string, data: object | AndroidData | null, success?: () => void, error?: (message: string) => void): void;
/**
* fire global native evet (valid only for android)
* @param type
* @param isGlobal
* @param data
* @param success
* @param error
*/
fireNativeEvent(type: string, isGlobal:boolean, data: object | AndroidData | null, success?: () => void, error?: (message: string) => void): void;
* fire global native evet (valid only for android)
* @param type
* @param isGlobal
* @param data
* @param success
* @param error
*/
fireNativeEvent(type: string, isGlobal:boolean, data: object | AndroidData | null, success?: () => void, error?: (message: string) => void): void;
/**
* add a listener
*
Expand All @@ -33,10 +34,10 @@ interface CordovaBroadcaster {
addEventListener(eventname: string, listener: Listener): void;
/**
* add a global listener (valid only for android)
*
* @param eventname
* @param isGlobal
* @param listener
*
* @param eventname
* @param isGlobal
* @param listener
*/
addEventListener(eventname: string, isGlobal: boolean, listener: Listener): void;
/**
Expand All @@ -47,9 +48,9 @@ interface CordovaBroadcaster {
*/
removeEventListener(eventname: string, listener: Listener): void;
}

interface Window {
broadcaster: CordovaBroadcaster;
}
declare var broadcaster: CordovaBroadcaster;

declare var broadcaster: CordovaBroadcaster;

0 comments on commit a0007d0

Please sign in to comment.