summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/ContactsGroupSyncSelector.java
blob: 80b5166861d6e1c0a52b0a708e553f37868f6214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts;

import com.google.android.googlelogin.GoogleLoginServiceConstants;
import com.google.android.googlelogin.GoogleLoginServiceHelper;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Gmail;
import android.provider.Contacts.Groups;
import android.provider.Contacts.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public final class ContactsGroupSyncSelector extends ListActivity implements View.OnClickListener {

    private static final String[] PROJECTION = new String[] {
            Groups._ID, // 0
            Groups.NAME, // 1
            Groups.SHOULD_SYNC, // 2
            Groups.SYSTEM_ID, // 3
    };
    private static final int COLUMN_INDEX_ID = 0;
    private static final int COLUMN_INDEX_NAME = 1;
    private static final int COLUMN_INDEX_SHOULD_SYNC = 2;
    private static final int COLUMN_INDEX_SYSTEM_ID = 3;

    private static final int SUBACTIVITY_GET_ACCOUNT = 1;

    boolean[] mChecked;
    boolean mSyncAllGroups;
    long[] mGroupIds;
    
    private final class GroupsAdapter extends ArrayAdapter<CharSequence> {
        public GroupsAdapter(CharSequence[] items) {
            super(ContactsGroupSyncSelector.this,
                    android.R.layout.simple_list_item_checked,
                    android.R.id.text1, items);
        }

        @Override
        public boolean areAllItemsEnabled() {
            return mSyncAllGroups; 
        }

        @Override
        public boolean isEnabled(int pos) {
            if (mSyncAllGroups && pos != 0) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            if (mSyncAllGroups && position != 0) {
                v.setEnabled(false);
            } else {
                v.setEnabled(true);
            }
            return v;
        }
    }

    /**
     * Handles clicks on the list items
     */
    @Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
        boolean isChecked = list.isItemChecked(position);
        mChecked[position] = isChecked;
        if (position == 0) {
            mSyncAllGroups = isChecked;
            adjustChecks();
        }
    }

    /**
     * Handles clicks on the OK and cancel buttons
     */
    public void onClick(View view) {
        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.length;
                    for (int i = 1; i < count; i++) {
                        values.clear();
                        values.put(Groups.SHOULD_SYNC, mChecked[i]);
                        resolver.update(ContentUris.withAppendedId(Groups.CONTENT_URI, mGroupIds[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;
            }
        }
    }

    @Override
    protected void onCreate(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);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, 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();
            }
        }
    }

    private void buildItems() {
        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;
                CharSequence[] items = new String[count];
                boolean[] checked = new boolean[count];
                long[] groupIds = new long[count];
    
                int i = 0;
                items[i++] = getString(R.string.syncAllGroups);
                items[i++] = getString(R.string.groupNameMyContacts);
    
                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)) {
                        items[i] = name;
                        checked[i] = cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0;
                        groupIds[i] = cursor.getLong(COLUMN_INDEX_ID);
                        i++;
                    } else {
                        checked[1] = cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0;
                        groupIds[1] = cursor.getLong(COLUMN_INDEX_ID);
                    }
                }
                mChecked = checked;
                mSyncAllGroups = getShouldSyncEverything(resolver);
                checked[0] = mSyncAllGroups;
                mGroupIds = groupIds;
    
                // Setup the adapter
                setListAdapter(new GroupsAdapter(items));
            } finally {
                cursor.close();
            }
        }
    }

    private void adjustChecks() {
        final ListView list = getListView();
        if (mSyncAllGroups) {
            int count = list.getCount();
            for (int i = 0; i < count; i++) {
                list.setItemChecked(i, true);
            }
        } else {
            boolean[] checked = mChecked;
            int count = list.getCount();
            for (int i = 0; i < count; i++) {
                list.setItemChecked(i, checked[i]);
            }
        }
    }

    private static boolean getShouldSyncEverything(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);
    }
}