Java Code Examples for android.content.ContentResolver#addPeriodicSync()

The following examples show how to use android.content.ContentResolver#addPeriodicSync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SunshineSyncAdapter.java    From Krishi-Seva with MIT License 6 votes vote down vote up
/**
 * Helper method to schedule the sync adapter periodic execution
 */
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    Account account = getSyncAccount(context);
    String authority = context.getString(R.string.content_authority);
    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);
    }
}
 
Example 2
Source File: WoodminSyncAdapter.java    From Woodmin with Apache License 2.0 6 votes vote down vote up
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    Account account = getSyncAccount(context);
    String authority = context.getString(R.string.content_authority);
    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);
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
    } else {
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
    }
}
 
Example 3
Source File: SyncUtils.java    From v2ex with Apache License 2.0 6 votes vote down vote up
/**
 * Create an entry for this application in the system account list, if it isn't already there.
 *
 * @param context Context
 */
public static void createSyncAccount(Context context) {

    // Create account, if it's missing. (Either first run, or user has deleted account.)
    Account account = AccountUtils.getActiveAccount(context);
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    accountManager.addAccountExplicitly(account, null, null);
    // Inform the system that this account supports sync
    ContentResolver.setIsSyncable(account, V2exContract.CONTENT_AUTHORITY, 1);
    // Inform the system that this account is eligible for auto sync when the network is up
    ContentResolver.setSyncAutomatically(account, V2exContract.CONTENT_AUTHORITY, true);

    Bundle extras = new Bundle();

    extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false);
    extras.putBoolean(SyncAdapter.EXTRA_SYNC_REMOTE, true);

    // Recommend a schedule for automatic synchronization. The system may modify this based
    // on other scheduled syncs and network utilization.
    ContentResolver.addPeriodicSync(
            account, V2exContract.CONTENT_AUTHORITY, extras, SYNC_FREQUENCY);
}
 
Example 4
Source File: SunshineSyncAdapter.java    From Advanced_Android_Development with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to schedule the sync adapter periodic execution
 */
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    Account account = getSyncAccount(context);
    String authority = context.getString(R.string.content_authority);
    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);
    }
}
 
Example 5
Source File: AccountResolver.java    From RememBirthday with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Add account for Birthday Adapter to Android system
 */
public Bundle addAccountAndSync() {
    Log.d(getClass().getSimpleName(), "Adding calendar account : " + account.name);

    // enable automatic sync once per day
    ContentResolver.setSyncAutomatically(account, authority, true);
    ContentResolver.setIsSyncable(account, type, 1);

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

    AccountManager accountManager = AccountManager.get(context);
    if (accountManager.addAccountExplicitly(account, null, null)) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);

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

        return result;
    } else {
        return null;
    }
}
 
Example 6
Source File: Accounts.java    From cathode with Apache License 2.0 6 votes vote down vote up
public static void setupAccount(Context context) {
  AccountManager manager = AccountManager.get(context);

  Account account = getAccount(context);

  try {
    if (manager.addAccountExplicitly(account, null, null)) {
      ContentResolver.setIsSyncable(account, BuildConfig.AUTHORITY_DUMMY_CALENDAR, 1);
      ContentResolver.setSyncAutomatically(account, BuildConfig.AUTHORITY_DUMMY_CALENDAR, true);
      ContentResolver.addPeriodicSync(account, BuildConfig.AUTHORITY_DUMMY_CALENDAR, new Bundle(),
          12 * 60 * 60 /* 12 hours in seconds */);
    }
  } catch (SecurityException e) {
    Timber.e(e, "Unable to add account");
  }
}
 
Example 7
Source File: AccountManagerHelper.java    From moVirt with Apache License 2.0 6 votes vote down vote up
public void updatePeriodicSync(MovirtAccount account, boolean syncEnabled) {
    ObjectUtils.requireNotNull(account, "account");
    String authority = OVirtContract.CONTENT_AUTHORITY;
    Bundle bundle = Bundle.EMPTY;

    try {
        ContentResolver.setSyncAutomatically(account.getAccount(), OVirtContract.CONTENT_AUTHORITY, syncEnabled);

        if (syncEnabled) {
            long intervalInSeconds = environmentStore.getSharedPreferencesHelper(account).getPeriodicSyncInterval() * (long) Constants.SECONDS_IN_MINUTE;
            ContentResolver.addPeriodicSync(account.getAccount(), authority, bundle, intervalInSeconds);
        } else {
            ContentResolver.removePeriodicSync(account.getAccount(), authority, bundle);
        }
    } catch (AccountDeletedException ignored) {
    }
}
 
Example 8
Source File: SyncAdapter.java    From react-native-sync-adapter with MIT License 6 votes vote down vote up
/**
 * Based on https://gist.github.com/udacityandroid/7230489fb8cb3f46afee
 */
private static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    Account account = getSyncAccount(context, syncInterval, flexTime);
    String authority = context.getString(R.string.rnsb_content_authority);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // We can enable inexact timers in our periodic sync (better for batter life)
        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);
    }
}
 
Example 9
Source File: SyncAdapter.java    From gito-github-client with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to schedule the sync adapter periodic execution
 */
