blob: 031380d4ee65389e58e538792d48b0724c39b58b [file] [log] [blame]
Chiao Cheng89437e82012-11-01 13:41:51 -07001/*
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
Tingting Wang108f7832016-07-19 17:42:35 -070030 protected static final String BLANK_HEADER_STRING = "\u2026"; // ellipsis
31
Chiao Cheng89437e82012-11-01 13:41:51 -070032 private String[] mSections;
33 private int[] mPositions;
34 private int mCount;
Chiao Cheng89437e82012-11-01 13:41:51 -070035
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 */
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++) {
John Shaoda188712016-08-19 14:57:11 -070058 // Enforce that there will be no null or empty sections.
Chiao Cheng89437e82012-11-01 13:41:51 -070059 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
71 public Object[] getSections() {
72 return mSections;
73 }
74
Wenyi Wanga5cae5d2016-06-23 12:25:31 -070075 public int[] getPositions() {
76 return mPositions;
77 }
78
Chiao Cheng89437e82012-11-01 13:41:51 -070079 public int getPositionForSection(int section) {
80 if (section < 0 || section >= mSections.length) {
81 return -1;
82 }
83
84 return mPositions[section];
85 }
86
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
John Shaoda188712016-08-19 14:57:11 -0700105 public void setFavoritesHeader(int numberOfItemsToAdd) {
Chiao Cheng89437e82012-11-01 13:41:51 -0700106 if (mSections != null) {
107 // Don't do anything if the header is already set properly.
John Shaoda188712016-08-19 14:57:11 -0700108 if (mSections.length > 0 && mSections[0].isEmpty()) {
Chiao Cheng89437e82012-11-01 13:41:51 -0700109 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];
John Shaoda188712016-08-19 14:57:11 -0700116 // Favorites section is empty to hide fast scroll preview.
117 tempSections[0] = "";
Chiao Cheng89437e82012-11-01 13:41:51 -0700118 tempPositions[0] = 0;
119 for (int i = 1; i <= mPositions.length; i++) {
120 tempSections[i] = mSections[i - 1];
Wenyi Wang25774d22016-04-08 11:15:11 -0700121 tempPositions[i] = mPositions[i - 1] + numberOfItemsToAdd;
Chiao Cheng89437e82012-11-01 13:41:51 -0700122 }
123 mSections = tempSections;
124 mPositions = tempPositions;
Wenyi Wang25774d22016-04-08 11:15:11 -0700125 mCount = mCount + numberOfItemsToAdd;
Chiao Cheng89437e82012-11-01 13:41:51 -0700126 }
127 }
128}