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