private static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        SyncRequest request = new SyncRequest.Builder().
                syncPeriodic(syncInterval, flexTime).
                setSyncAdapter(mAccount, context.getString(R.string.sync_authority)).
                setExtras(new Bundle()).build();
        ContentResolver.requestSync(request);
    } else {
        ContentResolver.addPeriodicSync(mAccount,
                context.getString(R.string.sync_authority), new Bundle(), syncInterval);
    }
}
 
Example 10
Source File: PredatorSyncAdapter.java    From Capstone-Project with MIT License 5 votes vote down vote up
public static void initializePeriodicSync(Context context, long syncIntervalInSeconds) {
    // Enable Sync
    ContentResolver.setIsSyncable(PredatorAccount.getAccount(context),
            Constants.Authenticator.PREDATOR_ACCOUNT_TYPE,
            Constants.Sync.ON);

    // Set periodic sync interval
    Logger.d(TAG, "initializePeriodicSync: interval(seconds): " + syncIntervalInSeconds);
    ContentResolver.addPeriodicSync(
            PredatorAccount.getAccount(context),
            Constants.Authenticator.PREDATOR_ACCOUNT_TYPE,
            Bundle.EMPTY,
            syncIntervalInSeconds);
}
 
Example 11
Source File: SyncUtils.java    From QuantumFlux with Apache License 2.0 5 votes vote down vote up
/**
 * Turn on periodic syncing
 */
public static void startPeriodicSync(Account account) {
    ContentResolver.addPeriodicSync(
            account,
            SyncConstants.AUTHORITY,
            Bundle.EMPTY,
            SYNC_INTERVAL);
}
 
Example 12
Source File: SyncUtils.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setSyncPeriodic(String authority, long interval_in_minute,
                            long seconds_per_minute, long milliseconds_per_second) {
    Account account = mUser.getAccount();
    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);

}
 
Example 13
Source File: AbstractBufferResolver.java    From TimberLorry with Apache License 2.0 5 votes vote down vote up
@Override
public void scheduleSync(long period) {
    // TODO: consider the condition that master sync is disabled.
    if (!Utils.isAccountAdded(accountManager, account.type)) {
        accountManager.addAccountExplicitly(account, null, null);
    }
    ContentResolver.setSyncAutomatically(account, BufferProvider.AUTHORITY, true);
    ContentResolver.addPeriodicSync(account, BufferProvider.AUTHORITY, Bundle.EMPTY, period);
}
 
Example 14
Source File: SyncPopup.java    From haxsync with GNU General Public License v2.0 5 votes vote down vote up
private void setSyncRate(long seconds){
	AccountManager am = AccountManager.get(this);
	Account account = am.getAccountsByType("org.mots.haxsync.account")[0];
	ContentResolver.addPeriodicSync(account, ContactsContract.AUTHORITY, new Bundle(), seconds);
	ContentResolver.addPeriodicSync(account, CalendarContract.AUTHORITY, new Bundle(), seconds);
	SharedPreferences.Editor editor = prefs.edit();
	editor.putLong("sync_seconds", seconds);
	editor.commit();
}
 
Example 15
Source File: SyncAdapter.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressLint("MissingPermission")
public static void setSyncPeriod(
        IGISApplication application,
        Bundle extras,
        long pollFrequency)
{
    Context context = ((Context) application).getApplicationContext();
    final AccountManager accountManager = AccountManager.get(context);
    Log.d(TAG, "SyncAdapter: AccountManager.get(" + context + ")");

    for (Account account : accountManager.getAccountsByType(application.getAccountsType())) {
        ContentResolver.addPeriodicSync(account, application.getAuthority(), extras, pollFrequency);
    }
}
 
Example 16
Source File: SyncUtils.java    From android-BasicSyncAdapter with Apache License 2.0 5 votes vote down vote up
/**
 * Create an entry for this application in the system account list, if it isn't already there.
 *
 * @param context Context
 */
