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.ntsync.android.sync.shared.SyncUtils.java

/**
 * /*from w w  w.  j ava  2s  . co  m*/
 * @param authority
 *            the provider to specify in the sync request
 * @param extras
 *            extra parameters to go along with the sync request
 * @param frequency
 *            in seconds
 * @param context
 */
public static void addPeriodicSync(String authority, Bundle extras, long frequency, Context context) {

    AccountManager am = AccountManager.get(context);
    Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE);
    for (Account ac : accounts) {
        ContentResolver.addPeriodicSync(ac, authority, extras, frequency);
    }
}

From source file:com.github.baoti.pioneer.app.AppModule.java

@Provides
@Singleton
public AccountManager provideAccountManager(@ForApp Context context) {
    return AccountManager.get(context);
}

From source file:br.com.bioscada.apps.biotracks.fragments.ChooseAccountDialogFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FragmentActivity fragmentActivity = getActivity();
    accounts = AccountManager.get(fragmentActivity).getAccountsByType(Constants.ACCOUNT_TYPE);

    if (accounts.length == 1) {
        dismiss();//from  ww  w.  j  av a 2  s.c om
        caller.onChooseAccountDone(accounts[0].name);
        return;
    }
}

From source file:com.granita.contacticloudsync.syncadapter.AccountSettings.java

public AccountSettings(Context context, Account account) {
    this.context = context;
    this.account = account;

    accountManager = AccountManager.get(context);

    synchronized (AccountSettings.class) {
        int version = 0;
        try {//  w ww . ja  va2s  .co m
            version = Integer.parseInt(accountManager.getUserData(account, KEY_SETTINGS_VERSION));
        } catch (NumberFormatException e) {
        }
        Constants.log.info("AccountSettings version: v" + version + ", should be: " + version);

        if (version < CURRENT_VERSION) {
            showNotification(Constants.NOTIFICATION_ACCOUNT_SETTINGS_UPDATED,
                    context.getString(R.string.settings_version_update_title),
                    context.getString(R.string.settings_version_update_description));
            update(version);
        }

        // check whether Android version has changed
        String lastAndroidVersionInt = accountManager.getUserData(account, KEY_LAST_ANDROID_VERSION);
        if (lastAndroidVersionInt != null && NumberUtils.toInt(lastAndroidVersionInt) < Build.VERSION.SDK_INT) {
            // notify user
            showNotification(Constants.NOTIFICATION_ANDROID_VERSION_UPDATED,
                    context.getString(R.string.settings_android_update_title),
                    context.getString(R.string.settings_android_update_description));
        }
        accountManager.setUserData(account, KEY_LAST_ANDROID_VERSION, String.valueOf(Build.VERSION.SDK_INT));
    }
}

From source file:org.klnusbaum.udj.PlayersLoader.java

public PlayersLoader(Context context, Account account, Location location) {
    super(context);
    am = AccountManager.get(context);
    this.account = account;
    this.location = location;
    this.players = null;
    this.searchQuery = null;
    locationSearch = true;//from w  w  w  .j a v a  2 s  .  c  om
}

From source file:com.digitalarx.android.syncadapter.AbstractOwnCloudSyncAdapter.java

public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    this.setAccountManager(AccountManager.get(context));
}

