Example usage for android.content ContentResolver setSyncAutomatically

List of usage examples for android.content ContentResolver setSyncAutomatically

Introduction

In this page you can find the example usage for android.content ContentResolver setSyncAutomatically.

Prototype

public static void setSyncAutomatically(Account account, String authority, boolean sync) 

Source Link

Document

Set whether or not the provider is synced when it receives a network tickle.

Usage

From source file:com.nextgis.forestinspector.activity.MainActivity.java

@Override
public void onAddAccount(Account account, String token, boolean accountAdded) {
    if (accountAdded) {

        //free any map data here
        final MainApplication app = (MainApplication) getApplication();
        MapBase map = app.getMap();/*from w w w  .j  a  v a 2 s .  c o m*/

        // delete all layers from map if any
        map.delete();

        //set sync with server
        ContentResolver.setSyncAutomatically(account, app.getAuthority(), true);

        // goto step 2
        refreshActivityView();
    } else
        Toast.makeText(this, R.string.error_init, Toast.LENGTH_SHORT).show();
}

From source file:com.odoo.MainActivity.java

/**
 * Sets the auto sync./*  w ww  .  j av a  2s  .  c o  m*/
 * 
 * @param authority
 *            the authority
 * @param isON
 *            the is on
 */
public void setAutoSync(String authority, boolean isON) {
    try {
        Account account = OdooAccountManager.getAccount(this, OUser.current(mContext).getAndroidName());
        if (!ContentResolver.isSyncActive(account, authority)) {
            ContentResolver.setSyncAutomatically(account, authority, isON);
        }
    } catch (NullPointerException eNull) {

    }
}

From source file:org.voidsink.anewjkuapp.activity.KusssAuthenticatorActivity.java

private void finishLogin(Intent intent) {
    String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
    String accountType = intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);

    Log.i(TAG, "finish login to " + accountName);

    Account account;//from   www . j  a  v  a  2 s .  c o  m
    boolean addNewAccount;

    final Account oldAccount = AppUtils.getAccount(this);
    if (oldAccount != null) {
        if (oldAccount.name.equals(accountName)) {
            account = oldAccount;
            addNewAccount = false;
        } else {
            AppUtils.removeAccout(mAccountManager, oldAccount);
            account = new Account(accountName, accountType);
            addNewAccount = true;
        }
    } else {
        account = new Account(accountName, accountType);
        addNewAccount = true;
    }

    if (addNewAccount) {
        String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
        String authtokenType = mAuthTokenType;

        // Creating the account on the device and setting the auth token we
        // got
        // (Not setting the auth token will cause another call to the server
        // to authenticate the user)
        mAccountManager.addAccountExplicitly(account, accountPassword, null);
        mAccountManager.setAuthToken(account, authtokenType, authtoken);
        mAccountManager.setPassword(account, accountPassword);

        // Turn on periodic syncing
        long interval = PreferenceWrapper.getSyncInterval(this) * 60 * 60;

        ContentResolver.addPeriodicSync(account, CalendarContractWrapper.AUTHORITY(), new Bundle(), interval);
        ContentResolver.addPeriodicSync(account, KusssContentContract.AUTHORITY, new Bundle(), interval);
        // Inform the system that this account supports sync
        ContentResolver.setIsSyncable(account, CalendarContractWrapper.AUTHORITY(), 1);
        ContentResolver.setIsSyncable(account, KusssContentContract.AUTHORITY, 1);
        // Inform the system that this account is eligible for auto sync
        // when the network is up
        ContentResolver.setSyncAutomatically(account, CalendarContractWrapper.AUTHORITY(), true);
        ContentResolver.setSyncAutomatically(account, KusssContentContract.AUTHORITY, true);
        // Recommend a schedule for automatic synchronization. The system
        // may modify this based
        // on other scheduled syncs and network utilization.
    } else {
        mAccountManager.setPassword(account, accountPassword);
    }

    // Kalender aktualisieren
    CalendarUtils.createCalendarsIfNecessary(this, account);

    // Sync NOW
    KusssAuthenticator.triggerSync(this);

    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);

    finish();
}

From source file:edu.mit.mobile.android.locast.accounts.AuthenticatorActivity.java

/**
 *
 * Called when response is received from the server for authentication request. See
 * onAuthenticationResult(). Sets the AccountAuthenticatorResult which is sent back to the
 * caller. Also sets the authToken in AccountManager for this account.
 *
 * @param userData/*from  w  ww .ja v  a2s  . c o m*/
 *            TODO
 * @param the
 *            confirmCredentials result.
 */

