blob: 12e82ce3b9e16c6abae3a8919f10d2ef73dffd51 [file] [log] [blame]
Frank Sposaro5f34b2e2011-06-21 15:54:30 -07001/*
2 * Copyright (C) 2011 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 */
16package com.android.contacts;
17
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070018import android.content.Context;
19import android.content.CursorLoader;
20import android.net.Uri;
21import android.provider.ContactsContract;
22import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
23import android.provider.ContactsContract.Contacts;
24import android.provider.ContactsContract.Data;
25import android.provider.ContactsContract.Directory;
26
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070027import com.android.contacts.preference.ContactsPreferences;
28
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070029import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Group Member loader. Loads all group members from the given groupId
34 */
35public final class GroupMemberLoader extends CursorLoader {
36
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070037 public static class GroupEditorQuery {
38 private static final String[] PROJECTION = new String[] {
39 Data.CONTACT_ID, // 0
40 Data.RAW_CONTACT_ID, // 1
41 Data.DISPLAY_NAME_PRIMARY, // 2
42 Data.PHOTO_URI, // 3
43 Data.LOOKUP_KEY, // 4
44 };
45
46 public static final int CONTACT_ID = 0;
47 public static final int RAW_CONTACT_ID = 1;
48 public static final int CONTACT_DISPLAY_NAME_PRIMARY = 2;
49 public static final int CONTACT_PHOTO_URI = 3;
50 public static final int CONTACT_LOOKUP_KEY = 4;
51 }
52
53 public static class GroupDetailQuery {
54 private static final String[] PROJECTION = new String[] {
Katherine Kuan81029592011-11-14 11:21:09 -080055 Data.CONTACT_ID, // 0
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070056 Data.PHOTO_URI, // 1
57 Data.LOOKUP_KEY, // 2
58 Data.DISPLAY_NAME_PRIMARY, // 3
59 Data.CONTACT_PRESENCE, // 4
60 Data.CONTACT_STATUS, // 5
61 };
62
Katherine Kuan81029592011-11-14 11:21:09 -080063 public static final int CONTACT_ID = 0;
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070064 public static final int CONTACT_PHOTO_URI = 1;
65 public static final int CONTACT_LOOKUP_KEY = 2;
66 public static final int CONTACT_DISPLAY_NAME_PRIMARY = 3;
67 public static final int CONTACT_PRESENCE_STATUS = 4;
68 public static final int CONTACT_STATUS = 5;
69 }
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070070
71 private final long mGroupId;
72
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070073 /**
74 * @return GroupMemberLoader object which can be used in group editor.
75 */
76 public static GroupMemberLoader constructLoaderForGroupEditorQuery(
77 Context context, long groupId) {
78 return new GroupMemberLoader(context, groupId, GroupEditorQuery.PROJECTION);
79 }
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070080
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070081 /**
82 * @return GroupMemberLoader object used in group detail page.
83 */
84 public static GroupMemberLoader constructLoaderForGroupDetailQuery(
85 Context context, long groupId) {
86 return new GroupMemberLoader(context, groupId, GroupDetailQuery.PROJECTION);
87 }
88
89 private GroupMemberLoader(Context context, long groupId, String[] projection) {
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070090 super(context);
91 mGroupId = groupId;
92 setUri(createUri());
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070093 setProjection(projection);
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070094 setSelection(createSelection());
95 setSelectionArgs(createSelectionArgs());
Isaac Katzenelson4b9b26e2011-09-23 15:01:00 -070096
97 ContactsPreferences prefs = new ContactsPreferences(context);
98 if (prefs.getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
99 setSortOrder(Contacts.SORT_KEY_PRIMARY);
100 } else {
101 setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
102 }
Frank Sposaro5f34b2e2011-06-21 15:54:30 -0700103 }
104
105 private Uri createUri() {
106 Uri uri = Data.CONTENT_URI;
107 uri = uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
108 String.valueOf(Directory.DEFAULT)).build();
Frank Sposaro5f34b2e2011-06-21 15:54:30 -0700109 return uri;
110 }
111
112 private String createSelection() {
113 StringBuilder selection = new StringBuilder();
114 selection.append(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?");
115 return selection.toString();
116 }
117
118 private String[] createSelectionArgs() {
119 List<String> selectionArgs = new ArrayList<String>();
120 selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE);
121 selectionArgs.add(String.valueOf(mGroupId));
122 return selectionArgs.toArray(new String[0]);
123 }
124}