diff options
author | Narayan Kamath <narayan@google.com> | 2014-12-16 17:03:02 +0000 |
---|---|---|
committer | Narayan Kamath <narayan@google.com> | 2014-12-16 17:08:25 +0000 |
commit | 3a2cf8007db4f3258b94fdcb74c147220350aa36 (patch) | |
tree | c8514f30328048f995b160ab82ed3a333343c2cc /src/com/android/browser/GoogleAccountLogin.java | |
parent | 3bf59298765b441ba3cd26f0731253962c190387 (diff) | |
download | packages_apps_Browser-3a2cf8007db4f3258b94fdcb74c147220350aa36.zip packages_apps_Browser-3a2cf8007db4f3258b94fdcb74c147220350aa36.tar.gz packages_apps_Browser-3a2cf8007db4f3258b94fdcb74c147220350aa36.tar.bz2 |
Move browser over to java.net.URLConnection.
bug: 18027885
Change-Id: Ie1db66432f0bd2b2eaaf2a7998828f69145645d9
Diffstat (limited to 'src/com/android/browser/GoogleAccountLogin.java')
-rw-r--r-- | src/com/android/browser/GoogleAccountLogin.java | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/src/com/android/browser/GoogleAccountLogin.java b/src/com/android/browser/GoogleAccountLogin.java index 2bd3c8c..93b10a6 100644 --- a/src/com/android/browser/GoogleAccountLogin.java +++ b/src/com/android/browser/GoogleAccountLogin.java @@ -27,18 +27,22 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.SharedPreferences.Editor; import android.net.Uri; -import android.net.http.AndroidHttpClient; import android.os.Bundle; import android.util.Log; import android.webkit.CookieSyncManager; import android.webkit.WebView; import android.webkit.WebViewClient; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.util.EntityUtils; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.Proxy; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.UnsupportedCharsetException; +import libcore.io.Streams; +import libcore.net.http.ResponseUtils; public class GoogleAccountLogin implements Runnable, AccountManagerCallback<Bundle>, OnCancelListener { @@ -104,25 +108,27 @@ public class GoogleAccountLogin implements Runnable, // Runnable @Override public void run() { - String url = ISSUE_AUTH_TOKEN_URL.buildUpon() + String urlString = ISSUE_AUTH_TOKEN_URL.buildUpon() .appendQueryParameter("SID", mSid) .appendQueryParameter("LSID", mLsid) .build().toString(); - // Intentionally not using Proxy. - AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); - HttpPost request = new HttpPost(url); - String result = null; + HttpURLConnection connection = null; + String authToken = null; try { - HttpResponse response = client.execute(request); - int status = response.getStatusLine().getStatusCode(); - if (status != HttpStatus.SC_OK) { + URL url = new URL(urlString); + // Intentionally not using Proxy. + connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); + connection.setRequestMethod("POST"); + connection.setRequestProperty("User-Agent", mUserAgent); + + int status = connection.getResponseCode(); + if (status != HttpURLConnection.HTTP_OK) { Log.d(LOGTAG, "LOGIN_FAIL: Bad status from auth url " - + status + ": " - + response.getStatusLine().getReasonPhrase()); + + status + ": " + connection.getResponseMessage()); // Invalidate the tokens once just in case the 403 was for other // reasons. - if (status == HttpStatus.SC_FORBIDDEN && !mTokensInvalidated) { + if (status == HttpURLConnection.HTTP_FORBIDDEN && !mTokensInvalidated) { Log.d(LOGTAG, "LOGIN_FAIL: Invalidating tokens..."); // Need to regenerate the auth tokens and try again. invalidateTokens(); @@ -134,24 +140,24 @@ public class GoogleAccountLogin implements Runnable, done(); return; } - HttpEntity entity = response.getEntity(); - if (entity == null) { - Log.d(LOGTAG, "LOGIN_FAIL: Null entity in response"); - done(); - return; - } - result = EntityUtils.toString(entity, "UTF-8"); + + final Charset responseCharset = ResponseUtils.responseCharset( + connection.getContentType()); + byte[] responseBytes = Streams.readFully(connection.getInputStream()); + authToken = new String(responseBytes, responseCharset); } catch (Exception e) { Log.d(LOGTAG, "LOGIN_FAIL: Exception acquiring uber token " + e); - request.abort(); done(); return; } finally { - client.close(); + if (connection != null) { + connection.disconnect(); + } } + final String newUrl = TOKEN_AUTH_URL.buildUpon() .appendQueryParameter("source", "android-browser") - .appendQueryParameter("auth", result) + .appendQueryParameter("auth", authToken) .appendQueryParameter("continue", BrowserSettings.getFactoryResetHomeUrl(mActivity)) .build().toString(); |