List of usage examples for android.accounts AccountManager get
public static AccountManager get(Context context)
From source file:com.masteriti.manager.AccountsActivity.java
/** * Registers for C2DM messaging with the given account name. * /*from w ww .j av a 2s .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(); 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>() { @Override 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>() { @Override 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; } } }
From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java
@Override public String promote(final Activity activity, String inAuthority, final String token) { final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority; final AccountManager am = AccountManager.get(activity); // Bundle options = new Bundle(); // if (token != null) { // options.putString(Constants.PROMOTION_TOKEN, token); // }// ww w .j av a2s . c o m final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)); final String userDataString = am.getUserData(a, AccountManager.KEY_USERDATA); invalidateToken(activity, authority); am.getAuthToken(new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)), authority, null, null, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Bundle bundle = null; try { bundle = result.getResult(); Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT); if (launch != null) { launch.putExtra(Constants.KEY_AUTHORITY, authority); launch.putExtra(Constants.PROMOTION_TOKEN, token); launch.putExtra(Constants.OLD_DATA, userDataString); activity.startActivityForResult(launch, SC_AUTH_ACTIVITY_REQUEST_CODE); } else if (bundle.getString(AccountManager.KEY_AUTHTOKEN) != null) { // am.setAuthToken(a, authority, bundle.getString(AccountManager.KEY_AUTHTOKEN)); // am.addAccountExplicitly(a, null, null); // no token acquired } else { storeAnonymousToken(token, authority, am, a); } } catch (Exception e) { // revert the invalidated token storeAnonymousToken(token, authority, am, a); return; } } }, null); return null; }
From source file:com.cerema.cloud2.lib.common.accounts.AccountUtils.java
public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) { // Account Manager AccountManager ac = AccountManager.get(context.getApplicationContext()); if (client != null) { String cookiesString = client.getCookiesString(); if (!"".equals(cookiesString)) { ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString); // Log_OC.d(TAG, "Saving Cookies: "+ cookiesString ); }//from w w w. ja v a2 s .c o m } }
From source file:edu.mit.mobile.android.locast.accounts.Authenticator.java
/** * Adds an account that acts as a placeholder to allow the sync to work properly. The sync * framework always requires an account. * * @param context//from ww w. j a va 2s . co m */ public static void addDemoAccount(Context context) { final Account[] accounts = Authenticator.getAccounts(context); if (accounts.length > 0) { // if there's already a demo account, we don't want to add another. // if there isn't, we're not going to delete the actual account. return; } final AccountManager am = AccountManager.get(context); final Account account = new Account(DEMO_ACCOUNT, AuthenticationService.ACCOUNT_TYPE); am.addAccountExplicitly(account, null, null); ContentResolver.setSyncAutomatically(account, MediaProvider.AUTHORITY, true); }
From source file:ir.keloud.android.lib.common.accounts.AccountUtils.java
/** * Restore the client cookies// ww w. j av a 2 s . c o m * @param account * @param client * @param context */ public static void restoreCookies(Account account, KeloudClient 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:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java
/** * @param context/* www . j a va 2 s .c o m*/ * @param accountType * @param key * @deprecated the first account shouldn't be assumed to be the active account * @return */ @Deprecated public static String getUserData(Context context, String accountType, String key) { final Account account = getFirstAccount(context, accountType); if (account == null) { throw new RuntimeException("no accounts registered"); } return AccountManager.get(context).getUserData(account, key); }
From source file:com.activiti.android.app.fragments.integration.alfresco.AlfrescoIntegrationFragment.java
private void updateUI() { hide(R.id.validation_panel);/* www .j a v a2 s . co m*/ // The idea is to get Integration Information and compare them to // alfresco account. if (checkActivity()) { return; } // Retrieve information from activiti Account if (getArguments() != null) { accountId = BundleUtils.getLong(getArguments(), ARGUMENT_ACCOUNT_ID); activitiAccount = ActivitiAccountManager.getInstance(getActivity()).getByAccountId(accountId); } // We have alfresco account integration // Is Alfresco APP Present ? // Is Alfresco Version recent ? PackageInfo info = getAlfrescoInfo(getActivity()); boolean outdated = (info != null && info.versionCode < 40); if (info == null || outdated) { // Alfresco APP is not present... // We request the installation from the play store titleTv.setText( getString(outdated ? R.string.settings_alfresco_update : R.string.settings_alfresco_install)); summaryTv.setText(Html.fromHtml(getString(outdated ? R.string.settings_alfresco_update_summary : R.string.settings_alfresco_install_summary))); actionButton.setText(getString(outdated ? R.string.settings_alfresco_update_action : R.string.settings_alfresco_install_action)); actionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startPlayStore(); } }); hide(R.id.validation_panel); return; } // Alfresco APP is present actionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { selectedAccount = null; launchAccountSelect(); } }); // Retrieve Alfresco Account Account[] accounts = AccountManager.get(getActivity()) .getAccountsByType(AlfrescoIntegrator.ALFRESCO_ACCOUNT_TYPE); if (accounts.length == 0 && selectedAccount == null) { // Need to create an alfresco account! titleTv.setText(getString(R.string.settings_alfresco_not_found)); summaryTv.setText(Html.fromHtml(getString(R.string.settings_alfresco_not_found_summary))); actionButton.setText(getString(R.string.settings_alfresco_not_found_action)); /* * actionButton.setOnClickListener(new View.OnClickListener() { * @Override public void onClick(View v) { selectedAccount = null; * Intent i = AlfrescoIntegrator.createAccount(getActivity(), * selectedIntegration.getRepositoryUrl(), * activitiAccount.getUsername()); startActivityForResult(i, 10); } * }); */ return; } // An activitiAccount match if username == username for (int i = 0; i < accounts.length; i++) { String username = AccountManager.get(getActivity()).getUserData(accounts[i], BuildConfig.ALFRESCO_ACCOUNT_ID.concat(".username")); if (activitiAccount.getUsername().equals(username)) { // Lets compare hostname String alfUrl = AccountManager.get(getActivity()).getUserData(accounts[i], BuildConfig.ALFRESCO_ACCOUNT_ID.concat(".url")); Uri alfUri = Uri.parse(alfUrl); Uri activitiUri = Uri.parse(selectedIntegration.getRepositoryUrl()); if (alfUri != null && activitiUri != null && alfUri.getHost().equals(activitiUri.getHost())) { // We found one ! selectedAccount = accounts[i]; break; } } } // Have found ? if (selectedAccount != null) { alfrescoId = AccountManager.get(getActivity()).getUserData(selectedAccount, BuildConfig.ALFRESCO_ACCOUNT_ID.concat(".id")); alfrescoUsername = AccountManager.get(getActivity()).getUserData(selectedAccount, BuildConfig.ALFRESCO_ACCOUNT_ID.concat(".username")); alfrescoAccountName = AccountManager.get(getActivity()).getUserData(selectedAccount, BuildConfig.ALFRESCO_ACCOUNT_ID.concat(".name")); titleTv.setText(getString(R.string.settings_alfresco_account_found)); summaryTv.setText( Html.fromHtml(String.format(getString(R.string.settings_alfresco_account_found_summary), alfrescoAccountName, alfrescoUsername))); actionButton.setText(getString(R.string.settings_alfresco_account_found_action)); show(R.id.validation_panel); Button validate = UIUtils.initValidation(getRootView(), R.string.general_action_confirm); validate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveIntegration(); } }); Button cancel = UIUtils.initCancel(getRootView(), R.string.general_action_cancel); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().onBackPressed(); } }); } else { titleTv.setText(getString(R.string.settings_alfresco_account_found)); summaryTv.setText(Html.fromHtml(getString(R.string.settings_alfresco_not_found_summary))); actionButton.setText(getString(R.string.settings_alfresco_not_found_action)); hide(R.id.validation_panel); } }
From source file:com.he5ed.lib.cloudprovider.auth.OAuth2Fragment.java
/** * Create a new user account or update the current user account * * @param user user information returned from server *///from w ww . j a v a 2s . co m private void addAccount(User user) { boolean accountExist = false; AccountManager am = AccountManager.get(getActivity()); // check if account already exist in AccountManager Account[] accounts = am.getAccountsByType(CloudProvider.ACCOUNT_TYPE); for (Account account : accounts) { if (account.name.equals(user.id)) { accountExist = true; break; } } Account account = new Account(user.id, CloudProvider.ACCOUNT_TYPE); Bundle userData = new Bundle(); // must be string value String accessToken = mTokenInfo.get(Authenticator.KEY_ACCESS_TOKEN); String refreshToken = mTokenInfo.get(Authenticator.KEY_REFRESH_TOKEN); String expiryDuration = mTokenInfo.get(Authenticator.KEY_EXPIRY); if (accountExist) { // update current account access token am.setAuthToken(account, CloudProvider.AUTH_TYPE, accessToken); if (refreshToken != null) am.setUserData(account, Authenticator.KEY_REFRESH_TOKEN, refreshToken); if (expiryDuration != null) am.setUserData(account, Authenticator.KEY_EXPIRY, expiryDuration); } else { // add new account into AccountManager if (refreshToken != null) userData.putString(Authenticator.KEY_REFRESH_TOKEN, refreshToken); if (expiryDuration != null) userData.putString(Authenticator.KEY_EXPIRY, expiryDuration); userData.putString(Authenticator.KEY_CLOUD_API, AuthHelper.getCloudApi(mCloudApi)); userData.putString(Authenticator.KEY_USERNAME, user.name); userData.putString(Authenticator.KEY_EMAIL, user.email); userData.putString(Authenticator.KEY_AVATAR_URL, user.avatarUrl); am.addAccountExplicitly(account, null, userData); am.setAuthToken(account, CloudProvider.AUTH_TYPE, accessToken); } // send result back to AccountManager Bundle result = new Bundle(); result.putString(AccountManager.KEY_ACCOUNT_NAME, user.id); result.putString(AccountManager.KEY_ACCOUNT_TYPE, CloudProvider.ACCOUNT_TYPE); ((AccountAuthenticatorActivity) getActivity()).setAccountAuthenticatorResult(result); getActivity().finish(); }
From source file:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java
public static String getUserData(Context context, Account account, String key) { if (account == null) { throw new RuntimeException("no accounts registered"); }/*from w w w . j ava2s . c o m*/ return AccountManager.get(context).getUserData(account, key); }
From source file:com.example.feedback.ActivityFeedback.java
/** * Get Google accounts from Android to populate spinner dropdown list. * Replace with custom method if application uses non-Google accounts. *//*from w ww . j a v a 2 s . c o m*/ public void getAccounts() { emailPattern = Patterns.EMAIL_ADDRESS; accountArray = AccountManager.get(getApplicationContext()).getAccounts(); accountList = new ArrayList<String>(); accountList.add(getResources().getString(R.string.feedback_anonymous)); for (Account accountItem : accountArray) { if (emailPattern.matcher(accountItem.name).matches() && accountItem.type.matches("com.google")) { accountList.add(accountItem.name); } } accountAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, accountList); }