1/* 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 18import com.android.contacts.preference.ContactsPreferences; 19 20import android.content.Context; 21import android.content.CursorLoader; 22import android.net.Uri; 23import android.provider.ContactsContract; 24import android.provider.ContactsContract.CommonDataKinds.GroupMembership; 25import android.provider.ContactsContract.Contacts; 26import android.provider.ContactsContract.Data; 27import android.provider.ContactsContract.Directory; 28 29import 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 37 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[] { 55 Data.CONTACT_ID, // 0 56 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 63 public static final int CONTACT_ID = 0; 64 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 } 70 71 private final long mGroupId; 72 73 /** 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 } 80 81 /** 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) { 90 super(context); 91 mGroupId = groupId; 92 setUri(createUri()); 93 setProjection(projection); 94 setSelection(createSelection()); 95 setSelectionArgs(createSelectionArgs()); 96 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 } 103 } 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(); 109 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} 125