android.content.SyncRequest Java Examples

The following examples show how to use android.content.SyncRequest. 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: 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 #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: 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 #4
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 #5
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 #6
Source File: ContentService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void cancelRequest(SyncRequest request) {
    SyncManager syncManager = getSyncManager();
    if (syncManager == null) return;
    int userId = UserHandle.getCallingUserId();
    final int callingUid = Binder.getCallingUid();

    if (request.isPeriodic()) {
        mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                "no permission to write the sync settings");
    }

    Bundle extras = new Bundle(request.getBundle());
    validateExtras(callingUid, extras);

    long identityToken = clearCallingIdentity();
    try {
        SyncStorageEngine.EndPoint info;

        Account account = request.getAccount();
        String provider = request.getProvider();
        info = new SyncStorageEngine.EndPoint(account, provider, userId);
        if (request.isPeriodic()) {
            // Remove periodic sync.
            getSyncManager().removePeriodicSync(info, extras,
                    "cancelRequest() by uid=" + callingUid);
        }
        // Cancel active syncs and clear pending syncs from the queue.
        syncManager.cancelScheduledSyncOperation(info, extras);
        syncManager.cancelActiveSync(info, extras, "API");
    } finally {
        restoreCallingIdentity(identityToken);
    }
}
 
Example #7
Source File: FileActivity.java    From Cirrus_depricated with GNU General Public License v2.0 5 votes vote down vote up
protected void startSynchronization() {
    Log_OC.d(TAG, "Got to start sync");
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
        Log_OC.d(TAG, "Canceling all syncs for " + MainApp.getAuthority());
        ContentResolver.cancelSync(null, MainApp.getAuthority());
        // cancel the current synchronizations of any ownCloud account
        Bundle bundle = new Bundle();
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
                MainApp.getAuthority());
        ContentResolver.requestSync(
                getAccount(),
                MainApp.getAuthority(), bundle);
    } else {
        Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
                MainApp.getAuthority() + " with new API");
        SyncRequest.Builder builder = new SyncRequest.Builder();
        builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
        builder.setExpedited(true);
        builder.setManual(true);
        builder.syncOnce();

        // Fix bug in Android Lollipop when you click on refresh the whole account
        Bundle extras = new Bundle();
        builder.setExtras(extras);

        SyncRequest request = builder.build();
        ContentResolver.requestSync(request);
    }
}
 
Example #8
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 #9
Source File: SyncStorageEngine.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras,
        @SyncExemption int syncExemptionFlag) {
    if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
            && mSyncRequestListener != null) {
        mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras,
                syncExemptionFlag);
    } else {
        SyncRequest.Builder req =
                new SyncRequest.Builder()
                        .syncOnce()
                        .setExtras(extras);
        req.setSyncAdapter(authorityInfo.target.account, authorityInfo.target.provider);
        ContentResolver.requestSync(req.build());
    }
}
 
Example #10
Source File: ContentService.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void cancelRequest(SyncRequest request) {
    SyncManager syncManager = getSyncManager();
    if (syncManager == null) return;
    int userId = UserHandle.getCallingUserId();
    final int callingUid = Binder.getCallingUid();

    if (request.isPeriodic()) {
        mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                "no permission to write the sync settings");
    }

    Bundle extras = new Bundle(request.getBundle());
    validateExtras(callingUid, extras);

    long identityToken = clearCallingIdentity();
    try {
        SyncStorageEngine.EndPoint info;

        Account account = request.getAccount();
        String provider = request.getProvider();
        info = new SyncStorageEngine.EndPoint(account, provider, userId);
        if (request.isPeriodic()) {
            // Remove periodic sync.
            getSyncManager().removePeriodicSync(info, extras,
                    "cancelRequest() by uid=" + callingUid);
        }
        // Cancel active syncs and clear pending syncs from the queue.
        syncManager.cancelScheduledSyncOperation(info, extras);
        syncManager.cancelActiveSync(info, extras, "API");
    } finally {
        restoreCallingIdentity(identityToken);
    }
}
 
Example #11
Source File: ContentService.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void cancelRequest(SyncRequest request) {
    SyncManager syncManager = getSyncManager();
    if (syncManager == null) return;
    int userId = UserHandle.getCallingUserId();
    final int callingUid = Binder.getCallingUid();

    long identityToken = clearCallingIdentity();
    try {
        SyncStorageEngine.EndPoint info;
        Bundle extras = new Bundle(request.getBundle());
        Account account = request.getAccount();
        String provider = request.getProvider();
        info = new SyncStorageEngine.EndPoint(account, provider, userId);
        if (request.isPeriodic()) {
            // Remove periodic sync.
            mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
                    "no permission to write the sync settings");
            getSyncManager().removePeriodicSync(info, extras,
                    "cancelRequest() by uid=" + callingUid);
        }
        // Cancel active syncs and clear pending syncs from the queue.
        syncManager.cancelScheduledSyncOperation(info, extras);
        syncManager.cancelActiveSync(info, extras, "API");
    } finally {
        restoreCallingIdentity(identityToken);
    }
}
 
Example #12
Source File: ContentService.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * If the user id supplied is different to the calling user, the caller must hold the
 * INTERACT_ACROSS_USERS_FULL permission.
 */
