• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.format;
18 
19 import com.android.contacts.widget.TextWithHighlighting;
20 import com.android.contacts.widget.TextWithHighlightingFactory;
21 
22 import android.database.CharArrayBuffer;
23 import android.widget.TextView;
24 
25 /**
26  * Sets the content of the given text view, to contain the formatted display name, with a
27  * prefix if necessary.
28  */
29 public final class DisplayNameFormatter {
30     private final CharArrayBuffer mNameBuffer = new CharArrayBuffer(128);
31     private final CharArrayBuffer mAlternateNameBuffer = new CharArrayBuffer(128);
32     private final PrefixHighlighter mPrefixHighlighter;
33 
34     private TextWithHighlightingFactory mTextWithHighlightingFactory;
35     private TextWithHighlighting mTextWithHighlighting;
36     private CharSequence mUnknownNameText;
37 
DisplayNameFormatter(PrefixHighlighter prefixHighlighter)38     public DisplayNameFormatter(PrefixHighlighter prefixHighlighter) {
39         mPrefixHighlighter = prefixHighlighter;
40     }
41 
getNameBuffer()42     public CharArrayBuffer getNameBuffer() {
43         return mNameBuffer;
44     }
45 
getAlternateNameBuffer()46     public CharArrayBuffer getAlternateNameBuffer() {
47         return mAlternateNameBuffer;
48     }
49 
setUnknownNameText(CharSequence unknownNameText)50     public void setUnknownNameText(CharSequence unknownNameText) {
51         mUnknownNameText = unknownNameText;
52     }
53 
setDisplayName(TextView view, int displayOrder, boolean highlightingEnabled, char[] highlightedPrefix)54     public void setDisplayName(TextView view, int displayOrder,
55             boolean highlightingEnabled, char[] highlightedPrefix) {
56         view.setText(getDisplayName(displayOrder, highlightingEnabled, highlightedPrefix));
57     }
58 
getDisplayName(int displayOrder, boolean highlightingEnabled, char[] highlightedPrefix)59     public CharSequence getDisplayName(int displayOrder, boolean highlightingEnabled,
60             char[] highlightedPrefix) {
61         int size = mNameBuffer.sizeCopied;
62         if (size == 0) {
63             return mUnknownNameText;
64         }
65 
66         CharSequence text;
67         if (highlightingEnabled) {
68             if (mTextWithHighlighting == null) {
69                 mTextWithHighlighting =
70                         mTextWithHighlightingFactory.createTextWithHighlighting();
71             }
72             mTextWithHighlighting.setText(mNameBuffer, mAlternateNameBuffer);
73             text = mTextWithHighlighting;
74         } else {
75             text = FormatUtils.charArrayBufferToString(mNameBuffer);
76         }
77         if (highlightedPrefix != null) {
78             text = mPrefixHighlighter.apply(text, highlightedPrefix);
79         }
80         return text;
81     }
82 }
83