From source file:com.nhvu.cordova.AccountManagerPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (manager == null) {
        manager = AccountManager.get(cordova.getActivity());
    }//from  ww  w .j av a  2 s . c  o  m

    try {
        if ("getAccountsByType".equals(action)) {
            Account[] account_list = manager.getAccountsByType(args.isNull(0) ? null : args.getString(0));
            JSONArray result = new JSONArray();

            for (Account account : account_list) {
                Integer index = indexForAccount(account);
                accounts.put(index, account);

                JSONObject account_object = new JSONObject();
                account_object.put("_index", (int) index);
                account_object.put("name", account.name);
                account_object.put("type", account.type);
                result.put(account_object);
            }

            callbackContext.success(result);
            return true;
        } else if ("addAccountExplicitly".equals(action)) {
            if (args.isNull(0) || args.getString(0).length() == 0) {
                callbackContext.error("accountType can not be null or empty");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("username can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("password can not be null or empty");
                return true;
            }

            Account account = new Account(args.getString(1), args.getString(0));
            Integer index = indexForAccount(account);

            Bundle userdata = new Bundle();
            if (!args.isNull(3)) {
                JSONObject userdata_json = args.getJSONObject(3);
                if (userdata_json != null) {
                    Iterator<String> keys = userdata_json.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        userdata.putString(key, userdata_json.getString(key));
                    }
                }
            }

            if (false == manager.addAccountExplicitly(account, args.getString(2), userdata)) {
                callbackContext.error("Account with username already exists!");
                return true;
            }

            accounts.put(index, account);

            JSONObject result = new JSONObject();
            result.put("_index", (int) index);
            result.put("name", account.name);
            result.put("type", account.type);

            callbackContext.success(result);
            return true;
        } else if ("updateCredentials".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            callbackContext.error("Not yet implemented");
            return true;
        } else if ("clearPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.clearPassword(account);
            callbackContext.success();
            return true;
        } else if ("removeAccount".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            int index = args.getInt(0);
            Account account = accounts.get(index);
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            // TODO: Add support for AccountManager (callback)
            AccountManagerFuture<Boolean> future = manager.removeAccount(account, null, null);
            try {
                if (future.getResult() == true) {
                    accounts.remove(index);
                    callbackContext.success();
                } else {
                    callbackContext.error("Failed to remove account");
                }
            } catch (OperationCanceledException e) {
                callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
            } catch (AuthenticatorException e) {
                callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                callbackContext.error("IO error: " + e.getLocalizedMessage());
            }

            return true;
        } else if ("setAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("authToken can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setAuthToken(account, args.getString(1), args.getString(2));
            callbackContext.success();
            return true;
        } else if ("peekAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.peekAuthToken(account, args.getString(1)));
            callbackContext.success(result);
            return true;
        } else if ("getAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            } else if (args.isNull(3)) {
                callbackContext.error("notifyAuthFailure can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            Bundle options = new Bundle();
            // TODO: Options support (will be relevent when we support AccountManagers)

            // TODO: AccountManager support
            AccountManagerFuture<Bundle> future = manager.getAuthToken(account, args.getString(1), options,
                    args.getBoolean(3), null, null);
            try {
                JSONObject result = new JSONObject();
                result.put("value", future.getResult().getString(AccountManager.KEY_AUTHTOKEN));
                callbackContext.success(result);
            } catch (OperationCanceledException e) {
                callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
            } catch (AuthenticatorException e) {
                callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                callbackContext.error("IO error: " + e.getLocalizedMessage());
            }

            return true;
        } else if ("setPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("password can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setPassword(account, args.getString(1));
            callbackContext.success();
            return true;
        } else if ("getPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.getPassword(account));
            callbackContext.success(result);
            return true;
        } else if ("setUserData".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("key can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("value can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setUserData(account, args.getString(1), args.getString(2));
            callbackContext.success();
            return true;
        } else if ("getUserData".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("key can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.getUserData(account, args.getString(1)));
            callbackContext.success(result);
            return true;
        }
    } catch (SecurityException e) {
        callbackContext.error("Access denied");
        return true;
    }

    return false;
}

From source file:org.klnusbaum.udj.EventsLoader.java

public EventsLoader(Context context, Account account, Location location) {
    super(context);
    am = AccountManager.get(context);
    this.account = account;
    this.location = location;
    this.events = null;
    this.searchQuery = null;
    locationSearch = true;//w  w w  .  j  a v a 2 s .  co  m
}

From source file:com.google.appinventor.components.runtime.util.ClientLoginHelper.java

/**
 * Create one of these for each HttpClient needing clientlogin authentication.
 * @param activity        An activity that can be used for user interaction.
 * @param service         The application service class (e.g. "fusiontables").
 * @param prompt          The user prompt (if needed) to choose an account.
 * @param client          The HttpClient to use (or null for a default one).
 *//*from w w  w  .j a v  a2  s . c o m*/
public ClientLoginHelper(Activity activity, String service, String prompt, HttpClient client) {
    this.service = service;
    this.client = (client == null) ? new DefaultHttpClient() : client;
    this.activity = activity;
    this.accountManager = AccountManager.get(activity);
    this.accountChooser = new AccountChooser(activity, service, prompt, service);
}

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

/**
 * Return the list of account names for users to select
 *///w w w .  j ava 2  s. com
public static String[] getAccountList(Context context) {
    AccountManager accountManager = AccountManager.get(context.getApplicationContext());
    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    String[] accountNames = null;
    if (accounts != null && accounts.length > 0) {
        accountNames = new String[accounts.length];
        for (int i = 0; i < accounts.length; i++) {
            accountNames[i] = accounts[i].name;
        }
    }
    return accountNames;
}