summaryrefslogtreecommitdiff
path: root/src/com/android/dialer/list/PhoneFavoriteMergedAdapter.java
blob: 8e23399615f1c3dfad84302a14f1e2623963d470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (C) 2011 Google Inc.
 * Licensed to The Android Open Source Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.dialer.list;

import android.content.Context;
import android.content.res.Resources;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.SectionIndexer;

import com.android.contacts.R;
import com.android.contacts.list.ContactEntryListAdapter;
import com.android.contacts.list.ContactListItemView;
import com.android.contacts.list.ContactTileAdapter;

/**
 * An adapter that combines items from {@link com.android.contacts.list.ContactTileAdapter} and
 * {@link com.android.contacts.list.ContactEntryListAdapter} into a single list. In between those two results,
 * an account filter header will be inserted.
 */
public class PhoneFavoriteMergedAdapter extends BaseAdapter implements SectionIndexer {

    private class CustomDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            notifyDataSetChanged();
        }
    }

    private final ContactTileAdapter mContactTileAdapter;
    private final ContactEntryListAdapter mContactEntryListAdapter;
    private final View mAccountFilterHeaderContainer;
    private final View mLoadingView;

    private final int mItemPaddingLeft;
    private final int mItemPaddingRight;

    // Make frequent header consistent with account filter header.
    private final int mFrequentHeaderPaddingTop;

    private final DataSetObserver mObserver;

    public PhoneFavoriteMergedAdapter(Context context,
            ContactTileAdapter contactTileAdapter,
            View accountFilterHeaderContainer,
            ContactEntryListAdapter contactEntryListAdapter,
            View loadingView) {
        Resources resources = context.getResources();
        mItemPaddingLeft = resources.getDimensionPixelSize(R.dimen.detail_item_side_margin);
        mItemPaddingRight = resources.getDimensionPixelSize(R.dimen.list_visible_scrollbar_padding);
        mFrequentHeaderPaddingTop = resources.getDimensionPixelSize(
                R.dimen.contact_browser_list_top_margin);
        mContactTileAdapter = contactTileAdapter;
        mContactEntryListAdapter = contactEntryListAdapter;

        mAccountFilterHeaderContainer = accountFilterHeaderContainer;

        mObserver = new CustomDataSetObserver();
        mContactTileAdapter.registerDataSetObserver(mObserver);
        mContactEntryListAdapter.registerDataSetObserver(mObserver);

        mLoadingView = loadingView;
    }

    @Override
    public boolean isEmpty() {
        // Cannot use the super's method here because we add extra rows in getCount() to account
        // for headers
        return mContactTileAdapter.getCount() + mContactEntryListAdapter.getCount() == 0;
    }

    @Override
    public int getCount() {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        final int contactEntryListAdapterCount = mContactEntryListAdapter.getCount();
        if (mContactEntryListAdapter.isLoading()) {
            // Hide "all" contacts during its being loaded. Instead show "loading" view.
            //
            // "+2" for mAccountFilterHeaderContainer and mLoadingView
            return contactTileAdapterCount + 2;
        } else {
            // "+1" for mAccountFilterHeaderContainer
            return contactTileAdapterCount + contactEntryListAdapterCount + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        final int contactEntryListAdapterCount = mContactEntryListAdapter.getCount();
        if (position < contactTileAdapterCount) {  // For "tile" and "frequent" sections
            return mContactTileAdapter.getItem(position);
        } else if (position == contactTileAdapterCount) {  // For "all" section's account header
            return mAccountFilterHeaderContainer;
        } else {  // For "all" section
            if (mContactEntryListAdapter.isLoading()) {  // "All" section is being loaded.
                return mLoadingView;
            } else {
                // "-1" for mAccountFilterHeaderContainer
                final int localPosition = position - contactTileAdapterCount - 1;
                return mContactTileAdapter.getItem(localPosition);
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        // "+2" for mAccountFilterHeaderContainer and mLoadingView
        return (mContactTileAdapter.getViewTypeCount()
                + mContactEntryListAdapter.getViewTypeCount()
                + 2);
    }

    @Override
    public int getItemViewType(int position) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        final int contactEntryListAdapterCount = mContactEntryListAdapter.getCount();
        // There should be four kinds of types that are usually used, and one more exceptional
        // type (IGNORE_ITEM_VIEW_TYPE), which sometimes comes from mContactTileAdapter.
        //
        // The four ordinary view types have the index equal to or more than 0, and less than
        // mContactTileAdapter.getViewTypeCount()+ mContactEntryListAdapter.getViewTypeCount() + 2.
        // (See also this class's getViewTypeCount())
        //
        // We have those values for:
        // - The view types mContactTileAdapter originally has
        // - The view types mContactEntryListAdapter originally has
        // - mAccountFilterHeaderContainer ("all" section's account header), and
        // - mLoadingView
        //
        // Those types should not be mixed, so we have a different range for each kinds of types:
        // - Types for mContactTileAdapter ("tile" and "frequent" sections)
        //   They should have the index, >=0 and <mContactTileAdapter.getViewTypeCount()
        //
        // - Types for mContactEntryListAdapter ("all" sections)
        //   They should have the index, >=mContactTileAdapter.getViewTypeCount() and
        //   <(mContactTileAdapter.getViewTypeCount() + mContactEntryListAdapter.getViewTypeCount())
        //
        // - Type for "all" section's account header
        //   It should have the exact index
        //   mContactTileAdapter.getViewTypeCount()+ mContactEntryListAdapter.getViewTypeCount()
        //
        // - Type for "loading" view used during "all" section is being loaded.
        //   It should have the exact index
        //   mContactTileAdapter.getViewTypeCount()+ mContactEntryListAdapter.getViewTypeCount() + 1
        //
        // As an exception, IGNORE_ITEM_VIEW_TYPE (-1) will be remained as is, which will be used
        // by framework's Adapter implementation and thus should be left as is.
        if (position < contactTileAdapterCount) {  // For "tile" and "frequent" sections
            return mContactTileAdapter.getItemViewType(position);
        } else if (position == contactTileAdapterCount) {  // For "all" section's account header
            return mContactTileAdapter.getViewTypeCount()
                    + mContactEntryListAdapter.getViewTypeCount();
        } else {  // For "all" section
            if (mContactEntryListAdapter.isLoading()) {  // "All" section is being loaded.
                return mContactTileAdapter.getViewTypeCount()
                        + mContactEntryListAdapter.getViewTypeCount() + 1;
            } else {
                // "-1" for mAccountFilterHeaderContainer
                final int localPosition = position - contactTileAdapterCount - 1;
                final int type = mContactEntryListAdapter.getItemViewType(localPosition);
                // IGNORE_ITEM_VIEW_TYPE must be handled differently.
                return (type < 0) ? type : type + mContactTileAdapter.getViewTypeCount();
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        final int contactEntryListAdapterCount = mContactEntryListAdapter.getCount();

        // Obtain a View relevant for that position, and adjust its horizontal padding. Each
        // View has different implementation, so we use different way to control those padding.
        if (position < contactTileAdapterCount) {  // For "tile" and "frequent" sections
            final View view = mContactTileAdapter.getView(position, convertView, parent);
            final int frequentHeaderPosition = mContactTileAdapter.getFrequentHeaderPosition();
            if (position < frequentHeaderPosition) {  // "starred" contacts
                // No padding adjustment.
            } else if (position == frequentHeaderPosition) {
                view.setPadding(mItemPaddingLeft, mFrequentHeaderPaddingTop,
                        mItemPaddingRight, view.getPaddingBottom());
            } else {
                // Views for "frequent" contacts use FrameLayout's margins instead of padding.
                final FrameLayout frameLayout = (FrameLayout) view;
                final View child = frameLayout.getChildAt(0);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.WRAP_CONTENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(mItemPaddingLeft, 0, mItemPaddingRight, 0);
                child.setLayoutParams(params);
            }
            return view;
        } else if (position == contactTileAdapterCount) {  // For "all" section's account header
            mAccountFilterHeaderContainer.setPadding(mItemPaddingLeft,
                    mAccountFilterHeaderContainer.getPaddingTop(),
                    mItemPaddingRight,
                    mAccountFilterHeaderContainer.getPaddingBottom());
            return mAccountFilterHeaderContainer;
        } else {  // For "all" section
            if (mContactEntryListAdapter.isLoading()) {  // "All" section is being loaded.
                mLoadingView.setPadding(mItemPaddingLeft,
                        mLoadingView.getPaddingTop(),
                        mItemPaddingRight,
                        mLoadingView.getPaddingBottom());
                return mLoadingView;
            } else {
                // "-1" for mAccountFilterHeaderContainer
                final int localPosition = position - contactTileAdapterCount - 1;
                final ContactListItemView itemView = (ContactListItemView)
                        mContactEntryListAdapter.getView(localPosition, convertView, null);
                itemView.setPadding(mItemPaddingLeft, itemView.getPaddingTop(),
                        mItemPaddingRight, itemView.getPaddingBottom());
                itemView.setSelectionBoundsHorizontalMargin(mItemPaddingLeft, mItemPaddingRight);
                return itemView;
            }
        }
    }

    @Override
    public boolean areAllItemsEnabled() {
        // If "all" section is being loaded we'll show mLoadingView, which is not enabled.
        // Otherwise check the all the other components in the ListView and return appropriate
        // result.
        return !mContactEntryListAdapter.isLoading()
                && (mContactTileAdapter.areAllItemsEnabled()
                && mAccountFilterHeaderContainer.isEnabled()
                && mContactEntryListAdapter.areAllItemsEnabled());
    }

    @Override
    public boolean isEnabled(int position) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        final int contactEntryListAdapterCount = mContactEntryListAdapter.getCount();
        if (position < contactTileAdapterCount) {  // For "tile" and "frequent" sections
            return mContactTileAdapter.isEnabled(position);
        } else if (position == contactTileAdapterCount) {  // For "all" section's account header
            // This will be handled by View's onClick event instead of ListView's onItemClick event.
            return false;
        } else {  // For "all" section
            if (mContactEntryListAdapter.isLoading()) {  // "All" section is being loaded.
                return false;
            } else {
                // "-1" for mAccountFilterHeaderContainer
                final int localPosition = position - contactTileAdapterCount - 1;
                return mContactEntryListAdapter.isEnabled(localPosition);
            }
        }
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        final int localPosition = mContactEntryListAdapter.getPositionForSection(sectionIndex);
        return contactTileAdapterCount + 1 + localPosition;
    }

    @Override
    public int getSectionForPosition(int position) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        if (position <= contactTileAdapterCount) {
            return 0;
        } else {
            // "-1" for mAccountFilterHeaderContainer
            final int localPosition = position - contactTileAdapterCount - 1;
            return mContactEntryListAdapter.getSectionForPosition(localPosition);
        }
    }

    @Override
    public Object[] getSections() {
        return mContactEntryListAdapter.getSections();
    }

    public boolean shouldShowFirstScroller(int firstVisibleItem) {
        final int contactTileAdapterCount = mContactTileAdapter.getCount();
        return firstVisibleItem > contactTileAdapterCount;
    }
}