Example usage for android.accounts AccountManager addAccountExplicitly

List of usage examples for android.accounts AccountManager addAccountExplicitly

Introduction

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

Prototype

public boolean addAccountExplicitly(Account account, String password, Bundle userdata) 

Source Link

Document

Adds an account directly to the AccountManager.

Usage

From source file:com.ravi.apps.android.newsbytes.sync.NewsSyncAdapter.java

/**
 * Gets the fictitious account to be used with the sync adapter, or makes a new one
 * if the fictitious account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *//*from   w  ww .  j a va  2 s  .  com*/
public static Account getSyncAccount(Context context) {
    // Get an instance of the android account manager.
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account.
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist. Add the account.
    if (null == accountManager.getPassword(newAccount)) {
        // Add the account and account type, no password or user data.
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }

        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:co.carlosjimenez.android.currencyalerts.app.sync.ForexSyncAdapter.java

/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account./*from  w  w w  . j  a  v  a2  s.c om*/
 */
public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {

        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */

        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:com.emergencyskills.doe.aed.UI.activity.TabsActivity.java

public static Account CreateSyncAccount(Context context) {
    // Create the account type and default account
    Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
    /*/*from   www.  j  av a  2 s .c o  m*/
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call context.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
        return newAccount;
    } else {
        /*
         * The account exists or some other error occurred. Log this, report it,
         * or handle it internally.
         */
        return null;
    }
}

From source file:net.networksaremadeofstring.rhybudd.ViewZenossEventsListActivity.java

public static Account CreateSyncAccount(Context context) {
    try {//from   w w w  . jav  a2  s.  c  o  m
        // Create the account type and default account
        Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
        // Get an instance of the Android account manager
        AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (accountManager.addAccountExplicitly(newAccount, null, null)) {
            /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call context.setIsSyncable(account, AUTHORITY, 1)
             * here.
             */
            //Log.e("CreateSyncAccount","Success!");
        } else {
            /*
             * The account exists or some other error occurred. Log this, report it,
             * or handle it internally.
             */
            //Log.e("CreateSyncAccount","Fail!");
        }

        return newAccount;
    } catch (Exception e) {
        BugSenseHandler.sendExceptionMessage("ViewZenossEventsListActivity", "CreateSyncAccount", e);
        return null;
    }
}

From source file:com.adkdevelopment.earthquakesurvival.data.syncadapter.SyncAdapter.java

/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account.//  w  w  w.  jav  a2 s . c  om
 */
private static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {

        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */

        onAccountCreated(newAccount, context);

    }
    return newAccount;
}

From source file:com.upenn.chriswang1990.sunshine.sync.SunshineSyncAdapter.java

/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account./*from ww  w .j av  a 2 s  .c o  m*/
 */
public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));
    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {

        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:com.jackie.sunshine.app.sync.SunshineSyncAdapter.java

/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account.// www .j a v a2  s  .co m
 */
public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager manager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if (manager.getPassword(newAccount) == null) {
        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (!manager.addAccountExplicitly(newAccount, "", null))
            return null;

        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
        onAccountCreated(newAccount, context);
    }

    return newAccount;
}

From source file:org.birthdayadapter.util.AccountHelper.java

/**
 * Add account for Birthday Adapter to Android system
 *//*  w ww .j  a v a2  s  . c  om*/
public Bundle addAccountAndSync() {
    Log.d(Constants.TAG, "Adding account...");

    // enable automatic sync once per day
    ContentResolver.setSyncAutomatically(Constants.ACCOUNT, Constants.CONTENT_AUTHORITY, true);
    ContentResolver.setIsSyncable(Constants.ACCOUNT, Constants.ACCOUNT_TYPE, 1);

    // add periodic sync interval once per day
    long freq = AlarmManager.INTERVAL_DAY;
    ContentResolver.addPeriodicSync(Constants.ACCOUNT, Constants.ACCOUNT_TYPE, new Bundle(), freq);

    AccountManager am = AccountManager.get(mContext);
    if (am.addAccountExplicitly(Constants.ACCOUNT, null, null)) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, Constants.ACCOUNT.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT.type);

        // Force a sync! Even when background sync is disabled, this will force one sync!
        manualSync();

        return result;
    } else {
        return null;
    }
}

