1 /*
2  * Copyright (C) 2018 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.calllogutils;
18 
19 import android.content.Context;
20 import android.provider.CallLog.Calls;
21 import android.support.v4.os.BuildCompat;
22 import com.android.dialer.NumberAttributes;
23 import com.android.dialer.calllog.model.CoalescedRow;
24 import com.android.dialer.glidephotomanager.PhotoInfo;
25 import com.android.dialer.spam.Spam;
26 import com.android.dialer.voicemail.model.VoicemailEntry;
27 
28 /** Builds {@link PhotoInfo} from other data types. */
29 public final class PhotoInfoBuilder {
30 
31   /** Returns a {@link PhotoInfo.Builder} with info from {@link CoalescedRow}. */
fromCoalescedRow(Context context, CoalescedRow coalescedRow)32   public static PhotoInfo.Builder fromCoalescedRow(Context context, CoalescedRow coalescedRow) {
33     return fromNumberAttributes(coalescedRow.getNumberAttributes())
34         .setName(CallLogEntryText.buildPrimaryText(context, coalescedRow).toString())
35         .setFormattedNumber(coalescedRow.getFormattedNumber())
36         .setIsVoicemail(coalescedRow.getIsVoicemailCall())
37         .setIsSpam(
38             Spam.shouldShowAsSpam(
39                 coalescedRow.getNumberAttributes().getIsSpam(), coalescedRow.getCallType()))
40         .setIsVideo((coalescedRow.getFeatures() & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO)
41         .setIsRtt(
42             BuildCompat.isAtLeastP()
43                 && (coalescedRow.getFeatures() & Calls.FEATURES_RTT) == Calls.FEATURES_RTT);
44   }
45 
46   /** Returns a {@link PhotoInfo.Builder} with info from {@link VoicemailEntry}. */
fromVoicemailEntry(VoicemailEntry voicemailEntry)47   public static PhotoInfo.Builder fromVoicemailEntry(VoicemailEntry voicemailEntry) {
48     return fromNumberAttributes(voicemailEntry.getNumberAttributes())
49         .setFormattedNumber(voicemailEntry.getFormattedNumber())
50         .setIsSpam(
51             Spam.shouldShowAsSpam(
52                 voicemailEntry.getNumberAttributes().getIsSpam(), voicemailEntry.getCallType()));
53   }
54 
55   /** Returns a {@link PhotoInfo.Builder} with info from {@link NumberAttributes}. */
fromNumberAttributes(NumberAttributes numberAttributes)56   private static PhotoInfo.Builder fromNumberAttributes(NumberAttributes numberAttributes) {
57     return PhotoInfo.newBuilder()
58         .setName(numberAttributes.getName())
59         .setPhotoUri(numberAttributes.getPhotoUri())
60         .setPhotoId(numberAttributes.getPhotoId())
61         .setLookupUri(numberAttributes.getLookupUri())
62         .setIsBusiness(numberAttributes.getIsBusiness())
63         .setIsBlocked(numberAttributes.getIsBlocked());
64   }
65 }
66