blob: 8bc7d04a1414ff0c3b905e22d12ef172aa64eb0e [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 Chenga0233a02012-11-01 16:41:08 -070027import com.android.contacts.common.preference.ContactsPreferences;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070028
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
Walter Jang47d5caa2016-04-16 10:39:50 -070044 Data.PHOTO_ID, // 5
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070045 };
46
47 public static final int CONTACT_ID = 0;
48 public static final int RAW_CONTACT_ID = 1;
49 public static final int CONTACT_DISPLAY_NAME_PRIMARY = 2;
50 public static final int CONTACT_PHOTO_URI = 3;
51 public static final int CONTACT_LOOKUP_KEY = 4;
Walter Jang47d5caa2016-04-16 10:39:50 -070052 public static final int CONTACT_PHOTO_ID = 5;
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070053 }
54
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070055 private final long mGroupId;
56
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070057 private GroupMemberLoader(Context context, long groupId, String[] projection) {
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070058 super(context);
59 mGroupId = groupId;
60 setUri(createUri());
Daisuke Miyakawaa9393722011-10-31 13:51:38 -070061 setProjection(projection);
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070062 setSelection(createSelection());
63 setSelectionArgs(createSelectionArgs());
Isaac Katzenelson4b9b26e2011-09-23 15:01:00 -070064
65 ContactsPreferences prefs = new ContactsPreferences(context);
Yorke Leec9bb2172014-07-10 11:38:34 -070066 if (prefs.getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
Isaac Katzenelson4b9b26e2011-09-23 15:01:00 -070067 setSortOrder(Contacts.SORT_KEY_PRIMARY);
68 } else {
69 setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
70 }
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070071 }
72
73 private Uri createUri() {
74 Uri uri = Data.CONTENT_URI;
75 uri = uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
76 String.valueOf(Directory.DEFAULT)).build();
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070077 return uri;
78 }
79
80 private String createSelection() {
81 StringBuilder selection = new StringBuilder();
82 selection.append(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?");
83 return selection.toString();
84 }
85
86 private String[] createSelectionArgs() {
87 List<String> selectionArgs = new ArrayList<String>();
88 selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE);
89 selectionArgs.add(String.valueOf(mGroupId));
90 return selectionArgs.toArray(new String[0]);
91 }
92}