Changed the login CancellableServerReceiver to handle cancelled logins with a RequestCancelledException so that GoogleLogin doesn't treat them like errors.

Change-Id: Id9b5188a372722dabef0e2858e063b160e82dd9f
diff --git a/src/com/google/gct/login/CancellableServerReceiver.java b/src/com/google/gct/login/CancellableServerReceiver.java
index ada3a51..c0e894b 100644
--- a/src/com/google/gct/login/CancellableServerReceiver.java
+++ b/src/com/google/gct/login/CancellableServerReceiver.java
@@ -25,7 +25,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.net.Socket;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
@@ -36,6 +35,7 @@
  */
 class CancellableServerReceiver implements VerificationCodeReceiver {
   private static final String CALLBACK_PATH = "/Callback";
+  public static final String REQUEST_CANCELLED_STRING = "Request cancelled.";
 
   /** Server or {@code null} before {@link #getRedirectUri()}. */
   private Server server;
@@ -100,13 +100,18 @@
   }
 
   @Override
-  public String waitForCode() throws IOException {
+  public String waitForCode() throws IOException, RequestCancelledException {
     lock.lock();
     try {
       while (code == null && error == null) {
         gotAuthorizationResponse.awaitUninterruptibly();
       }
       if (error != null) {
+        // If the user cancels the request from Studio (Request Cancelled.), or they cancel the request from the auth page ('access_denied')
+        // then return null and let the UI handle the cancellation.
+        if (error.equals(REQUEST_CANCELLED_STRING) || error.equals("access_denied")) {
+          throw new RequestCancelledException();
+        }
         throw new IOException("User authorization failed (" + error + ")");
       }
       return code;
@@ -126,7 +131,7 @@
       }
       lock.lock();
       try {
-        error = "Request cancelled.";
+        error = REQUEST_CANCELLED_STRING;
         code = null;
         gotAuthorizationResponse.signal();
       }
diff --git a/src/com/google/gct/login/GoogleLogin.java b/src/com/google/gct/login/GoogleLogin.java
index 4676152..4706947 100644
--- a/src/com/google/gct/login/GoogleLogin.java
+++ b/src/com/google/gct/login/GoogleLogin.java
@@ -615,8 +615,12 @@
       try {
         verificationCode = receiver.waitForCode();
       }
+      catch (RequestCancelledException e) {
+        GoogleLoginUtils.showErrorDialog("Login cancelled.", "Google Login");
+        return null;
+      }
       catch (IOException e) {
-        logErrorAndDisplayDialog(title == null? "Google Login" : title, e);
+        logErrorAndDisplayDialog(title == null ? "Google Login" : title, e);
         return null;
       }
       finally {
diff --git a/src/com/google/gct/login/RequestCancelledException.java b/src/com/google/gct/login/RequestCancelledException.java
new file mode 100644
index 0000000..f32c556
--- /dev/null
+++ b/src/com/google/gct/login/RequestCancelledException.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.gct.login;
+
+import java.io.IOException;
+
+/**
+ * An exception that indicates that the login request was cancelled by the user.
+ */
+class RequestCancelledException extends IOException {
+}