@Override
public void syncAsUser(SyncRequest request, int userId) {
    enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
    int callerUid = Binder.getCallingUid();
    // This makes it so that future permission checks will be in the context of this
    // process rather than the caller's process. We will restore this before returning.
    long identityToken = clearCallingIdentity();
    try {
        SyncManager syncManager = getSyncManager();
        if (syncManager == null) {
            return;
        }

        Bundle extras = request.getBundle();
        long flextime = request.getSyncFlexTime();
        long runAtTime = request.getSyncRunTime();
        if (request.isPeriodic()) {
            mContext.enforceCallingOrSelfPermission(
                    Manifest.permission.WRITE_SYNC_SETTINGS,
                    "no permission to write the sync settings");
            SyncStorageEngine.EndPoint info;
            info = new SyncStorageEngine.EndPoint(
                    request.getAccount(), request.getProvider(), userId);

            runAtTime = clampPeriod(runAtTime);
            // Schedule periodic sync.
            getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
                    flextime, extras);
        } else {
            syncManager.scheduleSync(
                    request.getAccount(), userId, callerUid, request.getProvider(), extras,
                    SyncStorageEngine.AuthorityInfo.UNDEFINED);
        }
    } finally {
        restoreCallingIdentity(identityToken);
    }
}
 
Example #13
Source File: ContentService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * If the user id supplied is different to the calling user, the caller must hold the
 * INTERACT_ACROSS_USERS_FULL permission.
 */
@Override
public void syncAsUser(SyncRequest request, int userId) {
    enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
    int callerUid = Binder.getCallingUid();

    final Bundle extras = request.getBundle();

    validateExtras(callerUid, extras);
    final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callerUid, extras);

    // This makes it so that future permission checks will be in the context of this
    // process rather than the caller's process. We will restore this before returning.
    long identityToken = clearCallingIdentity();
    try {
        SyncManager syncManager = getSyncManager();
        if (syncManager == null) {
            return;
        }
        long flextime = request.getSyncFlexTime();
        long runAtTime = request.getSyncRunTime();
        if (request.isPeriodic()) {
            mContext.enforceCallingOrSelfPermission(
                    Manifest.permission.WRITE_SYNC_SETTINGS,
                    "no permission to write the sync settings");
            SyncStorageEngine.EndPoint info;
            info = new SyncStorageEngine.EndPoint(
                    request.getAccount(), request.getProvider(), userId);

            runAtTime = clampPeriod(runAtTime);
            // Schedule periodic sync.
            getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
                    flextime, extras);
        } else {
            syncManager.scheduleSync(
                    request.getAccount(), userId, callerUid, request.getProvider(), extras,
                    SyncStorageEngine.AuthorityInfo.UNDEFINED,
                    syncExemption);
        }
    } finally {
        restoreCallingIdentity(identityToken);
    }
}
 
Example #14
Source File: ContentService.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/**
 * If the user id supplied is different to the calling user, the caller must hold the
 * INTERACT_ACROSS_USERS_FULL permission.
 */
@Override
public void syncAsUser(SyncRequest request, int userId, String callingPackage) {
    enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
    final int callingUid = Binder.getCallingUid();
    final int callingPid = Binder.getCallingPid();

    final Bundle extras = request.getBundle();

    validateExtras(callingUid, extras);
    final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callingUid, extras);

    // This makes it so that future permission checks will be in the context of this
    // process rather than the caller's process. We will restore this before returning.
    long identityToken = clearCallingIdentity();
    try {
        SyncManager syncManager = getSyncManager();
        if (syncManager == null) {
            return;
        }
        long flextime = request.getSyncFlexTime();
        long runAtTime = request.getSyncRunTime();
        if (request.isPeriodic()) {
            mContext.enforceCallingOrSelfPermission(
                    Manifest.permission.WRITE_SYNC_SETTINGS,
                    "no permission to write the sync settings");
            SyncStorageEngine.EndPoint info;
            info = new SyncStorageEngine.EndPoint(
                    request.getAccount(), request.getProvider(), userId);

            runAtTime = clampPeriod(runAtTime);
            // Schedule periodic sync.
            getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
                    flextime, extras);
        } else {
            syncManager.scheduleSync(
                    request.getAccount(), userId, callingUid, request.getProvider(), extras,
                    SyncStorageEngine.AuthorityInfo.UNDEFINED,
                    syncExemption, callingUid, callingPid, callingPackage);
        }
    } finally {
        restoreCallingIdentity(identityToken);
    }
}
 
Example #15
Source File: ContentService.java    From AndroidComponentPlugin with Apache License 2.0 2 votes vote down vote up
/**
 * Request a sync with a generic {@link android.content.SyncRequest} object. This will be
 * either:
 *   periodic OR one-off sync.
 * and
 *   anonymous OR provider sync.
 * Depending on the request, we enqueue to suit in the SyncManager.
 * @param request The request object. Validation of this object is done by its builder.
 */
@Override
public void sync(SyncRequest request) {
    syncAsUser(request, UserHandle.getCallingUserId());
}
 
Example #16
Source File: ContentService.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Request a sync with a generic {@link android.content.SyncRequest} object. This will be
 * either:
 *   periodic OR one-off sync.
 * and
 *   anonymous OR provider sync.
 * Depending on the request, we enqueue to suit in the SyncManager.
 * @param request The request object. Validation of this object is done by its builder.
 */
@Override
public void sync(SyncRequest request) {
    syncAsUser(request, UserHandle.getCallingUserId());
}
 
Example #17
Source File: ContentService.java    From AndroidComponentPlugin with Apache License 2.0 2 votes vote down vote up
/**
 * Request a sync with a generic {@link android.content.SyncRequest} object. This will be
 * either:
 *   periodic OR one-off sync.
 * and
 *   anonymous OR provider sync.
 * Depending on the request, we enqueue to suit in the SyncManager.
 * @param request The request object. Validation of this object is done by its builder.
 */
@Override
public void sync(SyncRequest request, String callingPackage) {
    syncAsUser(request, UserHandle.getCallingUserId(), callingPackage);
}