Home | History | Annotate | Download | only in listui
      1 /*
      2  * Copyright (C) 2017 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.dialer.voicemail.listui;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import com.android.dialer.calllogutils.CallLogDates;
     22 import com.android.dialer.common.LogUtil;
     23 import com.android.dialer.time.Clock;
     24 import com.android.dialer.voicemail.model.VoicemailEntry;
     25 import java.util.concurrent.TimeUnit;
     26 
     27 /**
     28  * Computes the primary text for voicemail entries.
     29  *
     30  * <p>These text values are shown in the voicemail tab.
     31  */
     32 public class VoicemailEntryText {
     33 
     34   public static String buildPrimaryVoicemailText(Context context, VoicemailEntry data) {
     35     StringBuilder primaryText = new StringBuilder();
     36     if (!TextUtils.isEmpty(data.numberAttributes().getName())) {
     37       primaryText.append(data.numberAttributes().getName());
     38     } else if (!TextUtils.isEmpty(data.formattedNumber())) {
     39       primaryText.append(data.formattedNumber());
     40     } else {
     41       // TODO(uabdullah): Handle CallLog.Calls.PRESENTATION_*, including Verizon restricted numbers.
     42       primaryText.append(context.getText(R.string.voicemail_entry_unknown));
     43     }
     44     return primaryText.toString();
     45   }
     46 
     47   /**
     48    * Uses the new date and location formatting rules to format the location and date in the new
     49    * voicemail tab.
     50    *
     51    * <p>Rules: $Location  Date
     52    *
     53    * <p>Examples:
     54    *
     55    * <p>Jun 20 San Francisco  Now
     56    *
     57    * <p>Markham, ON  Jul 27
     58    *
     59    * <p>Toledo, OH  12:15 PM
     60    *
     61    * <p>Date rules: if < 1 minute ago: "Now"; else if today: HH:MM(am|pm); else if < 3 days: day;
     62    * else: MON D *
     63    *
     64    * @return $Location  Date
     65    */
     66   public static String buildSecondaryVoicemailText(
     67       Context context, Clock clock, VoicemailEntry voicemailEntry) {
     68     return secondaryTextPrefix(context, clock, voicemailEntry);
     69   }
     70 
     71   private static String secondaryTextPrefix(
     72       Context context, Clock clock, VoicemailEntry voicemailEntry) {
     73     StringBuilder secondaryText = new StringBuilder();
     74     String location = voicemailEntry.geocodedLocation();
     75     if (!TextUtils.isEmpty(location)) {
     76       secondaryText.append(location);
     77     }
     78     if (secondaryText.length() > 0) {
     79       secondaryText.append("  ");
     80     }
     81     secondaryText.append(
     82         CallLogDates.newCallLogTimestampLabel(
     83             context, clock.currentTimeMillis(), voicemailEntry.timestamp()));
     84 
     85     long duration = voicemailEntry.duration();
     86     if (duration >= 0) {
     87       secondaryText.append("  ");
     88       String formattedDuration = getVoicemailDuration(context, voicemailEntry);
     89       secondaryText.append(formattedDuration);
     90     }
     91     return secondaryText.toString();
     92   }
     93 
     94   static String getVoicemailDuration(Context context, VoicemailEntry voicemailEntry) {
     95     long minutes = TimeUnit.SECONDS.toMinutes(voicemailEntry.duration());
     96     long seconds = voicemailEntry.duration() - TimeUnit.MINUTES.toSeconds(minutes);
     97 
     98     // The format for duration is "MM:SS" and we never expect the duration to be > 5 minutes
     99     // However an incorrect duration could be set by the framework/someone to be >99, and in that
    100     // case cap it at 99, for the UI to still be able to display it in "MM:SS" format.
    101     if (minutes > 99) {
    102       LogUtil.w(
    103           "VoicemailEntryText.getVoicemailDuration", "Duration was %d", voicemailEntry.duration());
    104       minutes = 99;
    105     }
    106     return context.getString(R.string.voicemailDurationFormat, minutes, seconds);
    107   }
    108 }
    109