Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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.common.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 | |
Tingting Wang | 108f783 | 2016-07-19 17:42:35 -0700 | [diff] [blame] | 30 | protected static final String BLANK_HEADER_STRING = "\u2026"; // ellipsis |
| 31 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 32 | private String[] mSections; |
| 33 | private int[] mPositions; |
| 34 | private int mCount; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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 | */ |
| 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 Shao | da18871 | 2016-08-19 14:57:11 -0700 | [diff] [blame] | 58 | // Enforce that there will be no null or empty sections. |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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 | |
| 71 | public Object[] getSections() { |
| 72 | return mSections; |
| 73 | } |
| 74 | |
Wenyi Wang | a5cae5d | 2016-06-23 12:25:31 -0700 | [diff] [blame] | 75 | public int[] getPositions() { |
| 76 | return mPositions; |
| 77 | } |
| 78 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 79 | 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 Shao | da18871 | 2016-08-19 14:57:11 -0700 | [diff] [blame] | 105 | public void setFavoritesHeader(int numberOfItemsToAdd) { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 106 | if (mSections != null) { |
| 107 | // Don't do anything if the header is already set properly. |
John Shao | da18871 | 2016-08-19 14:57:11 -0700 | [diff] [blame] | 108 | if (mSections.length > 0 && mSections[0].isEmpty()) { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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]; |
John Shao | da18871 | 2016-08-19 14:57:11 -0700 | [diff] [blame] | 116 | // Favorites section is empty to hide fast scroll preview. |
| 117 | tempSections[0] = ""; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 118 | tempPositions[0] = 0; |
| 119 | for (int i = 1; i <= mPositions.length; i++) { |
| 120 | tempSections[i] = mSections[i - 1]; |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 121 | tempPositions[i] = mPositions[i - 1] + numberOfItemsToAdd; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 122 | } |
| 123 | mSections = tempSections; |
| 124 | mPositions = tempPositions; |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 125 | mCount = mCount + numberOfItemsToAdd; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |