Example usage for android.content ContentResolver requestSync

List of usage examples for android.content ContentResolver requestSync

Introduction

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

Prototype

public static void requestSync(Account account, String authority, Bundle extras) 

Source Link

Document

Start an asynchronous sync operation.

Usage

From source file:com.activiti.android.platform.provider.transfer.ContentTransferManager.java

public static void requestUpload(Fragment fr, Uri uri, String objectId, int type, String mimetyp) {
    Cursor cursor = null;/*from  ww w.j  av a  2s .  co m*/
    String name = null, source, sourceId, mimetype = mimetyp;
    try {
        Bundle settingsBundle = new Bundle();

        // Retrieve other info
        cursor = fr.getActivity().getContentResolver().query(uri, null, null, null, null);

        if (cursor == null && uri != null) {
            // Is it a file ?
            File file = new File(uri.getPath());
            if (file.exists()) {
                // It's a file
                name = file.getName();
                settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_CONTENT_PATH, file.getPath());
                if (mimetype == null) {
                    MimeType mime = MimeTypeManager.getInstance(fr.getActivity()).getMimetype(file.getName());
                    mimetype = (mime != null) ? mime.getMimeType() : mimetyp;
                }
            }
        } else if (cursor != null && cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
            mimetype = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_CONTENT_URI, uri.toString());
        } else {
            throw new Exception("Cursor is empty");
        }

        settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);

        settingsBundle.putInt(ContentTransferSyncAdapter.ARGUMENT_MODE,
                ContentTransferSyncAdapter.MODE_SAF_UPLOAD);
        settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_FILE_PATH, name);
        if (type == TYPE_TASK_ID) {
            settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_TASK_ID, objectId);
        } else if (type == TYPE_PROCESS_ID) {
            settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_PROCESS_ID, objectId);
        } else if (type == TYPE_PROFILE_ID) {
            settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_PROFILE_ID, objectId);
        }
        settingsBundle.putString(ContentTransferSyncAdapter.ARGUMENT_MIMETYPE, mimetype);
        ContentResolver.requestSync(
                ActivitiAccountManager.getInstance(fr.getActivity()).getCurrentAndroidAccount(),
                ContentTransferProvider.AUTHORITY, settingsBundle);
    } catch (Exception e) {
        Log.w("COntent Transfer", Log.getStackTraceString(e));
    } finally {
        CursorUtils.closeCursor(cursor);
    }
}

From source file:mobisocial.musubi.service.AddressBookUpdateHandler.java

