2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.contacts;
19 import com.google.android.googlelogin.GoogleLoginServiceConstants;
20 import com.google.android.googlelogin.GoogleLoginServiceHelper;
22 import android.app.ListActivity;
23 import android.content.ContentResolver;
24 import android.content.ContentUris;
25 import android.content.ContentValues;
26 import android.content.Intent;
27 import android.database.Cursor;
28 import android.os.Bundle;
29 import android.provider.Contacts;
30 import android.provider.Gmail;
31 import android.provider.Contacts.Groups;
32 import android.provider.Contacts.Settings;
33 import android.text.TextUtils;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.ArrayAdapter;
37 import android.widget.ListView;
39 public final class ContactsGroupSyncSelector extends ListActivity implements View.OnClickListener {
41 private static final String[] PROJECTION = new String[] {
44 Groups.SHOULD_SYNC, // 2
45 Groups.SYSTEM_ID, // 3
47 private static final int COLUMN_INDEX_ID = 0;
48 private static final int COLUMN_INDEX_NAME = 1;
49 private static final int COLUMN_INDEX_SHOULD_SYNC = 2;
50 private static final int COLUMN_INDEX_SYSTEM_ID = 3;
52 private static final int SUBACTIVITY_GET_ACCOUNT = 1;
55 boolean mSyncAllGroups;
58 private final class GroupsAdapter extends ArrayAdapter<CharSequence> {
59 public GroupsAdapter(CharSequence[] items) {
60 super(ContactsGroupSyncSelector.this,
61 android.R.layout.simple_list_item_checked,
62 android.R.id.text1, items);
66 public boolean areAllItemsEnabled() {
67 return mSyncAllGroups;
71 public boolean isEnabled(int pos) {
72 if (mSyncAllGroups && pos != 0) {
80 public View getView(int position, View convertView, ViewGroup parent) {
81 View v = super.getView(position, convertView, parent);
82 if (mSyncAllGroups && position != 0) {
92 * Handles clicks on the list items
95 protected void onListItemClick(ListView list, View view, int position, long id) {
96 boolean isChecked = list.isItemChecked(position);
97 mChecked[position] = isChecked;
99 mSyncAllGroups = isChecked;
105 * Handles clicks on the OK and cancel buttons
107 public void onClick(View view) {
108 switch (view.getId()) {
115 final ContentResolver resolver = getContentResolver();
116 if (mSyncAllGroups) {
117 // For now we only support a single account and the UI doesn't know what
118 // the account name is, so we're using a global setting for SYNC_EVERYTHING.
119 // Some day when we add multiple accounts to the UI this should use the per
121 Settings.setSetting(resolver, null, Settings.SYNC_EVERYTHING, "1");
123 ContentValues values = new ContentValues();
124 int count = mChecked.length;
125 for (int i = 1; i < count; i++) {
127 values.put(Groups.SHOULD_SYNC, mChecked[i]);
128 resolver.update(ContentUris.withAppendedId(Groups.CONTENT_URI, mGroupIds[i]),
131 // For now we only support a single account and the UI doesn't know what
132 // the account name is, so we're using a global setting for SYNC_EVERYTHING.
133 // Some day when we add multiple accounts to the UI this should use the per
135 Settings.setSetting(resolver, null, Settings.SYNC_EVERYTHING, "0");
144 protected void onCreate(Bundle savedState) {
145 super.onCreate(savedState);
147 // Only look for an account on first run.
148 if (savedState == null) {
149 // This will request a Gmail account and if none are present, it will
150 // invoke SetupWizard to login or create one. The result is returned
151 // through onActivityResult().
152 Bundle bundle = new Bundle();
153 bundle.putCharSequence("optional_message", getText(R.string.contactsSyncPlug));
154 GoogleLoginServiceHelper.getCredentials(this, SUBACTIVITY_GET_ACCOUNT,
155 bundle, GoogleLoginServiceConstants.PREFER_HOSTED, Gmail.GMAIL_AUTH_SERVICE,
159 setContentView(R.layout.sync_settings);
161 findViewById(R.id.ok).setOnClickListener(this);
162 findViewById(R.id.cancel).setOnClickListener(this);
164 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
168 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
169 super.onActivityResult(requestCode, resultCode, intent);
170 if (requestCode == SUBACTIVITY_GET_ACCOUNT) {
171 if (resultCode == RESULT_OK) {
172 // There is an account setup, build the group list
181 private void buildItems() {
182 final ContentResolver resolver = getContentResolver();
183 Cursor cursor = resolver.query(Groups.CONTENT_URI, PROJECTION, null, null, Groups.NAME);
184 if (cursor != null) {
186 int count = cursor.getCount() + 1;
187 CharSequence[] items = new String[count];
188 boolean[] checked = new boolean[count];
189 long[] groupIds = new long[count];
192 items[i++] = getString(R.string.syncAllGroups);
193 items[i++] = getString(R.string.groupNameMyContacts);
195 while (cursor.moveToNext()) {
196 String name = cursor.getString(COLUMN_INDEX_NAME);
197 String systemId = cursor.isNull(COLUMN_INDEX_SYSTEM_ID) ?
198 null : cursor.getString(COLUMN_INDEX_SYSTEM_ID);
199 if (systemId == null || !Groups.GROUP_MY_CONTACTS.equals(systemId)) {
201 checked[i] = cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0;
202 groupIds[i] = cursor.getLong(COLUMN_INDEX_ID);
205 checked[1] = cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0;
206 groupIds[1] = cursor.getLong(COLUMN_INDEX_ID);
210 mSyncAllGroups = getShouldSyncEverything(resolver);
211 checked[0] = mSyncAllGroups;
212 mGroupIds = groupIds;
215 setListAdapter(new GroupsAdapter(items));
222 private void adjustChecks() {
223 final ListView list = getListView();
224 if (mSyncAllGroups) {
225 int count = list.getCount();
226 for (int i = 0; i < count; i++) {
227 list.setItemChecked(i, true);
230 boolean[] checked = mChecked;
231 int count = list.getCount();
232 for (int i = 0; i < count; i++) {
233 list.setItemChecked(i, checked[i]);
238 private static boolean getShouldSyncEverything(ContentResolver cr) {
239 // For now we only support a single account and the UI doesn't know what
240 // the account name is, so we're using a global setting for SYNC_EVERYTHING.
241 // Some day when we add multiple accounts to the UI this should use the per
243 String value = Contacts.Settings.getSetting(cr, null, Contacts.Settings.SYNC_EVERYTHING);
245 // If nothing is set yet we default to syncing everything
248 return !TextUtils.isEmpty(value) && !"0".equals(value);