Example usage for android.content ContentResolver addPeriodicSync

List of usage examples for android.content ContentResolver addPeriodicSync

Introduction

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

Prototype

public static void addPeriodicSync(Account account, String authority, Bundle extras, long pollFrequency) 

Source Link

Document

Specifies that a sync should be requested with the specified the account, authority, and extras at the given frequency.

Usage

From source file:at.bitfire.davdroid.AccountSettings.java

public void setSyncInterval(@NonNull String authority, long seconds) {
    if (seconds == SYNC_INTERVAL_MANUALLY) {
        ContentResolver.setSyncAutomatically(account, authority, false);
    } else {/*from   www. ja v a  2s. c  o m*/
        ContentResolver.setSyncAutomatically(account, authority, true);
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), seconds);
    }
}

From source file:pt.up.mobile.authenticator.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. We store the
 * authToken that's returned from the server as the 'password' for this
 * account - so we're never storing the user's actual password locally.
 * /* w ww .  java2  s  .c  o  m*/
 * @param result
 *            the confirmCredentials result.
 */
@TargetApi(8)
private void finishLogin(final User user) {

    Log.i(TAG, "finishLogin()");
    new Thread(new Runnable() {
        @Override
        public void run() {
            final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
            if (mRequestNewAccount) {
                final ContentValues values = new ContentValues();
                values.put(SigarraContract.Users.CODE, user.getUserCode());
                values.put(SigarraContract.Users.TYPE, user.getType());
                values.put(SigarraContract.Users.ID, account.name);
                getContentResolver().insert(SigarraContract.Users.CONTENT_URI, values);
                // Set contacts sync for this account.

                if (!mAccountManager.addAccountExplicitly(account, mPassword, Bundle.EMPTY)) {
                    getContentResolver().delete(SigarraContract.Users.CONTENT_URI,
                            SigarraContract.Users.PROFILE,
                            SigarraContract.Users.getUserSelectionArgs(account.name));
                    finish();
                }
                String syncIntervalValue = PreferenceManager
                        .getDefaultSharedPreferences(getApplicationContext())
                        .getString(getString(R.string.key_sync_interval),
                                Integer.toString(getResources().getInteger(R.integer.default_sync_interval)));

                String syncNotIntervalValue = PreferenceManager
                        .getDefaultSharedPreferences(getApplicationContext())
                        .getString(getString(R.string.key_notifications_sync_interval),
                                Integer.toString(getResources().getInteger(R.integer.default_sync_interval)));

                ContentResolver.setSyncAutomatically(account, SigarraContract.CONTENT_AUTHORITY, true);
                ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                    ContentResolver.addPeriodicSync(account, SigarraContract.CONTENT_AUTHORITY, Bundle.EMPTY,
                            Integer.parseInt(syncIntervalValue) * 3600);
                    ContentResolver.addPeriodicSync(account, SigarraContract.CONTENT_AUTHORITY,
                            SigarraSyncAdapterUtils.getNotificationsPeriodicBundle(),
                            Integer.parseInt(syncNotIntervalValue) * 3600);
                } else {
                    PeriodicSyncReceiver.cancelPreviousAlarms(getApplicationContext(), account,
                            SigarraContract.CONTENT_AUTHORITY, Bundle.EMPTY);
                    PeriodicSyncReceiver.addPeriodicSync(getApplicationContext(), account,
                            SigarraContract.CONTENT_AUTHORITY, Bundle.EMPTY,
                            Integer.parseInt(syncIntervalValue) * 3600);

                    PeriodicSyncReceiver.addPeriodicSync(getApplicationContext(), account,
                            SigarraContract.CONTENT_AUTHORITY, SigarraSyncAdapterUtils.getNotificationsBundle(),
                            Integer.parseInt(syncNotIntervalValue) * 3600);
                }
            } else {
                mAccountManager.setPassword(account, user.getPassword());
            }
            final Intent intent = new Intent();
            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            setAccountAuthenticatorResult(intent.getExtras());
            setResult(RESULT_OK, intent);
            finish();
        }
    }).start();
}