@Override
public void onChange(boolean selfChange) {
    final DatabaseManager dbManager = new DatabaseManager(mContext);
    if (!dbManager.getIdentitiesManager().hasConnectedAccounts()) {
        Log.w(TAG, "no connected accounts, skipping friend import");
        return;/* ww  w  .j  a  v a  2 s .  com*/
    }

    //a new meta contact appears (and the previous ones disappear) if the user merges
    //or if a new entry is added, we can detect the ones that have changed by
    //this condition
    long highestContactIdAlreadySeen = dbManager.getContactDataVersionManager().getMaxContactIdSeen();
    //a new data item corresponds with a new contact, but its possible
    //that a users just adds a new contact method to an existing contact
    //and we need to detect that
    long highestDataIdAlreadySeen = dbManager.getContactDataVersionManager().getMaxDataIdSeen();

    // BJD -- this didn't end up being faster once all import features were added.
    /*if (highestContactIdAlreadySeen == -1) {
       importFullAddressBook(mContext);
       return;
    }*/
    long now = System.currentTimeMillis();
    if (mLastRun + ONCE_PER_PERIOD > now) {
        //wake up when the period expires
        if (!mScheduled) {
            new Handler(mThread.getLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScheduled = false;
                    dispatchChange(false);
                }
            }, ONCE_PER_PERIOD - (now - mLastRun) + 1);
        }
        mScheduled = true;
        //skip this update
        return;
    }
    Log.i(TAG, "waking up to handle contact changes...");
    boolean identityAdded = false, profileDataChanged = false;
    Date start = new Date();

    assert (SYNC_EMAIL);
    String account_type_selection = getAccountSelectionString();

    Cursor c = mContext.getContentResolver().query(
            ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data._ID,
                    ContactsContract.Data.DATA_VERSION, ContactsContract.Data.CONTACT_ID },
            "(" + ContactsContract.Data.DATA_VERSION + ">0 OR " + //maybe updated
                    ContactsContract.Data.CONTACT_ID + ">? OR " + //definitely new or merged
                    ContactsContract.Data._ID + ">? " + //definitely added a data item
                    ") AND (" + ContactsContract.RawContacts.ACCOUNT_TYPE + "<>'" + mAccountType + "'"
                    + ") AND (" + NAME_OR_OTHER_SELECTION + account_type_selection + ")", // All known contacts.
            new String[] { String.valueOf(highestContactIdAlreadySeen),
                    String.valueOf(highestDataIdAlreadySeen) },
            null);

    if (c == null) {
        Log.e(TAG, "no valid cursor", new Throwable());
        mContext.getContentResolver().notifyChange(MusubiService.ADDRESS_BOOK_SCANNED, this);
        return;
    }

    HashMap<Pair<String, String>, MMyAccount> account_mapping = new HashMap<Pair<String, String>, MMyAccount>();
    int max_changes = c.getCount();
    TLongArrayList raw_data_ids = new TLongArrayList(max_changes);
    TLongArrayList versions = new TLongArrayList(max_changes);
    long new_max_data_id = highestDataIdAlreadySeen;
    long new_max_contact_id = highestContactIdAlreadySeen;
    TLongHashSet potentially_changed = new TLongHashSet();
    try {
        //the cursor points to a list of raw contact data items that may have changed
        //the items will include a type specific field that we are interested in updating
        //it is possible that multiple data item entries mention the same identifier
        //so we build a list of contacts to update and then perform synchronization
        //by refreshing given that we know the top level contact id.
        if (DBG)
            Log.d(TAG, "Scanning " + c.getCount() + " contacts...");
        while (c.moveToNext()) {
            if (DBG)
                Log.v(TAG, "check for updates of contact " + c.getLong(0));

            long raw_data_id = c.getLong(0);
            long version = c.getLong(1);
            long contact_id = c.getLong(2);

            //if the contact was split or merged, then we get a higher contact id
            //so if we have a higher id, data version doesnt really matter
            if (contact_id <= highestContactIdAlreadySeen) {
                //the data associated with this contact may not be dirty
                //we just can't do the join against our table because thise
                //api is implmented over the content provider
                if (dbManager.getContactDataVersionManager().getVersion(raw_data_id) == version)
                    continue;
            } else {
                new_max_contact_id = Math.max(new_max_contact_id, contact_id);
            }
            raw_data_ids.add(raw_data_id);
            versions.add(version);
            potentially_changed.add(contact_id);
            new_max_data_id = Math.max(new_max_data_id, raw_data_id);
        }
        if (DBG)
            Log.d(TAG, "Finished iterating over " + c.getCount() + " contacts for " + potentially_changed.size()
                    + " candidates.");
    } finally {
        c.close();
    }
    if (potentially_changed.size() == 0) {
        Log.w(TAG,
                "possible bug, woke up to update contacts, but no change was detected; there are extra wakes so it could be ok");
    }

    final SQLiteDatabase db = dbManager.getDatabase();

    Pattern emailPattern = getEmailPattern();
    Pattern numberPattern = getNumberPattern();
    //slice it up so we don't use too much system resource on keeping a lot of state in memory
    int total = potentially_changed.size();
    sAddressBookTotal = total;
    sAddressBookPosition = 0;

    final TLongArrayList slice_of_changed = new TLongArrayList(BATCH_SIZE);
    final StringBuilder to_fetch = new StringBuilder();
    final HashMap<Pair<String, String>, TLongHashSet> ids_for_account = new HashMap<Pair<String, String>, TLongHashSet>();
    final TLongObjectHashMap<String> names = new TLongObjectHashMap<String>();

    TLongIterator it = potentially_changed.iterator();
    for (int i = 0; i < total && it.hasNext();) {
        sAddressBookPosition = i;

        if (BootstrapActivity.isBootstrapped()) {
            try {
                Thread.sleep(mSleepTime * SLEEP_SCALE);
            } catch (InterruptedException e) {
            }
        }

        slice_of_changed.clear();
        ids_for_account.clear();
        names.clear();

        int max = i + BATCH_SIZE;
        for (; i < max && it.hasNext(); ++i) {
            slice_of_changed.add(it.next());
        }

        if (DBG)
            Log.v(TAG, "looking up names ");
        to_fetch.setLength(0);
        to_fetch.append(ContactsContract.Contacts._ID + " IN ");
        SQLClauseHelper.appendArray(to_fetch, slice_of_changed.iterator());
        //lookup the fields we care about from a user profile perspective
        c = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, },
                to_fetch.toString(), null, null);
        try {
            while (c.moveToNext()) {
                long id = c.getLong(0);
                String name = c.getString(1);
                if (name == null)
                    continue;
                //reject names that are just the email address or are just a number 
                //the default for android is just to propagate this as the name
                //if there is no name
                if (emailPattern.matcher(name).matches() || numberPattern.matcher(name).matches())
                    continue;
                names.put(id, name);
            }
        } finally {
            c.close();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            db.beginTransactionNonExclusive();
        } else {
            db.beginTransaction();
        }

        long before = SystemClock.elapsedRealtime();
        SliceUpdater updater = new SliceUpdater(dbManager, slice_of_changed, ids_for_account, names,
                account_type_selection);
        long after = SystemClock.elapsedRealtime();
        mSleepTime = (mSleepTime + after - before) / 2;
        slice_of_changed.forEach(updater);
        profileDataChanged |= updater.profileDataChanged;
        identityAdded |= updater.identityAdded;
        db.setTransactionSuccessful();
        db.endTransaction();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            db.beginTransactionNonExclusive();
        } else {
            db.beginTransaction();
        }
        //add all detected members to account feed
        for (Entry<Pair<String, String>, TLongHashSet> e : ids_for_account.entrySet()) {
            Pair<String, String> k = e.getKey();
            TLongHashSet v = e.getValue();
            MMyAccount cached_account = account_mapping.get(k);
            if (cached_account == null) {
                cached_account = lookupOrCreateAccount(dbManager, k.getValue0(), k.getValue1());
                prepareAccountWhitelistFeed(dbManager.getMyAccountManager(), dbManager.getFeedManager(),
                        cached_account);
                account_mapping.put(k, cached_account);
            }

            final MMyAccount account = cached_account;
            v.forEach(new TLongProcedure() {
                @Override
                public boolean execute(long id) {
                    dbManager.getFeedManager().ensureFeedMember(account.feedId_, id);
                    db.yieldIfContendedSafely(75);
                    return true;
                }
            });
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    sAddressBookTotal = sAddressBookPosition = 0;

    //TODO: handle deleted
    //for all android data ids in our table, check if they still exist in the
    //contacts table, probably in batches of 100 or something.  if they don't
    //null them out.  this is annoyingly non-differential.

    //TODO: adding friend should update accepted feed status, however,
    //if a crashe happens for whatever reason, then its possible that this may need to
    //be run for identities which actually exist in the db.  so this update code
    //needs to do the feed accepted status change for all users that were touched
    //by the profile update process

    //update the version ids so we can be faster on subsequent runs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        db.beginTransactionNonExclusive();
    } else {
        db.beginTransaction();
    }
    int changed_data_rows = raw_data_ids.size();
    for (int i = 0; i < changed_data_rows; ++i) {
        dbManager.getContactDataVersionManager().setVersion(raw_data_ids.get(i), versions.get(i));
    }
    db.setTransactionSuccessful();
    db.endTransaction();

    dbManager.getContactDataVersionManager().setMaxDataIdSeen(new_max_data_id);
    dbManager.getContactDataVersionManager().setMaxContactIdSeen(new_max_contact_id);
    ContentResolver resolver = mContext.getContentResolver();

    Date end = new Date();
    double time = end.getTime() - start.getTime();
    time /= 1000;
    Log.w(TAG, "update address book " + mChangeCount++ + " took " + time + " seconds");
    if (identityAdded) {
        //wake up the profile push
        resolver.notifyChange(MusubiService.WHITELIST_APPENDED, this);
    }
    if (profileDataChanged) {
        //refresh the ui...
        resolver.notifyChange(MusubiService.PRIMARY_CONTENT_CHANGED, this);
    }
    if (identityAdded || profileDataChanged) {
        //update the our musubi address book as needed.
        String accountName = mContext.getString(R.string.account_name);
        String accountType = mContext.getString(R.string.account_type);
        Account account = new Account(accountName, accountType);
        ContentResolver.requestSync(account, ContactsContract.AUTHORITY, new Bundle());
    }

    dbManager.close();
    mLastRun = new Date().getTime();
    resolver.notifyChange(MusubiService.ADDRESS_BOOK_SCANNED, this);
}

