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
17package com.android.incallui.contactgrid;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.support.annotation.Nullable;
22import android.text.TextUtils;
23import com.android.dialer.common.Assert;
24import com.android.incallui.call.DialerCall.State;
25import com.android.incallui.incall.protocol.PrimaryCallState;
26import com.android.incallui.videotech.utils.SessionModificationState;
27import com.android.incallui.videotech.utils.VideoUtils;
28
29/**
30 * Gets the content of the top row. For example:
31 *
32 * <ul>
33 *   <li>Captain Holt ON HOLD
34 *   <li>Calling...
35 *   <li>[Wi-Fi icon] Calling via Starbucks Wi-Fi
36 *   <li>[Wi-Fi icon] Starbucks Wi-Fi
37 *   <li>Call from
38 * </ul>
39 */
40public class TopRow {
41
42  /** Content of the top row. */
43  public static class Info {
44
45    @Nullable public final CharSequence label;
46    @Nullable public final Drawable icon;
47    public final boolean labelIsSingleLine;
48
49    public Info(@Nullable CharSequence label, @Nullable Drawable icon, boolean labelIsSingleLine) {
50      this.label = label;
51      this.icon = icon;
52      this.labelIsSingleLine = labelIsSingleLine;
53    }
54  }
55
56  private TopRow() {}
57
58  public static Info getInfo(Context context, PrimaryCallState state) {
59    CharSequence label = null;
60    Drawable icon = state.connectionIcon;
61    boolean labelIsSingleLine = true;
62
63    if (state.isWifi && icon == null) {
64      icon = context.getDrawable(R.drawable.quantum_ic_network_wifi_vd_theme_24);
65    }
66
67    if (state.state == State.INCOMING || state.state == State.CALL_WAITING) {
68      // Call from
69      // [Wi-Fi icon] Video call from
70      // Hey Jake, pick up!
71      if (!TextUtils.isEmpty(state.callSubject)) {
72        label = state.callSubject;
73        labelIsSingleLine = false;
74      } else {
75        label = getLabelForIncoming(context, state);
76      }
77    } else if (VideoUtils.hasSentVideoUpgradeRequest(state.sessionModificationState)
78        || VideoUtils.hasReceivedVideoUpgradeRequest(state.sessionModificationState)) {
79      label = getLabelForVideoRequest(context, state);
80    } else if (state.state == State.PULLING) {
81      label = context.getString(R.string.incall_transferring);
82    } else if (state.state == State.DIALING || state.state == State.CONNECTING) {
83      // [Wi-Fi icon] Calling via Google Guest
84      // Calling...
85      label = getLabelForDialing(context, state);
86    } else if (state.state == State.ACTIVE && state.isRemotelyHeld) {
87      label = context.getString(R.string.incall_remotely_held);
88    } else {
89      // Video calling...
90      // [Wi-Fi icon] Starbucks Wi-Fi
91      label = getConnectionLabel(state);
92    }
93
94    return new Info(label, icon, labelIsSingleLine);
95  }
96
97  private static CharSequence getLabelForIncoming(Context context, PrimaryCallState state) {
98    if (state.isVideoCall) {
99      return getLabelForIncomingVideo(context, state.isWifi);
100    } else if (state.isWifi && !TextUtils.isEmpty(state.connectionLabel)) {
101      return state.connectionLabel;
102    } else if (isAccount(state)) {
103      return context.getString(R.string.contact_grid_incoming_via_template, state.connectionLabel);
104    } else if (state.isWorkCall) {
105      return context.getString(R.string.contact_grid_incoming_work_call);
106    } else {
107      return context.getString(R.string.contact_grid_incoming_voice_call);
108    }
109  }
110
111  private static CharSequence getLabelForIncomingVideo(Context context, boolean isWifi) {
112    if (isWifi) {
113      return context.getString(R.string.contact_grid_incoming_wifi_video_call);
114    } else {
115      return context.getString(R.string.contact_grid_incoming_video_call);
116    }
117  }
118
119  private static CharSequence getLabelForDialing(Context context, PrimaryCallState state) {
120    if (!TextUtils.isEmpty(state.connectionLabel) && !state.isWifi) {
121      return context.getString(R.string.incall_calling_via_template, state.connectionLabel);
122    } else {
123      if (state.isVideoCall) {
124        if (state.isWifi) {
125          return context.getString(R.string.incall_wifi_video_call_requesting);
126        } else {
127          return context.getString(R.string.incall_video_call_requesting);
128        }
129      }
130      return context.getString(R.string.incall_connecting);
131    }
132  }
133
134  private static CharSequence getConnectionLabel(PrimaryCallState state) {
135    if (!TextUtils.isEmpty(state.connectionLabel)
136        && (isAccount(state) || state.isWifi || state.isConference)) {
137      // We normally don't show a "call state label" at all when active
138      // (but we can use the call state label to display the provider name).
139      return state.connectionLabel;
140    } else {
141      return null;
142    }
143  }
144
145  private static CharSequence getLabelForVideoRequest(Context context, PrimaryCallState state) {
146    switch (state.sessionModificationState) {
147      case SessionModificationState.WAITING_FOR_UPGRADE_TO_VIDEO_RESPONSE:
148        return context.getString(R.string.incall_video_call_requesting);
149      case SessionModificationState.REQUEST_FAILED:
150      case SessionModificationState.UPGRADE_TO_VIDEO_REQUEST_FAILED:
151        return context.getString(R.string.incall_video_call_request_failed);
152      case SessionModificationState.REQUEST_REJECTED:
153        return context.getString(R.string.incall_video_call_request_rejected);
154      case SessionModificationState.UPGRADE_TO_VIDEO_REQUEST_TIMED_OUT:
155        return context.getString(R.string.incall_video_call_request_timed_out);
156      case SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST:
157        return getLabelForIncomingVideo(context, state.isWifi);
158      case SessionModificationState.NO_REQUEST:
159      default:
160        Assert.fail();
161        return null;
162    }
163  }
164
165  private static boolean isAccount(PrimaryCallState state) {
166    return !TextUtils.isEmpty(state.connectionLabel) && TextUtils.isEmpty(state.gatewayNumber);
167  }
168}
169