From source file:com.gaba.alex.trafficincidents.MainActivity.java

private void configurePeriodicSync(Account appAccount) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    int hourlySyncInterval = Integer
            .parseInt(preferences.getString(getString(R.string.pref_auto_refresh_key), "6"));
    if (hourlySyncInterval == 0) {
        ContentResolver.setSyncAutomatically(appAccount, AUTHORITY, false);
    } else {/*from   www  .j a v a 2s.com*/
        long syncInterval = hourlySyncInterval * SECONDS_PER_HOUR;
        ContentResolver.setSyncAutomatically(appAccount, AUTHORITY, true);
        ContentResolver.addPeriodicSync(appAccount, AUTHORITY, Bundle.EMPTY, syncInterval);
    }
}

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  w ww  .ja v  a2 s  . com*/
    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:com.example.igorklimov.popularmoviesdemo.sync.SyncAdapter.java

private static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    String authority = context.getString(R.string.content_authority);
    Account account = getSyncAccount(context);
    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 {/*from w  w  w  .  j  a v  a  2 s  .c  o  m*/
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
    }
}

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

/**
 * Enable auot sync for the activity life-cycle
 * // www.j av a 2s .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: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);
        }//from ww  w. j  a  va2  s  .  c o  m
    } 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.odoo.MainActivity.java

/**
 * Sets the sync periodic./*w  ww  .  j a  v a 2 s.co m*/
 * 
 * @param authority
 *            the authority
 * @param interval_in_minute
 *            the interval_in_minute
 * @param seconds_per_minute
 *            the seconds_per_minute
 * @param milliseconds_per_second
 *            the milliseconds_per_second
 */
public void setSyncPeriodic(String authority, long interval_in_minute, long seconds_per_minute,
        long milliseconds_per_second) {
    Account account = OdooAccountManager.getAccount(this, OUser.current(mContext).getAndroidName());
    Bundle extras = new Bundle();
    this.setAutoSync(authority, true);
    ContentResolver.setIsSyncable(account, authority, 1);
    final long sync_interval = interval_in_minute * seconds_per_minute * milliseconds_per_second;
    ContentResolver.addPeriodicSync(account, authority, extras, sync_interval);

}

From source file:com.jasonmheim.rollout.station.CoreContentProvider.java

private void setSyncPeriod(double periodInMinutes) {
    Log.i("Rollout", "Data update period in minutes: " + periodInMinutes);
    ContentResolver.addPeriodicSync(ACCOUNT, AUTHORITY, Bundle.EMPTY,
            (long) (periodInMinutes * Constants.SECONDS_PER_MINUTE));
}

From source file:com.sharpcart.android.wizardpager.SharpCartLoginActivity.java

private void createAccount(final String mUsername, final String mPassword) {
    final Account account = new Account(mUsername, PARAM_ACCOUNT_TYPE);

    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, mPassword, null);

        /*/*from w  w w . j a v a2  s. c  o m*/
         * Turn on periodic syncing. I need to add randomness to the sync interval to make sure 
         * that not all users sync at the same time, which will overload the server.
         */

        final Bundle extras = new Bundle();
        final long random = (long) (Math.random() * 1000L);

        ContentResolver.addPeriodicSync(account, SharpCartContentProvider.AUTHORITY, extras,
                (8 * SYNC_INTERVAL) + random);

        //Will run the syncadapter everytime we get a network tinkle
        //ContentResolver.setSyncAutomatically(account,SharpCartContentProvider.AUTHORITY, true);

        //initiate a sync
        SharpCartUtilities.getInstance().syncFromServer(account);

    } else {
        mAccountManager.setPassword(account, mPassword);
    }

    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, PARAM_ACCOUNT_TYPE);

    if (mAuthTokenType != null && mAuthTokenType.equals(PARAM_AUTHTOKEN_TYPE)) {
        intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthToken);
    }

    setResult(RESULT_OK, intent);

    finish();
}