From source file:com.upenn.chriswang1990.sunshine.sync.SunshineSyncAdapter.java

/**
 * Helper method to have the sync adapter sync immediately
 *
 * @param context The context used to access the account service
 *//*w  ww .j  av  a 2  s. co  m*/
public static void syncImmediately(Context context) {
    Bundle bundle = new Bundle();
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    //Set status to no network when net work is not available
    if (!Utility.isNetworkAvailable(context)) {
        Utility.setLocationStatus(context, LOCATION_STATUS_NO_NETWORK);
    }
    ContentResolver.requestSync(getSyncAccount(context), context.getString(R.string.content_authority), bundle);
}

From source file:com.openerp.MainActivity.java

/**
 * Request sync./*from  w ww.j a  v a2 s .c om*/
 * 
 * @param authority
 *            the authority
 * @param bundle
 *            the extra data
 */
public void requestSync(String authority, Bundle bundle) {
    Account account = OpenERPAccountManager.getAccount(getApplicationContext(),
            OEUser.current(getApplicationContext()).getAndroidName());
    Bundle settingsBundle = new Bundle();
    settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    if (bundle != null) {
        settingsBundle.putAll(bundle);
    }
    ContentResolver.requestSync(account, authority, settingsBundle);
}

From source file:com.digitalarx.android.ui.activity.FileDisplayActivity.java

