From f7a6c3dbbb7676f855cb95ead41105e6c9934ea4 Mon Sep 17 00:00:00 2001
From: Nikolay Elenkov <nikolay.elenkov@gmail.com>
Date: Thu, 30 Jan 2014 00:20:26 +0900
Subject: [PATCH] a bit of cleanup

---
 .../v2/AccountManagerAuthenticator.java       | 63 ++++---------------
 .../console/v2/BaseAuthenticator.java         | 60 ++++++++++++++++--
 .../v2/OauthAccountManagerAuthenticator.java  | 47 +++-----------
 3 files changed, 77 insertions(+), 93 deletions(-)

diff --git a/src/com/github/andlyticsproject/console/v2/AccountManagerAuthenticator.java b/src/com/github/andlyticsproject/console/v2/AccountManagerAuthenticator.java
index 2daf0471..504c01f9 100644
--- a/src/com/github/andlyticsproject/console/v2/AccountManagerAuthenticator.java
+++ b/src/com/github/andlyticsproject/console/v2/AccountManagerAuthenticator.java
@@ -1,16 +1,5 @@
 package com.github.andlyticsproject.console.v2;
 
-import java.io.IOException;
-import java.util.List;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
-
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AuthenticatorException;
@@ -31,7 +20,17 @@
 import com.github.andlyticsproject.console.AuthenticationException;
 import com.github.andlyticsproject.console.NetworkException;
 import com.github.andlyticsproject.model.DeveloperConsoleAccount;
-import com.github.andlyticsproject.util.FileUtils;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.util.List;
 
 public class AccountManagerAuthenticator extends BaseAuthenticator {
 
@@ -208,14 +207,14 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
 
 			DeveloperConsoleAccount[] developerAccounts = findDeveloperAccounts(responseStr);
 			if (developerAccounts == null) {
-				debugAuthFailure(activity, responseStr);
+				debugAuthFailure(activity, responseStr, webloginUrl);
 
 				throw new AuthenticationException("Couldn't get developer account ID.");
 			}
 
 			String xsrfToken = findXsrfToken(responseStr);
 			if (xsrfToken == null) {
-				debugAuthFailure(activity, responseStr);
+				debugAuthFailure(activity, responseStr, webloginUrl);
 
 				throw new AuthenticationException("Couldn't get XSRF token.");
 			}
@@ -237,40 +236,4 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
 		}
 	}
 
-	private void debugAuthFailure(Activity activity, String responseStr) {
-		FileUtils.writeToAndlyticsDir("console-response.html", responseStr);
-		openAuthUrlInBrowser(activity);
-	}
-
-	private void openAuthUrlInBrowser(Activity activity) {
-		if (webloginUrl == null) {
-			Log.d(TAG, "Null webloginUrl?");
-			return;
-		}
-
-		Log.d(TAG, "Opening login URL in browser: " + webloginUrl);
-
-		Intent viewInBrowser = new Intent(Intent.ACTION_VIEW);
-		viewInBrowser.setData(Uri.parse(webloginUrl));
-
-		// Always show the notification
-		// When this occurs, it can often occur in batches, e.g. if a the user also clicks to view
-		// comments which results in multiple dev consoles opening in their browser without an
-		// explanation. This is even worse if they have multiple accounts and/or are currently
-		// signed in via a different account
-		Context ctx = AndlyticsApp.getInstance();
-		Builder builder = new NotificationCompat.Builder(ctx);
-		builder.setSmallIcon(R.drawable.statusbar_andlytics);
-		builder.setContentTitle(ctx.getResources().getString(R.string.auth_error, accountName));
-		builder.setContentText(ctx.getResources().getString(R.string.auth_error_open_browser,
-				accountName));
-		builder.setAutoCancel(true);
-		PendingIntent contentIntent = PendingIntent.getActivity(ctx, accountName.hashCode(),
-				viewInBrowser, PendingIntent.FLAG_UPDATE_CURRENT);
-		builder.setContentIntent(contentIntent);
-
-		NotificationManager nm = (NotificationManager) ctx
-				.getSystemService(Context.NOTIFICATION_SERVICE);
-		nm.notify(accountName.hashCode(), builder.build());
-	}
 }
