summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/contactgrid/TopRow.java
blob: d242c3a14a1352a5253232a5ff9209055664f954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.incallui.contactgrid;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.telephony.PhoneNumberUtils;
import android.text.BidiFormatter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.incallui.call.state.DialerCallState;
import com.android.incallui.incall.protocol.PrimaryCallState;
import com.android.incallui.incall.protocol.PrimaryInfo;
import com.android.incallui.videotech.utils.SessionModificationState;
import com.android.incallui.videotech.utils.VideoUtils;

/**
 * Gets the content of the top row. For example:
 *
 * <ul>
 *   <li>Captain Holt ON HOLD
 *   <li>Calling...
 *   <li>[Wi-Fi icon] Calling via Starbucks Wi-Fi
 *   <li>[Wi-Fi icon] Starbucks Wi-Fi
 *   <li>Call from
 * </ul>
 */
public class TopRow {

  /** Content of the top row. */
  public static class Info {

    @Nullable public final CharSequence label;
    @Nullable public final Drawable icon;
    public final boolean labelIsSingleLine;

    public Info(@Nullable CharSequence label, @Nullable Drawable icon, boolean labelIsSingleLine) {
      this.label = label;
      this.icon = icon;
      this.labelIsSingleLine = labelIsSingleLine;
    }
  }

  private TopRow() {}

  public static Info getInfo(Context context, PrimaryCallState state, PrimaryInfo primaryInfo) {
    CharSequence label = null;
    Drawable icon = state.connectionIcon();
    boolean labelIsSingleLine = true;

    if (state.isWifi() && icon == null) {
      icon = context.getDrawable(R.drawable.quantum_ic_network_wifi_vd_theme_24);
    }

    if (state.state() == DialerCallState.INCOMING
        || state.state() == DialerCallState.CALL_WAITING) {
      // Call from
      // [Wi-Fi icon] Video call from
      // Hey Jake, pick up!
      if (!TextUtils.isEmpty(state.callSubject())) {
        label = state.callSubject();
        labelIsSingleLine = false;
      } else {
        label = getLabelForIncoming(context, state);
        // Show phone number if it's not displayed in name (center row) or location field (bottom
        // row).
        if (shouldShowNumber(primaryInfo, true /* isIncoming */)) {
          label = TextUtils.concat(label, " ", spanDisplayNumber(primaryInfo.number()));
        }
      }
    } else if (VideoUtils.hasSentVideoUpgradeRequest(state.sessionModificationState())
        || VideoUtils.hasReceivedVideoUpgradeRequest(state.sessionModificationState())) {
      label = getLabelForVideoRequest(context, state);
    } else if (state.state() == DialerCallState.PULLING) {
      label = context.getString(R.string.incall_transferring);
    } else if (state.state() == DialerCallState.DIALING
        || state.state() == DialerCallState.CONNECTING) {
      // [Wi-Fi icon] Calling via Google Guest
      // Calling...
      label = getLabelForDialing(context, state);
    } else if (state.state() == DialerCallState.ACTIVE && state.isRemotelyHeld()) {
      label = context.getString(R.string.incall_remotely_held);
    } else if (state.state() == DialerCallState.ACTIVE
        && shouldShowNumber(primaryInfo, false /* isIncoming */)) {
      label = spanDisplayNumber(primaryInfo.number());
    } else if (state.state() == DialerCallState.CALL_PENDING
        && !TextUtils.isEmpty(state.customLabel())) {
      label = state.customLabel();
    } else {
      // Video calling...
      // [Wi-Fi icon] Starbucks Wi-Fi
      label = getConnectionLabel(state);
    }

    return new Info(label, icon, labelIsSingleLine);
  }

  private static CharSequence spanDisplayNumber(String displayNumber) {
    return PhoneNumberUtils.createTtsSpannable(
        BidiFormatter.getInstance().unicodeWrap(displayNumber, TextDirectionHeuristics.LTR));
  }

  private static boolean shouldShowNumber(PrimaryInfo primaryInfo, boolean isIncoming) {
    if (primaryInfo.nameIsNumber()) {
      return false;
    }
    // Don't show number since it's already shown in bottom row of incoming screen if there is no
    // location info.
    if (primaryInfo.location() == null && isIncoming) {
      return false;
    }
    if (primaryInfo.isLocalContact() && !isIncoming) {
      return false;
    }
    if (TextUtils.isEmpty(primaryInfo.number())) {
      return false;
    }
    return true;
  }

  private static CharSequence getLabelForIncoming(Context context, PrimaryCallState state) {
    if (state.isVideoCall()) {
      return getLabelForIncomingVideo(context, state.sessionModificationState(), state.isWifi());
    } else if (state.isWifi() && !TextUtils.isEmpty(state.connectionLabel())) {
      return state.connectionLabel();
    } else if (isAccount(state)) {
      return getColoredConnectionLabel(context, state);
    } else if (state.isWorkCall()) {
      return context.getString(R.string.contact_grid_incoming_work_call);
    } else {
      return context.getString(R.string.contact_grid_incoming_voice_call);
    }
  }

