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

/**
 * Create a new Account and activate the automatic sync
 * //  w  ww .j av a  2 s .c o  m
 * @param account
 *            null is not allowed
 * @param accountManager
 *            null is not allowed
 * @param password
 *            null is not allowed
 */
public static boolean createAccount(Context context, final Account account, AccountManager accountManager,
        String password) {
    boolean added = accountManager.addAccountExplicitly(account, password, null);
    if (added) {
        List<PeriodicSync> syncs = ContentResolver.getPeriodicSyncs(account, ContactsContract.AUTHORITY);
        if (syncs != null) {
            // Remove default syncs.
            for (PeriodicSync periodicSync : syncs) {
                ContentResolver.removePeriodicSync(account, ContactsContract.AUTHORITY, periodicSync.extras);
            }
        }
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        int synctime;
        try {
            synctime = settings.getInt("pref_synctime", DEFAULT_SYNCINTERVAL);
        } catch (ClassCastException e) {
            LogHelper.logI(TAG, "Invalid SyncTime-Settingvalue", e);
            synctime = DEFAULT_SYNCINTERVAL;
        }
        if (synctime != 0) {
            addPeriodicSync(ContactsContract.AUTHORITY, Bundle.EMPTY, synctime, context);
        }

        // Set contacts sync for this account.
        ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
    } else {
        LogHelper.logI(TAG, "Account " + account.name + " is already available.");
    }
    return added;
}

From source file:p1.nd.khan.jubair.mohammadd.popularmovies.sync.MovieSyncAdapter.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  .j  a va  2 s  . c o m*/
 */
public static Account getSyncAccount(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));
    if (null == accountManager.getPassword(newAccount)) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:Main.java

/**
 * Create a account for the sync adapter.
 *
 * @param _context The application context.
 * @return the new created Account./*from w  ww.  jav  a  2  s.  c  o m*/
 */
public static Account createSyncAccount(Context _context, String _username) {
    // create a new a account.
    Account newAccount = new Account(_username, ACCOUNT_TYPE);

    AccountManager accountManager = (AccountManager) _context.getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.equals(newAccount)) {
            // account already exists
            return null;
        }
    }

    // try to login and retrieve the token

    Bundle bundle = new Bundle();
    bundle.putString("Token", "some auth token please");

    if (accountManager.addAccountExplicitly(newAccount, null, bundle)) {

    } else {
        Log.i(TAG, "createSyncAccount: account already exists or an error happened");
    }

    return newAccount;
}

From source file:com.beesham.popularmovies.sync.MoviesSyncAdapter.java

private static Account getSyncAccount(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    if (accountManager.getPassword(newAccount) == null) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }// w w w  .j  a  v a 2  s  .  c o m
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:com.grepsound.activities.MainActivity.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 *//* www .j  a  v  a2 s .  co m*/
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);
    /*
     * 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.
         */
    } else {
        /*
         * The account exists or some other error occurred. Log this, report it,
         * or handle it internally.
         */
    }
    return newAccount;
}

From source file:io.github.hidroh.materialistic.data.SyncDelegate.java

@UiThread
public static void scheduleSync(Context context, Job job) {
    if (!Preferences.Offline.isEnabled(context)) {
        return;//www  . j a  v  a2  s.c  om
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !TextUtils.isEmpty(job.id)) {
        JobInfo.Builder builder = new JobInfo.Builder(Long.valueOf(job.id).intValue(),
                new ComponentName(context.getPackageName(), ItemSyncJobService.class.getName()))
                        .setRequiredNetworkType(
                                Preferences.Offline.isWifiOnly(context) ? JobInfo.NETWORK_TYPE_UNMETERED
                                        : JobInfo.NETWORK_TYPE_ANY)
                        .setExtras(job.toPersistableBundle());
        if (Preferences.Offline.currentConnectionEnabled(context)) {
            builder.setOverrideDeadline(0);
        }
        ((JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(builder.build());
    } else {
        Bundle extras = new Bundle(job.toBundle());
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        Account syncAccount;
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
        if (accounts.length == 0) {
            syncAccount = new Account(SYNC_ACCOUNT_NAME, BuildConfig.APPLICATION_ID);
            accountManager.addAccountExplicitly(syncAccount, null, null);
        } else {
            syncAccount = accounts[0];
        }
        ContentResolver.requestSync(syncAccount, MaterialisticProvider.PROVIDER_AUTHORITY, extras);
    }
}

From source file:com.murrayc.galaxyzoo.app.LoginUtils.java

/**
 * Add the anonymous Account.//from   ww  w.  j  a v  a 2  s  . c om
 *
 * Don't call this from the main thread - use an AsyncTask, for instance.
 * @param context
 */
private static void addAnonymousAccount(final Context context) {
    final AccountManager accountManager = AccountManager.get(context);
    final Account account = new Account(ACCOUNT_NAME_ANONYMOUS, LoginUtils.ACCOUNT_TYPE);
    //Note that this requires the AUTHENTICATE_ACCOUNTS permission on
    //SDK <=22:
    accountManager.addAccountExplicitly(account, null, null);

    //In case it has not been called yet.
    //This has no effect the second time.
    Utils.initDefaultPrefs(context);

    //Give the new account the existing (probably default) preferences,
    //so the SyncAdapter can use them.
    //See SettingsFragment.onSharedPreferenceChanged().
    copyPrefsToAccount(context, accountManager, account);

    //Tell the SyncAdapter to sync whenever the network is reconnected:
    setAutomaticAccountSync(context, account);
}

From source file:tech.salroid.filmy.syncs.FilmySyncAdapter.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 ww.ja  v a  2s  .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.creationgroundmedia.popularmovies.sync.MovieSyncAdapter.java

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)) {

        /*//from w ww .ja va2  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)) {
            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.rowland.hashtrace.sync.TweetHashTracerSyncAdapter.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.//ww w . ja v  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;
}