protected void finishLogin(Bundle userData) {
    Log.i(TAG, "finishLogin()");
    // ensure that there isn't a demo account sticking around.

    // TODO this is NOT the place where this code belongs. Find it a better home
    if (Authenticator.isDemoMode(this)) {
        Log.d(TAG, "cleaning up demo mode account...");
        ContentResolver.cancelSync(Authenticator.getFirstAccount(this), MediaProvider.AUTHORITY);

        mAccountManager.removeAccount(
                new Account(Authenticator.DEMO_ACCOUNT, AuthenticationService.ACCOUNT_TYPE),
                new AccountManagerCallback<Boolean>() {

                    @Override
                    public void run(AccountManagerFuture<Boolean> arg0) {
                        try {
                            if (arg0.getResult()) {

                                final ContentValues cv = new ContentValues();
                                // invalidate all the content to force a sync.
                                // this is to ensure that items which were marked favorite get set as
                                // such.
                                cv.put(Cast._SERVER_MODIFIED_DATE, 0);
                                cv.put(Cast._MODIFIED_DATE, 0);
                                getContentResolver().update(Cast.CONTENT_URI, cv, null, null);
                                if (Constants.DEBUG) {
                                    Log.d(TAG, "reset all cast modified dates to force a reload");
                                }
                            }
                        } catch (final OperationCanceledException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (final AuthenticatorException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (final IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }, null);

    }
    final Account account = new Account(mUsername, AuthenticationService.ACCOUNT_TYPE);

    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, mPassword, userData);
        // Automatically enable sync for this account
        ContentResolver.setSyncAutomatically(account, MediaProvider.AUTHORITY, true);
    } else {
        mAccountManager.setPassword(account, mPassword);
    }
    final Intent intent = new Intent();
    mAuthtoken = mPassword;
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AuthenticationService.ACCOUNT_TYPE);
    if (mAuthtokenType != null && mAuthtokenType.equals(AuthenticationService.AUTHTOKEN_TYPE)) {
        intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthtoken);
    }
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:eu.iescities.pilot.rovereto.roveretoexplorer.custom.data.DTHelper.java

/**
 * Enable auot sync for the activity life-cycle
 * //from w  w  w.j  a  v a2 s  . c o m
 * @throws NameNotFoundException
 * @throws DataException
 */
public static void activateAutoSync() {
    try {
        String authority = Constants.getAuthority(mContext);
        Account account = new Account(eu.trentorise.smartcampus.ac.Constants.getAccountName(mContext),
                eu.trentorise.smartcampus.ac.Constants.getAccountType(mContext));

        ContentResolver.setIsSyncable(account, authority, 1);
        ContentResolver.setSyncAutomatically(account, authority, true);
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), Constants.SYNC_INTERVAL * 60);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticatorActivity.java

/**
 *
 * Called when response is received from the server for authentication request. See
 * onAuthenticationResult(). Sets the AccountAuthenticatorResult which is sent back to the
 * caller. Also sets the authToken in AccountManager for this account.
 *
 * @param userData/*from www.  j  a  v a2s  .  com*/
 *            TODO
 * @param the
 *            confirmCredentials result.
 */

protected void finishLogin(Bundle userData) {
    if (BuildConfig.DEBUG) {
        Log.i(TAG, "finishLogin()");
    }

    final Account account = createAccount(mUsername);

    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, mPassword, userData);
        // Automatically enable sync for this account
        ContentResolver.setSyncAutomatically(account, getAuthority(), true);
    } else {
        mAccountManager.setPassword(account, mPassword);
    }
    final Intent intent = new Intent();
    mAuthtoken = mPassword;
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, getAccountType());
    if (mAuthtokenType != null && mAuthtokenType.equals(getAuthtokenType())) {
        intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthtoken);
    }
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:com.clearcenter.mobile_demo.mdAuthenticatorActivity.java

private void finishLogin(String authToken) {
    Log.i(TAG, "finishLogin()");

    final Account account = new Account(nickname, mdConstants.ACCOUNT_TYPE);
    if (request_new_account) {
        account_manager.addAccountExplicitly(account, authToken, null);
        account_manager.setUserData(account, "hostname", hostname);
        account_manager.setUserData(account, "username", username);

        // Set system info sync for this account.
        final Bundle extras = new Bundle();
        ContentResolver.setSyncAutomatically(account, mdContentProvider.AUTHORITY, true);
        if (android.os.Build.VERSION.SDK_INT >= 8) {
            ContentResolver.addPeriodicSync(account, mdContentProvider.AUTHORITY, extras, 10);
        }/* w w w .  j av  a 2s .  com*/
    } else
        account_manager.setPassword(account, authToken);

    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, nickname);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, mdConstants.ACCOUNT_TYPE);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);

    ContentResolver.requestSync(account, mdContentProvider.AUTHORITY, new Bundle());

    finish();
}

From source file:com.ntsync.android.sync.shared.SyncUtils.java

/**
 * Create a new Account and activate the automatic sync
 * // ww w .ja  v 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:com.adkdevelopment.earthquakesurvival.data.syncadapter.SyncAdapter.java

private static void onAccountCreated(Account newAccount, Context context) {
    /*/*from w  w w .j  a va2 s.  c o m*/
     * Since we've created an account
     */
    SyncAdapter.configurePeriodicSync(context, Utilities.getSyncIntervalPrefs(context),
            Utilities.getSyncIntervalPrefs(context) / 3);

    /*
     * Without calling setSyncAutomatically, our periodic sync will not be enabled.
     */
    ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.sync_provider_authority), true);

    /*
     * Finally, let's do a sync to get things started
     */
    syncImmediately(context);
}

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

/**
 * Helper method to schedule the sync adapter periodic execution
 *///from w ww.  ja  v  a2  s . c  om
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    Account account = getSyncAccount(context);
    String authority = context.getString(R.string.sync_provider_authority);
    if (syncInterval == -1) {
        ContentResolver.setSyncAutomatically(account, context.getString(R.string.sync_provider_authority),
                false);
    } else {
        ContentResolver.setSyncAutomatically(account, context.getString(R.string.sync_provider_authority),
                true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // we can enable inexact timers in our periodic sync
            SyncRequest request = new SyncRequest.Builder().syncPeriodic(syncInterval, flexTime)
                    .setSyncAdapter(account, authority).setExtras(new Bundle()).build();
            ContentResolver.requestSync(request);
        } else {
            ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
        }
    }
}