// Path: AndroidApp/SparkPixels/src/kc/spark/pixels/android/app/AppConfig.java
// public class AppConfig {
// 
// 	private static Context ctx;
// 
// 
// 	// Should be called when starting up the app, probably in
// 	// Application.onCreate()
// 	public static void initialize(Context context) {
// 		ctx = context.getApplicationContext();
// 	}
// 
// 	public static String getApiHostname() {
// 		int resId = useStaging() ? R.string.staging_hostname : R.string.prod_hostname;
// 		return ctx.getString(resId);
// 	}
// 
// 	public static String getApiUrlScheme() {
// 		return ctx.getString(R.string.api_url_scheme);
// 	}
// 
// 	public static int getApiHostPort() {
// 		return ctx.getResources().getInteger(R.integer.api_host_port);
// 	}
// 
// 	public static String getSparkTokenCreationCredentials() {
// 		return ctx.getString(R.string.spark_token_creation_credentials);
// 	}
// 
// 	public static boolean useStaging() {
// 		return ctx.getResources().getBoolean(R.bool.use_staging);
// 	}
// 
// 	public static String getApiVersion() {
// 		return ctx.getString(R.string.api_version);
// 	}
// 
// 	public static String getApiParamAccessToken() {
// 		return ctx.getString(R.string.api_param_access_token);
// 	}
// 
// 	public static String getSmartConfigHelloListenAddress() {
// 		return ctx.getString(R.string.smart_config_hello_listen_address);
// 	}
// 
// 	public static int getSmartConfigHelloListenPort() {
// 		return ctx.getResources().getInteger(R.integer.smart_config_hello_port);
// 	}
// 
// 	public static int getSmartConfigHelloMessageLength() {
// 		return ctx.getResources().getInteger(R.integer.smart_config_hello_msg_length);
// 	}
// 
// 	public static String getSmartConfigDefaultAesKey() {
// 		return ctx.getString(R.string.smart_config_default_aes_key);
// 	}
// 
// }
// 
// Path: AndroidApp/SparkPixels/src/kc/spark/pixels/android/cloud/ApiUrlHelper.java
// public class ApiUrlHelper {
// 
// 	// all Uris just end up getting built from this one anyhow, no need to keep
// 	// reconstructing them completely
// 	private static Uri baseUri;
// 
// 
// 	public static Uri.Builder buildUri(String authToken, String... pathSegments) {
// 		Uri.Builder builder = getBaseUriBuilder().appendPath(AppConfig.getApiVersion());
// 		for (String segment : pathSegments) {
// 			builder.appendPath(segment);
// 		}
// 
// 		if (truthy(authToken)) {
// 			builder.appendQueryParameter(AppConfig.getApiParamAccessToken(), authToken);
// 		}
// 		return builder;
// 	}
// 
// 	public synchronized static Uri.Builder getBaseUriBuilder() {
// 		if (baseUri == null) {
// 			baseUri = new Uri.Builder()
// 					.scheme(AppConfig.getApiUrlScheme())
// 					.encodedAuthority(AppConfig.getApiHostname() + ":" + AppConfig.getApiHostPort())
// 					.build();
// 		}
// 		return baseUri.buildUpon();
// 	}
// 
// 	public static URL convertToURL(Uri.Builder uriBuilder) {
// 		Uri builtUri = uriBuilder.build();
// 		try {
// 			return new URL(builtUri.toString());
// 		} catch (MalformedURLException e) {
// 			// Not printing exception here since I don't know for sure if this
// 			// could ever include the URL itself, which in the case of a GET
// 			// request would include the token(!)
// 			log.e("Unable to build URL from Uri");
// 			return null;
// 		}
// 	}
// 
// 
// 	private static final TLog log = new TLog(ApiUrlHelper.class);
// 
// }


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import kc.spark.pixels.android.app.AppConfig;
import kc.spark.pixels.android.cloud.ApiUrlHelper;
import org.apache.http.protocol.HTTP;
import org.solemnsilence.util.TLog;
import android.net.Uri;
import android.util.Base64;
import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;
			log.e("Error when logging in");
			return null;

		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
	}

	private TokenResponse requestTokenPrivate(HttpURLConnection urlConnection,
			TokenRequest tokenRequest) {

		TokenResponse response = new TokenResponse();
		int responseCode = -1;

		urlConnection.setDoOutput(true);
		urlConnection.setConnectTimeout(5000);
		urlConnection.setReadTimeout(15000);
		urlConnection.setRequestProperty("Authorization", getBasicAuthString());

		try {
			OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
			out.write(tokenRequest.asFormEncodedData().getBytes(HTTP.UTF_8));
			out.close();

			responseCode = urlConnection.getResponseCode();

			InputStream in = new BufferedInputStream(urlConnection.getInputStream());
			String responseStr = readStream(in);
// Path: AndroidApp/SparkPixels/src/kc/spark/pixels/android/app/AppConfig.java
// public class AppConfig {
// 
// 	private static Context ctx;
// 
// 
// 	// Should be called when starting up the app, probably in
// 	// Application.onCreate()
// 	public static void initialize(Context context) {
// 		ctx = context.getApplicationContext();
// 	}
// 
// 	public static String getApiHostname() {
// 		int resId = useStaging() ? R.string.staging_hostname : R.string.prod_hostname;
// 		return ctx.getString(resId);
// 	}
// 
// 	public static String getApiUrlScheme() {
// 		return ctx.getString(R.string.api_url_scheme);
// 	}
// 
// 	public static int getApiHostPort() {
// 		return ctx.getResources().getInteger(R.integer.api_host_port);
// 	}
// 
// 	public static String getSparkTokenCreationCredentials() {
// 		return ctx.getString(R.string.spark_token_creation_credentials);
// 	}
// 
// 	public static boolean useStaging() {
// 		return ctx.getResources().getBoolean(R.bool.use_staging);
// 	}
// 
// 	public static String getApiVersion() {
// 		return ctx.getString(R.string.api_version);
// 	}
// 
// 	public static String getApiParamAccessToken() {
// 		return ctx.getString(R.string.api_param_access_token);
// 	}
// 
// 	public static String getSmartConfigHelloListenAddress() {
// 		return ctx.getString(R.string.smart_config_hello_listen_address);
// 	}
// 
// 	public static int getSmartConfigHelloListenPort() {
// 		return ctx.getResources().getInteger(R.integer.smart_config_hello_port);
// 	}
// 
// 	public static int getSmartConfigHelloMessageLength() {
// 		return ctx.getResources().getInteger(R.integer.smart_config_hello_msg_length);
// 	}
// 
// 	public static String getSmartConfigDefaultAesKey() {
// 		return ctx.getString(R.string.smart_config_default_aes_key);
// 	}
// 
// }
// 
// Path: AndroidApp/SparkPixels/src/kc/spark/pixels/android/cloud/ApiUrlHelper.java
// public class ApiUrlHelper {
// 
// 	// all Uris just end up getting built from this one anyhow, no need to keep
// 	// reconstructing them completely
// 	private static Uri baseUri;
// 
// 
// 	public static Uri.Builder buildUri(String authToken, String... pathSegments) {
// 		Uri.Builder builder = getBaseUriBuilder().appendPath(AppConfig.getApiVersion());
// 		for (String segment : pathSegments) {
// 			builder.appendPath(segment);
// 		}
// 
// 		if (truthy(authToken)) {
// 			builder.appendQueryParameter(AppConfig.getApiParamAccessToken(), authToken);
// 		}
// 		return builder;
// 	}
// 
// 	public synchronized static Uri.Builder getBaseUriBuilder() {
// 		if (baseUri == null) {
// 			baseUri = new Uri.Builder()
// 					.scheme(AppConfig.getApiUrlScheme())
// 					.encodedAuthority(AppConfig.getApiHostname() + ":" + AppConfig.getApiHostPort())
// 					.build();
// 		}
// 		return baseUri.buildUpon();
// 	}
// 
// 	public static URL convertToURL(Uri.Builder uriBuilder) {
// 		Uri builtUri = uriBuilder.build();
// 		try {
// 			return new URL(builtUri.toString());
// 		} catch (MalformedURLException e) {
// 			// Not printing exception here since I don't know for sure if this
// 			// could ever include the URL itself, which in the case of a GET
// 			// request would include the token(!)
// 			log.e("Unable to build URL from Uri");
// 			return null;
// 		}
// 	}
// 
// 
// 	private static final TLog log = new TLog(ApiUrlHelper.class);
// 
// }


// Path: AndroidApp/SparkPixels/src/kc/spark/pixels/android/cloud/login/TokenTool.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import kc.spark.pixels.android.app.AppConfig;
import kc.spark.pixels.android.cloud.ApiUrlHelper;
import org.apache.http.protocol.HTTP;
import org.solemnsilence.util.TLog;
import android.net.Uri;
import android.util.Base64;
import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;

			log.e("Error when logging in");
			return null;

		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
	}

	private TokenResponse requestTokenPrivate(HttpURLConnection urlConnection,
			TokenRequest tokenRequest) {

		TokenResponse response = new TokenResponse();
		int responseCode = -1;

		urlConnection.setDoOutput(true);
		urlConnection.setConnectTimeout(5000);
		urlConnection.setReadTimeout(15000);
		urlConnection.setRequestProperty("Authorization", getBasicAuthString());

		try {
			OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
			out.write(tokenRequest.asFormEncodedData().getBytes(HTTP.UTF_8));
			out.close();

			responseCode = urlConnection.getResponseCode();

			InputStream in = new BufferedInputStream(urlConnection.getInputStream());
			String responseStr = readStream(in);
			in.close();