package com.appnexus.opensdk;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.appnexus.opensdk.utils.Clog;
import com.appnexus.opensdk.utils.Settings;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class InstallTrackerPixel extends BroadcastReceiver{
	BroadcastReceiver receiver_install;
	Context context;
	
	
	//Test with am broadcast -a com.android.vending.INSTALL_REFERRER --es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"
	//in adb
	public InstallTrackerPixel(){
		super();
	}

	@Override
	public void onReceive(Context context, final Intent intent) {
		this.context=context;
		Clog.error_context=context;
		final Bundle extras = intent.getExtras();
		
		new Thread(new Runnable(){

			@Override
			public void run() {
				String referralString = extras.getString("referrer");

				String url = getInstallUrl(referralString);
				
				
				Clog.d(Clog.baseLogTag, Clog.getString(R.string.conversion_pixel, url));
				
				try{
					HttpClient client = new DefaultHttpClient();
					HttpGet get = new HttpGet(url);
					client.execute(get);
					// TODO: what happens if it fails? what is the server response if it succeeds?
				}catch(Exception e){
					e.printStackTrace();
					// TODO: repeat the request later?
				}
				
			}
		}).start();

	}

	private String getInstallUrl(String params){
		String appid = null;
		if(context!=null){
			appid = context.getApplicationContext().getPackageName();
		}else{
			Clog.e("OPENSDK", "CONTEXT NULL!?!?!?!");
		}
			
		
		StringBuilder urlBuilder = new StringBuilder(Settings.getSettings().INSTALL_BASE_URL);
		urlBuilder.append(params);
		urlBuilder.append(appid!=null?"&appid="+Uri.encode(appid):"");
		
		return urlBuilder.toString();
	}

}

@@ -208,16 +208,68 @@
 s.Clog;%0A
+import com.appnexus.opensdk.utils.HashingFunctions;%0A
 import c
@@ -459,16 +459,57 @@
 .Bundle;
+%0Aimport android.provider.Settings.Secure;
 %0A%0Apublic
@@ -1715,16 +1715,65 @@
 = null;%0A
+%09%09String hidmd5 = null;%0A%09%09String hidsha1 = null;%0A
 %09%09if(con
@@ -1852,67 +1852,252 @@
 ;%0A%09%09
-%7Delse%7B%0A%09%09%09Clog.e(%22OPENSDK%22, %22CONTEXT NULL!?!?!?!%22);%0A%09%09%7D%0A%09%09%09
+%09String aid = android.provider.Settings.Secure.getString(%0A%09%09%09%09%09context.getContentResolver(), Secure.ANDROID_ID);%0A%09%09%09// Get hidmd5, hidsha1, the devide ID hashed%0A%09%09%09hidmd5 = HashingFunctions.md5(aid);%0A%09%09%09hidsha1 = HashingFunctions.sha1(aid);%0A%09%09%7D
 %0A%09%09%0A
@@ -2279,16 +2279,156 @@
 d):%22%22);%0A
+%09%09urlBuilder.append(hidmd5!=null?%22&md5udid=%22+Uri.encode(hidmd5):%22%22);%0A%09%09urlBuilder.append(hidsha1!=null?%22&sha1udid=%22+Uri.encode(hidmd5):%22%22);%0A
 %09%09%0A%09%09ret

Add device ids to /install call