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 */
16
17package com.android.contacts.common.list;
18
19import android.text.TextUtils;
20import android.widget.SectionIndexer;
21
22import java.util.Arrays;
23
24/**
25 * A section indexer that is configured with precomputed section titles and
26 * their respective counts.
27 */
28public class ContactsSectionIndexer implements SectionIndexer {
29
30    private String[] mSections;
31    private int[] mPositions;
32    private int mCount;
33    private static final String BLANK_HEADER_STRING = " ";
34
35    /**
36     * Constructor.
37     *
38     * @param sections a non-null array
39     * @param counts a non-null array of the same size as <code>sections</code>
40     */
41    public ContactsSectionIndexer(String[] sections, int[] counts) {
42        if (sections == null || counts == null) {
43            throw new NullPointerException();
44        }
45
46        if (sections.length != counts.length) {
47            throw new IllegalArgumentException(
48                    "The sections and counts arrays must have the same length");
49        }
50
51        // TODO process sections/counts based on current locale and/or specific section titles
52
53        this.mSections = sections;
54        mPositions = new int[counts.length];
55        int position = 0;
56        for (int i = 0; i < counts.length; i++) {
57            if (TextUtils.isEmpty(mSections[i])) {
58                mSections[i] = BLANK_HEADER_STRING;
59            } else if (!mSections[i].equals(BLANK_HEADER_STRING)) {
60                mSections[i] = mSections[i].trim();
61            }
62
63            mPositions[i] = position;
64            position += counts[i];
65        }
66        mCount = position;
67    }
68
69    public Object[] getSections() {
70        return mSections;
71    }
72
73    public int getPositionForSection(int section) {
74        if (section < 0 || section >= mSections.length) {
75            return -1;
76        }
77
78        return mPositions[section];
79    }
80
81    public int getSectionForPosition(int position) {
82        if (position < 0 || position >= mCount) {
83            return -1;
84        }
85
86        int index = Arrays.binarySearch(mPositions, position);
87
88        /*
89         * Consider this example: section positions are 0, 3, 5; the supplied
90         * position is 4. The section corresponding to position 4 starts at
91         * position 3, so the expected return value is 1. Binary search will not
92         * find 4 in the array and thus will return -insertPosition-1, i.e. -3.
93         * To get from that number to the expected value of 1 we need to negate
94         * and subtract 2.
95         */
96        return index >= 0 ? index : -index - 2;
97    }
98
99    public void setProfileHeader(String header) {
100        if (mSections != null) {
101            // Don't do anything if the header is already set properly.
102            if (mSections.length > 0 && header.equals(mSections[0])) {
103                return;
104            }
105
106            // Since the section indexer isn't aware of the profile at the top, we need to add a
107            // special section at the top for it and shift everything else down.
108            String[] tempSections = new String[mSections.length + 1];
109            int[] tempPositions = new int[mPositions.length + 1];
110            tempSections[0] = header;
111            tempPositions[0] = 0;
112            for (int i = 1; i <= mPositions.length; i++) {
113                tempSections[i] = mSections[i - 1];
114                tempPositions[i] = mPositions[i - 1] + 1;
115            }
116            mSections = tempSections;
117            mPositions = tempPositions;
118            mCount++;
119        }
120    }
121}
122