@TargetApi(Build.VERSION_CODES.FROYO)
public static void CreateSyncAccount(Context context) {
    boolean newAccount = false;
    boolean setupComplete = PreferenceManager
            .getDefaultSharedPreferences(context).getBoolean(PREF_SETUP_COMPLETE, false);

    // Create account, if it's missing. (Either first run, or user has deleted account.)
    Account account = GenericAccountService.GetAccount(ACCOUNT_TYPE);
    AccountManager accountManager =
            (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    if (accountManager.addAccountExplicitly(account, null, null)) {
        // Inform the system that this account supports sync
        ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
        // Inform the system that this account is eligible for auto sync when the network is up
        ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
        // Recommend a schedule for automatic synchronization. The system may modify this based
        // on other scheduled syncs and network utilization.
        ContentResolver.addPeriodicSync(
                account, CONTENT_AUTHORITY, new Bundle(),SYNC_FREQUENCY);
        newAccount = true;
    }

    // Schedule an initial sync if we detect problems with either our account or our local
    // data has been deleted. (Note that it's possible to clear app data WITHOUT affecting
    // the account list, so wee need to check both.)
    if (newAccount || !setupComplete) {
        TriggerRefresh();
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putBoolean(PREF_SETUP_COMPLETE, true).commit();
    }
}
 
Example 17
Source File: SyncAdapterHelper.java    From COCOFramework with Apache License 2.0 5 votes vote down vote up
public void periodicSync(Account mAccount, int minutes) {
    // Get the content resolver for your app
    context.getContentResolver();
    /*
     * Turn on periodic syncing
     */
    ContentResolver.addPeriodicSync(
            mAccount,
            AUTHORITY,
            Bundle.EMPTY,
            minutes * 60);
}
 
Example 18
Source File: Listactivity.java    From ImapNote2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    ((TextView) this.accountSpinner.getSelectedView()).setBackgroundColor(0xFFB6B6B6);
    Account account = Listactivity.accounts[pos];
    // Check periodic sync. If set to 86400 (once a day), set it to 900 (15 minutes)
    // this is due to bad upgrade to v4 which handles offline mode and syncing
    // Remove this code after V4.0 if version no more used
    List<PeriodicSync> currentSyncs = ContentResolver.getPeriodicSyncs (account, AUTHORITY);
    for (PeriodicSync onesync : currentSyncs) {
        if (onesync.period == 86400) {
            ContentResolver.setIsSyncable(account, AUTHORITY, 1);
            ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
            ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 60);
            Toast.makeText(getApplicationContext(), "Recreating this account is recommended to manage sync interval. Set to 15 minutes in the meantime",
                    Toast.LENGTH_LONG).show();
        }
    }
    // End of code
    Listactivity.imapNotes2Account.SetAccountname(account.name);
    Listactivity.imapNotes2Account.SetUsername(Listactivity.accountManager.getUserData (account, "username"));
    String pwd = Listactivity.accountManager.getPassword(account);
    Listactivity.imapNotes2Account.SetPassword(pwd);
    Listactivity.imapNotes2Account.SetServer(Listactivity.accountManager.getUserData (account, "server"));
    Listactivity.imapNotes2Account.SetPortnum(Listactivity.accountManager.getUserData (account, "portnum"));
    Listactivity.imapNotes2Account.SetSecurity(Listactivity.accountManager.getUserData (account, "security"));
    Listactivity.imapNotes2Account.SetUsesticky(accountManager.getUserData (account, "usesticky"));
    Listactivity.imapNotes2Account.SetSyncinterval(Listactivity.accountManager.getUserData (account, "syncinterval"));
    Listactivity.imapNotes2Account.SetaccountHasChanged();
    Listactivity.imapNotes2Account.SetAccount(account);
    this.RefreshList();
}
 
Example 19
Source File: LoginActivity.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
private void finishLogin(Intent intent) {
    Log.d("aptoide", TAG + "> finishLogin");

    String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
    final String accountType = intent.hasExtra(ARG_ACCOUNT_TYPE)
            ? intent.getStringExtra(ARG_ACCOUNT_TYPE)
            : Aptoide.getConfiguration().getAccountType();

    final Account account = new Account(accountName, accountType);

    if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false)) {
        Log.d("aptoide", TAG + "> finishLogin > addAccountExplicitly");
        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);

    } else {
        Log.d("aptoide", TAG + "> finishLogin > setPassword");
        mAccountManager.setPassword(account, accountPassword);
    }

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

    if (fromPreviousAptoideVersion) {
        PreferenceManager.getDefaultSharedPreferences(this).edit().remove(Constants.LOGIN_USER_LOGIN).commit();
    }
    finish();
    if (registerDevice != null && registerDevice.isChecked() && hasQueue)
        startService(new Intent(this, RabbitMqService.class));
    ContentResolver.setSyncAutomatically(account, Aptoide.getConfiguration().getUpdatesSyncAdapterAuthority(), true);
    if (Build.VERSION.SDK_INT >= 8)
        ContentResolver.addPeriodicSync(account, Aptoide.getConfiguration().getUpdatesSyncAdapterAuthority(), new Bundle(), 43200);
    ContentResolver.setSyncAutomatically(account, Aptoide.getConfiguration().getAutoUpdatesSyncAdapterAuthority(), true);
}
 
Example 20
Source File: SyncCard.java    From narrate-android with Apache License 2.0 4 votes vote down vote up
private void enableSync() {
    Account acc = User.getAccount();


    ContentResolver.setIsSyncable(acc, Contract.AUTHORITY, 1);
    ContentResolver.setSyncAutomatically(acc, Contract.AUTHORITY, true);
    ContentResolver.removePeriodicSync(acc, Contract.AUTHORITY, Bundle.EMPTY);

    long interval = Settings.getAutoSyncInterval();

    if (interval > 0) {
        ContentResolver.addPeriodicSync(acc, Contract.AUTHORITY, Bundle.EMPTY, interval);
    }

    Bundle b = new Bundle();
    b.putBoolean("resync_files", true);

    SyncHelper.requestManualSync(acc, b);

    Toast.makeText(GlobalApplication.getAppContext(), GlobalApplication.getAppContext().getString(R.string.data_resyncing), Toast.LENGTH_SHORT).show();
}