-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for global broadcasts #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,15 +60,17 @@ else if( data instanceof JSONObject ) { | |
* @param filter | ||
*/ | ||
protected void registerReceiver(android.content.BroadcastReceiver receiver, android.content.IntentFilter filter) { | ||
LocalBroadcastManager.getInstance(super.webView.getContext()).registerReceiver(receiver,filter); | ||
// LocalBroadcastManager.getInstance(super.webView.getContext()).registerReceiver(receiver,filter); | ||
this.webView.getContext().registerReceiver(receiver, filter); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this impact the local events' behavior? If not, I recommend just removing the commented-out lines |
||
} | ||
|
||
/** | ||
* | ||
* @param receiver | ||
*/ | ||
protected void unregisterReceiver(android.content.BroadcastReceiver receiver) { | ||
LocalBroadcastManager.getInstance(super.webView.getContext()).unregisterReceiver(receiver); | ||
// LocalBroadcastManager.getInstance(super.webView.getContext()).unregisterReceiver(receiver); | ||
this.webView.getContext().unregisterReceiver(receiver); | ||
} | ||
|
||
/** | ||
|
@@ -107,6 +109,22 @@ private void fireNativeEvent( final String eventName, JSONObject userData ) { | |
sendBroadcast( intent ); | ||
} | ||
|
||
private String sendGlobalBroadcast(final String eventName, final String actionName, JSONObject userData) { | ||
if( eventName == null ) { | ||
throw new IllegalArgumentException("eventName parameter is null!"); | ||
} | ||
|
||
Log.w("eventName: ", eventName); | ||
Log.w("actionName: ", actionName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove warnings or convert to debug messages |
||
|
||
final Intent intent = new Intent(eventName) | ||
.setAction(actionName) | ||
.putExtras(toBundle(new Bundle(), userData)); | ||
|
||
this.webView.getContext().sendBroadcast(intent); | ||
return intent.toString(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the benefit/value in returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, that was me debugging. |
||
|
||
/** | ||
* | ||
* @param action The action to execute. | ||
|
@@ -137,6 +155,24 @@ public void run() { | |
callbackContext.success(); | ||
return true; | ||
} | ||
else if (action.equals("sendGlobalBroadcast")) { | ||
final String eventName = args.getString(0); | ||
if( eventName==null || eventName.isEmpty() ) { | ||
callbackContext.error(EVENTNAME_ERROR); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make sure the style matches the rest of the existing code (white-space inside the parens, and such) |
||
} | ||
final String actionName = args.getString(1); | ||
final JSONObject userData = args.getJSONObject(2); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to do further checks on these values, like we did for |
||
cordova.getThreadPool().execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
sendGlobalBroadcast(eventName, actionName, userData); | ||
} | ||
}); | ||
|
||
callbackContext.success(); | ||
return true; | ||
} | ||
else if (action.equals("addEventListener")) { | ||
|
||
final String eventName = args.getString(0); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably want to use something other than MTab for your examples :)