package com.growthbeat.link.callback;

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import com.growthbeat.link.GrowthLink;
import com.growthbeat.link.model.Synchronization;
import com.growthbeat.utils.DeviceUtils;

public class DefaultSynchronizationCallback implements SynchronizationCallback {

	@Override
	public void onComplete(Synchronization synchronization) {

		if (!synchronization.getBrowser())
			return;

		new Thread(new Runnable() {
			@Override
			public void run() {

				String urlString = GrowthLink.getInstance().getSyncronizationUrl() + "?applicationId="
						+ GrowthLink.getInstance().getApplicationId();
				try {
					String advertisingId = DeviceUtils.getAdvertisingId().get();
					if (advertisingId != null) {
						urlString += "&advertisingId=" + advertisingId;
					}
				} catch (Exception e) {
					GrowthLink.getInstance().getLogger().warning("Failed to get advertisingId: " + e.getMessage());
				}

				Uri uri = Uri.parse(urlString);
				final android.content.Intent androidIntent = new android.content.Intent(android.content.Intent.ACTION_VIEW, uri);
				androidIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

				new Handler(Looper.getMainLooper()).post(new Runnable() {
					public void run() {
						GrowthLink.getInstance().getContext().startActivity(androidIntent);
					}
				});

			}
		}).start();

	}

}

@@ -323,16 +323,82 @@
 back %7B%0A%0A
+%09private static final long INSTALL_REFERRER_TIMEOUT = 10 * 1000;%0A%0A
 %09@Overri
@@ -424,16 +424,22 @@
 omplete(
+final 
 Synchron
@@ -472,124 +472,859 @@
 %0A%0A%09%09
-if (!synchronization.getBrowser())%0A%09%09%09return;%0A%0A%09%09new Thread(new Runnable(
+new Thread(new Runnable() %7B%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%0A%09%09%09%09String installReferrer = GrowthLink.getInstance().waitInstallReferrer(INSTALL_REFERRER_TIMEOUT);%0A%09%09%09%09if (installReferrer != null && installReferrer.length() != 0) %7B%0A%09%09%09%09%09synchronizeWithInstallReferrer(installReferrer);%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%0A%09%09%09%09if (synchronization.getBrowser())%0A%09%09%09%09%09synchronizeWithCookieTracking();%0A%0A%09%09%09%09installReferrer = GrowthLink.getInstance().waitInstallReferrer(Long.MAX_VALUE);%0A%09%09%09%09synchronizeWithInstallReferrer(installReferrer);%0A%0A%09%09%09%7D%0A%09%09%7D);%0A%0A%09%7D%0A%0A%09protected void synchronizeWithInstallReferrer(String installReferrer
 ) %7B%0A%09%09
-%09
-@Override%0A%09%09%09public void run
+String uriString = %22?%22 + installReferrer.replace(%22growthlink.clickId%22, %22clickId%22).replace(%22growthbeat.uuid%22, %22uuid%22);%0A%09%09GrowthLink.getInstance().handleOpenUrl(Uri.parse(uriString));%0A%09%7D%0A%0A%09protected void synchronizeWithCookieTracking
 () %7B%0A%0A
-%09%09
 %09%09St
@@ -1412,18 +1412,16 @@
 d=%22%0A%09%09%09%09
-%09%09
 + Growth
@@ -1459,18 +1459,16 @@
 onId();%0A
-%09%09
 %09%09try %7B%0A
@@ -1470,18 +1470,16 @@
 ry %7B%0A%09%09%09
-%09
-%09
 String a
@@ -1534,18 +1534,16 @@
 t();%0A%09%09%09
-%09%09
 if (adve
@@ -1567,18 +1567,16 @@
 ) %7B%0A%09%09%09%09
-%09
-%09
 urlStrin
@@ -1615,25 +1615,21 @@
 singId;%0A
-%09%09
 %09%09%09%7D%0A
-%09%09
 %09%09%7D catc
@@ -1646,18 +1646,16 @@
 on e) %7B%0A
-%09%09
 %09%09%09Growt
@@ -1745,27 +1745,23 @@
 age());%0A
-%09%09
 %09%09%7D%0A%0A%09%09
-%09
-%09
 Uri uri 
@@ -1784,18 +1784,16 @@
 tring);%0A
-%09%09
 %09%09final 
@@ -1900,18 +1900,16 @@
 , uri);%0A
-%09%09
 %09%09androi
@@ -1974,18 +1974,16 @@
 TASK);%0A%0A
-%09%09
 %09%09new Ha
@@ -2037,18 +2037,16 @@
 () %7B%0A%09%09%09
-%09%09
 public v
@@ -2057,18 +2057,16 @@
 run() %7B%0A
-%09%09
 %09%09%09%09Grow
@@ -2136,37 +2136,13 @@
 %0A%09%09%09
+%7D%0A
 %09%09%7D
-%0A%09%09%09%09%7D);%0A%0A%09%09%09%7D%0A%09%09%7D).start(
 );%0A%0A

Synchronize with both of install referrer and cookie tracking in SynchronizationCallback