JoinContactListAdapter.java revision 8773bcb491d18e88b4e3d1f9cf7c57f6bc8e69ed
1/*
2 * Copyright (C) 2010 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.list;
17
18import com.android.contacts.R;
19
20import android.content.Context;
21import android.content.CursorLoader;
22import android.database.Cursor;
23import android.database.MatrixCursor;
24import android.net.Uri;
25import android.net.Uri.Builder;
26import android.provider.ContactsContract;
27import android.provider.ContactsContract.Contacts;
28import android.provider.ContactsContract.Contacts.AggregationSuggestions;
29import android.text.TextUtils;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.TextView;
34
35public class JoinContactListAdapter extends ContactListAdapter {
36
37    /** Maximum number of suggestions shown for joining aggregates */
38    private static final int MAX_SUGGESTIONS = 4;
39
40    public static final int PARTITION_SUGGESTIONS = 0;
41    public static final int PARTITION_SHOW_ALL_CONTACTS = 1;
42    public static final int PARTITION_ALL_CONTACTS = 2;
43
44    private long mTargetContactId;
45
46    private int mShowAllContactsViewType;
47
48    /**
49     * Determines whether we display a list item with the label
50     * "Show all contacts" or actually show all contacts
51     */
52    private boolean mAllContactsListShown;
53
54
55    public JoinContactListAdapter(Context context) {
56        super(context);
57        setPinnedPartitionHeadersEnabled(true);
58        setSectionHeaderDisplayEnabled(true);
59        setIndexedPartition(PARTITION_ALL_CONTACTS);
60        mShowAllContactsViewType = getViewTypeCount() - 1;
61    }
62
63    @Override
64    protected void addPartitions() {
65
66        // Partition 0: suggestions
67        addPartition(false, true);
68
69        // Partition 1: "Show all contacts"
70        addPartition(false, false);
71
72        // Partition 2: All contacts
73        addPartition(createDefaultDirectoryPartition());
74    }
75
76    public void setTargetContactId(long targetContactId) {
77        this.mTargetContactId = targetContactId;
78    }
79
80    @Override
81    public void configureLoader(CursorLoader cursorLoader, long directoryId) {
82        JoinContactLoader loader = (JoinContactLoader)cursorLoader;
83        loader.setLoadSuggestionsAndAllContacts(mAllContactsListShown);
84
85        Builder builder = Contacts.CONTENT_URI.buildUpon();
86        builder.appendEncodedPath(String.valueOf(mTargetContactId));
87        builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
88
89        String filter = getQueryString();
90        if (!TextUtils.isEmpty(filter)) {
91            builder.appendEncodedPath(Uri.encode(filter));
92        }
93
94        builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
95
96        loader.setSuggestionUri(builder.build());
97
98        // TODO simplify projection
99        loader.setProjection(PROJECTION);
100        loader.setUri(buildSectionIndexerUri(Contacts.CONTENT_URI));
101        loader.setSelection(Contacts.IN_VISIBLE_GROUP + "=1 AND " + Contacts._ID + "!=?");
102        loader.setSelectionArgs(new String[]{String.valueOf(mTargetContactId)});
103        if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
104            loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
105        } else {
106            loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
107        }
108    }
109
110    @Override
111    public boolean isEmpty() {
112        return false;
113    }
114
115    public boolean isAllContactsListShown() {
116        return mAllContactsListShown;
117    }
118
119    public void setAllContactsListShown(boolean flag) {
120        mAllContactsListShown = flag;
121    }
122
123    public void setSuggestionsCursor(Cursor cursor) {
124        changeCursor(PARTITION_SUGGESTIONS, cursor);
125        if (cursor != null && cursor.getCount() != 0 && !mAllContactsListShown) {
126            changeCursor(PARTITION_SHOW_ALL_CONTACTS, getShowAllContactsLabelCursor());
127        } else {
128            changeCursor(PARTITION_SHOW_ALL_CONTACTS, null);
129        }
130    }
131
132    @Override
133    public void changeCursor(Cursor cursor) {
134        changeCursor(PARTITION_ALL_CONTACTS, cursor);
135    }
136
137    @Override
138    public void configureDefaultPartition(boolean showIfEmpty, boolean hasHeader) {
139         // Don't change default partition parameters from these defaults
140        super.configureDefaultPartition(false, true);
141    }
142
143    @Override
144    public int getViewTypeCount() {
145        return super.getViewTypeCount() + 1;
146    }
147
148    @Override
149    public int getItemViewType(int partition, int position) {
150        if (partition == PARTITION_SHOW_ALL_CONTACTS) {
151            return mShowAllContactsViewType;
152        }
153        return super.getItemViewType(partition, position);
154    }
155
156    @Override
157    protected View newHeaderView(Context context, int partition, Cursor cursor,
158            ViewGroup parent) {
159        switch (partition) {
160            case PARTITION_SUGGESTIONS: {
161              TextView view = (TextView) inflate(R.layout.list_separator, parent);
162              view.setText(R.string.separatorJoinAggregateSuggestions);
163              return view;
164            }
165            case PARTITION_ALL_CONTACTS: {
166              TextView view = (TextView) inflate(R.layout.list_separator, parent);
167              view.setText(R.string.separatorJoinAggregateAll);
168              return view;
169            }
170        }
171
172        return null;
173    }
174
175    @Override
176    protected void bindHeaderView(View view, int partitionIndex, Cursor cursor) {
177        // Header views are static - nothing needs to be bound
178    }
179
180    @Override
181    protected View newView(Context context, int partition, Cursor cursor, int position,
182            ViewGroup parent) {
183        switch (partition) {
184            case PARTITION_SUGGESTIONS:
185            case PARTITION_ALL_CONTACTS:
186                return super.newView(context, partition, cursor, position, parent);
187            case PARTITION_SHOW_ALL_CONTACTS:
188                return inflate(R.layout.contacts_list_show_all_item, parent);
189        }
190        return null;
191    }
192
193    private View inflate(int layoutId, ViewGroup parent) {
194        return LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
195    }
196
197    @Override
198    protected void bindView(View itemView, int partition, Cursor cursor, int position) {
199        switch (partition) {
200            case PARTITION_SUGGESTIONS: {
201                final ContactListItemView view = (ContactListItemView)itemView;
202                bindPhoto(view, cursor);
203                bindName(view, cursor);
204                break;
205            }
206            case PARTITION_SHOW_ALL_CONTACTS: {
207                break;
208            }
209            case PARTITION_ALL_CONTACTS: {
210                final ContactListItemView view = (ContactListItemView)itemView;
211                bindSectionHeaderAndDivider(view, position);
212                bindPhoto(view, cursor);
213                bindName(view, cursor);
214                break;
215            }
216        }
217    }
218
219    public Cursor getShowAllContactsLabelCursor() {
220        MatrixCursor matrixCursor = new MatrixCursor(PROJECTION);
221        Object[] row = new Object[PROJECTION.length];
222        matrixCursor.addRow(row);
223        return matrixCursor;
224    }
225
226    @Override
227    public Uri getContactUri(int partitionIndex, Cursor cursor) {
228        long contactId = cursor.getLong(CONTACT_ID_COLUMN_INDEX);
229        String lookupKey = cursor.getString(CONTACT_LOOKUP_KEY_COLUMN_INDEX);
230        return Contacts.getLookupUri(contactId, lookupKey);
231    }
232}
233