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