blob: 6da25316dedbf7cd5318009bef56ac417961ff87 [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
Evan Millar5f4af702009-08-11 11:12:00 -070019import com.android.contacts.NotifyingAsyncQueryHandler.AsyncQueryListener;
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
Evan Millar5f4af702009-08-11 11:12:00 -070063 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
173 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
174 mAdapter.changeCursor(cursor);
175
176 // Expand all data sources
177 final int groupCount = mAdapter.getGroupCount();
178 for (int i = 0; i < groupCount; i++) {
179 mList.expandGroup(i);
180 }
181 }
182
183 /**
184 * Handle any clicks on header views added to our {@link #mAdapter}, which
185 * are usually the global modifier checkboxes.
186 */
187 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
188 final CheckBox checkbox = (CheckBox)view.findViewById(android.R.id.checkbox);
189 switch (view.getId()) {
190 case R.id.header_all: {
191 checkbox.toggle();
192 final boolean displayAll = checkbox.isChecked();
193
194 Editor editor = mPrefs.edit();
195 editor.putBoolean(Prefs.DISPLAY_ALL, displayAll);
196 editor.commit();
197
198 mAdapter.setEnabled(!displayAll);
199 mAdapter.notifyDataSetChanged();
200
201 break;
202 }
203 case R.id.header_phones: {
204 checkbox.toggle();
205 final boolean displayOnlyPhones = checkbox.isChecked();
206
207 Editor editor = mPrefs.edit();
208 editor.putBoolean(Prefs.DISPLAY_ONLY_PHONES, displayOnlyPhones);
209 editor.commit();
210
211 mAdapter.setChildDescripWithPhones(displayOnlyPhones);
212 mAdapter.notifyDataSetChanged();
213
214 break;
215 }
216 }
217 }
218
219 /**
220 * Handle any clicks on {@link ExpandableListAdapter} children, which
221 * usually mean toggling its visible state.
222 */
223 @Override
224 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
225 int childPosition, long id) {
226 if (!mAdapter.isEnabled()) {
227 return false;
228 }
229
230 final CheckBox checkbox = (CheckBox)v.findViewById(android.R.id.checkbox);
231 checkbox.toggle();
232
233 // Build visibility update and send down to database
234 final ContentResolver resolver = getContentResolver();
235 final ContentValues values = new ContentValues();
236
237 values.put(Groups.GROUP_VISIBLE, checkbox.isChecked() ? 1 : 0);
238
239 final long groupId = mAdapter.getChildId(groupPosition, childPosition);
240 final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);
241
242 resolver.update(groupUri, values, null, null);
243
244 return true;
245 }
246
247 /**
248 * Helper for obtaining {@link Resources} instances that are based in an
249 * external package. Maintains internal cache to remain fast.
250 */
251 private static class ExternalResources {
252 private Context mContext;
253 private HashMap<String, Context> mCache = new HashMap<String, Context>();
254
255 public ExternalResources(Context context) {
256 mContext = context;
257 }
258
259 private Context getPackageContext(String packageName) throws NameNotFoundException {
260 Context theirContext = mCache.get(packageName);
261 if (theirContext == null) {
262 theirContext = mContext.createPackageContext(packageName, 0);
263 mCache.put(packageName, theirContext);
264 }
265 return theirContext;
266 }
267
268 public Resources getResources(String packageName) throws NameNotFoundException {
269 return getPackageContext(packageName).getResources();
270 }
271
272 public CharSequence getText(String packageName, int stringRes)
273 throws NameNotFoundException {
274 return getResources(packageName).getText(stringRes);
275 }
276 }
277
278 /**
279 * Adapter that shows all display groups as returned by a {@link Cursor}
280 * over {@link Groups#CONTENT_SUMMARY_URI}, along with their current visible
281 * status. Splits groups into sections based on {@link Groups#PACKAGE}.
282 */
283 private static class DisplayGroupsAdapter extends BaseExpandableListAdapter {
284 private boolean mDataValid;
285 private Cursor mCursor;
286 private Context mContext;
287 private Resources mResources;
288 private ExternalResources mExternalRes;
289 private LayoutInflater mInflater;
290 private int mRowIDColumn;
291
292 private TextView mAllContactsView;
293
294 private boolean mEnabled = true;
295 private boolean mChildWithPhones = false;
296
297 private ContentObserver mContentObserver = new MyChangeObserver();
298 private DataSetObserver mDataSetObserver = new MyDataSetObserver();
299
300 /**
301 * A single group in our expandable list.
302 */
303 private static class Group {
304 public long packageId = -1;
305 public String packageName = null;
306 public int firstPos;
307 public int lastPos;
308 public CharSequence label;
309 }
310
311 /**
312 * Maintain a list of all groups that need to be displayed by this
313 * adapter, usually built by walking across a single {@link Cursor} and
314 * finding the {@link Groups#PACKAGE} boundaries.
315 */
316 private static final ArrayList<Group> mGroups = new ArrayList<Group>();
317
318 public DisplayGroupsAdapter(Context context) {
319 mContext = context;
320 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
321 mResources = context.getResources();
322 mExternalRes = new ExternalResources(mContext);
323 }
324
325 /**
326 * In group descriptions, show the number of contacts with phone
327 * numbers, in addition to the total contacts.
328 */
329 public void setChildDescripWithPhones(boolean withPhones) {
330 mChildWithPhones = withPhones;
331 }
332
333 /**
334 * Set a {@link TextView} to be filled with the total number of contacts
335 * across all available groups.
336 */
337 public void setAllContactsView(TextView allContactsView) {
338 mAllContactsView = allContactsView;
339 }
340
341 /**
342 * Set the {@link View#setEnabled(boolean)} state of any views
343 * constructed by this adapter.
344 */
345 public void setEnabled(boolean enabled) {
346 mEnabled = enabled;
347 }
348
349 /**
350 * Returns the {@link View#setEnabled(boolean)} value being set for any
351 * children views of this adapter.
352 */
353 public boolean isEnabled() {
354 return mEnabled;
355 }
356
357 /**
358 * Used internally to build the {@link #mGroups} mapping. Call when you
359 * have a valid cursor and are ready to rebuild the mapping.
360 */
361 private void buildInternalMapping() {
362 final PackageManager pm = mContext.getPackageManager();
363 int totalContacts = 0;
364 Group group = null;
365
366 mGroups.clear();
367 mCursor.moveToPosition(-1);
368 while (mCursor.moveToNext()) {
369 final int position = mCursor.getPosition();
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700370 final long packageId = mCursor.getLong(Projections.COL_ID);
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700371 totalContacts += mCursor.getInt(Projections.COL_SUMMARY_COUNT);
372 if (group == null || packageId != group.packageId) {
373 group = new Group();
374 group.packageId = packageId;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700375 group.packageName = mCursor.getString(Projections.COL_RES_PACKAGE);
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700376 group.firstPos = position;
377 group.label = group.packageName;
378
379 try {
380 group.label = pm.getApplicationInfo(group.packageName, 0).loadLabel(pm);
381 } catch (NameNotFoundException e) {
382 Log.w(TAG, "couldn't find label for package " + group.packageName);
383 }
384
385 mGroups.add(group);
386 }
387 group.lastPos = position;
388 }
389
390 if (mAllContactsView != null) {
391 mAllContactsView.setText(mResources.getQuantityString(R.plurals.groupDescrip,
392 totalContacts, totalContacts));
393 }
394
395 }
396
397 /**
398 * Map the given group and child position into a flattened position on
399 * our single {@link Cursor}.
400 */
401 public int getCursorPosition(int groupPosition, int childPosition) {
402 // The actual cursor position for a child is simply stepping from
403 // the first position for that group.
404 final Group group = mGroups.get(groupPosition);
405 final int position = group.firstPos + childPosition;
406 return position;
407 }
408
409 public boolean hasStableIds() {
410 return true;
411 }
412
413 public boolean isChildSelectable(int groupPosition, int childPosition) {
414 return true;
415 }
416
417 public Object getChild(int groupPosition, int childPosition) {
418 if (mDataValid && mCursor != null) {
419 final int position = getCursorPosition(groupPosition, childPosition);
420 mCursor.moveToPosition(position);
421 return mCursor;
422 } else {
423 return null;
424 }
425 }
426
427 public long getChildId(int groupPosition, int childPosition) {
428 if (mDataValid && mCursor != null) {
429 final int position = getCursorPosition(groupPosition, childPosition);
430 if (mCursor.moveToPosition(position)) {
431 return mCursor.getLong(mRowIDColumn);
432 } else {
433 return 0;
434 }
435 } else {
436 return 0;
437 }
438 }
439
440 public int getChildrenCount(int groupPosition) {
441 if (mDataValid && mCursor != null) {
442 final Group group = mGroups.get(groupPosition);
443 final int size = group.lastPos - group.firstPos + 1;
444 return size;
445 } else {
446 return 0;
447 }
448 }
449
450 public Object getGroup(int groupPosition) {
451 if (mDataValid && mCursor != null) {
452 return mGroups.get(groupPosition);
453 } else {
454 return null;
455 }
456 }
457
458 public int getGroupCount() {
459 if (mDataValid && mCursor != null) {
460 return mGroups.size();
461 } else {
462 return 0;
463 }
464 }
465
466 public long getGroupId(int groupPosition) {
467 if (mDataValid && mCursor != null) {
468 final Group group = mGroups.get(groupPosition);
469 return group.packageId;
470 } else {
471 return 0;
472 }
473 }
474
475 public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
476 ViewGroup parent) {
477 if (!mDataValid) {
478 throw new IllegalStateException("called with invalid cursor");
479 }
480
481 final Group group = mGroups.get(groupPosition);
482
483 if (convertView == null) {
484 convertView = mInflater.inflate(R.layout.display_group, parent, false);
485 }
486
487 final TextView text1 = (TextView)convertView.findViewById(android.R.id.text1);
488
489 text1.setText(group.label);
490
491 convertView.setEnabled(mEnabled);
492
493 return convertView;
494 }
495
496 public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
497 View convertView, ViewGroup parent) {
498 if (!mDataValid) {
499 throw new IllegalStateException("called with invalid cursor");
500 }
501
502 final int position = getCursorPosition(groupPosition, childPosition);
503 if (!mCursor.moveToPosition(position)) {
504 throw new IllegalStateException("couldn't move cursor to position " + position);
505 }
506
507 if (convertView == null) {
508 convertView = mInflater.inflate(R.layout.display_child, parent, false);
509 }
510
511 final TextView text1 = (TextView)convertView.findViewById(android.R.id.text1);
512 final TextView text2 = (TextView)convertView.findViewById(android.R.id.text2);
513 final CheckBox checkbox = (CheckBox)convertView.findViewById(android.R.id.checkbox);
514
515 final int count = mCursor.getInt(Projections.COL_SUMMARY_COUNT);
516 final int withPhones = mCursor.getInt(Projections.COL_SUMMARY_WITH_PHONES);
517 final int membersVisible = mCursor.getInt(Projections.COL_GROUP_VISIBLE);
518
519 // Read title, but override with string resource when present
520 CharSequence title = mCursor.getString(Projections.COL_TITLE);
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700521 if (!mCursor.isNull(Projections.COL_RES_TITLE)) {
522 final String packageName = mCursor.getString(Projections.COL_RES_PACKAGE);
523 final int titleRes = mCursor.getInt(Projections.COL_RES_TITLE);
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700524 try {
525 title = mExternalRes.getText(packageName, titleRes);
526 } catch (NameNotFoundException e) {
527 Log.w(TAG, "couldn't load group title resource for " + packageName);
528 }
529 }
530
531 final int descripString = mChildWithPhones ? R.plurals.groupDescripPhones
532 : R.plurals.groupDescrip;
533
534 text1.setText(title);
535 text2.setText(mResources.getQuantityString(descripString, count, count, withPhones));
536 checkbox.setChecked((membersVisible == 1));
537
538 convertView.setEnabled(mEnabled);
539
540 return convertView;
541 }
542
543 public void changeCursor(Cursor cursor) {
544 if (cursor == mCursor) {
545 return;
546 }
547 if (mCursor != null) {
548 mCursor.unregisterContentObserver(mContentObserver);
549 mCursor.unregisterDataSetObserver(mDataSetObserver);
550 mCursor.close();
551 }
552 mCursor = cursor;
553 if (cursor != null) {
554 cursor.registerContentObserver(mContentObserver);
555 cursor.registerDataSetObserver(mDataSetObserver);
556 mRowIDColumn = cursor.getColumnIndexOrThrow("_id");
557 mDataValid = true;
558 buildInternalMapping();
559 // notify the observers about the new cursor
560 notifyDataSetChanged();
561 } else {
562 mRowIDColumn = -1;
563 mDataValid = false;
564 // notify the observers about the lack of a data set
565 notifyDataSetInvalidated();
566 }
567 }
568
569 protected void onContentChanged() {
570 if (mCursor != null && !mCursor.isClosed()) {
571 mDataValid = mCursor.requery();
572 }
573 }
574
575 private class MyChangeObserver extends ContentObserver {
576 public MyChangeObserver() {
577 super(new Handler());
578 }
579
580 @Override
581 public boolean deliverSelfNotifications() {
582 return true;
583 }
584
585 @Override
586 public void onChange(boolean selfChange) {
587 onContentChanged();
588 }
589 }
590
591 private class MyDataSetObserver extends DataSetObserver {
592 @Override
593 public void onChanged() {
594 mDataValid = true;
595 notifyDataSetChanged();
596 }
597
598 @Override
599 public void onInvalidated() {
600 mDataValid = false;
601 notifyDataSetInvalidated();
602 }
603 }
604
605 }
606
607 /**
608 * Database projections used locally.
609 */
610 private interface Projections {
611
612 public static final String[] PROJ_SUMMARY = new String[] {
613 Groups._ID,
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700614 Groups.TITLE,
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700615 Groups.RES_PACKAGE,
616 Groups.TITLE_RES,
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700617 Groups.GROUP_VISIBLE,
618 Groups.SUMMARY_COUNT,
619 Groups.SUMMARY_WITH_PHONES,
620 };
621
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700622 public static final String SORT_ORDER = Groups.ACCOUNT_TYPE + " ASC, "
623 + Groups.ACCOUNT_NAME + " ASC";
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700624
625 public static final int COL_ID = 0;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700626 public static final int COL_TITLE = 1;
627 public static final int COL_RES_PACKAGE = 2;
628 public static final int COL_RES_TITLE = 3;
629 public static final int COL_GROUP_VISIBLE = 4;
630 public static final int COL_SUMMARY_COUNT = 5;
631 public static final int COL_SUMMARY_WITH_PHONES = 6;
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700632
633 }
634
Evan Millar5f4af702009-08-11 11:12:00 -0700635 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
636 // Emtpy
637 }
638
Jeff Sharkeyd5c5b9a2009-06-21 19:46:04 -0700639}