private void startSynchronization() {
    Log_OC.e(TAG, "Got to start sync");
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
        Log_OC.e(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.e(TAG, "Requesting sync for " + getAccount().name + " at " + MainApp.getAuthority());
        ContentResolver.requestSync(getAccount(), MainApp.getAuthority(), bundle);
    } else {/* w w  w  . ja va 2 s . c o m*/
        Log_OC.e(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();
        SyncRequest request = builder.build();
        ContentResolver.requestSync(request);
    }
}

From source file:com.quarterfull.newsAndroid.NewsReaderListActivity.java

public void startSync() {
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) == null)
        StartLoginFragment(this);
    else {//from  w  w  w . j a  v a2s . c om
        if (!ownCloudSyncService.isSyncRunning()) {
            new PostDelayHandler(this).stopRunningPostDelayHandler();//Stop pending sync handler

            Bundle accBundle = new Bundle();
            accBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
            AccountManager mAccountManager = AccountManager.get(this);
            Account[] accounts = mAccountManager.getAccounts();
            for (Account acc : accounts)
                if (acc.type.equals(AccountGeneral.ACCOUNT_TYPE))
                    ContentResolver.requestSync(acc, AccountGeneral.ACCOUNT_TYPE, accBundle);
            //http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync
        } else {
            UpdateButtonLayout();
        }

    }
}

From source file:de.luhmer.owncloudnewsreader.NewsReaderListActivity.java

public void startSync() {
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) == null)
        StartLoginFragment(this);
    else {//from   w  w  w .ja  v  a  2 s.  co m
        try {
            if (!_ownCloudSyncService.isSyncRunning()) {
                new PostDelayHandler(this).stopRunningPostDelayHandler();//Stop pending sync handler

                Bundle accBundle = new Bundle();
                accBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
                AccountManager mAccountManager = AccountManager.get(this);
                Account[] accounts = mAccountManager.getAccounts();
                for (Account acc : accounts)
                    if (acc.type.equals(AccountGeneral.ACCOUNT_TYPE))
                        ContentResolver.requestSync(acc, AccountGeneral.ACCOUNT_TYPE, accBundle);
                //http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync
            } else {
                UpdateButtonLayout();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.android.contacts.list.DefaultContactBrowseListFragment.java

/**
 * Request sync for the Google accounts (not include Google+ accounts) specified by the given
 * filter.//from   w  w w . j  a  v a  2s .  com
 */
private void syncContacts(ContactListFilter filter) {
    if (filter == null) {
        return;
    }

    final Bundle bundle = new Bundle();
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);

    final List<AccountWithDataSet> accounts = AccountInfo
            .extractAccounts(Futures.getUnchecked(mWritableAccountsFuture));
    final List<Account> syncableAccounts = filter.getSyncableAccounts(accounts);
    if (syncableAccounts != null && syncableAccounts.size() > 0) {
        for (Account account : syncableAccounts) {
            // We can prioritize Contacts sync if sync is not initialized yet.
            if (!SyncUtil.isSyncStatusPendingOrActive(account) || SyncUtil.isUnsyncableGoogleAccount(account)) {
                ContentResolver.requestSync(account, ContactsContract.AUTHORITY, bundle);
            }
        }
    }
}

From source file:com.cerema.cloud2.ui.activity.FileDisplayActivity.java

private 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 {//from  w w  w .j a v a 2s  .co m
        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);
    }
}