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