blob: 7215966564440287c840bc293197fb5aa7aa07ab [file] [log] [blame]
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -07001/*
2 * Copyright (C) 2009 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
17package com.android.contacts;
18
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070019import com.android.contacts.util.NotifyingAsyncQueryHandler;
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -070020
21import android.app.ExpandableListActivity;
22import android.content.ContentResolver;
23import android.content.ContentUris;
24import android.content.ContentValues;
25import android.content.Context;
Evan Millar5f4af702009-08-11 11:12:00 -070026import android.content.EntityIterator;
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -070027import android.content.SharedPreferences;
28import android.content.SharedPreferences.Editor;
29import android.content.pm.PackageManager;
30import android.content.pm.PackageManager.NameNotFoundException;
31import android.content.res.Resources;
32import android.database.CharArrayBuffer;
33import android.database.ContentObserver;
34import android.database.Cursor;
35import android.database.DataSetObserver;
36import android.net.Uri;
37import android.os.Bundle;
38import android.os.Handler;
39import android.preference.PreferenceManager;
40import android.provider.ContactsContract.Groups;
41import android.provider.ContactsContract.GroupsColumns;
42import android.util.Log;
43import android.view.LayoutInflater;
44import android.view.View;
45import android.view.ViewGroup;
46import android.widget.AdapterView;
47import android.widget.BaseExpandableListAdapter;
48import android.widget.CheckBox;
49import android.widget.ExpandableListView;
50import android.widget.SectionIndexer;
51import android.widget.TextView;
52import android.widget.AdapterView.OnItemClickListener;
53
54import java.util.ArrayList;
55import java.util.HashMap;
56import java.util.Map;
57
58/**
59 * Shows a list of all available {@link Groups} available, letting the user
60 * select which ones they want to be visible.
61 */
62public final class DisplayGroupsActivity extends ExpandableListActivity implements
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070063 NotifyingAsyncQueryHandler.AsyncQueryListener, OnItemClickListener {
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -070064 private static final String TAG = "DisplayGroupsActivity";
65
66 public interface Prefs {
67 public static final String DISPLAY_ALL = "display_all";
68 public static final boolean DISPLAY_ALL_DEFAULT = true;
69
70 public static final String DISPLAY_ONLY_PHONES = "only_phones";
71 public static final boolean DISPLAY_ONLY_PHONES_DEFAULT = true;
72
73 }
74
75 private ExpandableListView mList;
76 private DisplayGroupsAdapter mAdapter;
77
78 private SharedPreferences mPrefs;
79 private NotifyingAsyncQueryHandler mHandler;
80
81 private static final int QUERY_TOKEN = 42;
82
83 private View mHeaderAll;
84 private View mHeaderPhones;
85 private View mHeaderSeparator;
86
87 @Override
88 protected void onCreate(Bundle icicle) {
89 super.onCreate(icicle);
90 setContentView(android.R.layout.expandable_list_content);
91
92 mList = getExpandableListView();
93 mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
94
95 boolean displayAll = mPrefs.getBoolean(Prefs.DISPLAY_ALL, Prefs.DISPLAY_ALL_DEFAULT);
96 boolean displayOnlyPhones = mPrefs.getBoolean(Prefs.DISPLAY_ONLY_PHONES,
97 Prefs.DISPLAY_ONLY_PHONES_DEFAULT);
98
99 final LayoutInflater inflater = getLayoutInflater();
100
101 // Add the "All contacts" header modifier.
102 mHeaderAll = inflater.inflate(R.layout.display_header, mList, false);
103 mHeaderAll.setId(R.id.header_all);
104 {
105 CheckBox checkbox = (CheckBox)mHeaderAll.findViewById(android.R.id.checkbox);
106 TextView text1 = (TextView)mHeaderAll.findViewById(android.R.id.text1);
107 checkbox.setChecked(displayAll);
108 text1.setText(R.string.showAllGroups);
109 }
110 mList.addHeaderView(mHeaderAll, null, true);
111
112
113 // Add the "Only contacts with phones" header modifier.
114 mHeaderPhones = inflater.inflate(R.layout.display_header, mList, false);
115 mHeaderPhones.setId(R.id.header_phones);
116 {
117 CheckBox checkbox = (CheckBox)mHeaderPhones.findViewById(android.R.id.checkbox);
118 TextView text1 = (TextView)mHeaderPhones.findViewById(android.R.id.text1);
119 TextView text2 = (TextView)mHeaderPhones.findViewById(android.R.id.text2);
120 checkbox.setChecked(displayOnlyPhones);
121 text1.setText(R.string.showFilterPhones);
122 text2.setText(R.string.showFilterPhonesDescrip);
123 }
124 mList.addHeaderView(mHeaderPhones, null, true);
125
126
127 // Add the separator before showing the detailed group list.
128 mHeaderSeparator = inflater.inflate(R.layout.list_separator, mList, false);
129 {
130 TextView text1 = (TextView)mHeaderSeparator;
131 text1.setText(R.string.headerContactGroups);
132 }
133 mList.addHeaderView(mHeaderSeparator, null, false);
134
135
136 final TextView allContactsView = (TextView)mHeaderAll.findViewById(android.R.id.text2);
137
138 mAdapter = new DisplayGroupsAdapter(this);
139 mAdapter.setAllContactsView(allContactsView);
140
141 mAdapter.setEnabled(!displayAll);
142 mAdapter.setChildDescripWithPhones(displayOnlyPhones);
143
144 setListAdapter(mAdapter);
145
146 // Catch clicks on the header views
147 mList.setOnItemClickListener(this);
148
149 mHandler = new NotifyingAsyncQueryHandler(this, this);
150 startQuery();
151
152 }
153
154 @Override
155 protected void onRestart() {
156 super.onRestart();
157 startQuery();
158 }
159
160 @Override
161 protected void onStop() {
162 super.onStop();
163 mHandler.cancelOperation(QUERY_TOKEN);
164 }
165
166
167 private void startQuery() {
168 mHandler.cancelOperation(QUERY_TOKEN);
169 mHandler.startQuery(QUERY_TOKEN, null, Groups.CONTENT_SUMMARY_URI,
170 Projections.PROJ_SUMMARY, null, null, Projections.SORT_ORDER);
171 }
172
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700173 /** {@inheritDoc} */
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700174 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
175 mAdapter.changeCursor(cursor);
176
177 // Expand all data sources
178 final int groupCount = mAdapter.getGroupCount();
179 for (int i = 0; i < groupCount; i++) {
180 mList.expandGroup(i);
181 }
182 }
183
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700184 /** {@inheritDoc} */
185 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
186 // No actions
187 }
188
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700189 /**
190 * Handle any clicks on header views added to our {@link #mAdapter}, which
191 * are usually the global modifier checkboxes.
192 */
193 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
194 final CheckBox checkbox = (CheckBox)view.findViewById(android.R.id.checkbox);
195 switch (view.getId()) {
196 case R.id.header_all: {
197 checkbox.toggle();
198 final boolean displayAll = checkbox.isChecked();
199
200 Editor editor = mPrefs.edit();
201 editor.putBoolean(Prefs.DISPLAY_ALL, displayAll);
202 editor.commit();
203
204 mAdapter.setEnabled(!displayAll);
205 mAdapter.notifyDataSetChanged();
206
207 break;
208 }
209 case R.id.header_phones: {
210 checkbox.toggle();
211 final boolean displayOnlyPhones = checkbox.isChecked();
212
213 Editor editor = mPrefs.edit();
214 editor.putBoolean(Prefs.DISPLAY_ONLY_PHONES, displayOnlyPhones);
215 editor.commit();
216
217 mAdapter.setChildDescripWithPhones(displayOnlyPhones);
218 mAdapter.notifyDataSetChanged();
219
220 break;
221 }
222 }
223 }
224
225 /**
226 * Handle any clicks on {@link ExpandableListAdapter} children, which
227 * usually mean toggling its visible state.
228 */
229 @Override
230 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
231 int childPosition, long id) {
232 if (!mAdapter.isEnabled()) {
233 return false;
234 }
235
236 final CheckBox checkbox = (CheckBox)v.findViewById(android.R.id.checkbox);
237 checkbox.toggle();
238
239 // Build visibility update and send down to database
240 final ContentResolver resolver = getContentResolver();
241 final ContentValues values = new ContentValues();
242
243 values.put(Groups.GROUP_VISIBLE, checkbox.isChecked() ? 1 : 0);
244
245 final long groupId = mAdapter.getChildId(groupPosition, childPosition);
246 final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);
247
248 resolver.update(groupUri, values, null, null);
249
250 return true;
251 }
252
253 /**
254 * Helper for obtaining {@link Resources} instances that are based in an
255 * external package. Maintains internal cache to remain fast.
256 */
257 private static class ExternalResources {
258 private Context mContext;
259 private HashMap<String, Context> mCache = new HashMap<String, Context>();
260
261 public ExternalResources(Context context) {
262 mContext = context;
263 }
264
265 private Context getPackageContext(String packageName) throws NameNotFoundException {
266 Context theirContext = mCache.get(packageName);
267 if (theirContext == null) {
268 theirContext = mContext.createPackageContext(packageName, 0);
269 mCache.put(packageName, theirContext);
270 }
271 return theirContext;
272 }
273
274 public Resources getResources(String packageName) throws NameNotFoundException {
275 return getPackageContext(packageName).getResources();
276 }
277
278 public CharSequence getText(String packageName, int stringRes)
279 throws NameNotFoundException {
280 return getResources(packageName).getText(stringRes);
281 }
282 }
283
284 /**
285 * Adapter that shows all display groups as returned by a {@link Cursor}
286 * over {@link Groups#CONTENT_SUMMARY_URI}, along with their current visible
287 * status. Splits groups into sections based on {@link Groups#PACKAGE}.
288 */
289 private static class DisplayGroupsAdapter extends BaseExpandableListAdapter {
290 private boolean mDataValid;
291 private Cursor mCursor;
292 private Context mContext;
293 private Resources mResources;
294 private ExternalResources mExternalRes;
295 private LayoutInflater mInflater;
296 private int mRowIDColumn;
297
298 private TextView mAllContactsView;
299
300 private boolean mEnabled = true;
301 private boolean mChildWithPhones = false;
302
303 private ContentObserver mContentObserver = new MyChangeObserver();
304 private DataSetObserver mDataSetObserver = new MyDataSetObserver();
305
306 /**
307 * A single group in our expandable list.
308 */
309 private static class Group {
310 public long packageId = -1;
311 public String packageName = null;
312 public int firstPos;
313 public int lastPos;
314 public CharSequence label;
315 }
316
317 /**
318 * Maintain a list of all groups that need to be displayed by this
319 * adapter, usually built by walking across a single {@link Cursor} and
320 * finding the {@link Groups#PACKAGE} boundaries.
321 */
322 private static final ArrayList<Group> mGroups = new ArrayList<Group>();
323
324 public DisplayGroupsAdapter(Context context) {
325 mContext = context;
326 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
327 mResources = context.getResources();
328 mExternalRes = new ExternalResources(mContext);
329 }
330
331 /**
332 * In group descriptions, show the number of contacts with phone
333 * numbers, in addition to the total contacts.
334 */
335 public void setChildDescripWithPhones(boolean withPhones) {
336 mChildWithPhones = withPhones;
337 }
338
339 /**
340 * Set a {@link TextView} to be filled with the total number of contacts
341 * across all available groups.
342 */
343 public void setAllContactsView(TextView allContactsView) {
344 mAllContactsView = allContactsView;
345 }
346
347 /**
348 * Set the {@link View#setEnabled(boolean)} state of any views
349 * constructed by this adapter.
350 */
351 public void setEnabled(boolean enabled) {
352 mEnabled = enabled;
353 }
354
355 /**
356 * Returns the {@link View#setEnabled(boolean)} value being set for any
357 * children views of this adapter.
358 */
359 public boolean isEnabled() {
360 return mEnabled;
361 }
362
363 /**
364 * Used internally to build the {@link #mGroups} mapping. Call when you
365 * have a valid cursor and are ready to rebuild the mapping.
366 */
367 private void buildInternalMapping() {
368 final PackageManager pm = mContext.getPackageManager();
369 int totalContacts = 0;
370 Group group = null;
371
372 mGroups.clear();
373 mCursor.moveToPosition(-1);
374 while (mCursor.moveToNext()) {
375 final int position = mCursor.getPosition();
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700376 final long packageId = mCursor.getLong(Projections.COL_ID);
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700377 totalContacts += mCursor.getInt(Projections.COL_SUMMARY_COUNT);
378 if (group == null || packageId != group.packageId) {
379 group = new Group();
380 group.packageId = packageId;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700381 group.packageName = mCursor.getString(Projections.COL_RES_PACKAGE);
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700382 group.firstPos = position;
383 group.label = group.packageName;
384
385 try {
386 group.label = pm.getApplicationInfo(group.packageName, 0).loadLabel(pm);
387 } catch (NameNotFoundException e) {
388 Log.w(TAG, "couldn't find label for package " + group.packageName);
389 }
390
391 mGroups.add(group);
392 }
393 group.lastPos = position;
394 }
395
396 if (mAllContactsView != null) {
397 mAllContactsView.setText(mResources.getQuantityString(R.plurals.groupDescrip,
398 totalContacts, totalContacts));
399 }
400
401 }
402
403 /**
404 * Map the given group and child position into a flattened position on
405 * our single {@link Cursor}.
406 */
407 public int getCursorPosition(int groupPosition, int childPosition) {
408 // The actual cursor position for a child is simply stepping from
409 // the first position for that group.
410 final Group group = mGroups.get(groupPosition);
411 final int position = group.firstPos + childPosition;
412 return position;
413 }
414
415 public boolean hasStableIds() {
416 return true;
417 }
418
419 public boolean isChildSelectable(int groupPosition, int childPosition) {
420 return true;
421 }
422
423 public Object getChild(int groupPosition, int childPosition) {
424 if (mDataValid && mCursor != null) {
425 final int position = getCursorPosition(groupPosition, childPosition);
426 mCursor.moveToPosition(position);
427 return mCursor;
428 } else {
429 return null;
430 }
431 }
432
433 public long getChildId(int groupPosition, int childPosition) {
434 if (mDataValid && mCursor != null) {
435 final int position = getCursorPosition(groupPosition, childPosition);
436 if (mCursor.moveToPosition(position)) {
437 return mCursor.getLong(mRowIDColumn);
438 } else {
439 return 0;
440 }
441 } else {
442 return 0;
443 }
444 }
445
446 public int getChildrenCount(int groupPosition) {
447 if (mDataValid && mCursor != null) {
448 final Group group = mGroups.get(groupPosition);
449 final int size = group.lastPos - group.firstPos + 1;
450 return size;
451 } else {
452 return 0;
453 }
454 }
455
456 public Object getGroup(int groupPosition) {
457 if (mDataValid && mCursor != null) {
458 return mGroups.get(groupPosition);
459 } else {
460 return null;
461 }
462 }
463
464 public int getGroupCount() {
465 if (mDataValid && mCursor != null) {
466 return mGroups.size();
467 } else {
468 return 0;
469 }
470 }
471
472 public long getGroupId(int groupPosition) {
473 if (mDataValid && mCursor != null) {
474 final Group group = mGroups.get(groupPosition);
475 return group.packageId;
476 } else {
477 return 0;
478 }
479 }
480
481 public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
482 ViewGroup parent) {
483 if (!mDataValid) {
484 throw new IllegalStateException("called with invalid cursor");
485 }
486
487 final Group group = mGroups.get(groupPosition);
488
489 if (convertView == null) {
490 convertView = mInflater.inflate(R.layout.display_group, parent, false);
491 }
492
493 final TextView text1 = (TextView)convertView.findViewById(android.R.id.text1);
494
495 text1.setText(group.label);
496
497 convertView.setEnabled(mEnabled);
498
499 return convertView;
500 }
501
502 public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
503 View convertView, ViewGroup parent) {
504 if (!mDataValid) {
505 throw new IllegalStateException("called with invalid cursor");
506 }
507
508 final int position = getCursorPosition(groupPosition, childPosition);
509 if (!mCursor.moveToPosition(position)) {
510 throw new IllegalStateException("couldn't move cursor to position " + position);
511 }
512
513 if (convertView == null) {
514 convertView = mInflater.inflate(R.layout.display_child, parent, false);
515 }
516
517 final TextView text1 = (TextView)convertView.findViewById(android.R.id.text1);
518 final TextView text2 = (TextView)convertView.findViewById(android.R.id.text2);
519 final CheckBox checkbox = (CheckBox)convertView.findViewById(android.R.id.checkbox);
520
521 final int count = mCursor.getInt(Projections.COL_SUMMARY_COUNT);
522 final int withPhones = mCursor.getInt(Projections.COL_SUMMARY_WITH_PHONES);
523 final int membersVisible = mCursor.getInt(Projections.COL_GROUP_VISIBLE);
524
525 // Read title, but override with string resource when present
526 CharSequence title = mCursor.getString(Projections.COL_TITLE);
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700527 if (!mCursor.isNull(Projections.COL_RES_TITLE)) {
528 final String packageName = mCursor.getString(Projections.COL_RES_PACKAGE);
529 final int titleRes = mCursor.getInt(Projections.COL_RES_TITLE);
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700530 try {
531 title = mExternalRes.getText(packageName, titleRes);
532 } catch (NameNotFoundException e) {
533 Log.w(TAG, "couldn't load group title resource for " + packageName);
534 }
535 }
536
537 final int descripString = mChildWithPhones ? R.plurals.groupDescripPhones
538 : R.plurals.groupDescrip;
539
540 text1.setText(title);
541 text2.setText(mResources.getQuantityString(descripString, count, count, withPhones));
542 checkbox.setChecked((membersVisible == 1));
543
544 convertView.setEnabled(mEnabled);
545
546 return convertView;
547 }
548
549 public void changeCursor(Cursor cursor) {
550 if (cursor == mCursor) {
551 return;
552 }
553 if (mCursor != null) {
554 mCursor.unregisterContentObserver(mContentObserver);
555 mCursor.unregisterDataSetObserver(mDataSetObserver);
556 mCursor.close();
557 }
558 mCursor = cursor;
559 if (cursor != null) {
560 cursor.registerContentObserver(mContentObserver);
561 cursor.registerDataSetObserver(mDataSetObserver);
562 mRowIDColumn = cursor.getColumnIndexOrThrow("_id");
563 mDataValid = true;
564 buildInternalMapping();
565 // notify the observers about the new cursor
566 notifyDataSetChanged();
567 } else {
568 mRowIDColumn = -1;
569 mDataValid = false;
570 // notify the observers about the lack of a data set
571 notifyDataSetInvalidated();
572 }
573 }
574
575 protected void onContentChanged() {
576 if (mCursor != null && !mCursor.isClosed()) {
577 mDataValid = mCursor.requery();
578 }
579 }
580
581 private class MyChangeObserver extends ContentObserver {
582 public MyChangeObserver() {
583 super(new Handler());
584 }
585
586 @Override
587 public boolean deliverSelfNotifications() {
588 return true;
589 }
590
591 @Override
592 public void onChange(boolean selfChange) {
593 onContentChanged();
594 }
595 }
596
597 private class MyDataSetObserver extends DataSetObserver {
598 @Override
599 public void onChanged() {
600 mDataValid = true;
601 notifyDataSetChanged();
602 }
603
604 @Override
605 public void onInvalidated() {
606 mDataValid = false;
607 notifyDataSetInvalidated();
608 }
609 }
610
611 }
612
613 /**
614 * Database projections used locally.
615 */
616 private interface Projections {
617
618 public static final String[] PROJ_SUMMARY = new String[] {
619 Groups._ID,
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700620 Groups.TITLE,
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700621 Groups.RES_PACKAGE,
622 Groups.TITLE_RES,
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700623 Groups.GROUP_VISIBLE,
624 Groups.SUMMARY_COUNT,
625 Groups.SUMMARY_WITH_PHONES,
626 };
627
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700628 public static final String SORT_ORDER = Groups.ACCOUNT_TYPE + " ASC, "
629 + Groups.ACCOUNT_NAME + " ASC";
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700630
631 public static final int COL_ID = 0;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700632 public static final int COL_TITLE = 1;
633 public static final int COL_RES_PACKAGE = 2;
634 public static final int COL_RES_TITLE = 3;
635 public static final int COL_GROUP_VISIBLE = 4;
636 public static final int COL_SUMMARY_COUNT = 5;
637 public static final int COL_SUMMARY_WITH_PHONES = 6;
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700638
639 }
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700640}