diff --git a/src/com/github/andlyticsproject/console/v2/BaseAuthenticator.java b/src/com/github/andlyticsproject/console/v2/BaseAuthenticator.java
index e60e3d62..12b0008c 100644
--- a/src/com/github/andlyticsproject/console/v2/BaseAuthenticator.java
+++ b/src/com/github/andlyticsproject/console/v2/BaseAuthenticator.java
@@ -1,17 +1,32 @@
 package com.github.andlyticsproject.console.v2;
 
+import android.app.Activity;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.support.v4.app.NotificationCompat;
+import android.support.v4.app.NotificationCompat.Builder;
+import android.util.Log;
+
+import com.github.andlyticsproject.AndlyticsApp;
+import com.github.andlyticsproject.R;
+import com.github.andlyticsproject.model.DeveloperConsoleAccount;
+import com.github.andlyticsproject.util.FileUtils;
+
+import org.apache.commons.lang3.StringEscapeUtils;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.commons.lang3.StringEscapeUtils;
-
-import com.github.andlyticsproject.model.DeveloperConsoleAccount;
-
 public abstract class BaseAuthenticator implements DevConsoleAuthenticator {
 
+	private static final String TAG = BaseAuthenticator.class.getSimpleName();
+
 	protected static final Pattern DEV_ACC_PATTERN = Pattern
 			.compile("\"DeveloperConsoleAccounts\":\"\\{\\\\\"1\\\\\":\\[\\{\\\\\"1\\\\\":\\\\\"(\\d{20})\\\\\"");
 	protected static final Pattern DEV_ACCS_PATTERN = Pattern
@@ -72,4 +87,41 @@ public String getAccountName() {
 		return accountName;
 	}
 
+	protected void debugAuthFailure(Activity activity, String responseStr, String webloginUrl) {
+		FileUtils.writeToAndlyticsDir("console-response.html", responseStr);
+		openAuthUrlInBrowser(activity, webloginUrl);
+	}
+
+	protected void openAuthUrlInBrowser(Activity activity, String webloginUrl) {
+		if (webloginUrl == null) {
+			Log.d(TAG, "Null webloginUrl?");
+			return;
+		}
+
+		Log.d(TAG, "Opening login URL in browser: " + webloginUrl);
+
+		Intent viewInBrowser = new Intent(Intent.ACTION_VIEW);
+		viewInBrowser.setData(Uri.parse(webloginUrl));
+
+		// Always show the notification
+		// When this occurs, it can often occur in batches, e.g. if a the user also clicks to view
+		// comments which results in multiple dev consoles opening in their browser without an
+		// explanation. This is even worse if they have multiple accounts and/or are currently
+		// signed in via a different account
+		Context ctx = AndlyticsApp.getInstance();
+		Builder builder = new NotificationCompat.Builder(ctx);
+		builder.setSmallIcon(R.drawable.statusbar_andlytics);
+		builder.setContentTitle(ctx.getResources().getString(R.string.auth_error, accountName));
+		builder.setContentText(ctx.getResources().getString(R.string.auth_error_open_browser,
+				accountName));
+		builder.setAutoCancel(true);
+		PendingIntent contentIntent = PendingIntent.getActivity(ctx, accountName.hashCode(),
+				viewInBrowser, PendingIntent.FLAG_UPDATE_CURRENT);
+		builder.setContentIntent(contentIntent);
+
+		NotificationManager nm = (NotificationManager) ctx
+				.getSystemService(Context.NOTIFICATION_SERVICE);
+		nm.notify(accountName.hashCode(), builder.build());
+	}
+
 }
