OSDN Git Service

6081c79484c6e616821fab231ea03b4b6dffbb51
[android-x86/packages-apps-Contacts.git] / src / com / android / contacts / ContactsGroupSyncSelector.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.contacts;
18
19 import com.google.android.googlelogin.GoogleLoginServiceConstants;
20 import com.google.android.googlelogin.GoogleLoginServiceHelper;
21
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;
38
39 public final class ContactsGroupSyncSelector extends ListActivity implements View.OnClickListener {
40
41     private static final String[] PROJECTION = new String[] {
42             Groups._ID, // 0
43             Groups.NAME, // 1
44             Groups.SHOULD_SYNC, // 2
45             Groups.SYSTEM_ID, // 3
46     };
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;
51
52     private static final int SUBACTIVITY_GET_ACCOUNT = 1;
53
54     boolean[] mChecked;
55     boolean mSyncAllGroups;
56     long[] mGroupIds;
57     
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);
63         }
64
65         @Override
66         public boolean areAllItemsEnabled() {
67             return mSyncAllGroups; 
68         }
69
70         @Override
71         public boolean isEnabled(int pos) {
72             if (mSyncAllGroups && pos != 0) {
73                 return false;
74             } else {
75                 return true;
76             }
77         }
78
79         @Override
80         public View getView(int position, View convertView, ViewGroup parent) {
81             View v = super.getView(position, convertView, parent);
82             if (mSyncAllGroups && position != 0) {
83                 v.setEnabled(false);
84             } else {
85                 v.setEnabled(true);
86             }
87             return v;
88         }
89     }
90
91     /**
92      * Handles clicks on the list items
93      */
94     @Override
95     protected void onListItemClick(ListView list, View view, int position, long id) {
96         boolean isChecked = list.isItemChecked(position);
97         mChecked[position] = isChecked;
98         if (position == 0) {
99             mSyncAllGroups = isChecked;
100             adjustChecks();
101         }
102     }
103
104     /**
105      * Handles clicks on the OK and cancel buttons
106      */
107     public void onClick(View view) {
108         switch (view.getId()) {
109             case R.id.cancel: {
110                 finish();
111                 break;
112             }
113             
114             case R.id.ok: {
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
120                     // account setting.
121                     Settings.setSetting(resolver, null, Settings.SYNC_EVERYTHING, "1");
122                 } else {
123                     ContentValues values = new ContentValues();
124                     int count = mChecked.length;
125                     for (int i = 1; i < count; i++) {
126                         values.clear();
127                         values.put(Groups.SHOULD_SYNC, mChecked[i]);
128                         resolver.update(ContentUris.withAppendedId(Groups.CONTENT_URI, mGroupIds[i]),
129                                 values, null, null);
130                     }
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
134                     // account setting.
135                     Settings.setSetting(resolver, null, Settings.SYNC_EVERYTHING, "0");
136                 }
137                 finish();
138                 break;
139             }
140         }
141     }
142
143     @Override
144     protected void onCreate(Bundle savedState) {
145         super.onCreate(savedState);
146
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,
156                     true);
157         }
158
159         setContentView(R.layout.sync_settings);
160
161         findViewById(R.id.ok).setOnClickListener(this);
162         findViewById(R.id.cancel).setOnClickListener(this);
163         
164         getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
165     }
166
167     @Override
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
173                 buildItems();
174                 adjustChecks();
175             } else {
176                 finish();
177             }
178         }
179     }
180
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) {
185             try {
186                 int count = cursor.getCount() + 1;
187                 CharSequence[] items = new String[count];
188                 boolean[] checked = new boolean[count];
189                 long[] groupIds = new long[count];
190     
191                 int i = 0;
192                 items[i++] = getString(R.string.syncAllGroups);
193                 items[i++] = getString(R.string.groupNameMyContacts);
194     
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)) {
200                         items[i] = name;
201                         checked[i] = cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0;
202                         groupIds[i] = cursor.getLong(COLUMN_INDEX_ID);
203                         i++;
204                     } else {
205                         checked[1] = cursor.getInt(COLUMN_INDEX_SHOULD_SYNC) != 0;
206                         groupIds[1] = cursor.getLong(COLUMN_INDEX_ID);
207                     }
208                 }
209                 mChecked = checked;
210                 mSyncAllGroups = getShouldSyncEverything(resolver);
211                 checked[0] = mSyncAllGroups;
212                 mGroupIds = groupIds;
213     
214                 // Setup the adapter
215                 setListAdapter(new GroupsAdapter(items));
216             } finally {
217                 cursor.close();
218             }
219         }
220     }
221
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);
228             }
229         } else {
230             boolean[] checked = mChecked;
231             int count = list.getCount();
232             for (int i = 0; i < count; i++) {
233                 list.setItemChecked(i, checked[i]);
234             }
235         }
236     }
237
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
242         // account setting.
243         String value = Contacts.Settings.getSetting(cr, null, Contacts.Settings.SYNC_EVERYTHING);
244         if (value == null) {
245             // If nothing is set yet we default to syncing everything
246             return true;
247         }
248         return !TextUtils.isEmpty(value) && !"0".equals(value);
249     }
250 }