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.util; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.content.pm.PackageManager.NameNotFoundException; 22 import android.content.res.Resources; 23 import android.graphics.Bitmap; 24 import android.graphics.BitmapFactory; 25 import android.text.TextUtils; 26 import android.text.format.DateUtils; 27 import android.util.Log; 28 29 import com.android.contacts.common.ContactPhotoManager; 30 import com.android.contacts.util.StreamItemEntry; 31 import com.android.contacts.R; 32 33 /** 34 * Provides static functions to extract summary information for aggregate contacts 35 */ 36 public class ContactBadgeUtil { 37 private static final String TAG = "ContactBadgeUtil"; 38 39 /** 40 * Returns the social snippet attribution for the given stream item entry, including the date. 41 */ 42 public static CharSequence getSocialDate(StreamItemEntry streamItem, Context context) { 43 final CharSequence timestampDisplayValue; 44 final Long statusTimestamp = streamItem.getTimestamp(); 45 if (statusTimestamp != null) { 46 // Set the date/time field by mixing relative and absolute 47 // times. 48 int flags = DateUtils.FORMAT_ABBREV_RELATIVE; 49 50 timestampDisplayValue = DateUtils.getRelativeTimeSpanString( 51 statusTimestamp.longValue(), System.currentTimeMillis(), 52 DateUtils.MINUTE_IN_MILLIS, flags); 53 } else { 54 timestampDisplayValue = null; 55 } 56 57 58 String labelDisplayValue = null; 59 60 final String statusLabelRes = streamItem.getLabelRes(); 61 final String statusResPackage = streamItem.getResPackage(); 62 63 // Package name used for resources.getIdentifier() 64 String identiferPackage = statusResPackage; 65 if (statusLabelRes != null) { 66 Resources resources; 67 if (TextUtils.isEmpty(statusResPackage)) { 68 resources = context.getResources(); 69 // In this case, we're using the framework resources. 70 identiferPackage = "android"; 71 } else { 72 PackageManager pm = context.getPackageManager(); 73 try { 74 resources = pm.getResourcesForApplication(statusResPackage); 75 } catch (NameNotFoundException e) { 76 Log.w(TAG, "Contact status update resource package not found: " 77 + statusResPackage); 78 resources = null; 79 } 80 } 81 82 if (resources != null) { 83 final int resId = resources.getIdentifier(statusLabelRes, "string", 84 identiferPackage); 85 if (resId == 0) { 86 Log.w(TAG, "Contact status update resource not found: " + statusLabelRes + 87 " in " + statusResPackage); 88 } else { 89 labelDisplayValue = resources.getString(resId); 90 } 91 } 92 } 93 94 final CharSequence attribution; 95 if (timestampDisplayValue != null && labelDisplayValue != null) { 96 attribution = context.getString( 97 R.string.contact_status_update_attribution_with_date, 98 timestampDisplayValue, labelDisplayValue); 99 } else if (timestampDisplayValue == null && labelDisplayValue != null) { 100 attribution = context.getString( 101 R.string.contact_status_update_attribution, 102 labelDisplayValue); 103 } else if (timestampDisplayValue != null) { 104 attribution = timestampDisplayValue; 105 } else { 106 attribution = null; 107 } 108 return attribution; 109 } 110 111 public static Bitmap loadDefaultAvatarPhoto(Context context, boolean hires, boolean darkTheme) { 112 return BitmapFactory.decodeResource(context.getResources(), 113 ContactPhotoManager.getDefaultAvatarResId(hires, darkTheme)); 114 } 115 } 116