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