From source file:org.mozilla.gecko.fxa.authenticator.AndroidFxAccount.java

public static AndroidFxAccount addAndroidAccount(Context context, String email, String profile,
        String idpServerURI, String tokenServerURI, String profileServerURI, State state,
        final Map<String, Boolean> authoritiesToSyncAutomaticallyMap, final int accountVersion,
        final boolean fromPickle, ExtendedJSONObject bundle)
        throws UnsupportedEncodingException, GeneralSecurityException, URISyntaxException {
    if (email == null) {
        throw new IllegalArgumentException("email must not be null");
    }// w w  w  .  j  a  va2  s .  c  o  m
    if (profile == null) {
        throw new IllegalArgumentException("profile must not be null");
    }
    if (idpServerURI == null) {
        throw new IllegalArgumentException("idpServerURI must not be null");
    }
    if (tokenServerURI == null) {
        throw new IllegalArgumentException("tokenServerURI must not be null");
    }
    if (profileServerURI == null) {
        throw new IllegalArgumentException("profileServerURI must not be null");
    }
    if (state == null) {
        throw new IllegalArgumentException("state must not be null");
    }

    // TODO: Add migration code.
    if (accountVersion != CURRENT_ACCOUNT_VERSION) {
        throw new IllegalStateException("Could not create account of version " + accountVersion
                + ". Current version is " + CURRENT_ACCOUNT_VERSION + ".");
    }

    // Android has internal restrictions that require all values in this
    // bundle to be strings. *sigh*
    Bundle userdata = new Bundle();
    userdata.putString(ACCOUNT_KEY_ACCOUNT_VERSION, "" + CURRENT_ACCOUNT_VERSION);
    userdata.putString(ACCOUNT_KEY_IDP_SERVER, idpServerURI);
    userdata.putString(ACCOUNT_KEY_TOKEN_SERVER, tokenServerURI);
    userdata.putString(ACCOUNT_KEY_PROFILE_SERVER, profileServerURI);
    userdata.putString(ACCOUNT_KEY_PROFILE, profile);

    if (bundle == null) {
        bundle = new ExtendedJSONObject();
        // TODO: How to upgrade?
        bundle.put(BUNDLE_KEY_BUNDLE_VERSION, CURRENT_BUNDLE_VERSION);
    }
    bundle.put(BUNDLE_KEY_STATE_LABEL, state.getStateLabel().name());
    bundle.put(BUNDLE_KEY_STATE, state.toJSONObject().toJSONString());

    userdata.putString(ACCOUNT_KEY_DESCRIPTOR, bundle.toJSONString());

    Account account = new Account(email, FxAccountConstants.ACCOUNT_TYPE);
    AccountManager accountManager = AccountManager.get(context);
    // We don't set an Android password, because we don't want to persist the
    // password (or anything else as powerful as the password). Instead, we
    // internally manage a sessionToken with a remotely owned lifecycle.
    boolean added = accountManager.addAccountExplicitly(account, null, userdata);
    if (!added) {
        return null;
    }

    // Try to work around an intermittent issue described at
    // http://stackoverflow.com/a/11698139.  What happens is that tests that
    // delete and re-create the same account frequently will find the account
    // missing all or some of the userdata bundle, possibly due to an Android
    // AccountManager caching bug.
    for (String key : userdata.keySet()) {
        accountManager.setUserData(account, key, userdata.getString(key));
    }

    AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);

    if (!fromPickle) {
        fxAccount.clearSyncPrefs();
    }

    fxAccount.setAuthoritiesToSyncAutomaticallyMap(authoritiesToSyncAutomaticallyMap);

    return fxAccount;
}

From source file:com.katamaditya.apps.weather4u.weathersync.Weather4USyncAdapter.java

/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account.//w ww .jav a 2 s .  c o  m
 */
public static Account getSyncAccount(Context context) {
    //Log.d("Weather4USyncAdapter", "getSyncAccount");
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {

        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */

        onAccountCreated(newAccount, context);
    }
    return newAccount;
}