7 package com.google.appinventor.components.runtime.util;
9 import android.accounts.Account;
10 import android.accounts.AccountManager;
11 import android.accounts.AccountManagerFuture;
12 import android.accounts.AuthenticatorException;
13 import android.accounts.OperationCanceledException;
14 import android.app.Activity;
15 import android.os.Bundle;
16 import android.os.Looper;
17 import android.util.Log;
19 import org.apache.http.Header;
20 import org.apache.http.HttpResponse;
21 import org.apache.http.client.ClientProtocolException;
22 import org.apache.http.client.HttpClient;
23 import org.apache.http.client.methods.HttpUriRequest;
24 import org.apache.http.impl.client.DefaultHttpClient;
26 import java.io.IOException;
39 private static final String LOG_TAG =
"ClientLoginHelper";
40 private static final String ACCOUNT_TYPE =
"com.google";
41 private static final String AUTHORIZATION_HEADER_PREFIX =
"GoogleLogin auth=";
43 private String service;
44 private HttpClient client;
45 private Activity activity;
46 private AccountManager accountManager;
49 private String authToken;
50 private boolean initialized =
false;
59 public ClientLoginHelper(Activity activity, String service, String prompt, HttpClient client) {
60 this.service = service;
61 this.client = (client ==
null) ?
new DefaultHttpClient() : client;
62 this.activity = activity;
63 this.accountManager = AccountManager.get(activity);
64 this.accountChooser =
new AccountChooser(activity, service, prompt, service);
71 private void initialize() throws ClientProtocolException {
73 Log.i(LOG_TAG,
"initializing");
75 throw new IllegalArgumentException(
"Can't initialize login helper from UI thread");
82 private boolean isUiThread() {
83 return Looper.getMainLooper().getThread().equals(Thread.currentThread());
92 public HttpResponse
execute(HttpUriRequest request)
93 throws ClientProtocolException, IOException {
95 addGoogleAuthHeader(request, authToken);
96 HttpResponse response = client.execute(request);
97 if (response.getStatusLine().getStatusCode() == 401) {
98 Log.i(LOG_TAG,
"Invalid token: " + authToken);
99 accountManager.invalidateAuthToken(ACCOUNT_TYPE, authToken);
101 removeGoogleAuthHeaders(request);
102 addGoogleAuthHeader(request, authToken);
103 Log.i(LOG_TAG,
"new token: " + authToken);
104 response = client.execute(request);
121 private static void addGoogleAuthHeader(HttpUriRequest request, String token) {
123 Log.i(LOG_TAG,
"adding auth token token: " + token);
124 request.addHeader(
"Authorization", AUTHORIZATION_HEADER_PREFIX + token);
128 private static void removeGoogleAuthHeaders(HttpUriRequest request) {
129 for (Header header : request.getAllHeaders()) {
130 if (header.getName().equalsIgnoreCase(
"Authorization") &&
131 header.getValue().startsWith(AUTHORIZATION_HEADER_PREFIX)) {
132 Log.i(LOG_TAG,
"Removing header:" + header);
133 request.removeHeader(header);
144 if (account !=
null) {
145 AccountManagerFuture<Bundle> future;
146 future = accountManager.getAuthToken(account, service,
null, activity,
null,
null);
147 Log.i(LOG_TAG,
"Have account, auth token: " + future);
150 result = future.getResult();
151 return result.getString(AccountManager.KEY_AUTHTOKEN);
152 }
catch (AuthenticatorException e) {
154 }
catch (IOException e) {
156 }
catch (OperationCanceledException e) {
160 throw new ClientProtocolException(
"Can't get valid authentication token");