  private static Spannable getColoredConnectionLabel(Context context, PrimaryCallState state) {
    Assert.isNotNull(state.connectionLabel());
    String label =
        context.getString(R.string.contact_grid_incoming_via_template, state.connectionLabel());
    Spannable spannable = new SpannableString(label);

    int start = label.indexOf(state.connectionLabel());
    int end = start + state.connectionLabel().length();
    spannable.setSpan(
        new ForegroundColorSpan(state.primaryColor()),
        start,
        end,
        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return spannable;
  }

  private static CharSequence getLabelForIncomingVideo(
      Context context, @SessionModificationState int sessionModificationState, boolean isWifi) {
    if (sessionModificationState == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
      if (isWifi) {
        return context.getString(R.string.contact_grid_incoming_wifi_video_call);
      } else {
        return context.getString(R.string.contact_grid_incoming_video_call);
      }
    } else {
      if (isWifi) {
        return context.getString(R.string.contact_grid_incoming_wifi_video_call);
      } else {
        return context.getString(R.string.contact_grid_incoming_video_call);
      }
    }
  }

  private static CharSequence getLabelForDialing(Context context, PrimaryCallState state) {
    if (!TextUtils.isEmpty(state.connectionLabel()) && !state.isWifi()) {
      CharSequence label = getCallingViaLabel(context, state);

      if (state.isAssistedDialed() && state.assistedDialingExtras() != null) {
        LogUtil.i("TopRow.getLabelForDialing", "using assisted dialing with via label.");
        String countryCode =
            String.valueOf(state.assistedDialingExtras().transformedNumberCountryCallingCode());
        label =
            TextUtils.concat(
                label,
                " • ",
                context.getString(
                    R.string.incall_connecting_assited_dialed_component,
                    countryCode,
                    state.assistedDialingExtras().userHomeCountryCode()));
      }
      return label;
    } else {
      if (state.isVideoCall()) {
        if (state.isWifi()) {
          return context.getString(R.string.incall_wifi_video_call_requesting);
        } else {
          return context.getString(R.string.incall_video_call_requesting);
        }
      }

      if (state.isAssistedDialed() && state.assistedDialingExtras() != null) {
        LogUtil.i("TopRow.getLabelForDialing", "using assisted dialing label.");
        String countryCode =
            String.valueOf(state.assistedDialingExtras().transformedNumberCountryCallingCode());
        return context.getString(
            R.string.incall_connecting_assited_dialed,
            countryCode,
            state.assistedDialingExtras().userHomeCountryCode());
      }
      return context.getString(R.string.incall_connecting);
    }
  }

  private static CharSequence getCallingViaLabel(Context context, PrimaryCallState state) {
    if (state.simSuggestionReason() != null) {
      switch (state.simSuggestionReason()) {
        case FREQUENT:
          return context.getString(
              R.string.incall_calling_on_recent_choice_template, state.connectionLabel());
        case INTRA_CARRIER:
          return context.getString(
              R.string.incall_calling_on_same_carrier_template, state.connectionLabel());
        default:
          break;
      }
    }
    return context.getString(R.string.incall_calling_via_template, state.connectionLabel());
  }

  private static CharSequence getConnectionLabel(PrimaryCallState state) {
    if (!TextUtils.isEmpty(state.connectionLabel())
        && (isAccount(state) || state.isWifi() || state.isConference())) {
      // We normally don't show a "call state label" at all when active
      // (but we can use the call state label to display the provider name).
      return state.connectionLabel();
    } else {
      return null;
    }
  }

  private static CharSequence getLabelForVideoRequest(Context context, PrimaryCallState state) {
    switch (state.sessionModificationState()) {
      case SessionModificationState.WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE:
        return context.getString(R.string.incall_video_call_upgrade_request);
      case SessionModificationState.REQUEST_FAILED:
      case SessionModificationState.UPGRADE_TO_VIDEO_REQUEST_FAILED:
        return context.getString(R.string.incall_video_call_request_failed);
      case SessionModificationState.REQUEST_REJECTED:
        return context.getString(R.string.incall_video_call_request_rejected);
      case SessionModificationState.UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT:
        return context.getString(R.string.incall_video_call_request_timed_out);
      case SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST:
        return getLabelForIncomingVideo(context, state.sessionModificationState(), state.isWifi());
      case SessionModificationState.NO_REQUEST:
      default:
        Assert.fail();
        return null;
    }
  }

  private static boolean isAccount(PrimaryCallState state) {
    return !TextUtils.isEmpty(state.connectionLabel()) && TextUtils.isEmpty(state.gatewayNumber());
  }
}