Example usage for android.accounts AccountManager get

List of usage examples for android.accounts AccountManager get

Introduction

In this page you can find the example usage for android.accounts AccountManager get.

Prototype

public static AccountManager get(Context context) 

Source Link

Document

Gets an AccountManager instance associated with a Context.

Usage

From source file:com.hemou.android.account.AccountUtils.java

/**
 * Get configured account//from  www  . j  a  va 2  s. c o m
 * 
 * @param context
 * @return account or null if none
 */
public static Account getAccount(final Context context) {
    final Account[] accounts = AccountManager.get(context).getAccountsByType(ACCOUNT_TYPE);
    return accounts.length > 0 ? accounts[0] : null;
}

From source file:eu.e43.impeller.Utils.java

public static String getProxyUrl(Context ctx, Account acct, JSONObject obj) {
    if (obj.has("pump_io")) {
        JSONObject pump_io = obj.optJSONObject("pump_io");
        String url = pump_io.optString("proxyURL", null);

        // If the hosts mismatch between the proxyURL and the Account ID, assume we have a
        // "leakage" issue
        Uri uri = Uri.parse(url);/*  www.ja v a  2  s.c  om*/
        Uri userUri = Uri.parse(AccountManager.get(ctx).getUserData(acct, "id"));
        if (!uri.getHost().equalsIgnoreCase(userUri.getHost())) {
            Log.w("Utils.getProxyUrl",
                    "Discarding proxyURL " + url + " due to host mismatch with user " + userUri.toString());
            return null;
        }

        if (url == null || url.length() == 0)
            return null;
        return url;
    } else
        return null;
}

From source file:pt.up.mobile.syncadapter.SigarraSyncAdapter.java

public SigarraSyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    mAccountManager = AccountManager.get(context.getApplicationContext());
    broadcastManager = LocalBroadcastManager.getInstance(context.getApplicationContext());
    EasyTracker.getInstance().setContext(context);
}

From source file:com.notifry.android.remote.BackendClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;/*from  w ww . j a  v a 2s . com*/
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, "ah", false, null, null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            // No auth token - will need to ask permission from user.
            Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
            if (intent != null) {
                // User input required
                context.startActivity(intent);
                throw new PendingAuthException("Asking user for permission.");
            }
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.w(TAG, e.getMessage());
    } catch (IOException e) {
        Log.w(TAG, e.getMessage());
    }

    return authToken;
}

From source file:com.cerema.cloud2.files.services.FileDownloader.java

/**
 * Service initialization//w ww .  j  av  a  2s  .c  o  m
 */
@Override
public void onCreate() {
    super.onCreate();
    Log_OC.d(TAG, "Creating service");
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("FileDownloaderThread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mBinder = new FileDownloaderBinder();

    // add AccountsUpdatedListener
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addOnAccountsUpdatedListener(this, null, false);
}

From source file:com.github.vseguip.sweet.contacts.SweetContactSync.java

public SweetContactSync(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    Log.i(TAG, "SweetContactSync");
    mContext = context;//from  w  ww. ja v  a2s .c  o  m
    AUTH_TOKEN_TYPE = mContext.getString(R.string.account_type);
    mAccountManager = AccountManager.get(mContext);
}

From source file:com.cerema.cloud2.lib.common.accounts.AccountUtils.java

/**
 * Extracts url server from the account/*from  w  w  w .ja  v  a2  s  .  c  o  m*/
 * @param context
 * @param account
 * @return url server or null on failure
 * @throws AccountNotFoundException     When 'account' is unknown for the AccountManager
 */
public static String getBaseUrlForAccount(Context context, Account account) throws AccountNotFoundException {
    AccountManager ama = AccountManager.get(context.getApplicationContext());
    String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL);

    if (baseurl == null)
        throw new AccountNotFoundException(account, "Account not found", null);

    return baseurl;
}

From source file:com.mobiperf.speedometer.AccountSelector.java

/** Starts an authentication request */
public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException {
    Logger.i("AccountSelector.authenticate() running");
    /*/* ww w .  ja  va 2s  .  co  m*/
     * We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during which we
     * can reuse the cookie. If authentication fails due to expired authToken, the client of
     * AccountSelector can call authImmedately() to request authenticate() upon the next checkin
     */
    long authTimeLast = this.getLastAuthTime();
    long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast;
    if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) {
        return;
    }

    Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. ");

    AccountManager accountManager = AccountManager.get(context.getApplicationContext());
    if (this.authToken != null) {
        // There will be no effect on the token if it is still valid
        Logger.i("Invalidating token");
        accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken);
    }

    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    Logger.i("Got " + accounts.length + " accounts");

    // get selected account
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
    String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null);

    if (accounts != null && accounts.length > 0 && selectedAccount != null) {
        Account accountToUse = null;
        for (Account account : accounts) {
            // if (account.name.toLowerCase().trim().endsWith(ACCOUNT_NAME)) {
            Logger.i("account list: " + account.name + " " + account.type + " " + account.toString());
            // If one of the available accounts is the one selected by user, use that
            if (account.name.equals(selectedAccount)) {
                accountToUse = account;
                Logger.i("selected account: " + account.name + " " + account.type + " " + account.toString());
            }
        }

        Logger.i("Trying to get auth token for " + accountToUse);

        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false,
                new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> result) {
                        Logger.i("AccountManagerCallback invoked");
                        try {
                            getAuthToken(result);
                        } catch (RuntimeException e) {
                            Logger.e("Failed to get authToken", e);
                            /*
                             * TODO(Wenjie): May ask the user whether to quit the app nicely here if a number of
                             * trials have been made and failed. Since Speedometer is basically useless without
                             * checkin
                             */
                        }
                    }
                }, null);
        Logger.i("AccountManager.getAuthToken returned " + future);
    } else {
        throw new RuntimeException("No google account found or no google account selected");
    }
}

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

@Override
public String getAuthToken(Context ctx, String inAuthority, IntentSender intentSender)
        throws OperationCanceledException, AuthenticatorException, IOException {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    AccountManager am = AccountManager.get(ctx);
    AccountManagerFuture<Bundle> future = am.getAuthToken(
            new Account(Constants.getAccountName(ctx), Constants.getAccountType(ctx)), authority, false,
            new OnTokenAcquired(ctx, authority, intentSender), null);
    String token = null;/*from w w  w.j  a  v  a2  s.  c  o m*/
    if (future.isDone()) {
        token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
    }
    return token;
}

From source file:com.google.android.apps.chrometophone.AppEngineClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;//ww w.  j a  v a 2  s.  c om
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, false, null,
                null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        if (authToken == null) {
            throw new PendingAuthException(bundle);
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.w(TAG, e.getMessage());
    } catch (IOException e) {
        Log.w(TAG, e.getMessage());
    }
    return authToken;
}