1 /*
2  * Copyright (C) 2016 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.incallui.contactgrid;
18 
19 import android.content.Context;
20 import android.support.annotation.Nullable;
21 import android.text.TextUtils;
22 import com.android.incallui.call.state.DialerCallState;
23 import com.android.incallui.incall.protocol.PrimaryCallState;
24 import com.android.incallui.incall.protocol.PrimaryInfo;
25 
26 /**
27  * Gets the content of the bottom row. For example:
28  *
29  * <ul>
30  *   <li>Mobile +1 (650) 253-0000
31  *   <li>[HD attempting icon]/[HD icon] 00:15
32  *   <li>Call ended
33  *   <li>Hanging up
34  * </ul>
35  */
36 public class BottomRow {
37 
38   /** Content of the bottom row. */
39   public static class Info {
40 
41     @Nullable public final CharSequence label;
42     public final boolean isTimerVisible;
43     public final boolean isWorkIconVisible;
44     public final boolean isHdAttemptingIconVisible;
45     public final boolean isHdIconVisible;
46     public final boolean isForwardIconVisible;
47     public final boolean isSpamIconVisible;
48     public final boolean shouldPopulateAccessibilityEvent;
49 
Info( @ullable CharSequence label, boolean isTimerVisible, boolean isWorkIconVisible, boolean isHdAttemptingIconVisible, boolean isHdIconVisible, boolean isForwardIconVisible, boolean isSpamIconVisible, boolean shouldPopulateAccessibilityEvent)50     public Info(
51         @Nullable CharSequence label,
52         boolean isTimerVisible,
53         boolean isWorkIconVisible,
54         boolean isHdAttemptingIconVisible,
55         boolean isHdIconVisible,
56         boolean isForwardIconVisible,
57         boolean isSpamIconVisible,
58         boolean shouldPopulateAccessibilityEvent) {
59       this.label = label;
60       this.isTimerVisible = isTimerVisible;
61       this.isWorkIconVisible = isWorkIconVisible;
62       this.isHdAttemptingIconVisible = isHdAttemptingIconVisible;
63       this.isHdIconVisible = isHdIconVisible;
64       this.isForwardIconVisible = isForwardIconVisible;
65       this.isSpamIconVisible = isSpamIconVisible;
66       this.shouldPopulateAccessibilityEvent = shouldPopulateAccessibilityEvent;
67     }
68   }
69 
BottomRow()70   private BottomRow() {}
71 
getInfo(Context context, PrimaryCallState state, PrimaryInfo primaryInfo)72   public static Info getInfo(Context context, PrimaryCallState state, PrimaryInfo primaryInfo) {
73     CharSequence label;
74     boolean isTimerVisible = state.state() == DialerCallState.ACTIVE;
75     boolean isForwardIconVisible = state.isForwardedNumber();
76     boolean isWorkIconVisible = state.isWorkCall();
77     boolean isHdIconVisible = state.isHdAudioCall() && !isForwardIconVisible;
78     boolean isHdAttemptingIconVisible = state.isHdAttempting();
79     boolean isSpamIconVisible = false;
80     boolean shouldPopulateAccessibilityEvent = true;
81 
82     if (isIncoming(state) && primaryInfo.isSpam()) {
83       label = context.getString(R.string.contact_grid_incoming_suspected_spam);
84       isSpamIconVisible = true;
85       isHdIconVisible = false;
86     } else if (state.state() == DialerCallState.DISCONNECTING) {
87       // While in the DISCONNECTING state we display a "Hanging up" message in order to make the UI
88       // feel more responsive.  (In GSM it's normal to see a delay of a couple of seconds while
89       // negotiating the disconnect with the network, so the "Hanging up" state at least lets the
90       // user know that we're doing something.  This state is currently not used with CDMA.)
91       label = context.getString(R.string.incall_hanging_up);
92     } else if (state.state() == DialerCallState.DISCONNECTED) {
93       label = state.disconnectCause().getLabel();
94       if (TextUtils.isEmpty(label)) {
95         label = context.getString(R.string.incall_call_ended);
96       }
97     } else {
98       label = getLabelForPhoneNumber(primaryInfo);
99       shouldPopulateAccessibilityEvent = primaryInfo.nameIsNumber();
100     }
101 
102     return new Info(
103         label,
104         isTimerVisible,
105         isWorkIconVisible,
106         isHdAttemptingIconVisible,
107         isHdIconVisible,
108         isForwardIconVisible,
109         isSpamIconVisible,
110         shouldPopulateAccessibilityEvent);
111   }
112 
getLabelForPhoneNumber(PrimaryInfo primaryInfo)113   private static CharSequence getLabelForPhoneNumber(PrimaryInfo primaryInfo) {
114     if (primaryInfo.location() != null) {
115       return primaryInfo.location();
116     }
117     if (!primaryInfo.nameIsNumber() && !TextUtils.isEmpty(primaryInfo.number())) {
118       if (primaryInfo.label() == null) {
119         return primaryInfo.number();
120       } else {
121         return TextUtils.concat(primaryInfo.label(), " ", primaryInfo.number());
122       }
123     }
124     return null;
125   }
126 
isIncoming(PrimaryCallState state)127   private static boolean isIncoming(PrimaryCallState state) {
128     return state.state() == DialerCallState.INCOMING
129         || state.state() == DialerCallState.CALL_WAITING;
130   }
131 }
132