PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/v3.2/nimbits-android/src/com/nimbits/android/account/OwnerAccountImpl.java

http://nimbits-server.googlecode.com/
Java | 108 lines | 82 code | 26 blank | 0 comment | 11 complexity | 515602545e463bc33421ee81ff103448 MD5 | raw file
  1package com.nimbits.android.account;
  2
  3import android.accounts.*;
  4import android.app.Activity;
  5import android.content.Context;
  6import android.os.Bundle;
  7import android.util.Log;
  8import com.nimbits.client.NimbitsClient;
  9import com.nimbits.client.NimbitsClientFactory;
 10import com.nimbits.client.exception.NimbitsRuntimeException;
 11import com.nimbits.client.model.Const;
 12import com.nimbits.client.model.common.CommonFactoryLocator;
 13import com.nimbits.client.model.email.EmailAddress;
 14
 15import java.io.IOException;
 16
 17
 18public class OwnerAccountImpl implements OwnerAccount {
 19    private static String tokenStore = null;
 20    private static EmailAddress emailStore = null;
 21
 22
 23    @Override
 24    public EmailAddress getEmail(final Context context) {
 25        if (emailStore == null) {
 26            final AccountManager accountManager = AccountManager.get(context);
 27            final Account account = getAccount(accountManager);
 28
 29            if (account == null) {
 30                return null;
 31            } else {
 32                emailStore = CommonFactoryLocator.getInstance().createEmailAddress(account.name);
 33                return emailStore;
 34            }
 35        } else {
 36            return emailStore;
 37
 38        }
 39
 40    }
 41
 42
 43    @Override
 44    public String getToken(final Context context) {
 45        String authToken;
 46
 47        if (tokenStore == null) {
 48            final AccountManager mgr = AccountManager.get(context);
 49            final Account[] accounts = mgr.getAccountsByType(Const.PARAM_GOOGLE_COM);
 50
 51            if (accounts.length > 0) {
 52                AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(accounts[0], Const.CONST_AH, null, (Activity) context, null, null);
 53                try {
 54                    final Bundle authTokenBundle = accountManagerFuture.getResult();
 55                    authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
 56                    tokenStore = authToken;
 57                    Log.i(Const.N, "got new token: " + authToken);
 58                } catch (OperationCanceledException e) {
 59                    Log.e(Const.N, e.getMessage(), e);
 60                    authToken = Const.MESSAGE_NO_ACCOUNT;
 61                    tokenStore = null;
 62                } catch (IOException e) {
 63                    authToken = Const.MESSAGE_NO_ACCOUNT;
 64                    tokenStore = null;
 65                    Log.e(Const.N, e.getMessage(), e);
 66                } catch (AuthenticatorException e) {
 67                    authToken = Const.MESSAGE_NO_ACCOUNT;
 68                    tokenStore = null;
 69                    Log.e(Const.N, e.getMessage(), e);
 70                }
 71            } else {
 72                authToken = Const.MESSAGE_NO_ACCOUNT;
 73                tokenStore = null;
 74
 75            }
 76
 77
 78        } else {
 79            authToken = tokenStore;
 80            Log.i(Const.N, "using cached token: " + authToken);
 81        }
 82        return authToken;
 83
 84
 85    }
 86
 87    @Override
 88    public NimbitsClient getNimbitsClient(final Context context, final String baseUrl) {
 89        try {
 90            final String authToken = getToken(context);
 91            final EmailAddress email = getEmail(context);
 92            return NimbitsClientFactory.getInstance(authToken, email, baseUrl);
 93        } catch (Exception e) {
 94            throw new NimbitsRuntimeException("Error getting Nimbits Client", e);
 95        }
 96    }
 97
 98    private Account getAccount(final AccountManager accountManager) {
 99        final Account[] accounts = accountManager.getAccountsByType(Const.PARAM_GOOGLE_COM);
100        return accounts.length > 0 ? accounts[0] : null;
101
102
103    }
104
105
106}
107
108