package uk.ac.bournemouth.darwin.auth;

import java.io.IOException;
import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

import android.accounts.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;


public class AccountInfoActivity extends Activity {

  private TextView aTextView;

  @Override
  protected void onCreate(Bundle pSavedInstanceState) {
    super.onCreate(pSavedInstanceState);
    ScrollView scrollview = new ScrollView(this);
    aTextView = new TextView(this);
    scrollview.addView(aTextView);
    
    StringBuilder text=new StringBuilder();
    {
      for (Provider p: Security.getProviders()) {
        TreeMap<String,List<String>> types = new TreeMap<String, List<String>>();
        text.append("Provider: ").append(p.getName()).append('\n');
        for(Service s: p.getServices()) {
          final String type = s.getType();
          List<String> list = types.get(type);
          if (list==null) { 
            list = new ArrayList<String>();
            types.put(s.getType(), list);
          }
          list.add(s.getAlgorithm());
        }
        for(Entry<String, List<String>> type: types.entrySet()) {
          for (String algorithm: type.getValue()) {
            text.append("  Service type:").append(type.getKey())
            .append(" algorithm:").append(algorithm).append('\n');
          }
        }
      }
    }
    aTextView.setText(text);
    setContentView(scrollview);
  }

  @SuppressWarnings("deprecation")
  @Override
  protected void onResume() {
    super.onResume();
    final AccountManager am=AccountManager.get(this);
    Account[] accounts = am.getAccountsByType(DarwinAuthenticator.ACCOUNT_TYPE);
    
    
    if (accounts.length>0) {
      AccountManagerCallback<Bundle> callback = new AccountManagerCallback<Bundle>() {
        
        @Override
        public void run(AccountManagerFuture<Bundle> pFuture) {
          try {
            final String token = pFuture.getResult().getString(AccountManager.KEY_AUTHTOKEN);
            if (token!=null) {
              Log.v("ACCOUNTINFO", "Got an authtoken: "+token);
              aTextView.setText("Got an auth token: "+token);
//            AccountManager am = AccountManager.get(AccountInfoActivity.this);
              am.invalidateAuthToken(DarwinAuthenticator.ACCOUNT_TOKEN_TYPE, token);
            }
          } catch (OperationCanceledException e) {
            Log.w("ACCOUNTINFO", e);
          } catch (AuthenticatorException e) {
            Log.w("ACCOUNTINFO", e);
          } catch (IOException e) {
            Log.w("ACCOUNTINFO", e);
          }
        }
      };
      am.getAuthToken(accounts[0], DarwinAuthenticator.ACCOUNT_TOKEN_TYPE, false, callback , null);
    } else {
      am.addAccount(DarwinAuthenticator.ACCOUNT_TYPE, DarwinAuthenticator.ACCOUNT_TOKEN_TYPE, null, null, this, null, null);
    }
  }
  
  protected void onPause() {
    super.onPause();
    aTextView.setText("Getting auth token");
  }
  
  
  

}

@@ -61,16 +61,73 @@
 eption;%0A
+import java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A
 import j
@@ -377,24 +377,55 @@
 p.Activity;%0A
+import android.content.Intent;%0A
 import andro
@@ -2248,42 +2248,304 @@
 nal 
-String token = pFuture.getR
+Bundle result = pFuture.getResult();%0A            CharSequence newText = null;%0A            if (result.containsKey(AccountManager.KEY_ERROR_CODE)%7C%7C result.containsKey(AccountManager.KEY_ERROR_MESSAGE)) %7B%0A              newText = %22error (%22+result.getString(AccountManager.KEY_ERROR_CODE)+%22): %22+r
 esult
-()
 .get
@@ -2574,17 +2574,21 @@
 KEY_
-AUTHTOKEN
+ERROR_MESSAGE
 );%0A 
@@ -2602,155 +2602,230 @@
     
-if (token!=null) %7B%0A              Log.v(%22ACCOUNTINFO%22, %22Got an authtoken: %22+token);%0A              aTextView.setText(%22Got an auth token: %22+tok
+%7D else if (result.containsKey(AccountManager.KEY_INTENT))%7B%0A              newText = %22received an intent%22;%0A              Intent intent = result.getParcelable(AccountManager.KEY_INTENT);%0A              startActivity(int
 en
+t
 );%0A
-//
+ 
     
@@ -2835,75 +2835,303 @@
     
- AccountManager am = AccountManager.get(AccountInfoActivity.this);%0A
+%7D else if (result.containsKey(AccountManager.KEY_AUTHTOKEN)) %7B%0A              final String token = result.getString(AccountManager.KEY_AUTHTOKEN);%0A              if (token!=null) %7B%0A                newText = %22Got an auth token: %22+token;%0A                Log.v(%22ACCOUNTINFO%22, newText.toString());%0A%0A  
     
@@ -3215,32 +3215,34 @@
 n);%0A            
+  
 %7D%0A          %7D ca
@@ -3241,118 +3241,128 @@
     
-%7D catch (OperationCanceledException e) %7B%0A            Log.w(%22ACCOUNTINFO%22, e);%0A          %7D catch (Authenticator
+  %7D%0A            if (newText!=null) %7B%0A              aTextView.setText(newText);%0A            %7D%0A          %7D catch (Accounts
 Exce
@@ -3384,37 +3384,32 @@
         
-Log.w(%22ACCOUNTINFO%22, 
+reportException(
 e);%0A    
@@ -3456,29 +3456,24 @@
     
-Log.w(%22ACCOUNTINFO%22, 
+reportException(
 e);%0A
@@ -3751,18 +3751,287 @@
   %7D%0A  %7D%0A
+%0A
   
+private void reportException(Throwable pThrowable) %7B%0A    StringWriter writer = new StringWriter();%0A    pThrowable.printStackTrace(new PrintWriter(writer));%0A    aTextView.setText(%22Cancelled: %22+writer.toString());%0A    Log.w(%22ACCOUNTINFO%22, pThrowable);%0A  %7D%0A  %0A  @Override
 %0A  prote

Make the test activity a bit more informative, and more resilient.