blob: 6803e9d7f81d0d824f6f0dc461ab39278ec0534b [file] [log] [blame]
Katherine Kuan79700882011-06-14 17:40:33 -07001/*
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
17package com.android.contacts.detail;
18
Katherine Kuan79700882011-06-14 17:40:33 -070019import android.content.Context;
Flavio Lerda65ca8b22011-08-09 20:46:23 +010020import android.content.pm.PackageManager;
21import android.content.pm.PackageManager.NameNotFoundException;
22import android.content.res.Resources;
23import android.content.res.Resources.NotFoundException;
Flavio Lerda65ca8b22011-08-09 20:46:23 +010024import android.graphics.drawable.Drawable;
Dave Santoro39156002011-07-19 01:18:14 -070025import android.net.Uri;
Katherine Kuan79700882011-06-14 17:40:33 -070026import android.provider.ContactsContract.DisplayNameSources;
Brian Attwellc62cc792014-10-02 12:35:07 -070027import android.text.BidiFormatter;
Dave Santoro39156002011-07-19 01:18:14 -070028import android.text.Html;
Brian Attwellc62cc792014-10-02 12:35:07 -070029import android.text.TextDirectionHeuristics;
Katherine Kuan79700882011-06-14 17:40:33 -070030import android.text.TextUtils;
Flavio Lerda65ca8b22011-08-09 20:46:23 +010031import android.util.Log;
Daniel Lehmannc42ea4e2012-02-16 21:22:37 -080032import android.view.MenuItem;
Katherine Kuan79700882011-06-14 17:40:33 -070033import android.view.View;
Katherine Kuan6c0470e2011-08-17 12:33:18 -070034import android.widget.ListView;
Katherine Kuan79700882011-06-14 17:40:33 -070035import android.widget.TextView;
36
Gary Mai0a49afa2016-12-05 15:53:58 -080037import com.android.contacts.R;
38import com.android.contacts.model.Contact;
39import com.android.contacts.model.RawContact;
40import com.android.contacts.model.dataitem.DataItem;
41import com.android.contacts.model.dataitem.OrganizationDataItem;
42import com.android.contacts.preference.ContactsPreferences;
43import com.android.contacts.util.MoreMath;
Gary Mai0a49afa2016-12-05 15:53:58 -080044import com.google.common.collect.Iterables;
45
Dave Santoro39156002011-07-19 01:18:14 -070046import java.util.List;
47
Katherine Kuan79700882011-06-14 17:40:33 -070048/**
49 * This class contains utility methods to bind high-level contact details
50 * (meaning name, phonetic name, job, and attribution) from a
Maurice Chu851222a2012-06-21 11:43:08 -070051 * {@link Contact} data object to appropriate {@link View}s.
Katherine Kuan79700882011-06-14 17:40:33 -070052 */
Paul Soulos333091a2014-07-22 13:54:41 -070053public class ContactDisplayUtils {
54 private static final String TAG = "ContactDisplayUtils";
Brian Attwellc62cc792014-10-02 12:35:07 -070055 private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
Flavio Lerda65ca8b22011-08-09 20:46:23 +010056
Makoto Onukif748d592011-08-18 17:22:39 -070057 /**
Makoto Onuki599700f2011-08-08 18:43:20 -070058 * Returns the display name of the contact, using the current display order setting.
59 * Returns res/string/missing_name if there is no display name.
Katherine Kuan79700882011-06-14 17:40:33 -070060 */
Maurice Chu851222a2012-06-21 11:43:08 -070061 public static CharSequence getDisplayName(Context context, Contact contactData) {
Katherine Kuan79700882011-06-14 17:40:33 -070062 ContactsPreferences prefs = new ContactsPreferences(context);
Alon Alberte09b9912013-09-05 12:58:45 -070063 final CharSequence displayName = contactData.getDisplayName();
Yorke Leec9bb2172014-07-10 11:38:34 -070064 if (prefs.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
Alon Alberte09b9912013-09-05 12:58:45 -070065 if (!TextUtils.isEmpty(displayName)) {
Brian Attwellc62cc792014-10-02 12:35:07 -070066 if (contactData.getDisplayNameSource() == DisplayNameSources.PHONE) {
67 return sBidiFormatter.unicodeWrap(
68 displayName.toString(), TextDirectionHeuristics.LTR);
69 }
Alon Alberte09b9912013-09-05 12:58:45 -070070 return displayName;
Katherine Kuan79700882011-06-14 17:40:33 -070071 }
Katherine Kuan81281ee2011-07-28 16:20:59 -070072 } else {
Alon Alberte09b9912013-09-05 12:58:45 -070073 final CharSequence altDisplayName = contactData.getAltDisplayName();
74 if (!TextUtils.isEmpty(altDisplayName)) {
75 return altDisplayName;
76 }
Katherine Kuan79700882011-06-14 17:40:33 -070077 }
Alon Alberte09b9912013-09-05 12:58:45 -070078 return context.getResources().getString(R.string.missing_name);
Katherine Kuan79700882011-06-14 17:40:33 -070079 }
80
81 /**
82 * Returns the phonetic name of the contact or null if there isn't one.
83 */
Maurice Chu851222a2012-06-21 11:43:08 -070084 public static String getPhoneticName(Context context, Contact contactData) {
Katherine Kuan79700882011-06-14 17:40:33 -070085 String phoneticName = contactData.getPhoneticName();
86 if (!TextUtils.isEmpty(phoneticName)) {
87 return phoneticName;
88 }
89 return null;
90 }
91
92 /**
Katherine Kuanafd283a2011-08-22 16:55:19 -070093 * Returns the attribution string for the contact, which may specify the contact directory that
94 * the contact came from. Returns null if there is none applicable.
Katherine Kuan79700882011-06-14 17:40:33 -070095 */
Maurice Chu851222a2012-06-21 11:43:08 -070096 public static String getAttribution(Context context, Contact contactData) {
Katherine Kuanafd283a2011-08-22 16:55:19 -070097 if (contactData.isDirectoryEntry()) {
Katherine Kuan79700882011-06-14 17:40:33 -070098 String directoryDisplayName = contactData.getDirectoryDisplayName();
99 String directoryType = contactData.getDirectoryType();
Yorke Lee2e385832013-09-09 14:42:01 -0700100 final String displayName;
101 if (!TextUtils.isEmpty(directoryDisplayName)) {
102 displayName = directoryDisplayName;
103 } else if (!TextUtils.isEmpty(directoryType)) {
104 displayName = directoryType;
105 } else {
106 return null;
107 }
Katherine Kuan79700882011-06-14 17:40:33 -0700108 return context.getString(R.string.contact_directory_description, displayName);
109 }
110 return null;
111 }
112
113 /**
114 * Returns the organization of the contact. If several organizations are given,
115 * the first one is used. Returns null if not applicable.
116 */
Maurice Chu851222a2012-06-21 11:43:08 -0700117 public static String getCompany(Context context, Contact contactData) {
Katherine Kuan79700882011-06-14 17:40:33 -0700118 final boolean displayNameIsOrganization = contactData.getDisplayNameSource()
119 == DisplayNameSources.ORGANIZATION;
Maurice Chu851222a2012-06-21 11:43:08 -0700120 for (RawContact rawContact : contactData.getRawContacts()) {
121 for (DataItem dataItem : Iterables.filter(
122 rawContact.getDataItems(), OrganizationDataItem.class)) {
123 OrganizationDataItem organization = (OrganizationDataItem) dataItem;
124 final String company = organization.getCompany();
125 final String title = organization.getTitle();
126 final String combined;
127 // We need to show company and title in a combined string. However, if the
128 // DisplayName is already the organization, it mirrors company or (if company
129 // is empty title). Make sure we don't show what's already shown as DisplayName
130 if (TextUtils.isEmpty(company)) {
131 combined = displayNameIsOrganization ? null : title;
132 } else {
133 if (TextUtils.isEmpty(title)) {
134 combined = displayNameIsOrganization ? null : company;
Katherine Kuan79700882011-06-14 17:40:33 -0700135 } else {
Maurice Chu851222a2012-06-21 11:43:08 -0700136 if (displayNameIsOrganization) {
137 combined = title;
Katherine Kuan79700882011-06-14 17:40:33 -0700138 } else {
Maurice Chu851222a2012-06-21 11:43:08 -0700139 combined = context.getString(
140 R.string.organization_company_and_title,
141 company, title);
Katherine Kuan79700882011-06-14 17:40:33 -0700142 }
143 }
Maurice Chu851222a2012-06-21 11:43:08 -0700144 }
Katherine Kuan79700882011-06-14 17:40:33 -0700145
Maurice Chu851222a2012-06-21 11:43:08 -0700146 if (!TextUtils.isEmpty(combined)) {
147 return combined;
Katherine Kuan79700882011-06-14 17:40:33 -0700148 }
149 }
150 }
151 return null;
152 }
153
Katherine Kuan79700882011-06-14 17:40:33 -0700154 /**
Katherine Kuan79700882011-06-14 17:40:33 -0700155 * Sets the starred state of this contact.
156 */
Daniel Lehmannc42ea4e2012-02-16 21:22:37 -0800157 public static void configureStarredMenuItem(MenuItem starredMenuItem, boolean isDirectoryEntry,
158 boolean isUserProfile, boolean isStarred) {
159 // Check if the starred state should be visible
160 if (!isDirectoryEntry && !isUserProfile) {
161 starredMenuItem.setVisible(true);
162 final int resId = isStarred
John Shaobd9ef3c2016-12-15 12:42:03 -0800163 ? R.drawable.quantum_ic_star_vd_theme_24
164 : R.drawable.quantum_ic_star_border_vd_theme_24;
Daniel Lehmannc42ea4e2012-02-16 21:22:37 -0800165 starredMenuItem.setIcon(resId);
166 starredMenuItem.setChecked(isStarred);
167 starredMenuItem.setTitle(isStarred ? R.string.menu_removeStar : R.string.menu_addStar);
168 } else {
169 starredMenuItem.setVisible(false);
170 }
171 }
172
173 /**
Katherine Kuan2eb969c2011-06-28 11:43:15 -0700174 * Sets the display name of this contact to the given {@link TextView}. If
175 * there is none, then set the view to gone.
176 */
Maurice Chu851222a2012-06-21 11:43:08 -0700177 public static void setDisplayName(Context context, Contact contactData, TextView textView) {
Katherine Kuan2eb969c2011-06-28 11:43:15 -0700178 if (textView == null) {
179 return;
180 }
181 setDataOrHideIfNone(getDisplayName(context, contactData), textView);
182 }
183
184 /**
185 * Sets the company and job title of this contact to the given {@link TextView}. If
186 * there is none, then set the view to gone.
187 */
Maurice Chu851222a2012-06-21 11:43:08 -0700188 public static void setCompanyName(Context context, Contact contactData, TextView textView) {
Katherine Kuan2eb969c2011-06-28 11:43:15 -0700189 if (textView == null) {
190 return;
191 }
192 setDataOrHideIfNone(getCompany(context, contactData), textView);
193 }
194
195 /**
Katherine Kuan79700882011-06-14 17:40:33 -0700196 * Sets the phonetic name of this contact to the given {@link TextView}. If
197 * there is none, then set the view to gone.
198 */
Maurice Chu851222a2012-06-21 11:43:08 -0700199 public static void setPhoneticName(Context context, Contact contactData, TextView textView) {
Katherine Kuan2eb969c2011-06-28 11:43:15 -0700200 if (textView == null) {
201 return;
202 }
Katherine Kuan79700882011-06-14 17:40:33 -0700203 setDataOrHideIfNone(getPhoneticName(context, contactData), textView);
204 }
205
206 /**
207 * Sets the attribution contact to the given {@link TextView}. If
208 * there is none, then set the view to gone.
209 */
Maurice Chu851222a2012-06-21 11:43:08 -0700210 public static void setAttribution(Context context, Contact contactData, TextView textView) {
Katherine Kuan2eb969c2011-06-28 11:43:15 -0700211 if (textView == null) {
212 return;
213 }
Katherine Kuan79700882011-06-14 17:40:33 -0700214 setDataOrHideIfNone(getAttribution(context, contactData), textView);
215 }
216
217 /**
218 * Helper function to display the given text in the {@link TextView} or
219 * hides the {@link TextView} if the text is empty or null.
220 */
221 private static void setDataOrHideIfNone(CharSequence textToDisplay, TextView textView) {
222 if (!TextUtils.isEmpty(textToDisplay)) {
223 textView.setText(textToDisplay);
224 textView.setVisibility(View.VISIBLE);
225 } else {
226 textView.setText(null);
227 textView.setVisibility(View.GONE);
228 }
229 }
230
Makoto Onukida9cdc12012-02-27 16:11:50 -0800231 private static Html.ImageGetter sImageGetter;
232
233 public static Html.ImageGetter getImageGetter(Context context) {
234 if (sImageGetter == null) {
235 sImageGetter = new DefaultImageGetter(context.getPackageManager());
236 }
237 return sImageGetter;
238 }
239
Flavio Lerda65ca8b22011-08-09 20:46:23 +0100240 /** Fetcher for images from resources to be included in HTML text. */
241 private static class DefaultImageGetter implements Html.ImageGetter {
242 /** The scheme used to load resources. */
243 private static final String RES_SCHEME = "res";
244
245 private final PackageManager mPackageManager;
246
247 public DefaultImageGetter(PackageManager packageManager) {
248 mPackageManager = packageManager;
249 }
250
251 @Override
252 public Drawable getDrawable(String source) {
253 // Returning null means that a default image will be used.
254 Uri uri;
255 try {
256 uri = Uri.parse(source);
257 } catch (Throwable e) {
258 Log.d(TAG, "Could not parse image source: " + source);
259 return null;
260 }
261 if (!RES_SCHEME.equals(uri.getScheme())) {
262 Log.d(TAG, "Image source does not correspond to a resource: " + source);
263 return null;
264 }
265 // The URI authority represents the package name.
266 String packageName = uri.getAuthority();
267
268 Resources resources = getResourcesForResourceName(packageName);
269 if (resources == null) {
270 Log.d(TAG, "Could not parse image source: " + source);
271 return null;
272 }
273
274 List<String> pathSegments = uri.getPathSegments();
275 if (pathSegments.size() != 1) {
276 Log.d(TAG, "Could not parse image source: " + source);
277 return null;
278 }
279
280 final String name = pathSegments.get(0);
281 final int resId = resources.getIdentifier(name, "drawable", packageName);
282
283 if (resId == 0) {
284 // Use the default image icon in this case.
285 Log.d(TAG, "Cannot resolve resource identifier: " + source);
286 return null;
287 }
288
289 try {
290 return getResourceDrawable(resources, resId);
291 } catch (NotFoundException e) {
292 Log.d(TAG, "Resource not found: " + source, e);
293 return null;
294 }
295 }
296
297 /** Returns the drawable associated with the given id. */
298 private Drawable getResourceDrawable(Resources resources, int resId)
299 throws NotFoundException {
300 Drawable drawable = resources.getDrawable(resId);
301 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
302 return drawable;
303 }
304
305 /** Returns the {@link Resources} of the package of the given resource name. */
306 private Resources getResourcesForResourceName(String packageName) {
307 try {
308 return mPackageManager.getResourcesForApplication(packageName);
309 } catch (NameNotFoundException e) {
310 Log.d(TAG, "Could not find package: " + packageName);
311 return null;
312 }
313 }
314 }
Katherine Kuan25594d62011-08-12 16:05:15 -0700315
316 /**
317 * Sets an alpha value on the view.
318 */
319 public static void setAlphaOnViewBackground(View view, float alpha) {
320 if (view != null) {
321 // Convert alpha layer to a black background HEX color with an alpha value for better
322 // performance (i.e. use setBackgroundColor() instead of setAlpha())
Josh Gargus187c8162012-03-13 17:06:53 -0700323 view.setBackgroundColor((int) (MoreMath.clamp(alpha, 0.0f, 1.0f) * 255) << 24);
Katherine Kuan25594d62011-08-12 16:05:15 -0700324 }
325 }
Katherine Kuan6c0470e2011-08-17 12:33:18 -0700326
327 /**
328 * Returns the top coordinate of the first item in the {@link ListView}. If the first item
329 * in the {@link ListView} is not visible or there are no children in the list, then return
330 * Integer.MIN_VALUE. Note that the returned value will be <= 0 because the first item in the
331 * list cannot have a positive offset.
332 */
333 public static int getFirstListItemOffset(ListView listView) {
334 if (listView == null || listView.getChildCount() == 0 ||
335 listView.getFirstVisiblePosition() != 0) {
336 return Integer.MIN_VALUE;
337 }
338 return listView.getChildAt(0).getTop();
339 }
340
341 /**
342 * Tries to scroll the first item in the list to the given offset (this can be a no-op if the
343 * list is already in the correct position).
344 * @param listView that should be scrolled
345 * @param offset which should be <= 0
346 */
347 public static void requestToMoveToOffset(ListView listView, int offset) {
348 // We try to offset the list if the first item in the list is showing (which is presumed
349 // to have a larger height than the desired offset). If the first item in the list is not
350 // visible, then we simply do not scroll the list at all (since it can get complicated to
351 // compute how many items in the list will equal the given offset). Potentially
352 // some animation elsewhere will make the transition smoother for the user to compensate
353 // for this simplification.
354 if (listView == null || listView.getChildCount() == 0 ||
355 listView.getFirstVisiblePosition() != 0 || offset > 0) {
356 return;
357 }
358
359 // As an optimization, check if the first item is already at the given offset.
360 if (listView.getChildAt(0).getTop() == offset) {
361 return;
362 }
363
364 listView.setSelectionFromTop(0, offset);
365 }
Flavio Lerda22cb6632011-08-03 22:59:58 +0100366}