1/*
2 * Copyright (C) 2014 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.phone.settings;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.ProgressDialog;
22import android.view.WindowManager;
23
24import com.android.phone.R;
25
26public class VoicemailDialogUtil {
27
28    // Voicemail dialog identifiers.
29    public static final int VM_NOCHANGE_ERROR_DIALOG = 400;
30    public static final int VM_RESPONSE_ERROR_DIALOG = 500;
31    public static final int FWD_SET_RESPONSE_ERROR_DIALOG = 501;
32    public static final int FWD_GET_RESPONSE_ERROR_DIALOG = 502;
33    public static final int VM_CONFIRM_DIALOG = 600;
34    public static final int VM_FWD_SAVING_DIALOG = 601;
35    public static final int VM_FWD_READING_DIALOG = 602;
36    public static final int VM_REVERTING_DIALOG = 603;
37    public static final int TTY_SET_RESPONSE_ERROR = 800;
38
39    public static Dialog getDialog(VoicemailSettingsActivity parent, int id) {
40        if ((id == VM_RESPONSE_ERROR_DIALOG) || (id == VM_NOCHANGE_ERROR_DIALOG) ||
41            (id == FWD_SET_RESPONSE_ERROR_DIALOG) || (id == FWD_GET_RESPONSE_ERROR_DIALOG) ||
42                (id == VM_CONFIRM_DIALOG) || (id == TTY_SET_RESPONSE_ERROR)) {
43
44            AlertDialog.Builder b = new AlertDialog.Builder(parent);
45
46            int msgId;
47            int titleId = R.string.error_updating_title;
48            switch (id) {
49                case VM_CONFIRM_DIALOG:
50                    msgId = R.string.vm_changed;
51                    titleId = R.string.voicemail;
52                    // Set Button 2
53                    b.setNegativeButton(R.string.close_dialog, parent);
54                    break;
55                case VM_NOCHANGE_ERROR_DIALOG:
56                    // even though this is technically an error,
57                    // keep the title friendly.
58                    msgId = R.string.no_change;
59                    titleId = R.string.voicemail;
60                    // Set Button 2
61                    b.setNegativeButton(R.string.close_dialog, parent);
62                    break;
63                case VM_RESPONSE_ERROR_DIALOG:
64                    msgId = R.string.vm_change_failed;
65                    // Set Button 1
66                    b.setPositiveButton(R.string.close_dialog, parent);
67                    break;
68                case FWD_SET_RESPONSE_ERROR_DIALOG:
69                    msgId = R.string.fw_change_failed;
70                    // Set Button 1
71                    b.setPositiveButton(R.string.close_dialog, parent);
72                    break;
73                case FWD_GET_RESPONSE_ERROR_DIALOG:
74                    msgId = R.string.fw_get_in_vm_failed;
75                    b.setPositiveButton(R.string.alert_dialog_yes, parent);
76                    b.setNegativeButton(R.string.alert_dialog_no, parent);
77                    break;
78                case TTY_SET_RESPONSE_ERROR:
79                    titleId = R.string.tty_mode_option_title;
80                    msgId = R.string.tty_mode_not_allowed_video_call;
81                    b.setIconAttribute(android.R.attr.alertDialogIcon);
82                    b.setPositiveButton(R.string.ok, parent);
83                    break;
84                default:
85                    msgId = R.string.exception_error;
86                    // Set Button 3, tells the activity that the error is
87                    // not recoverable on dialog exit.
88                    b.setNeutralButton(R.string.close_dialog, parent);
89                    break;
90            }
91
92            b.setTitle(parent.getText(titleId));
93            String message = parent.getText(msgId).toString();
94            b.setMessage(message);
95            b.setCancelable(false);
96            AlertDialog dialog = b.create();
97
98            // make the dialog more obvious by bluring the background.
99            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
100
101            return dialog;
102        } else if (id == VM_FWD_SAVING_DIALOG || id == VM_FWD_READING_DIALOG ||
103                id == VM_REVERTING_DIALOG) {
104            ProgressDialog dialog = new ProgressDialog(parent);
105            dialog.setTitle(parent.getText(R.string.call_settings));
106            dialog.setIndeterminate(true);
107            dialog.setCancelable(false);
108            dialog.setMessage(parent.getText(
109                    id == VM_FWD_SAVING_DIALOG ? R.string.updating_settings :
110                    (id == VM_REVERTING_DIALOG ? R.string.reverting_settings :
111                    R.string.reading_settings)));
112            return dialog;
113        }
114
115        return null;
116    }
117}
118