Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Fix for Issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
nikDemyankov committed Oct 21, 2015
1 parent 6de6d6e commit 3587974
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/android/com/nordnetab/cordova/ul/UniversalLinksPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class UniversalLinksPlugin extends CordovaPlugin {
// callback through which we will send events to JS
private CallbackContext defaultCallback;

private JSMessage storedMessage;

// region Public API

@Override
Expand All @@ -54,6 +56,11 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
return isHandled;
}

@Override
public void onNewIntent(Intent intent) {
handleIntent(intent);
}

// endregion

// region JavaScript methods
Expand All @@ -65,46 +72,53 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
*/
private void initJS(CallbackContext callback) {
setDefaultCallback(callback);
handleLaunchIntent();
if (storedMessage != null) {
sendMessageToJs(storedMessage);
storedMessage = null;
return;
}

handleIntent(cordova.getActivity().getIntent());
}

private void setDefaultCallback(CallbackContext callback) {
this.defaultCallback = callback;
}

/**
* Send event to JS side.
* Send message to JS side.
*
* @param host host entry which corresponds to the launch link
* @param correspondingUri link from which our app has been started
* @param message message to send
* @return true - if message is sent; otherwise - false
*/
private void sendEventToJs(ULHost host, Uri correspondingUri) {
private boolean sendMessageToJs(JSMessage message) {
if (defaultCallback == null) {
return;
return false;
}

final JSMessage message = new JSMessage(host, correspondingUri);
final PluginResult result = new PluginResult(PluginResult.Status.OK, message);
result.setKeepCallback(true);

defaultCallback.sendPluginResult(result);

return true;
}

// endregion

// region Intent handling

/**
* Read data from the launch intent.
* If we started the app from the link - try to process that.
* Handle launch intent.
* If it is an UL intent - then event will be dispatched to the JS side.
*
* @param intent launch intent
*/
private void handleLaunchIntent() {
if (supportedHosts == null || supportedHosts.size() == 0) {
private void handleIntent(Intent intent) {
if (intent == null || supportedHosts == null || supportedHosts.size() == 0) {
return;
}

// read intent
Intent intent = cordova.getActivity().getIntent();
String action = intent.getAction();
Uri launchUri = intent.getData();

Expand All @@ -113,25 +127,21 @@ private void handleLaunchIntent() {
return;
}

// process url
processURL(launchUri);
}

/**
* Handle launch url.
* We will try to match it to the ones that were defined in config.xml.
* If matched - event will be send to JS.
*
* @param launchUri url that started the app
*/
private void processURL(Uri launchUri) {
// try to find host in the hosts list from the config.xml
ULHost host = findHostByUrl(launchUri);
if (host == null) {
Log.d("CUL", "Host " + launchUri.getHost() + " is not supported");
return;
}

sendEventToJs(host, launchUri);
// send message to the JS side;
// if callback is not yet initialized - store message for later use;
final JSMessage message = new JSMessage(host, launchUri);
if (!sendMessageToJs(message)) {
storedMessage = message;
} else {
storedMessage = null;
}
}

/**
Expand Down

0 comments on commit 3587974

Please sign in to comment.