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:falcofinder.android.fuehrerschein.chat.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from   ww  w  . j  a  v a  2 s .com*/
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.remove(Util.AUTH_COOKIE);
    editor.remove(Util.DEVICE_REGISTRATION_ID);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        final Account account = acct;
        if (account.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the email
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                //commento c2dm non usato
                //C2DMessaging.register(mContext, Setup.SENDER_ID);

            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                final Activity activity = this;
                mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        String authToken = getAuthToken(future);
                        // Ensure the token is not expired by invalidating it and
                        // obtaining a new one
                        mgr.invalidateAuthToken(account.type, authToken);
                        mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                            public void run(AccountManagerFuture<Bundle> future) {
                                String authToken = getAuthToken(future);
                                // Convert the token into a cookie for future use
                                String authCookie = getAuthCookie(authToken);
                                Editor editor = prefs.edit();
                                editor.putString(Util.AUTH_COOKIE, authCookie);
                                editor.commit();
                                //commento c2dm non usato
                                // C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.android.browser.GoogleAccountLogin.java

private void startLogin() {
    saveLoginTime();/*from w w  w  . j  av a2 s  .  co  m*/
    mProgressDialog = ProgressDialog.show(mActivity, mActivity.getString(R.string.pref_autologin_title),
            mActivity.getString(R.string.pref_autologin_progress, mAccount.name), true /* indeterminate */,
            true /* cancelable */, this);
    mState = 1; // SID
    AccountManager.get(mActivity).getAuthToken(mAccount, "SID", null, mActivity, this, null);
}

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

/**
* Restore the client cookies//from ww  w .j av a  2s .  co m
* @param account
* @param client 
* @param context
*/
public static void restoreCookies(Account account, OwnCloudClient client, Context context) {

    Log_OC.d(TAG, "Restoring cookies for " + account.name);

    // Account Manager
    AccountManager am = AccountManager.get(context.getApplicationContext());

    Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getWebdavUri();

    String cookiesString = am.getUserData(account, Constants.KEY_COOKIES);
    if (cookiesString != null) {
        String[] cookies = cookiesString.split(";");
        if (cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = new Cookie();
                int equalPos = cookies[i].indexOf('=');
                cookie.setName(cookies[i].substring(0, equalPos));
                cookie.setValue(cookies[i].substring(equalPos + 1));
                cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT 
                cookie.setPath(serverUri.getPath()); // VERY IMPORTANT

                client.getState().addCookie(cookie);
            }
        }
    }
}

From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java

public FlowzrSyncEngine(FlowzrSyncActivity a) {
    android.accounts.Account useCredential = null;
    if (a == null) {
        a = (FlowzrSyncActivity) FlowzrSyncActivity.getMySelf();
    }//from  w  ww.  j  a  va2 s  .  co m
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(a);
    this.options = FlowzrSyncOptions.fromPrefs(preferences);
    this.context = a;
    this.flowzrSyncActivity = a;
    FlowzrSyncActivity.isRunning = true;
    this.dba = DatabaseAdapter_.getInstance_(context);
    this.db = dba.db();

    if (flowzrSyncActivity == null) {
        Log.i(TAG, "No activity found, creating.");
        Intent intent = new Intent(context, FlowzrSyncActivity.class);
        context.startActivity(intent);
        return;
    }

    http_client = new DefaultHttpClient();

    if (options.appVersion == null) {
        try {
            options.appVersion = context.getPackageManager().getPackageInfo(context.getPackageName(),
                    0).versionName;
        } catch (NameNotFoundException e) {
            options.appVersion = "undef";
            e.printStackTrace();
        }
    }
    Log.i(TAG, "init sync engine, last sync was " + new Date(FlowzrSyncOptions.last_sync_ts).toLocaleString());

    AccountManager accountManager = AccountManager.get(context);
    android.accounts.Account[] accounts = accountManager.getAccountsByType("com.google");
    for (int i = 0; i < accounts.length; i++) {
        if (preferences.getString(FlowzrSyncOptions.PROPERTY_USE_CREDENTIAL, "")
                .equals(((android.accounts.Account) accounts[i]).name)) {
            useCredential = accounts[i];
        }
    }
    if (useCredential != null) {
        AccountManager.get(context).getAuthToken(useCredential, "ah", null, flowzrSyncActivity,
                new GetAuthTokenCallback(), null);
    } else {
        Log.e(TAG, "No account selected");
    }
}

From source file:com.listomate.activities.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from w w w .j  a  v a 2 s.  c o m*/
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.putString(Util.AUTH_COOKIE, null);
    editor.commit();

    // Obtain an auth token and register
    AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        if (acct.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the account name
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                mgr.getAuthToken(acct, "ah", null, this, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle authTokenBundle = future.getResult();
                            String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
                            String authCookie = getAuthCookie(authToken);
                            prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        } catch (AuthenticatorException e) {
                            Log.w(TAG, "Got AuthenticatorException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (IOException e) {
                            Log.w(TAG, "Got IOException " + Log.getStackTraceString(e));
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (OperationCanceledException e) {
                            Log.w(TAG, "Got OperationCanceledException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        }
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.meiste.greg.ptw.GAE.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private boolean reconnect() {
    final AccountManager mgr = AccountManager.get(mContext);
    final Account[] accts = mgr.getAccountsByType(ACCOUNT_TYPE);
    for (final Account acct : accts) {
        if (acct.name.equals(mAccountName)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                mgr.getAuthToken(acct, "ah", null, false, new AuthTokenCallback(), null);
            } else {
                mgr.getAuthToken(acct, "ah", false, new AuthTokenCallback(), null);
            }//from  w  w  w  .  j  ava2s  .c o m
            return true;
        }
    }
    Util.log("Account " + mAccountName + " not found!");
    return false;
}

From source file:es.uma.lcc.tasks.SendUpdateTask.java

private void handleAuthenticationError() {
    Thread t = new CookieRefresherThread(AccountManager.get(mMainActivity), mMainActivity);
    t.start();//from   w  w  w .  ja v  a2 s  . c o m
    try {
        t.join();
        new SendUpdateTask(mOperations, mPicId, mMainActivity, false).execute();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.jdom.get.stuff.done.android.AndroidApplicationContextFactory.java

public Set<String> getAvailableSyncAccounts() {
    AccountManager accountManager = AccountManager.get(activity);
    Account[] accounts = accountManager.getAccountsByType("com.google");

    Set<String> accountSet = new TreeSet<String>();
    for (Account account : accounts) {
        accountSet.add(account.name);/*from w w w . j  ava2  s .co m*/
    }
    return accountSet;
}

From source file:net.vleu.par.android.rpc.Transceiver.java

/**
 * This method may block while a network request completes, and must never
 * be made from the main thread. It requests a new GoogleAuthToken.
 * //from  ww w  . j  a  v  a2s. c o  m
 * @param account
 *            The account to fetch an auth token for
 * @return A token associated with this account
 * @throws OperationCanceledException
 *             if the request was canceled for any reason, including the
 *             user canceling a credential request
 * @throws AuthenticatorException
 *             if the authenticator failed to respond
 * @throws IOException
 *             if the authenticator experienced an I/O problem creating a
 *             new auth token, usually because of network trouble
 */
private GoogleAuthToken blockingGetNewAuthToken()
        throws OperationCanceledException, AuthenticatorException, IOException {
    final AccountManager am = AccountManager.get(this.context);
    final String authTokenStr = am.blockingGetAuthToken(this.account, APPENGINE_TOKEN_TYPE, true);
    if (Log.isLoggable(TAG, Log.INFO))
        Log.i(TAG, "Got a new GoogleAuthToken for account: " + this.account.name + ": " + authTokenStr);
    if (authTokenStr == null)
        throw new AuthenticatorException("Could not get an auth token");
    else
        return new GoogleAuthToken(authTokenStr);
}

From source file:com.capstonecontrol.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //  w w w  . j  a v a  2s .  c  o m
 * @param accountName
 *            a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.remove(Util.AUTH_COOKIE);
    editor.remove(Util.DEVICE_REGISTRATION_ID);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        final Account account = acct;
        if (account.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the email
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                final Activity activity = this;
                mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        String authToken = getAuthToken(future);
                        // Ensure the token is not expired by
                        // invalidating it and
                        // obtaining a new one
                        mgr.invalidateAuthToken(account.type, authToken);
                        mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                            public void run(AccountManagerFuture<Bundle> future) {
                                String authToken = getAuthToken(future);
                                // Convert the token into a
                                // cookie for future use
                                String authCookie = getAuthCookie(authToken);
                                Editor editor = prefs.edit();
                                editor.putString(Util.AUTH_COOKIE, authCookie);
                                editor.commit();
                                C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}