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:Main.java

/**
 * Gets the synchronized Accounts in device of user.
 * @param context Android app Context/*from   w w w  .  j  a  va  2s.com*/
 * @param type String with kind of accounts we want to obtain (Ex. com.google) 
 * @return Account[] with  synchronized user accounts into the device
 */
public static Account[] getAccountsByType(Context context, String type) {
    final AccountManager manager = AccountManager.get(context);
    final Account[] accounts = manager.getAccountsByType(type);
    return accounts;
}

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

public static Uri getHostUri(Context ctx, Account user, String... components) {
    AccountManager am = AccountManager.get(ctx);
    String host = am.getUserData(user, "host");

    Uri.Builder b = new Uri.Builder();
    b.scheme("https");
    b.authority(host);//from w  ww.jav a 2s . co  m

    for (String s : components) {
        b.appendPath(s);
    }

    return b.build();
}

From source file:Main.java

/**
 * Add account//from ww w  .  j a  va  2 s  .  c  o  m
 * @param accountType accountType
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void addAccount(String accountType, String authTokenType, String[] requiredFeatures,
        Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    final AccountManager accountManager = AccountManager.get(activity.getApplicationContext());
    accountManager.addAccount(accountType, authTokenType, requiredFeatures, options, activity, null, null);
}

From source file:edu.mit.mobile.android.livingpostcards.sync.AccountSyncService.java

/**
 * Sets the API URL stored in the account info to the desired URL
 *
 * @param context/*from  w w  w .  java  2s  .co m*/
 * @param account
 * @param desiredUrl
 */
public static void setApiUrl(Context context, Account account, String desiredUrl) {
    final AccountManager am = AccountManager.get(context);

    am.setUserData(account, AuthenticationService.USERDATA_LOCAST_API_URL, desiredUrl);
}

From source file:ca.xecure.easychip.SettingFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.settingfrag, container, false);

    Account[] acc = AccountManager.get(getActivity()).getAccountsByType("com.google");
    if (acc.length <= 1) {
        TextView setting_intro = (TextView) view.findViewById(R.id.settings);
        setting_intro.setText("");
        Button setting_button = (Button) view.findViewById(R.id.change_email_button);
        setting_button.setText("Nothing to set up...");
        setting_button.setEnabled(false);
    }/*from   ww  w  .jav a2s . c o  m*/

    return (view);
}

From source file:com.owncloud.android.utils.OwnCloudClientUtils.java

/**
 * Creates a WebdavClient setup for an ownCloud account
 * //  w  w  w  .  j  a  v a  2s.  co m
 * @param account   The ownCloud account
 * @param context   The application context
 * @return          A WebdavClient object ready to be used
 */
public static WebdavClient createOwnCloudClient(Account account, Context context) {
    Log.d(TAG, "Creating WebdavClient associated to " + account.name);

    String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
    OwnCloudVersion ownCloudVersion = new OwnCloudVersion(
            AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
    String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);

    WebdavClient client = createOwnCloudClient(Uri.parse(baseUrl + webDavPath));

    String username = account.name.substring(0, account.name.lastIndexOf('@'));
    String password = AccountManager.get(context).getPassword(account);
    //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);

    client.setCredentials(username, password);

    return client;
}

From source file:Main.java

public static String getAuthToken(Activity activity, String name, String googleApi) {
    String authToken = null;//from ww w. j  a  v a 2s. co m
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null, null);

    try {
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

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

public static Uri getUserUri(Context ctx, Account user, String... components) {
    AccountManager am = AccountManager.get(ctx);
    String username = am.getUserData(user, "username");
    ArrayList<String> parts = new ArrayList<String>();
    parts.add("api");
    parts.add("user");
    parts.add(username);//w w w  . j  a v a2 s  .c om
    for (String s : components)
        parts.add(s);

    return getHostUri(ctx, user, parts.toArray(components));
}

From source file:com.google.android.apps.dashclock.gmail.GmailExtension.java

static String[] getAllAccountNames(Context context) {
    final Account[] accounts = AccountManager.get(context)
            .getAccountsByType(GmailExtension.ACCOUNT_TYPE_GOOGLE);
    final String[] accountNames = new String[accounts.length];
    for (int i = 0; i < accounts.length; i++) {
        accountNames[i] = accounts[i].name;
    }//  w w w .  j  a v  a 2 s .com
    return accountNames;
}

From source file:org.ohmage.app.OhmageErrorHandler.java

@Override
public Throwable handleError(RetrofitError cause) {

    Response r = cause.getResponse();
    if (r != null && r.getStatus() == 401) {
        // invalidate the access token
        AccountManager accountManager = AccountManager.get(Ohmage.app());
        Account[] accounts = accountManager.getAccountsByType(AuthUtil.ACCOUNT_TYPE);
        if (accounts.length != 0) {
            String token = accountManager.peekAuthToken(accounts[0], AuthUtil.AUTHTOKEN_TYPE);
            if (token != null) {
                accountManager.invalidateAuthToken(AuthUtil.ACCOUNT_TYPE, token);
                Log.e(TAG, "Invalidated " + token);
            }/* w w  w.j ava 2s . co m*/
        }
        return new AuthenticationException("Error authenticating with ohmage", cause);
    }

    return cause;
}