FileDocCategorySizeDatePackage
ContactsGroupSyncSelector.javaAPI DocAndroid 1.5 API10407Wed May 06 22:42:44 BST 2009com.android.contacts

ContactsGroupSyncSelector

public final class ContactsGroupSyncSelector extends android.app.ListActivity implements View.OnClickListener

Fields Summary
private static final String[]
PROJECTION
private static final int
COLUMN_INDEX_ID
private static final int
COLUMN_INDEX_NAME
private static final int
COLUMN_INDEX_SHOULD_SYNC
private static final int
COLUMN_INDEX_SYSTEM_ID
private static final int
SUBACTIVITY_GET_ACCOUNT
ArrayList
mChecked
ArrayList
mGroupIds
boolean
mSyncAllGroups
Constructors Summary
Methods Summary
private voidadjustChecks()

        final ListView list = getListView();
        if (mSyncAllGroups) {
            int count = list.getCount();
            for (int i = 0; i < count; i++) {
                list.setItemChecked(i, true);
            }
        } else {
            ArrayList<Boolean> checked = mChecked;
            int count = list.getCount();
            for (int i = 0; i < count; i++) {
                list.setItemChecked(i, checked.get(i));
            }
        }
    
private voidbuildItems()

        final ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(Groups.CONTENT_URI, PROJECTION, null, null, Groups.NAME);
        if (cursor != null) {
            try {
                int count = cursor.getCount() + 1; // add 1 for "sync all"
                ArrayList<CharSequence> items = new ArrayList<CharSequence>(count);
                ArrayList<Boolean> checked = new ArrayList<Boolean>(count);
                ArrayList<Long> groupIds = new ArrayList<Long>(count);

                // The first item in the list is always "sync all"
                items.add(getString(R.string.syncAllGroups));
                checked.add(mSyncAllGroups);
                groupIds.add(Long.valueOf(0)); // dummy entry

                while (cursor.moveToNext()) {
                    String name = cursor.getString(COLUMN_INDEX_NAME);
                    String systemId = cursor.isNull(COLUMN_INDEX_SYSTEM_ID) ?
                            null : cursor.getString(COLUMN_INDEX_SYSTEM_ID);
                    if (systemId == null || !Groups.GROUP_MY_CONTACTS.equals(systemId)) {
                        // Localize the "Starred in Android" string which we get from the server
                        // side.
                        if (Groups.GROUP_ANDROID_STARRED.equals(name)) {
                            name = getString(R.string.starredInAndroid);
                        }
                        items.add(name);
                        checked.add(cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0);
                        groupIds.add(cursor.getLong(COLUMN_INDEX_ID));
                    } else {
                        // If My Contacts is around it wants to be the second list entry
                        items.add(1, getString(R.string.groupNameMyContacts));
                        checked.add(1, cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0);
                        groupIds.add(1, cursor.getLong(COLUMN_INDEX_ID));
                    }
                }
                mChecked = checked;
                mGroupIds = groupIds;
                mSyncAllGroups = getShouldSyncEverything(resolver);
    
                // Setup the adapter
                setListAdapter(new GroupsAdapter(items));
            } finally {
                cursor.close();
            }
        }
    
private static booleangetShouldSyncEverything(android.content.ContentResolver cr)

        // For now we only support a single account and the UI doesn't know what
        // the account name is, so we're using a global setting for SYNC_EVERYTHING.
        // Some day when we add multiple accounts to the UI this should use the per
        // account setting.
        String value = Contacts.Settings.getSetting(cr, null, Contacts.Settings.SYNC_EVERYTHING);
        if (value == null) {
            // If nothing is set yet we default to syncing everything
            return true;
        }
        return !TextUtils.isEmpty(value) && !"0".equals(value);
    
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent intent)

        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == SUBACTIVITY_GET_ACCOUNT) {
            if (resultCode == RESULT_OK) {
                // There is an account setup, build the group list
                buildItems();
                adjustChecks();
            } else {
                finish();
            }
        }
    
public voidonClick(android.view.View view)
Handles clicks on the OK and cancel buttons

        switch (view.getId()) {
            case R.id.cancel: {
                finish();
                break;
            }
            
            case R.id.ok: {
                // The list isn't setup yet, so just return without doing anything.
                if (mChecked == null) {
                    finish();
                    return;
                }

                final ContentResolver resolver = getContentResolver();
                if (mSyncAllGroups) {
                    // For now we only support a single account and the UI doesn't know what
                    // the account name is, so we're using a global setting for SYNC_EVERYTHING.
                    // Some day when we add multiple accounts to the UI this should use the per
                    // account setting.
                    Settings.setSetting(resolver, null, Settings.SYNC_EVERYTHING, "1");
                } else {
                    ContentValues values = new ContentValues();
                    int count = mChecked.size();
                    for (int i = 1; i < count; i++) {
                        values.clear();
                        values.put(Groups.SHOULD_SYNC, mChecked.get(i));
                        resolver.update(
                                ContentUris.withAppendedId(Groups.CONTENT_URI, mGroupIds.get(i)),
                                values, null, null);
                    }
                    // For now we only support a single account and the UI doesn't know what
                    // the account name is, so we're using a global setting for SYNC_EVERYTHING.
                    // Some day when we add multiple accounts to the UI this should use the per
                    // account setting.
                    Settings.setSetting(resolver, null, Settings.SYNC_EVERYTHING, "0");
                }
                finish();
                break;
            }
        }
    
protected voidonCreate(android.os.Bundle savedState)

        super.onCreate(savedState);

        // Only look for an account on first run.
        if (savedState == null) {
            // This will request a Gmail account and if none are present, it will
            // invoke SetupWizard to login or create one. The result is returned
            // through onActivityResult().
            Bundle bundle = new Bundle();
            bundle.putCharSequence("optional_message", getText(R.string.contactsSyncPlug));
            GoogleLoginServiceHelper.getCredentials(this, SUBACTIVITY_GET_ACCOUNT,
                    bundle, GoogleLoginServiceConstants.PREFER_HOSTED, Gmail.GMAIL_AUTH_SERVICE,
                    true);
        }

        setContentView(R.layout.sync_settings);

        findViewById(R.id.ok).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
        
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
protected voidonListItemClick(android.widget.ListView list, android.view.View view, int position, long id)
Handles clicks on the list items

        boolean isChecked = list.isItemChecked(position);
        mChecked.set(position, isChecked);
        if (position == 0) {
            mSyncAllGroups = isChecked;
            adjustChecks();
        }