diff --git a/src/com/github/andlyticsproject/console/v2/OauthAccountManagerAuthenticator.java b/src/com/github/andlyticsproject/console/v2/OauthAccountManagerAuthenticator.java
index e2680e74..b5c1531d 100644
--- a/src/com/github/andlyticsproject/console/v2/OauthAccountManagerAuthenticator.java
+++ b/src/com/github/andlyticsproject/console/v2/OauthAccountManagerAuthenticator.java
@@ -20,7 +20,6 @@
 import com.github.andlyticsproject.console.AuthenticationException;
 import com.github.andlyticsproject.console.NetworkException;
 import com.github.andlyticsproject.model.DeveloperConsoleAccount;
-import com.github.andlyticsproject.util.FileUtils;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
@@ -163,6 +162,12 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
 			pairs.add(new BasicNameValuePair("uberauth", uberToken));
 			pairs.add(new BasicNameValuePair("continue", DEVELOPER_CONSOLE_URL));
 			pairs.add(new BasicNameValuePair("source", "ChromiumBrowser"));
+			// for debugging?
+			webloginUrl = Uri.parse(MERGE_SESSION_URL).buildUpon()
+					.appendQueryParameter("source", "ChromiumBrowser")
+					.appendQueryParameter("uberauth", uberToken)
+					.appendQueryParameter("continue", DEVELOPER_CONSOLE_URL).build().toString();
+			Log.d(TAG, "MergeSession URL: " + webloginUrl);
 
 			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs, "UTF-8");
 			getConsole.setEntity(formEntity);
@@ -188,14 +193,14 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
 
 			DeveloperConsoleAccount[] developerAccounts = findDeveloperAccounts(responseStr);
 			if (developerAccounts == null) {
-				debugAuthFailure(activity, responseStr);
+				debugAuthFailure(activity, responseStr, webloginUrl);
 
 				throw new AuthenticationException("Couldn't get developer account ID.");
 			}
 
 			String xsrfToken = findXsrfToken(responseStr);
 			if (xsrfToken == null) {
-				debugAuthFailure(activity, responseStr);
+				debugAuthFailure(activity, responseStr, webloginUrl);
 
 				throw new AuthenticationException("Couldn't get XSRF token.");
 			}
@@ -217,40 +222,4 @@ private SessionCredentials authenticateInternal(Activity activity, boolean inval
 		}
 	}
 
-	private void debugAuthFailure(Activity activity, String responseStr) {
-		FileUtils.writeToAndlyticsDir("console-response.html", responseStr);
-		openAuthUrlInBrowser(activity);
-	}
-
-	private void openAuthUrlInBrowser(Activity activity) {
-		if (webloginUrl == null) {
-			Log.d(TAG, "Null webloginUrl?");
-			return;
-		}
-
-		Log.d(TAG, "Opening login URL in browser: " + webloginUrl);
-
-		Intent viewInBrowser = new Intent(Intent.ACTION_VIEW);
-		viewInBrowser.setData(Uri.parse(webloginUrl));
-
-		// Always show the notification
-		// When this occurs, it can often occur in batches, e.g. if a the user also clicks to view
-		// comments which results in multiple dev consoles opening in their browser without an
-		// explanation. This is even worse if they have multiple accounts and/or are currently
-		// signed in via a different account
-		Context ctx = AndlyticsApp.getInstance();
-		Builder builder = new NotificationCompat.Builder(ctx);
-		builder.setSmallIcon(R.drawable.statusbar_andlytics);
-		builder.setContentTitle(ctx.getResources().getString(R.string.auth_error, accountName));
-		builder.setContentText(ctx.getResources().getString(R.string.auth_error_open_browser,
-				accountName));
-		builder.setAutoCancel(true);
-		PendingIntent contentIntent = PendingIntent.getActivity(ctx, accountName.hashCode(),
-				viewInBrowser, PendingIntent.FLAG_UPDATE_CURRENT);
-		builder.setContentIntent(contentIntent);
-
-		NotificationManager nm = (NotificationManager) ctx
-				.getSystemService(Context.NOTIFICATION_SERVICE);
-		nm.notify(accountName.hashCode(), builder.build());
-	}
 }
