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.dialer.app.voicemail.error;
18
19import android.content.Context;
20import android.support.annotation.VisibleForTesting;
21import android.text.util.Linkify;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.TextView;
25import com.android.dialer.app.alert.AlertManager;
26import com.android.dialer.app.voicemail.error.VoicemailErrorMessage.Action;
27import com.android.dialer.common.Assert;
28import com.android.dialer.common.LogUtil;
29import java.util.List;
30
31/**
32 * UI for the voicemail error message, which will be inserted to the top of the voicemail tab if any
33 * occurred.
34 */
35public class VoicemailErrorAlert {
36
37  private final Context context;
38  private final AlertManager alertManager;
39  private final VoicemailErrorMessageCreator messageCreator;
40
41  private final View view;
42  private final TextView header;
43  private final TextView details;
44  private final TextView primaryAction;
45  private final TextView secondaryAction;
46  private final TextView primaryActionRaised;
47  private final TextView secondaryActionRaised;
48  private final AlertManager modalAlertManager;
49  private View modalView;
50
51  public VoicemailErrorAlert(
52      Context context,
53      AlertManager alertManager,
54      AlertManager modalAlertManager,
55      VoicemailErrorMessageCreator messageCreator) {
56    this.context = context;
57    this.alertManager = alertManager;
58    this.modalAlertManager = modalAlertManager;
59    this.messageCreator = messageCreator;
60
61    view = alertManager.inflate(R.layout.voicemail_error_message_fragment);
62    header = (TextView) view.findViewById(R.id.error_card_header);
63    details = (TextView) view.findViewById(R.id.error_card_details);
64    primaryAction = (TextView) view.findViewById(R.id.primary_action);
65    secondaryAction = (TextView) view.findViewById(R.id.secondary_action);
66    primaryActionRaised = (TextView) view.findViewById(R.id.primary_action_raised);
67    secondaryActionRaised = (TextView) view.findViewById(R.id.secondary_action_raised);
68  }
69
70  public void updateStatus(List<VoicemailStatus> statuses, VoicemailStatusReader statusReader) {
71    LogUtil.i("VoicemailErrorAlert.updateStatus", "%d status", statuses.size());
72    VoicemailErrorMessage message = null;
73    view.setVisibility(View.VISIBLE);
74    for (VoicemailStatus status : statuses) {
75      message = messageCreator.create(context, status, statusReader);
76      if (message != null) {
77        break;
78      }
79    }
80
81    alertManager.clear();
82    modalAlertManager.clear();
83    if (message != null) {
84      LogUtil.i(
85          "VoicemailErrorAlert.updateStatus",
86          "isModal: %b, %s",
87          message.isModal(),
88          message.getTitle());
89      if (message.isModal()) {
90        if (message instanceof VoicemailTosMessage) {
91          modalView = getTosView(modalAlertManager, (VoicemailTosMessage) message);
92        } else {
93          throw new IllegalArgumentException("Modal message type is undefined!");
94        }
95        modalAlertManager.add(modalView);
96      } else {
97        loadMessage(message);
98        alertManager.add(view);
99      }
100    }
101  }
102
103  @VisibleForTesting
104  public View getView() {
105    return view;
106  }
107
108  @VisibleForTesting
109  public View getModalView() {
110    return modalView;
111  }
112
113  void loadMessage(VoicemailErrorMessage message) {
114    header.setText(message.getTitle());
115    details.setText(message.getDescription());
116    bindActions(message);
117  }
118
119  private View getTosView(AlertManager alertManager, VoicemailTosMessage message) {
120    View view = alertManager.inflate(R.layout.voicemail_tos_fragment);
121    TextView tosTitle = (TextView) view.findViewById(R.id.tos_message_title);
122    tosTitle.setText(message.getTitle());
123    TextView tosDetails = (TextView) view.findViewById(R.id.tos_message_details);
124    tosDetails.setAutoLinkMask(Linkify.WEB_URLS);
125    tosDetails.setText(message.getDescription());
126
127    Assert.checkArgument(message.getActions().size() == 2);
128    Action primaryAction = message.getActions().get(0);
129    TextView primaryButton = (TextView) view.findViewById(R.id.voicemail_tos_button_decline);
130    primaryButton.setText(primaryAction.getText());
131    primaryButton.setOnClickListener(primaryAction.getListener());
132    Action secondaryAction = message.getActions().get(1);
133    TextView secondaryButton = (TextView) view.findViewById(R.id.voicemail_tos_button_accept);
134    secondaryButton.setText(secondaryAction.getText());
135    secondaryButton.setOnClickListener(secondaryAction.getListener());
136
137    if (message.getImageResourceId() != null) {
138      ImageView voicemailTosImage = (ImageView) view.findViewById(R.id.voicemail_image);
139      voicemailTosImage.setImageResource(message.getImageResourceId());
140      voicemailTosImage.setVisibility(View.VISIBLE);
141    }
142
143    return view;
144  }
145
146  /**
147   * Attach actions to buttons until all buttons are assigned. If there are not enough actions the
148   * rest of the buttons will be removed. If there are more actions then buttons the extra actions
149   * will be dropped. {@link VoicemailErrorMessage#getActions()} will specify what actions should be
150   * shown and in what order.
151   */
152  private void bindActions(VoicemailErrorMessage message) {
153    TextView[] buttons = new TextView[] {primaryAction, secondaryAction};
154    TextView[] raisedButtons = new TextView[] {primaryActionRaised, secondaryActionRaised};
155    for (int i = 0; i < buttons.length; i++) {
156      if (message.getActions() != null && i < message.getActions().size()) {
157        VoicemailErrorMessage.Action action = message.getActions().get(i);
158        TextView button;
159        if (action.isRaised()) {
160          button = raisedButtons[i];
161          buttons[i].setVisibility(View.GONE);
162        } else {
163          button = buttons[i];
164          raisedButtons[i].setVisibility(View.GONE);
165        }
166        button.setText(action.getText());
167        button.setOnClickListener(action.getListener());
168        button.setVisibility(View.VISIBLE);
169      } else {
170        buttons[i].setVisibility(View.GONE);
171        raisedButtons[i].setVisibility(View.GONE);
172      }
173    }
174  }
175}
176